MPTRAC
trac.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-2026 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27#ifdef KPP
28#include "kpp_chem.h"
29#endif
30
31/* ------------------------------------------------------------
32 Functions...
33 ------------------------------------------------------------ */
34
36void usage(
37 void);
38
39/* ------------------------------------------------------------
40 Main...
41 ------------------------------------------------------------ */
42
43int main(
44 int argc,
45 char *argv[]) {
46
47 ctl_t *ctl;
48
49 atm_t *atm;
50
51 cache_t *cache;
52
53 clim_t *clim;
54
55 depo_t *depo;
56
57 met_t *met0, *met1;
58
59 dd_t *dd;
60
61 FILE *dirlist;
62
63 char dirname[LEN], filename[2 * LEN];
64
65 int ntask = -1, rank = 0, size = 1;
66
67 /* Print usage information... */
68 USAGE;
69
70 /* Initialize MPI... */
71#ifdef MPI
72 MPI_Init(&argc, &argv);
73 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
74 MPI_Comm_size(MPI_COMM_WORLD, &size);
75#ifdef _OPENACC
76 const int num_devices = acc_get_num_devices(acc_device_nvidia);
77 if (num_devices <= 0)
78 ERRMSG("Not running on a GPU device!");
79 acc_set_device_num(rank % num_devices, acc_device_nvidia);
80#endif
81#endif
82
83 /* Check arguments... */
84 if (argc < 4)
85 ERRMSG("Missing or invalid command-line arguments.\n\n"
86 "Usage: trac <dirlist> <ctl> <atm_in> [KEY VALUE ...]\n\n"
87 "Use -h for full help.");
88
89 /* Open directory list... */
90 if (!(dirlist = fopen(argv[1], "r")))
91 ERRMSG("Cannot open directory list!");
92
93 /* Loop over directories... */
94 while (fscanf(dirlist, "%4999s", dirname) != EOF) {
95
96 /* MPI parallelization... */
97 if ((++ntask) % size != rank)
98 continue;
99
100 /* Write info... */
101 LOG(1, "Parallelization: ntask= %d | rank= %d | size= %d",
102 ntask, rank, size);
103
104 /* ------------------------------------------------------------
105 Initialize model run...
106 ------------------------------------------------------------ */
107
108 /* Allocate memory... */
109 mptrac_alloc(&ctl, &cache, &clim, &met0, &met1, &atm, &depo, &dd);
110
111 /* Read control parameters... */
112 sprintf(filename, "%s/%s", dirname, argv[2]);
113 mptrac_read_ctl(filename, argc, argv, ctl);
114
115 /* Read climatological data... */
116 mptrac_read_clim(ctl, clim);
117
118 /* Read atmospheric data... */
119 sprintf(filename, "%s/%s", dirname, argv[3]);
120 if (!mptrac_read_atm(filename, ctl, atm))
121 ERRMSG("Cannot open file!");
122
123 /* Initialize MPTRAC... */
124 mptrac_init(ctl, cache, clim, atm, depo, ntask);
125
126 /* ------------------------------------------------------------
127 Loop over timesteps...
128 ------------------------------------------------------------ */
129
130 /* Loop over timesteps... */
131 for (double t = ctl->t_start;
132 ctl->direction * (t - ctl->t_stop) < ctl->dt_mod;
133 t += ctl->direction * ctl->dt_mod) {
134
135 /* Adjust length of final time step... */
136 if (ctl->direction * (t - ctl->t_stop) > 0)
137 t = ctl->t_stop;
138
139 /* Get meteo data... */
140 mptrac_get_met(ctl, clim, t, &met0, &met1, dd);
141
142 /* Check time step... */
143 if (ctl->dt_mod > fabs(met0->lon[1] - met0->lon[0]) * 111132. / 150.)
144 WARN("Violation of CFL criterion! Check DT_MOD!");
145
146#ifdef DD
147 /* Set-up domain decomposition... */
148 if ((t == ctl->t_start))
149 dd_init(ctl, dd, atm);
150#else
151 /* Catch misconfiguration... */
152 if ((ctl->dd)
154 ERRMSG
155 ("Model configured for domain decomposition, but not compiled for it!");
156#endif
157
158 /* Run a single time step... */
159 mptrac_run_timestep(ctl, cache, clim, &met0, &met1, atm, depo, t, dd);
160
161 /* Write output... */
162 mptrac_write_output(dirname, ctl, met0, met1, atm, depo, t);
163 }
164
165 /* ------------------------------------------------------------
166 Finalize model run...
167 ------------------------------------------------------------ */
168
169 /* Flush output buffer... */
170 fflush(NULL);
171
172 /* Report problem size... */
173 LOG(1, "SIZE_NP = %d", atm->np);
174 LOG(1, "SIZE_MPI_TASKS = %d", size);
175 LOG(1, "SIZE_OMP_THREADS = %d", omp_get_max_threads());
176
177 /* Report memory usage... */
178 LOG(1, "MEMORY_ATM = %g MByte", sizeof(atm_t) / 1024. / 1024.);
179 LOG(1, "MEMORY_CACHE = %g MByte", sizeof(cache_t) / 1024. / 1024.);
180 LOG(1, "MEMORY_DEPO = %g MByte", sizeof(depo_t) / 1024. / 1024.);
181 LOG(1, "MEMORY_CLIM = %g MByte", sizeof(clim_t) / 1024. / 1024.);
182 LOG(1, "MEMORY_METEO = %g MByte", sizeof(met_t) / 1024. / 1024.);
183
184 /* Free memory... */
185 mptrac_free(ctl, cache, clim, met0, met1, atm, depo, dd);
186
187 /* Report timers... */
189 }
190
191 /* Finalize MPI... */
192#ifdef MPI
193 MPI_Finalize();
194#endif
195
196 return EXIT_SUCCESS;
197}
198
199/*****************************************************************************/
200
202void usage(
203 void) {
204
205 printf("\nMPTRAC trac tool.\n\n");
206 printf("Run forward or backward trajectory calculations.\n");
207 printf("\n");
208 printf("Usage:\n");
209 printf(" trac <dirlist> <ctl> <atm_in> [KEY VALUE ...]\n");
210 printf("\n");
211 printf("Arguments:\n");
212 printf(" <dirlist> Text file containing work directories to process.\n");
213 printf(" <ctl> Control file name relative to each work directory.\n");
214 printf
215 (" <atm_in> Atmospheric input file name relative to each work directory.\n");
216 printf(" [KEY VALUE] Optional control parameters.\n");
217 printf("\nFurther information:\n");
218 printf(" Manual: https://slcs-jsc.github.io/mptrac/\n");
219}
void mptrac_free(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t *met0, met_t *met1, atm_t *atm, depo_t *depo, dd_t *dd)
Frees memory resources allocated for MPTRAC.
Definition: mptrac.c:6377
void mptrac_write_output(const char *dirname, const ctl_t *ctl, met_t *met0, met_t *met1, atm_t *atm, depo_t *depo, const double t)
Writes various types of output data to files in a specified directory.
Definition: mptrac.c:8230
void mptrac_get_met(ctl_t *ctl, clim_t *clim, const double t, met_t **met0, met_t **met1, dd_t *dd)
Retrieves meteorological data for the specified time.
Definition: mptrac.c:6438
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:6663
void mptrac_init(ctl_t *ctl, cache_t *cache, clim_t *clim, atm_t *atm, depo_t *depo, const int ntask)
Initializes the MPTRAC model and its associated components.
Definition: mptrac.c:6563
int mptrac_read_atm(const char *filename, const ctl_t *ctl, atm_t *atm)
Reads air parcel data from a specified file into the given atmospheric structure.
Definition: mptrac.c:6588
void mptrac_alloc(ctl_t **ctl, cache_t **cache, clim_t **clim, met_t **met0, met_t **met1, atm_t **atm, depo_t **depo, dd_t **dd)
Allocates and initializes memory resources for MPTRAC.
Definition: mptrac.c:6294
void mptrac_run_timestep(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t **met0, met_t **met1, atm_t *atm, depo_t *depo, double t, dd_t *dd)
Executes a single timestep of the MPTRAC model simulation.
Definition: mptrac.c:7851
void mptrac_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:6723
MPTRAC library declarations.
void dd_init(const ctl_t *ctl, dd_t *dd, atm_t *atm)
Initialize the domain decomposition infrastructure.
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:559
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2406
#define USAGE
Print usage information on -h or --help.
Definition: mptrac.h:2213
#define WARN(...)
Print a warning message with contextual information.
Definition: mptrac.h:2373
#define PRINT_TIMERS
Print the current state of all timers.
Definition: mptrac.h:2465
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2336
Air parcel data.
Definition: mptrac.h:3563
int np
Number of air parcels.
Definition: mptrac.h:3566
Cache data structure.
Definition: mptrac.h:3618
Climatological data.
Definition: mptrac.h:3785
Control parameters.
Definition: mptrac.h:2494
int direction
Direction flag (1=forward calculation, -1=backward calculation).
Definition: mptrac.h:2822
int dd
Domain decomposition (0=no, 1=yes, with 2x2 if not specified).
Definition: mptrac.h:3539
double t_stop
Stop time of simulation [s].
Definition: mptrac.h:2828
double dt_mod
Time step of simulation [s].
Definition: mptrac.h:2831
int dd_subdomains_zonal
Domain decomposition zonal subdomain number.
Definition: mptrac.h:3542
double t_start
Start time of simulation [s].
Definition: mptrac.h:2825
int dd_subdomains_meridional
Domain decomposition meridional subdomain number.
Definition: mptrac.h:3545
Domain decomposition data structure.
Definition: mptrac.h:4021
Ground inventories of deposited radionuclides.
Definition: mptrac.h:3654
Meteo data structure.
Definition: mptrac.h:3844
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3865
int main(int argc, char *argv[])
Definition: trac.c:43
void usage(void)
Print command-line help.
Definition: trac.c:202