|
| 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) |
0 commit comments