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

Network2D flatter segfault. #42

Closed joaquinffernandez closed 3 years ago

joaquinffernandez commented 3 years ago

This model breaks on a segfault, I think it's due to an empty set, we should check of the model is wrong (it works OK in OpenModelica) Network2D model definition:

connector Pin
  Real v;
  flow Real i;
end Pin;

connector PositivePin
  Real v;
  flow Real i;
end PositivePin;

connector NegativePin
  Real v;
  flow Real i;
end NegativePin;

model Ground
  Pin p;  
equation
  p.v = 0;
end Ground;

model OnePort
  Real v;
  Real i;
  PositivePin p;
  NegativePin n;
equation
  v = p.v - n.v;
  0 = p.i + n.i;
  i = p.i;
end OnePort;

model ConditionalHeatPort
  parameter Real T=293.15;
  Real LossPower;
  Real T_heatPort;
equation
     T_heatPort = T;
end ConditionalHeatPort;

model Resistor
  parameter Real R(start=1);
  parameter Real T_ref=300.15;
  parameter Real alpha=0;
  Real v;
  Real i;
  PositivePin p;
  NegativePin n;
  Real R_actual;
  Real LossPower;
  Real T_heatPort;

equation
  T_heatPort = T_ref;
  v = p.v - n.v;
  0 = p.i + n.i;
  i = p.i;
  R_actual = R*(1 + alpha*(T_heatPort - T_ref));
  v = R_actual*i;
  LossPower = v*i;
end Resistor;

model Capacitor
  parameter Real C(start=1);
  Real v;
  Real i;
  PositivePin p;
  NegativePin n;

initial algorithm
  v := 0;

equation
  v = p.v - n.v;
  0 = p.i + n.i;
  i = p.i;
  i = C*der(v);
end Capacitor;

model ConstantVoltage
  parameter Real V(start=1);
  Real v;
  Real i;
  PositivePin p;
  NegativePin n;
equation
  v = p.v - n.v;
  0 = p.i + n.i;
  i = p.i;
  v = V;
end ConstantVoltage;

model RCcell
  Resistor resistor;
  Resistor resistor1;
  Capacitor capacitor;
  Capacitor capacitor1;
  Pin l;
  Pin d;
  Pin r;
  Pin u;
equation
  connect(resistor.n, capacitor.p);
  connect(resistor1.n, capacitor1.p);
  connect(resistor1.n, capacitor.p);
  connect(l, resistor.p);
  connect(capacitor.n, r);
  connect(resistor1.p, u);
  connect(capacitor1.n, d);
end RCcell;

model network2D
constant Integer N=4;
constant Integer M=5;
RCcell Cell[N,M];
ConstantVoltage S;
Ground G;
equation
for i in 1:N-1,j in 1:M-1 loop
  connect(Cell[i,j].r, Cell[i,j+1].l);
  connect(Cell[i,j].d, Cell[i+1,j].u);
end for;
for i in 1:N loop
  connect(Cell[i,M].r, Cell[i,1].l);
end for;
for j in 1:M loop
  connect(Cell[1,j].u,S.p);
  connect(Cell[N,j].d,S.n);
end for;
end network2D;

Backtrace:

(gdb) r ../test/mccprograms/flatter/Network2D.mo 
Starting program: /home/joaquin/work/modelicacc/bin/flatter ../test/mccprograms/flatter/Network2D.mo

Program received signal SIGFPE, Arithmetic exception.
0x00005555555af04c in SBG::IntervalImp1<SBG::UnordCT>::size (this=0x7fffffffb1b0) at ./util/graph/graph_definition.h:223
warning: Source file is more recent than executable.
223   int size()
(gdb) where
#0  0x00005555555af04c in SBG::IntervalImp1<SBG::UnordCT>::size (this=0x7fffffffb1b0) at ./util/graph/graph_definition.h:223
#1  Connectors::updateGraph (this=0x7fffffffd7c0, d1=0x555555d8c610, d2=0x555555dac5d0, mi1=..., mi2=...) at flatter/connectors.cpp:613
#2  0x00005555555b574e in Connectors::connect (this=0x7fffffffd7c0, co=...) at ./util/graph/graph_definition.h:257
#3  0x00005555555b6452 in Connectors::createGraph (this=0x7fffffffd7c0, eqs=...) at /usr/include/boost/variant/variant.hpp:1076
#4  0x00005555555b657a in Connectors::createGraph (this=0x7fffffffd7c0, eqs=...) at flatter/connectors.cpp:177
#5  0x00005555555bc8be in Connectors::solve (this=0x7fffffffd7c0) at flatter/connectors.cpp:109
#6  0x0000555555571443 in main (argc=<optimized out>, argv=<optimized out>) at flatter/main.cpp:108
(gdb) q
A debugging session is active.

    Inferior 1 [process 478662] will be killed.
