Skip to content

Commit

Permalink
all formating in formatting file
Browse files Browse the repository at this point in the history
  • Loading branch information
Nicholaswogan committed Oct 20, 2024
1 parent 106d612 commit 3824396
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 91 deletions.
44 changes: 1 addition & 43 deletions photochem/equilibrate.py
Original file line number Diff line number Diff line change
@@ -1,45 +1,3 @@
from ._equilibrate import ChemEquiAnalysis, EquilibrateException
from ._equilibrate import __version__
from .utils._format import FormatReactions_main, yaml, Loader, MyDumper, mechanism_dict_with_atoms
from photochem_clima_data import DATA_DIR

def generate_zahnle_earth_thermo(outfile='zahnle_earth_thermo.yaml', atoms_names=None, exclude_species=[]):
"""Generates a thermodynamic file for equilibrium solving that includes
condensible species (e.g., H2O condensate).
Parameters
----------
outfile : str, optional
Name of the output file, by default 'zahnle_earth_thermo.yaml'
atoms_names : list, optional
List of atoms to keep. By default all atoms in the mechanism are kept
exclude_species : list, optional
List of species to exclude.
"""

rx_folder = DATA_DIR+'/reaction_mechanisms/'

with open(rx_folder+'zahnle_earth.yaml','r') as f:
dat = yaml.load(f, Loader=Loader)

with open(rx_folder+'condensate_thermo.yaml','r') as f:
dat1 = yaml.load(f, Loader=Loader)

# Delete information that is not needed
for i,atom in enumerate(dat['atoms']):
del dat['atoms'][i]['redox']
del dat['particles']
del dat['reactions']

for i,sp in enumerate(dat1['species']):
dat['species'].append(sp)

if atoms_names is None:
atoms_names = [a['name'] for a in dat['atoms']]

dat = mechanism_dict_with_atoms(dat, atoms_names, exclude_species)

dat = FormatReactions_main(dat)

with open(outfile, 'w') as f:
yaml.dump(dat,f,Dumper=MyDumper,sort_keys=False,width=70)
from .utils import generate_zahnle_earth_thermo
46 changes: 2 additions & 44 deletions photochem/extensions/gasgiants.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@
import copy

from .._photochem import EvoAtmosphere, PhotoException
from .. import zahnle_earth
from .. import equilibrate
from ..utils._format import yaml, FormatSettings_main, MyDumper, FormatReactions_main, mechanism_dict_with_atoms
from ..utils._format import yaml, FormatSettings_main, MyDumper

