I have a local model, which when I check in Dymola claims to have 35 variables and 34 unknowns, while when I check exactly the same model in OMEdit it is balanced at 34/34. In determining what counts as a variable, do all inputs and outputs get included?
Below is my model:
model HeatStorage
extends PentakomoPlant.Interfaces.StorageFluidHeat;
parameter SI.Diameter D = 18.667 "Diameter";
parameter SI.Height H = 20 "Height";
parameter Boolean enable_losses = false "= true enable thermal losses with environment"
parameter SI.CoefficientOfHeatTransfer alpha = 1 "Constant heat transfer coefficient with the ambient"
parameter Boolean use_L = false "= true to display level as output variable"
parameter Real L_start = 0 "Start value of level in %"
parameter SI.Temperature T_start = from_degC(300) "Start value of temperature"
parameter SI.Temperature T_max = from_degC(550) "Maximum tank temperature"
parameter SI.Temperature T_set = from_degC(300) "Tank Heater Temperature Set-Point"
parameter SI.Power W_max = 30e8 "Hot Tank Heater Capacity"
parameter SI.Efficiency e_ht = 0.99 "Tank Heater Efficiency"
SI.Volume V;
SI.Mass m;
Medium.BaseProperties medium;
SI.Area A;
SI.HeatFlowRate Q_losses;
Medium.ThermodynamicState state_i = Medium.setState_pTX(medium.p, T_start);
SI.Power W_net;
SI.Power W_loss;
Modelica.Blocks.Interfaces.RealOutput L if use_L "Tank level in %"
Modelica.Blocks.Interfaces.RealInput T_amb if enable_losses
Modelica.Blocks.Interfaces.RealInput Q_heater
SI.HeatFlowRate Q_PB "Heat Flow to PowerBlock";
SI.HeatFlowRate Q_desal "Heat Flow to Desalination";
protected
parameter SI.Volume V_t = H * pi * D ^ 2 / 4;
Modelica.Blocks.Interfaces.RealOutput L_internal;
initial equation
medium.h = Medium.specificEnthalpy(state_i);
m = Medium.density(state_i) * V_t;
equation
if use_L then
connect(L_internal, L);
end if;
if enable_losses then
connect(T_amb_internal, T_amb);
Q_losses = -0.939 * exp(to_degC(medium.T) * 0.005111) * 1000;//*5/7;
else
T_amb_internal = Medium.T_default;
Q_losses = 0;
end if;
fluid_a.p = medium.p;
fluid_b.p = medium.p;
fluid_a.h_outflow = medium.h;
fluid_b.h_outflow = medium.h;
der(m) = fluid_a.m_flow + fluid_b.m_flow;
m * der(medium.h) + der(m) * medium.h = Q_losses + Q_PB + Q_desal + W_net + fluid_a.m_flow * inStream(fluid_a.h_outflow) + fluid_b.m_flow * medium.h;
V = m / medium.d;
L_internal = 100 * (max(medium.T, T_set) - T_set) / (T_max - T_set);
A = 2 * pi * (D / 2) * H;
W_net = Q_heater;
W_loss = W_net / e_ht;
//PowerBlock
heat_PB.Q_flow = Q_PB;
heat_DS.Q_flow = Q_desal;
heat_PB.T = medium.T;
heat_DS.T = medium.T;
end HeatStorage;
With:
partial model StorageFluidHeat
extends SolarTherm.Icons.StorageModel;
Modelica.Fluid.Interfaces.FluidPort_a fluid_a(redeclare package Medium = Medium)
Modelica.Fluid.Interfaces.FluidPort_b fluid_b(redeclare package Medium = Medium)
replaceable package Medium = SolarTherm.Media.MoltenSalt.MoltenSalt_base
constrainedby Modelica.Media.Interfaces.PartialMedium
"Medium in the component"
Modelica.Thermal.HeatTransfer.Interfaces.HeatPort_b heat_PB
Modelica.Thermal.HeatTransfer.Interfaces.HeatPort_b heat_DS
end StorageFluidHeat;
And (for the base properties of the medium):
redeclare model extends BaseProperties(final standardOrderComponents=true)
"Base properties of medium"
equation
d = rho_T(T);
h = state.h;
u = h - p/d;
MM = 0.091438;
R = 8.3144/MM;
state.p = p;
T = T_h(h);
end BaseProperties;
I am struggling to find where the extra variable may be, or why it might be different when using Dymola/OMEdit.
I have tried following the advice from ELmqvist about balancing models, so in counting variables would I be right in assuming (For this model):
8 x Specialised class variables
3 x Inputs
2 x Output
7 x Medium Base Property Variables
2 x 5 x Fluid port Variables
2 x 2 x Heat port Variables
= 34 variables
Am I missing something?
The model is not complete with all libraries to enable testing (assumedly there are similar issues with other media), so this will be an incomplete answer.
In addition it seems likely that some equations and variables could be removed to simplify the model.
But there are some concerns with the model:
T_amb_internal is unused and undeclared; that looks odd.
You normally need equations for two intensive properties for simple medium: e.g. p, h or p, T. Here medium.h is determined due to the differential equations, but not medium.p. (The mass also has a differential equation and can vary.)
Note that testing of a local model can be tricky - and you should preferably include it in a test-circuit as well.
Related
I'm trying to model a natural convection case using Modelica.Media package and I've set up a simple grid of 6x6 fixed-volume zones (code attached) which implements mass and energy conservation. Those zones are connected to a no-flow boundary condition model and communicate through a flow model.
But when simulating it, I got a set of non-linear equations during all the steps, related to the calculation of the state p, h, Xi of each zone (figure attached).
Do you know I can set it up to avoid that? Maybe a different set of initial conditions? Any help is much appreciated!
model Zone
replaceable package Medium = Modelica.Media.Interfaces.PartialMedium annotation(choicesAllMatching = true);
Medium.BaseProperties medium(p(start=101325, fixed=false),
T(start=293.15, fixed=false));
Test.Port portT(redeclare package Medium = Medium);
Test.Port portB(redeclare package Medium = Medium);
Test.Port portL(redeclare package Medium = Medium);
Test.Port portR(redeclare package Medium = Medium);
parameter Medium.AbsolutePressure P_ambient = 101325;
Medium.Temperature T_ambient = Units.Conversions.from_degC(20);
Medium.MassFraction X_ambient[Medium.nX] = Medium.X_default;
...
initial equation
medium.p = P_ambient;
medium.T = T_ambient;
medium.Xi = X_ambient[1:Medium.nXi];
equation
...
m = V * medium.d;
U = m * medium.u;
mXi = m * medium.Xi;
der(m) = portL.mf + portR.mf + portT.mf + portB.mf;
der(U) = portL.hf + portR.hf + portT.hf + portB.hf + portL.q + portR.q + portT.q + portB.q;
der(mXi) = portL.mXif + portR.mXif + portT.mXif + portB.mXif;
On the flow model, I`m using
portI.mf = mf;
portI.hf = semiLinear(portI.mf, portI.h, portJ.h);
portI.mXif = semiLinear(portI.mf, portI.Xi, portJ.Xi);
portI.q = q;
portI.mf + portJ.mf = 0;
portI.hf + portJ.hf = 0;
portI.q + portJ.q = 0;
portI.mXif + portJ.mXif = zeros(Medium.nXi);
Connector
Medium.Temperature T;
flow Units.HeatFlowRate q;
Medium.AbsolutePressure p;
flow Medium.MassFlowRate mf;
Medium.SpecificEnthalpy h;
flow Medium.EnthalpyFlowRate hf;
Medium.MassFraction Xi[Medium.nXi];
flow Medium.MassFlowRate mXif[Medium.nXi];
Non Linear Warning Image
I've just found the problem, my set of equations was not complete and initialization was not helpful at all.
Following this question, I modified my code to:
model test
// types
type Mass = Real(unit = "Kg", min = 0);
type Length = Real(unit = "m");
type Area = Real(unit = "m2", min = 0);
type Force = Real(unit = "Kg.m/s2");
type Pressure = Real(unit = "Kg/m/s2");
type Torque = Real(unit = "Kg.m2/s2");
type Velocity = Real(unit = "m/s");
type Time = Real(unit = "s");
// constants
constant Real pi = 2 * Modelica.Math.asin(1.0);
parameter Mass Mp = 0.01;
parameter Length r1 = 0.010;
parameter Length r3 = 0.004;
parameter Integer n = 3;
parameter Area A = 0.020 * 0.015;
parameter Time Stepping = 1.0;
parameter Real DutyCycle = 1.0;
parameter Pressure Pin = 500000;
parameter Real Js = 1;
//parameter Real Muk = 0.0;
parameter Real Muk = 0.158;
// variables
Length x[n];
Velocity vx[n];
Real theta;
Real vt;
Pressure P[n];
Force Fnsp[n];
Torque Tfsc;
initial equation
theta = 0;
vt = 0;
algorithm
for i in 1:n loop
if noEvent((i - 1) * Stepping < mod(time, n * Stepping)) and noEvent(mod(time, n * Stepping) < Stepping * ((i - 1) + DutyCycle)) then
P[i] := Pin;
else
P[i] := 0;
end if;
end for;
Tfsc := -r3 * Muk * sign(vt) * abs(sum(Fnsp));
equation
vx = der(x);
vt = der(theta);
x = r1 * {sin(theta + (i - 1) * 2 * pi / n) for i in 1:n};
Mp * der(vx) + P * A = Fnsp;
Js * der(theta) = Tfsc - r1 * Fnsp * {cos(theta + (i - 1) * 2 * pi / n) for i in 1:n};
// Js * der(theta) = - r1 * Fnsp * {cos(theta + (i - 1) * 2 * pi / n) for i in 1:n};
annotation(
experiment(StartTime = 0, StopTime = 30, Tolerance = 1e-06, Interval = 0.03),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));
end test;
However, I get the preprocessing warning of
[1] .... Translation Warning
Iteration variables with default zero start attribute in torn nonlinear equation system:
Fnsp[3]:VARIABLE(unit = "Kg.m/s2" ) type: Real [3]
Fnsp[2]:VARIABLE(unit = "Kg.m/s2" ) type: Real [3]
Fnsp[1]:VARIABLE(unit = "Kg.m/s2" ) type: Real [3]
$DER.vt:VARIABLE() type: Real
which doesn't make sense but I assume I can safely ignore, and the compiling error of:
Matrix singular!
under-determined linear system not solvable
which had also been previously reported here. if I remove the lines
Torque Tfsc;
and
Tfsc := -r3 * Muk * sign(vt) * abs(sum(Fnsp));
and changing
Js * der(theta) = - r1 * Fnsp * {cos(theta + (i - 1) * 2 * pi / n) for i in 1:n};
works perfectly fine. However, setting Muk to zero, which theoretically the same thing leads to the same error as above! I would appreciate if you could help me know what is the problem and how I can resolve it.
P.S.1. On the demo version of Dymola the simulation test finishes with no errors, only the warning:
Some variables are iteration variables of the initialization problem:
but they are not given any explicit start values. Zero will be used.
Iteration variables:
der(theta, 2)
P[1]
P[2]
P[3]
P.S.2. Using JModelica, removing the noEvent and using the python code:
model_name = 'test'
mo_file = 'test.mo'
from pymodelica import compile_fmu
from pyfmi import load_fmu
my_fmu = compile_fmu(model_name, mo_file)
myModel = load_fmu('test.fmu')
res = myModel.simulate(final_time=30)
theta = res['theta']
t = res['time']
import matplotlib.pyplot as plt
plt.plot(t, theta)
plt.show()
it solves the model blazingly fast for small values (e.g. 0.1) of Muk. But again it gets stuck for bigger values. The only warnings are:
Warning at line 30, column 3, in file 'test.mo':
Iteration variable "Fnsp[2]" is missing start value!
Warning at line 30, column 3, in file 'test.mo':
Iteration variable "Fnsp[3]" is missing start value!
Warning in flattened model:
Iteration variable "der(_der_theta)" is missing start value!
You do not need to use algorithm for the assignments of equations (even if they are in a for-loop and an if). I moved them to the equation section and removed your algorithm section completely:
model test
// types
type Mass = Real(unit = "Kg", min = 0);
type Length = Real(unit = "m");
type Area = Real(unit = "m2", min = 0);
type Force = Real(unit = "Kg.m/s2");
type Pressure = Real(unit = "Kg/m/s2");
type Torque = Real(unit = "Kg.m2/s2");
type Velocity = Real(unit = "m/s");
type Time = Real(unit = "s");
// constants
constant Real pi = 2 * Modelica.Math.asin(1.0);
parameter Mass Mp = 0.01;
parameter Length r1 = 0.010;
parameter Length r3 = 0.004;
parameter Integer n = 3;
parameter Area A = 0.020 * 0.015;
parameter Time Stepping = 1.0;
parameter Real DutyCycle = 1.0;
parameter Pressure Pin = 500000;
parameter Real Js = 1;
//parameter Real Muk = 0.0;
parameter Real Muk = 0.158;
// variables
Length x[n];
Velocity vx[n];
Real theta;
Real vt;
Pressure P[n];
Force Fnsp[n];
Torque Tfsc;
initial equation
theta = 0;
vt = 0;
equation
for i in 1:n loop
if noEvent((i - 1) * Stepping < mod(time, n * Stepping)) and noEvent(mod(time, n * Stepping) < Stepping * ((i - 1) + DutyCycle)) then
P[i] = Pin;
else
P[i] = 0;
end if;
end for;
Tfsc = -r3 * Muk * sign(vt) * abs(sum(Fnsp));
vx = der(x);
vt = der(theta);
x = r1 * {sin(theta + (i - 1) * 2 * pi / n) for i in 1:n};
Mp * der(vx) + P * A = Fnsp;
Js * der(theta) = Tfsc - r1 * Fnsp * {cos(theta + (i - 1) * 2 * pi / n) for i in 1:n};
// Js * der(theta) = - r1 * Fnsp * {cos(theta + (i - 1) * 2 * pi / n) for i in 1:n};
end test;
This makes it far easier for the compiler to find a sensible sorting and tearing for the strong components. This still breaks at 19s but before that it may just be what you are looking for. The newton solver diverges after that threshold, since i don't really know what you are doing here i unfortunately cannot provide any analysis of the results.
Also it seems like your event triggered by your if-equation could be cleanly replaced by a Sample operator. You might want to have a look at that.
I'm trying to solve the code
model modelTest
// types
type Mass = Real (unit = "Kg", min = 0);
type Length = Real (unit = "m");
type Area = Real (unit = "m2", min = 0);
type Force = Real (unit = "Kg.m/s2");
type Pressure = Real (unit = "Kg/m/s2");
type Torque = Real (unit = "Kg.m2/s2");
type Velocity = Real (unit = "m/s");
type Time = Real (unit = "s");
// constants
constant Real pi = 2 * Modelica.Math.asin(1.0);
parameter Mass Mp = 0.01;
parameter Length r1 = 0.010;
parameter Integer n = 3;
parameter Area A = 0.020 * 0.015;
parameter Time Stepping = 0.1;
parameter Real DutyCycle = 0.5;
parameter Pressure Pin = 5000;
parameter Real Js = 1;
// variables
Length x[n];
Velocity vx[n];
Real theta;
Real vt;
Pressure P[n];
initial equation
theta = 0;
vt = 0;
algorithm
for i in 1:n loop
if noEvent((i - 1) * Stepping < mod(time, Stepping)) and noEvent(mod(time, Stepping) < (i - 1) * Stepping + Stepping * DutyCycle) then
P[i] := Pin;
else
P[i] := 0;
end if;
end for;
equation
vx = der(x);
vt = der(theta);
x = r1 * {sin(theta + (i -1) * 2 * pi / n) for i in 1:n};
Js * der(theta) = r1 * sum((Mp * der(vx) + P * A) .* {cos(theta + (i -1) * 2 * pi / n) for i in 1:n});
annotation(
experiment(StartTime = 0, StopTime = 10, Tolerance = 1e-6, Interval = 0.01),
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));
end modelTest;
but the solver never finishes showing the error:
Failed to solve linear system of equations (no. 51) at time ... Residual norm is ...
The default linear solver fails, the fallback solver with total pivoting at time ... that might riase plv LOG_LS.
I would appreciate if you could help me know what is the problem and how I can solve it. Thanks for your support in advance.
P.S.1. I found this similar issue from 15 months ago.
P.S.2. There were several mistakes in the code. A modified version can be found here.
I am trying to approximate a function (the right hand side of a differential equation) of the form ddx=F(x,dx,u) (where x,dx,u are scalars and u is constant) with an RBF neural network. I have the function F as a black box (I can feed it with initial x,dx and u and take x and dx for a timespan I want) and during training (using sigma-modification) I am getting the following response plotting the real dx vs the approximated dx.
Then I save the parameters of the NN (the centers and the stds of the gaussians, and the final weights) and perform a simulation using the same initial x,dx and u as before and keeping, of course, the weights stable this time. But I get the following plot.
Is that logical? Am I missing something?
The training code is as follows:
%load the results I got from the real function
load sim_data t p pd dp %p is x,dp is dx and pd is u
real_states = [p,dp];
%down and upper limits of the variables
p_dl = 0;
p_ul = 2;
v_dl = -1;
v_ul = 4;
pd_dl = 0;%pd is constant each time,but the function should work for different pds
pd_ul = 2;
%number of gaussians
nc = 15;
x = p_dl:(p_ul-p_dl)/(nc-1):p_ul;
dx = v_dl:(v_ul-v_dl)/(nc-1):v_ul;
pdx = pd_dl:(pd_ul-pd_dl)/(nc-1):pd_ul;
%centers of gaussians
Cx = combvec(x,dx,pdx);
%stds of the gaussians
B = ones(1,3)./[2.5*(p_ul-p_dl)/(nc-1),2.5*(v_ul-v_dl)/(nc-1),2.5*(pd_ul-pd_dl)/(nc-1)];
nw = size(Cx,2);
wdx = zeros(nw,1);
state = real_states(1,[1,4]);%there are also y,dy,dz and z in real_states (ignored here)
states = zeros(length(t),2);
timestep = 0.005;
for step=1:length(t)
states(step,:) = state;
%compute the values of the sigmoids
Sx = exp(-1/2 * sum(((([real_states(step,1);real_states(step,4);pd(1)]*ones(1,nw))'-Cx').*(ones(nw,1)*B)).^2,2));
ddx = -530*state(2) + wdx'*Sx;
edx = state(2) - real_states(step,4);
dwdx = -1200*edx * Sx - 4 * wdx;
wdx = wdx + dwdx*timestep;
state = [state(1)+state(2)*timestep,state(2)+ddx*timestep];
end
save weights wdx Cx B
figure
plot(t,[dp(:,1),states(:,2)])
legend('x_d_o_t','x_d_o_t_h_a_t')
The code used to verify the approximation is the following:
load sim_data t p pd dp
real_states = [p,dp];
load weights wdx Cx B
nw = size(Cx,2);
state = real_states(1,[1,4]);
states = zeros(length(t),2);
timestep = 0.005;
for step=1:length(t)
states(step,:) = state;
Sx = exp(-1/2 * sum(((([real_states(step,1);real_states(step,4);pd(1)]*ones(1,nw))'-Cx').*(ones(nw,1)*B)).^2,2));
ddx = -530*state(2) + wdx'*Sx;
state = [state(1)+state(2)*timestep,state(2)+ddx*timestep];
end
figure
plot(t,[dp(:,1),states(:,2)])
legend('x_d_o_t','x_d_o_t_h_a_t')
I've got at State System, with "forced" inputs at bounds. My SS equation is: zp = A*z * B. (A is a square matrix, and B colunm)
If B is a step (along the time of experience), there is no problem, because I can use
tevent = 2;
tmax= 5*tevent;
n =100;
dT = n/tmax;
t = linspace(0,tmax,n);
u0 = 1 * ones(size(z'));
B = zeros(nz,n);
B(1,1)= utop(1)';
A = eye(nz,nz);
[tt,u]=ode23('SS',t,u0);
and SS is:
function zp = SS(t,z)
global A B
zp = A*z + B;
end
My problem is when I applied a slop, So B will be time dependent.
utop_init= 20;
utop_final = 50;
utop(1)=utop_init;
utop(tevent * dT)=utop_final;
for k = 2: tevent*dT -1
utop(k) = utop(k-1) +(( utop(tevent * dT) - utop(1))/(tevent * dT));
end
for k = (tevent * dT) +1 :(tmax*dT)
utop(k) = utop(k-1);
end
global A B
B = zeros(nz,1);
B(1,1:n) = utop(:)';
A = eye(nz,nz);
I wrote a new equation (to trying to solve), the problem, but I can't adjust "time step", and I don't get a u with 22x100 (which is the objective).
for k = 2 : n
u=solveSS(t,k,u0);
end
SolveSS has the code:
function [ u ] = solveSS( t,k,u0)
tspan = [t(k-1) t(k)];
[t,u] = ode15s(#SS,tspan,u0);
function zp = SS(t,z)
global A B
zp = A*z + B(:,k-1);
end
end
I hope that you can help!
You should define a function B that is continuously varying with t and pass it as a function handle. This way you will allow the ODE solver to adjust time steps efficiently (your use of ode15s, a stiff ODE solver, suggests that variable time stepping is even more crucial)
The form of your code will be something like this:
function [ u ] = solveSS( t,k,u0)
tspan = [t(k-1) t(k)];
[t,u] = ode15s(#SS,tspan,u0,#B);
function y = B(x)
%% insert B calculation
end
function zp = SS(t,z,B)
global A
zp = A*z + B(t);
end
end