Kalashnikovni commented 3 years ago

All variables should be of the same dimension, because operations were defined for elements with identical dimension (it simplified the operations).

Therefore, in the model the declaration of:

ConstantVoltage S;

should be replaced with:

ConstantVoltage S[1, 1];

Executing the following command after making the correction:

./bin/flatter ./test/mccprograms/flatter/Network2D.mo

produced the following output:

model network2D
  constant Integer N=4;
  constant Integer M=5;
  parameter Real Cell_resistor_R[N, M](each start=1);
  parameter Real Cell_resistor_T_ref[N, M]=fill(300.15,N);
  parameter Real Cell_resistor_alpha[N, M]=fill(0,N);
  Real Cell_resistor_v[N, M];
  Real Cell_resistor_i[N, M];
  Real Cell_resistor_p_v[N, M];
  flow Real Cell_resistor_p_i[N, M];
  Real Cell_resistor_n_v[N, M];
  flow Real Cell_resistor_n_i[N, M];
  Real Cell_resistor_R_actual[N, M];
  Real Cell_resistor_LossPower[N, M];
  Real Cell_resistor_T_heatPort[N, M];
  parameter Real Cell_resistor1_R[N, M](each start=1);
  parameter Real Cell_resistor1_T_ref[N, M]=fill(300.15,N);
  parameter Real Cell_resistor1_alpha[N, M]=fill(0,N);
  Real Cell_resistor1_v[N, M];
  Real Cell_resistor1_i[N, M];
  Real Cell_resistor1_p_v[N, M];
  flow Real Cell_resistor1_p_i[N, M];
  Real Cell_resistor1_n_v[N, M];
  flow Real Cell_resistor1_n_i[N, M];
  Real Cell_resistor1_R_actual[N, M];
  Real Cell_resistor1_LossPower[N, M];
  Real Cell_resistor1_T_heatPort[N, M];
  parameter Real Cell_capacitor_C[N, M](each start=1);
  Real Cell_capacitor_v[N, M];
  Real Cell_capacitor_i[N, M];
  Real Cell_capacitor_p_v[N, M];
  flow Real Cell_capacitor_p_i[N, M];
  Real Cell_capacitor_n_v[N, M];
  flow Real Cell_capacitor_n_i[N, M];
  parameter Real Cell_capacitor1_C[N, M](each start=1);
  Real Cell_capacitor1_v[N, M];
  Real Cell_capacitor1_i[N, M];
  Real Cell_capacitor1_p_v[N, M];
  flow Real Cell_capacitor1_p_i[N, M];
  Real Cell_capacitor1_n_v[N, M];
  flow Real Cell_capacitor1_n_i[N, M];
  Real Cell_l_v[N, M];
  flow Real Cell_l_i[N, M];
  Real Cell_d_v[N, M];
  flow Real Cell_d_i[N, M];
  Real Cell_r_v[N, M];
  flow Real Cell_r_i[N, M];
  Real Cell_u_v[N, M];
  flow Real Cell_u_i[N, M];
  parameter Real S_V[1, 1](each start=1);
  Real S_v[1, 1];
  Real S_i[1, 1];
  Real S_p_v[1, 1];
  flow Real S_p_i[1, 1];
  Real S_n_v[1, 1];
  flow Real S_n_i[1, 1];
  Real G_p_v;
  flow Real G_p_i;
initial algorithm
    for _Index_0 in 1:N,_Index_1 in 1:M loop
      Cell_capacitor_v[_Index_0,_Index_1]:=0;
      Cell_capacitor1_v[_Index_0,_Index_1]:=0;
    end for;
