Importing a current trace into PyBaMM requires separate checks on the input knots, the solver’s internal steps, and the saved output times. In the Chen2020/SPMe example below, a pulsed current and its constant mean both deliver 0.583333 Ah, yet their voltage traces differ by up to 100.3 mV. With the same lumped thermal model, their peak temperatures are 27.284 °C and 25.791 °C.
This article uses a synthetic profile with discharge, rest and regenerative charging to test averaging, input downsampling, output subsampling, current sign and units. The downloadable script saves every input, the termination reason and the integrated charge so that the same checks can be applied to another trace.
PyBaMM battery modelling series · Data pipeline (part 5 of 5): Previous: Training a Battery AI Model with PyBaMM: Predicting SOH and RUL · Series hub: Data pipeline (5) + Silent failures, measured (8) · RSS feed.
1. Three time grids with different jobs
The timestamps in the input define the current function. Here, linear interpolation joins adjacent knots before the function enters the electrochemical model. Removing knots changes the current between the retained points and therefore changes the imposed operating condition.
The solver chooses internal steps during integration. For the IDAKLU solver used here, t_eval can specify times that integration must reach, while t_interp specifies saved output times. Saving one value per second does not mean that integration takes one step per second. PyBaMM’s time-point documentation distinguishes these arguments.
After a solve, selecting every tenth row is a separate post-processing operation. We first retain the original 1 s input and output, then change either the input or the readout. This separates their effects.
2. Start with an input whose charge can be checked
The fixture repeats every 60 s. At integer-second knots it supplies 5 A for the first 10 s, 0 A for 10 s, −2 A for 5 s, 2 A for 15 s, then 0 A. Linear interpolation between knots gives each switch a 1 s transition. Positive current denotes discharge; negative current denotes charging.
The columns are named time_s,current_A. Time starts at zero and increases strictly. Duplicate timestamps and non-finite values are rejected. Convert absolute timestamps or milliseconds into elapsed seconds before constructing the interpolant.
import numpy as np
import pybamm
data = np.loadtxt("pulse_fine.csv", delimiter=",", skiprows=1)
t, current = data.T
assert np.isfinite(data).all()
assert t[0] == 0 and np.all(np.diff(t) > 0)
pv = pybamm.ParameterValues("Chen2020")
pv["Current function [A]"] = pybamm.Interpolant(
t, current, pybamm.t, interpolator="linear"
)
sim = pybamm.Simulation(
pybamm.lithium_ion.SPMe(),
parameter_values=pv,
solver=pybamm.IDAKLUSolver(rtol=1e-8, atol=1e-10),
)
sol = sim.solve([0, t[-1]], t_interp=t, initial_soc=0.6)
q_input = np.trapezoid(current, t) / 3600
q_state = float(sol["Discharge capacity [A.h]"](t[-1]))
print(f"input={q_input:.6f} Ah, state={q_state:.6f} Ah")
input=0.583333 Ah, state=0.583333 Ah
With current in amperes and time in seconds, net charge in ampere-hours is
\[Q_{\mathrm{net}}=\frac{1}{3600}\int_0^T I(t)\,\mathrm{d}t.\]
The trapezoidal rule gives the exact area of this piecewise-linear input. We compare it with the model’s integrated state. The variable Discharge capacity [A.h] accumulates signed charge from the start of this run; it becomes negative in the reversed-current control below. It is distinct from SOH and available cell capacity, whose definitions are examined in the capacity-variable article.
3. Equal ampere-hours do not preserve voltage or temperature
Replacing the trace with its mean, 1.166667 A, preserves the net charge of 0.583333 Ah over 1800 s. The pulsed input has an RMS current of 2.301 A; the constant mean has an RMS value of 1.167 A.
| Input | Net charge / Ah | Minimum voltage / V | Maximum voltage / V | Final voltage / V |
|---|---|---|---|---|
| Original 1 s profile | 0.583333 | 3.5783 | 3.8881 | 3.5950 |
| Constant mean | 0.583333 | 3.6718 | 3.8059 | 3.6718 |
Matching net charge constrains the current integral. Terminal voltage also depends on instantaneous current, polarization and the preceding concentration history, so averaging changes the voltage trajectory. The largest difference at a common time is 100.3 mV. With matching default thermal parameters, the peak temperature rises are 2.284 K and 0.791 K. The ohmic heating term contains current squared, which averaging also changes.
4. Input and output sampling can produce similar-looking charge errors
First retain an input knot every 10, 30 or 60 s, join the retained points linearly, and solve again. This table uses sampling phase zero. Voltage differences are evaluated against the original-input solution on the same 1 s grid.
| Input spacing | Input integral / Ah | Model charge state / Ah | Maximum voltage difference / mV |
|---|---|---|---|
| 1 s | 0.583333 | 0.583333 | 0 |
| 10 s | 0.416667 | 0.416667 | 141.5 |
| 30 s | 1.750000 | 1.750000 | 370.7 |
| 60 s | 2.500000 | 2.500000 | 675.2 |
Sampling every 60 s selects the same phase of the cycle. The new input becomes a constant 5 A, and the solver integrates it to 2.5 Ah. The file now imposes a different current. Tighter tolerances cannot recover the removed rest and charging intervals.
Next retain the original input and solution, but read current at sparse times during post-processing. Applying the trapezoidal rule to those samples produces the same charge figures. The original model state remains 0.583333 Ah.
| Readout spacing | Reintegrated readout / Ah | Original model charge state / Ah |
|---|---|---|
| 1 s | 0.583333 | 0.583333 |
| 10 s | 0.416667 | 0.583333 |
| 30 s | 1.750000 | 0.583333 |
| 60 s | 2.500000 | 0.583333 |
When an exported CSV has an unexpected charge integral, compare three quantities: the input integral, the saved model charge state, and the integral of the exported current. Agreement between the first two localizes the discrepancy to readout or export. A changed model state calls for checking the current that actually entered the model.
Sampling phase matters too. The downloads contain three phases for each interval and a second, smooth input family. Sampling that smooth current every 60 s at phase zero preserves 0.583333 Ah, while its voltage differs from the fine-input solution by up to 43.81 mV. A charge-only check misses this case. Choose a recording interval that resolves the pulses, switches and dynamics of interest; these periodic fixtures do not establish an error magnitude for measured driving data.
5. Interpolating sparse saved states can lose a transient
The readout table above post-processes one fine solution. A separate 120 s control changes t_interp during the solve: one run saves every second; the other saves only 0, 60 and 120 s. Both end at 0.03888885 Ah and 3.70301710 V.
Querying voltage at 15 s afterwards gives 3.82157550 V from the fine saved solution and 3.71901534 V from the sparse one, a difference of 102.56 mV. A query between sparse saved times interpolates the saved variable values and cannot reliably reconstruct an omitted voltage transient. The saved grid must also resolve a short voltage drop when that is the quantity being studied.
For the experiment route, pybamm.step.current(data), inspect the returned sol.t. In this single-step drive-cycle control on 26.10.1.1, supplying a sparse t_interp still returned 121 points. The downloadable helper restricts custom output_times to the Interpolant route so that the two interfaces’ saving behavior is kept explicit.
6. Sign, units and numerical controls
Reversing the current column produces −0.583333 Ah and changes final voltage from 3.5950 V to 4.1075 V. Both runs reach 1800 s. Completion alone cannot establish that the cycler’s sign convention matches the model’s convention. Check a segment that is known to be discharging.
Chen2020 has a nominal capacity of 5 Ah. A column expressed in C-rate must therefore be multiplied by 5 to obtain the corresponding current in amperes. Dividing the correct ampere values by 5 and importing those numbers as amperes gives 0.116667 Ah and a final voltage of 3.7874 V. The column label should identify the units after conversion.
| Control | Result at common evaluation times |
|---|---|
| Interpolant versus step.current route | Maximum voltage difference below 0.001 mV for the original profile |
| Tighten rtol/atol to 10⁻¹⁰/10⁻¹² | Maximum voltage difference below 0.001 mV |
| Replace SPMe with DFN, retaining the input | Maximum voltage difference 1.857 mV |
| Duplicate times, nonzero start, non-finite value | Input validation rejects each of these three cases |
These controls separate input changes from differences due to the interface, tolerance and model choice. All 29 main cases reach the requested endpoint. Their raw results retain termination reasons; when another trace triggers a cutoff, report the interval that completed. The solver troubleshooting article covers convergence diagnosis.
7. Reproduction and measurement conditions
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
python drive_cycle_lab.py --output results
python plot_results.py --results results --output figures
Download the complete lab, or obtain the solver script, 29-case summary and readout results separately. The archive includes pulse_fine.csv, all generated inputs and saved arrays. The short output-grid control is output_grid_probe.py.
The environment is PyBaMM 26.10.1.1, Python 3.13.9, Apple M3. Main cases use Chen2020, initial SOC 0.6, a duration of 1800 s, isothermal SPMe at 25 °C, and IDAKLU with rtol/atol of 10⁻⁸/10⁻¹⁰. Thermal cases use the lumped model with Chen2020’s default thermal parameters. DFN and tighter tolerances are separate controls. The current consists of the generated repeating and smooth fixtures described above; temperatures are calculated results for these models and parameters.
For another trace, retain the original current and its hash, record units and sign, compare charge integrals before and after modeling, and choose saved times for the dynamics of interest. These checks establish what current the model received before electrochemical parameters are adjusted.
References
A tool I built
Validate your cell model on independent data
VoltSift is an online workspace I built. Upload three CSV files (a fitting record, an independent validation record and an open-circuit voltage curve), and it fits a cell model, checks voltage error and coverage on the held-out record, and produces a report. The standard workflow supports single NMC cells at 25°C and SOC 20%–100%. Register and verify your email to get one free analysis, available for seven days.
Visit VoltSift · Prepare the three CSV files · How to read the validation results
