PartMC  2.8.0
partmc.F90
Go to the documentation of this file.
1 ! Copyright (C) 2007-2012, 2016, 2017, 2018, 2021 Nicole Riemer and Matthew West
2 ! Licensed under the GNU General Public License version 2 or (at your
3 ! option) any later version. See the file COPYING for details.
4 
5 !> \file
6 !> The partmc program.
7 
8 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
9 
10 !> \mainpage PartMC Code Documentation
11 !!
12 !! \subpage input_format - Input file format description.
13 !!
14 !! \subpage output_format - Output file format description.
15 !!
16 !! \subpage module_diagram - Diagram of modules and dependencies.
17 !!
18 !! \subpage coding_style - Description of code conventions and style.
19 !!
20 !! \subpage publications - Publications about PartMC.
21 
22 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
23 
24 !> \page input_format Input File Format
25 !!
26 !! The input file format is plain text:
27 !!
28 !! \subpage spec_file_format "Spec File Format"
29 !!
30 !! When running PartMC with the command <tt>partmc input.spec</tt> the
31 !! first line of the <tt>input.spec</tt> file must define the \c
32 !! run_type with:
33 !! <pre>
34 !! run_type &lt;type&gt;
35 !! </pre>
36 !! where <tt>&lt;type&gt;</tt> is one of \c particle, \c exact, or \c
37 !! sectional. This determines the type of run as well as the format of the
38 !! remainder of the spec file. The rest of the spec file is described by:
39 !!
40 !! \subpage input_format_particle "Particle-resolved simulation"
41 !!
42 !! \subpage input_format_exact "Exact (analytical) solution"
43 !!
44 !! \subpage input_format_sectional "Sectional model simulation"
45 
46 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
47 
48 !> \page module_diagram Module Diagram
49 !!
50 !! \dotfile partmc_modules.gv
51 
52 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
53 
54 !> \page coding_style Coding Style
55 !!
56 !! The code is mainly modern Fortran, with a few parts still clearly
57 !! showing their Fortran 77 heritage. Fortran 2003 features are used
58 !! heavily (especially allocatable array features). The code needs to
59 !! be processed with \c cpp or a compatible pre-processor.
60 !!
61 !! \section oo_fortran Object Oriented Fortran
62 !!
63 !! Extensive use is made of Fortran 90 derived types. Derived types
64 !! are named \c my_type_t and are generally defined in modules named
65 !! \c pmc_my_type within files named \c my_type.F90. Almost all
66 !! subroutines and function in each \c my_type.F90 file have names of
67 !! the form \c my_type_*() and take an object of type \c my_type_t
68 !! (called \c my_type) as the first argument on which to operate.
69 !!
70 !! Module names are always the same as the name of the containing
71 !! file, but prefixed with \c pmc_. Thus the module \c
72 !! pmc_condense is contained in the file \c condense.F90.
73 !!
74 !! \section mem_manage Memory Management
75 !!
76 !! The memory allocation policy is to always use \c allocatable
77 !! arrays and to do the allocation in the lowest-level routine
78 !! possible. Explicit \c allocate() and \c deallocate() statements are
79 !! discouraged in favor of automatic memory management, where
80 !! possible.
81 
82 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
83 
84 !> \page publications PartMC Publications
85 !!
86 !! - Z.&nbsp;Zheng, J.&nbsp;H. Curtis, Y. Yao, J.&nbsp;T. Gasparik,
87 !! V.&nbsp;G. Anantharaj, L. Zhao, M. West, and N. Riemer (2021)
88 !! Estimating submicron aerosol mixing state at the global scale
89 !! with machine learning and earth system modeling, <i>Earth and
90 !! Space Science</i> <b>8</b>(2), e2020EA001500, DOI: <a
91 !! href="http://dx.doi.org/10.1029/2020EA001500">10.1029/2020EA001500</a>.
92 !! - J.&nbsp;T. Gasparik, Q. Ye, J.&nbsp;H. Curtis,
93 !! A.&nbsp;A. Presto, N.&nbsp;M. Donahue, R.&nbsp;C. Sullivan,
94 !! M. West, and N. Riemer (2020) Quantifying errors in the aerosol
95 !! mixing-state index based on limited particle sample size,
96 !! <i>Aerosol Science and Technology</i> <b>54</b>(12), 1527-1541,
97 !! DOI: <a
98 !! href="http://dx.doi.org/10.1080/02786826.2020.1804523">10.1080/02786826.2020.1804523</a>.
99 !! - C. Shou, N. Riemer, T.&nbsp;B. Onasch, A.&nbsp;J. Sedlacek,
100 !! A.&nbsp;T. Lambe, E.&nbsp;R. Lewis, P. Davidovits, and M. West
101 !! (2019) Mixing state evolution of agglomerating particles in an
102 !! aerosol chamber: Comparison of measurements and
103 !! particle-resolved simulations, <i>Aerosol Science and
104 !! Technology</i> <b>53</b>(11), 1229-1243, DOI: <a
105 !! href="http://dx.doi.org/10.1080/02786826.2019.1661959">10.1080/02786826.2019.1661959</a>
106 !! - N. Riemer, A.&nbsp;P. Ault, M. West, R.&nbsp;L. Craig, and
107 !! J.&nbsp;H. Curtis (2019) Aerosol mixing state: Measurements,
108 !! modeling, and impacts, <i>Reviews of Geophysics</i>
109 !! <b>57</b>(2), 187-249, DOI: <a
110 !! href="http://dx.doi.org/10.1029/2018RG000615">10.1029/2018RG000615</a>
111 !! - R.&nbsp;E.&nbsp;L. DeVille, N. Riemer, and M. West (2019)
112 !! Convergence of a generalized Weighted Flow Algorithm for
113 !! stochastic particle coagulation, <i>Journal of Computational
114 !! Dynamics</i> <b>6</b>(1), 69-94, DOI: <a
115 !! href="http://dx.doi.org/10.3934/jcd.2019003">10.3934/jcd.2019003</a>
116 !! - M.&nbsp;Hughes, J.&nbsp;K.&nbsp;Kodros, J.&nbsp;R.&nbsp;Pierce,
117 !! M.&nbsp;West, and N.&nbsp;Riemer (2018) Machine learning to
118 !! predict the global distribution of aerosol mixing state
119 !! metrics, <i>Atmosphere</i> <b>9</b>(1), 15, DOI: <a
120 !! href="http://dx.doi.org/10.3390/atmos9010015">10.3390/atmos9010015</a>.
121 !! - J.&nbsp;Ching, M.&nbsp;West, and N.&nbsp;Riemer (2018)
122 !! Quantifying impacts of aerosol mixing state on
123 !! nucleation-scavenging of black carbon aerosol particles,
124 !! <i>Atmosphere</i> <b>9</b>(1), 17, DOI: <a
125 !! href="http://dx.doi.org/10.3390/atmos9010017">10.3390/atmos9010017</a>.
126 !! - J.&nbsp;H.&nbsp;Curtis, N.&nbsp;Riemer, and M.&nbsp;West,
127 !! (2017) A single-column particle-resolved model for simulating
128 !! the vertical distribution of aerosol mixing state:
129 !! WRF-PartMC-MOSAIC-SCM v1.0, <i>Geoscientific Model
130 !! Development</i> <b>10</b>, 4057-4079, DOI: <a
131 !! href="http://dx.doi.org/10.5194/gmd-10-4057-2017">10.5194/gmd-10-4057-2017</a>.
132 !! - J.&nbsp;Tian, B.&nbsp;T.&nbsp;Brem, M.&nbsp;West,
133 !! T.&nbsp;C.&nbsp;Bond, M.&nbsp;J.&nbsp;Rood, and N.&nbsp;Riemer
134 !! (2017) Simulating aerosol chamber experiments with the
135 !! particle-resolved aerosol model PartMC, <i>Aerosol Science and
136 !! Technology</i> <b>51</b>(7), 856-867, DOI: <a
137 !! href="http://dx.doi.org/10.1080/02786826.2017.1311988">10.1080/02786826.2017.1311988</a>.
138 !! - J.&nbsp;Ching, J.&nbsp;Fast, M.&nbsp;West, and N.&nbsp;Riemer
139 !! (2017) Metrics to quantify the importance of mixing state for
140 !! CCN activity, <i>Atmospheric Chemistry and Physics</i>
141 !! <b>17</b>, 7445-7458, DOI: <a
142 !! href="http://dx.doi.org/10.5194/acp-17-7445-2017">10.5194/acp-17-7445-2017</a>.
143 !! - J.&nbsp;Ching, N.&nbsp;Riemer, and M.&nbsp;West (2016) Black
144 !! carbon mixing state impacts on cloud microphysical properties:
145 !! Effects of aerosol plume and environmental conditions,
146 !! <i>Journal of Geophysical Research</i> <b>121</b>(10),
147 !! 5990-6013, DOI: <a
148 !! href="http://dx.doi.org/10.1002/2016JD024851">10.1002/2016JD024851</a>.
149 !! - J.&nbsp;H.&nbsp;Curtis, M.&nbsp;D.&nbsp;Michelotti,
150 !! N.&nbsp;Riemer, M.&nbsp;Heath, and M.&nbsp;West (2016)
151 !! Accelerated simulation of stochastic particle removal processes
152 !! in particle-resolved aerosol models, <i>Journal of
153 !! Computational Physics</i> <b>322</b>, 21-32, DOI: <a
154 !! href="http://dx.doi.org/10.1016/j.jcp.2016.06.029">10.1016/j.jcp.2016.06.029</a>.
155 !! - R.&nbsp;M.&nbsp;Healy, N.&nbsp;Riemer, J.&nbsp;C.&nbsp;Wenger,
156 !! M.&nbsp;Murphy, M.&nbsp;West, L.&nbsp;Poulain,
157 !! A.&nbsp;Wiedensohler, I.&nbsp;P.&nbsp;O'Connor,
158 !! E.&nbsp;McGillicuddy, J.&nbsp;R.&nbsp;Sodeau, and
159 !! G.&nbsp;J.&nbsp;Evans, Single particle diversity and mixing
160 !! state measurements, <i>Atmospheric Chemistry and Physics</i>
161 !! <b>14</b>, 6289-6299, DOI: <a
162 !! href="http://dx.doi.org/10.5194/acp-14-6289-2014">10.5194/acp-14-6289-2014</a>.
163 !! - J.&nbsp;Tian, N.&nbsp;Riemer, M.&nbsp;West,
164 !! L.&nbsp;Pfaffenberger, H.&nbsp;Schlager, and A.&nbsp;Petzold
165 !! (2014) Modeling the evolution of aerosol particles in a ship
166 !! plume using PartMC-MOSAIC, <i>Atmospheric Chemistry and
167 !! Physics</i> <b>14</b>, 5327-5347, DOI: <a
168 !! href="http://dx.doi.org/10.5194/acp-14-5327-2014">10.5194/acp-14-5327-2014</a>.
169 !! - N.&nbsp;Riemer and M.&nbsp;West (2013) Quantifying aerosol mixing
170 !! state with entropy and diversity measures, <i>Atmospheric
171 !! Chemistry and Physics</i> <b>13</b>, 11423-11439, DOI: <a
172 !! href="http://dx.doi.org/10.5194/acp-13-11423-2013">10.5194/acp-13-11423-2013</a>.
173 !! - M.&nbsp;D.&nbsp;Michelotti, M.&nbsp;T.&nbsp;Heath, and
174 !! M.&nbsp;West (2013) Binning for efficient stochastic multiscale
175 !! particle simulations, <i>Atmospheric Chemistry and Physics</i>
176 !! <b>11</b>(4), 1071-1096, DOI: <a
177 !! href="http://dx.doi.org/10.1137/130908038">10.1137/130908038</a>.
178 !! - J.&nbsp;Ching, N.&nbsp;Riemer, and M.&nbsp;West (2012) Impacts of
179 !! black carbon mixing state on black carbon nucleation scavenging:
180 !! Insights from a particle-resolved model, <i>Journal of
181 !! Geophysical Research</i> <b>117</b>(D23209), DOI: <a
182 !! href="http://dx.doi.org/10.1029/2012JD018269">10.1029/2012JD018269</a>.
183 !! - R.&nbsp;E.&nbsp;L.&nbsp;DeVille, N.&nbsp;Riemer, and
184 !! M.&nbsp;West (2011) Weighted Flow Algorithms (WFA) for
185 !! stochastic particle coagulation, <i>Journal of Computational
186 !! Physics</i> <b>230</b>(23), 8427-8451, DOI: <a
187 !! href="http://dx.doi.org/10.1016/j.jcp.2011.07.027">10.1016/j.jcp.2011.07.027</a>.
188 !! - R.&nbsp;A.&nbsp;Zaveri, J.&nbsp;C.&nbsp;Barnard,
189 !! R.&nbsp;C.&nbsp;Easter, N.&nbsp;Riemer, and M.&nbsp;West (2010)
190 !! Particle-resolved simulation of aerosol size, composition,
191 !! mixing state, and the associated optical and cloud condensation
192 !! nuclei activation properties in an evolving urban plume,
193 !! <i>Journal of Geophysical Research</i> <b>115</b>(D17210), DOI: <a
194 !! href="http://dx.doi.org/10.1029/2009JD013616">10.1029/2009JD013616</a>.
195 !! - N.&nbsp;Riemer, M.&nbsp;West, R.&nbsp;A.&nbsp;Zaveri, and
196 !! R.&nbsp;C.&nbsp;Easter (2010) Estimating black carbon aging
197 !! time-scales with a particle-resolved aerosol model, <i>Journal
198 !! of Aerosol Science</i> <b>41</b>(1), 143-158, DOI: <a
199 !! href="http://dx.doi.org/10.1016/j.jaerosci.2009.08.009">10.1016/j.jaerosci.2009.08.009</a>
200 !! - N.&nbsp;Riemer, M.&nbsp;West, R.&nbsp;A.&nbsp;Zaveri, and
201 !! R.&nbsp;C.&nbsp;Easter (2009) Simulating the evolution of soot
202 !! mixing state with a particle-resolved aerosol model, <i>Journal
203 !! of Geophysical Research</i> <b>114</b>(D09202), DOI: <a
204 !! href="http://dx.doi.org/10.1029/2008JD011073">10.1029/2008JD011073</a>
205 !! - R.&nbsp;McGraw, L.&nbsp;Leng, W.&nbsp;Zhu, N.&nbsp;Riemer, and
206 !! M.&nbsp;West (2008) Aerosol dynamics using the quadrature
207 !! method of moments: Comparing several quadrature schemes with
208 !! particle-resolved simulation, <i>Journal of Physics: Conference
209 !! Series</i> <b>125</b>(012020), DOI: <a
210 !! href="http://dx.doi.org/10.1088/1742-6596/125/1/012020">10.1088/1742-6596/125/1/012020</a>
211 
212 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
213 
214 !> Top level driver.
215 program partmc
216 
217  use pmc_mpi
218  use pmc_bin_grid
219  use pmc_aero_state
220  use pmc_aero_dist
221  use pmc_aero_binned
222  use pmc_coag_kernel
223  use pmc_aero_data
224  use pmc_scenario
225  use pmc_env_state
226  use pmc_run_part
227  use pmc_run_exact
228  use pmc_run_sect
229  use pmc_spec_file
230  use pmc_gas_data
231  use pmc_gas_state
232  use pmc_util
233 #ifdef PMC_USE_CAMP
234  use camp_camp_core
235  use pmc_photolysis
236 #endif
237 #ifdef PMC_USE_SUNDIALS
238  use pmc_condense
239 #endif
240 
241  character(len=300) :: spec_name
242 
243  call pmc_mpi_init()
244 
245  if (pmc_mpi_rank() == 0) then
246  ! only the root process accesses the commandline
247 
248  if (command_argument_count() /= 1) then
249  call print_usage()
250  call die_msg(739173192, "invalid commandline arguments")
251  end if
252 
253  call get_command_argument(1, spec_name)
254  end if
255 
256  call pmc_mpi_bcast_string(spec_name)
257  call partmc_run(spec_name)
258 
259  call pmc_mpi_finalize()
260 
261 contains
262 
263 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
264 
265  !> Print the usage text to stderr.
266  subroutine print_usage()
267 
268  write(*,*) 'Usage: partmc <spec-file>'
269 
270  end subroutine print_usage
271 
272 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
273 
274  !> Do a PartMC run.
275  subroutine partmc_run(spec_name)
276 
277  !> Spec filename.
278  character(len=*), intent(in) :: spec_name
279 
280  type(spec_file_t) :: file
281  character(len=100) :: run_type
282  integer :: i
283 
284  ! check filename (must be "filename.spec")
285  i = len_trim(spec_name)
286  if (spec_name((i-4):i) /= '.spec') then
287  call die_msg(710381938, "input filename must end in .spec")
288  end if
289 
290  if (pmc_mpi_rank() == 0) then
291  ! only the root process does I/O
292  call spec_file_open(spec_name, file)
293  call spec_file_read_string(file, 'run_type', run_type)
294  end if
295 
296  call pmc_mpi_bcast_string(run_type)
297  if (trim(run_type) == 'particle') then
298  call partmc_part(file)
299  elseif (trim(run_type) == 'exact') then
300  call partmc_exact(file)
301  elseif (trim(run_type) == 'sectional') then
302  call partmc_sect(file)
303  else
304  call die_msg(719261940, "unknown run_type: " // trim(run_type))
305  end if
306 
307  end subroutine partmc_run
308 
309 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
310 
311  !> Run a Monte Carlo simulation.
312  subroutine partmc_part(file)
313 
314  !> Spec file.
315  type(spec_file_t), intent(inout) :: file
316 
317  type(gas_data_t) :: gas_data
318  type(gas_state_t) :: gas_state
319  type(gas_state_t) :: gas_state_init
320  type(aero_data_t) :: aero_data
321  type(aero_dist_t) :: aero_dist_init
322  type(aero_state_t) :: aero_state
323  type(aero_state_t) :: aero_state_init
324  type(scenario_t) :: scenario
325  type(env_state_t) :: env_state
326  type(env_state_t) :: env_state_init
327  type(run_part_opt_t) :: run_part_opt
328 #ifdef PMC_USE_CAMP
329  type(camp_core_t), pointer :: camp_core
330  type(photolysis_t), pointer :: photolysis
331 #endif
332  integer :: i_repeat, i_group
333  integer :: rand_init
334  character, allocatable :: buffer(:)
335  integer :: buffer_size, max_buffer_size
336  integer :: position
337  logical :: do_restart, do_init_equilibrate, aero_mode_type_exp_present
338  character(len=PMC_MAX_FILENAME_LEN) :: restart_filename
339  integer :: dummy_index, dummy_i_repeat
340  real(kind=dp) :: n_part
341 
342  !> \page input_format_particle Input File Format: Particle-Resolved Simulation
343  !!
344  !! See \ref spec_file_format for the input file text format.
345  !!
346  !! A particle-resolved simulation spec file has the parameters:
347  !! - \b run_type (string): must be \c particle
348  !! - \b output_prefix (string): prefix of the output filenames
349  !! --- see \ref output_format for the full name format
350  !! - \b n_repeat (integer): number of repeats
351  !! - \b n_part (integer): number of computational particles to
352  !! simulate (actual number used will vary between <tt>n_part /
353  !! 2</tt> and <tt>n_part * 2</tt> if \c allow_doubling and \c
354  !! allow_halving are \c yes)
355  !! - \b restart (logical): whether to restart the simulation from
356  !! a saved output data file. If \c restart is \c yes, then the
357  !! following parameters must also be provided:
358  !! - \b restart_file (string): name of file from which to load
359  !! restart data, which must be a PartMC output NetCDF file
360  !! - \b do_select_weighting (logical): whether to explicitly select
361  !! the weighting scheme. If \c do_select_weighting is \c yes, then the
362  !! following parameters must also be provided:
363  !! - \subpage input_format_weight_type
364  !! - \b t_max (real, unit s): total simulation time
365  !! - \b del_t (real, unit s): timestep size
366  !! - \b t_output (real, unit s): the interval on which to
367  !! output data to disk (see \ref output_format)
368  !! - \b t_progress (real, unit s): the interval on which to
369  !! write summary information to the screen while running
370  !! - \b do_camp_chem (logical): whether to run <b>CAMP</b>.
371  !! If \c do_camp_chem is \c yes, then the following parameters
372  !! must also be provided:
373  !! - \b camp_config (string): name of JSON file containing a list of \b
374  !! CAMP configuration files.
375  !! - \b gas_data (string): name of file from which to read the gas
376  !! material data (only provide if \c restart is \c no) --- the
377  !! file format should be \subpage input_format_gas_data
378  !! - \b gas_init (string): name of file from which to read the
379  !! initial gas state at the start of the simulation (only
380  !! provide option if \c restart is \c no) --- the file format
381  !! should be \subpage input_format_gas_state
382  !! - \b aerosol_data (string): name of file from which to read the
383  !! aerosol material data (only provide if \c restart is \c no)
384  !! --- the file format should be \subpage input_format_aero_data
385  !! - \b do_fractal (logical): whether to consider particles
386  !! as fractal agglomerates. If \c do_fractal is \c no, then all the
387  !! particles are treated as spherical. If \c do_fractal is \c yes,
388  !! then the following parameters must also be provided:
389  !! - \subpage input_format_fractal
390  !! - \b aerosol_init (string): filename containing the initial
391  !! aerosol state at the start of the simulation (only provide
392  !! option if \c restart is \c no) --- the file format should
393  !! be \subpage input_format_aero_dist
394  !! - \subpage input_format_scenario
395  !! - \subpage input_format_env_state
396  !! - \b do_coagulation (logical): whether to perform particle
397  !! coagulation. If \c do_coagulation is \c yes, then the
398  !! following parameters must also be provided:
399  !! - \subpage input_format_coag_kernel
400  !! - \b do_condensation (logical): whether to perform explicit
401  !! water condensation (requires SUNDIALS support to be compiled
402  !! in; cannot be used simultaneously with MOSAIC). If \c
403  !! do_condensation is \c yes, then the following parameters must
404  !! also be provided:
405  !! - \b do_init_equilibrate (logical): whether to equilibrate
406  !! the water content of each particle before starting the
407  !! simulation, note that \b do_init_equilibriate (sic!)
408  !! spelling will work as well for compatibility
409  !! - \b do_mosaic (logical): whether to use the MOSAIC chemistry
410  !! code (requires support to be compiled in; cannot be used
411  !! simultaneously with condensation). If \c do_mosaic is \c
412  !! yes, then the following parameters must also be provided:
413  !! - \b do_optical (logical): whether to compute optical
414  !! properties of the aerosol particles for the output files ---
415  !! see output_format_aero_state
416  !! - \b do_nucleation (logical): whether to perform particle
417  !! nucleation. If \c do_nucleation is \c yes, then the following
418  !! parameters must also be provided:
419  !! - \subpage input_format_nucleate
420  !! - \b rand_init (integer): if greater than zero then use as
421  !! the seed for the random number generator, or if zero then
422  !! generate a random seed for the random number generator ---
423  !! two simulations on the same machine with the same seed
424  !! (greater than 0) will produce identical output
425  !! - \b allow_doubling (logical): if \c yes, then whenever the
426  !! number of simulated particles falls below <tt>n_part /
427  !! 2</tt>, every particle is duplicated to give better
428  !! statistics
429  !! - \b allow_halving (logical): if \c yes, then whenever the
430  !! number of simulated particles rises above <tt>n_part *
431  !! 2</tt>, half of the particles are removed (chosen randomly)
432  !! to reduce the computational expense
433  !! - \b record_removals (logical): whether to record information
434  !! about aerosol particles removed from the simulation --- see
435  !! \ref output_format_aero_removed
436  !! - \b do_parallel (logical): whether to run in parallel mode
437  !! (requires MPI support to be compiled in). If \c do_parallel
438  !! is \c yes, then the following parameters must also be
439  !! provided:
440  !! - \subpage input_format_output
441  !! - \b mix_timescale (real, unit s): timescale on which to mix
442  !! aerosol particle information amongst processes in an
443  !! attempt to keep the aerosol state consistent (the mixing
444  !! rate is inverse to \c mix_timescale)
445  !! - \b gas_average (logical): whether to average the gas state
446  !! amongst processes each timestep, to ensure uniform gas
447  !! concentrations
448  !! - \b env_average (logical): whether to average the
449  !! environment state amongst processes each timestep, to
450  !! ensure a uniform environment
451  !! - \subpage input_format_parallel_coag
452 
453  ! initialize RNG with random seed for UUID generation
454  call pmc_srand(0, pmc_mpi_rank())
455 
456  if (pmc_mpi_rank() == 0) then
457  call spec_file_read_run_part(file, run_part_opt, aero_data, &
458  aero_state_init, gas_data, gas_state_init, env_state_init, &
459  aero_dist_init, scenario, &
460 #ifdef PMC_USE_CAMP
461  camp_core, photolysis, aero_state, &
462 #endif
463  n_part, rand_init, do_init_equilibrate, do_restart)
464 
465  if (.not. do_restart) then
466  call uuid4_str(run_part_opt%uuid)
467  end if
468  end if
469 
470  call pmc_mpi_broadcast_run_part(run_part_opt, aero_data, &
471  aero_state_init, gas_data, gas_state_init, env_state_init, &
472  aero_dist_init, scenario, &
473 #ifdef PMC_USE_CAMP
474  camp_core, photolysis, aero_state, &
475 #endif
476  n_part, rand_init, do_init_equilibrate, do_restart)
477 
478  ! initialize the chemistry solver
479  if (run_part_opt%do_camp_chem) then
480 #ifdef PMC_USE_CAMP
481  call camp_core%solver_initialize()
482 #endif
483  end if
484 
485  ! re-initialize RNG with the given seed
486  call pmc_rand_finalize()
487  call pmc_srand(rand_init, pmc_mpi_rank())
488 
489  run_part_opt%t_wall_start = system_clock_time()
490 
491  do i_repeat = 1,run_part_opt%n_repeat
492  run_part_opt%i_repeat = i_repeat
493 
494  gas_state = gas_state_init
495  if (do_restart) then
496  aero_state = aero_state_init
497  call aero_state_set_n_part_ideal(aero_state, n_part)
498  else
499  call aero_state_zero(aero_state)
500  aero_mode_type_exp_present &
501  = aero_dist_contains_aero_mode_type(aero_dist_init, &
502  aero_mode_type_exp) &
503  .or. scenario_contains_aero_mode_type(scenario, &
504  aero_mode_type_exp)
505  if (aero_mode_type_exp_present) then
506  call warn_msg(245301880, "using flat weighting only due to " &
507  // "presence of exp aerosol mode")
508  call aero_state_set_weight(aero_state, aero_data, &
510  else
511  call aero_state_set_weight(aero_state, aero_data, &
512  run_part_opt%weighting_type, run_part_opt%weighting_exponent)
513  end if
514  call aero_state_set_n_part_ideal(aero_state, n_part)
515  call aero_state_add_aero_dist_sample(aero_state, aero_data, &
516  aero_dist_init, 1d0, 1d0, 0d0, run_part_opt%allow_doubling, &
517  run_part_opt%allow_halving)
518  end if
519  env_state = env_state_init
520  call scenario_init_env_state(scenario, env_state, &
521  env_state_init%elapsed_time)
522 
523 #ifdef PMC_USE_SUNDIALS
524  if (do_init_equilibrate) then
525  call condense_equilib_particles(env_state, aero_data, aero_state)
526  end if
527 #endif
528 
529  if (run_part_opt%do_camp_chem) then
530 #ifdef PMC_USE_CAMP
531  call run_part(scenario, env_state, aero_data, aero_state, gas_data, &
532  gas_state, run_part_opt, camp_core=camp_core, &
533  photolysis=photolysis)
534 #endif
535  else
536  call run_part(scenario, env_state, aero_data, aero_state, gas_data, &
537  gas_state, run_part_opt)
538  end if
539 
540  end do
541 
542  if (run_part_opt%do_tchem) then
543 #ifdef PMC_USE_TCHEM
544  call pmc_tchem_cleanup()
545 #endif
546  end if
547 
548  call pmc_rand_finalize()
549 
550  end subroutine partmc_part
551 
552 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
553 
554  !> Run an exact solution simulation.
555  subroutine partmc_exact(file)
556 
557  !> Spec file.
558  type(spec_file_t), intent(inout) :: file
559 
560  character(len=100) :: soln_name
561  type(aero_data_t) :: aero_data
562  type(scenario_t) :: scenario
563  type(env_state_t) :: env_state
564  type(aero_dist_t) :: aero_dist_init
565  type(run_exact_opt_t) :: run_exact_opt
566  type(bin_grid_t) :: bin_grid
567  type(gas_data_t) :: gas_data
568  character(len=PMC_MAX_FILENAME_LEN) :: sub_filename
569  type(spec_file_t) :: sub_file
570 
571  !> \page input_format_exact Exact (Analytical) Solution
572  !!
573  !! The coagulation kernel and initial distribution must be matched
574  !! for an exact solution to exist. The valid choices are:
575  !!
576  !! <table>
577  !! <tr><th>Coagulation kernel</th>
578  !! <th>Initial aerosol distribution</th></tr>
579  !! <tr><td>Additive</td>
580  !! <td>Single exponential mode</td></tr>
581  !! <tr><td>Constant</td>
582  !! <td>Single exponential mode</td></tr>
583  !! <tr><td>Zero</td>
584  !! <td>Anything</td></tr>
585  !! </table>
586  !!
587  !! See \ref spec_file_format for the input file text format.
588  !!
589  !! An exact (analytical) simulation spec file has the parameters:
590  !! - \b run_type (string): must be \c exact
591  !! - \b output_prefix (string): prefix of the output filenames ---
592  !! the filenames will be of the form \c PREFIX_SSSSSSSS.nc where
593  !! \c SSSSSSSS is is the eight-digit output index (starting at 1
594  !! and incremented each time the state is output)
595  !! - \b t_max (real, unit s): total simulation time
596  !! - \b t_output (real, unit s): the interval on which to output
597  !! data to disk and to print progress information to the screen
598  !! (see \ref output_format)
599  !! - \subpage input_format_diam_bin_grid
600  !! - \b gas_data (string): name of file from which to read the
601  !! gas material data --- the file format should be
602  !! \subpage input_format_gas_data
603  !! - \b aerosol_data (string): name of file from which to read the
604  !! aerosol material data --- the file format should be
605  !! \subpage input_format_aero_data
606  !! - \b do_fractal (logical): whether to consider particles
607  !! as fractal agglomerates. If \c do_fractal is \c no, then all the
608  !! particles are treated as spherical. If \c do_fractal is \c yes,
609  !! then the following parameters must also be provided:
610  !! - \subpage input_format_fractal
611  !! - \b aerosol_init (string): filename containing the initial
612  !! aerosol state at the start of the simulation --- the file
613  !! format should be \subpage input_format_aero_dist
614  !! - \subpage input_format_scenario
615  !! - \subpage input_format_env_state
616  !! - \b do_coagulation (logical): whether to perform particle
617  !! coagulation. If \c do_coagulation is \c yes, then the
618  !! following parameters must also be provided:
619  !! - \subpage input_format_coag_kernel
620  !!
621  !! Example:
622  !! <pre>
623  !! run_type exact # exact solution
624  !! output_prefix additive_exact # prefix of output files
625  !!
626  !! t_max 600 # total simulation time (s)
627  !! t_output 60 # output interval (0 disables) (s)
628  !!
629  !! n_bin 160 # number of bins
630  !! d_min 1e-8 # minimum diameter (m)
631  !! d_max 1e-3 # maximum diameter (m)
632  !!
633  !! gas_data gas_data.dat # file containing gas data
634  !!
635  !! aerosol_data aero_data.dat # file containing aerosol data
636  !! do_fractal no # whether to do fractal treatment
637  !! aerosol_init aero_init_dist.dat # aerosol initial condition file
638  !!
639  !! temp_profile temp.dat # temperature profile file
640  !! height_profile height.dat # height profile file
641  !! gas_emissions gas_emit.dat # gas emissions file
642  !! gas_background gas_back.dat # background gas mixing ratios file
643  !! aero_emissions aero_emit.dat # aerosol emissions file
644  !! aero_background aero_back.dat # aerosol background file
645  !!
646  !! rel_humidity 0.999 # initial relative humidity (1)
647  !! pressure 1e5 # initial pressure (Pa)
648  !! latitude 0 # latitude (degrees, -90 to 90)
649  !! longitude 0 # longitude (degrees, -180 to 180)
650  !! altitude 0 # altitude (m)
651  !! start_time 0 # start time (s since 00:00 UTC)
652  !! start_day 1 # start day of year (UTC)
653  !!
654  !! do_coagulation yes # whether to do coagulation (yes/no)
655  !! kernel additive # Additive coagulation kernel
656  !! additive_kernel_coeff 1000d0 # Additive kernel constant
657  !! </pre>
658 
659  ! only serial code here
660  if (pmc_mpi_rank() /= 0) then
661  return
662  end if
663 
664  call spec_file_read_run_exact(file, run_exact_opt, aero_data, &
665  bin_grid, gas_data, env_state, aero_dist_init, scenario)
666 
667  call run_exact(bin_grid, scenario, env_state, aero_data, &
668  aero_dist_init, gas_data, run_exact_opt)
669 
670  call pmc_rand_finalize()
671 
672  end subroutine partmc_exact
673 
674 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
675 
676  !> Run a sectional code simulation.
677  subroutine partmc_sect(file)
678 
679  !> Spec file.
680  type(spec_file_t), intent(inout) :: file
681 
682  type(run_sect_opt_t) :: run_sect_opt
683  type(aero_data_t) :: aero_data
684  type(aero_dist_t) :: aero_dist_init
685  type(aero_state_t) :: aero_init
686  type(scenario_t) :: scenario
687  type(env_state_t) :: env_state
688  type(bin_grid_t) :: bin_grid
689  type(gas_data_t) :: gas_data
690  character(len=PMC_MAX_FILENAME_LEN) :: sub_filename
691  type(spec_file_t) :: sub_file
692 
693  !> \page input_format_sectional Sectional Model Simulation
694  !!
695  !! See \ref spec_file_format for the input file text format.
696  !!
697  !! A sectional simulation spec file has the parameters:
698  !! - \b run_type (string): must be \c sectional
699  !! - \b output_prefix (string): prefix of the output filenames ---
700  !! the filenames will be of the form \c PREFIX_SSSSSSSS.nc where
701  !! \c SSSSSSSS is is the eight-digit output index (starting at 1
702  !! and incremented each time the state is output)
703  !! - \b del_t (real, unit s): timestep size
704  !! - \b t_output (real, unit s): the interval on which to
705  !! output data to disk (see \ref output_format)
706  !! - \b t_progress (real, unit s): the interval on which to
707  !! write summary information to the screen while running
708  !! - \subpage input_format_diam_bin_grid
709  !! - \b gas_data (string): name of file from which to read the
710  !! gas material data --- the file format should be
711  !! \subpage input_format_gas_data
712  !! - \b aerosol_data (string): name of file from which to read the
713  !! aerosol material data --- the file format should be
714  !! \subpage input_format_aero_data
715  !! - \b do_fractal (logical): whether to consider particles
716  !! as fractal agglomerates. If \c do_fractal is \c no, then all the
717  !! particles are treated as spherical. If \c do_fractal is \c yes,
718  !! then the following parameters must also be provided:
719  !! - \subpage input_format_fractal
720  !! - \b aerosol_init (string): filename containing the initial
721  !! aerosol state at the start of the simulation --- the file
722  !! format should be \subpage input_format_aero_dist
723  !! - \subpage input_format_scenario
724  !! - \subpage input_format_env_state
725  !! - \b do_coagulation (logical): whether to perform particle
726  !! coagulation. If \c do_coagulation is \c yes, then the
727  !! following parameters must also be provided:
728  !! - \subpage input_format_coag_kernel
729  !!
730  !! Example:
731  !! <pre>
732  !! run_type sectional # sectional code run
733  !! output_prefix brown_sect # prefix of output files
734  !!
735  !! t_max 86400 # total simulation time (s)
736  !! del_t 60 # timestep (s)
737  !! t_output 3600 # output interval (0 disables) (s)
738  !! t_progress 600 # progress printing interval (0 disables) (s)
739  !!
740  !! n_bin 220 # number of bins
741  !! d_min 1e-10 # minimum diameter (m)
742  !! d_max 1e-4 # maximum diameter (m)
743  !!
744  !! gas_data gas_data.dat # file containing gas data
745  !! aerosol_data aero_data.dat # file containing aerosol data
746  !! do_fractal no # whether to do fractal treatment
747  !! aerosol_init aero_init_dist.dat # initial aerosol distribution
748  !!
749  !! temp_profile temp.dat # temperature profile file
750  !! height_profile height.dat # height profile file
751  !! gas_emissions gas_emit.dat # gas emissions file
752  !! gas_background gas_back.dat # background gas mixing ratios file
753  !! aero_emissions aero_emit.dat # aerosol emissions file
754  !! aero_background aero_back.dat # aerosol background file
755  !!
756  !! rel_humidity 0.999 # initial relative humidity (1)
757  !! pressure 1e5 # initial pressure (Pa)
758  !! latitude 0 # latitude (degrees_north, -90 to 90)
759  !! longitude 0 # longitude (degrees_east, -180 to 180)
760  !! altitude 0 # altitude (m)
761  !! start_time 0 # start time (s since 00:00 UTC)
762  !! start_day 1 # start day of year (UTC)
763  !!
764  !! do_coagulation yes # whether to do coagulation (yes/no)
765  !! kernel brown # coagulation kernel
766  !! </pre>
767 
768  ! only serial code here
769  if (pmc_mpi_rank() /= 0) then
770  return
771  end if
772 
773  call spec_file_read_run_sect(file, run_sect_opt, aero_data, bin_grid, &
774  gas_data, env_state, aero_dist_init, scenario)
775 
776  call run_sect(bin_grid, gas_data, aero_data, aero_dist_init, scenario, &
777  env_state, run_sect_opt)
778 
779  call pmc_rand_finalize()
780 
781  end subroutine partmc_sect
782 
783 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
784 
785 end program partmc
pmc_mpi::pmc_mpi_init
subroutine pmc_mpi_init()
Initialize MPI.
Definition: mpi.F90:56
pmc_run_sect::run_sect_opt_t
Options controlling the operation of run_sect().
Definition: run_sect.F90:32
pmc_aero_state::aero_state_zero
subroutine aero_state_zero(aero_state)
Resets an aero_state to have zero particles per bin.
Definition: aero_state.F90:350
pmc_scenario::scenario_contains_aero_mode_type
elemental logical function scenario_contains_aero_mode_type(scenario, aero_mode_type)
Whether any of the contained aerosol modes are of the given type.
Definition: scenario.F90:754
pmc_run_sect
1D sectional simulation.
Definition: run_sect.F90:17
pmc_gas_data::gas_data_t
Constant gas data.
Definition: gas_data.F90:35
pmc_mpi
Wrapper functions for MPI.
Definition: mpi.F90:13
pmc_util::system_clock_time
real(kind=dp) function system_clock_time()
Returns the current system clock time in seconds.
Definition: util.F90:1977
pmc_aero_state::aero_state_add_aero_dist_sample
subroutine aero_state_add_aero_dist_sample(aero_state, aero_data, aero_dist, sample_prop, characteristic_factor, create_time, allow_doubling, allow_halving, n_part_add)
Generates a Poisson sample of an aero_dist, adding to aero_state, with the given sample proportion.
Definition: aero_state.F90:761
pmc_scenario
The scenario_t structure and associated subroutines.
Definition: scenario.F90:9
pmc_scenario::scenario_t
Scenario data.
Definition: scenario.F90:54
pmc_gas_data
The gas_data_t structure and associated subroutines.
Definition: gas_data.F90:9
pmc_run_part::run_part
subroutine run_part(scenario, env_state, aero_data, aero_state, gas_data, gas_state, run_part_opt)
Do a particle-resolved Monte Carlo simulation.
Definition: run_part.F90:127
partmc
program partmc
Top level driver.
Definition: partmc.F90:215
pmc_aero_state::aero_state_set_weight
subroutine aero_state_set_weight(aero_state, aero_data, weight_type, exponent)
Sets the weighting functions for an aero_state.
Definition: aero_state.F90:186
pmc_util::die_msg
subroutine die_msg(code, error_msg)
Error immediately.
Definition: util.F90:135
pmc_mpi::pmc_mpi_rank
integer function pmc_mpi_rank()
Returns the rank of the current process.
Definition: mpi.F90:117
pmc_aero_state::aero_state_set_n_part_ideal
subroutine aero_state_set_n_part_ideal(aero_state, n_part)
Set the ideal number of particles to the given value. The aero_state%awa must be already set correctl...
Definition: aero_state.F90:242
pmc_constants::dp
integer, parameter dp
Kind of a double precision real number.
Definition: constants.F90:12
pmc_coag_kernel
Generic coagulation kernel.
Definition: coag_kernel.F90:9
pmc_condense
Water condensation onto aerosol particles.
Definition: condense.F90:29
pmc_run_part::run_part_opt_t
Options controlling the execution of run_part().
Definition: run_part.F90:49
pmc_spec_file
Reading formatted text input.
Definition: spec_file.F90:43
pmc_condense::condense_equilib_particles
subroutine condense_equilib_particles(env_state, aero_data, aero_state)
Call condense_equilib_particle() on each particle in the aerosol to ensure that every particle has it...
Definition: condense.F90:747
pmc_env_state::env_state_t
Current environment state.
Definition: env_state.F90:29
pmc_aero_state
The aero_state_t structure and assocated subroutines.
Definition: aero_state.F90:9
pmc_aero_state::aero_state_weight_flat
integer, parameter aero_state_weight_flat
Single flat weighting scheme.
Definition: aero_state.F90:41
pmc_spec_file::spec_file_t
An input file with extra data for printing messages.
Definition: spec_file.F90:59
pmc_rand::pmc_srand
subroutine pmc_srand(seed, offset)
Initializes the random number generator to the state defined by the given seed plus offset....
Definition: rand.F90:67
pmc_gas_state
The gas_state_t structure and associated subroutines.
Definition: gas_state.F90:9
pmc_mpi::pmc_mpi_finalize
subroutine pmc_mpi_finalize()
Shut down MPI.
Definition: mpi.F90:89
pmc_util::warn_msg
subroutine warn_msg(code, warning_msg, already_warned)
Prints a warning message.
Definition: util.F90:38
pmc_run_sect::spec_file_read_run_sect
subroutine spec_file_read_run_sect(file, run_sect_opt, aero_data, bin_grid, gas_data, env_state, aero_dist_init, scenario)
Read the specification for a run_sect simulation from a spec file.
Definition: run_sect.F90:205
pmc_aero_dist
The aero_dist_t structure and associated subroutines.
Definition: aero_dist.F90:18
pmc_run_part::pmc_mpi_broadcast_run_part
subroutine pmc_mpi_broadcast_run_part(run_part_opt, aero_data, aero_state_init, gas_data, gas_state_init, env_state_init, aero_dist_init, scenario, n_part, rand_init, do_init_equilibrate, do_restart)
Read the specification for a run_part simulation from a spec file.
Definition: run_part.F90:1060
print_usage
subroutine print_usage()
Print the usage text to stderr.
Definition: partmc.F90:267
pmc_spec_file::spec_file_open
subroutine spec_file_open(filename, file)
Open a spec file for reading.
Definition: spec_file.F90:112
pmc_photolysis
The photolysis_t type and related functions.
Definition: photolysis.F90:9
pmc_run_part
Monte Carlo simulation.
Definition: run_part.F90:9
pmc_env_state
The env_state_t structure and associated subroutines.
Definition: env_state.F90:9
pmc_run_exact::run_exact
subroutine run_exact(bin_grid, scenario, env_state, aero_data, aero_dist_init, gas_data, run_exact_opt)
Run an exact simulation.
Definition: run_exact.F90:46
pmc_gas_state::gas_state_t
Current state of the gas mixing ratios in the system.
Definition: gas_state.F90:33
pmc_rand::uuid4_str
subroutine uuid4_str(uuid)
Generate a version 4 UUID as a string.
Definition: rand.F90:661
pmc_aero_data::aero_data_t
Aerosol material properties and associated data.
Definition: aero_data.F90:55
pmc_run_exact::run_exact_opt_t
Options controlling the execution of run_exact().
Definition: run_exact.F90:24
pmc_aero_dist::aero_dist_t
A complete aerosol distribution, consisting of several modes.
Definition: aero_dist.F90:33
pmc_run_part::spec_file_read_run_part
subroutine spec_file_read_run_part(file, run_part_opt, aero_data, aero_state_init, gas_data, gas_state_init, env_state_init, aero_dist_init, scenario, n_part, rand_init, do_init_equilibrate, do_restart)
Read the specification for a run_part simulation from a spec file.
Definition: run_part.F90:461
pmc_util
Common utility subroutines.
Definition: util.F90:9
pmc_run_exact::spec_file_read_run_exact
subroutine spec_file_read_run_exact(file, run_exact_opt, aero_data, bin_grid, gas_data, env_state, aero_dist_init, scenario)
Read the specification for a run_exact simulation from a spec file.
Definition: run_exact.F90:92
pmc_scenario::scenario_init_env_state
subroutine scenario_init_env_state(scenario, env_state, time)
Initialize the time-dependent contents of the environment. Thereafter scenario_update_env_state() sho...
Definition: scenario.F90:111
pmc_aero_binned
The aero_binned_t structure and associated subroutines.
Definition: aero_binned.F90:9
pmc_spec_file::spec_file_read_string
subroutine spec_file_read_string(file, name, var)
Read a string from a spec file that must have a given name.
Definition: spec_file.F90:605
pmc_bin_grid
The bin_grid_t structure and associated subroutines.
Definition: bin_grid.F90:9
pmc_aero_data
The aero_data_t structure and associated subroutines.
Definition: aero_data.F90:9
pmc_run_sect::run_sect
subroutine run_sect(bin_grid, gas_data, aero_data, aero_dist, scenario, env_state, run_sect_opt)
Run a sectional simulation.
Definition: run_sect.F90:58
pmc_bin_grid::bin_grid_t
1D grid, either logarithmic or linear.
Definition: bin_grid.F90:33
partmc_run
subroutine partmc_run(spec_name)
Do a PartMC run.
Definition: partmc.F90:276
pmc_rand::pmc_rand_finalize
subroutine pmc_rand_finalize()
Cleanup the random number generator.
Definition: rand.F90:119
pmc_aero_dist::aero_dist_contains_aero_mode_type
elemental logical function aero_dist_contains_aero_mode_type(aero_dist, aero_mode_type)
Whether any of the modes are of the given type.
Definition: aero_dist.F90:155
pmc_mpi::pmc_mpi_bcast_string
subroutine pmc_mpi_bcast_string(val)
Broadcast the given value from process 0 to all other processes.
Definition: mpi.F90:307
pmc_aero_state::aero_state_t
The current collection of aerosol particles.
Definition: aero_state.F90:69
pmc_run_exact
Exact solution simulation.
Definition: run_exact.F90:9