Spaces:
Running
Running
equilibrium fixed
Browse files- marimo/equilibrium.py +5 -3
- marimo/equilibrium_basic.py +12 -11
marimo/equilibrium.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
-
__generated_with = "0.
|
| 4 |
app = marimo.App(width="full")
|
| 5 |
|
| 6 |
|
|
@@ -10,12 +10,14 @@ def _():
|
|
| 10 |
import numpy as np
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
import copy
|
|
|
|
|
|
|
| 13 |
|
| 14 |
nspecies = mo.ui.number(2,10,value=2,label="Number of species")
|
| 15 |
mo.vstack([
|
| 16 |
mo.md("#**Numerical Solution of Equilibrium Problems**").center(),
|
| 17 |
nspecies],gap=2)
|
| 18 |
-
return copy, mo, np, nspecies, plt
|
| 19 |
|
| 20 |
|
| 21 |
@app.cell
|
|
@@ -297,7 +299,7 @@ def _(compute_Q, concentrations, execute, keq, mo, np, stoichiometry):
|
|
| 297 |
mm = mo.md("##**Optimisation Achieved**")
|
| 298 |
else:
|
| 299 |
mm = mo.md("##**Optimisation Failed**")
|
| 300 |
-
|
| 301 |
mo.vstack([
|
| 302 |
mm.center(),
|
| 303 |
mo.hstack([plot_c,plot_f,plot_d],
|
|
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
+
__generated_with = "0.12.5"
|
| 4 |
app = marimo.App(width="full")
|
| 5 |
|
| 6 |
|
|
|
|
| 10 |
import numpy as np
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
import copy
|
| 13 |
+
from typing import Dict
|
| 14 |
+
from numpy.typing import NDArray
|
| 15 |
|
| 16 |
nspecies = mo.ui.number(2,10,value=2,label="Number of species")
|
| 17 |
mo.vstack([
|
| 18 |
mo.md("#**Numerical Solution of Equilibrium Problems**").center(),
|
| 19 |
nspecies],gap=2)
|
| 20 |
+
return Dict, NDArray, copy, mo, np, nspecies, plt
|
| 21 |
|
| 22 |
|
| 23 |
@app.cell
|
|
|
|
| 299 |
mm = mo.md("##**Optimisation Achieved**")
|
| 300 |
else:
|
| 301 |
mm = mo.md("##**Optimisation Failed**")
|
| 302 |
+
|
| 303 |
mo.vstack([
|
| 304 |
mm.center(),
|
| 305 |
mo.hstack([plot_c,plot_f,plot_d],
|
marimo/equilibrium_basic.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
-
__generated_with = "0.
|
| 4 |
app = marimo.App(width="medium")
|
| 5 |
|
| 6 |
|
|
@@ -9,6 +9,8 @@ def _():
|
|
| 9 |
import marimo as mo
|
| 10 |
import numpy as np
|
| 11 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 12 |
|
| 13 |
conc_a = mo.ui.text(value="0.2",label="$[\mathrm{A}]_0$")
|
| 14 |
conc_b = mo.ui.text(value="0.1",label="$[\mathrm{B}]_0$")
|
|
@@ -28,7 +30,7 @@ def _():
|
|
| 28 |
{step} {tol}
|
| 29 |
"""
|
| 30 |
)
|
| 31 |
-
return conc_a, conc_b, keq, mo, np, plt, step, tol
|
| 32 |
|
| 33 |
|
| 34 |
@app.cell
|
|
@@ -112,7 +114,7 @@ def _(
|
|
| 112 |
) -> NDArray:
|
| 113 |
"""
|
| 114 |
Solves chemical equilibrium equations using an iterative approach.
|
| 115 |
-
|
| 116 |
Args:
|
| 117 |
initial_conc: Dictionary of initial concentrations for each species
|
| 118 |
stoichiometry: Dictionary of stoichiometric coefficients
|
|
@@ -120,7 +122,7 @@ def _(
|
|
| 120 |
dc: Concentration step size for iterations
|
| 121 |
rtol: Relative tolerance for convergence
|
| 122 |
max_iterations: Maximum number of iterations before stopping
|
| 123 |
-
|
| 124 |
Returns:
|
| 125 |
NDArray: Array with columns [iteration, conc_A, conc_B, force]
|
| 126 |
"""
|
|
@@ -129,14 +131,14 @@ def _(
|
|
| 129 |
conc_A = np.zeros(max_iterations + 1)
|
| 130 |
conc_B = np.zeros(max_iterations + 1)
|
| 131 |
forces = np.zeros(max_iterations + 1)
|
| 132 |
-
|
| 133 |
# Set initial values
|
| 134 |
conc = initial_conc.copy()
|
| 135 |
force_0 = compute_force(conc, stoichiometry, pK_eq)
|
| 136 |
conc_A[0] = conc['A']
|
| 137 |
conc_B[0] = conc['B']
|
| 138 |
forces[0] = force_0
|
| 139 |
-
|
| 140 |
# Iterate until convergence or max iterations
|
| 141 |
for i in range(max_iterations):
|
| 142 |
# Update values
|
|
@@ -145,13 +147,13 @@ def _(
|
|
| 145 |
# if force*forces[i] < 0:
|
| 146 |
# dc /=2
|
| 147 |
pQ = -np.log10(compute_Q(conc, stoichiometry))
|
| 148 |
-
|
| 149 |
# Store results
|
| 150 |
iterations[i + 1] = i + 1
|
| 151 |
conc_A[i + 1] = conc['A']
|
| 152 |
conc_B[i + 1] = conc['B']
|
| 153 |
forces[i + 1] = force
|
| 154 |
-
|
| 155 |
# Check convergence
|
| 156 |
# if np.isclose(pQ, pK_eq, rtol=rtol):
|
| 157 |
if np.abs(force) < rtol:
|
|
@@ -162,7 +164,7 @@ def _(
|
|
| 162 |
conc_B[:i + 2],
|
| 163 |
forces[:i + 2]
|
| 164 |
])
|
| 165 |
-
|
| 166 |
# Return all iterations if no convergence
|
| 167 |
return np.column_stack([iterations, conc_A, conc_B, forces])
|
| 168 |
|
|
@@ -172,7 +174,7 @@ def _(
|
|
| 172 |
plt.figure(figsize=(4,4))
|
| 173 |
for i in range(0,ncols-1):
|
| 174 |
plt.plot(data[:,0],data[:,i+1],label=labels[i],color=colors[i])
|
| 175 |
-
|
| 176 |
if refs is not None:
|
| 177 |
for i in range(len(refs)):
|
| 178 |
plt.axhline(refs[i],linestyle='dashed',label=labels[i]+"$_{exact}$",color=colors[i])
|
|
@@ -214,7 +216,6 @@ def _(
|
|
| 214 |
mo.vstack([initial,final,
|
| 215 |
mo.hstack([plot_c,plot_f])
|
| 216 |
])
|
| 217 |
-
|
| 218 |
return (
|
| 219 |
analytic_solution,
|
| 220 |
conc,
|
|
|
|
| 1 |
import marimo
|
| 2 |
|
| 3 |
+
__generated_with = "0.12.5"
|
| 4 |
app = marimo.App(width="medium")
|
| 5 |
|
| 6 |
|
|
|
|
| 9 |
import marimo as mo
|
| 10 |
import numpy as np
|
| 11 |
import matplotlib.pyplot as plt
|
| 12 |
+
from typing import Dict
|
| 13 |
+
from numpy.typing import NDArray
|
| 14 |
|
| 15 |
conc_a = mo.ui.text(value="0.2",label="$[\mathrm{A}]_0$")
|
| 16 |
conc_b = mo.ui.text(value="0.1",label="$[\mathrm{B}]_0$")
|
|
|
|
| 30 |
{step} {tol}
|
| 31 |
"""
|
| 32 |
)
|
| 33 |
+
return Dict, NDArray, conc_a, conc_b, keq, mo, np, plt, step, tol
|
| 34 |
|
| 35 |
|
| 36 |
@app.cell
|
|
|
|
| 114 |
) -> NDArray:
|
| 115 |
"""
|
| 116 |
Solves chemical equilibrium equations using an iterative approach.
|
| 117 |
+
|
| 118 |
Args:
|
| 119 |
initial_conc: Dictionary of initial concentrations for each species
|
| 120 |
stoichiometry: Dictionary of stoichiometric coefficients
|
|
|
|
| 122 |
dc: Concentration step size for iterations
|
| 123 |
rtol: Relative tolerance for convergence
|
| 124 |
max_iterations: Maximum number of iterations before stopping
|
| 125 |
+
|
| 126 |
Returns:
|
| 127 |
NDArray: Array with columns [iteration, conc_A, conc_B, force]
|
| 128 |
"""
|
|
|
|
| 131 |
conc_A = np.zeros(max_iterations + 1)
|
| 132 |
conc_B = np.zeros(max_iterations + 1)
|
| 133 |
forces = np.zeros(max_iterations + 1)
|
| 134 |
+
|
| 135 |
# Set initial values
|
| 136 |
conc = initial_conc.copy()
|
| 137 |
force_0 = compute_force(conc, stoichiometry, pK_eq)
|
| 138 |
conc_A[0] = conc['A']
|
| 139 |
conc_B[0] = conc['B']
|
| 140 |
forces[0] = force_0
|
| 141 |
+
|
| 142 |
# Iterate until convergence or max iterations
|
| 143 |
for i in range(max_iterations):
|
| 144 |
# Update values
|
|
|
|
| 147 |
# if force*forces[i] < 0:
|
| 148 |
# dc /=2
|
| 149 |
pQ = -np.log10(compute_Q(conc, stoichiometry))
|
| 150 |
+
|
| 151 |
# Store results
|
| 152 |
iterations[i + 1] = i + 1
|
| 153 |
conc_A[i + 1] = conc['A']
|
| 154 |
conc_B[i + 1] = conc['B']
|
| 155 |
forces[i + 1] = force
|
| 156 |
+
|
| 157 |
# Check convergence
|
| 158 |
# if np.isclose(pQ, pK_eq, rtol=rtol):
|
| 159 |
if np.abs(force) < rtol:
|
|
|
|
| 164 |
conc_B[:i + 2],
|
| 165 |
forces[:i + 2]
|
| 166 |
])
|
| 167 |
+
|
| 168 |
# Return all iterations if no convergence
|
| 169 |
return np.column_stack([iterations, conc_A, conc_B, forces])
|
| 170 |
|
|
|
|
| 174 |
plt.figure(figsize=(4,4))
|
| 175 |
for i in range(0,ncols-1):
|
| 176 |
plt.plot(data[:,0],data[:,i+1],label=labels[i],color=colors[i])
|
| 177 |
+
|
| 178 |
if refs is not None:
|
| 179 |
for i in range(len(refs)):
|
| 180 |
plt.axhline(refs[i],linestyle='dashed',label=labels[i]+"$_{exact}$",color=colors[i])
|
|
|
|
| 216 |
mo.vstack([initial,final,
|
| 217 |
mo.hstack([plot_c,plot_f])
|
| 218 |
])
|
|
|
|
| 219 |
return (
|
| 220 |
analytic_solution,
|
| 221 |
conc,
|