MPTRAC
atm_init.c
Go to the documentation of this file.
1/*
2 This file is part of MPTRAC.
3
4 MPTRAC is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation, either version 3 of the License, or
7 (at your option) any later version.
8
9 MPTRAC is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with MPTRAC. If not, see <http://www.gnu.org/licenses/>.
16
17 Copyright (C) 2013-2024 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27int main(
28 int argc,
29 char *argv[]) {
30
31 atm_t *atm;
32
33 ctl_t ctl;
34
35 gsl_rng *rng;
36
37 /* Allocate... */
38 ALLOC(atm, atm_t, 1);
39
40 /* Check arguments... */
41 if (argc < 3)
42 ERRMSG("Give parameters: <ctl> <atm_out>");
43
44 /* Read control parameters... */
45 read_ctl(argv[1], argc, argv, &ctl);
46 double t0 = scan_ctl(argv[1], argc, argv, "INIT_T0", -1, "0", NULL);
47 double t1 = scan_ctl(argv[1], argc, argv, "INIT_T1", -1, "0", NULL);
48 double dt = scan_ctl(argv[1], argc, argv, "INIT_DT", -1, "1", NULL);
49 double z0 = scan_ctl(argv[1], argc, argv, "INIT_Z0", -1, "0", NULL);
50 double z1 = scan_ctl(argv[1], argc, argv, "INIT_Z1", -1, "0", NULL);
51 double dz = scan_ctl(argv[1], argc, argv, "INIT_DZ", -1, "1", NULL);
52 double lon0 = scan_ctl(argv[1], argc, argv, "INIT_LON0", -1, "0", NULL);
53 double lon1 = scan_ctl(argv[1], argc, argv, "INIT_LON1", -1, "0", NULL);
54 double dlon = scan_ctl(argv[1], argc, argv, "INIT_DLON", -1, "1", NULL);
55 double lat0 = scan_ctl(argv[1], argc, argv, "INIT_LAT0", -1, "0", NULL);
56 double lat1 = scan_ctl(argv[1], argc, argv, "INIT_LAT1", -1, "0", NULL);
57 double dlat = scan_ctl(argv[1], argc, argv, "INIT_DLAT", -1, "1", NULL);
58 double st = scan_ctl(argv[1], argc, argv, "INIT_ST", -1, "0", NULL);
59 double sz = scan_ctl(argv[1], argc, argv, "INIT_SZ", -1, "0", NULL);
60 double slon = scan_ctl(argv[1], argc, argv, "INIT_SLON", -1, "0", NULL);
61 double slat = scan_ctl(argv[1], argc, argv, "INIT_SLAT", -1, "0", NULL);
62 double sx = scan_ctl(argv[1], argc, argv, "INIT_SX", -1, "0", NULL);
63 double ut = scan_ctl(argv[1], argc, argv, "INIT_UT", -1, "0", NULL);
64 double uz = scan_ctl(argv[1], argc, argv, "INIT_UZ", -1, "0", NULL);
65 double ulon = scan_ctl(argv[1], argc, argv, "INIT_ULON", -1, "0", NULL);
66 double ulat = scan_ctl(argv[1], argc, argv, "INIT_ULAT", -1, "0", NULL);
67 int even =
68 (int) scan_ctl(argv[1], argc, argv, "INIT_EVENLY", -1, "0", NULL);
69 int rep = (int) scan_ctl(argv[1], argc, argv, "INIT_REP", -1, "1", NULL);
70 double m = scan_ctl(argv[1], argc, argv, "INIT_MASS", -1, "0", NULL);
71 double vmr = scan_ctl(argv[1], argc, argv, "INIT_VMR", -1, "0", NULL);
72 double bellrad =
73 scan_ctl(argv[1], argc, argv, "INIT_BELLRAD", -1, "0", NULL);
74
75 /* Initialize random number generator... */
76 gsl_rng_env_setup();
77 rng = gsl_rng_alloc(gsl_rng_default);
78
79 /* Create grid... */
80 for (double t = t0; t <= t1; t += dt)
81 for (double z = z0; z <= z1; z += dz)
82 for (double lon = lon0; lon <= lon1; lon += dlon)
83 for (double lat = lat0; lat <= lat1; lat += dlat)
84 for (int irep = 0; irep < rep; irep++) {
85
86 /* Set position... */
87 double rg = gsl_ran_gaussian_ziggurat(rng, st / 2.3548);
88 double ru = ut * (gsl_rng_uniform(rng) - 0.5);
89 atm->time[atm->np] = (t + rg + ru);
90
91 rg = gsl_ran_gaussian_ziggurat(rng, sz / 2.3548);
92 ru = uz * (gsl_rng_uniform(rng) - 0.5);
93 atm->p[atm->np] = P(z + rg + ru);
94
95 rg = gsl_ran_gaussian_ziggurat(rng, slon / 2.3548);
96 double rx =
97 gsl_ran_gaussian_ziggurat(rng, DX2DEG(sx, lat) / 2.3548);
98 ru = ulon * (gsl_rng_uniform(rng) - 0.5);
99 atm->lon[atm->np] = (lon + rg + rx + ru);
100
101 do {
102 rg = gsl_ran_gaussian_ziggurat(rng, slat / 2.3548);
103 rx = gsl_ran_gaussian_ziggurat(rng, DY2DEG(sx) / 2.3548);
104 ru = ulat * (gsl_rng_uniform(rng) - 0.5);
105 atm->lat[atm->np] = (lat + rg + rx + ru);
106 } while (even && gsl_rng_uniform(rng) >
107 fabs(cos(DEG2RAD(atm->lat[atm->np]))));
108
109 /* Apply cosine bell (Williamson et al., 1992)... */
110 if (bellrad > 0) {
111 double x0[3], x1[3];
112 geo2cart(0.0, 0.5 * (lon0 + lon1), 0.5 * (lat0 + lat1), x0);
113 geo2cart(0.0, atm->lon[atm->np], atm->lat[atm->np], x1);
114 double rad = RE * acos(DOTP(x0, x1) / NORM(x0) / NORM(x1));
115 if (rad > bellrad)
116 continue;
117 if (ctl.qnt_m >= 0)
118 atm->q[ctl.qnt_m][atm->np] =
119 0.5 * (1. + cos(M_PI * rad / bellrad));
120 if (ctl.qnt_vmr >= 0)
121 atm->q[ctl.qnt_vmr][atm->np] =
122 0.5 * (1. + cos(M_PI * rad / bellrad));
123 }
124
125 /* Set particle counter... */
126 if ((++atm->np) > NP)
127 ERRMSG("Too many particles!");
128 }
129
130 /* Check number of air parcels... */
131 if (atm->np <= 0)
132 ERRMSG("Did not create any air parcels!");
133
134 /* Initialize mass... */
135 if (ctl.qnt_m >= 0 && bellrad <= 0)
136 for (int ip = 0; ip < atm->np; ip++)
137 atm->q[ctl.qnt_m][ip] = m / atm->np;
138
139 /* Initialize volume mixing ratio... */
140 if (ctl.qnt_vmr >= 0 && bellrad <= 0)
141 for (int ip = 0; ip < atm->np; ip++)
142 atm->q[ctl.qnt_vmr][ip] = vmr;
143
144 /* Initialize air parcel index... */
145 if (ctl.qnt_idx >= 0)
146 for (int ip = 0; ip < atm->np; ip++)
147 atm->q[ctl.qnt_idx][ip] = ip;
148
149 /* Initialize age of air... */
150 if (ctl.qnt_aoa >= 0)
151 for (int ip = 0; ip < atm->np; ip++)
152 atm->q[ctl.qnt_aoa][ip] = atm->time[ip];
153
154 /* Save data... */
155 write_atm(argv[2], &ctl, atm, 0);
156
157 /* Free... */
158 gsl_rng_free(rng);
159 free(atm);
160
161 return EXIT_SUCCESS;
162}
int main(int argc, char *argv[])
Definition: atm_init.c:27
void write_atm(const char *filename, const ctl_t *ctl, const atm_t *atm, const double t)
Writes air parcel data to a file in various formats.
Definition: mptrac.c:8472
double scan_ctl(const char *filename, int argc, char *argv[], const char *varname, const int arridx, const char *defvalue, char *value)
Scans a control file or command-line arguments for a specified variable.
Definition: mptrac.c:8098
void read_ctl(const char *filename, int argc, char *argv[], ctl_t *ctl)
Reads control parameters from a configuration file and populates the given structure.
Definition: mptrac.c:4805
void geo2cart(const double z, const double lon, const double lat, double *x)
Converts geographic coordinates (longitude, latitude, altitude) to Cartesian coordinates.
Definition: mptrac.c:986
MPTRAC library declarations.
#define RE
Mean radius of Earth [km].
Definition: mptrac.h:217
#define DOTP(a, b)
Calculate the dot product of two vectors.
Definition: mptrac.h:604
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:1901
#define P(z)
Compute pressure at given altitude.
Definition: mptrac.h:1289
#define DX2DEG(dx, lat)
Convert a distance in kilometers to degrees longitude at a given latitude.
Definition: mptrac.h:521
#define NORM(a)
Compute the norm (magnitude) of a vector.
Definition: mptrac.h:1232
#define ALLOC(ptr, type, n)
Allocate memory for a pointer with error handling.
Definition: mptrac.h:347
#define DEG2RAD(deg)
Converts degrees to radians.
Definition: mptrac.h:476
#define NP
Maximum number of atmospheric data points.
Definition: mptrac.h:241
#define DY2DEG(dy)
Convert a distance in kilometers to degrees latitude.
Definition: mptrac.h:539
Air parcel data.
Definition: mptrac.h:3120
double time[NP]
Time [s].
Definition: mptrac.h:3126
double lat[NP]
Latitude [deg].
Definition: mptrac.h:3135
double lon[NP]
Longitude [deg].
Definition: mptrac.h:3132
int np
Number of air parcels.
Definition: mptrac.h:3123
double q[NQ][NP]
Quantity data (for various, user-defined attributes).
Definition: mptrac.h:3138
double p[NP]
Pressure [hPa].
Definition: mptrac.h:3129
Control parameters.
Definition: mptrac.h:2155
int qnt_m
Quantity array index for mass.
Definition: mptrac.h:2204
int qnt_aoa
Quantity array index for age of air.
Definition: mptrac.h:2456
int qnt_vmr
Quantity array index for volume mixing ratio.
Definition: mptrac.h:2207
int qnt_idx
Quantity array index for air parcel IDs.
Definition: mptrac.h:2195