From e9682725593c1440bb811dfdaefad9ccece4cadc Mon Sep 17 00:00:00 2001 From: tinosulzer Date: Mon, 18 Nov 2019 22:47:54 -0500 Subject: [PATCH] #492 add docs and unit tests --- .../current_control_external_circuit.rst | 6 +++++ .../function_control_external_circuit.rst | 5 ++++ .../submodels/external_circuit/index.rst | 14 ++++++++++ .../power_control_external_circuit.rst | 6 +++++ .../voltage_control_external_circuit.rst | 6 +++++ docs/source/models/submodels/index.rst | 1 + examples/scripts/charging_strategies.py | 6 ++--- .../full_battery_models/base_battery_model.py | 2 +- pybamm/models/submodels/base_submodel.py | 3 ++- .../test_external_circuit/__init__.py | 0 .../test_current_control.py | 25 ++++++++++++++++++ .../test_function_control.py | 26 +++++++++++++++++++ .../test_power_control.py | 26 +++++++++++++++++++ .../test_voltage_control.py | 26 +++++++++++++++++++ 14 files changed, 147 insertions(+), 5 deletions(-) create mode 100644 docs/source/models/submodels/external_circuit/current_control_external_circuit.rst create mode 100644 docs/source/models/submodels/external_circuit/function_control_external_circuit.rst create mode 100644 docs/source/models/submodels/external_circuit/index.rst create mode 100644 docs/source/models/submodels/external_circuit/power_control_external_circuit.rst create mode 100644 docs/source/models/submodels/external_circuit/voltage_control_external_circuit.rst create mode 100644 tests/unit/test_models/test_submodels/test_external_circuit/__init__.py create mode 100644 tests/unit/test_models/test_submodels/test_external_circuit/test_current_control.py create mode 100644 tests/unit/test_models/test_submodels/test_external_circuit/test_function_control.py create mode 100644 tests/unit/test_models/test_submodels/test_external_circuit/test_power_control.py create mode 100644 tests/unit/test_models/test_submodels/test_external_circuit/test_voltage_control.py diff --git a/docs/source/models/submodels/external_circuit/current_control_external_circuit.rst b/docs/source/models/submodels/external_circuit/current_control_external_circuit.rst new file mode 100644 index 0000000000..6f3f0a001e --- /dev/null +++ b/docs/source/models/submodels/external_circuit/current_control_external_circuit.rst @@ -0,0 +1,6 @@ +Current control external circuit +================================ + +.. autoclass:: pybamm.external_circuit.CurrentControl + :members: + diff --git a/docs/source/models/submodels/external_circuit/function_control_external_circuit.rst b/docs/source/models/submodels/external_circuit/function_control_external_circuit.rst new file mode 100644 index 0000000000..fa0c693ef7 --- /dev/null +++ b/docs/source/models/submodels/external_circuit/function_control_external_circuit.rst @@ -0,0 +1,5 @@ +Function control external circuit +================================= + +.. autoclass:: pybamm.external_circuit.FunctionControl + :members: diff --git a/docs/source/models/submodels/external_circuit/index.rst b/docs/source/models/submodels/external_circuit/index.rst new file mode 100644 index 0000000000..d59aa61a4c --- /dev/null +++ b/docs/source/models/submodels/external_circuit/index.rst @@ -0,0 +1,14 @@ +External circuit +================ + +Models to enforce different boundary conditions (as imposed by an imaginary external +circuit) such as constant current, constant voltage, constant power, or any other +relationship between the current and voltage + +.. toctree:: + :maxdepth: 1 + + current_control_external_circuit + function_control_external_circuit + power_control_external_circuit + voltage_control_external_circuit diff --git a/docs/source/models/submodels/external_circuit/power_control_external_circuit.rst b/docs/source/models/submodels/external_circuit/power_control_external_circuit.rst new file mode 100644 index 0000000000..6955e8ad63 --- /dev/null +++ b/docs/source/models/submodels/external_circuit/power_control_external_circuit.rst @@ -0,0 +1,6 @@ +Power control external circuit +============================== + +.. autoclass:: pybamm.external_circuit.PowerControl + :members: + diff --git a/docs/source/models/submodels/external_circuit/voltage_control_external_circuit.rst b/docs/source/models/submodels/external_circuit/voltage_control_external_circuit.rst new file mode 100644 index 0000000000..4737a5a92d --- /dev/null +++ b/docs/source/models/submodels/external_circuit/voltage_control_external_circuit.rst @@ -0,0 +1,6 @@ +Voltage control external circuit +================================ + +.. autoclass:: pybamm.external_circuit.VoltageControl + :members: + diff --git a/docs/source/models/submodels/index.rst b/docs/source/models/submodels/index.rst index 5377dcd868..d5a1e98721 100644 --- a/docs/source/models/submodels/index.rst +++ b/docs/source/models/submodels/index.rst @@ -9,6 +9,7 @@ Submodels convection/index electrode/index electrolyte/index + external_circuit/index interface/index oxygen_diffusion/index particle/index diff --git a/examples/scripts/charging_strategies.py b/examples/scripts/charging_strategies.py index 24575f84aa..17e1d220ef 100644 --- a/examples/scripts/charging_strategies.py +++ b/examples/scripts/charging_strategies.py @@ -2,7 +2,7 @@ # Compare some charging strategies for lithium-ion batteries # # 1. CC-CV: Charge at 1A to 4.2V then 4.2V hold -# 2. CV: Charge at 4.2V +# 2. CV: Charge at 4.1V # 3. Constant Power-CV: Charge at 4W to 4.2V then 4.2V hold # import argparse @@ -43,8 +43,8 @@ def cccv(I, V): params[0]["CCCV switch"] = 1 # start with CC params[0]["External circuit function"] = cccv -# 2. CV: Charge at 4.2V -params[1]["Voltage function"] = 4.2 +# 2. CV: Charge at 4.1V +params[1]["Voltage function"] = 4.1 for model, param in zip(models, params): param.process_model(model) diff --git a/pybamm/models/full_battery_models/base_battery_model.py b/pybamm/models/full_battery_models/base_battery_model.py index 261f82f30c..a819ded5c1 100644 --- a/pybamm/models/full_battery_models/base_battery_model.py +++ b/pybamm/models/full_battery_models/base_battery_model.py @@ -166,7 +166,7 @@ def options(self, extra_options): "current", "voltage", "power", - "arbitrary", + "custom", ]: raise pybamm.OptionError( "operating mode '{}' not recognised".format(options["operating mode"]) diff --git a/pybamm/models/submodels/base_submodel.py b/pybamm/models/submodels/base_submodel.py index e91d5d6764..ad69a41c45 100644 --- a/pybamm/models/submodels/base_submodel.py +++ b/pybamm/models/submodels/base_submodel.py @@ -46,7 +46,7 @@ class BaseSubModel: symbols. """ - def __init__(self, param, domain=None, reactions=None): + def __init__(self, param, domain=None, reactions=None, name="Unnamed submodel"): super().__init__() self.param = param # Initialise empty variables (to avoid overwriting with 'None') @@ -61,6 +61,7 @@ def __init__(self, param, domain=None, reactions=None): self.domain = domain self.set_domain_for_broadcast() self.reactions = reactions + self.name = name @property def domain(self): diff --git a/tests/unit/test_models/test_submodels/test_external_circuit/__init__.py b/tests/unit/test_models/test_submodels/test_external_circuit/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/unit/test_models/test_submodels/test_external_circuit/test_current_control.py b/tests/unit/test_models/test_submodels/test_external_circuit/test_current_control.py new file mode 100644 index 0000000000..c2dc1c19de --- /dev/null +++ b/tests/unit/test_models/test_submodels/test_external_circuit/test_current_control.py @@ -0,0 +1,25 @@ +# +# Test current control submodel +# + +import pybamm +import tests +import unittest + + +class TestCurrentControl(unittest.TestCase): + def test_public_functions(self): + param = pybamm.standard_parameters_lithium_ion + submodel = pybamm.external_circuit.CurrentControl(param) + std_tests = tests.StandardSubModelTests(submodel) + std_tests.test_all() + + +if __name__ == "__main__": + print("Add -v for more debug output") + import sys + + if "-v" in sys.argv: + debug = True + pybamm.settings.debug_mode = True + unittest.main() diff --git a/tests/unit/test_models/test_submodels/test_external_circuit/test_function_control.py b/tests/unit/test_models/test_submodels/test_external_circuit/test_function_control.py new file mode 100644 index 0000000000..ddd7454ede --- /dev/null +++ b/tests/unit/test_models/test_submodels/test_external_circuit/test_function_control.py @@ -0,0 +1,26 @@ +# +# Test function control submodel +# +import numpy as np +import pybamm +import tests +import unittest + + +class TestFunctionControl(unittest.TestCase): + def test_public_functions(self): + param = pybamm.standard_parameters_lithium_ion + submodel = pybamm.external_circuit.FunctionControl(param) + variables = {"Terminal voltage [V]": pybamm.Scalar(0)} + std_tests = tests.StandardSubModelTests(submodel, variables) + std_tests.test_all() + + +if __name__ == "__main__": + print("Add -v for more debug output") + import sys + + if "-v" in sys.argv: + debug = True + pybamm.settings.debug_mode = True + unittest.main() diff --git a/tests/unit/test_models/test_submodels/test_external_circuit/test_power_control.py b/tests/unit/test_models/test_submodels/test_external_circuit/test_power_control.py new file mode 100644 index 0000000000..0f1b71d35f --- /dev/null +++ b/tests/unit/test_models/test_submodels/test_external_circuit/test_power_control.py @@ -0,0 +1,26 @@ +# +# Test power control submodel +# + +import pybamm +import tests +import unittest + + +class TestPowerControl(unittest.TestCase): + def test_public_functions(self): + param = pybamm.standard_parameters_lithium_ion + submodel = pybamm.external_circuit.PowerControl(param) + variables = {"Terminal voltage [V]": pybamm.Scalar(0)} + std_tests = tests.StandardSubModelTests(submodel, variables) + std_tests.test_all() + + +if __name__ == "__main__": + print("Add -v for more debug output") + import sys + + if "-v" in sys.argv: + debug = True + pybamm.settings.debug_mode = True + unittest.main() diff --git a/tests/unit/test_models/test_submodels/test_external_circuit/test_voltage_control.py b/tests/unit/test_models/test_submodels/test_external_circuit/test_voltage_control.py new file mode 100644 index 0000000000..ccf29c8b79 --- /dev/null +++ b/tests/unit/test_models/test_submodels/test_external_circuit/test_voltage_control.py @@ -0,0 +1,26 @@ +# +# Test voltage control submodel +# + +import pybamm +import tests +import unittest + + +class TestVoltageControl(unittest.TestCase): + def test_public_functions(self): + param = pybamm.standard_parameters_lithium_ion + submodel = pybamm.external_circuit.VoltageControl(param) + variables = {"Terminal voltage [V]": pybamm.Scalar(0)} + std_tests = tests.StandardSubModelTests(submodel, variables) + std_tests.test_all() + + +if __name__ == "__main__": + print("Add -v for more debug output") + import sys + + if "-v" in sys.argv: + debug = True + pybamm.settings.debug_mode = True + unittest.main()