6.0 KiB
6.0 KiB
Story 7.2: energy-balance-validation
Status: in-progress
Story
As a simulation engineer, I want First AND Second Law verification across the thermodynamic system, so that thermodynamic consistency is guaranteed, giving me confidence in the simulation results.
Acceptance Criteria
- Given converged solution When computing balances Then energy error
\Sigma \dot{Q} + \dot{W} - \Sigma (\dot{m} \cdot h) < 1\mathrm{e-}6kW (First Law) - Given converged solution When computing balances Then violations trigger a validation error with a breakdown of error contribution
- Given converged solution When checking Second Law Then entropy generation
\Sigma (\dot{m} \cdot s) - \Sigma \frac{\dot{Q}}{T} \geq 0 - Given converged solution When checking Second Law Then trigger a warning (or error) if there is negative entropy destruction
- Given converged solution When computing balances Then tests are added and checks are performed after every solve alongside mass balance.
Tasks / Subtasks
- Task 1: Component Trait Extension for Energy/Entropy (AC: 1, 3)
- Add
energy_transfers(&self, state: &SystemState) -> Option<(Power, Power)>(e.g. returning Heat\dot{Q}and Work\dot{W}) or similar toComponenttrait, or allow components to report\dot{Q}and\dot{W}. - Add default implementation for adiabatic/passive components (e.g., Pipes, Valves return 0.0).
- Update components (Compressor, Condenser, Evaporator, etc.) to report their specific work and heat transfer.
- Ensure entropy generation can be calculated (requires fetching
EntropyfromFluidBackendif not already exposed).
- Add
- Task 2: Implement
check_energy_balanceinSystem(AC: 1, 2)- Add
check_energy_balance(&self, state: &StateSlice)tocrates/solver/src/system.rs. - Iterate over all components/nodes, computing
\Sigma \dot{Q} + \dot{W}and subtracting\Sigma (\dot{m} \cdot h_{out} - \dot{m} \cdot h_{in}). - Compare total energy residual against the
1\mathrm{e-}6kW threshold.
- Add
- Task 3: Implement Second Law Check (AC: 3, 4)
- Implement
check_entropy_generation(&self, state: &StateSlice, backend: &dyn FluidBackend)to verify\Sigma \Delta s \geq 0. - Log a warning (
tracing::warn!) if entropy generation is negative (impossible thermodynamic state).
- Implement
- Task 4: Hook into Solver & Error Handling (AC: 2, 5)
- Call both
check_energy_balanceandcheck_entropy_generationinsolver.rsright aftercheck_mass_balance. - Update
ThermoError::Validationto properly reportenergy_errorvalues, providing breakdowns if applicable.
- Call both
- Task 5: Unit and Integration Testing (AC: 5)
- Write a unit test
test_energy_balance_passwith a known good state. - Write a unit test
test_energy_balance_failcreating an artificial energy imbalance. - Write tests verifying Second Law warnings.
- Write a unit test
Review Follow-ups (AI)
- [AI-Review][High] No codebase implementation found for this story. All tasks and ACs remain undone.
- [AI-Review][Medium] No tests implemented.
- [AI-Review][High] 25 files modified in git, but File List in story is empty. Ensure changes are committed or documented.
Dev Notes
- Architecture Patterns & Constraints:
- Zero-Panic Policy: Return
ThermoError::Validationif energy balance fails. No unwraps in the validation logic. - Scientific Testing Patterns: Use
approx::assert_relative_eq!(..., epsilon = 1e-6)for energy tolerance. - Physical Types: Ensure mathematical operations on
Power,MassFlow,Enthalpy,Temperature, andEntropyare type-safe. Convert to primitive floats safely if doing aggregate sums, but ensure unit consistency (e.g. convertingMassFlow * Enthalpy(kg/s * J/kg=W) tokWproperly).
- Zero-Panic Policy: Return
- Recent Git Intelligence (from Story 7.1):
- In story 7.1 (
check_mass_balance), a new trait methodport_mass_flows(&self, state: &SystemState)was added toComponent. We likely need a similar trait method or pair of methods (heat_transfer,work_transfer) to query individual component's energy interactions. - Ensure compatibility with
MacroComponentif it needs to flatten energy balances.
- In story 7.1 (
- Source Tree Components to Touch:
crates/components/src/lib.rs(Component trait additions for energy/entropy)- Specific components like
crates/components/src/compressor.rs,heat_exchanger/*.rsto implement the new trait methods. crates/solver/src/system.rs(Energy and entropy balancing logic)crates/solver/src/solver.rs(Hooks for validation after convergence)crates/core/src/types.rs(IfEntropynewtype is missing)
- Testing Standards:
- All warnings (
clippy -D warnings, etc.) must pass. - Validation requires checking the overall $\Sigma$-balances. For testing, it's easiest to mock a small 2-3 component topology or use the existing integration tests in
tests/validation/energy_balance.rsmentioned in prior stories.
- All warnings (
Project Structure Notes
- The changes squarely fit into the defined
ComponentandSystemarchitecture boundary. Modifications are internal to the component and solver logic and do not violate any FFI boundaries directly.
References
- [Source: _bmad-output/planning-artifacts/epics.md] - Epic 7, Story 7.2 Requirements & FR36, FR39
- [Source: _bmad-output/planning-artifacts/architecture.md#Error-Handling-Strategy] - Error Handling
- [Source: _bmad-output/planning-artifacts/architecture.md#Scientific-Testing-Patterns] - Testing Patterns definition
Dev Agent Record
Agent Model Used
BMad Create Story Workflow (Claude 3.5 Sonnet / Antigravity)
Debug Log References
Completion Notes List
- The story
7-2-energy-balance-validation.mdhas been created adhering strictly to the architecture and context guidelines.
File List
_bmad-output/implementation-artifacts/7-2-energy-balance-validation.md