Abstract switch in Modelica - modelica

I would like to motivate a question I asked before about a Modelica array of partial model. Consider the following model of a switch between 2 controllers.
model Switch
input Real u;
input Integer sel;
output Real y;
protected
Real x;
equation
if sel == 1 then
y = 0.1 * (0 - u);
der(x) = 0;
else
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end if;
end Switch;
Let's ignore the fact that the PI controller may break when it is not selected for some time due to divergence of x. This can be fixed by resetting x when the PI controller is selected. However, this is not the point here.
I want to abstract this switch in 2 ways. Firstly, to switch among a parametric number of controllers. Secondly, to abstract controllers using partial models. Let Ctrl be the partial model of a controller.
partial model Ctrl
input Real u;
output Real y;
end Ctrl;
We can instantiate the two controllers embedded in the switch as follows.
model P extends Ctrl;
equation
y = 0.1 * (0 - u);
end P;
model PI extends Ctrl;
protected
Real x;
equation
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end PI;
The abstract version of the switch is supposed to be something like:
model Switch
parameter Integer N(min=1);
Ctrl c[N];
input Real u;
input Integer sel(min=1, max=N);
output Real y;
equation
for i in 1:N loop
c[i].u = u;
end for;
y = c[sel].y;
end Switch;
However, this model has some problems. Firstly, it is not clear how this model can be instantiated, e.g. with one P and one PI controller. Secondly, I get a warning which surprises me, namely: The following input lacks a binding equation: c[1].u
Is it possible to do express this abstract switch in Modelica in some way?

This doesn't work with an array of models as you cannot bind it to different models via a modification. You need to specify all the controllers you have inside the GenericSwitch. You could generate the GenericSwitch and Switch model automatically if needed.
partial model Ctrl
input Real u;
output Real y;
end Ctrl;
model P
extends Ctrl;
equation
y = 0.1 * (0 - u);
end P;
model PI
extends Ctrl;
protected
Real x;
equation
y = 0.1 * (0 - u) + 0.2 * x;
der(x) = 0 - u;
end PI;
model GenericSwitch
replaceable model MyCtrl1 = Ctrl;
replaceable model MyCtrl2 = Ctrl;
MyCtrl1 c1(u = u);
MyCtrl2 c2(u = u);
input Real u;
input Integer sel;
output Real y;
equation
y = if sel == 1 then c1.y else c2.y;
end GenericSwitch;
model Switch = GenericSwitch(
redeclare model MyCtrl1 = P,
redeclare model MyCtrl2 = PI);

I guess it should work with something like:
model GenericSwitch
parameter Integer N(min=1);
replaceable model MyCtlr = Ctrl constrainedby Ctrl;
MyCtlr c[N](each u = u);
input Real u;
input Integer sel(min=1, max=N);
output Real y;
equation
y = c[sel].y;
end GenericSwitch;
model PSwitch = GenericSwitch(redeclare model MyCtrl = P);
model PISwitch = GenericSwitch(redeclare model MyCtrl = PI);

Related

Continuous variable used in clocked when clause is automatically discretized

model test
import Modelica.Constants.pi;
Real f;
discrete Real g;
Clock clk=Clock(0.1);
equation
f = sin(pi*time);
when Clock(0.1) then
if f >= 0 then
g = (sin(pi*time)) - 0.1;
else
g = (sin(pi*time)) + 0.1;
end if;
end when;
end test;
f is assigned as a continuous function. I want to sample the value of g depended on f, but f also be changed to a discrete value. Is there anything wrong ?
The clock partitioning sees f as being used directly inside the when Clock and thus f is also seen as a clocked variable.
Use sample(f) if that is not desired:
model test
import Modelica.Constants.pi;
Real f;
discrete Real g;
Clock clk=Clock(0.1);
equation
f = sin(pi*time);
when Clock(0.1) then
if sample(f) >= 0 then
g = (sin(pi*time)) - 0.1;
else
g = (sin(pi*time)) + 0.1;
end if;
end when;
end test;
See also: Failure to handle clock inference in Dymola

An initialization warning in Dymola

