Skip to content

Commit

Permalink
#492 fixing more tests and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
valentinsulzer committed Nov 30, 2019
1 parent 4238ad1 commit 194c707
Show file tree
Hide file tree
Showing 12 changed files with 128 additions and 139 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

## Features

- Added submodels for operating modes other than current-controlled ([#751](https://github.com/pybamm-team/PyBaMM/pull/751))
- Generalized importing of external variables ([#728](https://github.com/pybamm-team/PyBaMM/pull/728))
- Separated active and inactive material volume fractions ([#726](https://github.com/pybamm-team/PyBaMM/pull/726))
- Added submodels for tortuosity ([#726](https://github.com/pybamm-team/PyBaMM/pull/726))
Expand Down Expand Up @@ -31,6 +32,7 @@

## Bug fixes

- Improved automatic broadcasting ([#747](https://github.com/pybamm-team/PyBaMM/pull/747))
- Fixed bug with wrong temperature in initial conditions ([#737](https://github.com/pybamm-team/PyBaMM/pull/737))
- Improved flexibility of parameter values so that parameters (such as diffusivity or current) can be set as functions or scalars ([#723](https://github.com/pybamm-team/PyBaMM/pull/723))
- Fixed a bug where boundary conditions were sometimes handled incorrectly in 1+1D models ([#713](https://github.com/pybamm-team/PyBaMM/pull/713))
Expand All @@ -46,6 +48,7 @@
- The parameters "Bruggeman coefficient" must now be specified separately as "Bruggeman coefficient (electrolyte)" and "Bruggeman coefficient (electrode)"
- The current classes (`GetConstantCurrent`, `GetUserCurrent` and `GetUserData`) have now been removed. Please refer to the [`change-input-current` notebook](https://github.com/pybamm-team/PyBaMM/blob/master/examples/notebooks/change-input-current.ipynb) for information on how to specify an input current
- Parameter functions must now use pybamm functions instead of numpy functions (e.g. `pybamm.exp` instead of `numpy.exp`), as these are then used to construct the expression tree directly. Generally, pybamm syntax follows numpy syntax; please get in touch if a function you need is missing.
- The current must now be updated by changing "Current function [A]" or "C-rate" instead of "Typical current [A]"


# [v0.1.0](https://github.com/pybamm-team/PyBaMM/tree/v0.1.0) - 2019-10-08
Expand Down
6 changes: 4 additions & 2 deletions docs/source/models/submodels/external_circuit/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,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
relationship between the current and voltage. "Current control" and "Voltage control"
submodels enforce these directly through boundary conditions, while "Function control"
submodels add an algebraic equation (for the current) and hence can be used to set any
variable to be constant.

.. toctree::
:maxdepth: 1

current_control_external_circuit
function_control_external_circuit
power_control_external_circuit
voltage_control_external_circuit
75 changes: 19 additions & 56 deletions examples/notebooks/change-input-current.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"\n",
"In this notebook we will use the SPM as the example model, and change the input current from the default option. If you are not familiar with running a model in PyBaMM, please see [this](./models/SPM.ipynb) notebook for more details.\n",
"\n",
"In PyBaMM, the current function is set using the parameter \"Current function\". This can be a scalar, but only accepts values 0 and 1. By default this is set to be a constant current by setting it to '1'. The size of a constant current input is changed by changing the parameter \"Typical current [A]\". Below we load the SPM with the default parameters, and then change the the typical current 16A. We then explicitly set the current function to be a constant current."
"In PyBaMM, the current function is set using the parameter \"Current function [A]\". Below we load the SPM with the default parameters, and then change the the current function to 16A."
]
},
{
Expand All @@ -45,9 +45,8 @@
"# set the default model parameters\n",
"param = model.default_parameter_values\n",
"\n",
"# change the typical current and set a constant discharge using the typical current value\n",
"param[\"Typical current [A]\"] = 16\n",
"param[\"Current function\"] = \"[constant]\"\n"
"# change the current function\n",
"param[\"Current function [A]\"] = 16"
]
},
{
Expand All @@ -67,12 +66,12 @@
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "26ae7952fb10438ebbf83297fe284014",
"model_id": "118979747d4f418982a358d3769dc257",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.02, step=0.005), Output()), _dom_classes=("
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.00787878787878788, step=0.005), Output()),…"
]
},
"metadata": {},
Expand Down Expand Up @@ -117,7 +116,7 @@
"metadata": {},
"outputs": [],
"source": [
"param[\"Current function\"] = \"[zero]\""
"param[\"Current function [A]\"] = 0"
]
},
{
Expand All @@ -131,31 +130,11 @@
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"param[\"Current function\"]"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "630250e18bf6416089e4bed2a883c10e",
"model_id": "397b778427504eb8ac88b53de8397683",
"version_major": 2,
"version_minor": 0
},
Expand Down Expand Up @@ -187,39 +166,23 @@
"source": [
"## Loading in current data <a name=\"data\"></a>\n",
"\n",
"Data can be loaded in from a csv file by specifying the path to that file and using the prefix \"[current data]\"."
"Data can be loaded in from a csv file by putting the file in the folder 'input/drive_cycles' and using the prefix \"[current data]\". As an example, we show how to solve the SPM using the US06 drive cycle"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"param[\"Current function\"] = \"[current data]US06\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"As an example, we show how to solve the SPM using the US06 drive cycle"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "18118e75c01e4fb1ba9a00134298b5fd",
"model_id": "4acb95c01295406db377f5dd1593e014",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.026526276390989537, step=0.001), Output())…"
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.057314594406781015, step=0.001), Output())…"
]
},
"metadata": {},
Expand All @@ -234,7 +197,7 @@
"\n",
"# load parameter values and process model and geometry\n",
"param = model.default_parameter_values\n",
"param[\"Current function\"] = \"[current data]US06\"\n",
"param[\"Current function [A]\"] = \"[current data]US06\"\n",
"param.process_model(model)\n",
"param.process_geometry(geometry)\n",
"\n",
Expand Down Expand Up @@ -279,7 +242,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -301,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -316,7 +279,7 @@
"# set user defined current function\n",
"A = pybamm.electrical_parameters.I_typ\n",
"omega = 0.1\n",
"param[\"Current function\"] = my_fun(A,omega)\n",
"param[\"Current function [A]\"] = my_fun(A,omega)\n",
"\n",
"# process model and geometry\n",
"param.process_model(model)\n",
Expand All @@ -332,18 +295,18 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"application/vnd.jupyter.widget-view+json": {
"model_id": "b6fae116be424e22955be046e20a6211",
"model_id": "19720a31f3964e55b01905476f50393a",
"version_major": 2,
"version_minor": 0
},
"text/plain": [
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.0013263138195494769, step=6.63156909774738"
"interactive(children=(FloatSlider(value=0.0, description='t', max=0.0028657297203390506, step=0.00014328648601"
]
},
"metadata": {},
Expand Down Expand Up @@ -398,7 +361,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.7.4"
}
},
"nbformat": 4,
Expand Down
25 changes: 7 additions & 18 deletions examples/notebooks/compare-comsol-discharge-curve.ipynb

Large diffs are not rendered by default.

Binary file modified examples/notebooks/models/spm1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified examples/notebooks/models/spm2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
97 changes: 60 additions & 37 deletions examples/notebooks/using-submodels.ipynb

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions examples/scripts/compare_comsol/discharge_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@

# solve model at comsol times
t = comsol_time / tau
solution = model.default_solver.solve(model, t)
solution = pybamm.CasadiSolver(mode="fast").solve(model, t)

# discharge capacity
discharge_capacity = pybamm.ProcessedVariable(
model.variables["Discharge capacity [A.h]"], solution.t, solution.y, mesh=mesh
)
discharge_capacity_sol = discharge_capacity(solution.t)
comsol_discharge_capacity = comsol_time * param["Typical current [A]"] / 3600
comsol_discharge_capacity = comsol_time * param["Current function [A]"] / 3600

# extract the voltage
voltage = pybamm.ProcessedVariable(
Expand Down
17 changes: 5 additions & 12 deletions examples/scripts/compare_lead_acid.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,16 @@
pybamm.set_logging_level("INFO")

# load models
options = {"operating mode": "voltage"}
models = [
pybamm.lead_acid.LOQS(options),
# pybamm.lead_acid.FOQS(options),
# pybamm.lead_acid.Composite(options),
pybamm.lead_acid.Full(options),
pybamm.lead_acid.LOQS(),
# pybamm.lead_acid.FOQS(),
# pybamm.lead_acid.Composite(),
pybamm.lead_acid.Full(),
]

# load parameter values and process models and geometry
param = models[0].default_parameter_values
param.update(
{
"Current function [A]": 10,
"Initial State of Charge": 1,
"Voltage function [V]": 14 / 6,
}
)
param.update({"Current function [A]": 10, "Initial State of Charge": 1})
for model in models:
param.process_model(model)

Expand Down
3 changes: 2 additions & 1 deletion examples/scripts/compare_lithium_ion.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

# load parameter values and process models and geometry
param = models[0].default_parameter_values
param["Typical current [A]"] = 1.0
param["Current function [A]"] = 1.0

for model in models:
param.process_model(model)

Expand Down
3 changes: 3 additions & 0 deletions examples/scripts/custom_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
model = pybamm.lithium_ion.BaseModel(name="my li-ion model")

# set choice of submodels
model.submodels["external circuit"] = pybamm.external_circuit.CurrentControl(
model.param
)
model.submodels["current collector"] = pybamm.current_collector.Uniform(model.param)
model.submodels["thermal"] = pybamm.thermal.isothermal.Isothermal(model.param)
model.submodels["negative electrode"] = pybamm.electrode.ohm.LeadingOrder(
Expand Down
34 changes: 23 additions & 11 deletions pybamm/parameters/parameter_values.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,6 @@ def __setitem__(self, key, value):
self.update({key: value})

def update(self, values, check_conflict=False, path=""):
# check parameter values
values = self.check_and_update_parameter_values(values)
# update
for name, value in values.items():
# check for conflicts
Expand All @@ -157,9 +155,11 @@ def update(self, values, check_conflict=False, path=""):
# Functions are flagged with the string "[function]"
if isinstance(value, str):
if value.startswith("[function]"):
self[name] = pybamm.load_function(
loaded_value = pybamm.load_function(
os.path.join(path, value[10:] + ".py")
)
super().__setitem__(name, loaded_value)
values[name] = loaded_value
# Data is flagged with the string "[data]" or "[current data]"
elif value.startswith("[current data]") or value.startswith(
"[data]"
Expand All @@ -178,11 +178,15 @@ def update(self, values, check_conflict=False, path=""):
).to_numpy()
# Save name and data
super().__setitem__(name, (function_name, data))
values[name] = (function_name, data)
# Anything else should be a converted to a float
else:
super().__setitem__(name, float(value))
values[name] = float(value)
else:
super().__setitem__(name, value)
# check parameter values
self.check_and_update_parameter_values(values)
# reset processed symbols
self._processed_symbols = {}

Expand Down Expand Up @@ -214,18 +218,24 @@ def check_and_update_parameter_values(self, values):
if "C-rate" in values:
# Can't provide C-rate as a function
if callable(values["C-rate"]):
values["Current function [A]"] = CrateToCurrent(
values["C-rate"], capacity
)
value = CrateToCurrent(values["C-rate"], capacity)
elif isinstance(values["C-rate"], tuple):
data = values["C-rate"][1]
data[:, 1] *= capacity
value = (values["C-rate"][0] + "_toCrate", data)
else:
values["Current function [A]"] = float(values["C-rate"]) * capacity
value = values["C-rate"] * capacity
super().__setitem__("Current function [A]", value)
elif "Current function [A]" in values:
if callable(values["Current function [A]"]):
values["C-rate"] = CurrentToCrate(
values["Current function [A]"], capacity
)
value = CurrentToCrate(values["Current function [A]"], capacity)
elif isinstance(values["Current function [A]"], tuple):
data = values["Current function [A]"][1]
data[:, 1] /= capacity
value = (values["Current function [A]"][0] + "_toCurrent", data)
else:
values["C-rate"] = float(values["Current function [A]"]) / capacity
value = values["Current function [A]"] / capacity
super().__setitem__("C-rate", value)

return values

Expand Down Expand Up @@ -538,6 +548,7 @@ def evaluate(self, symbol):

class CurrentToCrate:
"Convert a current function to a C-rate function"

def __init__(self, function, capacity):
self.function = function
self.capacity = capacity
Expand All @@ -548,6 +559,7 @@ def __call__(self, t):

class CrateToCurrent:
"Convert a C-rate function to a current function"

def __init__(self, function, capacity):
self.function = function
self.capacity = capacity
Expand Down

0 comments on commit 194c707

Please sign in to comment.