Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@

## Features

- Added `BaseBatterySubModel` to handle battery-specific domain validation separately from `BaseSubModel` ([#5254](https://github.com/pybamm-team/PyBaMM/pull/5254))
- Added uniform grid sizing across subdomains in the x-dimension, ensuring consistent grid spacing when geometries have varying lengths. ([#5253](https://github.com/pybamm-team/PyBaMM/pull/5253))
- Added the `electrode_phases` kwarg to `plot_voltage_components()` which allows choosing between plotting primary or secondary phase overpotentials. ([#5229](https://github.com/pybamm-team/PyBaMM/pull/5229))
- Added the `num_steps_no_progress` and `t_no_progress` options in the `IDAKLUSolver` to early terminate the simulation if little progress is detected. ([#5201](https://github.com/pybamm-team/PyBaMM/pull/5201))
Expand Down
1 change: 1 addition & 0 deletions src/pybamm/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@

# Submodel classes
from .models.submodels.base_submodel import BaseSubModel
from .models.submodels.base_battery_submodel import BaseBatterySubModel

from .models.submodels import (
active_material,
Expand Down
2 changes: 1 addition & 1 deletion src/pybamm/models/submodels/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
__all__ = ['active_material', 'base_submodel', 'convection',
__all__ = ['active_material', 'base_submodel', 'base_battery_submodel', 'convection',
'current_collector', 'electrode', 'electrolyte_conductivity',
'electrolyte_diffusion', 'equivalent_circuit_elements',
'external_circuit', 'interface', 'oxygen_diffusion', 'particle',
Expand Down
42 changes: 42 additions & 0 deletions src/pybamm/models/submodels/base_battery_submodel.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import pybamm

from .base_submodel import BaseSubModel


class BaseBatterySubModel(BaseSubModel):
"""
Base class for all battery-specific submodels.
Contains domain checks and other logic specific to battery modeling.
"""

ok_domain_list = [
"negative",
"separator",
"positive",
"negative electrode",
"negative electrolyte",
"separator electrolyte",
"positive electrode",
"positive electrolyte",
None,
]

def __init__(
self,
param,
domain=None,
name="Unnamed battery submodel",
external=False,
options=None,
phase=None,
):
# normalize domain
domain_lower = domain.lower() if isinstance(domain, str) else domain

if domain_lower not in self.ok_domain_list:
raise pybamm.DomainError(
f"Invalid domain '{domain}' for battery submodel. "
f"Allowed: {self.ok_domain_list}"
)

super().__init__(param, domain, name, external, options, phase)
16 changes: 6 additions & 10 deletions src/pybamm/models/submodels/base_submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,17 +112,13 @@ def domain(self):

@domain.setter
def domain(self, domain):
"""
Sets the domain for the submodel.
For general submodels, no restrictions are applied.
"""
self._domain = domain
if domain is not None:
domain = domain.lower()
ok_domain_list = ["negative", "separator", "positive", None]
if domain in ok_domain_list:
self._domain = domain
if domain is not None:
self._Domain = domain.capitalize()
else:
raise pybamm.DomainError(
f"Domain '{domain}' not recognised (must be one of {ok_domain_list})"
)
self._Domain = str(domain).capitalize()

@property
def domain_Domain(self):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

import pybamm


class DummyParam:
def __init__(self):
self.domain_params = {"Negative": {}, "Positive": {}}


def test_valid_domain():
submodel = pybamm.BaseBatterySubModel(DummyParam(), "Negative")
assert submodel.domain == "Negative"


def test_invalid_domain():
with pytest.raises(pybamm.DomainError):
pybamm.BaseBatterySubModel(DummyParam(), "InvalidDomain")
6 changes: 3 additions & 3 deletions tests/unit/test_models/test_submodels/test_base_submodel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def test_domain(self):
submodel = pybamm.BaseSubModel(None, None)
assert submodel.domain is None

# bad string
with pytest.raises(pybamm.DomainError):
pybamm.BaseSubModel(None, "bad string")
# bad string — BaseSubModel no longer validates domains
submodel = pybamm.BaseSubModel(None, "bad string")
assert submodel.domain == "bad string"

def test_phase(self):
# Without domain
Expand Down
Loading