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

import copy


[docs] def perturb_model(self, nsamples, transf_mapper): """Extend the CHIMERE chemistry scheme to accommodate ensemble members. Creates ``nsamples`` copies of each active species (``acspecies``), output species (``outspecies``), emitted species (``emis_species``), and biogenic species (``bio_species``) in the chemistry plugin, then removes the original un-suffixed species. The copies are named with a numeric suffix (e.g. ``CH4000``, ``CH4001``, …). Also records a ``perturbed_species`` mapping from sample name back to original name. .. note:: Reactions in the chemical scheme are not yet supported with perturbations; an exception is raised if ``self.reactions`` is set. Args: self (Plugin): CHIMERE model plugin instance. nsamples (int): number of ensemble members. transf_mapper (dict): transform mapper (unused; kept for API consistency with other model plugins). Raises: Exception: if the chemical scheme contains reactions. """ if hasattr(self, "reactions"): raise Exception("There are reactions in your chemical scheme. " "This is not yet implemented!!") # Perturb active species in the chemical scheme list_acspecies = copy.deepcopy(self.chemistry.acspecies.attributes[:]) self.perturbed_species = {} for spec in list_acspecies: for i in range(nsamples): spec_sample = f"{spec}{i:03d}" spec_plg = getattr(self.chemistry.acspecies, spec) setattr(self.chemistry.acspecies, spec_sample, 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) # Perturb output species in the chemical scheme list_outspecies = copy.deepcopy(self.chemistry.outspecies.attributes[:]) for spec in list_outspecies: for i in range(nsamples): spec_sample = f"{spec}{i:03d}" spec_plg = getattr(self.chemistry.outspecies, spec) setattr(self.chemistry.outspecies, spec_sample, spec_plg) self.chemistry.outspecies.attributes.append(spec_sample) delattr(self.chemistry.outspecies, spec) self.chemistry.outspecies.attributes.remove(spec) # Perturb emis species in the chemical scheme list_emispecies = copy.deepcopy(self.chemistry.emis_species.attributes[:]) for spec in list_emispecies: for i in range(nsamples): spec_sample = f"{spec}{i:03d}" spec_plg = getattr(self.chemistry.emis_species, spec) setattr(self.chemistry.emis_species, spec_sample, spec_plg) self.chemistry.emis_species.attributes.append(spec_sample) delattr(self.chemistry.emis_species, spec) self.chemistry.emis_species.attributes.remove(spec) # Perturb bio species in the chemical scheme list_biospecies = copy.deepcopy(self.chemistry.bio_species.attributes[:]) for spec in list_biospecies: for i in range(nsamples): spec_sample = f"{spec}{i:03d}" spec_plg = getattr(self.chemistry.bio_species, spec) setattr(self.chemistry.bio_species, spec_sample, spec_plg) self.chemistry.bio_species.attributes.append(spec_sample) delattr(self.chemistry.bio_species, spec) self.chemistry.bio_species.attributes.remove(spec) # Dump updated chemical scheme as text files for CHIMERE executable self.chemistry.create_chemicalscheme()