forked from ObbeTuinenburg/UTrack-atmospheric-moisture
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
293 lines (256 loc) · 9.61 KB
/
main.c
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
/*
* Obbe Tuinenburg
* Recycling model
*/
#include <omp.h>
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <time.h>
#include <netcdf.h>
#include "recycling.h"
#include "defines.h"
int main(int argc, char *argv[]) {
// Get the starting date and simulation length from command line arguments
int year=atoi(argv[1]);
int month=atoi(argv[2]);
int day=atoi(argv[3]);
int release_days=atoi(argv[4]); // Number of days to emit the tracked moisture
int nday=release_days+24; // Number of days to run the simulation
struct simulation_settings* allsims= NULL;
struct simulation_settings* initial = NULL;
FILE *ifp;
int simulationtype,interpolationtype,nparcelsreleased;
char inputfilepath[100];
char outputfilepath[100];
// date utils
int month_days[] = {0, 31, 28, 31, 30, 31, 30, 31 ,31 ,30, 31, 30, 31};
if(year%4 == 0 && year%100 != 0 || year%400 == 0)
{
month_days[2] = 29;
} else {
month_days[2] = 28;
}
// Looping integers
int i,j,k,l;
// OpenMP variables
int iam = 0 ,np = 1;
// Allocate memory for ERA5 lsm
short* lsm = malloc(sizeof(short)*721*1440);
double* lsmadd = malloc(sizeof(double));
double* lsmscale = malloc(sizeof(double));
get_lsm(lsm,lsmadd,lsmscale);
int num_locations=0;
float curlatrad,degreelength;
l=0;
ifp = fopen("list.txt", "r");
if (ifp == NULL)
{
fprintf(stderr, "Can't open input list.txt\n");
exit(1);
}
while (fscanf(ifp,"%d %d %d %s %s",
&simulationtype, &interpolationtype, &nparcelsreleased, inputfilepath,outputfilepath)
!= EOF)
{
if (initial == NULL)
{
allsims = initial = malloc(sizeof(struct simulation_settings));
}
else
{
allsims->next = malloc(sizeof(struct simulation_settings));
allsims = allsims->next;
}
allsims->simulation_number=simulationtype;
if (simulationtype==4){allsims->mixing=6.0;}
if (simulationtype==8){allsims->mixing=1.0;}
if (simulationtype==9){allsims->mixing=24.0;}
if (simulationtype==10){allsims->mixing=120.0;}
if (simulationtype==11){allsims->mixing=1.0;}
if (simulationtype==12){allsims->mixing=6.0;}
if (simulationtype==13){allsims->mixing=24.0;}
if (simulationtype==14){allsims->mixing=120.0;}
allsims->interpolation=interpolationtype;
allsims->release_parcel=nparcelsreleased;
allsims->runtime=0;
strcpy(allsims->inputfile,inputfilepath);
strcpy(allsims->outputfile,outputfilepath);
// Allocate memory for input
allsims->release_input = malloc(sizeof(int)*721*1440);
get_input(allsims->release_input,inputfilepath);
// Calculate the number of parcels to release during the simulation
num_locations=0;
for (i=0;i<721*1440;i++){
if (allsims->release_input[i]==1){
num_locations++;
}
}
allsims->parcels_in_system=malloc(sizeof(int));
*allsims->parcels_in_system=0;
allsims->release_days=release_days;
allsims->nday=nday;
allsims->num_release=num_locations*allsims->release_days*24*allsims->release_parcel;
allsims->grid_size_NW=malloc(sizeof(float)*721);
allsims->grid_size_EW=malloc(sizeof(float)*721);
for (i=0;i<721;i++){
curlatrad=(90-i*0.25)*2*3.14159/360;
degreelength=((111412.84*cos(curlatrad))-93.5*cos(3*curlatrad)+0.118*cos(5*curlatrad))/1000;
allsims->grid_size_EW[i]=degreelength;
degreelength=(111132.92+(-559.82*cos(2*curlatrad))+1.175*cos(4*curlatrad)-0.0023*cos(6*curlatrad))/1000;
allsims->grid_size_NW[i]=degreelength;}
// Allocate memory for output
if (allsims->simulation_number==7){allsims->insystem = malloc(sizeof(double)*25*721*1440);
memset (allsims->insystem, 0, sizeof (double) * 25*721*1440);} // 3D state for Eulerian
if (allsims->simulation_number==7||allsims->simulation_number==6||allsims->simulation_number==15){
allsims->budget= malloc(sizeof(double)*25*721*1440);}
if (allsims->simulation_number==15){allsims->insystem = malloc(sizeof(double)*2*721*1440);
memset (allsims->insystem, 0, sizeof (double) * 2*721*1440);} // 3D state for Eulerian
allsims->insystem_out = malloc(sizeof(double)*721*1440);
allsims->allocated_out = malloc(sizeof(double)*721*1440);
allsims->emitted_out = malloc(sizeof(double)*721*1440);
for (i=0;i<721*1440;i++){
allsims->insystem_out[i]=0;
allsims->allocated_out[i]=0;
allsims->emitted_out[i]=0;
}
// Allocated memory for parcels during tracking
allsims->particle_lat=malloc(sizeof(float)*allsims->num_release);
allsims->particle_lon=malloc(sizeof(float)*allsims->num_release);
allsims->particle_lev=malloc(sizeof(float)*allsims->num_release);
allsims->particle_theta=malloc(sizeof(float)*allsims->num_release);
allsims->particle_original=malloc(sizeof(float)*allsims->num_release);
allsims->particle_present=malloc(sizeof(float)*allsims->num_release);
allsims->particle_curtime=malloc(sizeof(float)*allsims->num_release);
}
// Seed the randomizer.//
unsigned int iseed = (unsigned int)time(NULL);
srand (iseed);
// Load data for first two hours from netcdf files
struct meteoday meteo[2];
int curmonth=month;
int curday=day;
int curyear=year;
int curhour=0;
clock_t begin = clock();
clock_t end = clock();
// Allocated memory for two hours of data
meteo[0].q=malloc(sizeof(float) * 25* 721* 1440);
meteo[0].u=malloc(sizeof(float) * 25* 721* 1440);
meteo[0].v=malloc(sizeof(float) * 25* 721* 1440);
meteo[0].w=malloc(sizeof(float) * 25* 721* 1440);
meteo[0].t=malloc(sizeof(float) * 25* 721* 1440);
meteo[0].tadd=malloc(sizeof(double));
meteo[0].tscale=malloc(sizeof(double));
meteo[0].wadd=malloc(sizeof(double));
meteo[0].wscale=malloc(sizeof(double));
meteo[0].vadd=malloc(sizeof(double));
meteo[0].vscale=malloc(sizeof(double));
meteo[0].uadd=malloc(sizeof(double));
meteo[0].uscale=malloc(sizeof(double));
meteo[0].qadd=malloc(sizeof(double));
meteo[0].qscale=malloc(sizeof(double));
meteo[0].pw=malloc(sizeof(float) * 721* 1440);
meteo[0].nf=malloc(sizeof(float) * 721* 1440);
meteo[0].ef=malloc(sizeof(float) * 721* 1440);
meteo[0].E=malloc(sizeof(float) * 721* 1440);
meteo[0].P=malloc(sizeof(float) * 721* 1440);
meteo[0].pwadd=malloc(sizeof(double));
meteo[0].pwscale=malloc(sizeof(double));
meteo[0].nfadd=malloc(sizeof(double));
meteo[0].nfscale=malloc(sizeof(double));
meteo[0].efadd=malloc(sizeof(double));
meteo[0].efscale=malloc(sizeof(double));
meteo[0].Eadd=malloc(sizeof(double));
meteo[0].Escale=malloc(sizeof(double));
meteo[0].Padd=malloc(sizeof(double));
meteo[0].Pscale=malloc(sizeof(double));
meteo[0].lat=malloc(sizeof(float) * 721);
meteo[0].lon=malloc(sizeof(float) * 1440);
meteo[1].q=malloc(sizeof(float) * 25* 721* 1440);
meteo[1].u=malloc(sizeof(float) * 25* 721* 1440);
meteo[1].v=malloc(sizeof(float) * 25* 721* 1440);
meteo[1].w=malloc(sizeof(float) * 25* 721* 1440);
meteo[1].t=malloc(sizeof(float) * 25* 721* 1440);
meteo[1].tadd=malloc(sizeof(double));
meteo[1].tscale=malloc(sizeof(double));
meteo[1].wadd=malloc(sizeof(double));
meteo[1].wscale=malloc(sizeof(double));
meteo[1].vadd=malloc(sizeof(double));
meteo[1].vscale=malloc(sizeof(double));
meteo[1].uadd=malloc(sizeof(double));
meteo[1].uscale=malloc(sizeof(double));
meteo[1].qadd=malloc(sizeof(double));
meteo[1].qscale=malloc(sizeof(double));
meteo[1].pw=malloc(sizeof(float) * 721* 1440);
meteo[1].nf=malloc(sizeof(float) * 721* 1440);
meteo[1].ef=malloc(sizeof(float) * 721* 1440);
meteo[1].E=malloc(sizeof(float) * 721* 1440);
meteo[1].P=malloc(sizeof(float) * 721* 1440);
meteo[1].pwadd=malloc(sizeof(double));
meteo[1].pwscale=malloc(sizeof(double));
meteo[1].nfadd=malloc(sizeof(double));
meteo[1].nfscale=malloc(sizeof(double));
meteo[1].efadd=malloc(sizeof(double));
meteo[1].efscale=malloc(sizeof(double));
meteo[1].Eadd=malloc(sizeof(double));
meteo[1].Escale=malloc(sizeof(double));
meteo[1].Padd=malloc(sizeof(double));
meteo[1].Pscale=malloc(sizeof(double));
meteo[1].lat=malloc(sizeof(float) * 721);
meteo[1].lon=malloc(sizeof(float) * 1440);
// Load initial two hours of forcing data into memory
struct meteoday *p;
i=load_meteo_hour(curyear,curmonth,curday,curhour,&(meteo[0]));
i=load_meteo_hour(curyear,curmonth,curday,curhour,&(meteo[1]));
// ERA5 Latitude and Longitude values
float* lats=malloc(sizeof(float)*721);
float* lons=malloc(sizeof(float)*1440);
for (i=0;i<721;i++){lats[i]=90-0.25*i;}
for (i=0;i<1440;i++){lons[i]=0.25*i;}
int daynum=0;
while (daynum<nday-1){
// Free array for low time point
meteo[0]=meteo[1];
// Load new day
curhour++;
if (curhour>23){
curhour=0;
curday++;
daynum++;
if (curday>month_days[curmonth]){
curday=1;
curmonth++;
if (curmonth>12){
curmonth=1;
curyear++;}}}
i=load_meteo_hour(curyear,curmonth,curday,curhour,&meteo[1]);
printf("%d %d %d %d\n",curyear,curmonth,curday,curhour);
allsims = initial;
while (allsims)
{
begin = clock();
// For all simulations, do simulation for one hour
allsims->daynum=daynum;
allsims->hour=curhour;
if (allsims->simulation_number<6||(allsims->simulation_number>7&&allsims->simulation_number<15)){
lagrangian_simulation(meteo,*allsims);}
if (allsims->simulation_number>5&&allsims->simulation_number<8){
eulerian_simulation(meteo,*allsims);}
end = clock();
allsims->runtime+=(float)(end - begin) / CLOCKS_PER_SEC;
allsims=allsims->next;
}
}
// Output
// Write output arrays to netcdf file
allsims = initial;
while (allsims)
{
i=write_output(allsims->emitted_out,allsims->insystem_out,allsims->allocated_out,lats,lons,allsims->nday,allsims->outputfile,allsims->runtime);
allsims=allsims->next;
}
return 0;
}