Skip to content
Open
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
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ keywords = [
categories = ["science"]

[workspace.dependencies]
quantity = "0.12"
quantity = { git = "https://github.com/itt-ustutt/quantity", branch = "consts_instead_of_typenum" }
num-dual = "0.13"
ndarray = "0.17"
nalgebra = "0.34"
Expand All @@ -33,7 +33,6 @@ serde = "1.0"
serde_json = "1.0"
indexmap = "2.0"
itertools = "0.14"
typenum = "1.16"
rayon = "1.11"
petgraph = "0.8"
rustdct = "0.7"
Expand Down
1 change: 0 additions & 1 deletion crates/feos-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ serde = { workspace = true, features = ["derive"] }
serde_json = { workspace = true, features = ["preserve_order"] }
indexmap = { workspace = true, features = ["serde"] }
rayon = { workspace = true, optional = true }
typenum = { workspace = true }
itertools = { workspace = true }

[dev-dependencies]
Expand Down
5 changes: 3 additions & 2 deletions crates/feos-core/src/equation_of_state/residual.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ use nalgebra::{DVector, DefaultAllocator, Dim, Dyn, OMatrix, OVector, U1, alloca
use num_dual::{DualNum, Gradients, partial, partial2, second_derivative, third_derivative};
use quantity::ad::first_derivative;
use quantity::*;
use std::ops::Deref;
use std::ops::{Deref, Div};
use std::sync::Arc;
use typenum::Quot;

type Quot<T1, T2> = <T1 as Div<T2>>::Output;

/// Molar weight of all components.
///
Expand Down
55 changes: 31 additions & 24 deletions crates/feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#![warn(clippy::allow_attributes)]
use quantity::{Quantity, SIUnit};
use std::ops::{Div, Mul};
use typenum::Integer;

/// Print messages with level `Verbosity::Iter` or higher.
#[macro_export]
Expand Down Expand Up @@ -132,20 +131,20 @@ const fn powi(x: f64, n: i32) -> f64 {
/// Conversion between reduced units and SI units.
pub trait ReferenceSystem {
type Inner;
type T: Integer;
type L: Integer;
type M: Integer;
type I: Integer;
type THETA: Integer;
type N: Integer;
type J: Integer;
const FACTOR: f64 = powi(REFERENCE_VALUES[0], Self::T::I32)
* powi(REFERENCE_VALUES[1], Self::L::I32)
* powi(REFERENCE_VALUES[2], Self::M::I32)
* powi(REFERENCE_VALUES[3], Self::I::I32)
* powi(REFERENCE_VALUES[4], Self::THETA::I32)
* powi(REFERENCE_VALUES[5], Self::N::I32)
* powi(REFERENCE_VALUES[6], Self::J::I32);
const T: i8;
const L: i8;
const M: i8;
const I: i8;
const THETA: i8;
const N: i8;
const J: i8;
const FACTOR: f64 = powi(REFERENCE_VALUES[0], Self::T as i32)
* powi(REFERENCE_VALUES[1], Self::L as i32)
* powi(REFERENCE_VALUES[2], Self::M as i32)
* powi(REFERENCE_VALUES[3], Self::I as i32)
* powi(REFERENCE_VALUES[4], Self::THETA as i32)
* powi(REFERENCE_VALUES[5], Self::N as i32)
* powi(REFERENCE_VALUES[6], Self::J as i32);

fn from_reduced(value: Self::Inner) -> Self
where
Expand All @@ -161,17 +160,25 @@ pub trait ReferenceSystem {
}

/// Conversion to and from reduced units
impl<Inner, T: Integer, L: Integer, M: Integer, I: Integer, THETA: Integer, N: Integer, J: Integer>
ReferenceSystem for Quantity<Inner, SIUnit<T, L, M, I, THETA, N, J>>
impl<
Inner,
const T: i8,
const L: i8,
const M: i8,
const I: i8,
const THETA: i8,
const N: i8,
const J: i8,
> ReferenceSystem for Quantity<Inner, SIUnit<T, L, M, I, THETA, N, J>>
{
type Inner = Inner;
type T = T;
type L = L;
type M = M;
type I = I;
type THETA = THETA;
type N = N;
type J = J;
const T: i8 = T;
const L: i8 = L;
const M: i8 = M;
const I: i8 = I;
const THETA: i8 = THETA;
const N: i8 = N;
const J: i8 = J;
fn from_reduced(value: Inner) -> Self
where
Inner: Mul<f64, Output = Inner>,
Expand Down
3 changes: 1 addition & 2 deletions crates/feos-core/src/phase_equilibria/bubble_dew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use ndarray::Array1;
use num_dual::linalg::LU;
use num_dual::{DualNum, DualStruct, Gradients};
use quantity::{Density, Dimensionless, Moles, Pressure, Quantity, RGAS, SIUnit, Temperature};
use typenum::{N1, N2, P1, Z0};

const MAX_ITER_INNER: usize = 5;
const TOL_INNER: f64 = 1e-9;
Expand Down Expand Up @@ -94,7 +93,7 @@ impl<D: DualNum<f64> + Copy> TemperatureOrPressure<D> for Temperature<D> {
// used instead of the explicit unit. Maybe the type is too complicated for the
// compiler?
impl<D: DualNum<f64> + Copy> TemperatureOrPressure<D>
for Quantity<D, SIUnit<N2, N1, P1, Z0, Z0, Z0, Z0>>
for Quantity<D, SIUnit<-2, -1, 1, 0, 0, 0, 0>>
{
type Other = Temperature<D>;
const IDENTIFIER: &'static str = "pressure";
Expand Down
13 changes: 6 additions & 7 deletions crates/feos-core/src/state/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,25 +14,24 @@ use quantity::*;
/// # use quantity::*;
/// # use nalgebra::dvector;
/// # use approx::assert_relative_eq;
/// # use typenum::P3;
/// # fn main() -> FeosResult<()> {
/// // Create a state for given T,V,N
/// let eos = &PengRobinson::new(PengRobinsonParameters::new_simple(&[369.8], &[41.9 * 1e5], &[0.15], &[15.0])?);
/// let state = StateBuilder::new(&eos)
/// .temperature(300.0 * KELVIN)
/// .volume(12.5 * METER.powi::<P3>())
/// .volume(12.5 * METER.powi::<3>())
/// .moles(&(dvector![2.5] * MOL))
/// .build()?;
/// assert_eq!(state.density, 0.2 * MOL / METER.powi::<P3>());
/// assert_eq!(state.density, 0.2 * MOL / METER.powi::<3>());
///
/// // For a pure component, the composition does not need to be specified.
/// let eos = &PengRobinson::new(PengRobinsonParameters::new_simple(&[369.8], &[41.9 * 1e5], &[0.15], &[15.0])?);
/// let state = StateBuilder::new(&eos)
/// .temperature(300.0 * KELVIN)
/// .volume(12.5 * METER.powi::<P3>())
/// .volume(12.5 * METER.powi::<3>())
/// .total_moles(2.5 * MOL)
/// .build()?;
/// assert_eq!(state.density, 0.2 * MOL / METER.powi::<P3>());
/// assert_eq!(state.density, 0.2 * MOL / METER.powi::<3>());
///
/// // The state can be constructed without providing any extensive property.
/// let eos = &PengRobinson::new(
Expand All @@ -45,10 +44,10 @@ use quantity::*;
/// );
/// let state = StateBuilder::new(&eos)
/// .temperature(300.0 * KELVIN)
/// .partial_density(&(dvector![0.2, 0.6] * MOL / METER.powi::<P3>()))
/// .partial_density(&(dvector![0.2, 0.6] * MOL / METER.powi::<3>()))
/// .build()?;
/// assert_relative_eq!(state.molefracs, dvector![0.25, 0.75]);
/// assert_relative_eq!(state.density, 0.8 * MOL / METER.powi::<P3>());
/// assert_relative_eq!(state.density, 0.8 * MOL / METER.powi::<3>());
/// # Ok(())
/// # }
/// ```
Expand Down
4 changes: 3 additions & 1 deletion crates/feos-core/src/state/cache.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use nalgebra::allocator::Allocator;
use nalgebra::{DefaultAllocator, Dim, OVector, Scalar};
use quantity::*;
use std::ops::Sub;
use std::sync::OnceLock;
use typenum::Diff;

type Diff<T1, T2> = <T1 as Sub<T2>>::Output;

#[derive(Clone, Debug)]
#[expect(clippy::type_complexity)]
Expand Down
13 changes: 6 additions & 7 deletions crates/feos-core/src/state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -798,52 +798,51 @@ mod critical_point;
mod tests {
use super::*;
use nalgebra::dvector;
use typenum::P3;

#[test]
fn test_validate() {
let temperature = 298.15 * KELVIN;
let density = 3000.0 * MOL / METER.powi::<P3>();
let density = 3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![0.03, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_ok());
}

#[test]
fn test_negative_temperature() {
let temperature = -298.15 * KELVIN;
let density = 3000.0 * MOL / METER.powi::<P3>();
let density = 3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![0.03, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_err());
}

#[test]
fn test_nan_temperature() {
let temperature = f64::NAN * KELVIN;
let density = 3000.0 * MOL / METER.powi::<P3>();
let density = 3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![0.03, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_err());
}

#[test]
fn test_negative_mole_number() {
let temperature = 298.15 * KELVIN;
let density = 3000.0 * MOL / METER.powi::<P3>();
let density = 3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![-0.03, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_err());
}

#[test]
fn test_nan_mole_number() {
let temperature = 298.15 * KELVIN;
let density = 3000.0 * MOL / METER.powi::<P3>();
let density = 3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![f64::NAN, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_err());
}

#[test]
fn test_negative_density() {
let temperature = 298.15 * KELVIN;
let density = -3000.0 * MOL / METER.powi::<P3>();
let density = -3000.0 * MOL / METER.powi::<3>();
let molefracs = dvector![0.01, 0.02, 0.05];
assert!(validate(temperature, density, &molefracs).is_err());
}
Expand Down
1 change: 0 additions & 1 deletion crates/feos-dft/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ num-traits = { workspace = true }
libm = { workspace = true }
gauss-quad = { workspace = true, optional = true }
petgraph = { workspace = true }
typenum = { workspace = true }

feos-core = { workspace = true }

Expand Down
4 changes: 2 additions & 2 deletions crates/feos-dft/src/adsorption/pore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@ use quantity::{
Temperature, Volume,
};
use rustdct::DctNum;
use typenum::Diff;
use std::ops::Sub;

const POTENTIAL_OFFSET: f64 = 2.0;
const DEFAULT_GRID_POINTS: usize = 2048;

pub type _HenryCoefficient = Diff<_Moles, _Pressure>;
pub type _HenryCoefficient = <_Moles as Sub<_Pressure>>::Output;
pub type HenryCoefficient<T> = Quantity<T, _HenryCoefficient>;

/// Parameters required to specify a 1D pore.
Expand Down
6 changes: 4 additions & 2 deletions crates/feos-dft/src/pdgt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ use quantity::{
Temperature,
};
use std::ops::{Add, AddAssign, Sub};
use typenum::{Diff, P2, Sum};

type Sum<T1, T2> = <T1 as Add<T2>>::Output;
type Diff<T1, T2> = <T1 as Sub<T2>>::Output;

type _InfluenceParameter = Diff<Sum<_MolarEnergy, _Area>, _Density>;
type InfluenceParameter<T> = Quantity<T, _InfluenceParameter>;
Expand Down Expand Up @@ -218,7 +220,7 @@ pub trait PdgtFunctionalProperties: HelmholtzEnergyFunctional {

// calculate interfacial width
let w_temp = integrate_trapezoidal(&rho_r * &*z * z_int, dx);
*w = (24.0 * (w_temp - 0.5 * ze.powi::<P2>())).sqrt();
*w = (24.0 * (w_temp - 0.5 * ze.powi::<2>())).sqrt();

// shift density profile
*z -= ze;
Expand Down
7 changes: 3 additions & 4 deletions crates/feos-dft/src/profile/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ use num_dual::DualNum;
use quantity::{_Volume, DEGREES, Density, Length, Moles, Quantity, Temperature, Volume};
use std::ops::{Add, MulAssign};
use std::sync::Arc;
use typenum::Sum;

mod properties;

Expand Down Expand Up @@ -304,7 +303,7 @@ where
pub fn integrate<S: Data<Elem = f64>, U>(
&self,
profile: &Quantity<ArrayBase<S, D>, U>,
) -> Quantity<f64, Sum<_Volume, U>>
) -> Quantity<f64, <_Volume as Add<U>>::Output>
where
_Volume: Add<U>,
{
Expand All @@ -322,7 +321,7 @@ where
pub fn integrate_comp<S: Data<Elem = f64>, U>(
&self,
profile: &Quantity<ArrayBase<S, D::Larger>, U>,
) -> Quantity<DVector<f64>, Sum<_Volume, U>>
) -> Quantity<DVector<f64>, <_Volume as Add<U>>::Output>
where
_Volume: Add<U>,
{
Expand All @@ -335,7 +334,7 @@ where
pub fn integrate_segments<S: Data<Elem = f64>, U>(
&self,
profile: &Quantity<ArrayBase<S, D::Larger>, U>,
) -> Quantity<DVector<f64>, Sum<_Volume, U>>
) -> Quantity<DVector<f64>, <_Volume as Add<U>>::Output>
where
_Volume: Add<U>,
{
Expand Down
1 change: 0 additions & 1 deletion crates/feos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ serde_json = { workspace = true }
indexmap = { workspace = true }
rayon = { workspace = true, optional = true }
itertools = { workspace = true }
typenum = { workspace = true }

feos-core = { workspace = true }
feos-derive = { workspace = true, optional = true }
Expand Down
3 changes: 1 addition & 2 deletions crates/feos/benches/dft_pore.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use feos::hard_sphere::{FMTFunctional, FMTVersion};
use feos::pcsaft::{PcSaftFunctional, PcSaftParameters};
use nalgebra::dvector;
use quantity::{ANGSTROM, KELVIN, NAV};
use typenum::P3;

fn fmt(c: &mut Criterion) {
let mut group = c.benchmark_group("DFT_pore_fmt");
Expand All @@ -23,7 +22,7 @@ fn fmt(c: &mut Criterion) {
None,
None,
);
let bulk = State::new_pure(&func, KELVIN, 0.75 / NAV / ANGSTROM.powi::<P3>()).unwrap();
let bulk = State::new_pure(&func, KELVIN, 0.75 / NAV / ANGSTROM.powi::<3>()).unwrap();
group.bench_function("liquid", |b| {
b.iter(|| pore.initialize(&bulk, None, None).unwrap().solve(None))
});
Expand Down
3 changes: 1 addition & 2 deletions crates/feos/benches/dual_numbers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ use feos_core::parameter::PureRecord;
use nalgebra::{DVector, Dyn, dvector};
use num_dual::{Dual2_64, Dual3_64, Dual64, DualNum, HyperDual64};
use quantity::*;
use typenum::P3;

/// Helper function to create a state for given parameters.
/// - temperature is 80% of critical temperature,
Expand Down Expand Up @@ -129,7 +128,7 @@ fn methane_co2_pcsaft(c: &mut Criterion) {

// 230 K, 50 bar, x0 = 0.15
let temperature = 230.0 * KELVIN;
let density = 24.16896 * KILO * MOL / METER.powi::<P3>();
let density = 24.16896 * KILO * MOL / METER.powi::<3>();
let volume = 10.0 * MOL / density;
let x = dvector![0.15, 0.85];
let moles = &x * 10.0 * MOL;
Expand Down
5 changes: 2 additions & 3 deletions crates/feos/benches/state_properties.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use feos::core::{Contributions, Residual, State};
use feos::pcsaft::{PcSaft, PcSaftParameters};
use nalgebra::{DVector, dvector};
use quantity::*;
use typenum::P3;

/// Evaluate a property of a state given the EoS, the property to compute,
/// temperature, volume, moles, and the contributions to consider.
Expand Down Expand Up @@ -42,7 +41,7 @@ fn properties_pcsaft(c: &mut Criterion) {
.unwrap();
let eos = PcSaft::new(parameters);
let t = 300.0 * KELVIN;
let density = 71.18 * KILO * MOL / METER.powi::<P3>();
let density = 71.18 * KILO * MOL / METER.powi::<3>();
let v = 100.0 * MOL / density;
let x = dvector![1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
let m = &x * 100.0 * MOL;
Expand Down Expand Up @@ -92,7 +91,7 @@ fn properties_pcsaft_polar(c: &mut Criterion) {
.unwrap();
let eos = PcSaft::new(parameters);
let t = 300.0 * KELVIN;
let density = 71.18 * KILO * MOL / METER.powi::<P3>();
let density = 71.18 * KILO * MOL / METER.powi::<3>();
let v = 100.0 * MOL / density;
let x = dvector![1.0 / 3.0, 1.0 / 3.0, 1.0 / 3.0];
let m = &x * 100.0 * MOL;
Expand Down
Loading