-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.m
240 lines (202 loc) · 6.16 KB
/
Main.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
function Positivity_neu5()
%02.11.2018 Dietz - aktuellste version Version(29.11.2018)
% Programm positivity_neu, um die AtcSol Matrizen zu vergleichen
close all;
clear all; %#ok<*CLALL>
%%
global Reak Species dtMax Bsp
Bsp='Brusselator';
% load reactions,species,start and end time:
switch Bsp
case 'SmallStratoKPP'
[Reak,Species,tBegin,tEnd,dt]=SmallStratoKPP(); %SmallStratoKPP
case 'Robertson'
[Reak,Species,tBegin,tEnd,dt,dtMax]=Robertson();%Fehler?
case 'Brusselator'
[Reak,Species,tBegin,tEnd,dt,dtMax]=Brusselator();%Brusselator
case 'Tilgner'
[Reak,Species,tBegin,tEnd,dt]=Tilgner();%Tilgner
end
for i=1:size(Reak,1)
Reak(i).M=0;
for j=1:Reak(i).Left
Pos=GetPosList(Reak(i).NameL(j).Name,Species);
Reak(i).M=Reak(i).M+Species(Pos).MolMass*Reak(i).KoeffL(j);
end
end
StMatrix = StochioMatrix(Reak,Species);
%Positivitätsabfrage des Reagktionssystems
% positive()
%Anfangswert y0
y=zeros(size(Species));
MolMass=zeros(size(Species));
for i=1:size(Species,1)
y(Species(i).Pos)=Species(i).c;
MolMass(Species(i).Pos)=Species(i).MolMass;
end
[P,D]=PDMatrix(tBegin,y);
MPD=P-D;
M=MHatMatrix(Reak,Species,tBegin,y);
% Einmalig ausführen für matfile der exakten Loesung:
% erzeugung_matfile_exakte_Loesung(tBegin,tEnd,y)
% load('test_niedrige_tol_-3')
%load exakte Lösung für relTol=absTol=1e-8, tBegin=43200 und tEnd=36*3600
%%
%ODE Solver
options = odeset('RelTol',1e-9,'AbsTol',1e-12,'Jacobian',@Jacobian);
% options = odeset('RelTol',1e-3,'AbsTol',1e-3);
[tt_tempF,yy_tempF] = ode23s(@odefun,[tBegin,tEnd],y,options);
%Eigene Solver
RKExp=RungeKutta('RK2a');
RK=RungeKutta('RK2a');
%RK=RungeKutta('RK2b');
%RKPat=RungeKutta('SSP1');
% RKPat=RungeKutta('meister3');
%RKPat=RungeKutta('meister2');
RKPat=RungeKutta('meister1');
yRK=zeros(size(y,1),4);
yRKExp=zeros(size(y,1),4);
yRKPat=zeros(size(y,1),4);
yRKPatPD=zeros(size(y,1),4);
errRK=zeros(4,1);
errRKExp=zeros(4,1);
errRKPat=zeros(4,1);
errRKPatPD=zeros(4,1);
err1=zeros(4,1);
err2=zeros(4,1);
err3=zeros(4,1);
h=zeros(4,1);
for i=1:8
h(i)=dtMax;
err1(i)=h(i);
err2(i)=1.e2*h(i)^2;
err3(i)=1.e3*h(i)^3;
[tRKExp,yyRKExp]=RungeKuttaExpMethod(tBegin,tEnd,y,dt,RKExp);
%[tRK,yyRK]=RungeKuttaMethod(tBegin,tEnd,y,dt,RK);
% [tRKPat,yyRKPat]=RungeKuttaPat2Method(tBegin,tEnd,y,dt,RK);
[tRKPat,yyRKPat]=RungeKuttaPat3Method(tBegin,tEnd,y,dt,RKPat);
[tRKPatPD,yyRKPatPD]=RungeKuttaPat3PD(tBegin,tEnd,y,dt,RKPat);
%yRK(:,i)=yyRK(end,:);
yRKExp(:,i)=yyRKExp(end,:);
yRKPat(:,i)=yyRKPat(end,:);
yRKPatPD(:,i)=yyRKPatPD(end,:);
%errRK(i)=norm(yRK(:,i)-yy_tempF(end,:)');
errRKExp(i)=norm(yRKExp(:,i)-yy_tempF(end,:)');
errRKPat(i)=norm(yRKPat(:,i)-yy_tempF(end,:)');
errRKPatPD(i)=norm(yRKPatPD(:,i)-yy_tempF(end,:)');
dtMax=0.25*dtMax;
end
figure;
p_1=loglog(h,errRKPat,'-o');hold on;
%p_1=loglog(h,errRKExp,'-o');hold on;
p_1=loglog(h,errRKPatPD,'-o');hold on;
p_2=loglog(h,err2);hold on;
p_3=loglog(h,err3);hold off;
h=[p_1;p_2;p_3];
legend(h,'errRKPat','err2','err3', 'Location', 'NorthWest');
% Plots
fun_plot(tt_tempF,yy_tempF,Species,y,'odefun1')%ode
fun_plot(tt_temp2,yy_temp2,Species,y,'Verfahren2')
figure
fun_plot(tt_tempF,yy_tempF,Species,y,'odefun1')%ode
fun_plot(tt_temp3,yy_temp3,Species,y,'Verfahren3')
figure
fun_plot(tt_temp,yy_temp,Species,y,'odefun1')%ode
fun_plot(tt_temp4F,yy_temp4F,Species,y,'Verfahren4')
% fun_plot_brussel(tt_temp,yy_temp,tt_temp2,yy_temp2,Species,y,'odefun1','Verfahren2')
% figure
% fun_plot_brussel(tt_temp,yy_temp,tt_temp3,yy_temp3,Species,y,'odefun1','Verfahren3')
% figure
% fun_plot_brussel(tt_temp,yy_temp,tt_temp4,yy_temp4,Species,y,'odefun1','Verfahren4')
%Plot Schrittweite ode23s
figure
plot(0:length(tt_tempF)-1, diff([tBegin; tt_tempF]))
grid
end
%%
function dydt = odefun1(t,y)
global Reak Species
M=M_Berechnung(Reak,Species,t,y);%alt
dydt=M*y;
end
function dydt = odefun(t,y)%löschbar, test für odefun1
global Reak Species
dydt=zeros(size(y));
for i=1:size(Reak,1)
r=Rate(Reak(i),t);
for j=1:Reak(i).Left
Pos=GetPos(Reak(i).NameL(j).Name,Species);
r=r*y(Pos);
end
for j=1:Reak(i).Left
Pos=GetPos(Reak(i).NameL(j).Name,Species);
dydt(Pos)=dydt(Pos)-Reak(i).KoeffL(j)*r;
end
for j=1:Reak(i).Right
Pos=GetPos(Reak(i).NameR(j).Name,Species);
dydt(Pos)=dydt(Pos)+Reak(i).KoeffR(j)*r;
end
end
%dydt1 = odefun1(t,y)
end
function fun_plot_brussel(tt,yy,tt2,yy2,Species,y,Verfahren,Verfahren2)
%Plot function
for i=1:size(y)
for j=1:size(y)
if i==Species(j).Pos
PerPos=j;
end
end
hold on
% figure
subplot(2,3,i);
plot(tt,yy(:,i),'-o')
hold on
% figure
plot(tt2,yy2(:,i),'--')
legend( {Verfahren,Verfahren2},'FontSize',22);
title( {Species(PerPos).Name} ,'FontSize',24);
end
end
function positive()
global Reak Species
%Positivitätsabfrage des Reagktionssystems
sum_left_side=0;
sum_right_side=0;
for i=1:size(Reak,1)
for j=1:Reak(i).Left
Pos=GetPosList(Reak(i).NameL(j).Name,Species);
sum_left_side=sum_left_side+Species(Pos).MolMass*Reak(i).KoeffL(j);
end
for j=1:Reak(i).Right
Pos=GetPosList(Reak(i).NameR(j).Name,Species);
sum_right_side=sum_right_side+Species(Pos).MolMass*Reak(i).KoeffR(j);
end
end
if sum_left_side>=sum_right_side && sum_right_side>=0
disp('The PDS is conservative')% production-destruction systems (PDS)
else
disp('The PDS is NOT conservative')% production-destruction systems (PDS)
Error
end
end
function erzeugung_matfile_exakte_Loesung(tBegin,tEnd,y) %#ok<*DEFNU>
%Automatisches erzeugen und speichern der exakten Lösung in 1h Bereichen für
%24Stunden
% save('StratoExakteLoesung_dt1h_24h')
% save('StratoExakteLoesung_dt1h_24h_2')
stunde=3600;
nn = (tEnd-tBegin)/3600;
tNeu = tBegin;
options = odeset('RelTol',1e-3,'AbsTol',1e-3);
for ii=1:nn
tBegin=tNeu;
tNeu=tNeu+stunde;
[tt_temp,yy_temp] = ode23s(@odefun,[tBegin,tNeu],y,options);
tm=length(tt_temp);
exakte_Loesung(ii).tt_temp=tt_temp(tm); %#ok<*AGROW>
exakte_Loesung(ii).yy_temp=yy_temp(tm,:); %#ok<*AGROW>
save('test_niedrige_tol_-3','exakte_Loesung')
end
disp('>>>>>>>>>>>>>exakte Loesung erzeugt!')
end