Source code for pycif.plugins.models.lagrangian.perturb_model

import copy
from ....utils.classes.chemistries import Chemistry


[docs] def perturb_model(self, nsamples, transf_mapper): """Extend the Lagrangian footprint model to accommodate ensemble members. Creates ``nsamples`` copies of each active species (``acspecies``) in the chemistry plugin, using the ``__sample#NNN`` naming convention, then removes the original un-suffixed species. Records a ``perturbed_species`` mapping from sample name back to original name. Args: self: Lagrangian model plugin instance. nsamples (int): number of ensemble members. transf_mapper (dict): unused; kept for API consistency. """ # Perturb active species in the chemical scheme list_acspecies = copy.deepcopy(self.chemistry.acspecies.attributes[:]) self.perturbed_species = {} for ispec, spec in enumerate(list_acspecies): for i in range(nsamples): spec_sample = f"{spec}__sample#{i:03d}" spec_plg = getattr(self.chemistry.acspecies, spec) setattr( self.chemistry.acspecies, spec_sample, Chemistry(plg_orig=spec_plg) ) self.chemistry.acspecies.attributes.append(spec_sample) self.perturbed_species[spec_sample] = spec delattr(self.chemistry.acspecies, spec) self.chemistry.acspecies.attributes.remove(spec)