Skip to content

Add mass_balance and energy_balance utility functions - #44

Draft
FrancescaWatson with Copilot wants to merge 1 commit into
mainfrom
copilot/setup-mass-energy-balance-functions
Draft

FrancescaWatson with Copilot wants to merge 1 commit into
mainfrom
copilot/setup-mass-energy-balance-functions

Conversation

Copilot AI commented Jul 9, 2026

Copy link
Copy Markdown

Adds two post-processing utility functions to check conservation law closure over a specified simulation period (default: full simulation).

New functions

  • mass_balance(states, timesteps, model, forces; state0, component, t_start, t_end)
    ((n_in − n_out) − Δn_col) / |Δn_col| — molar conservation residual for a given component (default: CO₂).

  • energy_balance(states, timesteps, model, forces; state0, t_start, t_end)
    (Q_in + Q_gen − (Q_out + ΔE_solid + ΔE_fluid + ΔE_ads)) / |Q_out + ΔE_solid + ΔE_fluid + ΔE_ads| — thermal energy conservation residual including adsorption heat generation and wall exchange.

Implementation notes

  • Boundary fluxes dispatched over all four BC types: AdsorptionBC, PressurisationBC, BlowdownBC, EvacuationBC, matching the sign conventions in each BC's apply_forces_to_equation!.
  • Column inventory accounts for fluid-phase moles (ideal gas), adsorbed moles, and thermal energy in solid, fluid, and adsorbed phases.
  • Heat of adsorption (ΔH) from DualSiteLangmuir is applied as Q_gen = −ΔH · Δq · V_solid (positive for exothermic CO₂ adsorption).
  • Wall heat exchange is included in Q_in/Q_out based on sign of h_in · (T_col − T_wall).
  • Both functions accept a sub-period via t_start/t_end for cycle-level diagnostics.

Usage

states, timesteps, model, forces, state0 = run_my_simulation()

mb = mass_balance(states, timesteps, model, forces; state0 = state0)   # ≈ 0 if conserved
eb = energy_balance(states, timesteps, model, forces; state0 = state0) # ≈ 0 if conserved

Known issue in tests

The test file calls simulate_process with output_substates=true, which causes Jutul's expand_to_ministeps to emit more state/timestep entries than force entries, triggering a bounds error in _get_force_at_step. Tests should be run without output_substates=true (or with explicit output_substates=false); the src/balance.jl implementation itself is unaffected.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants