- Index
- » Programming
- » Modelica Language
- » obtain value of a continuous time...
obtain value of a continuous time variable in the previous state-event
obtain value of a continuous time variable in the previous state-event
Hello,
I have a probably quite basic question. That is how to obtain the value of a continuous time variable in the previous state-event (for a variable time integration algorithm). I.e. how to obtain the last calculated value for a continuous time variable?
See below for the explanation and an example code.
See in the accompanying code the ImplicitExplicit model for a rough outline of my problem. In this code the pressure drop over one branch is determined as a function of the mass-flow rates in N pipe-segments. If the Boolean value calcExplicit is set to true then the friction pressure drop dp_f depens on der(M) which in turn depends on der(U). Therefore the entering mass-flow rate mflows[1] needs to be determined iteratively. When there are multiple branches than computational times increase even more. Since also the flow distribution over multiple branches needs to be determined.
Now I’m planning to reduce computational time by disconnecting the mass and energy balances from the pressure drop calculation (by setting the Boolean value calcExplicit to false).
However I’m puzzled how to implement this. I would appreciate some pointers in the right direction.
Regards, Arne
model ImplicitExplicit
parameter Boolean calcExplicit
"if true than dp_f is no longer influenced by the mass and energy balances";
parameter Integer N = 10;
parameter Real L = 1;
parameter Real D = 0.025;
parameter Real T_in = 300;
//parameter Real m_flow_in = 1;
parameter Real p_in = 1e6;
parameter Real p_out = 0;
parameter Real Q_flow = 1000;
parameter Real R = 1e6;
Real[N+1] T(each start=500);
Real[N+1] rho;
Real[N+1] h;
Real[N] M;
Real[N] U;
Real[N] dMdt;
//Real h_A;
Real[N+1] m_flows;
Real[N] dp_f;
Real[N+1] m_flows_exp "approximated mass flow rates";
Real[N+1] rho_exp "density in the previous event";
Real[N] dMdt_exp "derivate of the mass flow rates in the previous time instant";
protected
parameter Real A = 0.25*Modelica.Constants.pi*D^2;
parameter Real V = A*L;
equation
// boundary conditions
T[1] = T_in;
// EXTRA CODE
m_flows_exp[1]=m_flows[1];
rho_exp[1] = rho[1];
for i in 1:N loop
dMdt_exp[i] = m_flows_exp[i] - m_flows_exp[i+1];
if calcExplicit then
dMdt_exp[i] = der(M[i]); // How to obtain the value of dMdt as calculated in the previous time event
rho_exp[i+1] = rho[i+1]; // ...
else
dMdt_exp[i] = pre(dMdt[i]); // THIS DOES NOT WORK ?????????;
rho_exp[i+1] = pre(rho[i]); // THIS DOES NOT WORK ?????????;
end if;
end for;
// calculations
dMdt = der(M);
for i in 1:N+1 loop
rho[i] = 2090 - 0.636*T[i];
h[i] = 1443*T[i] + 0.172/2*T[i]^2;
end for;
for i in 1:N loop
M[i] = rho[i+1]*V;
U[i] = M[i]*h[i+1];
der(M[i]) = m_flows[i] - m_flows[i+1];
der(U[i]) = m_flows[i]*h[i] - m_flows[i+1]*h[i+1] + Q_flow;
dp_f[i] = Modelica.Fluid.Pipes.BaseClasses.WallFriction.Detailed.pressureLoss_m_flow(
m_flows_exp[i],
rho_exp[i],
rho_exp[i],
0.035,
0.035,
L,
D,
0.04/1000);
//dp_f[i] = sign(m_flows[i+1])*R*m_flows[i+1]^2;
end for;
p_out = p_in - sum(dp_f);
annotation ();
end ImplicitExplicit;
- Index
- » Programming
- » Modelica Language
- » obtain value of a continuous time...