###
### Extension of EvoAtmosphere class for gas giants
Expand Down Expand Up @@ -956,45 +955,4 @@ def composition_at_metallicity(gas, T, P, CtoO, metal, rainout_condensed_atoms =
- name: H2
lower-boundary: {type: Moses}
upper-boundary: {type: veff, veff: 0}
"""

###
### Generates reactions and thermo files for gasgaints
###

def generate_photochem_rx_and_thermo_files(
atoms_names=['H','He','N','O','C','S'],
rxns_filename='photochem_rxns.yaml',
thermo_filename='photochem_thermo.yaml',
exclude_species=[],
remove_particles=False,
remove_reaction_particles=True
):
"""Generates input reactions and thermodynamic files for photochem.
Parameters
----------
atoms_names : list, optional
Atoms to include in the thermodynamics, by default ['H','He','N','O','C','S']
rxns_filename : str, optional
Name of output reactions file, by default 'photochem_rxns.yaml'
thermo_filename : str, optional
Name of output thermodynamic file, by default 'photochem_thermo.yaml'
exclude_species : list, optional
List of species to exclude.
remove_particles : bool, optional
If True, then particles will be removed, by default False.
remove_reaction_particles : bool, optional
If True, then reactions particles are removed, by default True.
"""
# Kinetics
with open(zahnle_earth,'r') as f:
rxns = yaml.load(f,Loader=yaml.Loader)
rxns = mechanism_dict_with_atoms(rxns, atoms_names, exclude_species, remove_particles, remove_reaction_particles)
rxns = FormatReactions_main(rxns)
with open(rxns_filename,'w') as f:
yaml.dump(rxns,f,Dumper=MyDumper,sort_keys=False,width=70)

# Thermodynamics
equilibrate.generate_zahnle_earth_thermo(thermo_filename, atoms_names, exclude_species)

"""
2 changes: 1 addition & 1 deletion photochem/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Formatting
from ._format import FormatReactions, FormatSettings
from ._format import resave_mechanism_with_atoms
from ._format import resave_mechanism_with_atoms, generate_zahnle_earth_thermo, zahnle_rx_and_thermo_files

# Converting tools
from ._convert_atmos import atmos2yaml, atmosbc2yaml
Expand Down
110 changes: 107 additions & 3 deletions photochem/utils/_format.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import yaml
from photochem_clima_data import DATA_DIR

try:
from yaml import CLoader as Loader, CDumper as Dumper
Expand Down Expand Up @@ -177,6 +178,10 @@ def FormatSettings_main(data):

return data

###
### Several routines for altering reaction files.
###

def species_in_reaction(rx):
rx1 = rx.replace('<=>','=>').replace('(','').replace(')','')
react, prod = [a.replace(' ','').split('+') for a in rx1.split('=>')]
Expand Down Expand Up @@ -268,8 +273,32 @@ def mechanism_dict_with_atoms(dat, atoms_names, exclude_species=[], remove_parti

return out

def resave_mechanism_with_atoms(infile, outfile, atoms_names, exclude_species=[], remove_particles=False, remove_reaction_particles=False):

def resave_mechanism_with_atoms(
infile,
outfile,
atoms_names,
exclude_species=[],
remove_particles=False,
remove_reaction_particles=False
):
"""Alters a reaction mechanism file with altered atoms.
Parameters
----------
infile : str
Path to input yaml file
outfile : str
Path to output yaml file
atoms_names : list
List of atoms to include in the output file
exclude_species : list, optional
List of species to exclude, by default []
remove_particles : bool, optional
If True, then all particles are removed, by default False
remove_reaction_particles : bool, optional
If True, then partcles forming from reactions are removed, by default False
"""

with open(infile,'r') as f:
dat = yaml.load(f,Loader=Loader)

Expand All @@ -285,4 +314,79 @@ def resave_mechanism_with_atoms(infile, outfile, atoms_names, exclude_species=[]
with open(outfile,'w') as f:
yaml.dump(out,f,Dumper=MyDumper,sort_keys=False,width=70)


def generate_zahnle_earth_thermo(outfile='zahnle_earth_thermo.yaml', atoms_names=None, exclude_species=[]):
"""Generates a thermodynamic file for equilibrium solving that includes
condensible species (e.g., H2O condensate).
Parameters
----------
outfile : str, optional
Name of the output file, by default 'zahnle_earth_thermo.yaml'
atoms_names : list, optional
List of atoms to keep. By default all atoms in the mechanism are kept
exclude_species : list, optional
List of species to exclude.
"""

rx_folder = DATA_DIR+'/reaction_mechanisms/'

with open(rx_folder+'zahnle_earth.yaml','r') as f:
dat = yaml.load(f, Loader=Loader)

with open(rx_folder+'condensate_thermo.yaml','r') as f:
dat1 = yaml.load(f, Loader=Loader)

# Delete information that is not needed
for i,atom in enumerate(dat['atoms']):
del dat['atoms'][i]['redox']
del dat['particles']
del dat['reactions']

for i,sp in enumerate(dat1['species']):
dat['species'].append(sp)

if atoms_names is None:
atoms_names = [a['name'] for a in dat['atoms']]

dat = mechanism_dict_with_atoms(dat, atoms_names, exclude_species)

dat = FormatReactions_main(dat)

with open(outfile, 'w') as f:
yaml.dump(dat,f,Dumper=MyDumper,sort_keys=False,width=70)

def zahnle_rx_and_thermo_files(
atoms_names=['H','He','N','O','C','S'],
rxns_filename='photochem_rxns.yaml',
thermo_filename='photochem_thermo.yaml',
exclude_species=[],
remove_particles=False,
remove_reaction_particles=False
):
"""Generates input reactions and thermodynamic files for photochem by altering the main
reaction network (zahnle_earth.yaml).
Parameters
----------
atoms_names : list, optional
Atoms to include in the thermodynamics, by default ['H','He','N','O','C','S']
rxns_filename : str, optional
Name of output reactions file, by default 'photochem_rxns.yaml'
thermo_filename : str, optional
Name of output thermodynamic file, by default 'photochem_thermo.yaml'
exclude_species : list, optional
List of species to exclude.
remove_particles : bool, optional
If True, then particles will be removed, by default False.
remove_reaction_particles : bool, optional
If True, then reactions particles are removed, by default True.
"""

zahnle_earth = DATA_DIR+'/reaction_mechanisms/zahnle_earth.yaml'
# Kinetics
if rxns_filename is not None:
resave_mechanism_with_atoms(zahnle_earth, rxns_filename, atoms_names, exclude_species, remove_particles, remove_reaction_particles)

# Thermodynamics
if thermo_filename is not None:
generate_zahnle_earth_thermo(thermo_filename, atoms_names, exclude_species)

0 comments on commit 3824396

Please sign in to comment.