Skip to content

Commit

Permalink
Merge pull request #218 from jotelha/pep-8-electrochemistry
Browse files Browse the repository at this point in the history
MAINT: Remodel electrochemistry module interface API to adhere to PEP-8 conventions
  • Loading branch information
pastewka authored Jan 14, 2024
2 parents ba21791 + 5aec6e3 commit c79e1d8
Show file tree
Hide file tree
Showing 13 changed files with 166 additions and 205 deletions.
10 changes: 10 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ on:
- '**'
tags:
- '**'
pull_request:
branches: [ master ]

jobs:
tests:
Expand All @@ -24,6 +26,14 @@ jobs:
# Upgrade to latest meson and ninja
sudo pip install --upgrade meson ninja
# needs fenics only to run test_poisson_nernst_planck_solver_fenics
- name: install_fenics
run: |
sudo apt-get install -y --no-install-recommends software-properties-common
sudo add-apt-repository -y ppa:fenics-packages/fenics
sudo apt-get update -qy
sudo apt-get install -y fenics python3-dolfin python3-mshr
- name: build_c
run: |
sudo pip install .[test]
Expand Down
66 changes: 0 additions & 66 deletions .github/workflows/tests_fenics.yml

This file was deleted.

8 changes: 4 additions & 4 deletions docs/applications/electrochemistry_1.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@
pnp = PoissonNernstPlanckSystem(c=c, z=z, L=L, delta_u=delta_u)

# %% [markdown]
# The method `useStandardInterfaceBC` will apply boundary conditions as shown in <a hef="#figure1">Figure 1</a>, with Dirichlet boundary conditions on the potential and zero total flux boundary conditions on ion diffusion and electromigration.
# The method `use_standard_interface_bc` will apply boundary conditions as shown in <a hef="#figure1">Figure 1</a>, with Dirichlet boundary conditions on the potential and zero total flux boundary conditions on ion diffusion and electromigration.

# %%
pnp.useStandardInterfaceBC()
pnp.use_standard_interface_bc()
ui, nij, _ = pnp.solve()

# %% [markdown]
Expand Down Expand Up @@ -188,7 +188,7 @@ def make_patch_spines_invisible(ax):
# The only difference is the application of another set of predefined boundary conditions.

# %%
pnp.useStandardCellBC()
pnp.use_standard_cell_bc()

# %% [markdown]
# These boundary conditions assume no Stern layer, i.e. $\lambda_S = 0$
Expand Down Expand Up @@ -309,7 +309,7 @@ def make_patch_spines_invisible(ax):
# The following applies zero flux and, particularly, Robin boundary conditions as shown in <a href="#figure2">Figure 2</a>.

# %%
pnp.useSternLayerCellBC()
pnp.use_stern_layer_cell_bc()

pnp.output = True
xij = pnp.solve()
Expand Down
8 changes: 4 additions & 4 deletions docs/applications/electrochemistry_2.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,12 @@ def make_patch_spines_invisible(ax):
c, z, L, delta_u=delta_u, N=N,
solver="hybr", options={'xtol':1e-12})

pnp['std_interface'].useStandardInterfaceBC()
pnp['std_interface'].use_standard_interface_bc()
uij, nij, lamj = pnp['std_interface'].solve()

# define desired system
pnp['fenics_interface'] = PoissonNernstPlanckSystemFEniCS(c, z, L, delta_u=delta_u, N=N)
pnp['fenics_interface'].useStandardInterfaceBC()
pnp['fenics_interface'].use_standard_interface_bc()
uij, nij, _ = pnp['fenics_interface'].solve()

# %% [markdown]
Expand Down Expand Up @@ -184,15 +184,15 @@ def make_patch_spines_invisible(ax):
pnp['std_interface_high_potential'] = PoissonNernstPlanckSystem(
c, z, L, delta_u=delta_u,N=N,
solver="hybr", options={'xtol':1e-14})
pnp['std_interface_high_potential'].useStandardInterfaceBC()
pnp['std_interface_high_potential'].use_standard_interface_bc()
uij, nij, lamj = pnp['std_interface_high_potential'].solve()

# %% [markdown]
# Apparently, the `PoissonNernstPlanckSystem` controlled-volumes solver does not converge ...

# %%
pnp['fenics_interface_high_potential'] = PoissonNernstPlanckSystemFEniCS(c, z, L, delta_u=delta_u, N=200)
pnp['fenics_interface_high_potential'].useStandardInterfaceBC()
pnp['fenics_interface_high_potential'].use_standard_interface_bc()
uij, nij, _ = pnp['fenics_interface_high_potential'].solve()

