I am trying to prepare a modelica code to understand array and for loop.
When I am compiling the code, I get following error.
The initialization problem is inconsistent due to the following equation: 0 != 1 = 1.0 - I[0]
Error in initialization. Storing results and exiting.
Use -lv=LOG_INIT -w for more information.
Simulation process failed. Exited with code -1.
I have tried to adjust array numbers to see if this solves. Could not trace the reason why the code is failing.
class flu
parameter Real beta = 10.0/(40*8*24);
parameter Real gamma= 3.0/(15*24);
parameter Real dt = 0.1;
parameter Real D = 30;
parameter Integer N_t = integer(D*24/dt);
parameter Integer array = integer(N_t*dt);
//parameter Real time[array];
Real S[array] ;
Real I[array] ;
Real R[array] ;
initial equation
S[0] = 50;
I[0] = 1;
R[0] = 0;
equation
for n in 0:(array-1) loop
S[n+1] = S[n] - dt*beta*S[n]*I[n];
I[n+1] = I[n] + dt*beta*S[n]*I[n] - dt*gamma*I[n];
R[n+1] = R[n] + dt*gamma*I[n];
end for;
annotation(
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end flu;
I am expecting to get three curves as a result .
There seems to be a few things wrong here. First of all, it seems OpenModelica does not output the index of the equation so the debugger can be opened.
But more importantly, you are setting
I[0] = 1
I[1] is the first index in Modelica... I don't know why this wouldn't give any warning in OpenModelica. Probably some edge case in an if equation that never triggers and thus no warning/error in this case.
class flu
parameter Real beta = 10.0/(40*8*24);
parameter Real gamma= 3.0/(15*24);
parameter Real dt = 0.1;
parameter Real D = 30;
parameter Integer N_t = integer(D*24/dt);
parameter Integer array = integer(N_t*dt);
//parameter Real time[array];
Real S[array] ;
Real I[array] ;
Real R[array] ;
equation
S[1] = 50;
I[1] = 1;
R[1] = 0;
equation
for n in 1:(array-1) loop
S[n+1] = S[n] - dt*beta*S[n]*I[n];
I[n+1] = I[n] + dt*beta*S[n]*I[n] - dt*gamma*I[n];
R[n+1] = R[n] + dt*gamma*I[n];
end for;
end flu;
class flu
parameter Real beta = 10.0/(40*8*24);
parameter Real gamma= 3.0/(15*24);
Real S(start = 50) ;
Real I(start = 1) ;
Real R(start = 0) ;
equation
der(S)= -beta*S*I;
der(I)=(beta*S*I) - (gamma*I);
der(R)=gamma*I;
annotation(
__OpenModelica_simulationFlags(lv = "LOG_STATS", outputFormat = "mat", s = "dassl"));end flu;
Related
I have a working Matlab algorithm for the evolution of a pulse via solving the 1D wave equation. I know the code works and I can see the pulse traveling to the sides and bouncing off the walls, when plotting; but when I translate the corresponding steps as Julia code, however, the results are completely different for the same set of inputs. I insert a simple pulse that should propagate across a string in both directions but that isn't exactly the result that I'm getting in Julia... and I don't know why.
Could anybody help point out what I'm doing wrong?
Thank you,
This is the corresponding Julia code:
using Plots,OhMyREPL
#-solving a PDE.
#NOTE: DOMAIN
# space
Lx = 20
dx = 0.1
nx = Int(trunc(Lx/dx))
x = range(0,Lx,nx)#0:dx:Lx-1
#NOTE: TIME
T = 10
#NOTE: Field variable.
#-variables.
wn = zeros(nx)#-current value.
wnm1 = wn#zeros(nx)#-w at time n-1.
wnp1 = wn#zeros(nx)#-w at time n+1.
# wnp1[1:end] = wn[1:end]#-w at time n+1.
#NOTE: PARAMETERS.
CFL = 1#-CFL = c.dt/dx
c = 1#-the propagation speed.
dt = CFL * dx/c
# #NOTE: Initial Conditions.
wn[49] = 0.1; wn[50] = 0.2; wn[51] = 0.1
wnp1[1:end] = wn[1:end]#--assumption that they are being equaled.
wnp1 = wn#--assumption that they are being equaled.
run = true
if run == true
#NOTE: Initial Conditions.
wn[49] = 0.1; wn[50] = 0.2; wn[51] = 0.1
wnp1 = wn#--wnp1[1:end] = wn[1:end]#--assumption that they are being equaled.
# NOTE: Time stepping loop.
t = 0
while t<T
#-apply reflecting boundary conditions.
wn[1]=0.0;wn[end]=0.0
#NOTE: solution::
global t = t+dt
global wnm1 = wn; global wn = wnp1#-save CURRENT and PREVIOUS arrays.
for i=2:nx-1
global wnp1[i] = 2*wn[i] - wnm1[i] + CFL^2 * ( wn[i+1] -2*wn[i] + wn[i-1] )
end
end
plot(x,wnp1, yrange=[-0.2,0.2])
end
Only one peak
This is the corresponding Matlab code:
%Domain
%Space.
Lx = 20;
dx = 0.1;
nx = fix(Lx/dx);
x = linspace(0,Lx,nx);
%Time
T = 10;
%Field Variables
wn = zeros(nx,1);
wnm1 = wn;
wnp1 = wn;
%Parameters...
CFL = 1;
c = 1;
dt = CFL*dx/c;
%Initial conditions
wn(49:51) = [0.1 0.2 0.1];
wnp1(:) = wn(:);
run = true;
if run == true
%Initial conditions
wn(49:51) = [0.1 0.2 0.1];
wnp1(:) = wn(:);
%Time stepping loop
t = 0;
while t<T
%Reflecting boundary conditions:
wn([1 end]) = 0.0;
%Solution:
t=t+dt;
wnm1=wn;wn = wnp1;%-save current and previous arrays.
for i=2:nx-1
wnp1(i) = 2*wn(i) - wnm1(i) + CFL^2 * ( wn(i+1) - 2*wn(i) + wn(i-1) );
endfor
end
plot(x,wnp1)
end
Two peaks, as it should be.
If anybody could point me in the right direction, I'd greatly appreciate it.
Julia arrays are designed to be fast. So, when an array is assigned to another array, the default behaviour is make the two arrays the same - same variable address, occupying the same area of memory. Editing one array does the exact same edit on the other array:
x = [1,2,3]
y = x
x[1] = 7
# x is [7,2,3]
# y is [7,2,3] also
To copy data from one array to another an explicit copy is required:
x = [1,2,3]
y = copy(x)
x[1] = 7
# x is [7,2,3]
# y is [1,2,3] still
The same thing happens with Python lists:
x = [1,2,3]
y = x
x[0] = 7 # Julia indexes arrays 1+, Python indexes lists 0+
# x is [7,2,3]
# y is [7,2,3] also
It is done this way to minimise the effort of unnecessary copying, which can be expensive.
The upshot is that if the code is running but gives the wrong result, a possible cause is assigning an array when a copy is intended.
The question author indicated that this was indeed the cause of the code's behaviour.
I programmed PID in MATLAB:
classdef PID < handle
properties
Kp = 0
Ki = 0
Kd = 0
SetPoint = 1
Dt = 0.01
end
properties (Access = private)
IState = 0
PreErr = 0
end
methods
function obj = PID(Kp, Ki, Kd, SetPoint, Dt)
if nargin == 0
return;
end
obj.Kp = Kp;
obj.Ki = Ki;
obj.Kd = Kd;
obj.SetPoint = SetPoint;
obj.Dt = Dt;
end
function output = update(obj, measuredValue, t)
err = obj.SetPoint - measuredValue;
P = obj.getP(err);
I = obj.getI(err);
val = lowPass(obj,t);
D = obj.getD(err*val);
output = P + I + D;
end
function val = getP(obj, err)
val = obj.Kp*err;
end
function val = getI(obj, err)
obj.IState = obj.IState + err * obj.Dt;
val = obj.Ki * obj.IState;
end
function val = getD(obj, err)
val = obj.Kd * (err - obj.PreErr) / obj.Dt;
obj.PreErr = err;
end
function val = lowPass(obj,t)
N = 10;
val = 1-exp(-N*t);
end
end
end
And tested it using a random low pass filter as the plant:
function r = getResponse(t)
r = 1 - exp(-5*t);
end
The test code:
sr = 1e2; % sampling rate 100Hz
st = 10; % sampling time 10s
ss = st*sr+1; % sample size
t = 0:1/sr:st; % time
input = ones(1,ss)*100;
output = zeros(1,ss);
measured = 0;
pid = PID(0,1,1,input(1),t(2)-t(1));
for i = 2:ss
rPID(i) = pid.update(measured, t(i));
output(i) = rPID(i)*getResponse(t(i));
measured = output(i);
end
figure
plot(t,output)
hold on;
plot(t,input)
plot(t,rPID)
legend('Output','Input','PID')
Note that the parameters are set to kp=0;ki=1;kd=1;. I'm only testing the differential part here. The result is very wrong:
Notice the Y-axis is scaled by 10^307. It gets too big that after ~1.6s the PID value exceeds the range of double precision and therefore, the curve stops.
I have ensured that both P and I parts work well enough (see this question I asked a while ago).
From the curve for the D component (see figure below), one can clearly see that it starts to oscillate heavy from the very beginning; its value reaches >50k after the 5th timestamp at 0.04s:
I'm almost certain I must have made a mistake in implementing the low pass filter, but I also noticed that even with the low pass filter removed, the differential values still behave similarly.
To have some sort of reference and comparison, I also made a Simulink simulation of the same system, using the exact same PID gains (i.e. kp=0;ki=1;kd=1;). Below is the block diagram (left), figure for input and output (top right figure) and figure for PID values (bottom right)
Note that there is no top/lower limit in the gain blocks and the initial inputs/outputs are set to zeros.
These PID gains are nowhere near optimised but they give completely different results in the simulation and coded PID.
Therefore the big question is am I doing something wrong here? Why is there a difference between the two results?
The implementation of the low pass filter is not correct. The difference equation of a low pass filter is as shown:
The call of the getResponse function could look like this:
pid = PID(0,1,1,input(1),t(2)-t(1));
for i = 2:ss
rPID(i) = pid.update(measured, t(i));
alpha = getResponse(0.25,0.01);
output(i) = rPID(i)*alpha+(1-alpha)*output(i-1);
measured = output(i);
end
Thus getResponse is equivalent to alpha
function r = getResponse(wc,Ts)
r = 1 - exp(-wc*Ts);
end
Further you have to modify the lowPass function in the PID class.
function output = update(obj, measuredValue)
err = obj.SetPoint - measuredValue;
P = obj.getP(err);
I = obj.getI(err);
val = lowPass(obj,err,0.1,0.01);
D = obj.getD(val);
output = P + I + D;
end
% ...
function val = lowPass(obj,err,wc,Ts)
alpha = getResponse(wc,Ts);
output = err*alpha+(1-alpha)*obj.output_1;
obj.output_1 = output;
val = output;
end
I have the following function below which I need to translate into MATLAB code, I'm pretty sure I've got the function translated properly. However I've got a small issue with the code.
Function to translate:
When I attempt to run this function the value of phi comes out as being inf I'm really not sure why this is happening?
Also, can someone tell me if I'm doing the right thing with the sum part of this function? In my mind this is telling me to apply this function to every value and then take that value and add it to phi, but I'm not sure if that's what I'm actually doing.
function [exists] = detectHaarWatermark(watermarkedCoefs, watermark)
coefsSize = size(watermarkedCoefs,1);
phi = 0;
numeratorTotal = 0;
denomanatorTotal = 0;
for i = 1 : coefsSize
for j = 1 : coefsSize
y = watermarkedCoefs(i,j) %Watermarked coefficient
w = watermark(i,j) %Watermark
% val = ((w * sign(y))^2) / (w^2)
numerator = (w * sign(y))
numeratorTotal = numeratorTotal + numerator;
numeratorTotal = numeratorTotal^2;
denomanator = (w^2)
denomanatorTotal = denomanatorTotal + denomanator;
val = numeratorTotal / denomanatorTotal;
phi = val;
end
end
phi
stdDev = std2(watermarkedCoefs)
if phi > (10*stdDev)
exists = true;
else
exists = false;
end
end %end function
Edit:
numerator = (sum(sum(watermark.*(sign(watermarkedCoefs)))));
numerator = power(numerator, 2);
denomanator = sum(sum(watermark.^2));
phi = numerator / denomanator
I am using MATLAB to calculate the numerical integral of a complex function including natural exponent.
I get a warning:
Infinite or Not-a-Number value encountered
if I use the function integral, while another error is thrown:
Output of the function must be the same size as the input
if I use the function quadgk.
I think the reason could be that the integrand is infinite when the variable ep is near zero.
Code shown below. Hope you guys can help me figure it out.
close all
clear
clc
%%
N = 10^5;
edot = 10^8;
yita = N/edot;
kB = 8.6173324*10^(-5);
T = 300;
gamainf = 0.115;
dTol = 3;
K0 = 180;
K = K0/160.21766208;
nu = 3*10^12;
i = 1;
data = [];
%% lambda = ec/ef < 1
for ef = 0.01:0.01:0.1
for lambda = 0.01:0.01:0.08
ec = lambda*ef;
f = #(ep) exp(-((32/3)*pi*gamainf^3*(0.5+0.5*sqrt(1+2*dTol*K*(ep-ec)/gamainf)-dTol*K*(ep-ec)/gamainf).^3/(K*(ep-ec)).^2-16*pi*gamainf^3*(0.5+0.5*sqrt(1+2*dTol*K*(ep-ec)/gamainf)-dTol*K*(ep-ec)/gamainf).^2/((1+dTol*K*(ep-ec)/(gamainf*(0.5+0.5*sqrt(1+2*dTol*K*(ep-ec)/gamainf)-dTol*K*(ep-ec)/gamainf)))*(K*(ep-ec)).^2))/(kB*T));
q = integral(f,0,ef,'ArrayValued',true);
% q = quadgk(f,0,ef);
prob = 1-exp(-yita*nu*q);
data(i,1) = ef;
data(i,2) = lambda;
data(i,3) = q;
i = i+1;
end
end
I've rewritten your equations so that a human can actually understand it:
function integration
N = 1e5;
edot = 1e8;
yita = N/edot;
kB = 8.6173324e-5;
T = 300;
gamainf = 0.115;
dTol = 3;
K0 = 180;
K = K0/160.21766208;
nu = 3e12;
i = 1;
data = [];
%% lambda = ec/ef < 1
for ef = 0.01:0.01:0.1
for lambda = 0.01:0.01:0.08
ec = lambda*ef;
q = integral(#f,0,ef,'ArrayValued',true);
% q = quadgk(f,0,ef);
prob = 1 - exp(-yita*nu*q);
data(i,:) = [ef lambda q];
i = i+1;
end
end
function y = f(ep)
G = K*(ep - ec);
r = dTol*G/gamainf;
S = sqrt(1 + 2*r);
x = (1 + S)/2 - r;
Z = 16*pi*gamainf^3;
y = exp( -Z*x.^2.*( 2*x/(3*G.^2) - 1/(G.^2*(1 + r/x))) ) /...
(kB*T));
end
end
Now, for the first iteration, ep = 0.01, the value of the argument of the exp() function inside f is huge. In fact, if I rework the function to return the argument to the exponent (not the value):
function y = f(ep)
% ... all of the above
% NOTE: removed the exp() to return the argument
y = -Z*x.^2.*( 2*x/(3*G.^2) - 1/(G.^2*(1 + r/x))) ) /...
(kB*T);
end
and print its value at some example nodes like so:
for ef = 0.01 : 0.01 : 0.1
for lambda = 0.01 : 0.01 : 0.08
ec = lambda*ef;
zzz(i,:) = [f(0) f(ef/4) f(ef)];
i = i+1;
end
end
zzz
I get this:
% f(0) f(ef/4) f(ef)
zzz =
7.878426438111721e+07 1.093627454284284e+05 3.091140080273912e+03
1.986962280947140e+07 1.201698288371587e+05 3.187767404903769e+03
8.908646053687230e+06 1.325435523124976e+05 3.288027743119838e+03
5.055141696747510e+06 1.467952125661714e+05 3.392088351112798e+03
...
3.601790797707676e+04 2.897200140791236e+02 2.577170427480841e+01
2.869829209254144e+04 3.673888685004256e+02 2.404148067956737e+01
2.381082059148755e+04 4.671147785149462e+02 2.238181495716831e+01
So, integral() has to deal with things like exp(10^7). This may not be a problem per se if the argument would fall off quickly enough, but as shown above, it doesn't.
So basically you're asking for the integral of a function that ranges in value between exp(~10^7) and exp(~10^3). Needless to say, The d(ef) in the order of 0.01 isn't going to compensate for that, and it'll be non-finite in floating point arithmetic.
I suspect you have a scaling problem. Judging from the names of your variables as well as the equations, I would think that this has something to do with thermodynamics; a reworked form of Planck's law? In that case, I'd check if you're working in nanometers; a few factors of 10^(-9) will creep in, rescaling your integrand to the compfortably computable range.
In any case, it'll be wise to check all your units, because it's something like that that's messing up the numbers.
NB: the maximum exp() you can compute is around exp(709.7827128933840)
`sol = pdepe(m,#ParticleDiffusionpde,#ParticleDiffusionic,#ParticleDiffusionbc,x,t);
% Extract the first solution component as u.
u = sol(:,:,:);
function [c,f,s] = ParticleDiffusionpde(x,t,u,DuDx)
global Ds
c = 1/Ds;
f = DuDx;
s = 0;
function u0 = ParticleDiffusionic(x)
global qo
u0 = qo;
function [pl,ql,pr,qr] = ParticleDiffusionbc(xl,ul,xr,ur,t,x)
global Ds K n
global Amo Gc kf rhop
global uavg
global dr R nr
sum = 0;
for i = 1:1:nr-1
r1 = (i-1)*dr; % radius at i
r2 = i * dr; % radius at i+1
r1 = double(r1); % convert to double precision
r2 = double(r2);
sum = sum + (dr / 2 * (r1*ul+ r2*ur));
end;
uavg = 3/R^3 * sum;
ql = 1;
pl = 0;
qr = 1;
pr = -((kf/(Ds.*rhop)).*(Amo - Gc.*uavg - ((double(ur/K)).^2).^(n/2) ));`
dq(r,t)/dt = Ds( d2q(r,t)/dr2 + (2/r)*dq(r,t)/dr )
q(r, t=0) = 0
dq(r=0, t)/dr = 0
dq(r=dp/2, t)/dr = (kf/Ds*rhop) [C(t) - Cp(at r = dp/2)]
q = solid phase concentration of trace compound in a particle with radius dp/2
C = bulk liquid concentration of trace compound
Cp = trace compound concentration at particle surface
I want to solve the above pde with initial and boundary conditions given. Tried Matlab's pdepe, but does not work satisfactorily. Maybe the boundary conditions is creating problem for me. I also used this isotherm equation for equilibrium: q = K*Cp^(1/n). This is convection-diffusion equation but i could not find any write ups that addresses solving this type of equation properly.
There are two problems with the current implementation.
Incorrect Source Term
The PDE you are attempting to solve has the form
which has the equivalent form
where the last term arises due to the factor of 2 in the original PDE.
The last term needs to be incorporated into pdepe via a source term.
Calculation of q average
The current implementation attempts to calculate the average value of q using the left and right values of q passed to the boundary condition function.
This is incorrect.
The average value of q needs to be calculated from a vector of up-to-date values of the quantity.
However, we have the complication that the only function to receive all mesh values is ParticleDiffusionpde; however, the mesh values passed to that function are not guaranteed to be from the mesh we provided.
Solution: use events (as described in the pdepe documentation).
This is a hack since the event function is meant to detect zero-crossings, but it has the advantage that the function is given all values of q on the mesh we provide.
So, the working example below (you'll notice I set all of the parameters to 1 since I didn't know better) uses the events function to update a variable qStore that can be accessed by the boundary condition function (see here for an explanation), and the boundary condition function performs a vectorized trapezoidal integration for the average calculation.
Working Example
function [] = ParticleDiffusion()
% Parameters
Ds = 1;
q0 = 0;
K = 1;
n = 1;
Amo = 1;
Gc = 1;
kf = 1;
rhop = 1;
% Space
rMesh = linspace(0,1,10);
rMesh = rMesh(:) ;
dr = rMesh(2) - rMesh(1) ;
% Time
tSpan = linspace(0,1,10);
% Vector to store current u-value
qStore = zeros(size(rMesh));
options.Events = #(m,t,x,y) events(m,t,x,y);
% Solve
[sol,~,~,~,~] = pdepe(1,#ParticleDiffusionpde,#ParticleDiffusionic,#ParticleDiffusionbc,rMesh,tSpan,options);
% Use the events function to update qStore
function [value,isterminal,direction] = events(m,~,~,y)
qStore = y; % Value of q on rMesh
value = m; % Since m is constant, it will never be zero (no event detection)
isterminal = 0; % Continue integration
direction = 0; % Detect all zero crossings (not important)
end
function [c,f,s] = ParticleDiffusionpde(r,~,~,DqDr)
% Define the capacity, flux, and source
c = 1/Ds;
f = DqDr;
s = DqDr./r;
end
function u0 = ParticleDiffusionic(~)
u0 = q0;
end
function [pl,ql,pr,qr] = ParticleDiffusionbc(~,~,R,ur,~)
% Calculate average value of current solution
qL = qStore(1:end-1);
qR = qStore(2: end );
total = sum((qL.*rMesh(1:end-1) + qR.*rMesh(2:end))) * dr/2;
qavg = 3/R^3 * total;
% Left boundary
pl = 0;
ql = 1;
% Right boundary
qr = 1;
pr = -(kf/(Ds.*rhop)).*(Amo - Gc.*qavg - (ur/K).^n);
end
end