Python API¶
Every public function and class of the Python package mallard
(tools/mallard), from their docstrings. How to install it and what it is for:
Python.
import mallard
f = mallard.open("solut/flow.vtkhdf") # mallard.File
f.read("P"), f.read_all("P"), f.geometry()
mallard.series("solut"), mallard.run_record("."), mallard.run_files(".")
mallard.info(path), mallard.diff(a, b), mallard.post.extract(files, ...), mallard.post.convergence(runs, ...)
mallard.vtu, mallard.vtkhdf, mallard.h5 and mallard.tables are the
readers the example, documentation and setup scripts in tools/ still use (Tools);
they need h5py for HDF5 files, and are not part of this reference.
Reading files¶
mallard.open(path, dim=None)¶
Opens any file Mallard writes or reads and returns a File: snapshots (.vtkhdf, .vtu, .pvtu,
.h5 and the series indexes .vtkhdf.series, .pvd, .xmf), restart files (compressed too), probe
files (.csv, .h5, .vtkhdf, .pvd of .vtp), CSV tables (forces, integrals, probe reductions,
spectra) and mesh files (.msh, .cas, .cgns, Mallard HDF5 meshes, OpenFOAM cases). Output files are
read whatever the dimension of the library; a mesh file is read by the library of its dimension: dim, or
each library found in turn. Raises RuntimeError with the reader's message (a missing file, a file
Mallard did not write, a format version newer than the library reads).
mallard.File¶
An open file: what it holds, and its values. mallard.open returns one.
Its attributes are those of mallard info FILE --format json:
path,format("VTKHDF","VTU","XDMF series index (XDMF output snapshots)","restart", ...),version(the Mallard format version, orNone),dim(2, 3 orNone),precision("double","single"orNone),compressed.kind:"mesh","snapshot","restart","probe"or"table".fields:{name: Field}, in the file's order;species: the species ofY_,X_andRHOY_fields.snapshots:[Snapshot], in time order;times: their times as an array.mesh: the mesh it holds or refers to:cells,points,cell_types,bounds,boundary_zones,cell_zones, andfile(the mesh file of XDMF output).global_order: whether cell values come in the global cell order of the run's mesh (restart files, and XDMF and VTKHDF output of the whole mesh), so that two such files compare cell by cell.run: the run block (the code, build, resolved input, mesh, start andrun_idof the run that wrote it), as a dict;Noneif it has none.notes: what a reader of the file should know (no units, written before a format change, ...).contents: all of the above as the JSON dictmallard infoprints.
A file is also a context manager: with mallard.open(path) as f: closes it at the end.
file.times¶
The time of each snapshot, in s, as an array.
file.read(field, snapshot=-1)¶
A field's values at a snapshot, as stored: (n,), or (n, components) for vectors, in the order
of geometry() (cells, points, or a table's rows of that snapshot). snapshot is a position in
snapshots, negative from the end (default: the last). Values come as float64 whatever precision
wrote them; restart files hold the conservative variables (RHO, RHOU_X, ..., RHOE, RHOY_<s>).
file.read_all(field)¶
A field at every snapshot: (n_snapshots, n) or (n_snapshots, n, components); for a table, its
whole column (n_rows,). One read for tables and probe HDF5 files.
file.geometry(snapshot=-1)¶
The Geometry of a snapshot (default the last): the cells of snapshots and mesh files, the points
and cells of probe files. Restart files and tables hold none: the error says where to look.
file[field]¶
f["RHO"] is f.read("RHO"): the last snapshot.
file.close()¶
Releases the file; del and the end of a with block do too.
mallard.Field¶
A field of a file, as the file records it.
name:"RHO","U","MEAN_P", a table's column ("F_X_PRESSURE").components: 1, or 3 for vectors (z is zero in 2D).units: UDUNITS units in SI base units ("kg m-3","1"for dimensionless and for every quantity of a nondimensional case);""where the file records none.long_name:"density","velocity", ...location:"cell","point"or"column"(tables).
mallard.Snapshot¶
One time of a file: a snapshot of a series, a sample of a probe file, a block of a table's rows.
t: the simulated time, in s.step: the step, orNonewhere the file records none (files written before Mallard 0.10).file: the file holding it: the file itself, or a member of a series index.index: its index in that file (series files, probe files, tables).run_id: the run that wrote it, orNoneif the file does not say.block: its snapshot block, as a dict (reason,written_at,fieldswith their stored precision,statisticsof averaged fields, ...), orNone(files written before Mallard 0.10).
mallard.Geometry¶
The points and cells a file's values are on, in the file's order (the order of File.read).
points:(n_points, 3)coordinates, in m (z is zero in 2D).offsets:(n_cells + 1,): celliis the pointsconnectivity[offsets[i]:offsets[i + 1]].connectivity: point indices of every cell, one cell after the other.types: the VTK cell type of each cell (5 triangle, 9 quadrilateral, 10 tetrahedron, 12 hexahedron, 13 prism, 14 pyramid, 3 line, 4 polyline, 7 polygon, 1 vertex); probe HDF5 files hold sample points and no cells.
geometry.n_cells¶
The number of cells.
geometry.vertex_averages()¶
The average of each cell's points, (n_cells, 3): the centroid of a triangle, tetrahedron or
parallelogram, close to it for other cells; for plotting cell values at points.
geometry.triangles()¶
A triangle fan of each 2D cell, of its outline for quadratic cells (corners and edge points in turn):
(triangles, cell), the (n_triangles, 3) point indices and the cell of each triangle, for
matplotlib's tripcolor(points[:, 0], points[:, 1], triangles, facecolors=values[cell]). Raises
ValueError for 3D cells.
mallard.series(path, pattern='*')¶
The snapshots of a series, as (File, index) pairs in time order (then step): of a series index or
snapshot file, or of every series index and snapshot file in a directory (not its subdirectories) whose
name matches pattern, as run_files lists them, but files still being written and high-order views
(PREFIX_high_order..., unless pattern names them). file.read(field, index) reads one.
mallard.run_files(directory)¶
The output files of a run directory, sorted: its series indexes and the snapshots no index lists,
restart files, probe and CSV files, one level of subdirectories (solut/) included; members of an index
are listed through it.
mallard.run_record(path)¶
The records of a run record ([output] run_log): a run directory's mallard.jsonl, or the file
itself. A list of dicts in the order written, each with its type: start (the run block, placement,
setup times), progress, output (a snapshot written: its files with their sizes and SHA-256), event,
divergence, warning, error and end (how the run ended), as the
run record describes them.
Commands¶
mallard.info(path, **options)¶
mallard info PATH: what a file holds (as mallard.open(path).contents, with each field's min and
max at the last snapshot), or a run directory's files and its start and end.
mallard.diff(a, b, **options)¶
mallard diff A B: norms of the differences between two files, field by field, with the largest ones
and where they are; remap=True compares files on different meshes. Options: fields, tol, step, time,
snapshot, remap, mesh_a, mesh_b (mallard diff --help). Differences beyond tolerance are a
result, not an error: the report's result and exit_status give the verdict.
mallard.report(run_dir)¶
mallard info RUN_DIR --html: writes the run report, RUN_DIR/report.html, from RUN_DIR/mallard.jsonl
(for a run still going, killed, or of a Mallard before 0.10; a run writes its own). Returns
{"directory": ..., "report": path}. Raises CommandError when the directory has no mallard.jsonl.
mallard.pack(path, verify=False, **options)¶
mallard pack RUN_DIR: one archive that reproduces the run's last run, with a manifest of hashes. Options:
output (the archive; default the directory's name with .tar.gz, in the current directory, or cwd),
with_outputs ("small", "all", "none"). Returns the report: the archive, the rerun command and its
directory in the archive, notes, and each file with its role, size and SHA-256. A file changed since the
run read or wrote it raises CommandError.
verify=True is mallard pack --verify ARCHIVE: path is an archive, and each file is checked against its
manifest and the run's own hashes. Problems are a result, not an error: the report's result ("ok" or
"differs"), exit_status and problems (each path and problem) give the verdict.
mallard.command(*args, nprocs=None, mpirun='mpirun', threads=None, cwd=None, verdicts=(), **options)¶
Runs mallard ARGS... OPTIONS --format json and returns its JSON output, parsed.
Keyword arguments are the command's options, as mallard COMMAND --help lists them: fields=["RHO", "P"]
is --fields RHO,P, latest=True is --latest, mesh_a="a.msh" is --mesh-a a.msh, input="case.toml"
is -i case.toml; None and False leave an option out. nprocs runs the command on that many MPI ranks
with mpirun -n (mpirun names another launcher), threads sets --kokkos-num-threads, and cwd is the
directory it runs in (relative paths in a file's run block, such as its mesh file, resolve from there).
The executable is MALLARD_EXECUTABLE, else mallard on PATH. Raises CommandError when the command
exits with an error; an exit status in verdicts is a result instead (mallard diff's 1: a difference
beyond tolerance), returned with the JSON the command printed.
mallard.CommandError¶
A command that exited with an error (exit status 1: an error; 2: a usage error, such as an unknown option).
returncode: the exit status.argv: the command line run.stdout,stderr: what it printed.result: the JSON it printed, parsed, orNone.
mallard.post¶
mallard.post.extract(files, **options)¶
mallard post extract FILE...: probes, region integrals and snapshots of existing output, as the run
would have written them. files is a path or a list of them (restart files, snapshots of the whole mesh,
series). Options: input, probe, region, name, fields, reductions, sampling, snapshot,
steps, times, every, latest, output (mallard post extract --help). Returns the report: the
files written (under output, default extract/, for mallard.open), their times and fields, and the
reductions of the last snapshot.
mallard.post.derive(files, fields, **options)¶
mallard post derive FILE... --fields LIST: snapshots of derived fields (MACH, P0, Q_CRITERION, ...)
of existing output, as the run would have written them. files is a path or a list of them (restart files,
snapshots, series); fields the fields to derive, as mallard list fields names them. Options: input,
reference, mixture, with_state, region, snapshot, name, steps, times, every, latest,
output (mallard post derive --help). Returns the report: the snapshots derived and the files written
(under output, default derived/, for mallard.open), with their fields and units.
mallard.post.average(files, **options)¶
mallard post average FILE...: means MEAN_<a>, RMS_<a> and covariances COV_<a>_<b> over saved
snapshots, as [statistics] keeps them. files is a path or a list of them. Options: fields, products,
favre, t_start, homogeneous, conditional, statistics, rtol, region, reference, mixture,
input, snapshot, name, steps, times, every, latest, output (mallard post average --help);
conditional, region, reference and mixture take inline TOML tables as strings. Returns the report:
the averaging window, each snapshot with its weight, the fields and covariances, and the paths written
(under output, default average/).
mallard.post.convergence(runs, **options)¶
mallard post convergence RUN_DIR...: for each quantity of the runs' forces, integrals and probe tables,
the observed order of accuracy, the value extrapolated to zero cell size and the grid convergence index.
Options: quantity, time, average, order, safety_factor, sizes, field, exact
(mallard post convergence --help). A quantity that diverges is a result, not an error (exit status 1
with a report): each quantity's verdict says so.