CIFASIS / modelicacc

Modelica C Compiler implemented in C++ to develop and test novel algorithms for large scale models.
GNU General Public License v3.0
11 stars 9 forks source link

Add `for loops` to for equations in generated models. #39

Closed joaquinffernandez closed 3 years ago

joaquinffernandez commented 3 years ago

These are the equations generated for the RC model, the same happens for RecursiveRC and Network2D:

equation
  G_p_v = 0;
  R_T_heatPort[_Index_0] = R_T_ref[_Index_0];
  R_v[_Index_0] = R_p_v[_Index_0]-R_n_v[_Index_0];
  0 = R_p_i[_Index_0]+R_n_i[_Index_0];
  R_i[_Index_0] = R_p_i[_Index_0];
  R_R_actual[_Index_0] = R_R[_Index_0]*(1+R_alpha[_Index_0]*(R_T_heatPort[_Index_0]-R_T_ref[_Index_0]));
  R_v[_Index_0] = R_R_actual[_Index_0]*R_i[_Index_0];
  R_LossPower[_Index_0] = R_v[_Index_0]*R_i[_Index_0];
  C_v[_Index_1] = C_p_v[_Index_1]-C_n_v[_Index_1];
  0 = C_p_i[_Index_1]+C_n_i[_Index_1];
  C_i[_Index_1] = C_p_i[_Index_1];
  C_i[_Index_1] = C_C[_Index_1]*der(C_v[_Index_1]);
  S_v = S_p_v-S_n_v;
  0 = S_p_i+S_n_i;
  S_i = S_p_i;
  S_v = S_V;
  for i in 1:1:1 loop
    G_p_v = S_n_v;
  end for;
  for i in 1:1:4 loop
    C_n[i] = S_n;
  end for;
  for i in 1:1:1 loop
    S_n_i+sum(C_n_i[1:1:4])+G_p_i = 0;
  end for;
  for i in 4:1:4 loop
    C_p[i] = R_n[1*i+0];
  end for;
  for i in 4:1:4 loop
    C_p_i[1*i+0]+R_n_i[1*i+0] = 0;
  end for;
  for i in 1:1:3 loop
    C_p[i] = R_p[1*i+1];
  end for;
  for i in 1:1:3 loop
    R_n[i] = R_p[1*i+1];
  end for;
  for i in 2:1:4 loop
    R_n_i[1*i+-1]+R_p_i[1*i+0]+C_p_i[1*i+-1] = 0;
  end for;
  for i in 1:1:1 loop
    R_p[i] = S_p;
  end for;
  for i in 1:1:1 loop
    S_p_i+R_p_i[1*i+0] = 0;
  end for;

The connectequations are correct, but the model equations doesn't have the for loops.

joaquinffernandez commented 3 years ago

I tested the changes and now the results are correct :) But I don't know why there's two empty for loops at the beginning of the equations definition:

equation
  for i in 1:N-1 loop
  end for;
  for i in 1:N loop
  end for;
  G_p_v = 0;
  for _Index_0 in 1:N loop
    R_T_heatPort[_Index_0] = R_T_ref[_Index_0];
    R_v[_Index_0] = R_p_v[_Index_0]-R_n_v[_Index_0];
    0 = R_p_i[_Index_0]+R_n_i[_Index_0];
    R_i[_Index_0] = R_p_i[_Index_0];
    R_R_actual[_Index_0] = R_R[_Index_0]*(1+R_alpha[_Index_0]*(R_T_heatPort[_Index_0]-R_T_ref[_Index_0]));
    R_v[_Index_0] = R_R_actual[_Index_0]*R_i[_Index_0];
    R_LossPower[_Index_0] = R_v[_Index_0]*R_i[_Index_0];
  end for;
  for _Index_1 in 1:N loop
    C_v[_Index_1] = C_p_v[_Index_1]-C_n_v[_Index_1];
    0 = C_p_i[_Index_1]+C_n_i[_Index_1];
    C_i[_Index_1] = C_p_i[_Index_1];
    C_i[_Index_1] = C_C[_Index_1]*der(C_v[_Index_1]);
  end for;
  S_v = S_p_v-S_n_v;
  0 = S_p_i+S_n_i;
  S_i = S_p_i;
  S_v = S_V;
  for i in 1:1:1 loop
    G_p_v = S_n_v;
  end for;
  for i in 1:1:4 loop
    C_n[i] = S_n;
  end for;
  for i in 1:1:1 loop
    S_n_i+sum(C_n_i[1:1:4])+G_p_i = 0;
  end for;
  for i in 4:1:4 loop
    C_p[i] = R_n[i];
  end for;
  for i in 4:1:4 loop
    C_p_i[i]+R_n_i[i] = 0;
  end for;
  for i in 1:1:3 loop
    C_p[i] = R_p[i+1];
  end for;
  for i in 1:1:3 loop
    R_n[i] = R_p[i+1];
  end for;
  for i in 2:1:4 loop
    R_n_i[i-1]+R_p_i[i]+C_p_i[i-1] = 0;
  end for;
  for i in 1:1:1 loop
    R_p[i] = S_p;
  end for;
  for i in 1:1:1 loop
    S_p_i+R_p_i[i] = 0;
  end for;
Kalashnikovni commented 3 years ago

That's right, there was an error in the Connectors::createGraph function.