Archived OpenModelica forums. Posting is disabled.

Alternative forums include GitHub discussions or StackOverflow (make sure to read the Stack Overflow rules; you need to have well-formed questions)


Forgot password? | Forgot username? | Register

for loop in openmodelica

for loop in openmodelica

Hello,

    Hello, currently I work on the modeling of a PVT with OpenModelica, now I am creating the block MIMO which I will model the equations of the current source that exists in a Matlab file and I would like to convert them into openModelica but I have a problem with the use of the loop (for), I will give you the instructions that I want to translate to openModelica
can you help me, please?
------
(Tac and G are the inputs)
I=0
Iph=0
for i in 1:5 loop

io11 = (Icc / (exp(q * vo / (K * Tr)) - 1)) * (Tac / Tr) ^ 3 * exp(q * Eg * (1 / Tr - 1 / Tac) / K);
    io22 = (Icc / (exp(q * vo / (2 * K * Tr)) - 1)) * (Tac / Tr) ^ 3 * exp(q * Eg * (1 / Tr - 1 / Tac) / (2 * K));
   C = (v + Rs * I) / Rsh;
  I = ((Icc + ki * (Tac - Tr)) * G / Gr) - (exp(q * (v + Rs * I) / (K * Tac)) - 1) * io11 - (exp(q * (v + Rs * I) / (2 * K * Tac)) - 1) * io22 - ((v + Rs * I) / Rsh);
    Iph = (Icc + ki * (Tac - Tr)) * G / Gr;
    Relec = Ro * (1 - b * (Tac - 273 - Tref));
  end for;

end for

Re: for loop in openmodelica

You will want to use an algorithm section instead of an equation: a loop such as:

Code:

for i in 1:2 loop

  i = i + 1;
end for;

Is expanded into:

Code:

1 = 1 + 1;

2 = 2 + 1;

In an algorithm section the values are kept during the loop (note that I and Iph also need to be part of the same algorithm section or you will have double assignments):

Code:

for i in 1:2 loop

  i := i + 1;
end for;

Re: for loop in openmodelica

Hello
thank you sjoelund for your answer;

   I understand now how for loop works in OpenModelica, and you are right for your answer (note that I and Iph also need to be part of the same algorithm section or you will have double assignments), but in my program,  I still not understand how can I use the integer (i) because (Icc, q, K,  Tr, Eg) are the known parameters  but Tac and G are the inputs.
I want to calculate (io11, io22, Iph, I, Relec) at each loop and these parameters are dependently between them.
please, can you help me?

parameter Real np=3;
I := 0;
  Iph := 0;
  for i in 1:np loop
    io11 := io11 + (Icc / (exp(q * ((36 - 0.32 * (Tac - 224)) / 60) / (K * Tr)) - 1)) * (Tac / Tr) ^ 3 * exp(q * Eg * (1 / Tr - 1 / Tac) / K);
    io22 := io22 + (Icc / (exp(q * ((36 - 0.32 * (Tac - 273 - 49)) / 60) / (2 * K * Tr)) - 1)) * (Tac / Tr) ^ 3 * exp(q * Eg * (1 / Tr - 1 / Tac) / (2 * K));
    I := I + ((Icc + ki * (Tac - Tr)) * G / Gr) - (exp(q * ((0.5 * (1 - 0.0028 * (Tac - 273 - 25))) + Rs * I) / (K * Tac)) - 1) * io11 - (exp(q * ((0.5 * (1 - 0.0028 * (Tac - 273 - 25))) +     Rs * I) / (2 * K * Tac)) - 1) * io22 - (((0.5 * (1 - 0.0028 * (Tac - 273 - 25))) + Rs * I) / Rsh);
    Iph := Iph + (Icc + ki * (Tac - Tr)) * G / Gr;
    Relec := Relec + Ro * (1 - b * (Tac - 273 - Tref));
  end for;
end pvcurrent;

Re: for loop in openmodelica

So you mean that you want to save io11 for each iteration of the array? That you could do also with a for equation and using an array for each iteration index:

Code:


  Real io11[np];
equation
  io11[1] = 0;
  for i in 2:np loop
    io11[i] = io11[i-1] + 1;
  end for;

Re: for loop in openmodelica

Hello,
   thank you sjoelund so much for your answer, it was helpful
    weyy, thank you very much that's nice of you, I applied what you told me and it works very well, but I have another problem where I would like to connect between two multi-input multi-output blocks, so how will I connect the output (i) of block A to the input (j) of block B
my thank 

Re: for loop in openmodelica

Hello,

  how will I connect the output (i) of block A to the input (j) of block B
can you help me, please?

Edited by: Abderezakabidi - Apr-26-19 07:38:02
There are 0 guests and 0 other users also viewing this topic