Modelica: Using an Openmodelica Package in Dymola - modelica

I'm trying to use an Openmodelica Package (including one model and 3 functions) in Dymola, but there are some Errors (See Error 1).
When I try to declare the protected parameters (in the function cal_mod) as variables I get another Error (see Error 2).
Here is the whole code of the package and the Errors (Error 1 and Error 2) are below.
Code:
package RC_Wall
model RCWall
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC Tpa;
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC Tpb;
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC T[mNode](start = vector([24, 24, 24]), fixed = vector([true, true, true]));
Modelica.SIunits.HeatFlowRate Qa;
Modelica.SIunits.HeatFlowRate Qb;
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC T1_end;
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC T2_end;
Modelica.SIunits.Conversions.NonSIunits.Temperature_degC T3_end;
parameter Integer n;
parameter Modelica.SIunits.Area A;
parameter Modelica.SIunits.Length l[n];
parameter Modelica.SIunits.ThermalConductivity lambda[n];
parameter Modelica.SIunits.Density rho[n];
parameter Modelica.SIunits.SpecificHeatCapacity cp[n];
parameter Integer mNode = 3;
parameter Integer nNode(start = 2, fixed = false);
parameter Modelica.SIunits.SpecificHeatCapacity Ca(start = 24800, fixed = false);
parameter Modelica.SIunits.SpecificHeatCapacity Cb(start = 24800, fixed = false);
parameter Modelica.SIunits.SpecificHeatCapacity Cc(start = 102560, fixed = false);
parameter Modelica.SIunits.ThermalInsulance R1(start = 0.81, fixed = false);
parameter Modelica.SIunits.ThermalInsulance R2(start = 0.81, fixed = false);
parameter Modelica.SIunits.ThermalInsulance Ra(start = 0.03, fixed = false);
parameter Modelica.SIunits.ThermalInsulance Rb(start = 0.03, fixed = false);
Modelica.Thermal.HeatTransfer.Interfaces.HeatPort_a port_a annotation(Placement(visible = true, transformation(origin = {-100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {-100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
Modelica.Thermal.HeatTransfer.Interfaces.HeatPort_a port_a1 annotation(Placement(visible = true, transformation(origin = {100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0), iconTransformation(origin = {100, 0}, extent = {{-10, -10}, {10, 10}}, rotation = 0)));
initial equation
(Ca, Cb, Cc, Ra, Rb, R1, R2, nNode) = RC_Wall.rcopt(n, l, lambda, rho, cp);
equation
Tpa = port_a.T;
Tpb = port_a1.T;
if R1 < 0.0001 or R2 < 0.0001 or Ra < 0.0001 or Rb < 0.0001 then
Modelica.Utilities.Streams.error("Resistances too small or negative");
end if;
if Ca < 0.0001 or Cb < 0.0001 or Cc < 0.0001 and nNode == 3 then
Modelica.Utilities.Streams.error("Capacities too small or negative");
end if;
Ca * A * der(T[1]) = Qa + A / R1 * (T[2] - T[1]);
for i in 2:mNode loop
if i < nNode and i <> mNode then
Cc * A * der(T[i]) = A / R1 * (T[i - 1] - T[i]) + A / R2 * (T[i + 1] - T[i]);
else
Cb * A * der(T[i]) = Qb + A / R2 * (T[nNode - 1] - T[i]);
end if;
end for;
Qa = A * (Tpa - T[1]) / Ra;
Qb = A * (Tpb - T[nNode]) / Rb;
Qa = port_a.Q_flow;
Qb = port_a1.Q_flow;
T1_end = T[1];
T2_end = T[2];
T3_end = T[3];
end RCWall;
function rcopt
input Integer n;
input Real[:] l;
input Real[:] lbda;
input Real[:] rho;
input Real[:] specc;
output Real Ca;
output Real Cb;
output Real Cc;
output Real Ra;
output Real Rb;
output Real Ro1;
output Real Ro2;
output Integer nNode;
protected
Integer i;
Integer nComps;
Integer k2;
Complex A_o;
Complex B_o;
Complex C_o;
Complex D_o;
Complex A_a;
Complex B_a;
Complex C_a;
Complex D_a;
Complex Re1;
Complex Im1;
Complex[2, 2] ma;
Complex[2, 2] mb;
Complex[13] YaComp;
Complex[13] YbComp;
Complex[13] TdComp;
Real[13] T_ev;
Real devat;
Real devbt;
Real[13] deva;
Real[13] devb;
Real[13] devc;
Real devCc;
Real devCcOld;
Real devatOld1;
Real devbtOld1;
Real devct;
Real Ra1;
Real Rb1;
Real Ca1;
Real Cb1;
Real Cc1;
Real Ro11;
Real Ro21;
Real[n] Rlay;
Real[n] Clay;
Real Rt;
Real Ct;
Real Rc;
Real Rsa;
Real Rsb;
Real Csa;
Real Csb;
Real R1s;
Real R2s;
Real Cs;
Real CaRoot1;
Real CbRoot1;
Real Ahelp;
Real Bhelp;
Real Chelp;
Real Ras1;
Real Rbs1;
Real Ro1s1;
Real Ro2s1;
Real Cas1;
Real Cbs1;
Real Xr;
Real Xi;
Real Yr;
Real Yi;
Real Zr;
Real Zi;
Real R11;
Real R21;
Real Ratio;
Real RaSide;
Real RbSide;
Real CcB_a;
Real CcD_a;
Real RaSide1;
Real RbSide1;
Real RaRoot1;
Real RbRoot1;
Real omega_hi;
Real omega_lo;
Real omega;
Real omegac;
Real T[145];
Real Ts;
Real PII;
Real Tc1;
Real Tc2;
Real tol;
algorithm
nComps := 13;
devCcOld := 99999;
devatOld1 := 99999;
devbtOld1 := 99999;
Rt := 0;
Ct := 0;
Re1 := Complex(1, 0);
Im1 := Complex(0, 1);
PII := 4 * atan(1);
for i in 1:n loop
Rlay[i] := l[i] / lbda[i];
Rt := Rt + Rlay[i];
Clay[i] := rho[i] * specc[i] * l[i];
Ct := Ct + Clay[i];
end for;
Tc1 := Rt * Clay[1];
Tc2 := Rt * Clay[n];
if Tc1 >= Tc2 then
Ts := 2 * PII * Tc1;
else
Ts := 2 * PII * Tc2;
end if;
omega_lo := 2 * PII / Ts;
(A_a, B_a, C_a, D_a) := RC_Wall.cal_an(omega_lo, n, l, lbda, rho, specc);
Ra1 := (Modelica.ComplexMath.real(B_a) + Rt) / (1 - Modelica.ComplexMath.real(A_a));
Rb1 := (Modelica.ComplexMath.real(B_a) + Rt) / (1 - Modelica.ComplexMath.real(D_a));
Rc := Rt - Ra1 - Rb1;
Ca1 := (Modelica.ComplexMath.imag(B_a) + Modelica.ComplexMath.imag(A_a) * (Rc + Ra1)) / (Rc * (Rt - Ra1) * omega_lo);
Cb1 := (Modelica.ComplexMath.imag(B_a) + Modelica.ComplexMath.imag(D_a) * (Rc + Rb1)) / (Rc * (Rt - Rb1) * omega_lo);
Ro11 := Rc;
Ro21 := Rc;
Cc1 := 0;
T_ev := vector([1, 6, 12, 18, 24, 48, 96, 168, 240, 336, 504, 1008, 2016]);
for i in 1:nComps loop
omega_hi := 2 * PII / (T_ev[i] * 3600);
(A_a, B_a, C_a, D_a) := RC_Wall.cal_an(omega_hi, n, l, lbda, rho, specc);
YaComp[i] := -A_a / B_a;
YbComp[i] := D_a / B_a;
TdComp[i] := -1 / B_a;
(A_o, B_o, C_o, D_o) := RC_Wall.cal_mod(omega_hi, Ra1, Rb1, Ro11, Ro21, Ca1, Cb1, Cc1);
deva[i] := Modelica.ComplexMath.'abs'((YaComp[i] - (-A_o) / B_o) / YaComp[i]);
devb[i] := Modelica.ComplexMath.'abs'((YbComp[i] - D_o / B_o) / YbComp[i]);
devc[i] := Modelica.ComplexMath.'abs'((TdComp[i] - 1 / B_o) / TdComp[i]);
end for;
devat := 0;
devbt := 0;
devct := 0;
for i in 1:nComps loop
devat := devat + deva[i] / nComps;
devbt := devbt + devb[i] / nComps;
devct := devct + devc[i] / nComps;
end for;
tol := 0.3;
if devat < tol and devbt < tol and deva[2] < tol and devb[2] < tol and devc[2] < 2 * tol then
Ra := Ra1;
Rb := Rb1;
Ro1 := Ro11 + Ro21;
Ro2 := Ro11 + Ro21;
Ca := Ca1;
Cb := Cb1;
Cc := 0;
nNode := 2;
else
for k in 1:145 loop
if k < 40 then
T[k] := 0.5 * k;
elseif k < 70 then
T[k] := k - 20;
elseif k < 95 then
T[k] := (k - 70) * 2 + 50;
else
T[k] := (k - 95) * 4 + 100;
end if;
omega := 2 * PII / (3600 * T[k]);
(A_a, B_a, C_a, D_a) := RC_Wall.cal_an(omega, n, l, lbda, rho, specc);
Ratio := Modelica.ComplexMath.real((1 - A_a) / (1 - D_a));
RaSide := Rt / (Ratio + 1);
RbSide := Rt - RaSide;
CcB_a := -Modelica.ComplexMath.imag(B_a / (omega * RaSide * RbSide));
CcD_a := Modelica.ComplexMath.imag(D_a / (RaSide * omega));
devCc := abs((CcB_a - CcD_a) / CcB_a);
if CcB_a > 0 and devCc < devCcOld then
Cc1 := 0.8 * CcB_a;
RaSide1 := RaSide;
RbSide1 := RbSide;
devCcOld := devCc;
end if;
end for;
for k in 1:145 loop
omega := 2 * PII / (3600 * T[k]);
(A_a, B_a, C_a, D_a) := RC_Wall.cal_an(omega, n, l, lbda, rho, specc);
mb[1, 1] := 1 * Re1;
mb[1, 2] := RbSide1 * Re1;
mb[2, 1] := omega * Cc1 * Im1;
mb[2, 2] := 1 * Re1 + RbSide1 * omega * Cc1 * Im1;
Xr := Modelica.ComplexMath.real(mb[1, 2] / mb[2, 2]);
Xi := Modelica.ComplexMath.imag(mb[1, 2] / mb[2, 2]);
Yr := Modelica.ComplexMath.real(-A_a / B_a);
Yi := Modelica.ComplexMath.imag(-A_a / B_a);
Zr := Modelica.ComplexMath.real(mb[1, 2] / mb[2, 2] * (-A_a / B_a));
Zi := Modelica.ComplexMath.imag(mb[1, 2] / mb[2, 2] * (-A_a / B_a));
Chelp := (RaSide1 + Xr) * (Zr - 1 + Yr * RaSide1) + Xi * (Zi + RaSide1 * Yi);
Bhelp := (-(Zr - 1 + Yr * RaSide1) * (1 + Zr)) - (Zi ^ 2 + RaSide1 * Yi * Zi) - (Zi * Yi + RaSide1 * Yi ^ 2) * RaSide1 - (Yr * Zr - Yr + RaSide1 * Yr ^ 2) * RaSide1;
Ahelp := Zi * Yi + RaSide1 * Yi ^ 2 + Yr * Zr - Yr + Yr ^ 2 * RaSide1;
RaRoot1 := ((-Bhelp) - sqrt(Bhelp ^ 2 - 4 * Ahelp * Chelp)) / (2 * Ahelp);
R11 := RaSide1 - RaRoot1;
CaRoot1 := (Zr - 1 + Yr * RaSide1) / (omega * (Zi * RaRoot1 - Xi + Yi * (RaSide1 - RaRoot1) * RaRoot1));
devat := 0.0;
for k2 in 1:nComps loop
omegac := 2 * PII / (3600 * T_ev[k2]);
(A_o, B_o, C_o, D_o) := RC_Wall.cal_mod(omegac, RaRoot1, 0.0, R11, RbSide1, CaRoot1, 0.0, Cc1);
deva[k2] := Modelica.ComplexMath.'abs'(((-A_o / B_o) - YaComp[k2]) / YaComp[k2]);
devb[k2] := Modelica.ComplexMath.'abs'((D_o / B_o - YbComp[k2]) / YbComp[k2]);
devat := devat + deva[k2] / nComps;
end for;
if devat < devatOld1 then
Ras1 := RaRoot1;
Ro1s1 := R11;
Cas1 := CaRoot1;
devatOld1 := devat;
end if;
ma[1, 1] := 1 * Re1;
ma[1, 2] := -RaSide1 * Re1;
ma[2, 1] := -omega * Cc1 * Im1;
ma[2, 2] := 1 * Re1 + RaSide1 * omega * Cc1 * Im1;
Xr := -Modelica.ComplexMath.real(ma[1, 2] / ma[2, 2]);
Xi := -Modelica.ComplexMath.imag(ma[1, 2] / ma[2, 2]);
Yr := -Modelica.ComplexMath.real(D_a / B_a);
Yi := -Modelica.ComplexMath.imag(D_a / B_a);
Zr := Modelica.ComplexMath.real(ma[1, 2] / ma[2, 2] * (D_a / B_a));
Zi := Modelica.ComplexMath.imag(ma[1, 2] / ma[2, 2] * (D_a / B_a));
Chelp := (RbSide1 + Xr) * (Zr - 1.0 + Yr * RbSide1) + Xi * (Zi + RbSide1 * Yi);
Bhelp := (-(Zr - 1.0 + Yr * RbSide1) * (1.0 + Zr)) - (Zi ^ 2 + RbSide1 * Yi * Zi) - (Zi * Yi + RbSide1 * Yi ^ 2) * RbSide1 - (Yr * Zr - Yr + RbSide1 * Yr ^ 2) * RbSide1;
Ahelp := Zi * Yi + RbSide1 * Yi ^ 2 + Yr * Zr - Yr + Yr ^ 2 * RbSide1;
RbRoot1 := ((-Bhelp) - sqrt(Bhelp ^ 2 - 4.0 * Ahelp * Chelp)) / (2.0 * Ahelp);
R21 := RbSide1 - RbRoot1;
CbRoot1 := (Zr - 1 + Yr * RbSide1) / (omega * (Zi * RbRoot1 - Xi + Yi * (RbSide1 - RbRoot1) * RbRoot1));
devbt := 0;
for k2 in 1:nComps loop
omegac := 2 * PII / (3600 * T_ev[k2]);
(A_o, B_o, C_o, D_o) := RC_Wall.cal_mod(omegac, 0.0, RbRoot1, RaSide1, Ro21, 0.0, CbRoot1, Cc1);
deva[k2] := Modelica.ComplexMath.'abs'(((-A_o / B_o) - YaComp[k2]) / YaComp[k2]);
devb[k2] := Modelica.ComplexMath.'abs'((D_o / B_o - YbComp[k2]) / YbComp[k2]);
devbt := devbt + devb[k2] / nComps;
end for;
if devbt < devbtOld1 then
Rbs1 := RbRoot1;
Ro2s1 := R21;
Cbs1 := CbRoot1;
devbtOld1 := devbt;
end if;
end for;
devatOld1 := 99999;
devbtOld1 := 99999;
for k in 1:145 loop
omega := 2 * PII / (3600 * T[k]);
(A_a, B_a, C_a, D_a) := RC_Wall.cal_an(omega, n, l, lbda, rho, specc);
mb[1, 1] := 1 * Re1 + Ro2s1 * omega * Cbs1 * Im1;
mb[1, 2] := (Rbs1 + Ro2s1) * Re1 + Rbs1 * Ro2s1 * omega * Cbs1 * Im1;
mb[2, 1] := omega * (Cbs1 + Cc1) * Im1 - Ro2s1 * omega ^ 2 * Cbs1 * Cc1 * Re1;
mb[2, 2] := (1 - Ro2s1 * Rbs1 * omega ^ 2 * Cbs1 * Cc1) * Re1 + (Rbs1 * omega * (Cbs1 + Cc1) + omega * Ro2s1 * Cc1) * Im1;
Xr := Modelica.ComplexMath.real(mb[1, 2] / mb[2, 2]);
Xi := Modelica.ComplexMath.imag(mb[1, 2] / mb[2, 2]);
Yr := Modelica.ComplexMath.real(-A_a / B_a);
Yi := Modelica.ComplexMath.imag(-A_a / B_a);
Zr := Modelica.ComplexMath.real(mb[1, 2] / mb[2, 2] * (-A_a / B_a));
Zi := Modelica.ComplexMath.imag(mb[1, 2] / mb[2, 2] * (-A_a / B_a));
Chelp := (RaSide1 + Xr) * (Zr - 1 + Yr * RaSide1) + Xi * (Zi + RaSide1 * Yi);
Bhelp := (-(Zr - 1 + Yr * RaSide1) * (1 + Zr)) - (Zi ^ 2 + RaSide1 * Yi * Zi) - (Zi * Yi + RaSide1 * Yi ^ 2) * RaSide1 - (Yr * Zr - Yr + RaSide1 * Yr ^ 2) * RaSide1;
Ahelp := Zi * Yi + RaSide1 * Yi ^ 2 + Yr * Zr - Yr + Yr ^ 2 * RaSide1;
RaRoot1 := ((-Bhelp) - sqrt(Bhelp ^ 2 - 4 * Ahelp * Chelp)) / (2 * Ahelp);
R11 := RaSide1 - RaRoot1;
CaRoot1 := (Zr - 1 + Yr * RaSide1) / (omega * (Zi * RaRoot1 - Xi + Yi * (RaSide1 - RaRoot1) * RaRoot1));
devat := 0;
for k2 in 1:nComps loop
omegac := 2 * PII / (3600 * T_ev[k2]);
(A_o, B_o, C_o, D_o) := RC_Wall.cal_mod(omegac, RaRoot1, Rbs1, R11, Ro2s1, CaRoot1, Cbs1, Cc1);
deva[k2] := Modelica.ComplexMath.'abs'(((-A_o / B_o) - YaComp[k2]) / YaComp[k2]);
devb[k2] := Modelica.ComplexMath.'abs'((D_o / B_o - YbComp[k2]) / YbComp[k2]);
devat := devat + deva[k2] / nComps;
end for;
if devat < devatOld1 then
Ra1 := RaRoot1;
Ro11 := R11;
Ca1 := CaRoot1;
devatOld1 := devat;
end if;
ma[1, 1] := 1 * Re1 + Ro1s1 * omega * Cas1 * Im1;
ma[1, 2] := (-(Ras1 + Ro1s1) * Re1) - Ras1 * Ro1s1 * omega * Cas1 * Im1;
ma[2, 1] := (-omega * (Cas1 + Cc1) * Im1) + Ro1s1 * omega ^ 2 * Cas1 * Cc1 * Re1;
ma[2, 2] := (1 - Ro1s1 * Ras1 * omega ^ 2 * Cas1 * Cc1) * Re1 + (Ras1 * omega * (Cas1 + Cc1) + omega * Ro1s1 * Cc1) * Im1;
Xr := -Modelica.ComplexMath.real(ma[1, 2] / ma[2, 2]);
Xi := -Modelica.ComplexMath.imag(ma[1, 2] / ma[2, 2]);
Yr := -Modelica.ComplexMath.real(D_a / B_a);
Yi := -Modelica.ComplexMath.imag(D_a / B_a);
Zr := Modelica.ComplexMath.real(ma[1, 2] / ma[2, 2] * (D_a / B_a));
Zi := Modelica.ComplexMath.imag(ma[1, 2] / ma[2, 2] * (D_a / B_a));
Chelp := (RbSide1 + Xr) * (Zr - 1 + Yr * RbSide1) + Xi * (Zi + RbSide1 * Yi);
Bhelp := (-(Zr - 1 + Yr * RbSide1) * (1 + Zr)) - (Zi ^ 2 + RbSide1 * Yi * Zi) - (Zi * Yi + RbSide1 * Yi ^ 2) * RbSide1 - (Yr * Zr - Yr + RbSide1 * Yr ^ 2) * RbSide1;
Ahelp := Zi * Yi + RbSide1 * Yi ^ 2 + Yr * Zr - Yr + Yr ^ 2 * RbSide1;
RbRoot1 := ((-Bhelp) - sqrt(Bhelp ^ 2 - 4 * Ahelp * Chelp)) / (2 * Ahelp);
R21 := RbSide1 - RbRoot1;
CbRoot1 := (Zr - 1 + Yr * RbSide1) / (omega * (Zi * RbRoot1 - Xi + Yi * (RbSide1 - RbRoot1) * RbRoot1));
devbt := 0;
for k2 in 1:nComps loop
omegac := 2 * PII / (3600 * T_ev[k2]);
(A_o, B_o, C_o, D_o) := RC_Wall.cal_mod(omegac, Ras1, RbRoot1, Ro1s1, R21, Cas1, CbRoot1, Cc1);
deva[k2] := Modelica.ComplexMath.'abs'(((-A_o / B_o) - YaComp[k2]) / YaComp[k2]);
devb[k2] := Modelica.ComplexMath.'abs'((D_o / B_o - YbComp[k2]) / YbComp[k2]);
devbt := devbt + devb[k2] / nComps;
end for;
if devbt < devbtOld1 then
Rb1 := RbRoot1;
Ro21 := R21;
Cb1 := CbRoot1;
devbtOld1 := devbt;
end if;
end for;
Ro1 := Ro11;
Ro2 := Ro21;
Cc := Cc1;
Ra := Ra1;
Rb := Rb1;
Ca := Ca1;
Cb := Cb1;
nNode := 3;
end if;
end rcopt;
function cal_an
import Complex;
input Real omega;
input Integer n;
input Real[:] l1;
input Real[:] lbda1;
input Real[:] rho1;
input Real[:] specc1;
output Complex A_o;
output Complex B_o;
output Complex C_o;
output Complex D_o;
protected
Complex A;
Complex A1;
Complex B;
Complex B1;
Complex C;
Complex C1;
Complex D;
Complex D1;
Complex X;
Complex PART1;
Complex PART2;
Real therm_d;
Real k;
Real kl;
Real klb;
Integer i;
algorithm
X := Complex(1, 1);
for i in 1:n loop
therm_d := lbda1[i] / (rho1[i] * specc1[i]);
k := sqrt(omega / (2 * therm_d));
kl := k * l1[i];
klb := k * lbda1[i];
PART1 := Modelica.ComplexMath.exp(kl * X);
PART2 := Modelica.ComplexMath.exp(-kl * X);
A := (PART1 + PART2) / 2;
B := -(PART1 - PART2) / (2 * klb * X);
C := -klb * X * (PART1 - PART2) / 2;
D := (PART1 + PART2) / 2;
if i == 1 then
A_o := A;
B_o := B;
C_o := C;
D_o := D;
else
A1 := A * A_o + B * C_o;
B1 := A * B_o + B * D_o;
C1 := C * A_o + D * C_o;
D1 := C * B_o + D * D_o;
A_o := A1;
B_o := B1;
C_o := C1;
D_o := D1;
end if;
end for;
end cal_an;
function cal_mod
import Complex;
input Real omega;
input Real RaM;
input Real RbM;
input Real Ro1M;
input Real Ro2M;
input Real CaM;
input Real CbM;
input Real CcM;
output Complex A_o;
output Complex B_o;
output Complex C_o;
output Complex D_o;
protected
Complex A;
Complex A1;
Complex B;
Complex B1;
Complex C;
Complex C1;
Complex D;
Complex D1;
parameter Integer i;
algorithm
for i in 1:7 loop
A := Complex(1, 0);
D := Complex(1, 0);
if i == 1 then
B := -RaM * Complex(1, 0);
C := Complex(0, 0);
elseif i == 2 then
B := Complex(0, 0);
C := Complex(0, 1) * (-CaM * omega);
elseif i == 3 then
B := (-Ro1M) * Complex(1, 0);
C := Complex(0, 0);
elseif i == 4 then
B := Complex(0, 0);
C := Complex(0, 1) * (-CcM * omega);
elseif i == 5 then
B := (-Ro2M) * Complex(1, 0);
C := Complex(0, 0);
elseif i == 6 then
B := Complex(0, 0);
C := Complex(0, 1) * (-CbM * omega);
elseif i == 7 then
B := (-RbM) * Complex(1, 0);
C := Complex(0, 0);
end if;
if i == 1 then
A_o := A;
B_o := B;
C_o := C;
D_o := D;
else
A1 := A * A_o + B * C_o;
B1 := A * B_o + B * D_o;
C1 := C * A_o + D * C_o;
D1 := C * B_o + D * D_o;
A_o := A1;
B_o := B1;
C_o := C1;
D_o := D1;
end if;
end for;
end cal_mod;
annotation(Icon(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})), Diagram(coordinateSystem(extent = {{-100, -100}, {100, 100}}, preserveAspectRatio = true, initialScale = 0.1, grid = {2, 2})));
end RC_Wall;
Error 1:
Check of Bachelorarbeit_Bloch.RC_Wall:
Checking model Bachelorarbeit_Bloch.RC_Wall.RCWall:
Assignment to parameter or constant: A In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: D In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: A1 In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: B1 In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: C1 In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Assignment to parameter or constant: D1 In function
Bachelorarbeit_Bloch.RC_Wall.cal_mod
Errors detected in functions.
Check aborted.
Local classes checked, checking Bachelorarbeit_Bloch.RC_Wall:
ERROR: 20 errors were found
Error 2:
Check of Bachelorarbeit_Bloch.RC_Wall:
Checking model Bachelorarbeit_Bloch.RC_Wall.RCWall:
The model has the same number of unknowns and equations.
The parameter nNode has the attribute fixed = false indicating its
value to be calculated from initial conditions. Error: However, it is
used to specify a structural property and it must be possible to
evaluate it at translation.
The parameter Ca has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter Cb has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter Cc has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter R1 has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter R2 has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter Ra has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
The parameter Rb has the attribute fixed = false indicating its value
to be calculated from initial conditions. Error: However, it is used
to specify a structural property and it must be possible to evaluate
it at translation.
Errors detected in model.
Check aborted.
Local classes checked, checking Bachelorarbeit_Bloch.RC_Wall:
ERROR: 16 errors were found

This can be worked around as follows (at least for check).
Add a new non-parameter in RCwall
Integer nNode2=nNode;
And then modify the main code as follows:
initial equation
(Ca, Cb, Cc, Ra, Rb, R1, R2, nNode) = RC_Wall.rcopt(n, l, lambda, rho, cp);
initial algorithm
if R1 < 0.0001 or R2 < 0.0001 or Ra < 0.0001 or Rb < 0.0001 then
Modelica.Utilities.Streams.error("Resistances too small or negative");
end if;
if Ca < 0.0001 or Cb < 0.0001 or Cc < 0.0001 and nNode2 == 3 then
Modelica.Utilities.Streams.error("Capacities too small or negative");
end if;
equation
Tpa = port_a.T;
Tpb = port_a1.T;
Ca * A * der(T[1]) = Qa + A / R1 * (T[2] - T[1]);
for i in 2:mNode loop
if i < nNode2 and i <> mNode then
Cc * A * der(T[i]) = A / R1 * (T[i - 1] - T[i]) + A / R2 * (T[i + 1] - T[i]);
else
Cb * A * der(T[i]) = Qb + A / R2 * (T[nNode2 - 1] - T[i]);
end if;
end for;
Qa = A * (Tpa - T[1]) / Ra;
Qb = A * (Tpb - T[nNode2]) / Rb;
Qa = port_a.Q_flow;
Qb = port_a1.Q_flow;
T1_end = T[1];
T2_end = T[2];
T3_end = T[3];
The issue is that some code should treat parameters with fixed=false as if they were non-parameters (and the non-parameter case is handled), but it doesn't and instead generate error messages.
That will hopefully be corrected in a future Dymola release.

Related

matlab kalman filter coding

T = 0.2;
A = [1 T; 0 1];
B = [T^2 / 2 T];
H = [1 0];
G = [0 1]';
Q = 0.00005;
R = 0.006;
x1(1) = 0;
x2(1) = 0;
x1e(1) = 0;
x2e(1) = 0;
xest = [x1e(1) x2e(1)]';
x1p(1) = 0;
x2p(1) = 0;
PE = [R 0; 0 0];
PP = A * PE(1) * A' + Q;
for i= 1:25
if i < 10
u = 0.25;
else
u = 0;
end
x1(i+1) = x1(i) + T * x2(i) + (T^2 / 2) * u;
x2(i+1) = x2(i) + T * u + sqrt(Q) * randn;
y(i+1) = x1(i+1) + sqrt(R) * randn;
PP = A * PE * A' + G * Q * G';
K = PP * H' * inv(H * PP * H' + R);
PE = [eye(2) - K * H] * PP;
xpredict = A * xest + B * u;
xest = xpredict + K * (y(i+1) -H * xpredict);
x1e(i+1) = [1 0] * xest;
x2e(i+1) = [0 1] * xest;
end
Unable to perform assignment because the left and right sides have a different number of elements.
Error in rrrr (line 34)
x1e(i+1) = [1 0] * xest;
how i can solve the error
xpredict must be a 2x1 vector. To solve it, you need to transpose B in line 5 i.e., B = [T^2 / 2 T]',
Since from Newton's laws of motion with constant velocity, we have

MATLAB: Fitting experimental data to an ODE model, and performing optimization (Kinetic modeling)

I have experimental data (concentration vs. time), and I have a set of ODEs as a model to fit them to give the unknown parameters (k).
However, I have this error message: "Unable to perform assignment because the size of the left side is 1-by-1 and the size of the right side is 1-by-9.", I checked and it belonged to opt function.
Also, I do not have a solution for my ODEs?!
Best regards,
global td cd ci ki
T = readtable('experimental_data.xlsx','Sheet',2);
T1= T.Variables;
td= T1(:,1); %experimet time
cd= [T1(:,4), T1(:,3),zeros(9,1),T1(:,2),T1(:,5),zeros(9,1),T1(:,6),T1(:,7),T1(:,8)]; %species concentration
ci= [0.136 0.193 0 0.271 0 0 0 0 0]'; %initial concentration
ki= [1 1 1 1 1 1 1 1 1 1]'; %initial k guess
opt = fminsearch(#(k) optim(td,cd,k,ci), ki); %optimization function (ERROR)
function dc = diff(k,t,C)
r1= (-k(1)* C(1))/(1+ k(10) * C(5));
r2= (k(1) * C(1) - k(2) * C(2))/(1+ k(10) * C(5));
r3= (k(2) * C(2) - k(3) * C(3))/(1+ k(10) * C(5));
r4= (k(3) * C(3) - k(3) * C(3))/(1+ k(10) * C(5));
r5= (k(4) * C(4) - k(5) * C(5) -k(6)*C(5))/(1+ k(10) * C(5));
r6= (k(6) * C(5) - k(7) * C(6) -k(8)*C(6))/(1+ k(10) * C(5));
r7= (k(8) * C(6) - k(9) * C(7))/(1+ k(10) * C(5));
r8= (k(5) * C(5) + k(7) * C(6))/(1+ k(10) * C(5));
r9= (k(9) * C(7))/(1+ k(10) * C(5));
dc = [r1;r2;r3;r4;r5;r6;r7;r8;r9];
end
function SSE = optim(td,cd,k,ci)
global td cd ci
f = #(t,C) diff(k,t,C);
[tm, cm] = ode45(f, td, ci); %cm is the concentration predicted by the model
err = cd - cm;
SSE = sum(err.^2); %sum squared-error.
end

I have a problem with this code. some idea?

I have the next code: BD_MACRO, but when rum, I obtain this answer in command window in matlab
BD_MACROPA
Subscript indices must either be real positive integers or
logicals.
Error in BD_MACROPA (line 27)
dH = betaH((aH - bH) * H(i-1) + phi * h(i-1)) -
alphaH * Z(i-1);
[![% MODELO BASICO
dt=0.01; % pasos en el tiempo
tiempo=2000; % anos de evolucion
%CONDICIONES INICIALES
H(1)=100; h(1)=250; Z(1)=200; t(1)=0;
%PARAMETROS
%ADULTOS Y PREADULTOS
aH = 147.9;
ah = 34.25;
bh = 0.20;
bH = 0.20;
phi= 0.25;
ro = 34.24;
muZ = 0.15;
H0 = 10.0;
alphaH = 0.000001;
alphah = 0.000001;
lambda = 6.0;
betaH = 0.095;
betah = 0.095;
rand('state',sum(100*clock)); % semilla de generador de numeros aleatorios,
%
numreps=5000; % numero de realizaciones
for j=1:numreps
for i=2:tiempo
dH = betaH((aH - bH) * H(i-1) + phi * h(i-1)) - alphaH * Z(i-1);
dh = betah((ah - bh) * h(i-1) + ro * H(i-1)) - alphah * Z(i-1);
dZ = Z(i-1) * (lambda * H(i-1)./(H0 + H(i-1)) + (r - mu + (bH + bh)) - alpha * (H(i-1) + Z(i-1))./H(i-1));
H(i) = H(i-1) + dt * dH;
h(i) = h(i-1) + dt * dh;
Z(i) = Z(i-1) + dt * dZ;
t(i) = t(i-1) + dt;
end
end
% Create figure
figure1 = figure;
% Create axes
axes1 = axes('Parent',figure1);
hold(axes1,'on');
% Create multiple lines using matrix input to plot
plot1 = plot(t,H,t,P,t,Z,'LineWidth',2);
set(plot1(1),'DisplayName','H');
set(plot1(2),'DisplayName','P','LineStyle','--');
set(plot1(3),'DisplayName','Z','LineStyle','.-');
%plot(t,H,t,P)
% Create xlabel
xlabel('Tiempo');
% Create ylabel
ylabel('Tamaño de la Población');
box(axes1,'on');
grid(axes1,'on');
% Set the remaining axes properties
set(axes1,'FontSize',12);
% Create legend
legend1 = legend(axes1,'show');
set(legend1,'FontSize',20);
legend('Hospederos Adultos', 'Hospederos Preadultos','Zoosporas');][1]][1]
You are probably missing a * sign in the statement
dH = betaH((aH - bH) * H(i-1) + phi * h(i-1)) - alphaH * Z(i-1);
Right now, this is trying to access 14833-th element of dH whereas you define dH as a scalar. I think what you want is
dH = betaH*((aH - bH) * H(i-1) + phi * h(i-1)) - alphaH * Z(i-1);
Same issue with dH. You also have undeclared variables alpha, mu and r in the statement
dZ = Z(i-1) * (lambda * H(i-1)./(H0 + H(i-1)) + (r - mu + (bH + bh)) - alpha * (H(i-1) + Z(i-1))./H(i-1));

OpenModelica complains about a negative value which can't be negative

Following this question I have modified the energy based controller which I have described here to avoid negative values inside the sqrt:
model Model
//constants
parameter Real m = 1;
parameter Real k = 2;
parameter Real Fmax = 3;
parameter Real x0 = 1;
parameter Real x1 = 2;
parameter Real t1 = 5;
parameter Real v0 = -2;
//variables
Real x, v, a, xy, F, vm, K;
initial equation
x = x0;
v = v0;
equation
v = der(x);
a = der(v);
m * a + k * x = F;
algorithm
if time < t1 then
xy := x0;
else
xy := x1;
end if;
K := Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2;
if abs(xy - x) < 1e-6 then
F := k * x;
else
if K > 0 then
vm := sign(xy - x) * sqrt(2 * K / m);
F := Fmax * sign(vm - v);
else
F := Fmax * sign(x - xy);
end if;
end if;
annotation(
experiment(StartTime = 0, StopTime = 20, Tolerance = 1e-06, Interval = 0.001),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "euler"));
end Model;
However, it keeps giving me the error:
The following assertion has been violated at time 7.170000
Model error: Argument of sqrt(K / m) was -1.77973e-005 should be >= 0
Integrator attempt to handle a problem with a called assert.
The following assertion has been violated at time 7.169500
Model error: Argument of sqrt(K / m) was -6.5459e-006 should be >= 0
model terminate | Simulation terminated by an assert at the time: 7.1695
STATISTICS 
Simulation process failed. Exited with code -1.
I would appreciate if you could help me know what is the problem and how I can solve it.
The code you created does event localization to find out when the condition in the if-statements becomes true and/or false. During this search it is possible that the expression in the square-root becomes negative although you 'avoided' it with the if-statement.
Try reading this and to apply the solution presented there. Spoiler: It basically comes down to adding a noEvent() statement for you Boolean condition...

Matlab error in ode45 or fourth-order Runge-Kutta method to solve a system of coupled ODEs

I am a beginner at Matlab programming and with the Runge-Kutta method as well.
I'm trying to solve a system of coupled ODEs using a 4th-order Runge-Kutta method for my project work.
here is my problem...
G = 1.4;
g = 1.4;
k = 0;
z = 0;
b = 0.166667;
syms n;
x2 = symfun(sym('x2(n)'),[n]);
x1 = symfun(sym('x1(n)'),[n]);
x3 = symfun(sym('x3(n)'),[n]);
x4 = symfun(sym('x4(n)'),[n]);
x5 = symfun(sym('x5(n)'),[n]);
k1 = [x2 * x1 *n *(1 - z * x2)*(x1 - n) - 2 * x3 * n *(1 - z * x2) - x4^2 * x2 *(1 - z * x2)- G *x3 *x2 ]./ [( G * x3 - (x1 - n)^2 * x2 *(1 - z * x2)) * n];
k2 = [x2 * (1 - z * x2)*(x1 * x2 * ( x1 - 2 *n)*( x1 - n) + 2* x3 * n + x4^2 * x2 ) ]./ [( G * x3 - (x1 - n)^2 * x2 *(1 - z * x2)) * n * (x1 - n)];
k3 = [x3 * x2 * (2 * n * x1 - n)^2 * ( 1 - z * x2) + G * x1 * (x1 - 2 *n)* (x1 - n) + x4^2 * G]./ [( G * x3 - (x1 - n)^2 * x2 *(1 - z * x2)) * n * (x1 - n)];
k4 = [x4 * ( x1 + n)] ./ [n * (x1- n)];
k5 = - [x5] ./ [n * (x1- n)];
f = #(n,x) [k1; k2; k3; k4; k5];
[n,xa] = ode45(f,[0 1],[1-b 1/b 1-b 0.01 0.02]);
errors are
Error using odearguments (line 93)
#(N,X)[K1;K2;K3;K4;K5] returns a vector of length 1, but the length
of initial conditions vector is 5. The vector returned by
#(N,X)[K1;K2;K3;K4;K5] and the initial conditions vector must have
the same number of elements.
Error in ode45 (line 114)
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs,
odeFcn, ...
Please guide me how can I solve the above problem with fourth-order Runge-Kutta method...
The error stems from using symbolic functions (mixed with a function handle) with a numeric solver.
You need to create numeric functions for ode45 to function properly (I also replaced all of the [ and ] with ( and ) for grouping):
G = 1.4;
g = 1.4;
k = 0;
z = 0;
b = 0.166667;
k1 = #(n,x) (x(2) * x(1)*n *(1 - z * x(2))*(x(1) - n) - 2 * x(3) * n *(1 - z * x(2)) - x(4)^2 * x(2) *(1 - z * x(2))- G *x(3) *x(2)) ./ (( G * x(3) - (x(1) - n)^2 * x(2) *(1 - z * x(2))) * n);
k2 = #(n,x) (x(2) * (1 - z * x(2))*(x(1) * x(2) * ( x(1) - 2 *n)*( x(1) - n) + 2* x(3) * n + x(4)^2 * x(2) )) ./ (( G * x(3) - (x(1) - n)^2 * x(2) *(1 - z * x(2))) * n * (x(1) - n));
k3 = #(n,x) (x(3) * x(2) * (2 * n * x(1) - n)^2 * ( 1 - z * x(2)) + G * x(1) * (x(1) - 2 *n)* (x(1) - n) + x(4)^2 * G)./ (( G * x(3) - (x(1) - n)^2 * x(2) *(1 - z * x(2))) * n * (x(1) - n));
k4 = #(n,x) (x(4) * (x(1) + n)) ./ (n * (x(1)- n));
k5 = #(n,x) - x(5) ./ (n * (x(1)- n));
f = #(n,x) [k1(n,x); k2(n,x); k3(n,x); k4(n,x); k5(n,x)];
[n,xa] = ode45(f,[0 1],[1-b 1/b 1-b 0.01 0.02]);
This runs for my install of Matlab.
However, the output is all NaNs since the function produce Infs; I may have introduced errors by replacing the brackets, but I don't know what the actual equations are so I'm going to leave that to you. :)
In general, you consult first the documentation. If not available, use the search engine of you choice, for instance https://www.google.de/search?q=matlab+ode45+example to find as first result https://de.mathworks.com/help/matlab/ref/ode45.html
There you find a multidimensional example
function dy = rigid(t,y)
dy = zeros(3,1); % a column vector
dy(1) = y(2) * y(3);
dy(2) = -y(1) * y(3);
dy(3) = -0.51 * y(1) * y(2);
end
options = odeset('RelTol',1e-4,'AbsTol',[1e-4 1e-4 1e-5]);
[T,Y] = ode45(#rigid,[0 12],[0 1 1],options);
plot(T,Y(:,1),'-',T,Y(:,2),'-.',T,Y(:,3),'.')
that could be used as a blueprint. In the related page https://de.mathworks.com/help/matlab/math/ordinary-differential-equations.html they use a different syntax closer to yours for the van der Pol Equation in the first example
function dydt = vdp1(t,y)
dydt = [y(2); (1-y(1)^2)*y(2)-y(1)];
end
[t,y] = ode45(#vdp1,[0 20],[2; 0]);
plot(t,y(:,1),'-',t,y(:,2),'--')
title('Solution of van der Pol Equation, \mu = 1');
xlabel('time t');
ylabel('solution y');
legend('y_1','y_2')
Following these examples, rewrite your code as
G = 1.4;
g = 1.4;
k = 0;
z = 0;
b = 0.166667;
function dotx = dxdn(n,x)
t1 = n*(x1-n)
t2 = x(2)*(1 - z * x(2))
den123 = ( G * x(3) - (x(1) - n)^2 * t2)
k1 = ( x(1)*t1*t2 - 2 * x(3) * n *(1 - z * x(2)) - x(4)^2 * t2- G *x(3) *x(2) ) / (den123*n);
k2 = ( t2*(x(1) * x(2) * ( x(1) - 2 *n)*( x(1) - n) + 2* x(3) * n + x(4)^2 * x(2) ) ) / (den123 * t1);
k3 = ( x(3) * (2 * n * x(1) - n)^2 * t2 + G * x(1) * (x(1) - 2 *n)* (x(1) - n) + x(4)^2 * G ) / (den123*t1);
k4 = ( x(4) * ( x(1) + n) ) / t1;
k5 = - x(5) / t1;
dotx = [k1; k2; k3; k4; k5];
end
[n,xa] = ode45(#dxdn,[0.001 1],[1-b; 1/b; 1-b; 0.01; 0.02]);
Because of the division by zero at n=0 you can not start the iteration in that singularity. In the code above, this is mitigated by starting with some (very) small positive n, you can also try starting with n=1e-8 or smaller. The slope will be very large in all components, so the integration may be slow, and the result might be not overly exact close to zero. For the correct handling of singular ODE ask in the math.stackexchange forum.