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-2025 Forschungszentrum Juelich GmbH
18*/
19
25#include "mptrac.h"
26
27#ifdef KPP
28#include "kpp_chem.h"
29#endif
30
31int main(
32 int argc,
33 char *argv[]) {
34
35 ctl_t *ctl;
36
37 atm_t *atm;
38
39 cache_t *cache;
40
41 clim_t *clim;
42
43 met_t *met0, *met1;
44
45#ifdef DD
46 mpi_info_t mpi_info;
47#endif
48
49 FILE *dirlist;
50
51 char dirname[LEN], filename[2 * LEN];
52
53 int ntask = -1, rank = 0, size = 1;
54
55 /* Initialize MPI... */
56#ifdef MPI
57 MPI_Init(&argc, &argv);
58 MPI_Comm_rank(MPI_COMM_WORLD, &rank);
59 MPI_Comm_size(MPI_COMM_WORLD, &size);
60
61#ifdef DD
62 mpi_info.rank = rank;
63 mpi_info.size = size;
64#endif
65
66#endif
67
68 /* Check arguments... */
69 if (argc < 4)
70 ERRMSG("Give parameters: <dirlist> <ctl> <atm_in>");
71
72 /* Open directory list... */
73 if (!(dirlist = fopen(argv[1], "r")))
74 ERRMSG("Cannot open directory list!");
75
76 /* Loop over directories... */
77 while (fscanf(dirlist, "%4999s", dirname) != EOF) {
78
79 /* MPI parallelization... */
80 if ((++ntask) % size != rank)
81 continue;
82
83 /* Write info... */
84 LOG(1, "Parallelization: ntask= %d | rank= %d | size= %d",
85 ntask, rank, size);
86
87 /* ------------------------------------------------------------
88 Initialize model run...
89 ------------------------------------------------------------ */
90
91 /* Start timers... */
93
94 /* Allocate memory... */
95 mptrac_alloc(&ctl, &cache, &clim, &met0, &met1, &atm);
96
97 /* Read control parameters... */
98 sprintf(filename, "%s/%s", dirname, argv[2]);
99 mptrac_read_ctl(filename, argc, argv, ctl);
100
101 /* Read climatological data... */
102 mptrac_read_clim(ctl, clim);
103
104 /* Read atmospheric data... */
105 sprintf(filename, "%s/%s", dirname, argv[3]);
106 if (!mptrac_read_atm(filename, ctl, atm))
107 ERRMSG("Cannot open file!");
108
109 /* Initialize MPTRAC... */
110 mptrac_init(ctl, cache, clim, atm, ntask);
111
112 /* ------------------------------------------------------------
113 Loop over timesteps...
114 ------------------------------------------------------------ */
115
116 /* Loop over timesteps... */
117 for (double t = ctl->t_start;
118 ctl->direction * (t - ctl->t_stop) < ctl->dt_mod;
119 t += ctl->direction * ctl->dt_mod) {
120
121 /* Adjust length of final time step... */
122 if (ctl->direction * (t - ctl->t_stop) > 0)
123 t = ctl->t_stop;
124
125 /* Get meteo data... */
126 mptrac_get_met(ctl, clim, t, &met0, &met1);
127
128 /* Check time step... */
129 if (ctl->dt_mod > fabs(met0->lon[1] - met0->lon[0]) * 111132. / 150.)
130 WARN("Violation of CFL criterion! Check DT_MOD!");
131
132 /* Set-up domain decomposition... */
133#ifdef DD
134 // TODO: Remove dd_init_flg ...
135 static int dd_init_flg = 0;
136 if (t == ctl->t_start || !dd_init_flg)
137 dd_init(ctl, &mpi_info, atm, &met0, t, &dd_init_flg);
138#endif
139
140 /* Run a single time step... */
141#ifdef DD
142 mptrac_run_timestep(ctl, cache, clim, &met0, &met1, atm, t, &mpi_info);
143#else
144 mptrac_run_timestep(ctl, cache, clim, &met0, &met1, atm, t);
145#endif
146
147 /* Write output... */
148 mptrac_write_output(dirname, ctl, met0, met1, atm, t);
149 }
150
151 /* ------------------------------------------------------------
152 Finalize model run...
153 ------------------------------------------------------------ */
154
155 /* Flush output buffer... */
156 fflush(NULL);
157
158 /* Report problem size... */
159 LOG(1, "SIZE_NP = %d", atm->np);
160 LOG(1, "SIZE_MPI_TASKS = %d", size);
161 LOG(1, "SIZE_OMP_THREADS = %d", omp_get_max_threads());
162
163 /* Report memory usage... */
164 LOG(1, "MEMORY_ATM = %g MByte", sizeof(atm_t) / 1024. / 1024.);
165 LOG(1, "MEMORY_CACHE = %g MByte", sizeof(cache_t) / 1024. / 1024.);
166 LOG(1, "MEMORY_CLIM = %g MByte", sizeof(clim_t) / 1024. / 1024.);
167 LOG(1, "MEMORY_METEO = %g MByte", sizeof(met_t) / 1024. / 1024.);
168
169 /* Free memory... */
170#ifdef DD
171 mptrac_free(ctl, cache, clim, met0, met1, atm, &mpi_info);
172#else
173 mptrac_free(ctl, cache, clim, met0, met1, atm);
174#endif
175
176 /* Report timers... */
179 }
180
181 /* Finalize MPI... */
182#ifdef MPI
183 MPI_Finalize();
184#endif
185
186 return EXIT_SUCCESS;
187}
void mptrac_alloc(ctl_t **ctl, cache_t **cache, clim_t **clim, met_t **met0, met_t **met1, atm_t **atm)
Allocates and initializes memory resources for MPTRAC.
Definition: mptrac.c:4212
void mptrac_get_met(ctl_t *ctl, clim_t *clim, const double t, met_t **met0, met_t **met1)
Retrieves meteorological data for the specified time.
Definition: mptrac.c:4301
void mptrac_init(ctl_t *ctl, cache_t *cache, clim_t *clim, atm_t *atm, const int ntask)
Initializes the MPTRAC model and its associated components.
Definition: mptrac.c:4422
void mptrac_read_clim(const ctl_t *ctl, clim_t *clim)
Reads various climatological data and populates the given climatology structure.
Definition: mptrac.c:4512
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:4441
void mptrac_write_output(const char *dirname, const ctl_t *ctl, met_t *met0, met_t *met1, atm_t *atm, const double t)
Writes various types of output data to files in a specified directory.
Definition: mptrac.c:5928
void mptrac_free(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t *met0, met_t *met1, atm_t *atm)
Frees memory resources allocated for MPTRAC.
Definition: mptrac.c:4269
void mptrac_run_timestep(ctl_t *ctl, cache_t *cache, clim_t *clim, met_t **met0, met_t **met1, atm_t *atm, double t)
Executes a single timestep of the MPTRAC model simulation.
Definition: mptrac.c:5571
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:4572
MPTRAC library declarations.
#define LEN
Maximum length of ASCII data lines.
Definition: mptrac.h:309
#define ERRMSG(...)
Print an error message with contextual information and terminate the program.
Definition: mptrac.h:2102
#define WARN(...)
Print a warning message with contextual information.
Definition: mptrac.h:2069
#define START_TIMERS
Starts a timer for tracking.
Definition: mptrac.h:2201
#define PRINT_TIMERS
Print the current state of all timers.
Definition: mptrac.h:2161
#define LOG(level,...)
Print a log message with a specified logging level.
Definition: mptrac.h:2032
#define STOP_TIMERS
Stop the current timer.
Definition: mptrac.h:2216
Air parcel data.
Definition: mptrac.h:3371
int np
Number of air parcels.
Definition: mptrac.h:3374
Cache data structure.
Definition: mptrac.h:3449
Climatological data.
Definition: mptrac.h:3589
Control parameters.
Definition: mptrac.h:2356
int direction
Direction flag (1=forward calculation, -1=backward calculation).
Definition: mptrac.h:2660
double t_stop
Stop time of simulation [s].
Definition: mptrac.h:2666
double dt_mod
Time step of simulation [s].
Definition: mptrac.h:2669
double t_start
Start time of simulation [s].
Definition: mptrac.h:2663
Meteo data structure.
Definition: mptrac.h:3648
double lon[EX]
Longitudes [deg].
Definition: mptrac.h:3673
MPI information data.
Definition: mptrac.h:3426
int rank
Rank of node.
Definition: mptrac.h:3428
int size
Size of node.
Definition: mptrac.h:3431
int main(int argc, char *argv[])
Definition: trac.c:31