Problem with EventFunction triggering MATLAB

5 views (last 30 days)
Hello! Feel free to ask me anything if it helps better understand the code. Thank you for the help.
I'm compiling this code to simulate the behaviour of a mechanical machine. The simulation is based on two odes (eq1 and eq2) and it works perfectly fine for certain values of Casp and Cman. However, when I change them to higher values, the simulation starts having issues. The simulation starts with eq1 and the code correctly changes to eq2. However, when EventFunction2 is triggered, the simulation starts going in a loop where it is continuously switching between eq1 and eq2. When I plot the results, it looks like the simulation gets stuck in eq2.
Here is the code:
%Parametri Da Cambiare
nciclo = 10; %tempo
sigma = 0.02; % 1% del volume del pistone
Casp = 30; %coefficiente aspirazione
Cman = 90; %coefficiente mandata
coeff3_out = 100; %questo va aumentato se la pressione raggiunta è troppo alta (avendo modificato sigma)
h = 0; %1E-6; %0 %spessore trafilamento %con trafilamento fa ancora fatica
ratio_volume = 0.02; %Bunsen coefficient
type = 'I'; %A
%PARAMETRI PISTONE
N = 420; %rpm
freq = N/60; %frequenza, giri al secondo
omega = 2*pi*N/60; %rad/s
d = 0.0605; %m %diametro pistone
c = 6*d; %corsa
r = c/2; %raggio di manovella
l_pist = d; %lunghezza pistone
lambda = 1/0.1875;
Vcil = pi/4*d^2*c; %m^3
%sfasa pistone
sfasa = fzero(@(tett) r*(1-cos(tett)+lambda-sqrt(lambda^2-sin(tett).^2))-c/2,pi/2);
%CARTER
P_cart = 1*10^5; %1 bar
beta_0 = 1700*10^6; %Pa %rigidità olio
rho_oil = 862; %kg/m3 %850
visc_cin = 68*10^(-6);
visc_din = rho_oil * visc_cin;
V03 = Vcil*3; %300 di olio
%PIASTRA FORATA
Nf = 30; %numero di fori
Df = 0.01; %diametro del foro
Cd = 0.8; %possibile cambiarlo
%MEMBRANA
delta_m = 0.006; %m
r_m = sqrt(Vcil/(pi/3*2*delta_m)); %raggio
rhom = 7855.6; %kg/m3 %densità acciaio
thic = 1.5*10^-3; %m
young_m = 210*10^9; %Pa
poisson_m = 0.3;
m_m = rhom*thic*pi*r_m^2; %massa membrana
k_m = 16*young_m/(3*(1-poisson_m^2))*thic^3*pi/((r_m)^2);
damp = 0;
%GAS
Pasp = Casp*10^6; % Pasp = 10 MPa
Tasp = 298.15; %K Tamb
Pman = Cman*10^6; % Pa %pressione di scarico % Pman = 30 MPa
A_in_gas = 2*10^-4; %m^2
A_out_gas = 0.7*10^-4; %m^2 %area ragionevole
fluido = 'hydrogen';%'air.mix';
%VALVOLE OLIO
%plima = 0.01*10^5; % 0.01 bar ovvero 1 kPa
p_cp = Pasp;
p_over = Pman*1.1; %oil pressure should be 10% higher than gas discharge pressure
pout = 1*10^5;
%FONDO CORSA
Unrecognized function or variable 'Vcil'.
[gamma,cmax,tetamax,deltamax] = calc_gamma(sigma,Vcil,c,d,r,lambda,r_m);
deltamax = round(deltamax,6);
delta_mezz = deltamax;
Vgas_cavity = pi/3*r_m^2*deltamax; %è con deltamax %prof
V02 = 0.2*Vgas_cavity;
V01 = 0.01*Vgas_cavity;
%Compensating Pump
c_cp = 0.01; %m
[r_cp] = volume_CP_integrale_test(sigma,r,c_cp,Vcil,omega,c,lambda);
%m %questo è da cambiare ogni volta
%r_cp = 0.07; %m %0.0134
A_cp = pi*r_cp^2;
delta = 0;
lambda_cp = 0.2; %NON cambia anche lambda se cambio raggio, perchè il raggio che considera in questa formula è quello di manovella
%CONDIZIONI INIZIALI (parto da membrana in quiete in mezzeria)
P3_0 = Pasp;
P2_0 = Pasp;
P0 = Pasp;
T0 = Tasp;
M0 = refpropm('D','T',T0,'P',Pasp/1000,fluido) * (V01+Vgas_cavity/2);
U0 = M0*refpropm('U','T',Tasp,'P',Pasp/1000,fluido); %KJ %non è diviso solo per 100??
delta0 = 0;
deltap0 = 0;
%parameters related to odes
y0 = [P3_0 P2_0 delta0 deltap0 M0 U0];
y = y0;
tciclo = nciclo/freq;
tspan = [0 tciclo];
tstart = tspan(1);
t = tstart;
tend = tspan(end);
teout = [];
yeout = [];
ieout = [];
fcn = @eq1_0611_betaNC;
extdfunc1 = @(t,y)EventFunction1(t,y,deltamax);
extdfunc2 = @(t,y)EventFunction2(t,y,Pman);
opt = odeset('Events',extdfunc1);
flag = 0; %definisco la flag iniziale siccome sto iniziando con eq1
%ODES
while(t(end) < tend)
[at,ay,ate,aye,aie] = ode23t(@(tt,y) fcn(tt,y,d,omega,lambda,r,beta_0,type,rho_oil,Cd,r_m, ...
damp,m_m,k_m,Pasp,Tasp,Pman,A_in_gas,A_out_gas,Nf,Df, ...
fluido,V01,V02,V03,P_cart,h,visc_din,l_pist,p_cp,p_over,pout,sfasa,deltamax,...
coeff3_out,lambda_cp,A_cp,c_cp,delta,ratio_volume), [t(end) tend], y(end,:) , opt);
%conserva val
t = [t; at(2:end)];
y = [y; ay(2:end,:)]; %mi salva i valori ogni qualvolta una delle funzioni raggiunge la sua event function
if flag == 2 || flag == 0
y(end,3) = deltamax; %0.0053;
y(end,4) = -0.00001*y(end,4); %coeff = 0.5
end
if flag == 1
y(end,4) = abs(y(end,4));
%impongo anche che la P2 e la P3 devono essere molto simili da quando
%ripartono
y(end,1) = Pman; %li ho aggiunti
y(end,2) = Pman;
end
teout = [teout; ate];
yeout = [yeout; aye];
ieout = [ieout; aie];
%y(end,2)
%y(end,3)
%y(end,4)
%decide new equation and event function
if y(end,4) > 0 %controllare condizioni
fcn = @eq1_0611_betaNC;
opt = odeset('Events', extdfunc1);
disp('no')
flag = 2;
else
fcn = @eq2_0611_betaNC;
opt = odeset('Events', extdfunc2);
disp('si')
flag = 1;
end
end
figure(5)
plot(t,y(:,3),t,y(:,4))
legend('spostamento membrana','velocità membrana','location','southeast')
And the event functions are:
function [position,isterminal,direction] = EventFunction1(t,y,deltamax)
position = y(3)-deltamax-1e-7; % importante che non sia proprio 0.005 se no ho problemi con if
isterminal = 1; % Halt integration
direction = +1; % The zero can be approached from either direction
end
function [position,isterminal,direction] = EventFunction2(t,y,Pman)
position = y(1)-Pman-10; %potrei volere che la P2 debba arrivare alle Pman
isterminal = 1; % Halt integration
direction = -1; %-1 % The zero can be approached from either direction
end
  6 Comments
Torsten
Torsten on 9 Nov 2023
Edited: Torsten on 9 Nov 2023
I doubt that anybody will then be able to help you because we cannot execute your code.
Andrea Martella
Andrea Martella on 9 Nov 2023
Alright, thank you anyways. I could provide you the vectors of the parameters calculated by refprop, but more than that I really don't know how to help.

Sign in to comment.

Answers (0)

Categories

Find more on Programming in Help Center and File Exchange

Products


Release

R2022a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!