Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement Integrated electrolyte conductivity #1188

Conversation

brosaplanella
Copy link
Sponsor Member

@brosaplanella brosaplanella commented Oct 13, 2020

Description

Implementation of the Integrated electrolyte conductivity model as defined in Brosa Planella et al. (2020).

Paper is not yet on arXiv, we can wait to merge until it is to avoid another PR in a couple of weeks.

Fixes #884

Type of change

Please add a line in the relevant section of CHANGELOG.md to document the change (include PR #) - note reverse order of PR #s. If necessary, also add to the list of breaking changes.

  • New feature (non-breaking change which adds functionality)

Key checklist:

  • No style issues: $ flake8
  • All tests pass: $ python run-tests.py --unit
  • The documentation builds: $ cd docs and then $ make clean; make html

You can run all three at once, using $ python run-tests.py --quick.

Further checks:

  • Code is commented, particularly in hard-to-understand areas
  • Tests added that prove fix is effective or that feature works

Copy link
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good @brosaplanella , you should also add a test in test_spme.py (both unit and integration)

@@ -27,7 +27,7 @@ def electrolyte_conductivity_Nyman2008(c_e, T):
0.1297 * (c_e / 1000) ** 3 - 2.51 * (c_e / 1000) ** 1.5 + 3.329 * (c_e / 1000)
)

