How chaffybird56/batterypack works
BatteryPack is a Python library for offline electro-thermal simulation of lithium-ion packs, not a deployed service. The architecture is centered on a pipeline that starts with chemistry/configuration data, feeds a cell-and-pack model, then runs drive-cycle or charge simulations, and finally emits metrics, plots, exports, and safety evaluations.
1. Entry points and user-facing surface
The main public API appears to be battery_pack.pack.BatteryPack, with supporting helpers in config.py such as cell_params_for_chemistry(). Scripts in scripts/ provide reproducible demos, sweeps, UPS sizing runs, ML training, and README plot generation.
2. Configuration and chemistry presets
config.py and config_loader.py define how cell and pack parameters are loaded and standardized. This is the boundary where chemistry-specific values like LFP, NMC811, NCA, and LCO are turned into model inputs for the rest of the system.
3. Core cell model
cell.py likely implements the first-order equivalent-circuit model described in the README: an ECM with R0 + R1||C1, plus SoC/temperature-dependent behavior. This is the lowest-level physical abstraction used by pack-level simulation.
4. Pack-level electrical model
pack.py is the main composition layer, assembling cells into an Ns × Np pack. This is where pack voltage, current, power, state of charge, and efficiency are computed from the cell model and operating profile.
5. Thermal modeling
thermal.py and thermal_network.py handle lumped and optionally multi-node thermal behavior. Electrical losses from the pack model are translated into heat generation, then into temperature rise and thermal gradients.
6. Simulation orchestration
simulation.py appears to orchestrate charge/discharge runs and round-trip efficiency calculations. It is the primary “engine” that accepts a pack, a load/charge profile, and time-step settings, then produces time-series results.
7. Load, mission, and drive-cycle inputs
drive_cycles.py, drive_cycles_real.py, and mission.py define the external demand imposed on the pack. These modules convert synthetic cycles, CSV-like profiles, and automotive-style duty cycles into simulation inputs; they are the main upstream data source for the engine.
8. Charging and power-limit behavior
charging.py, limits.py, and bms.py define operational constraints and charge behavior. This is the boundary where max charge/discharge power, voltage/current/temperature limits, and BMS-style protection logic influence whether the pack can continue operating.
9. Safety and fault screening
safety.py adds failure-mode and thermal-runaway screening, including hazard-style outputs and likely FMEA-oriented checks. This is a separate safety analysis layer rather than part of the base electrical model, which matters for diagramming the safety boundary.
10. Post-processing and reporting
metrics.py, export.py, and plots.py handle result summarization and output formats. The repo generates CSV/JSON/HDF5-style artifacts and figures; the outputs/ tree shows these are saved as run-specific experiment outputs rather than source code.
11. Advanced analysis modules
aging.py, economics.py, ups_backup.py, sweep.py, uncertainty.py, variation.py, validation.py, and ml.py extend the core simulation into adjacent analysis domains:
- aging/lifetime behavior
- economics and LCOE-style analysis
- UPS backup sizing
- Monte Carlo / parameter sweeps
- cell-to-cell or parameter variation
- validation against expected behavior
- ML-based surrogate or support tooling
12. External adapter boundary
pybamm_adapter.py is a notable integration point. It suggests optional interop with PyBaMM, so the repo can either run its own lightweight ECM stack or adapt to a more detailed battery modeling backend.
13. Developer and automation layer
.github/workflows/ci.yml runs the test and formatting checks in CI. tests/ provides coverage for basic behavior, advanced flows, and safety rules, which mirrors the library’s main architectural slices.
14. Overall data flow
The canonical flow is: chemistry/config → cell model → pack + thermal model → drive cycle/charge profile → simulation engine → safety/limits/metrics → export/plots/analysis. Advanced modules branch off after or alongside simulation results rather than replacing the core model.
15. Artifact and run structure
outputs/ contains generated demo, sweep, ML, and advanced-demo results, which indicates the repo is organized around reproducible batch experiments. These outputs are downstream artifacts and should be treated as generated data, not part of the core runtime.
Main entry points: simulation.py Simulation · pack.py Pack model · thermal.py Thermal model · config.py Config
Components 34
Core simulation
Chemistry, cell, pack, thermal, and simulation engine.
- Config · Loads and standardizes cell and pack parameters for supported chemistries.
battery_pack/config.py - Loader · Configuration loading and normalization boundary.
battery_pack/config_loader.py - Cell model · Equivalent-circuit cell behavior with SoC and temperature dependence.
battery_pack/cell.py - Pack model · Assembles cells into an Ns x Np pack and computes pack-level electrical behavior.
battery_pack/pack.py - Advanced pack · Higher-level pack behaviors built on the core pack model.
battery_pack/pack_advanced.py - Thermal model · Maps electrical losses into heat and temperature rise.
battery_pack/thermal.py - Thermal net · Thermal network representation for gradients and coupled nodes.
battery_pack/thermal_network.py - Simulation · Orchestrates charge and discharge runs over profiles and time steps.
battery_pack/simulation.py
Inputs
Profiles and mission sources that drive simulation.
- Drive cycles · Synthetic and standard drive-cycle inputs for simulation.
battery_pack/drive_cycles.py - Real cycles · Real-world or CSV-like duty-cycle inputs.
battery_pack/drive_cycles_real.py - Mission · Mission-style operating profiles and workload definitions.
battery_pack/mission.py
Analysis
Post-processing, extensions, safety, and external adapters.
- Charging · Charge behavior and operating transitions during charging.
battery_pack/charging.py - Limits · Current, voltage, power, and temperature constraints.
battery_pack/limits.py - BMS · BMS-style protection and decision logic around allowable operation.
battery_pack/bms.py - Safety · Safety evaluation and thermal-runaway style screening.
battery_pack/safety.py - Metrics · Result aggregation and performance metrics.
battery_pack/metrics.py - Export · Writes simulation results to files and external formats.
battery_pack/export.py - Plots · Generates figures and report graphics from results.
battery_pack/plots.py - Aging · Battery degradation and lifetime-related analysis.
battery_pack/aging.py - Economics · Economic and lifecycle cost analysis.
battery_pack/economics.py - UPS sizing · UPS backup sizing and runtime estimation.
battery_pack/ups_backup.py - Sweeps · Batch exploration of model parameters and scenarios.
battery_pack/sweep.py - Uncertainty · Uncertainty and stochastic variation analysis.
battery_pack/uncertainty.py - Variation · Cell-to-cell or parameter variation analysis.
battery_pack/variation.py - Validation · Validation against expected behavior and reference outcomes.
battery_pack/validation.py - ML · Machine-learning support or surrogate modeling.
battery_pack/ml.py - PyBaMM · Optional integration boundary to PyBaMM-based modeling.
battery_pack/pybamm_adapter.py
Tooling
Scripts and CI around the library.
- Demo · Reproducible demo run for the core simulation pipeline.
scripts/run_demo.py - Advanced demo · Runs extended scenarios and advanced analysis flows.
scripts/run_advanced_demo.py - Sweeps run · Executes batch parameter sweeps.
scripts/run_sweeps.py - UPS demo · Runs UPS backup sizing demonstrations.
scripts/run_ups_demo.py - Train ML · Trains ML support models or surrogates.
scripts/train_ml.py - README plots · Builds documentation figures and example plots.
scripts/generate_readme_plots.py - CI · Continuous integration for tests and checks.
.github/workflows/ci.yml
Connections 33
- Loader → Config: normalizes · Loads and standardizes chemistry and pack parameters.
- Config → Cell model: feeds · Provides cell parameter sets to the cell model.
- Cell model → Pack model: builds · Cell model is composed into pack-level simulation.
- Pack model → Thermal model: losses to heat · Electrical losses become thermal inputs.
- Thermal model → Thermal net: extends · Thermal network refines lumped thermal behavior.
- Pack model → Simulation: simulates · Pack model is executed by the simulation engine.
- Thermal model → Simulation: couples · Thermal state feeds time-step simulation.
- Drive cycles → Simulation: profiles · Drive-cycle inputs drive simulation runs.
- Real cycles → Simulation: profiles · Real-world profiles are converted into simulation inputs.
- Mission → Simulation: profiles · Mission definitions act as upstream demand.
- Charging → Simulation: controls · Charging behavior shapes simulation trajectories.
- Limits → BMS: constrains · Operating limits inform BMS protection decisions.
- BMS → Simulation: gates · Protection logic can stop or limit operation.
- Safety → Simulation: evaluates · Safety screening analyzes simulation outcomes.
- Simulation → Metrics: produces · Simulation results are summarized into metrics.
- Simulation → Export: emits · Results are exported to files and artifacts.
- Simulation → Plots: visualizes · Time series and summaries are plotted.
- Simulation → Aging: analyzes · Simulation outputs can be reused for aging studies.
- Simulation → Economics: analyzes · Outputs feed economic analysis.
- Simulation → UPS sizing: sizes · Simulation supports UPS runtime sizing.
- Simulation → Sweeps: explores · Batch sweeps vary model parameters and scenarios.
- Simulation → Uncertainty: samples · Uncertainty analysis wraps repeated simulations.
- Simulation → Variation: varies · Variation analysis probes pack and cell differences.
- Simulation → Validation: checks · Validation compares outputs against expectations.
- Simulation → ML: trains · ML tooling consumes simulation data for surrogates.
- PyBaMM → Pack model: adapts · Optional adapter boundary for alternate battery backend integration.
- Demo → Simulation: drives · Demo script exercises the main simulation pipeline.
- Advanced demo → Advanced pack: drives · Advanced demo exercises extended pack behaviors.
- Sweeps run → Sweeps: runs · Sweep script executes parameter exploration.
- UPS demo → UPS sizing: runs · UPS demo executes backup sizing flows.
- Train ML → ML: trains · Training script builds ML artifacts.
- README plots → Plots: renders · Documentation script generates showcase figures.
- CI → Demo: checks · CI validates the demo path and basic behavior.