Skip to content

Implementation of mass_balance_check() and energy_balance_check() trait functions in blocks module #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 11, 2024
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
/target
.DS_Store
1 change: 1 addition & 0 deletions Cargo.lock

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

5 changes: 4 additions & 1 deletion oscps-gui/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,8 @@
"type": "module",
"dependencies": {
"@tauri-apps/api": "^1.5.6"
}
},
"trustedDependencies": [
"svelte-preprocess"
]
}
1 change: 1 addition & 0 deletions oscps-lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ edition = "2021"

[dependencies]
uom = "0.36.0"
once_cell = "1.17.1"
126 changes: 106 additions & 20 deletions oscps-lib/src/blocks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Energy> = Lazy::new(|| Energy::new::<joule>(5.0));

// trait ElementBalance {
// fn element_balance(){}
// }
//Initiallizing a global variable for the tolerance for the mass balance
static TOLERENCE_MASS: Lazy<Mass> = Lazy::new(|| Mass::new::<kilogram>(5.0));

// pub struct Mixer{
// pub block_id: String,
// pub input_stream: Vec<connector::Mconnector>,
// 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::<kilogram>();
let mass_out_kg = mass_out.get::<kilogram>();

// impl MassBalance for Mixer{
let mass_difference = mass_in_kg - mass_out_kg;

// }
return mass_difference <= TOLERENCE_MASS.get::<kilogram>();
}
}

// 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::<joule>();
let energy_out_joules = energy_out.get::<joule>();

// 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::<joule>();

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<connector::Mconnector>,
//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::<pound>(100.0);
let mass_out = Mass::new::<pound>(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::<kilojoule>(10.0);
let energy_out = Energy::new::<kilojoule>(95.0);
assert!(mixer_test_obj.energy_balance_check(energy_in, energy_out));
}
}
26 changes: 15 additions & 11 deletions oscps-lib/src/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<kelvin>(273.15);
let water_boiling_point = ThermodynamicTemperature::new::<kelvin>(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::<kelvin>(273.15);
let water_boiling_point = ThermodynamicTemperature::new::<kelvin>(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(())
}
}
18 changes: 9 additions & 9 deletions oscps-lib/src/connector.rs
Original file line number Diff line number Diff line change
@@ -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
}