diff --git a/.gitignore b/.gitignore index ea8c4bf..0592392 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ /target +.DS_Store diff --git a/Cargo.lock b/Cargo.lock index db5a28b..ee6fa03 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1679,6 +1679,7 @@ checksum = "3fdb12b2476b595f9358c5161aa467c2438859caa136dec86c26fdd2efe17b92" name = "oscps-lib" version = "0.1.0" dependencies = [ + "once_cell", "uom", ] diff --git a/oscps-gui/package.json b/oscps-gui/package.json index 0b00707..8f2f021 100644 --- a/oscps-gui/package.json +++ b/oscps-gui/package.json @@ -34,5 +34,8 @@ "type": "module", "dependencies": { "@tauri-apps/api": "^1.5.6" - } + }, + "trustedDependencies": [ + "svelte-preprocess" + ] } diff --git a/oscps-lib/Cargo.toml b/oscps-lib/Cargo.toml index 797037d..bc023d5 100644 --- a/oscps-lib/Cargo.toml +++ b/oscps-lib/Cargo.toml @@ -5,3 +5,4 @@ edition = "2021" [dependencies] uom = "0.36.0" +once_cell = "1.17.1" diff --git a/oscps-lib/src/blocks.rs b/oscps-lib/src/blocks.rs index 6f92d08..46ad169 100644 --- a/oscps-lib/src/blocks.rs +++ b/oscps-lib/src/blocks.rs @@ -4,33 +4,119 @@ //! implemented by various structs to represent different unit operations. //! //! For example, if a block is a simple mixer, then it will implement the -//! MassBalance trait but not th +//! MassBalance trait but not the EnergyBalance. //! -// use crate::connector; -// trait MassBalance { -// fn overall_massive_balance() {} -// } +use uom::si::f64::{Energy}; +use uom::si::f64::{Mass}; +use uom::si::mass::kilogram; +use uom::si::energy::joule; +use crate::connector; +use once_cell::sync::Lazy; -// trait EnergyBalance { -// fn energy_balance(){} -// } +//Initiallizing a global variable for the tolerance for the energy balance +static TOLERENCE_ENERGY: Lazy = Lazy::new(|| Energy::new::(5.0)); -// trait ElementBalance { -// fn element_balance(){} -// } +//Initiallizing a global variable for the tolerance for the mass balance +static TOLERENCE_MASS: Lazy = Lazy::new(|| Mass::new::(5.0)); -// pub struct Mixer{ -// pub block_id: String, -// pub input_stream: Vec, -// pub output_stream: connector::Mconnector -// } +/// Trait for ensuring the overall mass balance is maintained in a flowsheet. +/// +/// This trait can be implemented by any block that needs to ensure mass conservation. +pub trait MassBalance { + // total mass in - total mass out < tolerance + fn mass_balance_check(&self, mass_in : Mass, mass_out : Mass) -> bool { + let mass_in_kg = mass_in.get::(); + let mass_out_kg = mass_out.get::(); -// impl MassBalance for Mixer{ + let mass_difference = mass_in_kg - mass_out_kg; -// } + return mass_difference <= TOLERENCE_MASS.get::(); + } +} -// impl EnergyBalance for Mixer{ +/// # EnergyBalance +/// +/// This trait ensures that blocks in the flowsheet adhere to energy conservation principles. +/// +/// This is useful for distinguishing between "dynamic" and "steady state" simulations. +trait EnergyBalance { -// } + // total energy in - total energy out < tolerance + fn energy_balance_check(&self, energy_in : Energy, energy_out : Energy) -> bool { + // Convert both energy_in and energy_out to joules + let energy_in_joules = energy_in.get::(); + let energy_out_joules = energy_out.get::(); + + // Calculate the difference between energy_in and energy_out in joules + let energy_difference = energy_in_joules - energy_out_joules; + + // Check if the energy difference is less than the global threshold + let within_threshold = energy_difference <= TOLERENCE_ENERGY.get::(); + + return within_threshold; + + } +} + +/// # ElementBalance (also known as Atomic Balance) +/// +/// This trait ensures atomic conservation, especially relevant in reactor simulations. +/// +/// Similar to the EnergyBalance trait, this is useful for determining the nature of the simulation (dynamic or steady state). +pub trait ElementBalance { + fn element_balance_check() {} +} + +/// # Mixer Block +/// +/// A block used for simple stream mixing operations. +/// +/// This struct requires the implementation of both EnergyBalance and MassBalance traits to ensure proper conservation principles are followed. +pub struct Mixer { + pub block_id: String, + //pub input_stream: Vec, + //pub output_stream: connector::Mconnector, +} + +impl MassBalance for Mixer { + // Implement mass balance methods specific to Mixer here. +} + +impl EnergyBalance for Mixer { + // Implement energy balance methods specific to Mixer here. +} + + +#[cfg(test)] +mod block_tests { + use super::*; + use std::io; + use uom::si::f64::Energy; + use uom::si::energy::kilojoule; + use uom::si::mass::pound; + + #[test] + fn test_mass_balance_check_steady_state_for_mixer() { + // here you will need to check that the mass into the mixer = mass out of mixer + + let mixer_test_obj = Mixer{ + block_id : String::from("Test Mixer") + }; + let mass_in = Mass::new::(100.0); + let mass_out = Mass::new::(95.0); + assert!(mixer_test_obj.mass_balance_check(mass_in, mass_out)); + } + + #[test] + fn test_energy_balance_check_steady_state_for_mixer() { + // energy into mixer = energy out of mixer + let mixer_test_obj = Mixer{ + block_id : String::from("Test Mixer") + }; + let energy_in = Energy::new::(10.0); + let energy_out = Energy::new::(95.0); + assert!(mixer_test_obj.energy_balance_check(energy_in, energy_out)); + } +} diff --git a/oscps-lib/src/component.rs b/oscps-lib/src/component.rs index 7f64572..0d979c3 100644 --- a/oscps-lib/src/component.rs +++ b/oscps-lib/src/component.rs @@ -57,16 +57,20 @@ impl ChemicalProperties { } } -use std::io; -use uom::si::thermodynamic_temperature::kelvin; +#[cfg(test)] +mod component_tests { + use super::*; + use std::io; + use uom::si::thermodynamic_temperature::kelvin; -#[test] -fn test_chemical_properties_constructor() -> io::Result<()> { - // Test using water - let water_melting_point = ThermodynamicTemperature::new::(273.15); - let water_boiling_point = ThermodynamicTemperature::new::(373.15); - let water_properties = ChemicalProperties::new(water_melting_point, water_boiling_point); - assert_eq!(water_properties.normal_melting_point, water_melting_point); - assert_eq!(water_properties.normal_boiling_point, water_boiling_point); - Ok(()) + #[test] + fn test_chemical_properties_constructor() -> io::Result<()> { + // Test using water + let water_melting_point = ThermodynamicTemperature::new::(273.15); + let water_boiling_point = ThermodynamicTemperature::new::(373.15); + let water_properties = ChemicalProperties::new(water_melting_point, water_boiling_point); + assert_eq!(water_properties.normal_melting_point, water_melting_point); + assert_eq!(water_properties.normal_boiling_point, water_boiling_point); + Ok(()) + } } diff --git a/oscps-lib/src/connector.rs b/oscps-lib/src/connector.rs index 3de38ab..6581f5a 100644 --- a/oscps-lib/src/connector.rs +++ b/oscps-lib/src/connector.rs @@ -1,11 +1,11 @@ -//here we will have 2 connector structs - //- Mass Streams - //- Energy Streams -// pub struct Mconnector{ -// m_conn_id: String -// } +//!here we will have 2 connector structs + //!- Mass Streams + //!- Energy Streams +pub struct Mconnector{ + m_conn_id: String +} -// pub struct Econnector{ -// e_conn_id: String -// } +pub struct Econnector{ + e_conn_id: String +}