Skip to content

Commit fcf7553

Browse files
committed
#492 added get voltage classes
1 parent 3ef3e59 commit fcf7553

File tree

5 files changed

+166
-0
lines changed

5 files changed

+166
-0
lines changed

pybamm/parameters/standard_voltage_functions/__init__.py

Whitespace-only changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#
2+
# Base class for setting/getting voltage
3+
#
4+
5+
6+
class GetVoltage(object):
7+
"""
8+
The base class for setting the input voltage for a simulation. The parameters
9+
dictionary holds the symbols of any parameters required to evaluate the voltage.
10+
During processing, the evaluated parameters are stored in parameters_eval.
11+
"""
12+
13+
def __init__(self):
14+
self.parameters = {}
15+
self.parameters_eval = {}
16+
17+
def __str__(self):
18+
return "Base voltage"
19+
20+
def __call__(self, t):
21+
return 1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#
2+
# Constant voltage "function"
3+
#
4+
import pybamm
5+
6+
7+
class GetConstantVoltage(pybamm.GetVoltage):
8+
"""
9+
Sets a constant input voltage for a simulation.
10+
11+
Parameters
12+
----------
13+
voltage : :class:`pybamm.Symbol` or float
14+
The size of the voltage in Volts.
15+
16+
**Extends:"": :class:`pybamm.GetVoltage`
17+
"""
18+
19+
def __init__(self, voltage=pybamm.electrical_parameters.V_typ):
20+
self.parameters = {"Cell voltage [V]": voltage}
21+
self.parameters_eval = {"Cell voltage [V]": voltage}
22+
23+
def __str__(self):
24+
return "Constant voltage"
25+
26+
def __call__(self, t):
27+
return self.parameters_eval["Cell voltage [V]"]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#
2+
# Load voltage profile from a csv file
3+
#
4+
import pybamm
5+
import os
6+
import pandas as pd
7+
import numpy as np
8+
import warnings
9+
import scipy.interpolate as interp
10+
11+
12+
class GetVoltageData(pybamm.GetVoltage):
13+
"""
14+
A class which loads a voltage profile from a csv file and creates an
15+
interpolating function which can be called during solve.
16+
17+
Parameters
18+
----------
19+
filename : str
20+
The name of the file to load.
21+
units : str, optional
22+
The units of the voltage data which is to be loaded. Can be "[]" for
23+
dimenionless data (default), or "[V]" for voltage in Volts.
24+
voltage_scale : :class:`pybamm.Symbol` or float, optional
25+
The scale the voltage in Volts if loading non-dimensional data. Default
26+
is to use the typical voltage V_typ
27+
28+
**Extends:"": :class:`pybamm.GetVoltage`
29+
"""
30+
31+
def __init__(
32+
self, filename, units="[]", voltage_scale=pybamm.electrical_parameters.V_typ
33+
):
34+
self.parameters = {"Voltage [V]": voltage_scale}
35+
self.parameters_eval = {"Voltage [V]": voltage_scale}
36+
37+
# Load data from csv
38+
if filename:
39+
pybamm_path = pybamm.root_dir()
40+
data = pd.read_csv(
41+
os.path.join(pybamm_path, "input", "drive_cycles", filename),
42+
comment="#",
43+
skip_blank_lines=True,
44+
).to_dict("list")
45+
46+
self.time = np.array(data["time [s]"])
47+
self.units = units
48+
self.voltage = np.array(data["voltage " + units])
49+
# If voltage data is present, load it into the class
50+
else:
51+
raise pybamm.ModelError("No input file provided for voltage")
52+
53+
def __str__(self):
54+
return "Voltage from data"
55+
56+
def interpolate(self):
57+
" Creates the interpolant from the loaded data "
58+
# If data is dimenionless, multiply by a typical voltage (e.g. data
59+
# could be C-rate and voltage the 1C discharge voltage). Otherwise,
60+
# just import the voltage data.
61+
if self.units == "[]":
62+
voltage = self.parameters_eval["Cell voltage [V]"] * self.voltage
63+
elif self.units == "[V]":
64+
voltage = self.voltage
65+
else:
66+
raise pybamm.ModelError(
67+
"Voltage data must have units [V] or be dimensionless"
68+
)
69+
# Interpolate using Piecewise Cubic Hermite Interpolating Polynomial
70+
# (does not overshoot non-smooth data)
71+
self.voltage_interp = interp.PchipInterpolator(self.time, voltage)
72+
73+
def __call__(self, t):
74+
"""
75+
Calls the interpolating function created using the data from user-supplied
76+
data file at time t (seconds).
77+
"""
78+
79+
if np.min(t) < self.time[0] or np.max(t) > self.time[-1]:
80+
warnings.warn(
81+
"Requested time ({}) is outside of the data range [{}, {}]".format(
82+
t, self.time[0], self.time[-1]
83+
),
84+
pybamm.ModelWarning,
85+
)
86+
87+
return self.voltage_interp(t)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Allow a user-defined voltage function
3+
#
4+
import pybamm
5+
6+
7+
class GetUserVoltage(pybamm.GetVoltage):
8+
"""
9+
Sets a user-defined function as the input voltage for a simulation.
10+
11+
Parameters
12+
----------
13+
function : method
14+
The method which returns the voltage (in Volts) as a function of time
15+
(in seconds). The first argument of function must be time, followed by
16+
any keyword arguments, i.e. function(t, **kwargs).
17+
**kwargs : Any keyword arguments required by function.
18+
19+
**Extends:"": :class:`pybamm.GetVoltage`
20+
"""
21+
22+
def __init__(self, function, **kwargs):
23+
self.parameters = kwargs
24+
self.parameters_eval = kwargs
25+
self.function = function
26+
27+
def __str__(self):
28+
return "User defined voltage"
29+
30+
def __call__(self, t):
31+
return self.function(t, **self.parameters_eval)

0 commit comments

Comments
 (0)