E_k_e = 34700
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this a mistake in the original implementation?
you could argue we should just not have the arrhenius term in this case

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if a mistake, or we decided to keep the Arrhenius term from the Marquis2019 set (originally it all was in parameters.csv. However, if now it is specified as a function I think it would be good to stick to what the paper provides, and it this case, Nyman et al. 2008, does not provide temperature dependence so I removed it.

If that makes sense, I will remove the whole Arrhenius term so it is clearer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, that makes sense, just remove the arrhenius term and put in a comment

@@ -24,7 +24,7 @@ def electrolyte_diffusivity_Nyman2008(c_e, T):
"""

D_c_e = 8.794e-11 * (c_e / 1000) ** 2 - 3.972e-10 * (c_e / 1000) + 4.862e-10
E_D_e = 37040
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as above

The domain in which the model holds

**Extends:** :class:`pybamm.electrolyte_conductivity.BaseElectrolyteConductivity`
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add reference to your paper in the docstring

/ param.gamma_e
)

integral_n = indef_integral_n - pybamm.boundary_value(indef_integral_n, "left")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

left boundary value of an indefinite integral should always be zero by definition (and implementation) but worth checking

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I checked, and maybe it evaluates to zero (actually it should), but is not zero by definition. The definition is

<bound method UnaryOperator.evaluate of BoundaryValue(-0x33bf8d7889aadb3a, boundary value, children=["* integrated w.r.t x_n on ['negative electrode'](3.0 * x_n ** 2.0)"], domain=['current collector'], auxiliary_domains={})>

integral_s = (
indef_integral_s
- pybamm.boundary_value(indef_integral_s, "left")
+ pybamm.boundary_value(integral_n, "right")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pybamm.Integral(a) should give the same value as pybamm.boundary_value(pybamm.IndefiniteIntegral(a), "right") while building a smaller tree

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but maybe it automatically gets simplified down to the same tree anyway

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can I check if they reduce to the same tree? In any case, I think that if they are equivalent, using pybamm.Integral(a) and removing the boundary values on the left should result in a much more readable code.

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just tried replacing the boundary value at the "right" by Integral and I get a very long error (see below) which basically looks like a dimension mismatch. So I guess they are not the same object...

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: 1041553401910644705

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: 2459353937759019131

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: 4130260032263747209

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: 7060176036539492958

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: 8911840126246680561

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 817, in process_symbol
    return self._discretised_symbols[symbol.id]
KeyError: -7919469058354380383

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/symbol.py", line 594, in evaluate_for_shape
    return self._saved_evaluate_for_shape
AttributeError: 'MatrixMultiplication' object has no attribute '_saved_evaluate_for_shape'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/symbol.py", line 801, in test_shape
    self.shape_for_testing
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/symbol.py", line 785, in shape_for_testing
    evaluated_self = self.evaluate_for_shape()
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/symbol.py", line 596, in evaluate_for_shape
    self._saved_evaluate_for_shape = self._evaluate_for_shape()
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/binary_operators.py", line 201, in _evaluate_for_shape
    return self._binary_evaluate(left, right)
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/binary_operators.py", line 497, in _binary_evaluate
    return left @ right
  File "/home/ferranbrosa/coding/reduced-TEC/env/lib/python3.7/site-packages/scipy/sparse/base.py", line 564, in __matmul__
    return self.__mul__(other)
  File "/home/ferranbrosa/coding/reduced-TEC/env/lib/python3.7/site-packages/scipy/sparse/base.py", line 502, in __mul__
    raise ValueError('dimension mismatch')
ValueError: dimension mismatch

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/ferranbrosa/miniconda3/lib/python3.7/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/home/ferranbrosa/miniconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/ferranbrosa/.vscode-server/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/ferranbrosa/.vscode-server/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 430, in main
    run()
  File "/home/ferranbrosa/.vscode-server/extensions/ms-python.python-2020.9.114305/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 267, in run_file
    runpy.run_path(options.target, run_name=compat.force_str("__main__"))
  File "/home/ferranbrosa/miniconda3/lib/python3.7/runpy.py", line 263, in run_path
    pkg_name=pkg_name, script_name=fname)
  File "/home/ferranbrosa/miniconda3/lib/python3.7/runpy.py", line 96, in _run_module_code
    mod_name, mod_spec, pkg_name, script_name)
  File "/home/ferranbrosa/miniconda3/lib/python3.7/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/home/ferranbrosa/coding/reduced-TEC/TSPMe_compare_DFN.py", line 91, in <module>
    sim.solve([0, 3700 / Crate])
  File "/home/ferranbrosa/PyBaMM/pybamm/simulation.py", line 338, in solve
    self.build(check_model=check_model)
  File "/home/ferranbrosa/PyBaMM/pybamm/simulation.py", line 291, in build
    self._model_with_set_params, inplace=False, check_model=check_model
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 173, in process_model
    self.set_internal_boundary_conditions(model)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 419, in set_internal_boundary_conditions
    rbc = (boundary_gradient(first_orphan, next_orphan), "Neumann")
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 399, in boundary_gradient
    right_symbol_disc = self.process_symbol(right_symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 819, in process_symbol
    discretised_symbol = self._process_symbol(symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 853, in _process_symbol
    disc_right = self.process_symbol(right)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 819, in process_symbol
    discretised_symbol = self._process_symbol(symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 853, in _process_symbol
    disc_right = self.process_symbol(right)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 819, in process_symbol
    discretised_symbol = self._process_symbol(symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 862, in _process_symbol
    disc_child = self.process_symbol(child)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 819, in process_symbol
    discretised_symbol = self._process_symbol(symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 852, in _process_symbol
    disc_left = self.process_symbol(left)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 819, in process_symbol
    discretised_symbol = self._process_symbol(symbol)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 852, in _process_symbol
    disc_left = self.process_symbol(left)
  File "/home/ferranbrosa/PyBaMM/pybamm/discretisations/discretisation.py", line 821, in process_symbol
    discretised_symbol.test_shape()
  File "/home/ferranbrosa/PyBaMM/pybamm/expression_tree/symbol.py", line 803, in test_shape
    raise pybamm.ShapeError("Cannot find shape (original error: {})".format(e))

@brosaplanella
Copy link
Sponsor Member Author

I have addressed all the comments except the one about the Integral which seems to be a deeper issue with the integral. Once I submit the draft to arXiv (hopefully in the next two weeks) I will have to update the citation. Is it better to merge now and update later or to wait to merge?

Copy link
Member

@valentinsulzer valentinsulzer left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @brosaplanella , looks good now, don't worry about the integral thing

model.submodels[
"electrolyte conductivity"
] = pybamm.electrolyte_conductivity.Integrated(model.param)
model.build_model()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might want to provide an option for doing this automatically

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought about that, but wasn't sure what approach we want to take. We discussed it some time ago, but can't remember what were the arguments against being able to pass options to set all the submodels.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't remember the arguments against either. I think you should add the option

@valentinsulzer
Copy link
Member

Ready to merge?

@brosaplanella brosaplanella merged commit d77ed8a into pybamm-team:develop Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

SPMe electrolyte model with general Ohmic term
2 participants