Fix bugs from 5-2 code review
This commit is contained in:
@@ -50,9 +50,7 @@ pub enum InitializerError {
|
||||
},
|
||||
|
||||
/// The provided state slice length does not match the system state vector length.
|
||||
#[error(
|
||||
"State slice length {actual} does not match system state vector length {expected}"
|
||||
)]
|
||||
#[error("State slice length {actual} does not match system state vector length {expected}")]
|
||||
StateLengthMismatch {
|
||||
/// Expected length (from `system.state_vector_len()`).
|
||||
expected: usize,
|
||||
@@ -272,10 +270,7 @@ impl SmartInitializer {
|
||||
"Unknown fluid for Antoine estimation — using fallback pressures \
|
||||
(P_evap = 5 bar, P_cond = 20 bar)"
|
||||
);
|
||||
Ok((
|
||||
Pressure::from_bar(5.0),
|
||||
Pressure::from_bar(20.0),
|
||||
))
|
||||
Ok((Pressure::from_bar(5.0), Pressure::from_bar(20.0)))
|
||||
}
|
||||
Some(coeffs) => {
|
||||
let t_source_c = t_source.to_celsius();
|
||||
@@ -514,20 +509,36 @@ mod tests {
|
||||
#[test]
|
||||
fn test_populate_state_2_edges() {
|
||||
use crate::system::System;
|
||||
use entropyk_components::{Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState};
|
||||
use entropyk_components::{
|
||||
Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState,
|
||||
};
|
||||
|
||||
struct MockComp;
|
||||
impl Component for MockComp {
|
||||
fn compute_residuals(&self, _s: &SystemState, r: &mut ResidualVector) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() { *v = 0.0; }
|
||||
fn compute_residuals(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
r: &mut ResidualVector,
|
||||
) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() {
|
||||
*v = 0.0;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn jacobian_entries(&self, _s: &SystemState, j: &mut JacobianBuilder) -> Result<(), ComponentError> {
|
||||
fn jacobian_entries(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
j: &mut JacobianBuilder,
|
||||
) -> Result<(), ComponentError> {
|
||||
j.add_entry(0, 0, 1.0);
|
||||
Ok(())
|
||||
}
|
||||
fn n_equations(&self) -> usize { 1 }
|
||||
fn get_ports(&self) -> &[ConnectedPort] { &[] }
|
||||
fn n_equations(&self) -> usize {
|
||||
1
|
||||
}
|
||||
fn get_ports(&self) -> &[ConnectedPort] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
let mut sys = System::new();
|
||||
@@ -560,29 +571,53 @@ mod tests {
|
||||
#[test]
|
||||
fn test_populate_state_multi_circuit() {
|
||||
use crate::system::{CircuitId, System};
|
||||
use entropyk_components::{Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState};
|
||||
use entropyk_components::{
|
||||
Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState,
|
||||
};
|
||||
|
||||
struct MockComp;
|
||||
impl Component for MockComp {
|
||||
fn compute_residuals(&self, _s: &SystemState, r: &mut ResidualVector) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() { *v = 0.0; }
|
||||
fn compute_residuals(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
r: &mut ResidualVector,
|
||||
) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() {
|
||||
*v = 0.0;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn jacobian_entries(&self, _s: &SystemState, j: &mut JacobianBuilder) -> Result<(), ComponentError> {
|
||||
fn jacobian_entries(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
j: &mut JacobianBuilder,
|
||||
) -> Result<(), ComponentError> {
|
||||
j.add_entry(0, 0, 1.0);
|
||||
Ok(())
|
||||
}
|
||||
fn n_equations(&self) -> usize { 1 }
|
||||
fn get_ports(&self) -> &[ConnectedPort] { &[] }
|
||||
fn n_equations(&self) -> usize {
|
||||
1
|
||||
}
|
||||
fn get_ports(&self) -> &[ConnectedPort] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
let mut sys = System::new();
|
||||
// Circuit 0: evaporator side
|
||||
let n0 = sys.add_component_to_circuit(Box::new(MockComp), CircuitId(0)).unwrap();
|
||||
let n1 = sys.add_component_to_circuit(Box::new(MockComp), CircuitId(0)).unwrap();
|
||||
let n0 = sys
|
||||
.add_component_to_circuit(Box::new(MockComp), CircuitId(0))
|
||||
.unwrap();
|
||||
let n1 = sys
|
||||
.add_component_to_circuit(Box::new(MockComp), CircuitId(0))
|
||||
.unwrap();
|
||||
// Circuit 1: condenser side
|
||||
let n2 = sys.add_component_to_circuit(Box::new(MockComp), CircuitId(1)).unwrap();
|
||||
let n3 = sys.add_component_to_circuit(Box::new(MockComp), CircuitId(1)).unwrap();
|
||||
let n2 = sys
|
||||
.add_component_to_circuit(Box::new(MockComp), CircuitId(1))
|
||||
.unwrap();
|
||||
let n3 = sys
|
||||
.add_component_to_circuit(Box::new(MockComp), CircuitId(1))
|
||||
.unwrap();
|
||||
|
||||
sys.add_edge(n0, n1).unwrap(); // circuit 0 edge
|
||||
sys.add_edge(n2, n3).unwrap(); // circuit 1 edge
|
||||
@@ -598,7 +633,7 @@ mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(state.len(), 4); // 2 edges × 2 entries
|
||||
// Edge 0 (circuit 0) → p_evap
|
||||
// Edge 0 (circuit 0) → p_evap
|
||||
assert_relative_eq!(state[0], p_evap.to_pascals(), max_relative = 1e-9);
|
||||
assert_relative_eq!(state[1], h_default.to_joules_per_kg(), max_relative = 1e-9);
|
||||
// Edge 1 (circuit 1) → p_cond
|
||||
@@ -610,20 +645,36 @@ mod tests {
|
||||
#[test]
|
||||
fn test_populate_state_length_mismatch() {
|
||||
use crate::system::System;
|
||||
use entropyk_components::{Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState};
|
||||
use entropyk_components::{
|
||||
Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState,
|
||||
};
|
||||
|
||||
struct MockComp;
|
||||
impl Component for MockComp {
|
||||
fn compute_residuals(&self, _s: &SystemState, r: &mut ResidualVector) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() { *v = 0.0; }
|
||||
fn compute_residuals(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
r: &mut ResidualVector,
|
||||
) -> Result<(), ComponentError> {
|
||||
for v in r.iter_mut() {
|
||||
*v = 0.0;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
fn jacobian_entries(&self, _s: &SystemState, j: &mut JacobianBuilder) -> Result<(), ComponentError> {
|
||||
fn jacobian_entries(
|
||||
&self,
|
||||
_s: &SystemState,
|
||||
j: &mut JacobianBuilder,
|
||||
) -> Result<(), ComponentError> {
|
||||
j.add_entry(0, 0, 1.0);
|
||||
Ok(())
|
||||
}
|
||||
fn n_equations(&self) -> usize { 1 }
|
||||
fn get_ports(&self) -> &[ConnectedPort] { &[] }
|
||||
fn n_equations(&self) -> usize {
|
||||
1
|
||||
}
|
||||
fn get_ports(&self) -> &[ConnectedPort] {
|
||||
&[]
|
||||
}
|
||||
}
|
||||
|
||||
let mut sys = System::new();
|
||||
@@ -642,7 +693,10 @@ mod tests {
|
||||
let result = init.populate_state(&sys, p_evap, p_cond, h_default, &mut state);
|
||||
assert!(matches!(
|
||||
result,
|
||||
Err(InitializerError::StateLengthMismatch { expected: 2, actual: 5 })
|
||||
Err(InitializerError::StateLengthMismatch {
|
||||
expected: 2,
|
||||
actual: 5
|
||||
})
|
||||
));
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user