# %% [markdown]
Expand Down
59 changes: 34 additions & 25 deletions matscipy/cli/electrochemistry/poisson_nernst_planck_solver_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,37 @@ class ArgumentDefaultsAndRawDescriptionHelpFormatter(
logger = logging.getLogger()
logger.setLevel(loglevel)

# use FEniCS finite element solver if available,
# otherwise own controlled volume scheme
try:
import fenics
from matscipy.electrochemistry.poisson_nernst_planck_solver_fenics \
import PoissonNernstPlanckSystemFEniCS as PoissonNernstPlanckSystem
import dolfin
import ffc
dolfin.cpp.log.set_log_level(loglevel)

if not args.outfile:
# fenics writes some log messages to stdout. If we pipe the output,
# we don't want that, hence here we have to suppress fenics logging
# if no output file has been specified
ffc.log.set_level(logging.ERROR)
fenics.set_log_level(logging.ERROR)
fenics.set_log_active(False)
dolfin.cpp.log.set_log_level(logging.ERROR)
dolfin.cpp.log.set_log_active(False)

logging.getLogger('UFL').setLevel(logging.ERROR)
logging.getLogger('FFC').setLevel(logging.ERROR)

logger.info("Will use FEniCS finite element solver.")
except ModuleNotFoundError:
logger.warning(
"No FEniCS finite element solver found,"
" falling back to internal controlled-volume implementation."
" ATTENTION: Number conservation not exact.")
from matscipy.electrochemistry import PoissonNernstPlanckSystem

# remove all handlers
for h in logger.handlers:
logger.removeHandler(h)
Expand All @@ -181,22 +212,6 @@ class ArgumentDefaultsAndRawDescriptionHelpFormatter(
fh.setLevel(loglevel)
logger.addHandler(fh)

# use FEniCS finite element solver if available,
# otherwise own controlled volume scheme
try:
import fenics
from matscipy.electrochemistry.poisson_nernst_planck_solver_fenics \
import PoissonNernstPlanckSystemFEniCS as PoissonNernstPlanckSystem
import dolfin
dolfin.cpp.log.set_log_level(loglevel)
logger.info("Will use FEniCS finite element solver.")
except ModuleNotFoundError:
logger.warning(
"No FEniCS finite element solver found,"
" falling back to internal controlled-volume implementation."
" ATTENTION: Number conservation not exact.")
from matscipy.electrochemistry import PoissonNernstPlanckSystem

# set up system
pnp = PoissonNernstPlanckSystem(
c=np.array(args.concentrations, dtype=float),
Expand All @@ -211,22 +226,16 @@ class ArgumentDefaultsAndRawDescriptionHelpFormatter(
relative_permittivity=float(args.relative_permittivity))

if args.boundary_conditions in ('cell-robin') and float(args.lambda_S) > 0:
pnp.useSternLayerCellBC()
pnp.use_stern_layer_cell_bc()
elif ((args.boundary_conditions in ('cell-robin') and float(args.lambda_S) == 0)
or args.boundary_conditions == 'cell'):
pnp.useStandardCellBC()
pnp.use_standard_cell_bc()
elif args.boundary_conditions == 'interface':
pnp.useStandardInterfaceBC()
pnp.use_standard_interface_bc()
else:
raise ValueError("Boundary conditions '{}' not implemented!".format(
args.boundary_conditions))

if not args.outfile:
# fenics writes some log messages to stdout. If we pipe the output,
# we don't want that, hence here we have to suppress fenics logging
# if no output file has been specified
dolfin.cpp.log.set_log_active(False)

pnp.solve()

extra_kwargs = {}
Expand Down
2 changes: 1 addition & 1 deletion matscipy/electrochemistry/continuous2discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ def generate_structure(distribution, box=np.array([50, 50, 100]), count=100, n_g
Z, _ = integrate.quad(d, support[-1][0], support[-1][1])
else: # discrete support
support.append(np.linspace(0, box[k], n_gridpoints[k]))
Z = np.sum(d(support[-1])) # Normalization constant
Z = np.sum(d(support[-1])) # Normalization constant

logger.info("Normalizing 'distribution' {} by {}.".format(d, Z))
normalized_distribution.append(
Expand Down
3 changes: 1 addition & 2 deletions matscipy/electrochemistry/poisson_boltzmann_distribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,7 @@ def debye(c, z,
lambda_D : float
Debye length, sqrt( epsR*eps*R*T/(2*F^2*I) ) [m]
"""
I = ionic_strength(c, z)
return np.sqrt(relative_permittivity*vacuum_permittivity*R*T/(2.0*F**2*I))
return np.sqrt(relative_permittivity*vacuum_permittivity*R*T/(2.0*F**2*ionic_strength(c, z)))


def gamma(u, T=298.15):
Expand Down
Loading

0 comments on commit c79e1d8

Please sign in to comment.