- Index
- » Programming
- » Modelica Language
- » If statement errors!
If statement errors!
If statement errors!
Hello,
i am getting the following error, can some one help?
Unable to expand if-statement:
if (abs((iVoc_real-Voc_real)/Voc_real) < 0.001) then
Voc_real = iVoc_real;
else
// No equations
end if;
since the different branches have different number of equations.
If-statements where the conditions are non-parameters
must have the same number of scalar equations in all branches.
Re: If statement errors!
You can't write equations like that. It would basically be translated to either assigning to Voc_real or not assigning to anything, which means this if-equation is 0 or 1 scalar equations and the equation count will not work.
It sounds more like you want to use when/reinit or limiting Voc_real when it is calculated or you need to introduce some additional variable.
Code:
tmp = ... // What calculated Voc_real before
Voc_real = if ... then tmp else iVoc_real
- sjoelund.se
- 1700 Posts
Re: If statement errors!
This is my full code.
How do i change it?
Isc_real = Isc * (G_real / Gstc) * (1 + (Ki * (T_real - Tstc))); // eqn 2.27
IL = Isc_real + ((Isc_real * Rs) / Rsh_real); //eqn 2.29
Voc_real = Voc * (1 + Kv * (T_real - Tstc)) + ns * Vt * log(G_real / Gstc); //eqn 2.30
for count1 in 1:maxlim2 loop
iVoc_real = Wcor * Voc_real + (1 - Wcor) * (ns * Vt * log((IL * Rsh_real - Voc_real) / (I0 * Rsh_real)) * (1 + Kv * (T_real - Tstc)));
if G_real < 0.001 then
Voc_real = 0;
else
if abs((iVoc_real - Voc_real) / Voc_real) < accuracy then
Voc_real = iVoc_real;
else
end if;
end if;
end for;
Re: If statement errors!
Rewrite your code to an algorithm-section (eqn 2.30 and the for-loop as an algorithm calculating Voc_real and iVoc_real). You are writing to the same scalar variable multiple times in a for-loop which will cause problems further down the line. Modelica enforces single-assignment for equations...
- sjoelund.se
- 1700 Posts
- sjoelund.se
- 1700 Posts
Re: If statement errors!
ok. thanx. but this is a Model that I am making. How do i include an algorithm in this? can you show an example?
model Photovoltaic
extends SinglePhase.Interfaces.OnePortGrounded;
import Modelica.ComplexMath.'abs';
parameter Real Gstc = 1000;
parameter Integer maxlim2= 1000;
parameter Integer matrixsize = 10000;
parameter Real Impp_real, Pout_max, iVoc_real, Tstc=298,iIr, Vr, Ir;
parameter Integer count2, count3;
parameter Integer Vout_GranTurismo[:] = zeros( matrixsize);
parameter Integer Iout_GranTurismo[:] = zeros( matrixsize);
parameter Integer Pout_GranTurismo[:] = zeros(matrixsize);
input Real Ki, Kv, T_real;
input Integer G_real;
equation
if G_real == 0 then //to avoid division by 0
Rsh_real = 100000000000000; //close to infinite
else
Rsh_real = Rsh * (Gstc / G_real); //eqn 2.28
end if;
Isc_real = Isc * (G_real / Gstc) * (1 + (Ki * (T_real - Tstc))); // eqn 2.27
IL = Isc_real + ((Isc_real * Rs) / Rsh_real); //eqn 2.29
Voc_real= Voc * (1 + Kv * (T_real - Tstc)) + ns * Vt * log(G_real / Gstc); //eqn 2.30
for count1 in 1:maxlim2 loop
iVoc_real= Wcor * Voc_real + (1 - Wcor) * (ns * Vt * log((IL * Rsh_real - Voc_real) / (I0 * Rsh_real)) * (1 + Kv * (T_real - Tstc)));
if G_real < 0.001 then
Voc_real = 0;
else
if abs((iVoc_real - Voc_real) / Voc_real) < accuracy then
Voc_real = iVoc_real;
else
end if;
end if;
end for;
I0_GranTurismo = (Isc_real - (Voc_real - Isc_real * Rs) / Rsh_real) * exp(-Voc_real / (ns * Vt)); //eqn 2.23
Ir = Isc_real;
count2 = 0;
for Vr in 0:Voc_real / (matrixsize - 1):Voc_real loop
count3 = 1;
count2 = count2 + 1;
for count3 in 1:maxlim2 loop
iIr = Wcor * Ir + (1 - Wcor) * (IL - I0_GranTurismo * (exp((Vr + Ir * Rs) / (ns * Vt)) - 1) - (Vr + Ir * Rs) / Rsh_real);
if abs(iIr - Ir) < accuracy then
Iout_GranTurismo[count2] = iIr;
else
end if;
Ir = iIr;
Iout_GranTurismo[count2] = iIr;
count3 = count3+1; //counter to limit number of iterations
end for;
Vout_GranTurismo[count2] = Vr;
end for;
Pout_max = 0;
if G_real < 0.0001 then
Vmpp_GranTurismo = 0;
Impp_real = 0;
else
for count4 in 1:count2 loop
Pout_GranTurismo[count4] = Vout_GranTurismo[count4] * Iout_GranTurismo[count4];
if Pout_GranTurismo[count4] > Pout_max then
Vmpp_GranTurismo = Vout_GranTurismo[count4];
Impp_real = Iout_GranTurismo[count4];
else
end if;
end for;
end if;
end Photovoltaic;
Re: If statement errors!
Unable to expand if-statement:
if (firstOrder.y > 0.0001 and Throttle > 0.01 and currentGear < floor( upshiftTable.y[1])) then
shiftBegin = (if GearNumber < floor(upshiftTable.y[1]) then true else false);
shiftEnd = (if GearNumber < floor(upshiftTable.y[1]) then false else (if GearNumber == floor(upshiftTable.y[1]) then true else false));
elseif (firstOrder.y < -0.0001 or currentGear > floor(downshiftTable.y[1])) then
shiftBegin = (if GearNumber > floor(downshiftTable.y[1]) then true else false);
shiftEnd = (if GearNumber > floor(downshiftTable.y[1]) then false else (if GearNumber == floor(downshiftTable.y[1]) then true else false));
else
// No equations
end if;
Not able to rectify the problem. please help me
- Index
- » Programming
- » Modelica Language
- » If statement errors!