Skip to content

Commit

Permalink
#2376, added bpx conversions and some defaults, remove events in simu…
Browse files Browse the repository at this point in the history
…lation, still problems with diffusivity
  • Loading branch information
martinjrobins committed Oct 18, 2022
1 parent 7feb92e commit f36845d
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 41 deletions.
86 changes: 47 additions & 39 deletions pybamm/parameters/bpx.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from bpx import BPX, Function, InterpolatedTable
import numbers
import pybamm
import math
from dataclasses import dataclass
import numpy as np
from pybamm import constants
from pybamm import exp


@dataclass
Expand All @@ -18,6 +20,7 @@ class Domain:
short_pre_name='Positive ')
electrolyte = Domain(name='electrolyte', pre_name='Electrolyte ', short_pre_name='')
separator = Domain(name='separator', pre_name='Separator ', short_pre_name='')
experiment = Domain(name='experiment', pre_name='', short_pre_name='')


def bpx_to_param_dict(bpx: BPX) -> dict:
Expand All @@ -32,14 +35,41 @@ def bpx_to_param_dict(bpx: BPX) -> dict:
bpx.parameterisation.cathode, pybamm_dict, cathode
)
pybamm_dict = _bpx_to_param_dict(
bpx.parameterisation.cathode, pybamm_dict, electrolyte
bpx.parameterisation.electrolyte, pybamm_dict, electrolyte
)
print(pybamm_dict, bpx)
pybamm_dict = _bpx_to_param_dict(
bpx.parameterisation.separator, pybamm_dict, separator
)
pybamm_dict = _bpx_to_param_dict(
bpx.parameterisation.separator, pybamm_dict, experiment
)

# set a default current function
pybamm_dict['Current function [A]'] = 1

# Ambient temp
pybamm_dict['Ambient temperature [K]'] = pybamm_dict['Reference temperature [K]']

for domain in [anode, cathode]:
pybamm_dict[domain.pre_name + 'electrons in reaction'] = 1.0

# typical electrolyte concentration
pybamm_dict['Typical electrolyte concentration [mol.m-3]'] = pybamm_dict[
'Initial concentration in electrolyte [mol.m-3]'
]

for domain in [anode, cathode]:
pybamm_dict[domain.pre_name + 'OCP entropic change [V.K-1]'] = 0.0

for domain in [anode, separator, cathode]:
pybamm_dict[domain.pre_name + 'Bruggeman coefficient (electrolyte)'] = 1.5
pybamm_dict[domain.pre_name + 'Bruggeman coefficient (electrode)'] = 1.5

pybamm_dict['Number of cells connected in series to make a battery'] = 1

# electrode area
equal_len_width = math.sqrt(pybamm_dict['Electrode area [m]'])
equal_len_width = math.sqrt(pybamm_dict['Electrode area [m2]'])
pybamm_dict['Electrode width [m]'] = equal_len_width
pybamm_dict['Electrode height [m]'] = equal_len_width

Expand All @@ -51,7 +81,8 @@ def bpx_to_param_dict(bpx: BPX) -> dict:
]:
for domain in [anode, cathode, separator]:
pybamm_name = domain.pre_name + name[:1].lower() + name[1:]
pybamm_dict[pybamm_name] = pybamm_dict[name]
if name in pybamm_dict:
pybamm_dict[pybamm_name] = pybamm_dict[name]

# BET surface area
for domain in [anode, cathode]:
Expand Down Expand Up @@ -83,7 +114,8 @@ def bpx_to_param_dict(bpx: BPX) -> dict:
k_norm = pybamm_dict[
domain.pre_name + 'reaction rate [mol.m-2.s-1]'
]
k = k_norm * F / (c_n_max * c_e ** 0.5)
F = 96485
k = k_norm * F / (c_max * c_e ** 0.5)
def exchange_current_density(c_e, c_s_surf, c_s_max, T):
k_ref = k # (A/m2)(mol/m3)**1.5 - includes ref concentrations
E_r = 53400
Expand All @@ -96,37 +128,8 @@ def exchange_current_density(c_e, c_s_surf, c_s_max, T):
exchange_current_density
)

