Beispiel #1
0
def load_equilibria_dict(chems_path, equilibria_path):
    '''Expected format of the equilibrium data file:
    on each row:
    - name of the equilibrium
    - list of the names of chemical species in their different form (space
      separated)
    - list of the pK (space separated)
    All those fields are separated by ":"
    acetate: Ac Ac(-): 4.78
    It is also possible to insert comments by beginning the line by "#"
    '''
    chems_dict = load_chems_dict(chems_path)
    equilibria_dict = dict()
    with open(equilibria_path, "r") as fh:
        for line in fh:
            if not line.startswith("#"):
                name, reagents, pKs_str = [
                    field.lstrip().rstrip() for field in line.split(":")
                ]
                chems = [
                    chems_dict[reagent] for reagent in reagents.split(" ")
                ]
                pKs = [float(pK) for pK in pKs_str.split(" ")]
                equilibria_dict[name] = Equilibrium(chems, pKs)
    return equilibria_dict
Beispiel #2
0
def outline_systems_chemistry(input_file):
    '''This function does a lot of different things. Basically, it looks at all
    the chemical reactions and equilibria that are supposed to happen in the
    system, and it decides which chemical specie shall be present in the
    concentration vector of the simulation, and in which order.
    Solving this problem implies to update the chems dictionary (because new chems are created
    to represent populations' biomass)
    '''
    # see if chems or reaction dictionary is overriden
    chems_description_path = input_file.get("chems_description_path",
                                            default_chems_description_path)
    dHf0_description_path = input_file.get("dHf0_description_path",
                                           default_dHf0_description_path)
    reactions_description_path = input_file.get(
        "reactions_description_path", default_reactions_description_path)
    equilibria_description_path = input_file.get(
        "equilibria_description_path", default_equilibria_description_path)
    glt_description_path = input_file.get("glt_description_path",
                                          default_glt_description_path)

    # load the chems and reactions dict
    chems_dict = load_chems_dict(chems_description_path, dHf0_description_path)
    reactions_dict = load_reactions_dict(
        chems_description_path,
        reactions_description_path,
        dHf0_path=default_dHf0_description_path)
    equilibria_dict = load_equilibria_dict(chems_description_path,
                                           equilibria_description_path)
    glt_dict = load_glt_dict(chems_description_path, glt_description_path)

    # look for concentration settings, catabolisms and equilibria to find which
    # chemical species should be simulated
    concentration_settings = input_file.get("concentrations", {})
    equilibria = [
        equilibria_dict[equilibrium_name]
        for equilibrium_name in (input_file.get("equilibria") or [])
    ]

    gl_info = input_file.get("gas-liquid interface")
    if gl_info:
        glts = [glt_dict[glt] for glt in gl_info.get("transfers", [])]
    else:
        glts = []

    # look at the pathways for catabolic reagents, without instanciating the populations
    reactions = get_catabolic_reactions(input_file, chems_dict, reactions_dict)
    # instanciate a preliminary chems list, without the growth-model-specific variables
    preliminary_chems_list, nesting = inventory_chems(chems_dict, reactions,
                                                      equilibria, glts,
                                                      concentration_settings)
    # instanciate the populations and add their specific variables to the chems list
    chems_list, nesting, chems_dict, populations = register_biomass(
        input_file, chems_dict, reactions_dict, preliminary_chems_list,
        nesting)

    return chems_list, nesting, equilibria, chems_dict, glt_dict, populations
Beispiel #3
0
def load_reactions_dict(chems_path, reactions_path, dHf0_path=None):
    chems_dict = load_chems_dict(chems_path, dHf0_data_path=dHf0_path)
    with open(reactions_path, "r") as reactions_fh:
        reactions_dict = dict()
        for line in reactions_fh:
            reaction_dict = dict()
            reaction_name, reaction_string = line.rstrip().split(": ")
            reaction = Reaction.from_string(chems_dict, reaction_string, name=reaction_name)
            reaction.check_balance()
            reactions_dict[reaction_name] = reaction
    return reactions_dict
Beispiel #4
0
def load_glt_dict(chems_data_path, glt_data_path):
    chems_dict = load_chems_dict(chems_data_path)
    with open(glt_data_path, "r") as stream:
        glt_dict = dict()
        next(stream)  # skip header
        for line in stream:
            if not line.startswith("#"):
                name, liquid, gas, H0cp, Hsol = line.rstrip().split(",")
                glt = GasLiquidTransfer(chems_dict[gas], chems_dict[liquid],
                                        float(H0cp), float(Hsol))
                glt_dict[name] = glt
    return glt_dict
Beispiel #5
0
determination up, leading either to wrong pH or impossibility to determine pH.
"""

from micodymora.Chem import load_chems_dict
from micodymora.Reaction import Reaction
from micodymora.Constants import T0

import os
import numpy as np
from scipy.optimize import brentq

# tolerance for Brent's method when determining [H+]
H_tolerance = 1e-12
chems_dict_path = os.path.join(os.path.dirname(os.path.realpath(__file__)),
                               "data", "chems.csv")
chems_dict = load_chems_dict(
    chems_dict_path)  # only needed by WaterEquilibrium


class Equilibrium:
    def __init__(self, chems, pK):
        self.chems = chems
        self.pK = [0] + pK
        # product of equilibrium constants from the first specie of the network
        # to each chem of the network
        self.Kprod = [10**-sum(self.pK[:i + 1]) for i in range(len(self.pK))]
        # difference of charge with first species of the network
        self.charges = [chem.composition["charge"] for chem in self.chems]
        self.dgamma = [charge - self.charges[0] for charge in self.charges]

    def equilibrate(self, total, H):
        '''Computes the concentration of each chems according to proton