Skip to the content.

Batch Reactor Guide

The Batch class simulates 0-dimensional (0D) batch reactor experiments - closed systems where only reactions occur without spatial transport.

Creating a Batch

from porousmedialab.batch import Batch

batch = Batch(tend=40, dt=1)

Parameters:

Adding Species

batch.add_species(name='O2', init_conc=0.2, allow_negative=False)
batch.add_species(name='CH4', init_conc=0.001)
batch.add_species(name='CO2', init_conc=0)

Parameters:

Defining Constants

Constants are parameters used in rate expressions:

batch.constants['k1'] = 0.1          # Rate constant
batch.constants['Km'] = 0.001        # Half-saturation constant
batch.constants['T'] = 25            # Temperature

Defining Rate Expressions

Rate expressions are strings. The SciPy solvers compile them to NumPy code. 'rk4', 'butcher5' and their rate reconstruction evaluate them with numexpr. Use the operators and functions below for compatibility with every solver.

# First-order kinetics
batch.rates['R1'] = 'k1 * CH4'

# Michaelis-Menten kinetics
batch.rates['R2'] = 'k1 * O2 / (Km + O2)'

# Multiple terms
batch.rates['R3'] = 'k1 * CH4 * O2 / (Km + O2)'

# Inhibition term
batch.rates['R4'] = 'k1 * CH4 * Km / (Km + CO2)'

Available operators: +, -, *, /, ** (power)

Available functions: exp, log, log10, sqrt, sin, cos, tan, abs, where

Setting dcdt (Time Derivatives)

Define how each species concentration changes over time:

# Methane oxidation uses the R3 rate from above
batch.dcdt['CH4'] = '-R3'
batch.dcdt['O2'] = '-2 * R3'
batch.dcdt['CO2'] = 'R3'

Running the Simulation

batch.solve(verbose=True)

Parameters:

Solver settings

Pass rtol, atol, scipy_method and non_negative_rates by keyword or assign them on an existing model. Changes take effect at the next reaction step.

Accessing Results

Results are stored in species dictionaries:

# Get concentration time series (shape: [1, num_timesteps])
conc = batch.O2.concentration

# Get final concentration
final = batch.O2.concentration[0, -1]

# Get time array
time = batch.time

# Plot concentration vs time
import matplotlib.pyplot as plt
plt.plot(batch.time, batch.O2.concentration[0])

Plotting Methods

Built-in plotting methods:

# Plot single species
batch.plot('O2')

# Plot all species
batch.plot_profiles()

# Plot reaction rates
batch.reconstruct_rates()
batch.plot_rates()

# Plot rate of change (delta)
batch.plot_deltas()

The SciPy solvers leave estimated_rates at zero until reconstruction. plot_deltas() works directly from concentration changes or stored deltas. Use plot_rate(rate) for a single reaction, plot_delta(element) for a single species and plot_fractions() for acid-base fractions.

Saving Results

Save results to HDF5 format:

batch.save_results_in_hdf5()

This creates results.h5 containing:

Pass a filename to write somewhere else:

batch.save_results_in_hdf5('batch-results.h5')

Advanced: Henry Equilibrium

For gas-liquid partitioning, specify the dimensionless ratio Hcc = C_aq / C_gas. The default gas_water_ratio=None assumes equal volumes. henry_equilibrium is an alias for add_partition_equilibrium.

# Add both aqueous and gas species
batch.add_species(name='CO2_aq', init_conc=0.001)
batch.add_species(name='CO2_gas', init_conc=0)

# Set Henry equilibrium (Hcc is dimensionless Henry constant)
batch.add_partition_equilibrium(aq='CO2_aq', gas='CO2_gas', Hcc=0.83)

# For a headspace whose volume is 25% of the water volume, use this call instead:
# batch.add_partition_equilibrium(aq='CO2_aq', gas='CO2_gas', Hcc=0.83, gas_water_ratio=0.25)

Advanced: Acid-Base Equilibrium

For pH-dependent systems:

# Add carbonate species
batch.add_species(name='H2CO3', init_conc=0.001)
batch.add_species(name='HCO3', init_conc=0.01)
batch.add_species(name='CO3', init_conc=0.0001)

# Define acid with pKa values
batch.add_acid(
    species=['H2CO3', 'HCO3', 'CO3'],
    pKa=[6.35, 10.33],
    charge=0  # Charge of fully protonated form
)

# Add non-dissociating ion
batch.add_species(name='Na', init_conc=0.1)
batch.add_ion(name='Na', charge=1)

# Create the acid-base system
batch.create_acid_base_system()

After create_acid_base_system(), a pH species is automatically created and tracked.

Complete Example

from porousmedialab.batch import Batch

# Create batch reactor
batch = Batch(tend=40, dt=1)

# Add species
batch.add_species(name='OM', init_conc=0.01)    # Organic matter
batch.add_species(name='O2', init_conc=0.2)     # Oxygen
batch.add_species(name='CO2', init_conc=0)      # Carbon dioxide

# Set constants
batch.constants['k'] = 0.5      # Degradation rate
batch.constants['Km'] = 0.02    # Half-saturation

# Define rate
batch.rates['R_deg'] = 'k * OM * O2 / (Km + O2)'

# Set time derivatives
batch.dcdt['OM'] = '-R_deg'
batch.dcdt['O2'] = '-R_deg'
batch.dcdt['CO2'] = 'R_deg'

# Run and plot
batch.solve()
batch.plot_profiles()

Tips

  1. Timestep selection: Start with a larger dt and decrease if you see numerical instabilities
  2. Units: Be consistent with units throughout (e.g., mol/L for concentrations, 1/day for rates)
  3. Debugging: Use verbose=True to see simulation progress
  4. Rate expressions: Ensure all variables in rate expressions are defined as species or constants