return pybamm_dict


def positive_electrode_exchange_current_density(c_e, c_s_surf, c_s_max, T):
"""
Exchange-current density for Butler-Volmer reactions.
Parameters
----------
c_e : :class:`pybamm.Symbol`
Electrolyte concentration [mol.m-3]
c_s_surf : :class:`pybamm.Symbol`
Particle concentration [mol.m-3]
c_s_max : :class:`pybamm.Symbol`
Maximum particle concentration [mol.m-3]
T : :class:`pybamm.Symbol`
Temperature [K]
Returns
-------
:class:`pybamm.Symbol`
Exchange-current density [A.m-2]
"""

k_ref = 1.3499500722289229e-06 # (A/m2)(mol/m3)**1.5 - includes ref concentrations
E_r = 27010
arrhenius = exp(E_r / constants.R * (1 / 298.15 - 1 / T))

return (
k_ref * arrhenius * c_e ** 0.5 * c_s_surf ** 0.5 * (c_s_max - c_s_surf) ** 0.5
)

pybamm_dict[] = k_norm * F / (c_max * c_e ** 0.5)
return pybamm_dict


preamble = (
Expand All @@ -143,11 +146,11 @@ def _bpx_to_param_dict(instance: BPX, pybamm_dict: dict, domain: Domain) -> dict
value = value.to_python_function(preamble=preamble)
elif isinstance(value, InterpolatedTable):
timescale = 1
x = value.x
y = value.y
x = np.array(value.x)
y = np.array(value.y)
interpolator = 'linear'
value = pybamm.Interpolant(
x, y, pybamm.t * timescale,
[x], y, pybamm.t * timescale,
name=name, interpolator=interpolator
)

Expand All @@ -160,12 +163,17 @@ def _bpx_to_param_dict(instance: BPX, pybamm_dict: dict, domain: Domain) -> dict
init_len = len("Initial concentration ")
pybamm_name = (
pybamm_name[:init_len] +
'in ' +
domain.pre_name.lower() +
pybamm_name[init_len:]
)
elif pybamm_name.startswith("Particle radius"):
pybamm_name = domain.short_pre_name + pybamm_name_lower
else:
elif pybamm_name.startswith('OCP'):
pybamm_name = domain.pre_name + pybamm_name
elif pybamm_name.startswith('Cation transference number'):
pybamm_name = pybamm_name
elif domain.pre_name != '':
pybamm_name = domain.pre_name + pybamm_name_lower

pybamm_dict[pybamm_name] = value
Expand Down
14 changes: 12 additions & 2 deletions tests/unit/test_parameters/test_parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
import pandas as pd

import pybamm
import tests
print(tests.__file__)
import tests.shared as shared
from pybamm.input.parameters.lithium_ion.Marquis2019 import (
lico2_ocp_Dualfoil1998,
Expand Down Expand Up @@ -1034,6 +1036,7 @@ def test_bpx(self):
"Cell": {
"Initial temperature [K]": 299.0,
"Reference temperature [K]": 299.0,
"Nominal cell capacity [A.h]": 5.0,
"Electrode area [m2]": 2.0,
"Number of electrodes connected in parallel to make a cell": 1,
},
Expand Down Expand Up @@ -1083,7 +1086,7 @@ def test_bpx(self):
import tempfile
filename = 'tmp.json'
with tempfile.NamedTemporaryFile(
suffix=filename, delete=False
suffix=filename, delete=False, mode='w'
) as tmp:
# write to a tempory file so we can
# get the source later on using inspect.getsource
Expand All @@ -1092,7 +1095,14 @@ def test_bpx(self):
tmp.flush()

pv = pybamm.ParameterValues.create_from_bpx(tmp.name)
print('got pv', pv)

model = pybamm.lithium_ion.DFN()
model.events = []
experiment = pybamm.Experiment([
"Discharge at C/5 for 1 hour",
])
sim = pybamm.Simulation(model, parameter_values=pv, experiment=experiment)
sim.solve(calc_esoh=False)


if __name__ == "__main__":
Expand Down

0 comments on commit f36845d

Please sign in to comment.