diff --git a/examples/electrochemistry/porous_electrode.py b/examples/electrochemistry/porous_electrode.py index e1f21cc..a7b4b29 100644 --- a/examples/electrochemistry/porous_electrode.py +++ b/examples/electrochemistry/porous_electrode.py @@ -14,32 +14,51 @@ # # A porous electrode benchmark # -# This demo solves the porous electrode of the benchmark of -# {cite}`Wang2025`, in one, two and three dimensions, and validates it -# against a reference solution of the problem reduced to one dimension, see -# {ref}`electrode-validation`. It shows how to: -# -# - Build a battery model with sumfem's -# {ref}`models of electrochemistry `, then -# with its {ref}`generic components ` alone, and in +# This example solves the porous-electrode benchmark of +# {cite}`Wang2025` in one, two and three dimensions. +# The numerical solutions are verified against a high-accuracy +# reference solution of the one-dimensional problem, as described +# in {ref}`electrode-validation`. It demonstrates how to: +# +# - Model electronic and ionic charge transport in a porous electrode +# using {ref}`SUMFEM's electrochemistry components `, +# {ref}`generic components `, and # {ref}`plain UFL ` -# - Give its parameters units, check them, and solve the model scaled -# - Reduce the model to +# - Specify physical units, check dimensional consistency, and +# scale the governing equations +# - Reduce the coupled charge-conservation equations to # {ref}`one equation for the overpotential ` # # ## The model # -# In porous electrode theory {cite}`NewmanTiedemann1975`, a macrohomogeneous -# model, the pores of the electrode are not resolved. Averaged over a -# volume, the solid and the electrolyte become two continua that coexist -# at every point, each with its own potential: the dual-continuum model of -# {cite}`Wang2025`. In an electrode of width $W$, the solid has the potential -# $\phi_e$ and the conductivity $\sigma$, and the electrolyte the potential -# $\phi_l$ and the conductivity $\kappa$. The reaction at their interface, -# of specific area $a$, carries the current $a\, i(\eta)$ from the solid to -# the electrolyte, by Butler-Volmer kinetics -# {cite}`Butler1924,ErdeyGruzVolmer1930` with the exchange current density -# $i_0$, +# In macrohomogeneous porous-electrode theory +# {cite}`NewmanTiedemann1975`, the microscopic pore structure is +# not explicitly resolved. Instead, volume averaging represents +# the solid and electrolyte phases as two overlapping continua, +# each with its own electric potential. This approach is known +# as the dual-continuum model {cite}`Wang2025`. +# This benchmark considers a secondary current distribution +# model, in which electronic and ionic conduction are coupled +# through Butler-Volmer reaction kinetics. Species transport +# and concentration variations are neglected. This approximation +# is appropriate when concentration gradients and their effects +# on electrochemical kinetics are sufficiently small. +# The effective conductivities, exchange current density, +# equilibrium potential, and temperature are prescribed constants. +# The electrode has a thickness $W$. The solid-phase electric +# potential is $\phi_e$, with effective electronic conductivity +# $\sigma$, while the electrolyte potential is $\phi_l$, with +# effective ionic conductivity $\kappa$. +# +# Electrochemical reactions at the solid-electrolyte interface +# transfer current between the two phases. The specific +# interfacial surface area $a$ is the electrochemically active +# interface area per unit electrode volume. +# The volumetric interfacial current-transfer term is +# $a\,i(\eta)$, where $i(\eta)$ follows Butler-Volmer kinetics +# {cite}`Butler1924,ErdeyGruzVolmer1930`. Its sign determines +# the direction of current transfer. The exchange current +# density is denoted by $i_0$: # # $$ # \begin{aligned} @@ -50,10 +69,16 @@ # \end{aligned} # $$ (electrode) # -# with the overpotential $\eta$ and the equilibrium potential $E_{eq}$. The -# applied current density $j$ enters the solid through the current -# collector at $x = 0$, and leaves the electrolyte through the membrane at -# $x = W$: +# Here, $\eta$ is the overpotential and $E_{eq}$ is the +# equilibrium potential. The Butler-Volmer expression assumes +# a one-electron reaction with symmetric anodic and cathodic +# charge-transfer coefficients of 0.5. +# A signed applied current density $j$ is prescribed at the +# current collector ($x=0$) and membrane ($x=W$). +# For positive $j$, conventional current enters the electrode +# through the solid at the current collector and leaves +# through the electrolyte at the membrane. For negative $j$, +# as used in this benchmark, the current direction is reversed: # # $$ # \begin{aligned} @@ -62,7 +87,8 @@ # \end{aligned} # $$ (electrode-currents) # -# and no current crosses the other boundaries. +# Here, $n$ denotes the outward unit normal to the electrode +# boundary. No current crosses the remaining boundaries. # # We import the relevant packages required for this modelling @@ -104,8 +130,9 @@ # # The electrode is a rectangle of width $W$ and height $H$, of # quadrilaterals, in the precision of PETSc's real type, which assembly and -# solvers use. The potentials are of degree two. The number of cells across -# the electrode is shared with the meshes of the other dimensions below: +# solvers use. The potentials are of degree two. +# The number of cells across the electrode is also used +# in the one- and three-dimensional examples below: cell_name = "quadrilateral" degree = 2 @@ -162,9 +189,9 @@ def mark( def create_parameters(domain: symbolic.Domain) -> dict[str, symbolic.Parameter]: """The parameters of the benchmark on a domain.""" values = { - "sigma": (103.1891, S / m, "Conductivity of the solid"), - "kappa": (5.9514, S / m, "Conductivity of the electrolyte"), - "a": (1.64e4, 1 / m, "Specific area"), + "sigma": (103.1891, S / m, "Effective electronic conductivity of the solid"), + "kappa": (5.9514, S / m, "Effective ionic conductivity of the electrolyte"), + "a": (1.64e4, 1 / m, "Specific interfacial surface area"), "i0": (2.7657, A / m**2, "Exchange current density"), "E_eq": (-0.1609, V, "Equilibrium potential"), "j": (-400.0, A / m**2, "Applied current density"), @@ -190,45 +217,67 @@ def create_potentials( maps = discrete.DomainMaps({domain: marked}) # - -# The model is written three times below, from the top down: with the -# models of electrochemistry, with sumfem's generic components alone, and in -# plain UFL. Each gives the same solution. +# The same mathematical model is implemented below in three +# ways: using dedicated electrochemistry components, generic +# SUMFEM components, and plain UFL. +# All three implementations produce equivalent numerical solutions. # # (electrode-electrochemistry)= # ## With the models of electrochemistry # -# {py:mod}`sumfem.electrochemistry` holds the models of electrochemistry. An -# {py:class}`~sumfem.electrochemistry.Electrolyte` holds Faraday's constant -# $F$, the gas constant $R$ and the temperature $T$, as parameters with -# units that the models built on it share; here it is of uniform -# composition, without ions. -# -# The model is a sum of components, one per term of {eq}`electrode` and -# {eq}`electrode-currents`. Each takes a name first, which identifies it in -# messages and prefixes the parameters it creates, then the field whose -# equation it adds to, then its coefficients: -# -# - {py:class}`Conduction `, the Ohmic conduction -# term of each equation of {eq}`electrode`, takes the potential and the -# conductivity. A component in the volume acts on the whole domain unless -# it is given the markers of the cells to act on, an optional argument, so -# the conductions need none here. -# - {py:class}`~sumfem.electrochemistry.ExchangeCurrentButlerVolmer`, the -# reaction, takes the potentials of the solid and of the electrolyte and -# the electrolyte, and adds the current $a\, i(\eta)$ to the equation of -# the solid and subtracts it from that of the electrolyte. Its exchange -# current density, equilibrium potential, transfer coefficients and -# specific area are keyword arguments. -# - {py:class}`BoundaryFlux `, the applied -# current of {eq}`electrode-currents`, takes the potential and the current -# that enters through the boundary per unit area: $j$ into the solid on -# the current collector, and $-j$ into the electrolyte on the membrane. A -# flux acts on a part of the boundary, so its markers are required: a -# tuple of the names given by `mark` above, as a component may act on -# several marked parts. -# -# The conductions and the applied currents are generic components of -# {py:mod}`sumfem.models`, the reaction a model of electrochemistry: +# {py:mod}`sumfem.electrochemistry` provides reusable +# components for electrochemical models. +# An {py:class}`~sumfem.electrochemistry.Electrolyte` +# object provides shared electrochemical parameters, +# including Faraday's constant $F$, the gas constant +# $R$, and temperature $T$. +# In this benchmark, individual ionic species are not +# explicitly represented. Ionic conduction is instead +# modeled using a prescribed effective electrolyte +# conductivity. +# +# The model is assembled by combining components for +# solid-phase conduction, electrolyte conduction, +# interfacial electrochemical reactions, and applied +# boundary currents. Each component contributes one +# or more terms to the governing equations +# {eq}`electrode` and {eq}`electrode-currents`. +# Each component is assigned a name for identification +# and for naming any parameters it creates. +# +# - {py:class}`Conduction ` +# represents Ohmic conduction for a specified electric +# potential and conductivity. By default, it acts +# throughout the computational domain, but optional +# cell markers can restrict it to selected regions. +# +# - {py:class}`~sumfem.electrochemistry.ExchangeCurrentButlerVolmer` +# takes the solid and electrolyte potentials, together +# with an `Electrolyte` object containing shared +# electrochemical parameters. It introduces the +# interfacial reaction current $a\,i(\eta)$ into the +# two charge-conservation equations with opposite +# signs, ensuring conservation of total current. +# The exchange current density, equilibrium potential, +# charge-transfer coefficients, and specific +# interfacial surface area are keyword arguments. +# +# - {py:class}`BoundaryFlux ` +# prescribes the signed inward current density on +# selected boundaries. The solid-phase component +# receives $j$ at the current collector, while the +# electrolyte-phase component receives $-j$ at the +# membrane. For positive $j$, conventional current +# flows from the current collector toward the membrane. +# For negative $j$, as used in this benchmark, +# the current direction is reversed. +# Boundary markers specify where each flux is applied. +# +# The conduction and boundary-current terms are +# implemented using generic components from +# {py:mod}`sumfem.models`, while the electrochemical +# reaction is implemented using +# {py:class}`~sumfem.electrochemistry.ExchangeCurrentButlerVolmer`: # + @@ -279,8 +328,10 @@ def solve( disc = solve(model, maps) # - -# The reaction carries all of the applied current, which enters through the -# current collector of height $H$: +# The integral of the volumetric interfacial current density +# over the electrode must equal the signed applied current +# per unit out-of-plane depth, $jH$, where $H$ is the height +# of the current collector: # + reacted = disc.assemble_scalar(reaction.source() * domain.dx()) @@ -289,8 +340,12 @@ def solve( assert np.isclose(reacted, applied, rtol=1e-8) # - -# The overpotential varies across the electrode alone. It is plotted with -# [PyVista](https://pyvista.org), gathered from all processes by +# Because the geometry, material properties, and boundary +# conditions are uniform in the $y$-direction, the +# overpotential depends only on the coordinate $x$ across +# the electrode thickness. It is visualized using +# [PyVista](https://pyvista.org), with data gathered from +# all processes by # {py:func}`gather_grid `: @@ -340,11 +395,14 @@ def relative_difference(disc: discrete.Discretization, reference: np.ndarray) -> # (electrode-generic)= # ## With generic components # -# Without the models of electrochemistry, $F$, $R$ and $T$ are parameters -# of their own: the exact constants +# When using generic SUMFEM components, Faraday's constant +# $F$, the gas constant $R$, and temperature $T$ are defined +# explicitly. The constants # {py:data}`FARADAY ` and -# {py:data}`GAS_CONSTANT ` in SI units, and the -# room temperature, the default of the electrolyte above: +# {py:data}`GAS_CONSTANT ` +# are expressed in SI units, and the temperature is set +# to 298.15 K, matching the default used by the +# `Electrolyte` object above: # + C, J, K, mol = syu.coulomb, syu.joule, syu.kelvin, syu.mol @@ -357,10 +415,14 @@ def relative_difference(disc: discrete.Discretization, reference: np.ndarray) -> T = symbolic.Parameter("T", domain, default=298.15, unit=K) # - -# The reaction is then a source in each equation of {eq}`electrode`, with the -# current density of {py:func}`butler_volmer `, -# which takes the overpotential over the thermal voltage $R T / F$, and its -# transfer coefficients one half by default: +# The electrochemical reaction is represented by source +# terms in the two charge-conservation equations of +# {eq}`electrode`. The interfacial current density is +# calculated using +# {py:func}`butler_volmer `, +# which takes the overpotential normalized by the thermal +# voltage $RT/F$. By default, both charge-transfer +# coefficients are 0.5: # + eta = phi_e_field - phi_l_field - parameters["E_eq"] @@ -381,9 +443,15 @@ def relative_difference(disc: discrete.Discretization, reference: np.ndarray) -> # (electrode-ufl)= # ## In plain UFL # -# The weak form of {eq}`electrode` and {eq}`electrode-currents` is written in -# plain UFL, as in a UFL file of DOLFINx, on a mesh of the same cells, with -# the current collector and the membrane as the boundary markers 1 and 2: +# The governing equations and boundary terms of +# {eq}`electrode` and {eq}`electrode-currents` +# are expressed directly using UFL, without predefined +# SUMFEM physics components. The resulting residual is +# then passed to `symbolic.UFLResidual` for discretization +# and solution through SUMFEM. +# The symbolic mesh uses the same cell type as the +# two-dimensional electrode, with boundary markers +# 1 and 2 representing the current collector and membrane: # + mesh = ufl.Mesh(element("Lagrange", cell_name, 1, shape=(2,))) @@ -510,9 +578,12 @@ def across( # (electrode-one-equation)= # ## One equation for the overpotential # -# With constant conductivities, the two potentials can be eliminated: -# subtracting the equations of {eq}`electrode` divided by their -# conductivities gives one equation for the overpotential, +# When the effective conductivities and equilibrium +# potential are spatially constant, the two +# charge-conservation equations can be combined into +# a single equation for the overpotential. +# Dividing the equations in {eq}`electrode` by their +# respective conductivities and subtracting them gives # # $$ # \begin{aligned} @@ -524,14 +595,17 @@ def across( # \end{aligned} # $$ (overpotential-equation) # -# a diffusion with a source. The reduction has no model of -# electrochemistry: it is one equation for one field, derived for constant -# conductivities. It is written, as in {ref}`electrode-generic`, with the -# generic components {py:class}`Diffusion `, -# {py:class}`Source ` and -# {py:class}`BoundaryFlux ` and the parameters -# $F$, $R$ and $T$ defined there, on the rectangle. Integrating it shows -# that the reaction carries the applied current here too: +# This is a nonlinear reaction-diffusion equation for +# the overpotential. Although the reduced equation +# retains Butler-Volmer reaction kinetics, it does not +# require dedicated SUMFEM electrochemistry components. +# It is implemented using the generic components +# {py:class}`Diffusion `, +# {py:class}`Source `, and +# {py:class}`BoundaryFlux `, +# together with the parameters $F$, $R$, and $T$ +# defined above. Integrating the reaction term provides +# another check of the applied current balance: # + eta_field = symbolic.Field("eta", domain, ("Lagrange", degree), unit=V, scale=0.1) @@ -560,12 +634,15 @@ def across( # - # (electrode-validation)= -# ## Validation +# ## Numerical verification # -# The solution depends on $x$ alone. Across the electrode, the current -# $i_l$ of the electrolyte grows from zero at the current collector to $j$ -# at the membrane, and with constant conductivities the overpotential and -# $i_l$ solve +# The solution depends only on $x$. Across the electrode, +# the electrolyte current density $i_l$ varies from zero +# at the current collector to the prescribed current +# density $j$ at the membrane. Its magnitude increases +# toward the membrane. For constant conductivities, +# the overpotential $\eta$ and the electrolyte current +# density $i_l$ satisfy # # $$ # \begin{aligned} @@ -574,10 +651,12 @@ def across( # \end{aligned} # $$ (electrode-reduced) # -# a boundary value problem for {py:func}`scipy.integrate.solve_bvp`, with -# the values of the parameters and of the electrolyte in SI. It starts from -# a mesh of the given points, refines it until the residual is below the -# tolerance, and stops with an error past the given number of nodes: +# This system forms a boundary-value problem solved using +# {py:func}`scipy.integrate.solve_bvp`, with all physical +# parameters expressed in SI units. The solver starts +# from a prescribed initial mesh and adaptively refines +# it to satisfy the specified numerical tolerances, +# subject to a maximum number of mesh nodes: # + @@ -616,11 +695,12 @@ def boundary(y0: np.ndarray, yW: np.ndarray) -> np.ndarray: # - -# The tolerance is far below the errors of the finite elements, so that the -# reference is exact for the comparison. The solution varies most in a -# reaction layer at the membrane, which the solver resolves by refining; a -# start from points as fine as the finite elements, and a limit of nodes far -# above what the refinement needs, leave it room to: +# A strict solver tolerance is used to obtain a high-accuracy +# numerical reference solution for comparison with the finite-element +# solutions. The overpotential exhibits steep spatial variations +# near the membrane, which are resolved through adaptive mesh +# refinement. A sufficiently fine initial mesh and a large maximum +# number of nodes are specified to avoid restricting the refinement: reference = reference_solution( parameters, @@ -630,10 +710,16 @@ def boundary(y0: np.ndarray, yW: np.ndarray) -> np.ndarray: max_nodes=100_000, ) -# Each solution is compared with it on a line across the middle of its -# electrode, from the current collector to the membrane, by the relative -# root mean square of the difference. The solution is interpolated on the -# line with its own degree, at a number of points that resolves it: +# Each numerical solution is compared with the reference solution +# along a line through the middle of the electrode, extending from +# the current collector to the membrane. The error is calculated +# as the root-mean-square difference between the interpolated +# numerical and reference solutions, normalized by the maximum +# absolute value of the reference overpotential, estimated at +# 200 uniformly distributed points. +# The numerical solution is interpolated onto the line using +# the same polynomial degree as the original finite-element +# approximation: # + @@ -746,18 +832,39 @@ def integral(expression: ufl.core.expr.Expr) -> float: ) assert (rates > p_degree + 0.5).all() # - +# The observed convergence rates approach the theoretical +# orders of 2, 3, and 4 for polynomial degrees 1, 2, +# and 3, respectively. This supports the expected +# spatial accuracy of the finite-element discretization +# for the benchmark. +# # ### A thicker electrode # -# In a thicker electrode, the conductivity of the electrolyte, lower than -# that of the solid, confines the reaction to a layer at the membrane. The -# overpotential is nearly uniform across most of the electrode and varies -# steeply in the layer. The parameters, not the electrode, set the thickness -# of the layer, so the thicker the electrode, the smaller the part of it the -# layer takes. The same components solve the problem, from the same initial -# guess, on a thicker electrode. The same number of cells across would leave -# few cells in the layer, so the mesh has more, and the reference starts -# from points across the thicker electrode: +# In a sufficiently thick electrode, the lower effective +# ionic conductivity of the electrolyte compared with +# the electronic conductivity of the solid causes most +# of the electrochemical reaction to occur near the +# membrane. The reaction rate is much lower throughout +# the remaining electrode, although a smaller reaction +# region may also develop near the current collector. +# The characteristic reaction penetration depth is +# primarily determined by the effective conductivities +# and electrochemical kinetics. When the electrode +# thickness increases while these parameters remain +# unchanged, the reaction region occupies a smaller +# fraction of the total electrode thickness. +# Here, the electrode thickness is increased from 5 mm +# to 50 mm while retaining the same material properties +# and reaction kinetics. The same SUMFEM components and +# initial guess are used. +# Retaining the original number of elements would +# substantially reduce the resolution of the localized +# reaction region. Therefore, the number of elements +# across the electrode is increased from 50 to 200. +# The one-dimensional reference solver starts from +# 100 points distributed across the thicker electrode +# and adaptively refines the mesh: # + thick = 10 * width @@ -775,10 +882,13 @@ def integral(expression: ufl.core.expr.Expr) -> float: assert error_thick < 1e-4 # - -# The current balance is not checked here: the steep layer needs a higher -# quadrature degree for the integral of the reaction than the default -# estimate gives. Across the thick electrode, the overpotential and its -# reaction layer at the membrane: +# The integrated reaction-current balance is not checked here +# because the default quadrature degree may be insufficient +# to accurately integrate the rapidly varying nonlinear +# reaction term near the membrane. +# The overpotential distribution across the thicker electrode +# is plotted below alongside the reference solution to +# illustrate the localized reaction region near the membrane: x_thick = eta_thick.function_space.tabulate_dof_coordinates()[:, 0] order_thick = np.argsort(x_thick)