|
| 1 | +# Batch Reactor Guide |
| 2 | + |
| 3 | +The `Batch` class simulates 0-dimensional (0D) batch reactor experiments - closed systems where only reactions occur without spatial transport. |
| 4 | + |
| 5 | +## Creating a Batch |
| 6 | + |
| 7 | +```python |
| 8 | +from porousmedialab.batch import Batch |
| 9 | + |
| 10 | +batch = Batch(tend=40, dt=1) |
| 11 | +``` |
| 12 | + |
| 13 | +**Parameters:** |
| 14 | +- `tend` (float): End time of simulation (in your chosen time units) |
| 15 | +- `dt` (float): Timestep for integration |
| 16 | + |
| 17 | +## Adding Species |
| 18 | + |
| 19 | +```python |
| 20 | +batch.add_species(name='O2', init_conc=0.2) |
| 21 | +batch.add_species(name='CH4', init_conc=0.001) |
| 22 | +batch.add_species(name='CO2', init_conc=0) |
| 23 | +``` |
| 24 | + |
| 25 | +**Parameters:** |
| 26 | +- `name` (str): Name of the chemical species (used in rate expressions) |
| 27 | +- `init_conc` (float): Initial concentration |
| 28 | + |
| 29 | +## Defining Constants |
| 30 | + |
| 31 | +Constants are parameters used in rate expressions: |
| 32 | + |
| 33 | +```python |
| 34 | +batch.constants['k1'] = 0.1 # Rate constant |
| 35 | +batch.constants['Km'] = 0.001 # Half-saturation constant |
| 36 | +batch.constants['T'] = 25 # Temperature |
| 37 | +``` |
| 38 | + |
| 39 | +## Defining Rate Expressions |
| 40 | + |
| 41 | +Rate expressions are strings evaluated using [numexpr](https://github.com/pydata/numexpr): |
| 42 | + |
| 43 | +```python |
| 44 | +# First-order kinetics |
| 45 | +batch.rates['R1'] = 'k1 * A' |
| 46 | + |
| 47 | +# Michaelis-Menten kinetics |
| 48 | +batch.rates['R2'] = 'Vmax * S / (Km + S)' |
| 49 | + |
| 50 | +# Multiple terms |
| 51 | +batch.rates['R3'] = 'k1 * A * B / (Km + B)' |
| 52 | + |
| 53 | +# Inhibition term |
| 54 | +batch.rates['R4'] = 'k1 * A * Km_inh / (Km_inh + I)' |
| 55 | +``` |
| 56 | + |
| 57 | +**Available operators:** `+`, `-`, `*`, `/`, `**` (power) |
| 58 | + |
| 59 | +**Available functions:** `exp`, `log`, `log10`, `sqrt`, `sin`, `cos`, `tan`, `abs`, `where` |
| 60 | + |
| 61 | +## Setting dcdt (Time Derivatives) |
| 62 | + |
| 63 | +Define how each species concentration changes over time: |
| 64 | + |
| 65 | +```python |
| 66 | +# Simple consumption and production |
| 67 | +batch.dcdt['A'] = '-R1' |
| 68 | +batch.dcdt['B'] = 'R1' |
| 69 | + |
| 70 | +# Multiple reactions affecting one species |
| 71 | +batch.dcdt['C'] = 'R1 - R2 + 0.5 * R3' |
| 72 | + |
| 73 | +# Stoichiometric coefficients |
| 74 | +batch.dcdt['O2'] = '-2 * R1 - 4 * R2' |
| 75 | +``` |
| 76 | + |
| 77 | +## Running the Simulation |
| 78 | + |
| 79 | +```python |
| 80 | +batch.solve(verbose=True) |
| 81 | +``` |
| 82 | + |
| 83 | +**Parameters:** |
| 84 | +- `verbose` (bool): If `True`, prints progress and time estimates |
| 85 | + |
| 86 | +## Accessing Results |
| 87 | + |
| 88 | +Results are stored in species dictionaries: |
| 89 | + |
| 90 | +```python |
| 91 | +# Get concentration time series (shape: [1, num_timesteps]) |
| 92 | +conc = batch.A.concentration |
| 93 | + |
| 94 | +# Get final concentration |
| 95 | +final = batch.A.concentration[0, -1] |
| 96 | + |
| 97 | +# Get time array |
| 98 | +time = batch.time |
| 99 | + |
| 100 | +# Plot concentration vs time |
| 101 | +import matplotlib.pyplot as plt |
| 102 | +plt.plot(batch.time, batch.A.concentration[0]) |
| 103 | +``` |
| 104 | + |
| 105 | +## Plotting Methods |
| 106 | + |
| 107 | +Built-in plotting methods: |
| 108 | + |
| 109 | +```python |
| 110 | +# Plot single species |
| 111 | +batch.plot('A') |
| 112 | + |
| 113 | +# Plot all species |
| 114 | +batch.plot_profiles() |
| 115 | + |
| 116 | +# Plot reaction rates |
| 117 | +batch.plot_rates() |
| 118 | + |
| 119 | +# Plot rate of change (delta) |
| 120 | +batch.plot_deltas() |
| 121 | +``` |
| 122 | + |
| 123 | +## Saving Results |
| 124 | + |
| 125 | +Save results to HDF5 format: |
| 126 | + |
| 127 | +```python |
| 128 | +batch.save_results_in_hdf5() |
| 129 | +``` |
| 130 | + |
| 131 | +This creates `results.h5` containing: |
| 132 | +- `time`: Time array |
| 133 | +- `concentrations`: All species concentrations |
| 134 | +- `estimated_rates`: Calculated rates |
| 135 | +- `parameters`: Constants used |
| 136 | + |
| 137 | +## Advanced: Henry Equilibrium |
| 138 | + |
| 139 | +For gas-liquid partitioning: |
| 140 | + |
| 141 | +```python |
| 142 | +# Add both aqueous and gas species |
| 143 | +batch.add_species(name='CO2_aq', init_conc=0.001) |
| 144 | +batch.add_species(name='CO2_gas', init_conc=0) |
| 145 | + |
| 146 | +# Set Henry equilibrium (Hcc is dimensionless Henry constant) |
| 147 | +batch.henry_equilibrium(aq='CO2_aq', gas='CO2_gas', Hcc=0.83) |
| 148 | +``` |
| 149 | + |
| 150 | +## Advanced: Acid-Base Equilibrium |
| 151 | + |
| 152 | +For pH-dependent systems: |
| 153 | + |
| 154 | +```python |
| 155 | +# Add carbonate species |
| 156 | +batch.add_species(name='H2CO3', init_conc=0.001) |
| 157 | +batch.add_species(name='HCO3', init_conc=0.01) |
| 158 | +batch.add_species(name='CO3', init_conc=0.0001) |
| 159 | + |
| 160 | +# Define acid with pKa values |
| 161 | +batch.add_acid( |
| 162 | + species=['H2CO3', 'HCO3', 'CO3'], |
| 163 | + pKa=[6.35, 10.33], |
| 164 | + charge=0 # Charge of fully protonated form |
| 165 | +) |
| 166 | + |
| 167 | +# Add non-dissociating ion |
| 168 | +batch.add_species(name='Na', init_conc=0.1) |
| 169 | +batch.add_ion(name='Na', charge=1) |
| 170 | + |
| 171 | +# Create the acid-base system |
| 172 | +batch.create_acid_base_system() |
| 173 | +``` |
| 174 | + |
| 175 | +After `create_acid_base_system()`, a `pH` species is automatically created and tracked. |
| 176 | + |
| 177 | +## Complete Example |
| 178 | + |
| 179 | +```python |
| 180 | +from porousmedialab.batch import Batch |
| 181 | + |
| 182 | +# Create batch reactor |
| 183 | +batch = Batch(tend=40, dt=1) |
| 184 | + |
| 185 | +# Add species |
| 186 | +batch.add_species(name='OM', init_conc=0.01) # Organic matter |
| 187 | +batch.add_species(name='O2', init_conc=0.2) # Oxygen |
| 188 | +batch.add_species(name='CO2', init_conc=0) # Carbon dioxide |
| 189 | + |
| 190 | +# Set constants |
| 191 | +batch.constants['k'] = 0.5 # Degradation rate |
| 192 | +batch.constants['Km'] = 0.02 # Half-saturation |
| 193 | + |
| 194 | +# Define rate |
| 195 | +batch.rates['R_deg'] = 'k * OM * O2 / (Km + O2)' |
| 196 | + |
| 197 | +# Set time derivatives |
| 198 | +batch.dcdt['OM'] = '-R_deg' |
| 199 | +batch.dcdt['O2'] = '-R_deg' |
| 200 | +batch.dcdt['CO2'] = 'R_deg' |
| 201 | + |
| 202 | +# Run and plot |
| 203 | +batch.solve() |
| 204 | +batch.plot_profiles() |
| 205 | +``` |
| 206 | + |
| 207 | +## Tips |
| 208 | + |
| 209 | +1. **Timestep selection**: Start with a larger `dt` and decrease if you see numerical instabilities |
| 210 | +2. **Units**: Be consistent with units throughout (e.g., mol/L for concentrations, 1/day for rates) |
| 211 | +3. **Debugging**: Use `verbose=True` to see simulation progress |
| 212 | +4. **Rate expressions**: Ensure all variables in rate expressions are defined as species or constants |
0 commit comments