equation
  Cell_resistor_T_heatPort[_Index_0,_Index_1] = Cell_resistor_T_ref[_Index_0,_Index_1];
  Cell_resistor_v[_Index_0,_Index_1] = Cell_resistor_p_v[_Index_0,_Index_1]-Cell_resistor_n_v[_Index_0,_Index_1];
  0 = Cell_resistor_p_i[_Index_0,_Index_1]+Cell_resistor_n_i[_Index_0,_Index_1];
  Cell_resistor_i[_Index_0,_Index_1] = Cell_resistor_p_i[_Index_0,_Index_1];
  Cell_resistor_R_actual[_Index_0,_Index_1] = Cell_resistor_R[_Index_0,_Index_1]*(1+Cell_resistor_alpha[_Index_0,_Index_1]*(Cell_resistor_T_heatPort[_Index_0,_Index_1]-Cell_resistor_T_ref[_Index_0,_Index_1]));
  Cell_resistor_v[_Index_0,_Index_1] = Cell_resistor_R_actual[_Index_0,_Index_1]*Cell_resistor_i[_Index_0,_Index_1];
  Cell_resistor_LossPower[_Index_0,_Index_1] = Cell_resistor_v[_Index_0,_Index_1]*Cell_resistor_i[_Index_0,_Index_1];
  Cell_resistor1_T_heatPort[_Index_0,_Index_1] = Cell_resistor1_T_ref[_Index_0,_Index_1];
  Cell_resistor1_v[_Index_0,_Index_1] = Cell_resistor1_p_v[_Index_0,_Index_1]-Cell_resistor1_n_v[_Index_0,_Index_1];
  0 = Cell_resistor1_p_i[_Index_0,_Index_1]+Cell_resistor1_n_i[_Index_0,_Index_1];
  Cell_resistor1_i[_Index_0,_Index_1] = Cell_resistor1_p_i[_Index_0,_Index_1];
  Cell_resistor1_R_actual[_Index_0,_Index_1] = Cell_resistor1_R[_Index_0,_Index_1]*(1+Cell_resistor1_alpha[_Index_0,_Index_1]*(Cell_resistor1_T_heatPort[_Index_0,_Index_1]-Cell_resistor1_T_ref[_Index_0,_Index_1]));
  Cell_resistor1_v[_Index_0,_Index_1] = Cell_resistor1_R_actual[_Index_0,_Index_1]*Cell_resistor1_i[_Index_0,_Index_1];
  Cell_resistor1_LossPower[_Index_0,_Index_1] = Cell_resistor1_v[_Index_0,_Index_1]*Cell_resistor1_i[_Index_0,_Index_1];
  Cell_capacitor_v[_Index_0,_Index_1] = Cell_capacitor_p_v[_Index_0,_Index_1]-Cell_capacitor_n_v[_Index_0,_Index_1];
  0 = Cell_capacitor_p_i[_Index_0,_Index_1]+Cell_capacitor_n_i[_Index_0,_Index_1];
  Cell_capacitor_i[_Index_0,_Index_1] = Cell_capacitor_p_i[_Index_0,_Index_1];
  Cell_capacitor_i[_Index_0,_Index_1] = Cell_capacitor_C[_Index_0,_Index_1]*der(Cell_capacitor_v[_Index_0,_Index_1]);
  Cell_capacitor1_v[_Index_0,_Index_1] = Cell_capacitor1_p_v[_Index_0,_Index_1]-Cell_capacitor1_n_v[_Index_0,_Index_1];
  0 = Cell_capacitor1_p_i[_Index_0,_Index_1]+Cell_capacitor1_n_i[_Index_0,_Index_1];
  Cell_capacitor1_i[_Index_0,_Index_1] = Cell_capacitor1_p_i[_Index_0,_Index_1];
  Cell_capacitor1_i[_Index_0,_Index_1] = Cell_capacitor1_C[_Index_0,_Index_1]*der(Cell_capacitor1_v[_Index_0,_Index_1]);
  S_v[_Index_2,_Index_3] = S_p_v[_Index_2,_Index_3]-S_n_v[_Index_2,_Index_3];
  0 = S_p_i[_Index_2,_Index_3]+S_n_i[_Index_2,_Index_3];
  S_i[_Index_2,_Index_3] = S_p_i[_Index_2,_Index_3];
  S_v[_Index_2,_Index_3] = S_V[_Index_2,_Index_3];
  G_p_v = 0;
  for i in 1:1:1,j in 2:1:5 loop
    Cell_u_v[i,j] = Cell_u_v[1*i+0,1];
  end for;
  for i in 1:1:1,j in 1:1:1 loop
    sum(Cell_u_i[1*i+0, 1:1:5]) = 0;
  end for;
  for i in 1:1:3,j in 5:1:5 loop
    Cell_capacitor1_n[1*i+0,1*j+0]+Cell_capacitor1_n[1*i+0,1*j+0]+Cell_capacitor1_n_i[1*i+0,1*j+0]+Cell_d_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 2:1:4,j in 5:1:5 loop
    Cell_resistor1_p_i[1*i+0,1*j+0]+Cell_u_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 4:1:4,j in 1:1:4 loop
    Cell_capacitor_n_i[1*i+0,1*j+0]+Cell_r_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 4:1:4,j in 2:1:5 loop
    Cell_d_v[i,j] = Cell_d_v[1*i+0,1];
  end for;
  for i in 4:1:4,j in 1:1:1 loop
    sum(Cell_d_i[1*i+0, 1:1:5]) = 0;
  end for;
  for i in 1:1:3,j in 1:1:4 loop
    Cell_d_i[1*i+0,1*j+0]+Cell_resistor1_p_i[1*i+1,1*j+0] = 0;
  end for;
  for i in 4:1:4,j in 2:1:5 loop
    Cell_l_i[1*i+0,1*j+0]+Cell_resistor_p_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 1:1:4,j in 5:1:5 loop
    Cell_r_i[1*i+0,1*j+0]+Cell_resistor_p_i[1*i+0,1*j+-4] = 0;
  end for;
  for i in 1:1:3,j in 1:1:4 loop
    Cell_r_i[1*i+0,1*j+0]+Cell_resistor_p_i[1*i+0,1*j+1] = 0;
  end for;
  for i in 1:1:4,j in 1:1:5 loop
    Cell_capacitor_p[i,j] = Cell_resistor_n[1*i+0,1*j+0];
  end for;
  for i in 1:1:4,j in 1:1:5 loop
    Cell_capacitor1_p[i,j] = Cell_resistor_n[1*i+0,1*j+0];
  end for;
  for i in 1:1:4,j in 1:1:5 loop
    Cell_resistor1_n[i,j] = Cell_resistor_n[1*i+0,1*j+0];
  end for;
  for i in 1:1:4,j in 1:1:5 loop
    Cell_resistor_n_i[1*i+0,1*j+0]+Cell_resistor1_n_i[1*i+0,1*j+0]+Cell_capacitor1_p[1*i+0,1*j+0]+Cell_capacitor1_p[1*i+0,1*j+0]+Cell_capacitor1_p_i[1*i+0,1*j+0]+Cell_capacitor_p_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 4:1:4,j in 1:1:5 loop
    Cell_capacitor1_n[i,j] = S_n;
  end for;
  for i in 1:1:1,j in 1:1:1 loop
    sum(Cell_capacitor1_n[1*i+3, 1:1:5])+sum(Cell_capacitor1_n[1*i+3, 1:1:5])+sum(Cell_capacitor1_n_i[1*i+3, 1:1:5])+S_n_i = 0;
  end for;
  for i in 1:1:1,j in 1:1:5 loop
    Cell_resistor1_p[i,j] = S_p;
  end for;
  for i in 1:1:1,j in 1:1:1 loop
    sum(Cell_resistor1_p_i[1*i+0, 1:1:5])+S_p_i = 0;
  end for;
  for i in 1:1:4,j in 1:1:1 loop
    Cell_l_i[1*i+0,1*j+0]+Cell_capacitor_n_i[1*i+0,1*j+4] = 0;
  end for;
  for i in 2:1:4,j in 1:1:4 loop
    Cell_capacitor1_n[1*i+-1,1*j+0]+Cell_capacitor1_n[1*i+-1,1*j+0]+Cell_capacitor1_n_i[1*i+-1,1*j+0]+Cell_u_i[1*i+0,1*j+0] = 0;
  end for;
  for i in 1:1:3,j in 2:1:5 loop
    Cell_l_i[1*i+0,1*j+0]+Cell_capacitor_n_i[1*i+0,1*j+-1] = 0;
  end for;
end network2D;