In my model, I got confused that why the initial conditions are NOT fully specified.
Here are the code and screenshot:
model WithAlgebraicLoop_Right
extends Modelica.Icons.Example;
Real x;
Real y(start=1, fixed=true);
Boolean cond;
equation
cond = x > 0.5;
when pre(cond) then
y = 1*time;
end when;
x = sin(y*10*time);
end WithAlgebraicLoop_Right;
I think that during the initialization, x could be calculated from y, so cond could be calculated from x, so why doesn't Dymola do as I think?
Sure, discrete-time variable cond can be calucated according to the given equations. However, its pre-value for the event iteration at initialization is not known and must be set, either by setting a fixed start value or by an initial equation, whatever you prefer.
model WithAlgebraicLoop_Right1
Real x;
Real y(start=1, fixed=true);
Boolean cond(start=false, fixed=true);
equation
cond = x > 0.5;
when pre(cond) then
y = 1*time;
end when;
x = sin(y*10*time);
end WithAlgebraicLoop_Right1;
or
model WithAlgebraicLoop_Right2
Real x;
Real y(start=1, fixed=true);
Boolean cond;
initial equation
pre(cond) = false;
equation
cond = x > 0.5;
when pre(cond) then
y = 1*time;
end when;
x = sin(y*10*time);
end WithAlgebraicLoop_Right2;

Modelica coding standards / new OpenModelica compiler frontend

looks like a issue with new OpenModelica compiler frontend. I am using official release version of openmodelica 1.14 on windows-7 64bit OS.
package Test1_14
model M1
parameter Integer size = 2 "matrix size";
parameter Real A[size] = {1.0, 1.0};
Real B[size];
Real T = 1;
Real P = 2;
equation
B = A * (T/P);
end M1;
model M1_Extn
Real C[size];
Real D[size];
equation
for J in 1:size loop
C[J] = Func1(T);
D[J] = C[J] / P;
end for;
end M1_Extn;
function Func1
input Real a;
output Real b;
algorithm
b := a*a;
end Func1;
model M1_Combined
parameter Integer size = 2 "matrix size";
Real B[size];
Real T = 1;
Real P = 2;
extends M1_Extn;
equation
B = D;
end M1_Combined;
end Test1_14;
When I compile the model ‘M1_Combined’, the code generation fails with new OpenModelica compiler frontend. Export FMU also fails with the same error.
Is my code as per Modelica programing standards?
How do I declare variables - size, T, P in the model M1_Extn and still use keyword ‘extends’ in ‘M1_Combined’ ?
This is because the old frontend did not handle the "extends" correctly, according to the Modelica Specification. The new frontend does it correctly.
To be clear, you cannot define a variable in this class and then use it in the extends any other way than via a modification (and via inner/outer, via redeclare as element). An example is below, you cannot use a inside M1.
package TestExtends
model M1
parameter Real b = a;
end M1;
model M2
parameter Real a = 1;
extends M1;
end M2;
end TestExtends;
To fix your model so that is according to Modelica standards, you can do:
package Test1_14
model M1
parameter Integer size = 2 "matrix size";
parameter Real A[size] = {1.0, 1.0};
Real B[size];
Real T = 1;
Real P = 2;
equation
B = A * (T/P);
end M1;
model M1_Extn
parameter Integer size = 2;
Real T = 1;
Real P = 2;
Real C[size];
Real D[size];
equation
for J in 1:size loop
C[J] = Func1(T);
D[J] = C[J] / P;
end for;
end M1_Extn;
function Func1
input Real a;
output Real b;
algorithm
b := a*a;
end Func1;
model M1_Combined
extends M1_Extn(size=2, T = 1, P = 2);
Real B[size]; // this one works as you can use things defined in extends.
equation
B = D;
end M1_Combined;
end Test1_14;

Including a causal relation in a Modelica simulation leads to translation Error while flattening model

I want to simulate a controller for a mass-spring model which works based on energy:
model model
//parameters
parameter Real m = 1;
parameter Real k = 1;
parameter Real Fmax = 3;
parameter Real x0 = 1;
parameter Real x1 = 2;
parameter Real t1 = 1;
//variables
Real x, v, a, xy, vm;
initial equation
x = x0;
v = 2;
equation
v = der(x);
a = der(v);
m * a + k * x = F;
algorithm
vm := sign(xy - x)*sqrt(2 * (Fmax * abs(xy - x) + k * (xy^2 - x^2) / 2) / m);
// step signal
if time < t1 then
xy := x0;
else
xy := x1;
end if;
if xy == x then
F := k * x;
else
F := sign(vm - v) * Fmax;
end if;
end model;
But it leads to the error message:
Translation Error
Error occurred while flattening model
I would appreciate it if you could help me know what is the problem and how I can fix it.
P.S.1. SIMULINK is also not able to finish!
P.S.2. New version of the code can be seen here.
P.S.3. According to this discussion on Discord, the algorithm section was not really meant for casual relations. More information about the keyword is here.

Balancing local models Modelica

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.