Skip to content

Commit 6b5c56a

Browse files
authored
Remove unit generics (#115)
* removed unit generics from feos-core * removed unit generics from feos-core python and fixed formats * removed unit generics from feos-dft * removed unit generics from feos, fixes in core and dft in python macros * ran cargo fmt
1 parent c6bbd86 commit 6b5c56a

72 files changed

Lines changed: 3260 additions & 3344 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1616
- Export `EosVariant` and `FunctionalVariant` directly in the crate root instead of their own modules. [#62](https://github.com/feos-org/feos/pull/62)
1717
- Changed constructors `VaporPressure::new` and `DataSet.vapor_pressure` (Python) to take a new optional argument `critical_temperature`. [#86](https://github.com/feos-org/feos/pull/86)
1818
- The limitations of the homo gc method for PC-SAFT are enforced more strictly. [#88](https://github.com/feos-org/feos/pull/88)
19+
- Removed generics for units in all structs and traits in favor of static SI units. [#115](https://github.com/feos-org/feos/pull/115)
1920

2021
## [0.3.0] - 2022-09-14
2122
- Major restructuring of the entire `feos` project. All individual models are reunited in the `feos` crate. `feos-core` and `feos-dft` still live as individual crates within the `feos` workspace.

benches/dual_numbers.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use std::sync::Arc;
1818
/// - temperature is 80% of critical temperature,
1919
/// - volume is critical volume,
2020
/// - molefracs (or moles) for equimolar mixture.
21-
fn state_pcsaft(parameters: PcSaftParameters) -> State<SIUnit, PcSaft> {
21+
fn state_pcsaft(parameters: PcSaftParameters) -> State<PcSaft> {
2222
let n = parameters.pure_records.len();
2323
let eos = Arc::new(PcSaft::new(Arc::new(parameters)));
2424
let moles = Array::from_elem(n, 1.0 / n as f64) * 10.0 * MOL;
@@ -36,11 +36,7 @@ where
3636
}
3737

3838
/// Benchmark for evaluation of the Helmholtz energy for different dual number types.
39-
fn bench_dual_numbers<E: EquationOfState>(
40-
c: &mut Criterion,
41-
group_name: &str,
42-
state: State<SIUnit, E>,
43-
) {
39+
fn bench_dual_numbers<E: EquationOfState>(c: &mut Criterion, group_name: &str, state: State<E>) {
4440
let mut group = c.benchmark_group(group_name);
4541
group.bench_function("a_f64", |b| {
4642
b.iter(|| a_res((&state.eos, &state.derive0())))

benches/state_creation.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ fn npt<E: EquationOfState>(
1515
SINumber,
1616
SINumber,
1717
&SIArray1,
18-
DensityInitialization<SIUnit>,
18+
DensityInitialization,
1919
),
2020
) {
2121
State::new_npt(eos, t, p, n, rho0).unwrap();

benches/state_properties.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,11 @@ use ndarray::arr1;
88
use quantity::si::*;
99
use std::sync::Arc;
1010

11-
type S = State<SIUnit, PcSaft>;
11+
type S = State<PcSaft>;
1212

1313
/// Evaluate a property of a state given the EoS, the property to compute,
1414
/// temperature, volume, moles, and the contributions to consider.
15-
fn property<E: EquationOfState, T, F: Fn(&State<SIUnit, E>, Contributions) -> T>(
15+
fn property<E: EquationOfState, T, F: Fn(&State<E>, Contributions) -> T>(
1616
(eos, property, t, v, n, contributions): (
1717
&Arc<E>,
1818
F,
@@ -28,7 +28,7 @@ fn property<E: EquationOfState, T, F: Fn(&State<SIUnit, E>, Contributions) -> T>
2828

2929
/// Evaluate a property with of a state given the EoS, the property to compute,
3030
/// temperature, volume, moles.
31-
fn property_no_contributions<E: EquationOfState, T, F: Fn(&State<SIUnit, E>) -> T>(
31+
fn property_no_contributions<E: EquationOfState, T, F: Fn(&State<E>) -> T>(
3232
(eos, property, t, v, n): (&Arc<E>, F, SINumber, SINumber, &SIArray1),
3333
) -> T {
3434
let state = State::new_nvt(eos, t, v, n).unwrap();

feos-core/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
- Added `StateVec::moles` getter. [#113](https://github.com/feos-org/feos/pull/113)
1111
- Added public constructors `PhaseDiagram::new` and `StateVec::new` that allow the creation of the respective structs from a list of `PhaseEquilibrium`s or `State`s in Rust and Python. [#113](https://github.com/feos-org/feos/pull/113)
1212
- Added new variant `EosError::Error` which allows dispatching generic errors that are not covered by the existing variants. [#98](https://github.com/feos-org/feos/pull/98)
13+
- Removed generics for units in all structs and traits in favor of static SI units. [#115](https://github.com/feos-org/feos/pull/115)
1314

1415
### Changed
1516
- Added `Sync` and `Send` as supertraits to `EquationOfState`. [#57](https://github.com/feos-org/feos/pull/57)

feos-core/src/cubic.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::state::StateHD;
1414
use crate::MolarWeight;
1515
use ndarray::{Array1, Array2};
1616
use num_dual::DualNum;
17-
use quantity::si::{SIArray1, SIUnit};
17+
use quantity::si::SIArray1;
1818
use serde::{Deserialize, Serialize};
1919
use std::f64::consts::SQRT_2;
2020
use std::fmt;
@@ -255,7 +255,7 @@ impl EquationOfState for PengRobinson {
255255
}
256256
}
257257

258-
impl MolarWeight<SIUnit> for PengRobinson {
258+
impl MolarWeight for PengRobinson {
259259
fn molar_weight(&self) -> SIArray1 {
260260
self.parameters.molarweight.clone() * GRAM / MOL
261261
}

feos-core/src/density_iteration.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2,26 +2,26 @@ use crate::equation_of_state::EquationOfState;
22
use crate::errors::{EosError, EosResult};
33
use crate::state::State;
44
use crate::EosUnit;
5-
use quantity::{QuantityArray1, QuantityScalar};
5+
use quantity::si::{SIArray1, SINumber, SIUnit};
66
use std::sync::Arc;
77

8-
pub fn density_iteration<U: EosUnit, E: EquationOfState>(
8+
pub fn density_iteration<E: EquationOfState>(
99
eos: &Arc<E>,
10-
temperature: QuantityScalar<U>,
11-
pressure: QuantityScalar<U>,
12-
moles: &QuantityArray1<U>,
13-
initial_density: QuantityScalar<U>,
14-
) -> EosResult<State<U, E>> {
10+
temperature: SINumber,
11+
pressure: SINumber,
12+
moles: &SIArray1,
13+
initial_density: SINumber,
14+
) -> EosResult<State<E>> {
1515
let maxdensity = eos.max_density(Some(moles))?;
1616
let (abstol, reltol) = (1e-12, 1e-14);
1717
let n = moles.sum();
1818

1919
let mut rho = initial_density;
20-
if rho <= 0.0 * U::reference_density() {
20+
if rho <= 0.0 * SIUnit::reference_density() {
2121
return Err(EosError::InvalidState(
2222
String::from("density iteration"),
2323
String::from("density"),
24-
rho.to_reduced(U::reference_density())?,
24+
rho.to_reduced(SIUnit::reference_density())?,
2525
));
2626
}
2727

@@ -117,7 +117,7 @@ pub fn density_iteration<U: EosUnit, E: EquationOfState>(
117117
} else {
118118
rho = (rho + initial_density) * 0.5;
119119
if (rho - initial_density)
120-
.to_reduced(U::reference_density())?
120+
.to_reduced(SIUnit::reference_density())?
121121
.abs()
122122
< 1e-8
123123
{
@@ -128,8 +128,11 @@ pub fn density_iteration<U: EosUnit, E: EquationOfState>(
128128
}
129129
// Newton step
130130
rho += delta_rho;
131-
if error.to_reduced(U::reference_pressure())?.abs()
132-
< f64::max(abstol, (rho * reltol).to_reduced(U::reference_density())?)
131+
if error.to_reduced(SIUnit::reference_pressure())?.abs()
132+
< f64::max(
133+
abstol,
134+
(rho * reltol).to_reduced(SIUnit::reference_density())?,
135+
)
133136
{
134137
break 'iteration;
135138
}
@@ -141,24 +144,24 @@ pub fn density_iteration<U: EosUnit, E: EquationOfState>(
141144
}
142145
}
143146

144-
fn pressure_spinodal<U: EosUnit, E: EquationOfState>(
147+
fn pressure_spinodal<E: EquationOfState>(
145148
eos: &Arc<E>,
146-
temperature: QuantityScalar<U>,
147-
rho_init: QuantityScalar<U>,
148-
moles: &QuantityArray1<U>,
149-
) -> EosResult<[QuantityScalar<U>; 2]> {
149+
temperature: SINumber,
150+
rho_init: SINumber,
151+
moles: &SIArray1,
152+
) -> EosResult<[SINumber; 2]> {
150153
let maxiter = 30;
151154
let abstol = 1e-8;
152155

153156
let maxdensity = eos.max_density(Some(moles))?;
154157
let n = moles.sum();
155158
let mut rho = rho_init;
156159

157-
if rho <= 0.0 * U::reference_density() {
160+
if rho <= 0.0 * SIUnit::reference_density() {
158161
return Err(EosError::InvalidState(
159162
String::from("pressure spinodal"),
160163
String::from("density"),
161-
rho.to_reduced(U::reference_density())?,
164+
rho.to_reduced(SIUnit::reference_density())?,
162165
));
163166
}
164167

@@ -174,7 +177,7 @@ fn pressure_spinodal<U: EosUnit, E: EquationOfState>(
174177
rho += delta_rho;
175178

176179
if dpdrho
177-
.to_reduced(U::reference_pressure() / U::reference_density())?
180+
.to_reduced(SIUnit::reference_pressure() / SIUnit::reference_density())?
178181
.abs()
179182
< abstol
180183
{

feos-core/src/equation_of_state.rs

Lines changed: 45 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use num_dual::{
66
Dual, Dual2_64, Dual3, Dual3_64, Dual64, DualNum, DualVec64, HyperDual, HyperDual64,
77
};
88
use num_traits::{One, Zero};
9-
use quantity::{QuantityArray1, QuantityScalar};
9+
use quantity::si::{SIArray1, SINumber, SIUnit};
1010
use std::fmt;
1111

1212
/// Individual Helmholtz energy contribution that can
@@ -149,8 +149,8 @@ impl fmt::Display for DefaultIdealGasContribution {
149149
///
150150
/// The trait is required to be able to calculate (mass)
151151
/// specific properties.
152-
pub trait MolarWeight<U: EosUnit> {
153-
fn molar_weight(&self) -> QuantityArray1<U>;
152+
pub trait MolarWeight {
153+
fn molar_weight(&self) -> SIArray1;
154154
}
155155

156156
/// A general equation of state.
@@ -219,15 +219,12 @@ pub trait EquationOfState: Send + Sync {
219219
/// of components of the equation of state. For a pure component, however,
220220
/// no moles need to be provided. In that case, it is set to the constant
221221
/// reference value.
222-
fn validate_moles<U: EosUnit>(
223-
&self,
224-
moles: Option<&QuantityArray1<U>>,
225-
) -> EosResult<QuantityArray1<U>> {
222+
fn validate_moles(&self, moles: Option<&SIArray1>) -> EosResult<SIArray1> {
226223
let l = moles.map_or(1, |m| m.len());
227224
if self.components() == l {
228225
match moles {
229226
Some(m) => Ok(m.to_owned()),
230-
None => Ok(Array::ones(1) * U::reference_moles()),
227+
None => Ok(Array::ones(1) * SIUnit::reference_moles()),
231228
}
232229
} else {
233230
Err(EosError::IncompatibleComponents(self.components(), l))
@@ -240,105 +237,102 @@ pub trait EquationOfState: Send + Sync {
240237
/// equilibria and other iterations. It is not explicitly meant to
241238
/// be a mathematical limit for the density (if those exist in the
242239
/// equation of state anyways).
243-
fn max_density<U: EosUnit>(
244-
&self,
245-
moles: Option<&QuantityArray1<U>>,
246-
) -> EosResult<QuantityScalar<U>> {
240+
fn max_density(&self, moles: Option<&SIArray1>) -> EosResult<SINumber> {
247241
let mr = self
248242
.validate_moles(moles)?
249-
.to_reduced(U::reference_moles())?;
250-
Ok(self.compute_max_density(&mr) * U::reference_density())
243+
.to_reduced(SIUnit::reference_moles())?;
244+
Ok(self.compute_max_density(&mr) * SIUnit::reference_density())
251245
}
252246

253247
/// Calculate the second virial coefficient $B(T)$
254-
fn second_virial_coefficient<U: EosUnit>(
248+
fn second_virial_coefficient(
255249
&self,
256-
temperature: QuantityScalar<U>,
257-
moles: Option<&QuantityArray1<U>>,
258-
) -> EosResult<QuantityScalar<U>> {
250+
temperature: SINumber,
251+
moles: Option<&SIArray1>,
252+
) -> EosResult<SINumber> {
259253
let mr = self.validate_moles(moles)?;
260254
let x = mr.to_reduced(mr.sum())?;
261255
let mut rho = HyperDual64::zero();
262256
rho.eps1[0] = 1.0;
263257
rho.eps2[0] = 1.0;
264-
let t = HyperDual64::from(temperature.to_reduced(U::reference_temperature())?);
258+
let t = HyperDual64::from(temperature.to_reduced(SIUnit::reference_temperature())?);
265259
let s = StateHD::new_virial(t, rho, x);
266-
Ok(self.evaluate_residual(&s).eps1eps2[(0, 0)] * 0.5 / U::reference_density())
260+
Ok(self.evaluate_residual(&s).eps1eps2[(0, 0)] * 0.5 / SIUnit::reference_density())
267261
}
268262

269263
/// Calculate the third virial coefficient $C(T)$
270-
fn third_virial_coefficient<U: EosUnit>(
264+
fn third_virial_coefficient(
271265
&self,
272-
temperature: QuantityScalar<U>,
273-
moles: Option<&QuantityArray1<U>>,
274-
) -> EosResult<QuantityScalar<U>> {
266+
temperature: SINumber,
267+
moles: Option<&SIArray1>,
268+
) -> EosResult<SINumber> {
275269
let mr = self.validate_moles(moles)?;
276270
let x = mr.to_reduced(mr.sum())?;
277271
let rho = Dual3_64::zero().derive();
278-
let t = Dual3_64::from(temperature.to_reduced(U::reference_temperature())?);
272+
let t = Dual3_64::from(temperature.to_reduced(SIUnit::reference_temperature())?);
279273
let s = StateHD::new_virial(t, rho, x);
280-
Ok(self.evaluate_residual(&s).v3 / 3.0 / U::reference_density().powi(2))
274+
Ok(self.evaluate_residual(&s).v3 / 3.0 / SIUnit::reference_density().powi(2))
281275
}
282276

283277
/// Calculate the temperature derivative of the second virial coefficient $B'(T)$
284-
fn second_virial_coefficient_temperature_derivative<U: EosUnit>(
278+
fn second_virial_coefficient_temperature_derivative(
285279
&self,
286-
temperature: QuantityScalar<U>,
287-
moles: Option<&QuantityArray1<U>>,
288-
) -> EosResult<QuantityScalar<U>> {
280+
temperature: SINumber,
281+
moles: Option<&SIArray1>,
282+
) -> EosResult<SINumber> {
289283
let mr = self.validate_moles(moles)?;
290284
let x = mr.to_reduced(mr.sum())?;
291285
let mut rho = HyperDual::zero();
292286
rho.eps1[0] = Dual64::one();
293287
rho.eps2[0] = Dual64::one();
294288
let t = HyperDual::from_re(
295-
Dual64::from(temperature.to_reduced(U::reference_temperature())?).derive(),
289+
Dual64::from(temperature.to_reduced(SIUnit::reference_temperature())?).derive(),
296290
);
297291
let s = StateHD::new_virial(t, rho, x);
298292
Ok(self.evaluate_residual(&s).eps1eps2[(0, 0)].eps[0] * 0.5
299-
/ (U::reference_density() * U::reference_temperature()))
293+
/ (SIUnit::reference_density() * SIUnit::reference_temperature()))
300294
}
301295

302296
/// Calculate the temperature derivative of the third virial coefficient $C'(T)$
303-
fn third_virial_coefficient_temperature_derivative<U: EosUnit>(
297+
fn third_virial_coefficient_temperature_derivative(
304298
&self,
305-
temperature: QuantityScalar<U>,
306-
moles: Option<&QuantityArray1<U>>,
307-
) -> EosResult<QuantityScalar<U>> {
299+
temperature: SINumber,
300+
moles: Option<&SIArray1>,
301+
) -> EosResult<SINumber> {
308302
let mr = self.validate_moles(moles)?;
309303
let x = mr.to_reduced(mr.sum())?;
310304
let rho = Dual3::zero().derive();
311305
let t = Dual3::from_re(
312-
Dual64::from(temperature.to_reduced(U::reference_temperature())?).derive(),
306+
Dual64::from(temperature.to_reduced(SIUnit::reference_temperature())?).derive(),
313307
);
314308
let s = StateHD::new_virial(t, rho, x);
315309
Ok(self.evaluate_residual(&s).v3.eps[0]
316310
/ 3.0
317-
/ (U::reference_density().powi(2) * U::reference_temperature()))
311+
/ (SIUnit::reference_density().powi(2) * SIUnit::reference_temperature()))
318312
}
319313
}
320314

321315
/// Reference values and residual entropy correlations for entropy scaling.
322-
pub trait EntropyScaling<U: EosUnit> {
316+
pub trait EntropyScaling {
323317
fn viscosity_reference(
324318
&self,
325-
temperature: QuantityScalar<U>,
326-
volume: QuantityScalar<U>,
327-
moles: &QuantityArray1<U>,
328-
) -> EosResult<QuantityScalar<U>>;
319+
temperature: SINumber,
320+
volume: SINumber,
321+
moles: &SIArray1,
322+
) -> EosResult<SINumber>;
329323
fn viscosity_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64>;
330324
fn diffusion_reference(
331325
&self,
332-
temperature: QuantityScalar<U>,
333-
volume: QuantityScalar<U>,
334-
moles: &QuantityArray1<U>,
335-
) -> EosResult<QuantityScalar<U>>;
326+
temperature: SINumber,
327+
volume: SINumber,
328+
moles: &SIArray1,
329+
) -> EosResult<SINumber>;
336330
fn diffusion_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64>;
337331
fn thermal_conductivity_reference(
338332
&self,
339-
temperature: QuantityScalar<U>,
340-
volume: QuantityScalar<U>,
341-
moles: &QuantityArray1<U>,
342-
) -> EosResult<QuantityScalar<U>>;
333+
temperature: SINumber,
334+
volume: SINumber,
335+
moles: &SIArray1,
336+
) -> EosResult<SINumber>;
343337
fn thermal_conductivity_correlation(&self, s_res: f64, x: &Array1<f64>) -> EosResult<f64>;
344338
}

0 commit comments

Comments
 (0)