-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdynamics.cpp
More file actions
296 lines (167 loc) · 8.72 KB
/
dynamics.cpp
File metadata and controls
296 lines (167 loc) · 8.72 KB
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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
#include "dynamics.h"
#include "network.h"
double PlantDynamics(int i, const state_type &x, Network network){
double temp1; // Hält den Ausdruck für die Konkurrenz der Pflanzen.
double temp2; // Hält den Ausdruck, der in Zähler und Nenner der functional response der Pflanze vorkommt.
int MaxPlants = network.get_MaxPlants();
vector<double> traits = network.get_PlantTraits();
vector<double> connecs = network.get_AnimalConnectivities();
vector<vector<double>> adj = network.get_AdjacencyMatrix();
// Bestimmung von temp1.
temp1 = 0;
for(int m = 0; m < network.get_noofplants(); m++){
temp1 = temp1 + (u + (w-u)*exp(-pow( (network.get_PlantTraits()[i] - network.get_PlantTraits()[m])/xi, 2)))*x[m];
};
// Bestimmung von temp2.
temp2 = 0;
for(int m = 0; m < network.get_noofanimals(); m++){
if(connecs[m] > 0){temp2 = temp2 + adj[m][i]*x[MaxPlants + m];}
};
return r*(1 - temp1)*(temp2/(r + temp2))*x[i] - mu_P*x[i];
}
double AnimalDynamics(int j, const state_type &x, Network network){
double temp1; // Hält den Ausdruck im Zähler der einzelnen functional responses, über die für den Bestäuber summiert wird.
double temp2; // Hält den Ausdruck im Nenner der einzelnen functional responses, über die für den Bestäuber summiert wird.
double temp3; // Hält die Summe über alle functional responses, über die für den Bestäuber summiert wird.
int MaxPlants = network.get_MaxPlants();
vector<double> AnimalTraits = network.get_AnimalTraits();
vector<double> connecs = network.get_AnimalConnectivities();
vector<vector<double>> adj = network.get_AdjacencyMatrix();
if(connecs[j] == 0){ // Wenn das Tier nicht verbunden ist, gibt es keinen Wachstumsterm.
return - mu_A*x[MaxPlants + j];
}
else{
// Bestimmung von temp3.
temp3 = 0;
for(int i = 0; i < network.get_noofplants(); i++){
temp1 = adj[j][i]*x[i];
temp2 = 0;
for(int m = 0; m < network.get_noofanimals(); m++){
temp2 = temp2 + adj[m][i]*x[MaxPlants + m];
};
temp3 = temp3 + temp1/(r + temp2);
};
return lambda*x[MaxPlants + j]*temp3 - mu_A*x[MaxPlants + j];
}
}
state_type MakeInitialCondition(int MaxPlants, int MaxAnimals){
int MaxSpecies = MaxPlants + MaxAnimals;
state_type x(MaxSpecies);
x.assign(MaxSpecies + 1, 0);
x[0] = 0.05; // 10.0*PLANT_EXTINCTION_THRESHOLD; // also... 0.1. vorher: 1.0;
x[MaxPlants] = 0.001; // 10.0*EXTINCTION_THRESHOLD; // also... 0.001. vorher: 1.0;
return x;
}
void AddInitialDensity(Network network, state_type &x){
int noofplants = network.get_noofplants();
// Gehe alle mit Pflanzen korrespondierenden Einträge von x durch. Falls einer davon 0 ist, setze ihn auf auf einen Wert leicht oberhalb der Pflanzen-Aussterbeschwelle.
for(int i = 0; i < noofplants; i++){
if(x[i] == 0){x[i] = 1.0001*PLANT_EXTINCTION_THRESHOLD;}
};
int MaxPlants = network.get_MaxPlants();
int noofanimals = network.get_noofanimals();
// Gehe alle mit Tieren korresnpondieren Einträge von x durch. Falls einer davon 0 ist, setze ihn auf 0.01.
for(int j = MaxPlants; j < MaxPlants + noofanimals; j++){
if(x[j] == 0){x[j] = 1.0001*EXTINCTION_THRESHOLD;}
};
}
bool DoExtinctions(Network &network, state_type &x){
bool extinction_detected = false; // Kontrollvariable, die ihren Wert zu true ändert, falls ein Aussterbeereignis zum Halt der Intgeration geführt hat.
// Überprüfung der Pflanzen.
int noofplants = network.get_noofplants(); // noofplants entspricht der Anzahl der Pflanzen im Netzwerk vor Beginn der Integration.
vector<int> extinct_plants = {}; // Vektor, der die ausgestorbenen Pflanzen speichert.
for(int i = 0; i < noofplants; i++){
if(x[i] != 0 && x[i] < PLANT_EXTINCTION_THRESHOLD){ // Wenn eine Spezies ausgestorben ist, ...
extinction_detected = true; // ändere den Ausgabewert von DoExtinctions() zu true, ...
extinct_plants.push_back(i); // füge die Pflanze zur Liste ausgestorbener Pflanzen hinzu und ...
x[i] = 0; // setze ihre Populationsdichte auf 0.
}
};
network.delete_plants(extinct_plants); // Löscht die ausgestorbenen Pflanzen aus dem Netz.
// Jetzt passen wir noch den Pflanzen-Teil des Zustandsvektors an. Sonst stehen ggf. mittendrin Nullen und NetDynamics arbeitet nicht korrekt.
int ctr = 0; // Zählt die ausgestorbenen Pflanzen.
for(int i = 0; i < noofplants; i++){
if(x[i] == 0){ctr++;}
};
for(int k = 0; k < ctr; k++){
for(int i = 1; i < noofplants; i++){
if(x[i-1] == 0){swap(x[i-1], x[i]);} // Wenn das Element links neben Dir 0 ist, tausche mit ihm den Platz.
};
};
// Überprüfung der Tiere.
int MaxPlants = network.get_MaxPlants();
int noofanimals = network.get_noofanimals(); // noofanimals entspricht der Anzahl der Tiere im Netzwerk vor Beginn der Integration.
vector<int> extinct_animals = {}; // Vektor, der die ausgestorbenen Tiere speichert.
for(int j = MaxPlants; j < MaxPlants + noofanimals; j++){
if(x[j] != 0 && x[j] < EXTINCTION_THRESHOLD){ // Wenn eine Spezies ausgestorben ist, ...
extinction_detected = true; // ändere den Ausgabewert von DoExtinctions zu true, ...
extinct_animals.push_back(j - MaxPlants); // füge das Tier zur Liste ausgestorbener Tiere hinzu und ...
x[j] = 0; // setze die Populationsdichte auf 0.
}
};
network.delete_animals(extinct_animals); // Löscht die ausgestorbenen Tiere aus dem Netz.
// Jetzt passen wir noch den Tiere-Teil des Zustandsvektors an. Sonst stehen ggf. mittendrin Nullen und NetDynamics arbeitet nicht korrekt.
ctr = 0; // Zählt nun die ausgestorbenen Tiere.
for(int j = MaxPlants; j < MaxPlants + noofanimals; j++){
if(x[j] == 0){ctr++;}
};
for(int k = 0; k < ctr; k++){
for(int j = MaxPlants + 1; j < MaxPlants + noofanimals; j++){
if(x[j-1] == 0){swap(x[j-1], x[j]);} // Wenn das Element links neben Dir 0 ist tausche mit ihm den Platz.
};
};
return extinction_detected;
}
bool DoExtinctions(Network &network, state_type &x, double time){
bool extinction_detected = false; // Kontrollvariable, die ihren Wert zu true ändert, falls ein Aussterbeereignis zum Halt der Intgeration geführt hat.
// Überprüfung der Pflanzen.
int noofplants = network.get_noofplants(); // noofplants entspricht der Anzahl der Pflanzen im Netzwerk vor Beginn der Integration.
vector<int> extinct_plants = {}; // Vektor, der die ausgestorbenen Pflanzen speichert.
for(int i = 0; i < noofplants; i++){
if(x[i] != 0 && x[i] < PLANT_EXTINCTION_THRESHOLD){ // Wenn eine Spezies ausgestorben ist, ...
extinction_detected = true; // ändere den Ausgabewert von DoExtinctions() zu true, ...
extinct_plants.push_back(i); // füge die Pflanze zur Liste ausgestorbener Pflanzen hinzu und ...
x[i] = 0; // setze ihre Populationsdichte auf 0.
}
};
network.delete_plants(extinct_plants, time); // Löscht die ausgestorbenen Pflanzen aus dem Netz.
// Jetzt passen wir noch den Pflanzen-Teil des Zustandsvektors an. Sonst stehen ggf. mittendrin Nullen und NetDynamics arbeitet nicht korrekt.
int ctr = 0; // Zählt die ausgestorbenen Pflanzen.
for(int i = 0; i < noofplants; i++){
if(x[i] == 0){ctr++;}
};
for(int k = 0; k < ctr; k++){
for(int i = 1; i < noofplants; i++){
if(x[i-1] == 0){swap(x[i-1], x[i]);} // Wenn das Element links neben Dir 0 ist, tausche mit ihm den Platz.
};
};
// Überprüfung der Tiere.
int MaxPlants = network.get_MaxPlants();
int noofanimals = network.get_noofanimals(); // noofanimals entspricht der Anzahl der Tiere im Netzwerk vor Beginn der Integration.
vector<int> extinct_animals = {}; // Vektor, der die ausgestorbenen Tiere speichert.
for(int j = MaxPlants; j < MaxPlants + noofanimals; j++){
if(x[j] != 0 && x[j] < EXTINCTION_THRESHOLD){ // Wenn eine Spezies ausgestorben ist, ...
extinction_detected = true; // ändere den Ausgabewert von DoExtinctions zu true, ...
extinct_animals.push_back(j - MaxPlants); // füge das Tier zur Liste ausgestorbener Tiere hinzu und ...
x[j] = 0; // setze die Populationsdichte auf 0.
}
};
network.delete_animals(extinct_animals, time); // Löscht die ausgestorbenen Tiere aus dem Netz.
// Jetzt passen wir noch den Tiere-Teil des Zustandsvektors an. Sonst stehen ggf. mittendrin Nullen und NetDynamics arbeitet nicht korrekt.
ctr = 0; // Zählt nun die ausgestorbenen Tiere.
for(int j = MaxPlants; j < MaxPlants + noofanimals; j++){
if(x[j] == 0){ctr++;}
};
for(int k = 0; k < ctr; k++){
for(int j = MaxPlants + 1; j < MaxPlants + noofanimals; j++){
if(x[j-1] == 0){swap(x[j-1], x[j]);} // Wenn das Element links neben Dir 0 ist tausche mit ihm den Platz.
};
};
return extinction_detected;
}
double GetPlantGrowthRate(int i, const state_type &x, Network network){
return PlantDynamics(i, x, network);
}
double GetAnimalGrowthRate(int j, const state_type &x, Network network){
return AnimalDynamics(j, x, network);
}