Ensembles: many AERMOD runs from one design¶
pyaermod.ensemble runs a designed set of AERMOD runs, such as one run
per particle size bin, met set and source configuration, and records them.
You give it the design as a list of rows and a function that builds the
project for one row. It then does four things:
- It runs every row in its own directory, several at a time.
- It names each run by a hash of what defines it.
- It records each run in a manifest as the run finishes.
- It picks up where it stopped when you run the same design again.
collect_plotfiles then reads the PLOTFILEs (AERMOD's per-receptor files)
of the design's finished runs into one table.
from pyaermod.ensemble import collect_plotfiles, run_design
rows = [
{"diameter_um": d, "density": rho}
for d in (2.5, 5.0, 10.0, 20.0)
for rho in (1.0, 2.65)
]
def build(factors):
project = make_base_project() # your own AERMODProject
project.sources.sources[0].particle_deposition = ParticleDepositionParams(
[factors["diameter_um"]], [1.0], [factors["density"]])
project.output.plot_file = "pit.plt"
return project
if __name__ == "__main__": # required on macOS and Windows
result = run_design(rows, build, "study/runs", n_workers=4,
executable="bin/aermod")
print(result.n_run, "runs made,", result.n_skipped, "already done,",
result.n_failed, "failed")
table = result.collect_plotfiles() # also writes plotfiles.csv/.npz
Why one directory per run¶
AERMOD reads its deck from aermod.inp in its working directory and
writes its output files wherever the deck says. This causes two problems
when decks share a directory:
- They overwrite each other's results. Two decks that name the same
PLOTFILE overwrite each other. EPA's own decks name outputs such as
../plotfiles/SURFCOAL_01H.PLT, so decks that share such a path clash even from different directories. - They run one at a time.
AERMODRunnerlocks the working directory for the length of a run, so that two runs do not both use itsaermod.inp. Decks in one directory therefore run one after another, whatevern_workersis.
run_design gives every run the directory root/runs/<run ID>. It
rewrites every output file name in the deck to a bare name inside that
directory (rewrite_output_names), so ../shared/pit.plt becomes
pit.plt. The keywords covered are PLOTFILE, POSTFILE, SUMMFILE,
MAXIFILE, RANKFILE, SEASONHR, EVALFILE, TOXXFILE, MAXDAILY, MXDYBYYR,
MAXDCONT, EVENTFIL, SAVEFILE, MULTYEAR, the SCIMBYHR summary files and
the debug files of DEBUGOPT (ControlPathway.debug_options: every field
that is not an option name is the file of the option before it). A debug
option given without a file name writes AERMOD's default name
(MODEL.DBG, AREA.DBG, ...), and DEPOS writes GDEP.DAT,
PDEP.DAT and DEPOS.DBG; those land in the run's own directory
already.
The same goes for the output lines that input_reader keeps verbatim,
because the project model has no field for them
(AERMODProject.unparsed_lines): a PLOTFILE with a rank below FIRST
(2ND, 8TH) or with a unit number, a second POSTFILE and ERRORFIL,
as well as those of the keywords above. The
field that holds the file name is the one AERMOD's setup routines read
it from (ouset.f, coset.f). These files are listed in the entry's
outputs with the others, so resume and collect_plotfiles see them
too.
It also links every file the deck has AERMOD read into the directory, and the deck names them without a directory:
- the surface and profile met files (
SURFFILE,PROFFILE); INITFILE, theMULTYEARinitial file,OZONEFILandNOX_FILEfiles, whole-domain or by sector;- the
HOUREMISfiles ofSourcePathway.hourly_emissions; - from the lines kept verbatim,
HOUREMISfiles, hourlyBACKGRNDfiles andINCLUDEDfiles.
A relative path is taken relative to the current directory when
run_design is called. Linking has three benefits:
- The deck does not depend on where the input files live.
- The deck stays within AERMOD's 200-character limit on a file name
(
ILEN_FLDinmodules.f). With v26135, a 201-characterSURFFILEpath fails with E291 "Filename specified is too long" and then E500. - The deck text stays the same on every machine.
Where links cannot be made, the files are copied. Two different input
files with the same name, or an input file named like an output, are an
error, since both would be one file in the run directory. An INCLUDED
file is linked as it is: the file names inside it are not rewritten.
Run IDs¶
A run's ID is the SHA-256 of the canonical JSON of five things:
- the row's factors;
- the SHA-256 of the AERMOD binary;
- the SHA-256 of the surface and profile met files;
- the SHA-256 of the other files the deck reads (see above), by role:
the keyword, as in
HOUREMIS, thenHOUREMIS.2for a second, different file; SCHEMA_VERSION, now 2.
The payload looks like this:
{"binary_sha256":"…","factors":{"density":1.0,"diameter_um":2.5},
"input_files_sha256":{"HOUREMIS":"…"},
"met_sha256":{"profile":"…","surface":"…"},"schema_version":2}
canonical_json sorts keys, writes no whitespace, and writes floats in
their shortest round-trip form (so 1.0 and 1 are different factors).
It also writes:
- an enum as its value;
- a dataclass, such as
ParticleDepositionParams, as its fields plus"_type"; - NumPy scalars and arrays as plain numbers and lists.
It refuses NaN, infinities and dict keys that are not strings, so an ID never depends on how Python happened to print something.
The ID does not depend on paths, dates or the machine. What follows from that:
- A new binary, or an edited met or emission file, gives new IDs,
so its runs go to new directories and the old runs are kept. An
emission profile kept in a
HOUREMISfile is therefore part of the run's identity even when no factor names it. - Two rows with the same factors are an error, because they would be the same run.
- A factor is anything that defines the run. Put the met set or the
source configuration in the row, even when
build_fnreads it from elsewhere. - Fixed parts of the deck are not in the ID. A change to
build_fnkeeps a row's ID. Resume notices the change from the deck text (see below).
Each run directory holds factors.json, the payload its ID hashes.
The manifest¶
root/manifest.json is an EnsembleManifest: a
runner_utils.RunManifest whose entries are EnsembleManifestEntry, a
subclass of RunManifestEntry, keyed by run ID. It is written in one
step (a temporary file and a rename), so a killed process never leaves
half a manifest. A plain RunManifest.load still reads it.
Before any run starts, every run of the design gets an entry with status
pending. Each entry is updated, and the file saved, as its run finishes.
manifest.csv, one row per run with the factors as factor.<name>
columns, is written at the end.
| Field | What it holds |
|---|---|
run_id, run_dir, input_file |
The ID, runs/<ID> and runs/<ID>/run.inp, relative to the root |
status |
pending, success or failed, by the runner's rule: AERMOD's *** AERMOD Finishes Successfully *** and no fatal errors |
factors, schema_version |
The row, as canonical JSON would write it |
input_sha256 |
The SHA-256 of the deck as written |
binary, binary_sha256, aermod_version |
The binary's path and SHA-256, and the version in the .out banner (26135) |
met_files, met_sha256 |
The source path and SHA-256 of each met file |
input_files, input_files_sha256 |
The same for the other input files, by role (HOUREMIS, ...) |
outputs |
The output file names by keyword, such as {"PLOTFILE": ["pit.plt"]}. After a successful run, DEBUGOPT lists only the debug files AERMOD wrote (see Resuming) |
git_commit, git_dirty, pyaermod_version |
pyaermod's commit, when it runs from a git checkout, and whether its tracked files differed from it |
runtime_seconds, started, finished |
The run's wall time and local start and end times |
return_code, warning_count, fatal_count, warnings, error_message |
AERMOD's exit code and message summary |
Resuming¶
Run the same design again with the same root. A run is skipped only when all of these hold:
- Its entry says
successfor the same deck text (input_sha256) and the same binary, met and input files (their SHA-256). - The deck on disk is that deck.
- Its
.outhas AERMOD's success banner and AERMOD's copy of that deck at the top. This is the rulerunner_utils.resume_batchapplies. - Its PLOTFILEs and POSTFILEs are still there, and so are the debug
files it wrote. AERMOD does not write every debug file a deck names:
with DEPOS not followed by MODEL,
coset.fsends MODEL's output toDEPOS.DBG(checked on v26135), so after a successful run the entry'soutputs["DEBUGOPT"]keeps only the files that exist.
Every other run is made again. That covers:
- a run that failed;
- a run that was killed, whose entry is still
pending; - a run whose
build_fnnow writes a different deck; - a run whose files were removed.
Before a run is made again, the outputs of the earlier attempt are
deleted, so a run that fails early cannot leave old results behind. Pass
resume=False to make every run again.
So if an ensemble is interrupted, with Ctrl-C, a killed job or a reboot, run the same script again. The runs that finished are skipped and the others are made.
Collecting the PLOTFILEs¶
collect_plotfiles(root) returns a pandas DataFrame with one row per
receptor of each PLOTFILE of each successful run of the latest
run_design call on that root. It reads the files with
aermod_outputs.read_plotfile. result.collect_plotfiles() does the
same for the runs of one DesignResult.
-
Which runs. The manifest keeps every run ever made under the root: those of rows since dropped from the design, and those made with an earlier binary or met file.
run_designwrites the IDs of its own runs toroot/design.json, andcollect_plotfilesreads only those by default. Passrun_ids=to choose others, such asrun_ids=result.manifest.entriesfor every run. A root written beforedesign.jsonexisted gives every successful run. -
Columns. The first three are
run_id,plotfile(the file name) andreceptor(the row's position in its file, from 0). After them come AERMOD's own columns in lower case with underscores:x,y,average_conc,total_depo,dry_depo,wet_depo,zelev,zhill,zflag,ave,grp,num_hrs(PERIOD files),rankanddate_conc(short-term files), andnet_id. - Missing columns. A column that one file lacks is empty in that file's rows.
- Factors. The factors are not repeated in this table. Join on
run_idwithresult.to_dataframe()or the manifest'sto_dataframe(). - Files written. The table is also written as
plotfiles.csvandplotfiles.npzunder the root (out_stem=changes the name, andNonewrites neither). The.npzholds one array per column, text as NumPy unicode arrays, sonumpy.loadreads it withoutallow_pickle. No new dependency is needed.
Parallel runs and their speed-up¶
n_workers runs go at a time, each in a worker process. n_workers=1
runs them one after another in the calling process. On macOS and
Windows the workers are started with spawn, so the calling script must
use the if __name__ == "__main__": guard (see AERMODRunner.run_batch
in Common errors).
DesignResult reports two times:
elapsed_seconds, the wall time of the call;run_seconds, the sum of the runs' own wall times.
Their ratio, concurrency, is how many runs were in progress on
average. It is not the speed-up over running one at a time: runs that
share a machine slow each other down, so each run's own time is longer
than it would be alone. To measure the speed-up, time the same design
with n_workers=1.
On a 12-core Apple M2 Pro (gfortran 15.2 -O2 -fbounds-check build of
AERMOD v26135), a design of four single-bin OPENPIT runs of 60 s each (720
hours of EPA's Cordero Mine met, 8,064 polar receptors) took 240.9 s with
n_workers=1 and 64.6 s with n_workers=4: a speed-up of 3.7, with
concurrency 3.85. Benchmarks has the batch throughput of
pyaermod itself.
BatchRunner.parameter_sweep¶
For a sweep of one field, BatchRunner.parameter_sweep is still
available. It writes every deck into one directory, so its runs do not
overlap (see above). Use run_design for parallel runs.
- Results. It returns a
SweepResults: a read-only mapping from each value, in sweep order, to itsAERMODRunResult. Values need not be hashable, and a lookup compares with==, so a sweep overParticleDepositionParamsworks andresults[psd]finds its run. It is not adict, as the sweep returned before: it cannot be changed or given tojson.dumps.dict(results)gives a dict when the values are hashable. - Deck names. A deck keeps the value's text in its name
(
run_emission_rate_0.5.inp) when the value is a short plain number, string or boolean. Any other value is named by its position and a hash, such asrun_particle_deposition_001_3fa9c0d27e41.inp. - Output names. Each output file the deck names is prefixed with
the deck's stem, so the runs do not overwrite each other's PLOTFILEs,
POSTFILEs or named debug files. As they all land in the sweep's
directory, two outputs with the same file name in different
directories (
annual/result.pltandhourly/result.plt, compared ignoring case) are refused withValueError, as is a new name longer than 200 characters. - Files AERMOD names itself are shared. Some files get their name
from AERMOD, not from the deck, so the sweep cannot rename them. Every
run writes each of them to the same file in the sweep's directory, and
only the last run's copy is kept. They are the file of a debug option
given without a name (
MODEL.DBG,METEOR.DBG,AREA.DBG, ..., with RLINE'sRLINE_GRIDWS.DBGand URBANDB'sURBDBUG1.DBGandURBDBUG2.DBG), theERRORS.LSTof an ERRORFIL given without a name, and the files whose names AERMOD always chooses: DEPOS'sGDEP.DAT,PDEP.DATandDEPOS.DBG, and PVMRM'sRelDisp.dbg. Name each debug file inControlPathway.debug_options, and the ERRORFIL, to keep those per run. For the files AERMOD always names, userun_design, which gives each run its own directory. - Equal values are refused, because they would be the same run.