Skip to content

Commit

Permalink
feat: implement fractional numbers and serialization tests (#47)
Browse files Browse the repository at this point in the history
* feat: implement fractional numbers

* refactor: move fluid types into fluid module

* add more tests

* remove unused generation function
  • Loading branch information
kayagokalp authored Jun 16, 2024
1 parent 56ec267 commit 91e4f08
Show file tree
Hide file tree
Showing 12 changed files with 387 additions and 224 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

3 changes: 1 addition & 2 deletions fluido-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ use fluido_ir::{
};
use fluido_parse::parser::Parse;
use fluido_types::{
concentration::Concentration,
error::{
FluidoError, IRGenerationError, InterefenceGraphGenerationError, MixerGenerationError,
},
expr::Expr,
fluid::Fluid,
fluid::{Concentration, Fluid},
};

/// A mixer generator for a specific target concentration from a given input space.
Expand Down
19 changes: 1 addition & 18 deletions fluido-generation/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use egg::{rewrite as rw, *};
use fluido_types::{
concentration::{Concentration, LimitedFloat, Volume},
error::MixerGenerationError,
fluid::Fluid,
fluid::{Concentration, Fluid, LimitedFloat, Volume},
};
use std::{collections::HashSet, time::Duration};

Expand Down Expand Up @@ -372,22 +371,6 @@ fn concentration_valid(
}
}

/// Generate all possible fluids with given start and end with step sizes.
pub fn generate_all_fluids() -> Vec<Fluid> {
let epsilon = Concentration::EPSILON;
let end = (1.0 / epsilon) as usize;

let mut result = Vec::with_capacity(end);
for i in 0..end {
let concentrationtion = Concentration::new(i as i64);
let volume = 1.0.into();
let fluid = Fluid::new(concentrationtion, volume);
result.push(fluid);
}

result
}

fn normalize_expr_by_min_volume(expr: &RecExpr<MixLang>) -> String {
// Find the smallest volume in the expression
let mut min_volume: Option<f64> = None;
Expand Down
7 changes: 4 additions & 3 deletions fluido-parse/src/parser.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![allow(clippy::empty_docs)]
use fluido_types::{
concentration::LimitedFloat, error::IRGenerationError, expr::Expr, fluid::Fluid,
error::IRGenerationError,
expr::Expr,
fluid::{Fluid, LimitedFloat},
};
use pest::Parser;
use pest_derive::Parser;
Expand Down Expand Up @@ -56,9 +58,8 @@ fn build_ast(pairs: pest::iterators::Pairs<Rule>) -> Result<Expr, IRGenerationEr
mod tests {
use crate::parser::Parse;
use fluido_types::{
concentration::{Concentration, Volume},
expr::Expr,
fluid::Fluid,
fluid::{Concentration, Fluid, Volume},
};

#[test]
Expand Down
3 changes: 3 additions & 0 deletions fluido-types/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,6 @@ anyhow = { workspace = true }
egg = { workspace = true }
serde = { workspace = true, features = ["derive"] }
thiserror = { workspace = true }

[dev-dependencies]
serde_test = "1.0.176"
191 changes: 0 additions & 191 deletions fluido-types/src/concentration.rs

This file was deleted.

2 changes: 1 addition & 1 deletion fluido-types/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::concentration::Concentration;
use crate::fluid::Concentration;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down
4 changes: 2 additions & 2 deletions fluido-types/src/expr.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::{concentration::LimitedFloat, fluid::Fluid};
use crate::fluid::{Concentration, Fluid};

#[derive(Debug, PartialEq, Clone, Eq, Hash)]
pub enum Expr {
Mix(Box<Expr>, Box<Expr>),
LimitedFloat(LimitedFloat),
LimitedFloat(Concentration),
Fluid(Fluid),
}
9 changes: 4 additions & 5 deletions fluido-types/src/fluid.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{fmt::Display, num::ParseFloatError, str::FromStr};

use crate::concentration::{Concentration, Volume};
pub use crate::number::LimitedFloat;
pub type Concentration = LimitedFloat;
pub type Volume = LimitedFloat;

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Fluid {
Expand Down Expand Up @@ -123,12 +125,9 @@ impl Fluid {

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

use crate::concentration::{Concentration, Volume};

use super::Fluid;

#[test]
fn mix_two_equal_volume_fluids() {
let concentration_a = Concentration::from(0.1);
Expand Down
2 changes: 1 addition & 1 deletion fluido-types/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
pub mod concentration;
pub mod error;
pub mod expr;
pub mod fluid;
pub mod number;
Loading

0 comments on commit 91e4f08

Please sign in to comment.