PyBaMM icon indicating copy to clipboard operation
PyBaMM copied to clipboard

BasicDFN2D with variable porosity and active fractions in x and z directions

Open DrSOKane opened this issue 1 month ago • 1 comments

Edited BasicDFN2D to make the porosity and active material fractions into FunctionParameters. However, it doesn't seem to have any effect:

import pybamm
import numpy as np

model = pybamm.lithium_ion.BasicDFN2D()
param = pybamm.ParameterValues("Ecker2015")
Lz = param["Electrode height [m]"]


def sigmoid(arg):
    return (1 + np.tanh(arg)) / 2


def top_hat(arg, a, b, k=2025):
    return sigmoid(k * (arg - a)) * sigmoid(k * (b - arg))


def eps_sep(x_s, z_s):
    return 0.508 - 0.458 * (
        top_hat(arg=z_s, a=Lz*0.45, b=Lz*0.55)
    )


param_defect = param.copy()
param_defect.update({"Separator porosity": eps_sep})

Ls = param_defect["Separator thickness [m]"]
param_defect["Separator porosity"](Ls/2,0.5*Lz)

The output is np.float64(0.05000000120084669), as expected.

exp = pybamm.Experiment(["Discharge at 1C until 2.7 V", "Charge at 1C until 4.2 V", "Hold at 4.2 V until C/20"])
sim = pybamm.Simulation(model, parameter_values=param_defect, experiment=exp)
sol = sim.solve()
output_variables = [
    "Negative electrolyte potential [V]",
    "Negative electrode potential [V]",
    "Negative electrode ocp [V]",
    "Electrolyte flux X-component [mol.m-2.s-1]",
    "Electrolyte flux Z-component [mol.m-2.s-1]",
    "Voltage [V]",
]
plot = sol.plot(output_variables, variable_limits="tight", shading="auto")
plot.plot(2.35)
plot.fig.savefig("defect_test_basic2d.png")

yields this figure, in which the z-component of the electrolyte flux is zero plus noise. defect_test_basic2d

DrSOKane avatar Nov 10 '25 11:11 DrSOKane

Codecov Report

:white_check_mark: All modified and coverable lines are covered by tests. :white_check_mark: Project coverage is 98.69%. Comparing base (6ff7848) to head (ef25431). :warning: Report is 1 commits behind head on develop.

Additional details and impacted files
@@           Coverage Diff            @@
##           develop    #5266   +/-   ##
========================================
  Coverage    98.69%   98.69%           
========================================
  Files          324      324           
  Lines        28010    28012    +2     
========================================
+ Hits         27645    27647    +2     
  Misses         365      365           

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

:rocket: New features to boost your workflow:
  • :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

codecov[bot] avatar Nov 18 '25 11:11 codecov[bot]

Thanks @aabills! I'm in a mind to add an example before merging.

DrSOKane avatar Dec 19 '25 16:12 DrSOKane

Check out this pull request on  ReviewNB

See visual diffs & provide feedback on Jupyter Notebooks.


Powered by ReviewNB

Example failed due to taking too long. How do I change var_pts for the BasicDFN2D class without altering the code?

DrSOKane avatar Dec 19 '25 20:12 DrSOKane

It should be the same as the standard models, except that you'll need to add var_pts for both the X and Z directions. And you'll need to directly create the spatial variables x and z or use the ones from the model.

aabills avatar Dec 19 '25 20:12 aabills