Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions src/climate/carbon.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// Average factors (approximate global/US averages)
const CO2_KG_PER_KWH: f64 = 0.385; // ~0.85 lbs -> 0.385 kg (US Avg)
const CO2_KG_PER_GALLON_GAS: f64 = 8.887; // EPA standard

/// Calculates estimated carbon footprint from electricity usage.
///
/// # Arguments
/// * `kwh` - Electricity used in kilowatt-hours
///
/// # Returns
/// * Carbon footprint in kg CO2e
pub fn calculate_carbon_footprint_electricity(kwh: f64) -> Result<f64, String> {
if kwh < 0.0 {
return Err("Usage cannot be negative".into());
}
Ok(kwh * CO2_KG_PER_KWH)
}

/// Calculates estimated carbon footprint from gasoline usage.
///
/// # Arguments
/// * `gallons` - Fuel consumed in gallons
///
/// # Returns
/// * Carbon footprint in kg CO2e
pub fn calculate_carbon_footprint_fuel(gallons: f64) -> Result<f64, String> {
if gallons < 0.0 {
return Err("Fuel cannot be negative".into());
}
Ok(gallons * CO2_KG_PER_GALLON_GAS)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_electricity_carbon() {
assert!((calculate_carbon_footprint_electricity(100.0).unwrap() - 38.5).abs() < 0.1);
}
}
5 changes: 5 additions & 0 deletions src/climate/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
//! Climate and environmental calculators.

pub mod carbon;

pub use carbon::{calculate_carbon_footprint_electricity, calculate_carbon_footprint_fuel};
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
//! assert!(fv > 14_000.0);
//! ```

pub mod climate;
pub mod energy;
pub mod finance;
pub mod finance_investment;
Expand Down
27 changes: 27 additions & 0 deletions src/logistics/freight.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/// Calculates the volumetric weight of a shipment.
///
/// # Arguments
/// * `length` - Length in cm
/// * `width` - Width in cm
/// * `height` - Height in cm
/// * `divisor` - Volumetric factor (Standard: 5000 for Air/Courier, 6000 often for others)
///
/// # Returns
/// * Volumetric Weight in kg
pub fn calculate_volumetric_weight(length: f64, width: f64, height: f64, divisor: f64) -> Result<f64, String> {
if length <= 0.0 || width <= 0.0 || height <= 0.0 || divisor <= 0.0 {
return Err("Dimensions and divisor must be greater than zero".into());
}
Ok((length * width * height) / divisor)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_volumetric_weight() {
// 50x40x30 box, divisor 5000 = 60000 / 5000 = 12kg
assert_eq!(calculate_volumetric_weight(50.0, 40.0, 30.0, 5000.0).unwrap(), 12.0);
}
}
26 changes: 26 additions & 0 deletions src/logistics/inventory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/// Calculates Economic Order Quantity (EOQ).
///
/// # Arguments
/// * `demand_rate` - Annual demand quantity
/// * `ordering_cost` - Cost per order (setup cost)
/// * `holding_cost` - Annual holding cost per unit
///
/// # Returns
/// * Optimal order quantity
pub fn calculate_eoq(demand_rate: f64, ordering_cost: f64, holding_cost: f64) -> Result<f64, String> {
if demand_rate <= 0.0 || ordering_cost <= 0.0 || holding_cost <= 0.0 {
return Err("All inputs must be greater than zero".into());
}
Ok(((2.0 * demand_rate * ordering_cost) / holding_cost).sqrt())
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_eoq() {
// D=1000, S=10, H=2 -> sqrt(2*1000*10 / 2) = sqrt(10000) = 100
assert_eq!(calculate_eoq(1000.0, 10.0, 2.0).unwrap(), 100.0);
}
}
7 changes: 7 additions & 0 deletions src/logistics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Logistics and Supply Chain calculators.

pub mod freight;
pub mod inventory;

pub use freight::calculate_volumetric_weight;
pub use inventory::calculate_eoq;
29 changes: 29 additions & 0 deletions src/physics/fluid.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/// Calculates Reynolds Number to predict fluid flow regime (laminar vs turbulent).
///
/// # Arguments
/// * `velocity` - Fluid velocity in m/s
/// * `characteristic_length` - Diameter of pipe or length in m
/// * `kinematic_viscosity` - Fluid kinematic viscosity in m²/s (e.g., water ~1e-6 at 20C)
///
/// # Returns
/// * Reynolds Number (dimensionless)
pub fn calculate_reynolds_number(velocity: f64, characteristic_length: f64, kinematic_viscosity: f64) -> Result<f64, String> {
if kinematic_viscosity <= 0.0 {
return Err("Viscosity must be greater than zero".into());
}
if characteristic_length < 0.0 {
return Err("Length cannot be negative".into());
}
Ok((velocity * characteristic_length) / kinematic_viscosity)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_reynolds() {
let re = calculate_reynolds_number(10.0, 0.5, 1e-6).unwrap();
assert_eq!(re, 5_000_000.0);
}
}
47 changes: 47 additions & 0 deletions src/physics/kinematics.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
const GRAVITY: f64 = 9.81;

/// Calculates kinetic energy.
///
/// # Arguments
/// * `mass` - Mass in kg
/// * `velocity` - Velocity in m/s
///
/// # Returns
/// * Kinetic Energy in Joules (J)
pub fn calculate_kinetic_energy(mass: f64, velocity: f64) -> Result<f64, String> {
if mass < 0.0 {
return Err("Mass cannot be negative".into());
}
Ok(0.5 * mass * velocity.powi(2))
}

/// Calculates gravitational potential energy.
///
/// # Arguments
/// * `mass` - Mass in kg
/// * `height` - Height in m
///
/// # Returns
/// * Potential Energy in Joules (J)
pub fn calculate_potential_energy(mass: f64, height: f64) -> Result<f64, String> {
if mass < 0.0 {
return Err("Mass cannot be negative".into());
}
Ok(mass * GRAVITY * height)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_kinetic() {
assert_eq!(calculate_kinetic_energy(10.0, 2.0).unwrap(), 20.0);
}

#[test]
fn test_potential() {
let pe = calculate_potential_energy(10.0, 10.0).unwrap();
assert!((pe - 981.0).abs() < 0.1);
}
}
7 changes: 7 additions & 0 deletions src/physics/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
//! Physics calculators for engineering and science.

pub mod fluid;
pub mod kinematics;

pub use fluid::calculate_reynolds_number;
pub use kinematics::{calculate_kinetic_energy, calculate_potential_energy};