### Benchmark function with setup method Source: https://github.com/pybamm-team/pybamm/blob/main/benchmarks/README.md Example of a benchmark function that utilizes a 'setup' method for pre-benchmark initialization. The 'setup' method runs before each benchmark execution and its duration is not included in the benchmark time. ```python class TimeSPM: def setup(self): model = pb.lithium_ion.SPM() geometry = model.default_geometry # ... self.model = model def time_solve_SPM_ScipySolver(self): solver = pb.ScipySolver() solver.solve(self.model, [0, 3600]) ``` -------------------------------- ### Test example scripts Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Execute all example scripts located in the examples folder. ```bash nox -s scripts ``` -------------------------------- ### Install PyBaMM and necessary libraries Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/initialize-model-with-solution.ipynb Installs PyBaMM with plotting and citation capabilities. This is a prerequisite for running the examples. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import pandas as pd import pybamm os.chdir(pybamm.__path__[0] + "/..") ``` -------------------------------- ### Install pybammsolvers via PyPI Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/README.md Standard installation command for the package. ```bash pip install pybammsolvers ``` -------------------------------- ### Install and Import PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/7-creating-a-submodel.ipynb Install the library and import the main package. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm ``` -------------------------------- ### Install PyBaMM with uv Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-source.rst Use uv to sync the environment and install dependencies in editable mode. ```bash uv sync --extra all --group dev --group docs ``` -------------------------------- ### Install PyBaMM and Set Up Simulation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/simulations_and_experiments/callbacks.ipynb Installs PyBaMM with plotting and citation capabilities and sets up a basic simulation with a defined experiment. This is a prerequisite for using callbacks. ```python %pip install "pybamm[plot,cite]" -q import pybamm model = pybamm.lithium_ion.DFN() experiment = pybamm.Experiment( [ ( "Discharge at C/5 for 10 hours or until 3.3 V", "Charge at 1 A until 4.1 V", "Hold at 4.1 V until 10 mA", ), ] * 3 ) sim = pybamm.Simulation(model, experiment=experiment) ``` -------------------------------- ### Install pre-commit hooks Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Install pre-commit hooks locally using either uv or pip. ```bash # Using uv uvx pre-commit install # Or using pip pip install pre-commit pre-commit install ``` -------------------------------- ### Initialize environment and imports Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/compare-particle-diffusion-models.ipynb Install necessary dependencies and import required libraries for the simulation. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import matplotlib.pyplot as plt import pybamm os.chdir(pybamm.__path__[0] + "/..") ``` -------------------------------- ### Install and Import Dependencies Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/solvers/idaklu-jax-interface.ipynb Install the required PyBaMM JAX support and import necessary libraries. ```python %pip install "pybamm[jax]" -q # install PyBaMM with JAX support if it is not installed import time import jax import jax.numpy as jnp import numpy as np import pybamm ``` -------------------------------- ### Install and Import PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/DFN.ipynb Install the required PyBaMM package and import the library. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm ``` -------------------------------- ### Import PyBaMM and NumPy Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/tortuosity_models.ipynb Initial setup for the notebook environment. ```python import numpy as np import pybamm ``` -------------------------------- ### Install airspeed velocity (asv) Source: https://github.com/pybamm-team/pybamm/blob/main/benchmarks/README.md Install the airspeed velocity benchmarking tool using pip. This is a prerequisite for running any benchmarks. ```shell pip install asv ``` -------------------------------- ### Install PyBaMM and Import Libraries Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/getting_started/tutorial-2-compare-models.ipynb Installs PyBaMM with plotting and citation capabilities and imports the library. Use this at the beginning of your notebook. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm ``` -------------------------------- ### Install PyBaMM from Source Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-source.rst Commands to install the package, including editable mode with development and documentation dependencies. ```bash pip install ./packages/pybamm ``` ```bash pip install -e ./packages/pybamm[all] --group packages/pybamm/pyproject.toml:dev --group packages/pybamm/pyproject.toml:docs ``` ```bash pip install -e './packages/pybamm[all]' --group packages/pybamm/pyproject.toml:dev --group packages/pybamm/pyproject.toml:docs ``` -------------------------------- ### Install and use Snakeviz for profiling Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Install the snakeviz package and use its magic command to visualize profiling data in a browser. ```bash pip install snakeviz ``` ```bash %load_ext snakeviz %snakeviz command_to_time() ``` -------------------------------- ### Install PyBaMM and Initialize DataLoader Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/pybamm_data.ipynb Installs PyBaMM with plotting and citation capabilities and initializes the DataLoader. This is the first step to accessing data files. ```python %pip install "pybamm[plot,cite]" -q import pybamm data_loader = pybamm.DataLoader() data_loader.show_registry() ``` -------------------------------- ### Install PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/index.rst Standard installation commands for PyBaMM using different package managers. ```bash uv pip install pybamm ``` ```bash pip install pybamm ``` ```bash conda install -c conda-forge pybamm-base ``` ```bash conda install -c conda-forge pybamm ``` -------------------------------- ### Check Docker Installation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-docker.rst Verify that Docker is installed on your system by checking its version. ```bash docker --version ``` -------------------------------- ### Install System Dependencies Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-source.rst Install required compilers and libraries for building PyBaMM on different operating systems. ```bash sudo apt install python3.X python3.X-dev libopenblas-dev gcc gfortran graphviz cmake make pandoc ``` ```bash brew install python openblas gcc gfortran graphviz libomp cmake pandoc ``` -------------------------------- ### Install Git on Ubuntu (WSL) Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/windows-wsl.rst Installs the Git version control system on your Ubuntu distribution within WSL. This is a prerequisite for cloning the PyBaMM repository. ```bash sudo apt install git-core ``` -------------------------------- ### Install PyBaMM and Plot Voltage Components Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/plotting/plot-voltage-components.ipynb Installs PyBaMM with plotting and citation capabilities and then solves a DFN model with the Chen2020 parameter set. Finally, it plots a selection of voltage components against time. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm model = pybamm.lithium_ion.DFN() experiment = pybamm.Experiment(["Discharge at 1C until 2.5 V"]) sim = pybamm.Simulation( model, experiment=experiment, parameter_values=pybamm.ParameterValues("Chen2020") ) sol = sim.solve() sol.plot( [ "Negative electrode bulk open-circuit potential [V]", "Positive electrode bulk open-circuit potential [V]", "Negative particle concentration overpotential [V]", "Positive particle concentration overpotential [V]", "X-averaged negative electrode reaction overpotential [V]", "X-averaged positive electrode reaction overpotential [V]", "X-averaged concentration overpotential [V]", "X-averaged electrolyte ohmic losses [V]", "X-averaged negative electrode ohmic losses [V]", "X-averaged positive electrode ohmic losses [V]", ], ) ``` -------------------------------- ### Configure custom library paths Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/README.md Use custom paths for SUNDIALS and SuiteSparse during installation. ```bash pip install . \ --config-settings=cmake.define.SUNDIALS_ROOT=/path/to/sundials \ --config-settings=cmake.define.SuiteSparse_ROOT=/path/to/suitesparse ``` -------------------------------- ### Build on Linux Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/README.md Commands to install system dependencies and build the package on Linux. ```bash sudo apt-get install libopenblas-dev gcc gfortran make g++ build-essential git submodule update --init --recursive pip install . ``` -------------------------------- ### Run Development Session with Nox Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-source.rst Execute the development session using Nox to verify the installation. ```bash nox -s dev ``` -------------------------------- ### PyBaMM Latexify Example 1 Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/latexify.ipynb This snippet shows a basic latexify output for a variable. ```latex A_{\mathrm{cc}} = L_{y} L_{z} ``` -------------------------------- ### Set up and Solve Simulation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/solution-data-and-processed-variables.ipynb Initializes a simulation, solves it over a specified time range, and creates a QuickPlot object for visualization. This is the starting point for analyzing simulation results. ```python simulation = pybamm.Simulation(model) t = [0, 3600] solution = simulation.solve(t) quick_plot = pybamm.QuickPlot(solution) quick_plot.dynamic_plot(); ``` -------------------------------- ### Benchmark function example Source: https://github.com/pybamm-team/pybamm/blob/main/benchmarks/README.md Example of a benchmark function within a class. Benchmark functions must start with the 'time_' prefix. This example times the solving process of a specific model and solver. ```python def time_solve_SPM_ScipySolver(self): solver = pb.ScipySolver() solver.solve(self.model, [0, 3600]) ``` -------------------------------- ### Configure SUNDIALS and SuiteSparse dependencies Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/CMakeLists.txt Checks for pre-built libraries and triggers the installation script if dependencies are missing. ```cmake if(NOT WIN32 AND NOT DEFINED VCPKG_ROOT_DIR) file(GLOB _sundials_prebuilt "${SUNDIALS_ROOT}/lib/libsundials_idas.*" "${SUNDIALS_ROOT}/lib64/libsundials_idas.*") file(GLOB _suitesparse_prebuilt "${SuiteSparse_ROOT}/lib/libklu.*" "${SuiteSparse_ROOT}/lib64/libklu.*") if(NOT _sundials_prebuilt OR NOT _suitesparse_prebuilt) if(NOT EXISTS "${CMAKE_SOURCE_DIR}/sundials/CMakeLists.txt" OR NOT EXISTS "${CMAKE_SOURCE_DIR}/SuiteSparse/SuiteSparse_config/CMakeLists.txt") message(FATAL_ERROR "SUNDIALS/SuiteSparse are not built (no libraries under " "${SUNDIALS_ROOT}) and the submodule sources are missing. Either:\n" " * fetch the sources: run `git submodule update --init` from the\n" " repository root (or clone with --recurse-submodules), or\n" " * use a system install: set SUNDIALS_ROOT and SuiteSparse_ROOT.") endif() execute_process( COMMAND "${PYTHON_EXECUTABLE}" install_KLU_Sundials.py WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}" RESULT_VARIABLE _idaklu_rc) if(_idaklu_rc) message(FATAL_ERROR "install_KLU_Sundials.py failed (exit ${_idaklu_rc}); " "check that make, cmake, and a C/Fortran compiler are installed.") endif() endif() endif() ``` -------------------------------- ### Install PyBaMM and Solve a Model Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/getting_started/tutorial-3-basic-plotting.ipynb Installs PyBaMM with plotting and citation capabilities and then builds and solves a standard lithium-ion DFN model. This is a prerequisite for further plotting examples. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm model = pybamm.lithium_ion.DFN() sim = pybamm.Simulation(model) sim.solve([0, 3600]) ``` -------------------------------- ### Build documentation locally Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Run the nox session to build and serve the documentation locally for testing. ```bash nox -s docs ``` -------------------------------- ### Remove MSMR Example Parameter Set Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/electrode-state-of-health.ipynb This code snippet removes the 'MSMR_Example' parameter set from the list of all parameter sets. This is necessary because the MSMR example requires a different setup for the ESOH solver. ```python # Skip the MSMR example parameter set since we need to set up the ESOH solver differently all_parameter_sets.remove("MSMR_Example") ``` -------------------------------- ### Build using Nox Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/README.md Recommended method for quick local builds. ```bash pip install nox nox ``` -------------------------------- ### Initialize PyBaMM Models Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/4-comparing-full-and-reduced-order-models.ipynb Install necessary dependencies and instantiate two empty BaseModel objects for comparison. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import matplotlib.pyplot as plt import numpy as np import pybamm full_model = pybamm.BaseModel(name="full model") reduced_model = pybamm.BaseModel(name="reduced model") ``` -------------------------------- ### Advection-Dominated System Model Setup Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/spatial_methods/finite-volumes.ipynb Sets up a PyBaMM model for an advection-dominated system, highlighting the importance of upwinding for accuracy. This serves as a starting point for exploring numerical stability in such cases. ```python model = pybamm.BaseModel() ``` -------------------------------- ### Install PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/5-half-cell-model.ipynb Installs the PyBaMM library with plotting and citation capabilities. Restart the kernel if necessary after installation. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm model = pybamm.BaseModel() ``` -------------------------------- ### Set up MPM parameters and run simulation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/MPM.ipynb Configures parameters for the MPM, including particle size distributions, and then runs a simulation. The simulation results are then plotted. ```python distribution_params = { "Negative minimum particle radius [m]": R_min_n, "Positive minimum particle radius [m]": R_min_p, "Negative maximum particle radius [m]": R_max_n, "Positive maximum particle radius [m]": R_max_p, "Negative area-weighted " + "particle-size distribution [m-1]": f_a_dist_n_dim, "Positive area-weighted " + "particle-size distribution [m-1]": f_a_dist_p_dim, } params.update(distribution_params) ``` ```python sim = pybamm.Simulation(model, parameter_values=params) sim.solve(t_eval=[0, 3600]) sim.plot(output_variables=output_variables) ``` -------------------------------- ### Install pybammsolvers via Conda Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/README.md Installation command for conda-forge environments. ```bash conda install pybammsolvers # or mamba install pybammsolvers ``` -------------------------------- ### Initialize Environment and PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/6-a-simple-SEI-model.ipynb Install the necessary PyBaMM package and set the working directory to the project root. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import numpy as np import pybamm os.chdir(pybamm.__path__[0] + "/..") ``` -------------------------------- ### Install idaklu Target Source: https://github.com/pybamm-team/pybamm/blob/main/packages/pybammsolvers/CMakeLists.txt Defines the installation path for the compiled idaklu library. ```cmake install(TARGETS idaklu LIBRARY DESTINATION ${SKBUILD_PROJECT_NAME}) ``` -------------------------------- ### Install PyBaMM with JaxSolver Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/index.rst Install PyBaMM including optional jax dependencies. ```bash uv pip install "pybamm[jax]" ``` ```bash pip install "pybamm[jax]" ``` ```bash conda install -c conda-forge "jax>=0.7.0,<0.9.0" ``` -------------------------------- ### Set Initial and Boundary Conditions Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/2-a-pde-model.ipynb Configure the initial state and boundary conditions for the PDE model. ```python # initial conditions model.initial_conditions = {c: pybamm.Scalar(1)} # boundary conditions lbc = pybamm.Scalar(0) rbc = pybamm.Scalar(2) model.boundary_conditions = {c: {"left": (lbc, "Neumann"), "right": (rbc, "Neumann")}} ``` -------------------------------- ### Create and activate virtual environments Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/index.rst Commands to initialize and activate virtual environments using uv or virtualenv. ```bash uv venv ``` ```bash source .venv/bin/activate ``` ```bash .venv\Scripts\activate.bat ``` ```bash virtualenv env ``` ```bash source env/bin/activate ``` ```bash python -m pip install virtualenv ``` ```bash python -m virtualenv env ``` ```bash env\Scripts\activate.bat ``` -------------------------------- ### List project configuration files Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Core configuration files for dependency management and environment locking. ```text pyproject.toml uv.lock ``` -------------------------------- ### Install Nox Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/install-from-source.rst Install the Nox automation tool for running tests and managing environments. ```bash uv tool install nox ``` ```bash python3.X -m pip install --user nox ``` -------------------------------- ### Install Python on macOS Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/index.rst Use Homebrew to install Python after setting up the package manager. ```bash ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" ``` ```bash brew install python ``` -------------------------------- ### Configure simulations Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/compare-particle-diffusion-models.ipynb Set up simulations with input-based current functions and define the evaluation time range. ```python simulations = [] for model in models: param = model.default_parameter_values param["Current function [A]"] = "[input]" simulations.append(pybamm.Simulation(model, parameter_values=param)) ``` ```python t_eval = [0, 3600] ``` -------------------------------- ### Set up Experiment with Custom Terminations Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/simulations_and_experiments/custom-experiments.ipynb Create a multi-step experiment that includes custom termination events alongside standard ones like voltage limits. This allows for complex charging/discharging protocols. ```python terminations = [anode_potential_termination, "4.2V"] experiment = pybamm.Experiment( [ ( pybamm.step.c_rate(-1, termination=terminations), pybamm.step.c_rate(-0.5, termination=terminations), pybamm.step.c_rate(-0.25, termination=terminations), "Hold at 4.2V until C/50", ) ] ) ``` -------------------------------- ### Install Pandoc Source: https://github.com/pybamm-team/pybamm/blob/main/CONTRIBUTING.md Install Pandoc via conda to support Jupyter notebook rendering in Sphinx. ```bash conda install -c conda-forge pandoc ``` -------------------------------- ### Setup Toy Model for Sensitivity Analysis Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/parameterization/sensitivities_and_data_fitting.ipynb Defines a simple toy model with a single input parameter 'a' and a model output 'y squared'. Uses IDAKLUSolver for sensitivity calculations. ```python # setup a simple test model model = pybamm.BaseModel("name") y = pybamm.Variable("y") a = pybamm.InputParameter("a") model.rhs = {y: a * y} model.initial_conditions = {y: 1} model.variables = {"y squared": y**2} solver = pybamm.IDAKLUSolver(rtol=1e-10, atol=1e-10) t_eval = np.linspace(0, 1, 80) ``` -------------------------------- ### Install PyBaMM via pip Source: https://github.com/pybamm-team/pybamm/blob/main/README.md Standard command to install the latest PyBaMM package from PyPI. ```bash pip install pybamm ``` -------------------------------- ### Load, Inspect, List, and Compare Parameter Sets Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/api/parameters/parameter_values.rst Demonstrates loading a parameter set, getting information about a specific parameter, listing parameters by category, and comparing two parameter sets. Requires PyBaMM library. ```python import pybamm # Load a parameter set params = pybamm.ParameterValues("Chen2020") # Get info about a parameter info = params.get_info("Maximum concentration in negative electrode [mol.m-3]") print(f"Value: {info.value}, Units: {info.units}, Category: {info.category}") # List parameters by category neg_electrode_params = params.list_by_category("negative electrode") print(f"Found {len(neg_electrode_params)} negative electrode parameters") # Compare with another parameter set marquis = pybamm.ParameterValues("Marquis2019") diff = params.diff(marquis) print(f"Changed parameters: {list(diff.changed.keys())[:5]}") ``` -------------------------------- ### Install Python on Debian-based systems Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/user_guide/installation/index.rst Update package lists and install Python 3 using apt-get. ```bash sudo apt-get update sudo apt-get install python3 ``` -------------------------------- ### Set Up Model Parameters Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/lithium-plating.ipynb Configures specific parameters for the lithium-ion model, including ambient temperature and voltage cut-offs, using a predefined parameter set. ```python # pick parameters parameter_values = pybamm.ParameterValues("OKane2022") parameter_values.update({"Ambient temperature [K]": 268.15}) parameter_values.update({"Upper voltage cut-off [V]": 4.21}) # parameter_values.update({"Lithium plating kinetic rate constant [m.s-1]": 1E-9}) parameter_values.update({"Lithium plating transfer coefficient": 0.5}) parameter_values.update({"Dead lithium decay constant [s-1]": 1e-4}) ``` -------------------------------- ### Set up and Solve Simulation with Experiment Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/getting_started/tutorial-5-run-experiments.ipynb Initializes a DFN model and creates a PyBaMM Simulation object, passing the defined experiment to the simulation. ```python model = pybamm.lithium_ion.DFN() sim = pybamm.Simulation(model, experiment=experiment) ``` -------------------------------- ### Initialize PyBaMM and Load Libraries Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/composite_particle.ipynb Installs PyBaMM if necessary and imports essential libraries for modeling and plotting. Sets the working directory for PyBaMM. ```python # %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import timeit import matplotlib.pyplot as plt from matplotlib import style import pybamm style.use("ggplot") os.chdir(pybamm.__path__[0] + "/..") ``` -------------------------------- ### Canonical JSON Encoding Example Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/api/expression_tree/operations/serialise.rst Example of how a PyBaMM Index object containing a StateVector is represented in the canonical JSON wire format. ```json { "name": "Index[0]", "index": {"start": 0, "stop": 1, "step": null}, "check_size": false, "children": [ { "name": "y[0:2]", "domains": {"primary": [], "secondary": [], "tertiary": [], "quaternary": []}, "y_slice": [{"start": 0, "stop": 2, "step": null}], "evaluation_array": [true, true], "$type": "pybamm.expression_tree.state_vector.StateVector" } ], "$type": "pybamm.expression_tree.unary_operators.Index" } ``` -------------------------------- ### Run a simulation with pybamm.Simulation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/performance/01-simulation-pipeline.ipynb Initializes a DFN model and IDAKLUSolver, solves the simulation, and plots the terminal voltage. ```python # create simulation model = pybamm.lithium_ion.DFN() solver = pybamm.IDAKLUSolver() sim = pybamm.Simulation(model, solver=solver) # solve solution = sim.solve([0, 3600]) # Post-processing t_evals = np.linspace(0, 3600, 100) voltage = solution["Terminal voltage [V]"](t_evals) # Plot the results import matplotlib.pyplot as plt plt.plot(t_evals, voltage) plt.xlabel("Time [s]") plt.ylabel("Voltage [V]") plt.show() ``` -------------------------------- ### Install PyBaMM and Solve Models Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/plotting/customize-quick-plot.ipynb Installs PyBaMM with plotting capabilities and solves multiple lithium-ion battery models. This is a prerequisite for plotting. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import pybamm models = [pybamm.lithium_ion.SPM(), pybamm.lithium_ion.SPMe(), pybamm.lithium_ion.DFN()] sims = [] for model in models: sim = pybamm.Simulation(model) sim.solve([0, 3600]) sims.append(sim) ``` -------------------------------- ### Install PyBaMM and import libraries Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/expression_tree/broadcasts.ipynb Installs PyBaMM with plotting and citation capabilities and imports necessary libraries like numpy and pybamm. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import numpy as np import pybamm ``` -------------------------------- ### Set Up Mesh and Discretization Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/creating_models/5-half-cell-model.ipynb Configure submesh types, define the number of points for each spatial variable, create a mesh, specify spatial methods, and discretize the model. This prepares the model for numerical solution. ```python submesh_types = { "separator": pybamm.Uniform1DSubMesh, "positive electrode": pybamm.Uniform1DSubMesh, "positive particle": pybamm.Uniform1DSubMesh, } var_pts = {x_s: 10, x_p: 20, r: 30} mesh = pybamm.Mesh(geometry, submesh_types, var_pts) spatial_methods = { "separator": pybamm.FiniteVolume(), "positive electrode": pybamm.FiniteVolume(), "positive particle": pybamm.FiniteVolume(), } disc = pybamm.Discretisation(mesh, spatial_methods) disc.process_model(model); ``` -------------------------------- ### Compare Full and Lumped Thermal Models Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/thermal-models.ipynb Setup and parameterize full and lumped thermal models to compare their behavior. ```python full_thermal_model = pybamm.lithium_ion.SPMe( {"thermal": "x-full"}, name="full thermal model" ) lumped_thermal_model = pybamm.lithium_ion.SPMe( {"thermal": "lumped"}, name="lumped thermal model" ) models = [full_thermal_model, lumped_thermal_model] ``` ```python parameter_values = pybamm.ParameterValues("Marquis2019") ``` ```python full_params = parameter_values.copy() full_params.update( { "Negative current collector" + " surface heat transfer coefficient [W.m-2.K-1]": 5, "Positive current collector" + " surface heat transfer coefficient [W.m-2.K-1]": 5, "Negative tab heat transfer coefficient [W.m-2.K-1]": 0, "Positive tab heat transfer coefficient [W.m-2.K-1]": 0, "Edge heat transfer coefficient [W.m-2.K-1]": 0, } ) ``` ```python A = parameter_values["Electrode width [m]"] * parameter_values["Electrode height [m]"] lumped_params = parameter_values.copy() lumped_params.update( { "Total heat transfer coefficient [W.m-2.K-1]": 5, "Cell cooling surface area [m2]": 2 * A, } ) ``` ```python params = [full_params, lumped_params] # loop over the models and solve sols = [] for model, param in zip(models, params, strict=False): param["Current function [A]"] = 3 * 0.68 sim = pybamm.Simulation(model, parameter_values=param) sim.solve([0, 3600]) sols.append(sim.solution) # plot output_variables = [ "Voltage [V]", "X-averaged cell temperature [K]", "Cell temperature [K]", ] pybamm.dynamic_plot(sols, output_variables) # plot the results pybamm.dynamic_plot( sols, [ "Volume-averaged cell temperature [K]", "Volume-averaged total heating [W.m-3]", "Current [A]", "Voltage [V]", ], ) ``` -------------------------------- ### Install PyBaMM via conda Source: https://github.com/pybamm-team/pybamm/blob/main/README.md Commands for installing PyBaMM or its base version via the conda-forge channel, including optional JAX dependencies. ```bash # for no extra dependencies conda install -c conda-forge pybamm-base # for all extra dependencies except jax conda install -c conda-forge pybamm # optionally install jax >=0.7.0, <0.9.0 on python >=3.11 conda install -c conda-forge "jax>=0.7.0,<0.9.0" ``` -------------------------------- ### Initialize Lead-Acid Models Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/lead-acid.ipynb Create instances of the Full and LOQS lead-acid battery models. ```python full = pybamm.lead_acid.Full() ``` ```python loqs = pybamm.lead_acid.LOQS() ``` -------------------------------- ### Sync development environment Source: https://github.com/pybamm-team/pybamm/blob/main/QWEN.md Use this command to create or refresh the development environment with all extras and dev dependencies. ```bash uv sync --extra all --group dev # create/refresh the dev environment ``` -------------------------------- ### Install PyBaMM and Solve Simulation Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/getting_started/tutorial-6-managing-simulation-outputs.ipynb Installs PyBaMM with plotting and citation capabilities and then builds and solves a Lithium-ion SPMe model. This is a prerequisite for accessing simulation outputs. ```python %pip install "pybamm[plot,cite]" -q import matplotlib.pyplot as plt import numpy as np import pybamm model = pybamm.lithium_ion.SPMe() sim = pybamm.Simulation(model) sim.solve([0, 3600]) ``` -------------------------------- ### Install PyBaMM and Load SPMe Model Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/solution-data-and-processed-variables.ipynb Installs PyBaMM with plotting and citation capabilities and loads the standard SPMe model for lithium-ion batteries. This is a prerequisite for exploring simulation data. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import matplotlib.pyplot as plt import numpy as np import pybamm os.chdir(pybamm.__path__[0] + "/..") # load model model = pybamm.lithium_ion.SPMe() ``` -------------------------------- ### Initialize Base Model Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/using-submodels.ipynb Start by loading a base lithium-ion model to set up the fundamental structure and default parameters. This base model does not select default submodels, so `build=False` is not necessary. ```python model = pybamm.lithium_ion.BaseModel() ``` -------------------------------- ### Install PyBaMM and Import Libraries Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/compare-ecker-data.ipynb Installs PyBaMM with plotting and citation capabilities and imports necessary libraries like matplotlib, pandas, and os. Changes the working directory to the PyBaMM root. ```python %pip install "pybamm[plot,cite]" -q # install PyBaMM if it is not installed import os import matplotlib.pyplot as plt import pandas as pd import pybamm os.chdir(pybamm.__path__[0] + "/..") ``` -------------------------------- ### Import PyBaMM Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/models/sodium-ion.ipynb Import the PyBaMM library to begin. This is a standard first step for any PyBaMM simulation. ```python import pybamm ``` -------------------------------- ### Initialize ParameterValues from chemistry Source: https://github.com/pybamm-team/pybamm/blob/main/docs/source/examples/notebooks/parameterization/parameter-values.ipynb Load pre-set parameter values using a known chemistry name. ```python chem_parameter_values = pybamm.ParameterValues("Marquis2019") print( "Negative current collector thickness is {} m".format( chem_parameter_values["Negative current collector thickness [m]"] ) ) ```