Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 24 additions & 14 deletions bird/postprocess/post_quantities.py
Original file line number Diff line number Diff line change
Expand Up @@ -932,13 +932,13 @@ def compute_instantaneous_kla(

where:
- :math:`kLa_{\rm spec}` is the mass transfer rate in :math:`h^{-1}`
- :math:`d_{\rm gas}` is the bubble diameter in :math:`m`
- :math:`\alpha_{\rm gas}` is the volume fraction of gas
- :math:`\mu_{\rm liq}` is the liquid viscosity in :math:`kg.m^{-1}.s^{-1}`
- :math:`\rho_{\rm liq}` is the liquid density in :math:`kg.m^{-3}`
- :math:`D_{\rm spec}` is the species molecular diffusivity in :math:`m^2.s^{-1}`
- :math:`|u_{\rm slip}|` is the magnitude of the slip velocity in :math:`m.s^{-1}`
- :math:`V_{\rm liq}` is the volume of liquid in :math:`m^3`
- :math:`d_{\rm gas}` is the bubble diameter in :math:`m`. Either read from the time folder, or looked up from phaseProperties
- :math:`\alpha_{\rm gas}` is the volume fraction of gas. Read from the time folder.
- :math:`\mu_{\rm liq}` is the liquid viscosity in :math:`kg.m^{-1}.s^{-1}`. Either read from the time folder or globalVars.
- :math:`\rho_{\rm liq}` is the liquid density in :math:`kg.m^{-3}`. Either read from the time folder or assumed to be 1000kg/m3
- :math:`D_{\rm spec}` is the species molecular diffusivity in :math:`m^2.s^{-1}`. Read from globalVars
- :math:`|u_{\rm slip}|` is the magnitude of the slip velocity in :math:`m.s^{-1}`. Read from the time folder.
- :math:`V_{\rm liq}` is the volume of liquid in :math:`m^3`. Read from the time folder.

.. math::

Expand All @@ -952,10 +952,10 @@ def compute_instantaneous_kla(

and
- :math:`C^{*}_{\rm spec}` is the saturation concentration of species spec in :math:`mol.m^{-3}`
- :math:`\rho_{\rm gas}` is the density of the gas in :math:`kg.m^{-3}`
- :math:`Y_{\rm spec, gas}` is the mass fraction of species spec in the gas phase
- :math:`He_{\rm spec}` is the Henry's constant of species spec
- :math:`W_{\rm spec}` is the molar mass of species spec in :math:`kg.mol^{-1}`
- :math:`\rho_{\rm gas}` is the density of the gas in :math:`kg.m^{-3}`. Read from the time folder.
- :math:`Y_{\rm spec, gas}` is the mass fraction of species spec in the gas phase. Read from the time folder.
- :math:`He_{\rm spec}` is the Henry's constant of species spec. Read from globalVars.
- :math:`W_{\rm spec}` is the molar mass of species spec in :math:`kg.mol^{-1}`. Read from globalVars.


Parameters
Expand Down Expand Up @@ -1032,9 +1032,18 @@ def compute_instantaneous_kla(
alpha_gas, field_dict = read_field(
field_name="alpha.gas", field_dict=field_dict, **kwargs
)
rho_liq, field_dict = read_field(
field_name="thermo:rho.liquid", field_dict=field_dict, **kwargs
)
try:
rho_liq, field_dict = read_field(
field_name="thermo:rho.liquid", field_dict=field_dict, **kwargs
)
except FileNotFoundError:
abs_time_path = os.path.join(case_folder, time_folder)
logger.warning(
f"thermo:rho.liquid not found in {abs_time_path}, assuming it is 1000kg/m3"
)
rho_liq = 1000.0
field_dict["rho_liq"] = rho_liq

rho_gas, field_dict = read_field(
field_name="thermo:rho.gas", field_dict=field_dict, **kwargs
)
Expand All @@ -1045,6 +1054,7 @@ def compute_instantaneous_kla(
field_name="U.liquid", field_dict=field_dict, **kwargs
)
d_gas, field_dict = read_bubble_diameter(field_dict=field_dict, **kwargs)

mu_liq, field_dict = read_mu_liquid(field_dict=field_dict, **kwargs)
species_gas = {}
for species_name in species_names:
Expand Down
140 changes: 85 additions & 55 deletions bird/utilities/ofio.py
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,41 @@ def read_mu_liquid(
return mu_liq, field_dict


def _cross_reference_math(expr):
"""
Resolve math in cross referencing in the globalVars dictionary
"""
# Replace 'Foam::pow' with 'math.pow'
if "Foam::pow" in expr:
expr = expr.replace("Foam::pow", "math.pow")

if "pow" in expr:
indstr = [m.start() for m in re.finditer("pow", expr)]
new_expr = expr
count_replace = 0
for ind in indstr:
if not "mat" in new_expr[ind - 6 : ind + 3]:
tmp = new_expr
new_expr = (
tmp[: ind + 5 * count_replace]
+ "math.pow"
+ tmp[ind + 5 * count_replace + 3 :]
)
count_replace += 1
expr = new_expr
# Replace 'exp' with 'math.exp'
expr = expr.replace("exp", "math.exp")
# Replace 'sin' with 'math.sin'
expr = expr.replace("sin", "math.sin")
# Replace 'cos' with 'math.cos'
expr = expr.replace("cos", "math.cos")
# Replace 'tan' with 'math.tan'
expr = expr.replace("tan", "math.tan")

degToRad = lambda x: x * np.pi / 180.0
return expr, degToRad


def _cross_reference_global_vars(
globalVars_dict: dict,
) -> dict:
Expand All @@ -1289,61 +1324,57 @@ def _cross_reference_global_vars(

for key, value in globalVars_dict.items():
# Check if we need to do cross referencing
if isinstance(value, str) and value.startswith("#calc"):
if isinstance(value, str):
# CALC case
if value.startswith("#calc"):
# Extract expression inside quotes
expr = value.split('"', 1)[1].rsplit('"', 1)[0]

# Replace $Var with its numeric value from globalVars_dict
def repl(match):
varname = match.group(1)
if varname not in cross_referenced_globalVars_dict:
raise KeyError(
f"Variable '{varname}' not found for expression {expr}"
)
return str(cross_referenced_globalVars_dict[varname])

# Extract expression inside quotes
expr = value.split('"', 1)[1].rsplit('"', 1)[0]
expr = var_pattern.sub(repl, expr)

# Replace $Var with its numeric value from globalVars_dict
def repl(match):
varname = match.group(1)
if varname not in cross_referenced_globalVars_dict:
raise KeyError(
f"Variable '{varname}' not found for expression {expr}"
expr, degToRad = _cross_reference_math(expr)
try:
result = eval(expr, {"math": math, "degToRad": degToRad})
result = float(result)
# Convert to int if whole number
# if result.is_integer():
# result = int(result)
cross_referenced_globalVars_dict[key] = result

except Exception as e:
logger.warning(
f"Could not evaluate globalVars expression for {key}: {expr}"
)
# Direct variable references
elif "$" in value:

def repl_direct(match):
varname = match.group(1)
return str(cross_referenced_globalVars_dict[varname])

try:
# Substitute the variable
expr = var_pattern.sub(repl_direct, value)
expr, degToRad = _cross_reference_math(expr)
result = eval(expr, {"math": math, "degToRad": degToRad})
# Convert to int if whole number
result = float(result)
# if result.is_integer():
# result = int(result)
cross_referenced_globalVars_dict[key] = result
except Exception as e:
logger.warning(
f"Could not evaluate globalVars expression for {key}: {expr}"
)
return str(cross_referenced_globalVars_dict[varname])

expr = var_pattern.sub(repl, expr)

# Replace 'Foam::pow' with 'math.pow'
if "Foam::pow" in expr:
expr = expr.replace("Foam::pow", "math.pow")

if "pow" in expr:
indstr = [m.start() for m in re.finditer("pow", expr)]
new_expr = expr
count_replace = 0
for ind in indstr:
if not "mat" in new_expr[ind - 6 : ind + 3]:
tmp = new_expr
new_expr = (
tmp[: ind + 5 * count_replace]
+ "math.pow"
+ tmp[ind + 5 * count_replace + 3 :]
)
count_replace += 1
expr = new_expr
# Replace 'exp' with 'math.exp'
expr = expr.replace("exp", "math.exp")
# Replace 'sin' with 'math.sin'
expr = expr.replace("sin", "math.sin")
# Replace 'cos' with 'math.cos'
expr = expr.replace("cos", "math.cos")
# Replace 'tan' with 'math.tan'
expr = expr.replace("tan", "math.tan")

degToRad = lambda x: x * np.pi / 180.0
try:
result = eval(expr, {"math": math, "degToRad": degToRad})
# Convert to int if whole number
if isinstance(result, float) and result.is_integer():
result = int(result)
cross_referenced_globalVars_dict[key] = result

except Exception as e:
logger.warning(
f"Could not evaluate globalVars expression for {key}: {expr}"
)

return cross_referenced_globalVars_dict

Expand Down Expand Up @@ -1374,7 +1405,6 @@ def read_global_vars(
globalVars_dict : dict
Dictionary that contains the globals vars variable values
"""

# Set the filename to read
if case_folder is None:
if filename is None or not os.path.isfile(filename):
Expand Down Expand Up @@ -1416,8 +1446,8 @@ def read_global_vars(
# Try numeric conversion otherwise store value as str
try:
val = float(value)
if val.is_integer():
val = int(val)
# if val.is_integer():
# val = int(val)
globalVars_dict[key] = val
except ValueError:
globalVars_dict[key] = value
Expand Down
2 changes: 1 addition & 1 deletion bird/version.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
"""Bio reactor design version"""

__version__ = "0.0.54"
__version__ = "0.0.55"
4 changes: 4 additions & 0 deletions noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ def run_lint(session: nox.Session) -> None:
"py314",
"--exclude",
".pixi",
"--exclude",
"pixi*",
".",
]
isort_command = [
Expand All @@ -79,6 +81,8 @@ def run_lint(session: nox.Session) -> None:
"--use-parentheses",
"--skip",
".pixi",
"--skip",
"pixi*",
".",
]

Expand Down
Loading
Loading