chore: sync project state and current artifacts
This commit is contained in:
@@ -4,13 +4,13 @@
|
||||
//! - AC: Components can dynamically read calibration factors (e.g. f_m, f_ua) from SystemState.
|
||||
//! - AC: The solver successfully optimizes these calibration factors to meet constraints.
|
||||
|
||||
use entropyk_components::{Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, SystemState};
|
||||
use entropyk_components::{
|
||||
Component, ComponentError, ConnectedPort, JacobianBuilder, ResidualVector, StateSlice,
|
||||
};
|
||||
use entropyk_core::CalibIndices;
|
||||
use entropyk_solver::{
|
||||
System, NewtonConfig, Solver,
|
||||
inverse::{
|
||||
BoundedVariable, BoundedVariableId, Constraint, ConstraintId, ComponentOutput,
|
||||
},
|
||||
inverse::{BoundedVariable, BoundedVariableId, ComponentOutput, Constraint, ConstraintId},
|
||||
NewtonConfig, Solver, System,
|
||||
};
|
||||
|
||||
/// A mock component that simulates a heat exchanger whose capacity depends on `f_ua`.
|
||||
@@ -21,28 +21,28 @@ struct MockCalibratedComponent {
|
||||
impl Component for MockCalibratedComponent {
|
||||
fn compute_residuals(
|
||||
&self,
|
||||
state: &SystemState,
|
||||
state: &StateSlice,
|
||||
residuals: &mut ResidualVector,
|
||||
) -> Result<(), ComponentError> {
|
||||
// Fix the edge states to a known value
|
||||
residuals[0] = state[0] - 300.0;
|
||||
residuals[1] = state[1] - 400.0;
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn jacobian_entries(
|
||||
&self,
|
||||
_state: &SystemState,
|
||||
_state: &StateSlice,
|
||||
jacobian: &mut JacobianBuilder,
|
||||
) -> Result<(), ComponentError> {
|
||||
// d(r0)/d(state[0]) = 1.0
|
||||
jacobian.add_entry(0, 0, 1.0);
|
||||
// d(r1)/d(state[1]) = 1.0
|
||||
jacobian.add_entry(1, 1, 1.0);
|
||||
|
||||
|
||||
// No dependence of physical equations on f_ua
|
||||
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -62,17 +62,17 @@ impl Component for MockCalibratedComponent {
|
||||
#[test]
|
||||
fn test_inverse_calibration_f_ua() {
|
||||
let mut sys = System::new();
|
||||
|
||||
|
||||
// Create a mock component
|
||||
let mock = Box::new(MockCalibratedComponent {
|
||||
calib_indices: CalibIndices::default(),
|
||||
});
|
||||
let comp_id = sys.add_component(mock);
|
||||
sys.register_component_name("evaporator", comp_id);
|
||||
|
||||
|
||||
// Add a self-edge just to simulate some connections
|
||||
sys.add_edge(comp_id, comp_id).unwrap();
|
||||
|
||||
|
||||
// We want the capacity to be exactly 4015 W.
|
||||
// The mocked math in System::extract_constraint_values_with_controls:
|
||||
// Capacity = state[1] * 10.0 + f_ua * 10.0 (primary effect)
|
||||
@@ -87,54 +87,61 @@ fn test_inverse_calibration_f_ua() {
|
||||
component_id: "evaporator".to_string(),
|
||||
},
|
||||
4015.0,
|
||||
)).unwrap();
|
||||
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
// Bounded variable (the calibration factor f_ua)
|
||||
let bv = BoundedVariable::with_component(
|
||||
BoundedVariableId::new("f_ua"),
|
||||
"evaporator",
|
||||
1.0, // initial
|
||||
0.1, // min
|
||||
10.0 // max
|
||||
).unwrap();
|
||||
1.0, // initial
|
||||
0.1, // min
|
||||
10.0, // max
|
||||
)
|
||||
.unwrap();
|
||||
sys.add_bounded_variable(bv).unwrap();
|
||||
|
||||
|
||||
// Link constraint to control
|
||||
sys.link_constraint_to_control(
|
||||
&ConstraintId::new("capacity_control"),
|
||||
&BoundedVariableId::new("f_ua")
|
||||
).unwrap();
|
||||
|
||||
&BoundedVariableId::new("f_ua"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
sys.finalize().unwrap();
|
||||
|
||||
|
||||
// Verify that the validation passes
|
||||
assert!(sys.validate_inverse_control_dof().is_ok());
|
||||
|
||||
let initial_state = vec![0.0; sys.full_state_vector_len()];
|
||||
|
||||
|
||||
// Use NewtonRaphson
|
||||
let mut solver = NewtonConfig::default().with_initial_state(initial_state);
|
||||
|
||||
|
||||
let result = solver.solve(&mut sys);
|
||||
|
||||
|
||||
// Should converge quickly
|
||||
assert!(dbg!(&result).is_ok());
|
||||
let converged = result.unwrap();
|
||||
|
||||
|
||||
// The control variable `f_ua` is at the end of the state vector
|
||||
let f_ua_idx = sys.full_state_vector_len() - 1;
|
||||
let final_f_ua: f64 = converged.state[f_ua_idx];
|
||||
|
||||
|
||||
// Target f_ua = 1.5
|
||||
let abs_diff = (final_f_ua - 1.5_f64).abs();
|
||||
assert!(abs_diff < 1e-4, "f_ua should converge to 1.5, got {}", final_f_ua);
|
||||
assert!(
|
||||
abs_diff < 1e-4,
|
||||
"f_ua should converge to 1.5, got {}",
|
||||
final_f_ua
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_inverse_expansion_valve_calibration() {
|
||||
use entropyk_components::expansion_valve::ExpansionValve;
|
||||
use entropyk_components::port::{FluidId, Port};
|
||||
use entropyk_core::{Pressure, Enthalpy};
|
||||
use entropyk_core::{Enthalpy, Pressure};
|
||||
|
||||
let mut sys = System::new();
|
||||
|
||||
@@ -149,7 +156,7 @@ fn test_inverse_expansion_valve_calibration() {
|
||||
Pressure::from_bar(10.0),
|
||||
Enthalpy::from_joules_per_kg(250000.0),
|
||||
);
|
||||
|
||||
|
||||
let inlet_target = Port::new(
|
||||
FluidId::new("R134a"),
|
||||
Pressure::from_bar(10.0),
|
||||
@@ -160,9 +167,13 @@ fn test_inverse_expansion_valve_calibration() {
|
||||
Pressure::from_bar(10.0),
|
||||
Enthalpy::from_joules_per_kg(250000.0),
|
||||
);
|
||||
|
||||
|
||||
let valve_disconnected = ExpansionValve::new(inlet, outlet, Some(1.0)).unwrap();
|
||||
let valve = Box::new(valve_disconnected.connect(inlet_target, outlet_target).unwrap());
|
||||
let valve = Box::new(
|
||||
valve_disconnected
|
||||
.connect(inlet_target, outlet_target)
|
||||
.unwrap(),
|
||||
);
|
||||
let comp_id = sys.add_component(valve);
|
||||
sys.register_component_name("valve", comp_id);
|
||||
|
||||
@@ -175,14 +186,16 @@ fn test_inverse_expansion_valve_calibration() {
|
||||
// Wait, let's look at ExpansionValve residuals:
|
||||
// residuals[1] = mass_flow_out - f_m * mass_flow_in;
|
||||
// state[0] = mass_flow_in, state[1] = mass_flow_out
|
||||
|
||||
|
||||
sys.add_constraint(Constraint::new(
|
||||
ConstraintId::new("flow_control"),
|
||||
ComponentOutput::Capacity { // Mocking output for test
|
||||
ComponentOutput::Capacity {
|
||||
// Mocking output for test
|
||||
component_id: "valve".to_string(),
|
||||
},
|
||||
0.5,
|
||||
)).unwrap();
|
||||
))
|
||||
.unwrap();
|
||||
|
||||
// Add a bounded variable for f_m
|
||||
let bv = BoundedVariable::with_component(
|
||||
@@ -190,14 +203,16 @@ fn test_inverse_expansion_valve_calibration() {
|
||||
"valve",
|
||||
1.0, // initial
|
||||
0.1, // min
|
||||
2.0 // max
|
||||
).unwrap();
|
||||
2.0, // max
|
||||
)
|
||||
.unwrap();
|
||||
sys.add_bounded_variable(bv).unwrap();
|
||||
|
||||
sys.link_constraint_to_control(
|
||||
&ConstraintId::new("flow_control"),
|
||||
&BoundedVariableId::new("f_m")
|
||||
).unwrap();
|
||||
&BoundedVariableId::new("f_m"),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
sys.finalize().unwrap();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user