# Analysis workflow The `Sample` class coordinates post-processing of a trajectory. You can configure multiple of samplers to analyse different properties at once. The easiest way to analyse a ReaxFF simulation is to run the `ana.py` script generated by `Simulate.generate()`. All you have to do is fill in the atoms, bonds, rdf pairs you want. ## Creating a `Sample` instance To start an analysis, create a `Sample` instance to read the trajectory and bond files. ```python from porereax import Sample atom_lib = {"Si": 1, "O": 2, "H": 3} masses = {"Si": 28.086, "O": 15.9994, "H": 2.016} sampler = Sample( atom_lib=atom_lib, masses=masses, trajectory_file="run_0.lammpstrj", bond_file="run_0.bonds", ) ``` - `atom_lib` maps every atom type name that appears in the trajectory to a numeric LAMMPS type ID. The defined names are later used to write down the bonding environments. - `masses` maps the same atom names to their mass; its keys must match `atom_lib` exactly. - `trajectory_file` is the `.lammpstrj` dump, and `bond_file` the matching ReaxFF `.bonds` file (both written by the LAMMPS). - `system` (optional) points to a `pore.yml` describing a pore, required for the `"Pore"`/`"Reservoir"`/`"Wall"` [regions](regions.md) and for `"Pore1D"`/`"Pore2D"` density sampling. - `start_end_nthframe=(start, end, nth)` (optional, default `(0, -1, 1)`) restricts sampling to a frame range, `end=-1` meaning the last frame, and samples every `nth` frame. ```{note} Do not `import ovito` yourself before creating a `Sample` instance. If your platform only supports the `fork` multiprocessing start method (see [Execution](#execution) below), an already-imported `ovito` module in the main process will raise an error when `sample()` tries to start subprocesses. ``` ## Registering samplers Calling any `add_*_sampling(...)` method adds the sampler to the list of samplers to run. - [`add_molecule_structure_sampling`](../sampler/molecule_structure_sampling.md) - lists all existing bonding environments per atom type to a defined depth. - [`add_charge_sampling`](../sampler/charge_sampling.md) - charge histograms. - [`add_angle_sampling`](../sampler/angle_sampling.md) - bond-angle histograms. - [`add_density_sampling`](../sampler/density_sampling.md) - atom density in space or time. - [`add_bond_length_sampling`](../sampler/bond_length_sampling.md) - bond length / bond order histograms. - [`add_rdf_sampling`](../sampler/rdf_sampling.md) - radial distribution functions. - [`add_reaction_sampling`](../sampler/reaction_sampling.md) - bond formation/breaking events in space or time. Each method takes a `name_out` (used for the output file, see [Output files](#output-files) below) and, depending on the sampler, one or more `atoms=[...]` / `bonds=[...]` / `pairs=[...]` / `reactions=[...]` entries describing the [bonding environments](bonding_environment.md) to sample (except for `molecule_structure_sampling`, which searches and counts all bonding environments). Optionaly a [`region`](regions.md) can be specified to restrict the sampling to a certain area of the box. ```python sampler.add_molecule_structure_sampling(name_out="molecule_structure") sampler.add_charge_sampling( name_out="charge", atoms=[{"atom": "O", "bonds": ["H", "H"]}] ) sampler.add_rdf_sampling( name_out="rdf", pairs=[({"atom": "O", "bonds": ["Si", "H"]}, {"atom": "O", "bonds": ["H", "H"]})], region="Pore", ) ``` (execution)= ## Execution ```python sampler.sample(is_parallel=True, num_cores=8) ``` `sample()` runs the analysis and samples with each registered sampler. The analysis can be run in parallel with `is_parallel=True` or serially with only one process. ```python sampler.sample(is_parallel=False) ``` ```{note} Parallel execution uses `multiprocessing`, preferring the `forkserver` or `spawn`, and only falling back to `fork` if neither is available. `fork` brings a risk of deadlocks if the `ovito` module is already imported or any pre defined object is not picklable. ``` (output-files)= ## Output files Each registered sampler writes a single `.obj` file once `sample()` finishes, e.g. the example above produces `molecule_structure.obj`, `charge.obj`, and `rdf.obj`. These are pickled dictionaries keyed by the [identifiers](bonding_environment.md) of the atoms/bonds/pairs/reactions you registered, plus an `input_params` entry recording how the sampler was configured. See [Visualisation](visualization.md) for how to load and plot them. ## Progress output `Sample` reports progress through the standard `logging` module (logger `porereax.sample`). Enable it with `logging.basicConfig(level=logging.INFO)`; the generated `ana.py` already does this.