Continuous variable used in clocked when clause is automatically discretized - modelica

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

Related

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 - iterator with exception?

Slightly generalised example:
How do I make a for loop with exceptions when I define model equations?
The following works:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in {1,3,5,6,7,8,9} loop
q[i] = 0;
end for;
end Test;
But I would rather like to write something like:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 and not in {2,4} loop
q[i] = 0;
end for;
end Test;
Is this possible?
This should be possible, as long as you make sure to have an equation for every unknown.
Probably not the perfect solution, but actually pretty readable:
model LoopException
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in 1:9 loop
if Modelica.Math.Vectors.find(i, {2, 4}) == 0 then
q[i] = 0;
end if;
end for;
end LoopException;
As an alternative, you could also try to write an "Array Constructor with Iterators" (Modelica Language Spec. Sec. 10.4.1), but that probably gets a bit messy...
With 2 helper functions you can easily emulate what you want:
model Test
Real[9] q;
equation
q[2] = 1.2;
q[4] = 1.4;
for i in allExcept(1:9, {2,4}) loop
q[i] = 0;
end for;
end Test;
Here are the functions you need for that:
function contains
"Check if vector v contains any element with value e"
input Integer vec[:];
input Integer e;
output Boolean result;
algorithm
result := false;
for v in vec loop
if v == e then
result := true;
break;
end if;
end for;
end contains;
function allExcept
"Return all elements of vector v which are not part of vector ex"
input Integer all[:];
input Integer ex[:];
output Integer vals[size(all, 1) - size(ex, 1)];
protected
Integer i=1;
algorithm
for v in all loop
if not contains(ex, v) then
vals[i] := v;
i := i + 1;
end if;
end for;
end allExcept;
Note that Modelica tools usually need to know the size of vectors during translation, especially here when you generate equations. Hence, the following line is required:
output Integer vals[size(all, 1) - size(ex, 1)];
It will fail when all or ex contain duplicate elements.
Therefore the model will not translate, if you try something like
for i in allExcept(1:3, {2, 2}) loop

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.

Pass extra variable parameters to ode15s function (MATLAB)

I'm trying to solve a system of ordinary differential equations in MATLAB.
I have a simple equation:
dy = -k/M *x - c/M *y+ F/M.
This is defined in my ode function test2.m, dependant on the values X and t. I want to trig 'F' with a signal, generated by my custom function squaresignal.m. The output hereof, is the variable u, spanding from 0 to 1, as it is a smooth heaviside function. - Think square wave. The inputs in squaresignal.m, is t and f.
u=squaresignal(t,f)
These values are to be used inside my function test2, in order to enable or disable variable 'F' with the value u==1 (enable). Disable for all other values.
My ode function test2.m reads:
function dX = test2(t ,X, u)
x = X (1) ;
y = X (2) ;
M = 10;
k = 50;
c = 10;
F = 300;
if u == 1
F = F;
else
F = 0,
end
dx = y ;
dy = -k/M *x - c/M *y+ F/M ;
dX = [ dx dy ]';
end
And my runscript reads:
clc
clear all
tstart = 0;
tend = 10;
tsteps = 0.01;
tspan = [0 10];
t = [tstart:tsteps:tend];
f = 2;
u = squaresignal(t,f)
for ii = 1:length(u)
options=odeset('maxstep',tsteps,'outputfcn',#odeplot);
[t,X]=ode15s(#(t,X)test2(t,X,u(ii)),[tstart tend],[0 0],u);
end
figure (1);
plot(t,X(:,1))
figure (2);
plot(t,X(:,2));
However, the for-loop does not seem to do it's magic. I still only get F=0, instead of F=F, at times when u==1. And i know, that u is equal to one at some times, because the output of squaresignal.m is visible to me.
So the real question is this. How do i properly pass my variable u, to my function test2.m, and use it there to trig F? Is it possible that the squaresignal.m should be inside the odefunction test2.m instead?
Here's an example where I pass a variable coeff to the differential equation:
function [T,Q] = main()
t_span = [0 10];
q0 = [0.1; 0.2]; % initial state
ode_options = odeset(); % currently no options... You could add some here
coeff = 0.3; % The parameter we wish to pass to the differential eq.
[T,Q] = ode15s(#(t,q)diffeq(t,q,coeff),t_span,q0, ode_options);
end
function dq = diffeq(t,q,coeff)
% Preallocate vector dq
dq = zeros(length(q),1);
% Update dq:
dq(1) = q(2);
dq(2) = -coeff*sin(q(1));
end
EDIT:
Could this be the problem?
tstart = 0;
tend = 10;
tsteps = 0.01;
tspan = [0 10];
t = [tstart:tsteps:tend];
f = 2;
u = squaresignal(t,f)
Here you create a time vector t which has nothing to do with the time vector returned by the ODE solver! This means that at first we have t[2]=0.01 but once you ran your ODE solver, t[2] can be anything. So yes, if you want to load an external signal source depending on time, then you need to call your squaresignal.m from within the differential equation and pass the solver's current time t! Your code should look like this (note that I'm passing f now as an additional argument to the diffeq):
function dX = test2(t ,X, f)
x = X (1) ;
y = X (2) ;
M = 10;
k = 50;
c = 10;
F = 300;
u = squaresignal(t,f)
if u == 1
F = F;
else
F = 0,
end
dx = y ;
dy = -k/M *x - c/M *y+ F/M ;
dX = [ dx dy ]';
end
Note however that matlab's ODE solvers do not like at all what you're doing here. You are drastically (i.e. non-smoothly) changing the dynamics of your system. What you should do is to use one of the following:
a) events if you want to trigger some behaviour (like termination) depending on the integrated variable x or
b) If you want to trigger the behaviour based on the time t, you should segment your integration into different parts where the differential equation does not vary during one segment. You can then resume your integration by using the current state and time as x0 and t0 for the next run of ode15s. Of course this only works of you're external signal source u is something simple like a step funcion or square wave. In case of the square wave you would only integrate for a timespan during which the wave does not jump. And then exactly at the time of the jump you start another integration with altered differential equations.