Skip to content

Commit

Permalink
use indexmap instead of hashmap for thermal bridging and building ele…
Browse files Browse the repository at this point in the history
…ments, to get predictable ordering
  • Loading branch information
shieldo committed Jul 20, 2023
1 parent c5a4c37 commit 3f28907
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 10 deletions.
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ edition = "2021"
[dependencies]
clap = { version = "4.3.10", features = ["derive"] }
csv = "1.2.2"
indexmap = { version = "2.0.0", features = ["serde"] }
is_close = "0.1.3"
itertools = "0.11.0"
nalgebra = "0.32.3"
Expand Down
8 changes: 4 additions & 4 deletions src/core/space_heat_demand/thermal_bridge.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use indexmap::IndexMap;
use serde_json::Value;
use std::collections::HashMap;

#[derive(Debug, PartialEq)]
pub enum ThermalBridging {
Number(f64),
Bridges(HashMap<String, ThermalBridge>),
Bridges(IndexMap<String, ThermalBridge>),
}

#[derive(Copy, Clone, Debug, PartialEq)]
Expand Down Expand Up @@ -36,7 +36,7 @@ pub fn heat_transfer_coefficient_for_thermal_bridge(thermal_bridge: &ThermalBrid
pub fn thermal_bridging_from_input(input: Value) -> ThermalBridging {
match input {
Value::Object(map) => {
let mut bridges = HashMap::from([]);
let mut bridges = IndexMap::new();
for (name, bridge_object) in map.clone().iter() {
bridges.insert(
(*name).clone(),
Expand Down Expand Up @@ -101,7 +101,7 @@ mod test {
"heat_transfer_coeff": 2.0
}}
)),
ThermalBridging::Bridges(HashMap::from([
ThermalBridging::Bridges(IndexMap::from([
(
"TB1".to_string(),
ThermalBridge::Linear {
Expand Down
9 changes: 5 additions & 4 deletions src/core/space_heat_demand/zone.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ use crate::core::units::{kelvin_to_celsius, SECONDS_PER_HOUR, WATTS_PER_KILOWATT
use crate::external_conditions::ExternalConditions;
use crate::input::BuildingElement;
use crate::simulation_time::SimulationTimeIterator;
use indexmap::IndexMap;
use nalgebra::{DMatrix, DVector};
use serde::Deserialize;
use std::collections::HashMap;
use std::hash::{Hash, Hasher};

// Convective fractions
Expand Down Expand Up @@ -87,7 +87,7 @@ impl<'a> Zone<'a> {
pub fn new(
area: f64,
volume: f64,
building_elements: HashMap<String, BuildingElement>,
building_elements: IndexMap<String, BuildingElement>,
thermal_bridging: ThermalBridging,
vent_elements: Vec<Box<dyn VentilationElement>>,
vent_cool_extra: Option<WindowOpeningForCooling>,
Expand Down Expand Up @@ -1181,6 +1181,7 @@ mod test {
MassDistributionClass, ZoneInput,
};
use crate::simulation_time::{SimulationTime, HOURS_IN_DAY};
use indexmap::IndexMap;
use rstest::*;

const BASE_AIR_TEMPS: [f64; 24] = [
Expand Down Expand Up @@ -1373,7 +1374,7 @@ mod test {
};

// Put building element objects in a list that can be iterated over
let be_objs = HashMap::from([
let be_objs = IndexMap::from([
("be_opaque_i".to_string(), be_opaque_i),
("be_opaque_d".to_string(), be_opaque_d),
("be_ztc".to_string(), be_ztc),
Expand All @@ -1396,7 +1397,7 @@ mod test {
};

// Put thermal bridge objects in a list that can be iterated over
let thermal_bridging = ThermalBridging::Bridges(HashMap::from([
let thermal_bridging = ThermalBridging::Bridges(IndexMap::from([
("tb_linear_1".to_string(), tb_linear_1),
("tb_linear_2".to_string(), tb_linear_2),
("tb_point".to_string(), tb_point),
Expand Down
5 changes: 3 additions & 2 deletions src/input.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::external_conditions::{DaylightSavingsConfig, ShadingSegment, WindowShadingObject};
use crate::simulation_time::SimulationTime;
use indexmap::IndexMap;
use serde::Deserialize;
use serde_json::{Map, Value};
use std::collections::HashMap;
Expand Down Expand Up @@ -673,7 +674,7 @@ pub struct ZoneInput {
temp_setpnt_cool: Option<f64>,
temp_setpnt_init: Option<f64>,
#[serde(rename(deserialize = "BuildingElement"))]
building_elements: HashMap<String, BuildingElement>,
building_elements: IndexMap<String, BuildingElement>,
#[serde(rename(deserialize = "ThermalBridging"))]
thermal_bridging: Value, // this can be either a float or a hashmap of thermal bridging details - see commented out structs below
}
Expand Down Expand Up @@ -768,7 +769,7 @@ pub enum MassDistributionClass {
// #[derive(Debug, Deserialize)]
// #[serde(untagged)]
// pub enum ThermalBridging {
// ThermalBridgingElements(HashMap<String, ThermalBridgingDetails>),
// ThermalBridgingElements(IndexMap<String, ThermalBridgingDetails>),
// ThermalBridgingNumber(f64),
// }
//
Expand Down

0 comments on commit 3f28907

Please sign in to comment.