Reactions Module (softpotato.reactions)¶
The softpotato.reactions subpackage provides modular abstractions for multi-species chemical metadata, homogeneous (solution-phase) reaction kinetics with automated analytical Jacobians, heterogeneous interfacial charge-transfer kinetics, and unified electrochemical mechanism containers that plug directly into DiffusionProblem and ScipyIVPSolver.
Theoretical Background¶
1. Coupled Reaction–Diffusion Transport & Analytical Jacobians¶
In a 1D multi-species electrochemical system with coupled solution-phase chemistry, the concentration \(c_i(x, t)\) of each chemical species \(i\) evolves according to the reaction–diffusion partial differential equation:
where \(D_i > 0\) is the diffusion coefficient of species \(i\), \(\nu_{i,j}\) is its net stoichiometric coefficient in reaction \(j\) (negative for consumed reactants, positive for produced products), and \(r_j(\mathbf{c})\) is the volumetric reaction rate.
Because homogeneous chemical rate constants \(k\) can span many orders of magnitude (\(10^{-2}\) to \(10^{6}\,\text{s}^{-1}\)), reaction–diffusion ODE systems produced by Method-of-Lines discretization are frequently stiff. Every HomogeneousReaction subclass provides exact analytical partial derivatives \(\partial r_j / \partial c_k\), allowing ReactionNetwork to assemble the exact local and global sparse kinetic Jacobian:
ScipyIVPSolver automatically combines the sparse spatial diffusion Jacobian with the sparse kinetic Jacobian for implicit stiff integrators (Radau and BDF), dramatically accelerating convergence and maintaining machine-precision mass conservation.
2. Characteristic Reaction Layer Thickness & Expanding Grids¶
For a coupled catalytic (EC') or follow-up (EC) mechanism with pseudo-first-order rate constant \(k'\), homogeneous chemical conversion confines concentration gradients within a reaction layer of characteristic thickness:
When \(k'\) is large, \(\mu \ll \delta = 6\sqrt{D t_{\max}}\) and the interfacial concentration profile becomes exponentially steep near \(x = 0\):
Pairing GeometricExpandingMesh or ExponentialMesh from softpotato.grid with ScipyIVPSolver (method="Radau") concentrates spatial nodes inside the thin reaction layer \(\mu\) while smoothly coarsening into the bulk, achieving \(< 1\%\) current accuracy with fewer than 100 grid nodes.
3. Heterogeneous Interfacial Charge-Transfer Kinetics¶
For a single-step interfacial redox reaction at the electrode surface (\(x = 0\)):
- Butler–Volmer kinetics (
ButlerVolmer) relates forward (reduction) and backward (oxidation) rate constants to the overpotential \(\eta = E(t) - E^{0\prime}\):
$$ k_f(E) = k^0 \exp!\left(-\frac{\alpha n F}{R T}(E - E^{0\prime})\right), \qquad k_b(E) = k^0 \exp!\left(\frac{(1 - \alpha) n F}{R T}(E - E^{0\prime})\right) $$
yielding the net interfacial reduction flux \(J = k_f(E) c_{\text{Ox}}(0, t) - k_b(E) c_{\text{Red}}(0, t)\) and IUPAC Faradaic current density (anodic positive):
$$ j = -n F J = n F \left[k_b(E) c_{\text{Red}}(0, t) - k_f(E) c_{\text{Ox}}(0, t)\right] $$
-
Tafel kinetics (
TafelKinetics/Tafel) isolates the dominant unidirectional branch at high overpotential (\(|\eta| \gg RT/nF\)), retaining only \(k_f(E)\) on the cathodic branch or \(k_b(E)\) on the anodic branch. -
Nernstian equilibrium (
NernstianEquilibrium) models infinitely fast (reversible) electron transfer by enforcing the thermodynamic surface concentration ratio directly as time-dependent Dirichlet boundary conditions:
$$ \frac{c_{\text{Ox}}(0, t)}{c_{\text{Red}}(0, t)} = \exp!\left(\frac{n F}{R T}\left(E(t) - E^{0\prime}\right)\right) $$
API Overview¶
Chemical Species & Collections¶
| Class | Key Parameters / Properties | Description |
|---|---|---|
Species |
name, diffusivity, bulk_concentration, charge, standard_potential, n_electrons |
Immutable dataclass encapsulating a chemical species and its physicochemical transport/thermodynamic metadata. |
SpeciesCollection |
names, diffusivities, bulk_concentrations, n_electrons_map |
Ordered mapping container managing a group of Species objects with dict-like lookup and DiffusionProblem export helpers. |
Homogeneous Reaction Kinetics¶
| Class | Mechanism / Rate Law | Key Parameters | Description |
|---|---|---|---|
HomogeneousReaction |
Abstract | stoichiometry, rate(), rate_derivatives() |
Abstract base class for custom solution-phase reactions with analytical or finite-difference Jacobians. |
MassActionReaction |
\(\sum a_i A_i \rightleftharpoons \sum b_j B_j\) | reactants, products, k_f, k_b |
General elementary mass-action reaction supporting arbitrary integer stoichiometric orders and reversibility. |
FirstOrderReaction |
\(A \xrightarrow{k} B\) | reactant, product, k |
Irreversible unimolecular reaction (\(r = k c_A\)), e.g. EC follow-up decay. |
ReversibleReaction |
\(A \underset{k_b}{\overset{k_f}{\rightleftharpoons}} B\) | reactant, product, k_f, k_b |
Reversible first-order equilibrium (\(r = k_f c_A - k_b c_B\)) with equilibrium constant \(K_{\text{eq}} = k_f / k_b\). |
SecondOrderReaction |
\(A + B \xrightarrow{k} C\) | reactant_1, reactant_2, product, k |
Irreversible bimolecular reaction (\(r = k c_A c_B\)). |
DimerizationReaction |
\(2 A \xrightarrow{k} A_2\) | monomer, dimer, k |
Irreversible homodimerization (\(r = k c_A^2\)). |
DisproportionationReaction |
\(2 B \xrightarrow{k} A + C\) | reactant, product_1, product_2, k |
Irreversible disproportionation (\(r = k c_B^2\)). |
CatalyticReaction |
\(B (+ Z) \xrightarrow{k} A (+ P)\) | reactant, regenerated, k, substrate, product |
Pseudo-first-order (\(r = k c_B\)) or bimolecular (\(r = k c_B c_Z\)) EC' catalytic regeneration step. |
CustomReaction |
User callable \(r(\mathbf{c}, x, t)\) | stoichiometry, rate, rate_derivatives |
Wraps arbitrary user-defined rate laws (e.g. Michaelis–Menten) with optional analytical or automatic finite-difference derivatives. |
ReactionNetwork |
\(\mathbf{R}(\mathbf{c}) = \sum_j \boldsymbol{\nu}_j r_j(\mathbf{c})\) | reactions, species |
Composes multiple homogeneous reactions and assembles vectorized rates and sparse block Jacobians for DiffusionProblem. |
Heterogeneous Charge-Transfer Kinetics¶
| Class / Function | Key Parameters | Description |
|---|---|---|
HeterogeneousKinetics |
E0, n, T, R, F |
Abstract base class for finite-rate interfacial electron-transfer models providing rate_constants, net_flux, current_density, and boundary_conditions. |
ButlerVolmer |
k0, alpha, E0, n, T |
Reversible and quasi-reversible Butler–Volmer charge-transfer kinetics. |
TafelKinetics (Tafel) |
k0, alpha, E0, n, branch, T |
High-overpotential / irreversible unidirectional Tafel kinetics ('auto', 'cathodic', or 'anodic'). |
NernstianEquilibrium |
E0, n, T |
Reversible thermodynamic equilibrium helper computing surface concentration ratios and Dirichlet boundary conditions. |
create_electrochemical_bcs |
kinetics, potential, ox, red, electrode_side, bulk_bc |
Factory constructing matched (left_bc, right_bc) boundary condition tuples for a redox pair in DiffusionProblem. |
Mechanism Containers¶
| Class | Key Parameters | Description |
|---|---|---|
ElectrochemicalStep |
ox, red, kinetics, electrode_side, bulk_bc |
Declarative specification of a single interfacial redox step at an electrode boundary. |
Mechanism |
species, reactions, electrochemical_steps, name |
Complete chemical/electrochemical model independent of grid and solver, with create_problem(...) helper. |
CustomMechanism |
species, reactions, rates, local_jacobian_func, electrochemical_steps, boundary_conditions_func |
Declarative extension point combining built-in reactions, custom rate/Jacobian callables, and custom boundary conditions. |
User Guide & Examples¶
1. Defining Species and a Catalytic (EC') Reaction Network¶
Define Species objects and combine homogeneous steps in a ReactionNetwork, then pass them directly to DiffusionProblem:
import math
import numpy as np
from softpotato.constants import FARADAY
from softpotato.geometry import PlanarGeometry
from softpotato.grid import GeometricExpandingMesh
from softpotato.reactions import CatalyticReaction, ReactionNetwork, Species
from softpotato.solver import DiffusionProblem, DirichletBC, ScipyIVPSolver
D = 1e-9 # m^2/s
c_bulk = 1.0 # mol/m^3 (1 mM)
area = 1e-4 # m^2 (1 cm^2)
k_prime = 10.0 # s^-1 (pseudo-first-order catalytic rate constant)
# Reaction layer thickness mu = sqrt(D / k'); span 10 * mu with an expanding mesh
mu = math.sqrt(D / k_prime)
mesh = GeometricExpandingMesh(0.0, 10.0 * mu, n_points=80, gamma=1.06)
ox = Species("Ox", diffusivity=D, bulk_concentration=c_bulk, n_electrons=1)
red = Species("Red", diffusivity=D, bulk_concentration=0.0, n_electrons=-1)
network = ReactionNetwork([CatalyticReaction(red, ox, k=k_prime)])
problem = DiffusionProblem(
grid=mesh,
species=[ox, red],
boundary_conditions={
"Ox": (DirichletBC(0.0), DirichletBC(c_bulk)),
"Red": (DirichletBC(c_bulk), DirichletBC(0.0)),
},
reactions=network,
geometry=PlanarGeometry(area=area),
)
solver = ScipyIVPSolver(method="Radau", rtol=1e-7, atol=1e-10)
result = solver.solve(problem, t_span=(0.0, 1.0), t_eval=np.linspace(0.0, 1.0, 100))
i_cat_exact = -FARADAY * area * c_bulk * math.sqrt(D * k_prime)
print(f"Simulated steady-state current: {result.currents['Ox'][-1] * 1e3:.4f} mA")
print(f"Analytical catalytic limit: {i_cat_exact * 1e3:.4f} mA")
2. Heterogeneous Kinetics & Mechanism Containers¶
Evaluate Butler–Volmer rate constants and construct boundary conditions via create_electrochemical_bcs or package species, homogeneous steps, and interfacial kinetics together in a Mechanism:
from softpotato.grid import create_diffusion_mesh
from softpotato.reactions import (
ButlerVolmer,
ElectrochemicalStep,
FirstOrderReaction,
Mechanism,
NernstianEquilibrium,
Species,
)
from softpotato.solver import ScipyIVPSolver
# 1. Inspect Butler-Volmer kinetics
bv = ButlerVolmer(k0=1e-4, alpha=0.5, E0=0.0, n=1)
kf, kb = bv.rate_constants(E=-0.1)
j0 = bv.exchange_current_density(c_ox=1.0, c_red=1.0)
# 2. Build a complete EC mechanism (reversible E step + follow-up chemical decay)
ox = Species("Ox", diffusivity=1e-9, bulk_concentration=1.0, n_electrons=1)
red = Species("Red", diffusivity=1e-9, bulk_concentration=0.0, n_electrons=-1)
prod = Species("P", diffusivity=1e-9, bulk_concentration=0.0)
ec_mech = Mechanism(
species=[ox, red, prod],
reactions=[FirstOrderReaction(red, prod, k=2.0)],
electrochemical_steps=[
ElectrochemicalStep(ox, red, NernstianEquilibrium(E0=0.0, n=1))
],
name="EC_Mechanism",
)
mesh = create_diffusion_mesh(
D=1e-9, t_max=1.0, kind="geometric", n_points=60, gamma=1.08
)
problem = ec_mech.create_problem(mesh, potential=-0.3)
result = ScipyIVPSolver(method="Radau").solve(problem, t_span=(0.0, 1.0))
3. Interactive Tutorials¶
For full worked examples with plots and analytical benchmarks, explore the interactive Jupyter notebooks:
- Catalytic EC' Mechanism:
tutorials/mechanisms/catalytic_ec.ipynb— chronoamperometry of an EC' catalytic cycle, steady-state concentration profiles, and validation against the Delahay–Stiehl and catalytic limiting current equations. - Heterogeneous Kinetics & Cyclic Voltammetry:
tutorials/mechanisms/cv_butler_volmer.ipynb— Butler–Volmer cyclic voltammetry across reversible, quasi-reversible, and irreversible regimes, with \(\Delta E_p\) vs. scan rate analysis against Randles–Ševčík and Nicholson theory.
Module Contents¶
reactions
¶
Chemical species and electrochemical reaction kinetics for Soft Potato.
This subpackage provides modular abstractions for chemical species metadata, homogeneous (solution-phase) reaction kinetics with automated analytical Jacobians, and heterogeneous charge-transfer kinetics.
Examples:
>>> from softpotato.reactions import ButlerVolmer, Species, SpeciesCollection
>>> ox = Species("Ox", diffusivity=1e-9, bulk_concentration=1.0, n_electrons=1)
>>> red = Species("Red", diffusivity=1e-9, bulk_concentration=0.0, n_electrons=-1)
>>> species = SpeciesCollection([ox, red])
>>> species.diffusivities
{'Ox': 1e-09, 'Red': 1e-09}
>>> bv = ButlerVolmer(k0=1e-4, alpha=0.5, E0=0.0, n=1)
>>> bv.rate_constants(0.0)
(0.0001, 0.0001)
Homogeneous reactions are combined in a ReactionNetwork, which can be
passed directly to DiffusionProblem(..., reactions=network):
>>> from softpotato.reactions import FirstOrderReaction, ReactionNetwork
>>> network = ReactionNetwork([FirstOrderReaction(red, "P", k=1.0)])
>>> network.species
['Red', 'P']
ButlerVolmer
dataclass
¶
Bases: HeterogeneousKinetics
Butler–Volmer heterogeneous charge-transfer kinetics.
Models forward (cathodic) and backward (anodic) electron-transfer rate constants for \(\text{Ox} + n e^- \rightleftharpoons \text{Red}\):
Rate constants, exchange current density, Faradaic current density, and Tafel
slopes are evaluated by delegating to softpotato.analytical.kinetics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k0
|
float
|
Standard heterogeneous rate constant \(k^0 > 0\) in \(\text{m}/\text{s}\) or \(\text{cm}/\text{s}\). |
required |
alpha
|
float
|
Cathodic charge-transfer coefficient (\(0 < \alpha < 1\)). |
0.5
|
E0
|
float
|
Formal reduction potential \(E^{0\prime}\) in Volts (V). |
0.0
|
n
|
float
|
Number of electrons transferred (\(n > 0\)). |
1
|
T
|
float
|
Temperature in Kelvin (\(T > 0\), default 298.15 K). |
STANDARD_TEMPERATURE
|
R
|
float
|
Molar gas constant (\(R > 0\)). |
GAS_CONSTANT
|
F
|
float
|
Faraday constant (\(F > 0\)). |
FARADAY
|
Examples:
>>> from softpotato.reactions import ButlerVolmer
>>> bv = ButlerVolmer(k0=1e-4, alpha=0.5, E0=0.2, n=1)
>>> kf, kb = bv.rate_constants(0.2)
>>> kf == kb == 1e-4
True
>>> bv.current_density(0.2, c_ox=1.0, c_red=1.0)
0.0
Source code in src/softpotato/reactions/heterogeneous.py
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 | |
k_f
¶
Evaluate forward (cathodic reduction) rate constant \(k_f(E)\).
Source code in src/softpotato/reactions/heterogeneous.py
k_b
¶
Evaluate backward (anodic oxidation) rate constant \(k_b(E)\).
Source code in src/softpotato/reactions/heterogeneous.py
rate_constants
¶
Evaluate \((k_f(E), k_b(E))\) at electrode potential \(E\).
exchange_current_density
¶
Compute exchange current density \(j_0\) via softpotato.analytical.kinetics.exchange_current_density.
Source code in src/softpotato/reactions/heterogeneous.py
current_density
¶
current_density(E: float | ArrayLike, c_ox: float | ArrayLike, c_red: float | ArrayLike = 0.0, *, anodic_positive: bool = True) -> float | np.ndarray
Evaluate net Butler–Volmer current density via softpotato.analytical.kinetics.butler_volmer_current_density.
Source code in src/softpotato/reactions/heterogeneous.py
tafel_slope
¶
Compute theoretical Tafel slope \(b\) in \(\text{V}/\text{dec}\) via softpotato.analytical.kinetics.tafel_slope.
Source code in src/softpotato/reactions/heterogeneous.py
HeterogeneousKinetics
¶
Bases: ABC
Abstract base class for heterogeneous interfacial charge-transfer kinetics.
Models single-step electron transfer at an electrode interface:
Subclasses must implement rate_constants and provide attributes
E0, n, T, R, and F.
Source code in src/softpotato/reactions/heterogeneous.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
overpotential
¶
Compute formal overpotential \(\eta = E - E^{0\prime}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float or array - like
|
Electrode potential \(E\) in Volts (V). |
required |
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Overpotential \(E - E^{0\prime}\) in Volts (V). |
Source code in src/softpotato/reactions/heterogeneous.py
rate_constants
abstractmethod
¶
Evaluate forward and backward heterogeneous rate constants \((k_f, k_b)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float or array - like
|
Electrode potential \(E\) in Volts (V). |
required |
Returns:
| Type | Description |
|---|---|
tuple of (float or np.ndarray, float or np.ndarray)
|
|
Source code in src/softpotato/reactions/heterogeneous.py
flux
¶
flux(E: float | ArrayLike, c_ox: float | ArrayLike, c_red: float | ArrayLike = 0.0) -> float | np.ndarray
Evaluate net interfacial reduction flux \(J\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float or array - like
|
Electrode potential \(E\) in Volts (V). |
required |
c_ox
|
float or array - like
|
Surface concentration of oxidized species \(c_{\text{Ox}}(0, t) \ge 0\). |
required |
c_red
|
float or array - like
|
Surface concentration of reduced species \(c_{\text{Red}}(0, t) \ge 0\). |
0.0
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Net interfacial flux \(J\) (positive for net reduction of Ox to Red). |
Source code in src/softpotato/reactions/heterogeneous.py
current_density
¶
current_density(E: float | ArrayLike, c_ox: float | ArrayLike, c_red: float | ArrayLike = 0.0, *, anodic_positive: bool = True) -> float | np.ndarray
Evaluate net interfacial Faradaic current density \(j\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float or array - like
|
Electrode potential \(E\) in Volts (V). |
required |
c_ox
|
float or array - like
|
Surface concentration of oxidized species \(c_{\text{Ox}}(0, t) \ge 0\). |
required |
c_red
|
float or array - like
|
Surface concentration of reduced species \(c_{\text{Red}}(0, t) \ge 0\). |
0.0
|
anodic_positive
|
bool
|
If |
True
|
Returns:
| Type | Description |
|---|---|
float or ndarray
|
Interfacial Faradaic current density \(j\) in \(\text{A}/\text{m}^2\) or \(\text{A}/\text{cm}^2\). |
Source code in src/softpotato/reactions/heterogeneous.py
NernstianEquilibrium
dataclass
¶
Reversible Nernstian charge-transfer equilibrium helper.
Enforces thermodynamic surface equilibrium at an electrode interface:
All thermodynamic calculations delegate directly to
nernst,
nernst_ratio, and
nernst_equilibrium_concentrations.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E0
|
float
|
Formal reduction potential \(E^{0\prime}\) in Volts (V). |
0.0
|
n
|
float
|
Number of electrons transferred (\(n > 0\)). |
1
|
T
|
float
|
Temperature in Kelvin (\(T > 0\), default 298.15 K). |
STANDARD_TEMPERATURE
|
R
|
float
|
Molar gas constant (\(R > 0\)). |
GAS_CONSTANT
|
F
|
float
|
Faraday constant (\(F > 0\)). |
FARADAY
|
Examples:
>>> from softpotato.reactions import NernstianEquilibrium
>>> eq = NernstianEquilibrium(E0=0.2, n=1)
>>> eq.concentration_ratio(0.2)
1.0
>>> eq.surface_concentrations(0.2, c_total=2.0)
(1.0, 1.0)
Source code in src/softpotato/reactions/heterogeneous.py
722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
potential
¶
Compute equilibrium electrode potential \(E_{\text{eq}}\) via softpotato.analytical.kinetics.nernst.
Source code in src/softpotato/reactions/heterogeneous.py
concentration_ratio
¶
Compute equilibrium ratio \(c_{\text{Ox}} / c_{\text{Red}}\) via softpotato.analytical.kinetics.nernst_ratio.
Source code in src/softpotato/reactions/heterogeneous.py
surface_concentrations
¶
surface_concentrations(E: float | ArrayLike, c_total: float | ArrayLike = 1.0, *, D_ox: float | None = None, D_red: float | None = None, c_ox_bulk: float | None = None, c_red_bulk: float | None = None) -> tuple[float | np.ndarray, float | np.ndarray]
Compute equilibrium surface concentrations \((c_{\text{Ox}}(0), c_{\text{Red}}(0))\).
When D_ox and D_red are omitted or equal, delegates directly to
softpotato.analytical.kinetics.nernst_equilibrium_concentrations
with \(c_{\text{Ox}}(0) + c_{\text{Red}}(0) = c_{\text{total}}\).
When unequal diffusivities D_ox != D_red are provided, applies the
semi-infinite planar flux-balanced Nernstian relation using the reversible
half-wave potential
\(E_{1/2} = E^{0\prime} - \frac{R T}{n F} \ln\sqrt{D_{\text{Ox}} / D_{\text{Red}}}\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
E
|
float or array - like
|
Electrode potential \(E\) in Volts (V). |
required |
c_total
|
float or array - like
|
Total electroactive concentration \(c_{\text{Ox}}^* + c_{\text{Red}}^*\). |
1.0
|
D_ox
|
float
|
Diffusion coefficient of oxidized species (\(D_{\text{Ox}} > 0\)). |
None
|
D_red
|
float
|
Diffusion coefficient of reduced species (\(D_{\text{Red}} > 0\)). |
None
|
c_ox_bulk
|
float
|
Bulk concentration of oxidized species (used when |
None
|
c_red_bulk
|
float
|
Bulk concentration of reduced species (used when |
None
|
Returns:
| Type | Description |
|---|---|
tuple of (float or np.ndarray, float or np.ndarray)
|
Equilibrium surface concentrations |
Source code in src/softpotato/reactions/heterogeneous.py
811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 | |
TafelKinetics
dataclass
¶
Bases: HeterogeneousKinetics
High-overpotential / irreversible Tafel heterogeneous charge-transfer kinetics.
Retains only the dominant exponential branch of Butler–Volmer kinetics:
branch='cathodic'(irreversible reduction): \(k_f(E) = k^0 \exp(-\alpha n F (E - E^{0\prime}) / (R T))\), \(k_b(E) = 0\).branch='anodic'(irreversible oxidation): \(k_f(E) = 0\), \(k_b(E) = k^0 \exp((1 - \alpha) n F (E - E^{0\prime}) / (R T))\).branch='auto': selects the anodic branch for \(E \ge E^{0\prime}\) and the cathodic branch for \(E < E^{0\prime}\).
All rate constants, current densities, Tafel slopes, and inverse overpotentials
delegate directly to softpotato.analytical.kinetics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k0
|
float
|
Standard heterogeneous rate constant \(k^0 > 0\) in \(\text{m}/\text{s}\) or \(\text{cm}/\text{s}\). |
required |
alpha
|
float
|
Cathodic charge-transfer coefficient (\(0 < \alpha < 1\)). |
0.5
|
E0
|
float
|
Formal reduction potential \(E^{0\prime}\) in Volts (V). |
0.0
|
n
|
float
|
Number of electrons transferred (\(n > 0\)). |
1
|
branch
|
('auto', 'anodic', 'cathodic')
|
Polarization branch to model. |
'auto'
|
T
|
float
|
Temperature in Kelvin (\(T > 0\), default 298.15 K). |
STANDARD_TEMPERATURE
|
R
|
float
|
Molar gas constant (\(R > 0\)). |
GAS_CONSTANT
|
F
|
float
|
Faraday constant (\(F > 0\)). |
FARADAY
|
Examples:
>>> from softpotato.reactions import TafelKinetics
>>> tk = TafelKinetics(k0=1e-4, alpha=0.5, E0=0.0, n=1, branch="cathodic")
>>> kf, kb = tk.rate_constants(-0.1)
>>> kf > 1e-4 and kb == 0.0
True
>>> round(tk.tafel_slope(signed=True) * 1000, 1)
-118.3
Source code in src/softpotato/reactions/heterogeneous.py
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 | |
rate_constants
¶
Evaluate Tafel rate constants \((k_f(E), k_b(E))\) via softpotato.analytical.kinetics.tafel.
Source code in src/softpotato/reactions/heterogeneous.py
current_density
¶
current_density(E: float | ArrayLike, c_ox: float | ArrayLike, c_red: float | ArrayLike = 0.0, *, anodic_positive: bool = True) -> float | np.ndarray
Evaluate Tafel Faradaic current density via softpotato.analytical.kinetics.tafel.
Source code in src/softpotato/reactions/heterogeneous.py
566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 | |
tafel_slope
¶
Compute theoretical Tafel slope \(b\) in \(\text{V}/\text{dec}\) via softpotato.analytical.kinetics.tafel_slope.
Source code in src/softpotato/reactions/heterogeneous.py
overpotential_for_current
¶
overpotential_for_current(current_density: float | ArrayLike, *, c_ox: float = 1.0, c_red: float = 1.0, branch: Literal['anodic', 'cathodic'] | None = None) -> float | np.ndarray
Compute activation overpotential \(\eta\) for a target current density via softpotato.analytical.kinetics.tafel_overpotential.
Source code in src/softpotato/reactions/heterogeneous.py
CatalyticReaction
¶
Bases: MassActionReaction
Catalytic regeneration step of an EC' mechanism.
The electrogenerated species \(B\) reacts with a substrate \(Z\), regenerating the electroactive species \(A\):
If substrate is None the substrate is assumed to be in large excess
and the step reduces to pseudo-first-order regeneration
\(B \xrightarrow{k'} A\) with \(k' = k c_Z^*\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactant
|
str or Species
|
Electrogenerated catalyst form \(B\). |
required |
regenerated
|
str or Species
|
Regenerated catalyst form \(A\). |
required |
k
|
float
|
Rate constant (second-order, or pseudo-first-order if
|
required |
substrate
|
str or Species or None
|
Substrate \(Z\) (explicitly tracked if given). |
None
|
product
|
str or Species or None
|
Substrate-derived product \(Y\) (explicitly tracked if given). |
None
|
Examples:
>>> import numpy as np
>>> from softpotato.reactions import CatalyticReaction
>>> rxn = CatalyticReaction("Red", "Ox", k=5.0, substrate="Z", product="Y")
>>> c = {"Red": np.array([0.2]), "Ox": np.array([0.8]),
... "Z": np.array([1.0]), "Y": np.array([0.0])}
>>> rxn.rates(c, None, 0.0)
{'Red': array([-1.]), 'Z': array([-1.]), 'Ox': array([1.]), 'Y': array([1.])}
Source code in src/softpotato/reactions/homogeneous.py
CustomReaction
¶
Bases: HomogeneousReaction
User-defined homogeneous reaction with an arbitrary rate law.
Build your own reaction by supplying its net stoichiometry and a rate law
rate(c, x, t). Source terms are \(R_i = \nu_i\, r(\mathbf{c}, x, t)\).
Analytical derivatives \(\partial r / \partial c_k\) can be supplied
through rate_derivatives; any dependency without one is differentiated
automatically with central finite differences, so the kinetic Jacobian is
always available.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
stoichiometry
|
Mapping[str | Species, float]
|
Net stoichiometric coefficients |
required |
rate
|
Callable[[dict[str, ndarray], ndarray, float], array_like]
|
Rate law \(r(\mathbf{c}, x, t)\). Receives the concentration dictionary, the spatial grid and the time, and returns a scalar or an array broadcastable to the concentration profiles. The rate law must be local (node-wise). |
required |
rate_derivatives
|
Mapping[str | Species, Callable]
|
Analytical partial derivatives |
None
|
dependencies
|
Sequence[str | Species]
|
Species the rate law depends on. Defaults to the species in
|
None
|
name
|
str
|
Human-readable label used in |
None
|
Examples:
Michaelis–Menten kinetics \(S \rightarrow P\) with \(r = V_{max} c_S / (K_M + c_S)\):
>>> import numpy as np
>>> from softpotato.reactions import CustomReaction
>>> mm = CustomReaction(
... stoichiometry={"S": -1, "P": +1},
... rate=lambda c, x, t: 2.0 * c["S"] / (1.0 + c["S"]),
... name="Michaelis-Menten",
... )
>>> c = {"S": np.array([1.0, 3.0]), "P": np.zeros(2)}
>>> mm.rates(c, None, 0.0)["P"]
array([1. , 1.5])
>>> jac = mm.jacobian(c, None, 0.0) # finite-difference derivatives
>>> bool(np.allclose(jac[("P", "S")], 2.0 / (1.0 + c["S"]) ** 2))
True
Supplying the analytical derivative instead:
>>> mm_exact = CustomReaction(
... stoichiometry={"S": -1, "P": +1},
... rate=lambda c, x, t: 2.0 * c["S"] / (1.0 + c["S"]),
... rate_derivatives={"S": lambda c, x, t: 2.0 / (1.0 + c["S"]) ** 2},
... )
>>> mm_exact.jacobian(c, None, 0.0)[("S", "S")]
array([-0.5 , -0.125])
Source code in src/softpotato/reactions/homogeneous.py
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 | |
stoichiometry
property
¶
Net stoichiometric coefficients {species: nu_i}.
rate
¶
rate_derivatives
¶
Analytical derivatives where supplied, finite differences otherwise.
Source code in src/softpotato/reactions/homogeneous.py
DimerizationReaction
¶
Bases: MassActionReaction
Irreversible dimerization \(2A \xrightarrow{k} A_2\).
With rate \(r = k c_A^2\):
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
monomer
|
str or Species
|
Monomer species \(A\). |
required |
dimer
|
str or Species or None
|
Dimer species \(A_2\); |
required |
k
|
float
|
Rate constant \(k \ge 0\). |
required |
Examples:
>>> import numpy as np
>>> from softpotato.reactions import DimerizationReaction
>>> rxn = DimerizationReaction("B", "B2", k=1.0)
>>> rxn.rates({"B": np.array([2.0]), "B2": np.array([0.0])}, None, 0.0)
{'B': array([-8.]), 'B2': array([4.])}
Source code in src/softpotato/reactions/homogeneous.py
DisproportionationReaction
¶
Bases: MassActionReaction
Irreversible disproportionation \(2B \xrightarrow{k} A + C\).
With rate \(r = k c_B^2\):
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactant
|
str or Species
|
Disproportionating species \(B\). |
required |
product_1
|
str or Species or None
|
Products \(A\) and \(C\) ( |
required |
product_2
|
str or Species or None
|
Products \(A\) and \(C\) ( |
required |
k
|
float
|
Rate constant \(k \ge 0\). |
required |
Examples:
>>> import numpy as np
>>> from softpotato.reactions import DisproportionationReaction
>>> rxn = DisproportionationReaction("B", "A", "C", k=0.5)
>>> rxn.rates({"B": np.array([2.0]), "A": np.zeros(1), "C": np.zeros(1)}, None, 0.0)
{'B': array([-4.]), 'A': array([2.]), 'C': array([2.])}
Source code in src/softpotato/reactions/homogeneous.py
FirstOrderReaction
¶
Bases: MassActionReaction
Irreversible first-order (unimolecular) reaction \(A \xrightarrow{k} B\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactant
|
str or Species
|
Decaying species \(A\). |
required |
product
|
str or Species or None
|
Product species \(B\). |
required |
k
|
float
|
First-order rate constant \(k \ge 0\) (s\ :sup: |
required |
Examples:
>>> import numpy as np
>>> from softpotato.reactions import FirstOrderReaction
>>> rxn = FirstOrderReaction("A", "B", k=0.5)
>>> rxn.rates({"A": np.array([2.0]), "B": np.array([0.0])}, None, 0.0)
{'A': array([-1.]), 'B': array([1.])}
Source code in src/softpotato/reactions/homogeneous.py
HomogeneousReaction
¶
Bases: ABC
Abstract base class for a single homogeneous (solution-phase) reaction.
A reaction is fully described by its net stoichiometry \(\nu_i\) and its rate law \(r(\mathbf{c}, x, t)\). The species source terms and the kinetic Jacobian follow automatically:
Subclass this to implement a new reaction type. Required members:
stoichiometry: mapping{species: net coefficient}.rate: the reaction rate \(r(\mathbf{c}, x, t)\).
Optional members:
dependencies: species whose concentrations the rate depends on (defaults to the species instoichiometry).rate_derivatives: analytical \(\partial r/\partial c_k\). The default implementation uses central finite differences, so it only needs overriding for speed or accuracy.
Rate laws must be local: the rate at grid node \(k\) may only depend on the concentrations at that same node (true for all homogeneous kinetics). This makes the kinetic Jacobian block-diagonal in space.
Examples:
A Michaelis–Menten enzymatic conversion \(S \rightarrow P\) written as a subclass:
>>> import numpy as np
>>> from softpotato.reactions import HomogeneousReaction
>>> class MichaelisMenten(HomogeneousReaction):
... def __init__(self, vmax, km):
... self.vmax, self.km = vmax, km
... @property
... def stoichiometry(self):
... return {"S": -1, "P": 1}
... def rate(self, c, x, t):
... s = np.asarray(c["S"], dtype=float)
... return self.vmax * s / (self.km + s)
>>> rxn = MichaelisMenten(vmax=2.0, km=1.0)
>>> rxn.rates({"S": np.array([1.0]), "P": np.array([0.0])}, None, 0.0)
{'S': array([-1.]), 'P': array([1.])}
>>> jac = rxn.jacobian({"S": np.array([1.0]), "P": np.array([0.0])}, None, 0.0)
>>> bool(np.isclose(jac[("S", "S")][0], -0.5))
True
Source code in src/softpotato/reactions/homogeneous.py
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 | |
stoichiometry
abstractmethod
property
¶
Net stoichiometric coefficients {species: nu_i} (products > 0).
dependencies
property
¶
Species whose concentrations the rate law depends on.
species
property
¶
All species involved (stoichiometry first, then extra dependencies).
rate
abstractmethod
¶
rate_derivatives
¶
Partial derivatives \(\partial r/\partial c_k\) for each dependency.
The default implementation uses vectorized central finite differences: because rate laws are local, perturbing an entire concentration profile at once yields the node-wise derivative at every grid point.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Mapping[str, array_like]
|
Concentrations keyed by species name. |
required |
x
|
array_like
|
Spatial grid (passed through to |
required |
t
|
float
|
Time. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Mapping |
Source code in src/softpotato/reactions/homogeneous.py
rates
¶
Species source terms \(R_i = \nu_i r\) for this reaction.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Mapping[str, array_like]
|
Concentrations keyed by species name. |
required |
x
|
array_like
|
Spatial grid. |
required |
t
|
float
|
Time. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Mapping |
Source code in src/softpotato/reactions/homogeneous.py
jacobian
¶
Node-wise kinetic Jacobian entries \(\partial R_i/\partial c_k\).
Returns:
| Type | Description |
|---|---|
dict[tuple[str, str], ndarray]
|
Mapping |
Source code in src/softpotato/reactions/homogeneous.py
__call__
¶
Alias for rates, usable directly as DiffusionProblem.reactions.
MassActionReaction
¶
Bases: HomogeneousReaction
General elementary reaction obeying the law of mass action.
The net stoichiometry is \(\nu = b - a\), so a species appearing on both sides (e.g. a catalyst) has its coefficients cancelled. Rate derivatives are computed analytically.
This class is also a convenient way to build custom elementary mechanisms with arbitrary stoichiometry.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactants
|
Mapping[str | Species, int]
|
Reactant species and their (positive integer) stoichiometric coefficients \(a_i\), which are also the reaction orders. |
required |
products
|
Mapping[str | Species, int]
|
Product species and their coefficients \(b_j\). May be empty (e.g. irreversible decay into untracked species). |
required |
k_f
|
float
|
Forward rate constant \(k_f \ge 0\). |
required |
k_b
|
float
|
Backward rate constant \(k_b \ge 0\). |
0.0
|
Examples:
>>> import numpy as np
>>> from softpotato.reactions import MassActionReaction
>>> rxn = MassActionReaction({"A": 1, "B": 2}, {"C": 1}, k_f=3.0)
>>> c = {"A": np.array([2.0]), "B": np.array([1.0]), "C": np.array([0.0])}
>>> rxn.rate(c, None, 0.0)
array([6.])
>>> rxn.rates(c, None, 0.0)
{'A': array([-6.]), 'B': array([-12.]), 'C': array([6.])}
Source code in src/softpotato/reactions/homogeneous.py
508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 | |
reactants
property
¶
Reactant stoichiometric coefficients (forward reaction orders).
products
property
¶
Product stoichiometric coefficients (backward reaction orders).
stoichiometry
property
¶
Net stoichiometric coefficients {species: b_i - a_i}.
rate
¶
Net mass-action rate \(k_f \prod c^{a} - k_b \prod c^{b}\).
Source code in src/softpotato/reactions/homogeneous.py
rate_derivatives
¶
Analytical derivatives \(\partial r / \partial c_k\).
Source code in src/softpotato/reactions/homogeneous.py
ReactionNetwork
¶
Collection of homogeneous reactions with summed rates and Jacobian.
Computes the combined species source terms
\(R_i = \sum_j \nu_{i,j} r_j\) and the kinetic Jacobian
\(\partial R_i / \partial c_k\). Instances are callable with the
signature expected by DiffusionProblem
(reactions(c_dict, grid, t) -> dict) and can be passed directly as
DiffusionProblem(..., reactions=network).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactions
|
Iterable[HomogeneousReaction]
|
Initial reactions. |
()
|
species
|
Sequence[str | Species]
|
Explicit species ordering used for the Jacobian and stoichiometry
matrix (e.g. |
None
|
Examples:
EC' catalytic mechanism with pseudo-first-order regeneration, solved with the SciPy Method of Lines solver:
>>> import numpy as np
>>> from softpotato.reactions import CatalyticReaction, ReactionNetwork
>>> from softpotato.solver import DiffusionProblem, DirichletBC, get_solver
>>> network = ReactionNetwork([CatalyticReaction("Red", "Ox", k=10.0)])
>>> problem = DiffusionProblem(
... grid=np.linspace(0.0, 1e-3, 41),
... diffusivity={"Ox": 1e-9, "Red": 1e-9},
... boundary_conditions={
... "Ox": (DirichletBC(0.0), DirichletBC(1.0)),
... "Red": (DirichletBC(1.0), DirichletBC(0.0)),
... },
... initial_conditions={"Ox": 1.0, "Red": 0.0},
... reactions=network,
... )
>>> result = get_solver("scipy_ivp").solve(problem, t_span=(0.0, 1.0))
>>> result.success
True
Kinetic Jacobian (species-major block ordering, sparse by default):
>>> c = {"Ox": np.ones(41), "Red": np.zeros(41)}
>>> J = network.jacobian(c, problem.grid, 0.0, species=problem.species)
>>> J.shape
(82, 82)
>>> network.local_jacobian(c, problem.grid, 0.0, species=problem.species)[:, :, 0]
array([[ 0., 10.],
[ 0., -10.]])
Source code in src/softpotato/reactions/homogeneous.py
967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 | |
species
property
¶
Ordered species names used for Jacobian and stoichiometry indexing.
stoichiometry_matrix
property
¶
Net stoichiometric matrix \(\nu_{i,j}\) of shape (n_species, n_reactions).
add
¶
Append a reaction to the network.
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If an explicit species order was given and the reaction involves a species not contained in it. |
Source code in src/softpotato/reactions/homogeneous.py
evaluate
¶
Combined species source terms \(R_i = \sum_j \nu_{i,j} r_j\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Mapping[str, array_like]
|
Concentration profiles keyed by species name. |
required |
x
|
array_like
|
Spatial grid. |
required |
t
|
float
|
Time. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Source term for every species in |
Source code in src/softpotato/reactions/homogeneous.py
__call__
¶
Alias for evaluate (DiffusionProblem.reactions signature).
local_jacobian
¶
local_jacobian(c: ConcentrationDict, x: Any, t: float, species: Sequence[str] | None = None) -> np.ndarray
Node-wise kinetic Jacobian \(J_{ik}(x) = \partial R_i / \partial c_k\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Mapping[str, array_like]
|
Concentration profiles keyed by species name. |
required |
x
|
array_like
|
Spatial grid. |
required |
t
|
float
|
Time. |
required |
species
|
Sequence[str]
|
Species ordering for the rows/columns; defaults to |
None
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array of shape |
Source code in src/softpotato/reactions/homogeneous.py
jacobian
¶
jacobian(c: ConcentrationDict, x: Any, t: float, species: Sequence[str] | None = None, sparse: bool = True) -> Any
Full kinetic Jacobian of the semi-discretized reaction term.
The state vector is ordered species-major,
\(\mathbf{y} = [c_{1,0}, \ldots, c_{1,N-1}, c_{2,0}, \ldots]\), so the
Jacobian is an (S*N, S*N) block matrix whose (i, k) block is the
diagonal matrix \(\mathrm{diag}(\partial R_i / \partial c_k)\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
c
|
Mapping[str, array_like]
|
1D concentration profiles keyed by species name. |
required |
x
|
array_like
|
Spatial grid. |
required |
t
|
float
|
Time. |
required |
species
|
Sequence[str]
|
Species ordering; defaults to |
None
|
sparse
|
bool
|
Return a |
True
|
Returns:
| Type | Description |
|---|---|
csr_matrix or ndarray
|
Kinetic Jacobian of shape |
Source code in src/softpotato/reactions/homogeneous.py
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 | |
ReversibleReaction
¶
Bases: MassActionReaction
Reversible first-order equilibrium \(A \underset{k_b}{\overset{k_f}{\rightleftharpoons}} B\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactant
|
str or Species
|
Species \(A\). |
required |
product
|
str or Species
|
Species \(B\). |
required |
k_f
|
float
|
Forward rate constant \(k_f \ge 0\). |
required |
k_b
|
float
|
Backward rate constant \(k_b \ge 0\). |
required |
Examples:
>>> from softpotato.reactions import ReversibleReaction
>>> rxn = ReversibleReaction("A", "B", k_f=10.0, k_b=2.0)
>>> rxn.equilibrium_constant
5.0
>>> rxn2 = ReversibleReaction.from_equilibrium("A", "B", K=5.0, k_f=10.0)
>>> rxn2.k_b
2.0
Source code in src/softpotato/reactions/homogeneous.py
equilibrium_constant
property
¶
Equilibrium constant \(K = k_f / k_b\) (inf if \(k_b = 0\)).
from_equilibrium
classmethod
¶
from_equilibrium(reactant: SpeciesLike, product: SpeciesLike, K: float, k_f: float) -> ReversibleReaction
Create from the equilibrium constant \(K = k_f / k_b\) and \(k_f\).
Source code in src/softpotato/reactions/homogeneous.py
SecondOrderReaction
¶
Bases: MassActionReaction
Irreversible bimolecular reaction \(A + B \xrightarrow{k} C\).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reactant_1
|
str or Species
|
Reacting species \(A\) and \(B\) (must differ; use
|
required |
reactant_2
|
str or Species
|
Reacting species \(A\) and \(B\) (must differ; use
|
required |
product
|
str or Species or None
|
Product \(C\); |
required |
k
|
float
|
Second-order rate constant \(k \ge 0\)
(concentration\ :sup: |
required |
Examples:
>>> import numpy as np
>>> from softpotato.reactions import SecondOrderReaction
>>> rxn = SecondOrderReaction("A", "B", "C", k=2.0)
>>> c = {"A": np.array([1.0]), "B": np.array([3.0]), "C": np.array([0.0])}
>>> rxn.rates(c, None, 0.0)
{'A': array([-6.]), 'B': array([-6.]), 'C': array([6.])}
Source code in src/softpotato/reactions/homogeneous.py
CustomMechanism
¶
Bases: Mechanism
User-defined chemical or electrochemical reaction mechanism.
Extends Mechanism with optional functional hooks so users can
define arbitrary mechanisms without subclassing:
reactions: built-in orCustomReactionobjects (analytical Jacobian assembled automatically).rates: arbitrary user callablerates(c, x, t) -> dict[str, ndarray]added on top of (or used instead of)reactions.local_jacobian_func: optional analytical node-wise Jacobianjac(c, x, t, species) -> ndarrayof shape(S, S, N)forrates. If omitted, vectorized central finite differences are computed automatically (only2 * Sevaluations ofratesper step).electrochemical_steps: list ofElectrochemicalStepobjects.boundary_conditions_func: optional custom callable returning the boundary condition dictionary forboundary_conditions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
SpeciesCollection or Iterable[Species]
|
Chemical species in the mechanism. |
required |
reactions
|
ReactionNetwork, HomogeneousReaction, or Iterable[HomogeneousReaction]
|
Structured homogeneous reactions. |
None
|
rates
|
Callable[[ConcentrationDict, Any, float], dict[str, ndarray]]
|
Custom homogeneous rate law returning |
None
|
local_jacobian_func
|
Callable[[ConcentrationDict, Any, float, Sequence[str]], ndarray]
|
Analytical node-wise Jacobian for |
None
|
electrochemical_steps
|
Iterable[ElectrochemicalStep]
|
Heterogeneous electron-transfer steps at electrode boundaries. |
()
|
boundary_conditions_func
|
Callable
|
Custom boundary condition builder invoked by |
None
|
name
|
str
|
Human-readable label for this mechanism. |
None
|
Examples:
Custom nonlinear mechanism with an arbitrary rate law and automatic local Jacobian:
>>> import numpy as np
>>> from softpotato.reactions import CustomMechanism, Species
>>> a = Species("A", diffusivity=1e-9, bulk_concentration=1.0)
>>> b = Species("B", diffusivity=1e-9, bulk_concentration=0.0)
>>> mech = CustomMechanism(
... species=[a, b],
... rates=lambda c, x, t: {
... "A": -5.0 * c["A"] / (1.0 + c["A"]),
... "B": +5.0 * c["A"] / (1.0 + c["A"]),
... },
... name="MichaelisMenten",
... )
>>> c = {"A": np.array([1.0, 1.0]), "B": np.array([0.0, 0.0])}
>>> mech(c, None, 0.0)["B"]
array([2.5, 2.5])
Source code in src/softpotato/reactions/mechanisms.py
710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 | |
evaluate
¶
Evaluate combined rates from structured reactions and custom rate callable.
Source code in src/softpotato/reactions/mechanisms.py
local_jacobian
¶
local_jacobian(c: ConcentrationDict, x: Any, t: float, species: Sequence[str] | None = None) -> np.ndarray
Compute node-wise kinetic Jacobian combining structured and custom terms.
Source code in src/softpotato/reactions/mechanisms.py
boundary_conditions
¶
boundary_conditions(potential: float | Callable[[float], float] | None = None, *, electrode_side: Literal['left', 'right'] | None = None, bulk_bc: Literal['dirichlet', 'neumann'] = 'dirichlet', overrides: Mapping[str, tuple[BoundaryCondition, BoundaryCondition]] | None = None, **bc_kwargs: Any) -> dict[str, tuple[BoundaryCondition, BoundaryCondition]]
Build boundary conditions using boundary_conditions_func if provided.
Source code in src/softpotato/reactions/mechanisms.py
ElectrochemicalStep
dataclass
¶
Specification of a single interfacial electron-transfer step.
Represents the heterogeneous redox reaction at an electrode boundary:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ox
|
Species or str
|
Oxidized species involved in the charge-transfer step. |
required |
red
|
Species or str
|
Reduced species involved in the charge-transfer step. |
required |
kinetics
|
HeterogeneousKinetics or NernstianEquilibrium
|
Interfacial charge-transfer model ( |
required |
electrode_side
|
('left', 'right')
|
Domain boundary where this electrochemical step takes place. |
'left'
|
bulk_bc
|
('dirichlet', 'neumann')
|
Default boundary condition type applied at the opposite (bulk) boundary. |
'dirichlet'
|
Source code in src/softpotato/reactions/mechanisms.py
Mechanism
¶
Modular chemical and electrochemical reaction mechanism.
Encapsulates species metadata, homogeneous solution-phase reactions, and
optional heterogeneous electrode charge-transfer steps into a single
reusable object that integrates directly with
DiffusionProblem and
ScipyIVPSolver.
Subclasses can override evaluate (and optionally
local_jacobian and boundary_conditions) to implement custom
mechanisms. When a subclass overrides evaluate without overriding
local_jacobian, the node-wise local Jacobian is computed
automatically via vectorized central finite differences, preserving the
block-diagonal sparsity structure required by stiff solvers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
SpeciesCollection or Iterable[Species]
|
Chemical species participating in the mechanism. |
required |
reactions
|
ReactionNetwork, HomogeneousReaction, or Iterable[HomogeneousReaction]
|
Homogeneous solution-phase reaction(s). |
None
|
electrochemical_steps
|
Iterable[ElectrochemicalStep]
|
Heterogeneous electron-transfer steps occurring at electrode boundaries. |
()
|
name
|
str
|
Human-readable label for the mechanism. |
None
|
Examples:
EC mechanism with Nernstian electron transfer and first-order decay:
>>> import numpy as np
>>> from softpotato.reactions import (
... ElectrochemicalStep,
... FirstOrderReaction,
... Mechanism,
... NernstianEquilibrium,
... Species,
... )
>>> ox = Species("Ox", diffusivity=1e-9, bulk_concentration=1.0, n_electrons=1)
>>> red = Species("Red", diffusivity=1e-9, bulk_concentration=0.0, n_electrons=-1)
>>> prod = Species("P", diffusivity=1e-9, bulk_concentration=0.0, n_electrons=0)
>>> ec = Mechanism(
... species=[ox, red, prod],
... reactions=[FirstOrderReaction(red, prod, k=10.0)],
... electrochemical_steps=[
... ElectrochemicalStep(ox, red, NernstianEquilibrium(E0=0.0, n=1))
... ],
... name="EC",
... )
>>> problem = ec.create_problem(np.linspace(0.0, 1e-3, 31), potential=-0.2)
>>> problem.species
['Ox', 'Red', 'P']
Source code in src/softpotato/reactions/mechanisms.py
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 | |
species
property
¶
Ordered SpeciesCollection of species in the mechanism.
electrochemical_steps
property
¶
Tuple of registered ElectrochemicalStep objects.
add_reaction
¶
Add a homogeneous reaction to the mechanism (returns self).
add_electrochemical_step
¶
Register an interfacial ElectrochemicalStep (returns self).
Source code in src/softpotato/reactions/mechanisms.py
evaluate
¶
Evaluate combined homogeneous species source terms \(R_i(c, x, t)\).
__call__
¶
Alias for evaluate (DiffusionProblem.reactions signature).
local_jacobian
¶
local_jacobian(c: ConcentrationDict, x: Any, t: float, species: Sequence[str] | None = None) -> np.ndarray
Node-wise kinetic Jacobian \(J_{ik}(x) = \partial R_i / \partial c_k\).
Returns an array of shape (n_species, n_species, *profile_shape).
Source code in src/softpotato/reactions/mechanisms.py
jacobian
¶
jacobian(c: ConcentrationDict, x: Any, t: float, species: Sequence[str] | None = None, sparse: bool = True) -> Any
Full (S*N, S*N) sparse or dense kinetic Jacobian in species-major order.
Source code in src/softpotato/reactions/mechanisms.py
boundary_conditions
¶
boundary_conditions(potential: float | Callable[[float], float] | None = None, *, electrode_side: Literal['left', 'right'] | None = None, bulk_bc: Literal['dirichlet', 'neumann'] = 'dirichlet', overrides: Mapping[str, tuple[BoundaryCondition, BoundaryCondition]] | None = None, **bc_kwargs: Any) -> dict[str, tuple[BoundaryCondition, BoundaryCondition]]
Build boundary conditions for all species in the mechanism.
Electroactive species from electrochemical_steps receive
matched interfacial boundary conditions via
create_electrochemical_bcs. Non-electroactive
species receive zero-flux (NeumannBC) at the
electrode boundary and bulk concentration (or zero-flux) at the bulk
boundary.
Source code in src/softpotato/reactions/mechanisms.py
464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 | |
create_problem
¶
create_problem(grid: ndarray | Mesh1D, *, potential: float | Callable[[float], float] | None = None, boundary_conditions: Mapping[str, tuple[BoundaryCondition, BoundaryCondition]] | None = None, geometry: Geometry | str = 'planar', diffusivity: Mapping[str, float] | None = None, initial_conditions: Mapping[str, float | ndarray | Callable[[ndarray], ndarray]] | None = None, n_electrons: int | dict[str, int] | None = None, electrodes: Literal['left', 'right', 'both'] = 'left', electrode_side: Literal['left', 'right', 'both'] | None = None, bulk_bc: Literal['dirichlet', 'neumann'] = 'dirichlet', bc_overrides: Mapping[str, tuple[BoundaryCondition, BoundaryCondition]] | None = None, **bc_kwargs: Any) -> DiffusionProblem
Create a DiffusionProblem from this mechanism.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
grid
|
ndarray or Mesh1D
|
1D spatial grid or mesh. |
required |
potential
|
float or Callable[[float], float]
|
Electrode potential \(E\) or waveform \(E(t)\) used to build
boundary conditions when |
None
|
boundary_conditions
|
Mapping[str, tuple[BoundaryCondition, BoundaryCondition]]
|
Explicit boundary conditions per species (overrides automatic generation). |
None
|
geometry
|
Geometry or str
|
Coordinate geometry passed to |
'planar'
|
diffusivity
|
Mapping[str, float]
|
Optional diffusivity overrides. |
None
|
initial_conditions
|
Mapping
|
Optional initial condition overrides. |
None
|
n_electrons
|
int or dict[str, int]
|
Optional electron transfer count overrides. |
None
|
electrodes
|
('left', 'right', 'both')
|
Active electrode boundary for current evaluation. |
'left'
|
electrode_side
|
('left', 'right', 'both')
|
Alias for |
'left'
|
bulk_bc
|
('dirichlet', 'neumann')
|
Bulk boundary condition type when auto-generating BCs. |
'dirichlet'
|
bc_overrides
|
Mapping
|
Per-species boundary condition overrides applied on top of generated BCs. |
None
|
**bc_kwargs
|
Any
|
Additional keyword arguments forwarded to |
{}
|
Returns:
| Type | Description |
|---|---|
DiffusionProblem
|
Configured reaction-diffusion problem ready for solving. |
Source code in src/softpotato/reactions/mechanisms.py
547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 | |
Species
dataclass
¶
Encapsulation of a chemical species and its physicochemical properties.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Unique chemical identifier (e.g., |
required |
diffusivity
|
float
|
Standard diffusion coefficient \(D_i > 0\) in \(\text{m}^2/\text{s}\) (or \(\text{cm}^2/\text{s}\) depending on system units). |
required |
bulk_concentration
|
float
|
Bulk / resting concentration \(c_i^* \ge 0\) (\(\text{mol/m}^3\) or M). |
0.0
|
charge
|
int
|
Formal ionic charge \(z_i\) (e.g., |
0
|
standard_potential
|
float or None
|
Standard formal reduction potential \(E^{0\prime}\) in Volts vs. reference electrode. |
None
|
n_electrons
|
int or None
|
Number of electrons transferred in the primary redox reaction. |
None
|
Examples:
>>> from softpotato.reactions import Species
>>> ox = Species(
... name="Ox",
... diffusivity=1e-9,
... bulk_concentration=1.0,
... charge=-3,
... standard_potential=0.2,
... n_electrons=1,
... )
>>> ox.name
'Ox'
>>> ox.diffusivity
1e-09
Source code in src/softpotato/reactions/species.py
SpeciesCollection
¶
Bases: Mapping[str, Species]
Ordered container and registry managing a group of Species objects.
Supports dict-like lookup by species name (collection["Ox"]) and provides
convenience property exports compatible with
DiffusionProblem.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
Iterable[Species]
|
Initial collection of |
()
|
Examples:
>>> from softpotato.reactions import Species, SpeciesCollection
>>> ox = Species("Ox", diffusivity=1e-9, bulk_concentration=1.0, n_electrons=1)
>>> red = Species("Red", diffusivity=1.2e-9, bulk_concentration=0.0, n_electrons=-1)
>>> col = SpeciesCollection([ox, red])
>>> col["Ox"].diffusivity
1e-09
>>> col.diffusivities
{'Ox': 1e-09, 'Red': 1.2e-09}
>>> col.initial_conditions
{'Ox': 1.0, 'Red': 0.0}
>>> col.n_electrons
{'Ox': 1, 'Red': -1}
Source code in src/softpotato/reactions/species.py
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | |
diffusivities
property
¶
Mapping from species name to diffusion coefficient \(D_i\).
bulk_concentrations
property
¶
Mapping from species name to bulk concentration \(c_i^*\).
initial_conditions
property
¶
Mapping from species name to initial (bulk) concentration for DiffusionProblem.
charges
property
¶
Mapping from species name to formal ionic charge \(z_i\).
standard_potentials
property
¶
Mapping from species name to standard potential \(E^{0\prime}\) for species defining it.
n_electrons
property
¶
Mapping from species name to electron transfer count for species defining n_electrons.
add
¶
Register a Species instance in the collection.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
species
|
Species
|
Species object to add. |
required |
Raises:
| Type | Description |
|---|---|
TypeError
|
If |
ValueError
|
If a species with the same name is already registered. |
Source code in src/softpotato/reactions/species.py
remove
¶
Remove and return a species by name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the species to remove. |
required |
Returns:
| Type | Description |
|---|---|
Species
|
The removed species instance. |
Raises:
| Type | Description |
|---|---|
KeyError
|
If |
Source code in src/softpotato/reactions/species.py
create_electrochemical_bcs
¶
create_electrochemical_bcs(kinetics: HeterogeneousKinetics | NernstianEquilibrium, potential: float | Callable[[float], float], ox: Species | str = 'Ox', red: Species | str = 'Red', *, D_ox: float | None = None, D_red: float | None = None, c_ox_bulk: float | None = None, c_red_bulk: float | None = None, c_ox_surface: float | Callable[[float], float] | None = None, c_red_surface: float | Callable[[float], float] | None = None, electrode_side: Literal['left', 'right'] = 'left', bulk_bc: Literal['dirichlet', 'neumann'] = 'dirichlet') -> dict[str, tuple[BoundaryCondition, BoundaryCondition]]
Construct matched (left_bc, right_bc) boundary conditions for a redox pair.
Generates a dictionary mapping {ox_name: (left_bc, right_bc), red_name: (left_bc, right_bc)}
compatible with DiffusionProblem:
- For
NernstianEquilibrium: generates time-dependentDirichletBCconditions enforcing the Nernst equilibrium surface concentrations at the electrode boundary. -
For
HeterogeneousKinetics(e.g.,ButlerVolmer,TafelKinetics): generates matchedNeumannBCflux boundary conditions satisfying flux continuity \(J_{\text{Ox}} = -J_{\text{Red}}\):\[ \left.\frac{\partial c_{\text{Ox}}}{\partial x}\right|_{\text{left}} = +\frac{J(t)}{D_{\text{Ox}}}, \qquad \left.\frac{\partial c_{\text{Red}}}{\partial x}\right|_{\text{left}} = -\frac{J(t)}{D_{\text{Red}}} \]
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
kinetics
|
HeterogeneousKinetics or NernstianEquilibrium
|
Interfacial charge-transfer model or Nernstian equilibrium helper. |
required |
potential
|
float or Callable[[float], float]
|
Constant electrode potential \(E\) or time-dependent waveform \(E(t)\) in Volts. |
required |
ox
|
Species or str
|
Oxidized species object or name. |
'Ox'
|
red
|
Species or str
|
Reduced species object or name. |
'Red'
|
D_ox
|
float
|
Diffusion coefficient of oxidized species. Inferred from |
None
|
D_red
|
float
|
Diffusion coefficient of reduced species. Inferred from |
None
|
c_ox_bulk
|
float
|
Bulk concentration of oxidized species (inferred from |
None
|
c_red_bulk
|
float
|
Bulk concentration of reduced species (inferred from |
None
|
c_ox_surface
|
float or Callable[[float], float]
|
Prescribed surface concentration \(c_{\text{Ox}}(0, t)\) used when
evaluating Neumann kinetic fluxes (defaults to |
None
|
c_red_surface
|
float or Callable[[float], float]
|
Prescribed surface concentration \(c_{\text{Red}}(0, t)\) used when
evaluating Neumann kinetic fluxes (defaults to |
None
|
electrode_side
|
('left', 'right')
|
Domain boundary acting as the working electrode. |
'left'
|
bulk_bc
|
('dirichlet', 'neumann')
|
Boundary condition type at the opposite (bulk) boundary: |
'dirichlet'
|
Returns:
| Type | Description |
|---|---|
dict[str, tuple[BoundaryCondition, BoundaryCondition]]
|
Dictionary ready to pass to |
Examples:
>>> from softpotato.reactions import (
... NernstianEquilibrium,
... Species,
... create_electrochemical_bcs,
... )
>>> ox = Species("Ox", diffusivity=1e-9, bulk_concentration=1.0)
>>> red = Species("Red", diffusivity=1e-9, bulk_concentration=0.0)
>>> eq = NernstianEquilibrium(E0=0.0, n=1)
>>> bcs = create_electrochemical_bcs(eq, potential=0.0, ox=ox, red=red)
>>> bcs["Ox"][0].evaluate(0.0), bcs["Red"][0].evaluate(0.0)
(0.5, 0.5)
Source code in src/softpotato/reactions/heterogeneous.py
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 | |