0

If i have a non-modelica function which has no input and no output like

void foo(void)
{
  variable1;
  variable2;
  for loop
   {
     if conditions
   }
  variable2=foobar(); // another function call, foobar() is not modelica function

}

then can i model it like below in modelica ?

model foo
 variable1;
 variable2;

algorithm
for loop
   {
     if conditions
   }
 variable2 :=foobar(); //foobar here is modelica function
end foo;
shilu
  • 135
  • 1
  • 6

2 Answers2

3

Actually, your description is a bit troubling. If your function has no inputs and no outputs, what is the point of calling it?

I'm guessing your function has side effects. But if you are calling functions with side effects from Modelica, you need to be extremely careful because, generally speaking, you do not have control over when they are called. For example, they will be called for so-called "candidate solutions" as well as actual simulation steps.

So it would be best if you explained a bit more about what this function does. Not only will that help to decide how you could express it in Modelica, it may also demonstrate that your function is best left as C code and called through the foreign function interface in Modelica instead.

Michael Tiller
  • 9,291
  • 3
  • 26
  • 41
  • Yes. function has side effects as it changes the value of global variable. i was wondering what is the best way to model such function. Passing it as a parameter to the function ? – shilu Nov 22 '13 at 11:04
  • 2
    If the function has side effects, you should qualify it with the keyword "impure" to let the Modelica solvers know that it has side effects. – Michael Tiller Nov 23 '13 at 21:33
1

You might want to have a quick look at the nice cheat sheet http://modref.xogeny.com/ to find out how to do for-loops. Also when inside algorithm sections you must use assignments instead of equations:

algorithm
...
variable2 := foobar(); //foobar here is modelica function
Dietmar Winkler
  • 942
  • 5
  • 11