Hi, I am trying to build a simulation system for chemical systems engineering. Right now I am implementing stirred tanks, but I have problems formulating the material balance. In general a tank will have a number of properties (composition, volume of liquid/gas, total volume which is a fixed parameter, etc.), and among these there will be entering/exiting material flows, modelled as connectors. Since I do not know how many flows I am going to get for a particular instance of a tank, I made a parent class that implements the mass balance over the tank; in order to write the mass balance for an undetermined number of flows, I must be able to use an array formulation. See this simplified example:
-----------------------
connector Flow
import Modelica.SIunits.EnthalpyFlowRate;
import Modelica.SIunits.MoleFraction;
flow MolarFlowRate F;
MoleFraction[5] z;
end Flow;
class StupidTank
import Modelica.SIunits.AmountOfSubstance;
import Modelica.SIunits.MoleFraction;
Flow[2] flows;
Real[5] n(start={1,0,0,1,0}) "Moles of each species.";
Real n_tot "Total moles.";
Real[5] z "Overall mole fractions";
equation
n_tot = sum(n);
z = n / n_tot;
for i in 1:5 loop
der(n[i]) = flows[1].F * flows[1].z[i] + flows[2].F *
flows[2].z[i];
// der(n[i]) = sum( array(flows[j].F*flows[j].z[i] for j
in 1:2 ) );
end for;
flows[1].z = z;
flows[2].z = z;
end StupidTank;
class Test
import Modelica.SIunits.Pressure;
import Modelica.SIunits.Temperature;
StupidTank tank;
equation
tank.flows[1].F = -2;
tank.flows[2].F = 1;
end Test;
--------------------------
The problem is that using the clumsy:
der(n[i]) = flows[1].F * flows[1].z[i] + flows[2].F * flows[2].z[i];
simulation works; if I use the other formulation, which I need for the generic
case in which the array flow has "m" components,
der(n[i]) = sum( array(flows[j].F*flows[j].z[i] for j in 1:2 ) );
instantiateModel works, but simulate() crashes, claiming a lot of issues
with "internal error code generation of expression [... somewhere including
<reduction> ...] failed".
Am I writing the equation wrong? InstantiateModel() produces the following
suspicious lines:
---------------------
der(tank.n[1]) = sum(<reduction>array(tank.flows[j].F * tank.flows[j].z[1] for
j in {1:2}));
---------------------
What is that "<reduction>"? Does it indicate a syntax error?
Cheers,
-Federico
signature.asc
Description: This is a digitally signed message part.
