Skip to content

Commit 6f5d9c6

Browse files
g-bauerprehner
andauthored
Add Sync + Send to EquationOfState trait bounds (feos-org#57)
* add sync + send to EquationOfState trait bounds, change Rc to Arc * Made State sync + send, added parallel execution to pure PhaseDiagram constructor * Added parallelization for estimator * Removed splitting in chunks from estimator. Added impls for all datasets except binary properties. * Updated models * make all DFT struct Sync + Send and remove unsendable attribute * more changes, added rayon feature * Removed parallel impls in estimator * Remove unused impls; updated Python interface * Remove surface tension test from estimator * fixed feature cfg * remove function for global thread pool * added rayon error as variant of EoSError, changed Cargo.toml feature name and ran formatter Co-authored-by: Philipp Rehner <prehner@ethz.ch>
1 parent 9b931ce commit 6f5d9c6

105 files changed

Lines changed: 1707 additions & 1559 deletions

File tree

Some content is hidden

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

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ serde = "1.0"
3737
serde_json = "1.0"
3838
lazy_static = { version = "1.4", optional = true }
3939
indexmap = "1.8"
40+
rayon = { version = "1.5", optional = true }
4041

4142
[dependencies.pyo3]
4243
version = "0.16"
@@ -55,5 +56,6 @@ pcsaft = ["association"]
5556
gc_pcsaft = ["association"]
5657
uvtheory = ["lazy_static"]
5758
pets = []
58-
python = ["pyo3", "numpy", "feos-core/python", "feos-dft?/python"]
59+
rayon = ["dep:rayon", "ndarray/rayon", "feos-core/rayon"]
60+
python = ["pyo3", "numpy", "feos-core/python", "feos-dft?/python", "rayon"]
5961
all_models = ["dft", "estimator", "pcsaft", "gc_pcsaft", "uvtheory", "pets"]

examples/pcsaft_phase_diagram.ipynb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@
490490
"name": "python",
491491
"nbconvert_exporter": "python",
492492
"pygments_lexer": "ipython3",
493-
"version": "3.9.7"
493+
"version": "3.9.12"
494494
}
495495
},
496496
"nbformat": 4,

feos-core/Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,12 @@ indexmap = "1.8"
2828
conv = "0.3"
2929
numpy = { version = "0.16", optional = true }
3030
pyo3 = { version = "0.16", optional = true }
31+
rayon = { version = "1.5", optional = true }
3132

3233
[dev-dependencies]
3334
approx = "0.4"
3435

3536
[features]
3637
default = []
37-
python = ["pyo3", "numpy", "quantity/python", "num-dual/python"]
38+
rayon = ["dep:rayon", "ndarray/rayon"]
39+
python = ["pyo3", "numpy", "quantity/python", "num-dual/python", "rayon"]

feos-core/src/cubic.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use quantity::si::{SIArray1, SIUnit};
1818
use serde::{Deserialize, Serialize};
1919
use std::f64::consts::SQRT_2;
2020
use std::fmt;
21-
use std::rc::Rc;
21+
use std::sync::Arc;
2222

2323
const KB_A3: f64 = 13806490.0;
2424

@@ -167,7 +167,7 @@ impl Parameter for PengRobinsonParameters {
167167
}
168168

169169
struct PengRobinsonContribution {
170-
parameters: Rc<PengRobinsonParameters>,
170+
parameters: Arc<PengRobinsonParameters>,
171171
}
172172

173173
impl<D: DualNum<f64>> HelmholtzEnergyDual<D> for PengRobinsonContribution {
@@ -206,7 +206,7 @@ impl fmt::Display for PengRobinsonContribution {
206206
/// A simple version of the Peng-Robinson equation of state.
207207
pub struct PengRobinson {
208208
/// Parameters
209-
parameters: Rc<PengRobinsonParameters>,
209+
parameters: Arc<PengRobinsonParameters>,
210210
/// Ideal gas contributions to the Helmholtz energy
211211
ideal_gas: Joback,
212212
/// Non-ideal contributions to the Helmholtz energy
@@ -215,7 +215,7 @@ pub struct PengRobinson {
215215

216216
impl PengRobinson {
217217
/// Create a new equation of state from a set of parameters.
218-
pub fn new(parameters: Rc<PengRobinsonParameters>) -> Self {
218+
pub fn new(parameters: Arc<PengRobinsonParameters>) -> Self {
219219
let ideal_gas = parameters.joback_records.as_ref().map_or_else(
220220
|| Joback::default(parameters.tc.len()),
221221
|j| Joback::new(j.clone()),
@@ -238,7 +238,7 @@ impl EquationOfState for PengRobinson {
238238
}
239239

240240
fn subset(&self, component_list: &[usize]) -> Self {
241-
Self::new(Rc::new(self.parameters.subset(component_list)))
241+
Self::new(Arc::new(self.parameters.subset(component_list)))
242242
}
243243

244244
fn compute_max_density(&self, moles: &Array1<f64>) -> f64 {
@@ -270,7 +270,7 @@ mod tests {
270270
use crate::{EosResult, Verbosity};
271271
use approx::*;
272272
use quantity::si::*;
273-
use std::rc::Rc;
273+
use std::sync::Arc;
274274

275275
fn pure_record_vec() -> Vec<PureRecord<PengRobinsonRecord, JobackRecord>> {
276276
let records = r#"[
@@ -317,7 +317,7 @@ mod tests {
317317
let tc = propane.model_record.tc;
318318
let pc = propane.model_record.pc;
319319
let parameters = PengRobinsonParameters::from_records(vec![propane], Array2::zeros((1, 1)));
320-
let pr = Rc::new(PengRobinson::new(Rc::new(parameters)));
320+
let pr = Arc::new(PengRobinson::new(Arc::new(parameters)));
321321
let options = SolverOptions::new().verbosity(Verbosity::Iter);
322322
let cp = State::critical_point(&pr, None, None, options)?;
323323
println!("{} {}", cp.temperature, cp.pressure(Contributions::Total));

feos-core/src/density_iteration.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@ use crate::errors::{EosError, EosResult};
33
use crate::state::State;
44
use crate::EosUnit;
55
use quantity::{QuantityArray1, QuantityScalar};
6-
use std::rc::Rc;
6+
use std::sync::Arc;
77

88
pub fn density_iteration<U: EosUnit, E: EquationOfState>(
9-
eos: &Rc<E>,
9+
eos: &Arc<E>,
1010
temperature: QuantityScalar<U>,
1111
pressure: QuantityScalar<U>,
1212
moles: &QuantityArray1<U>,
@@ -142,7 +142,7 @@ pub fn density_iteration<U: EosUnit, E: EquationOfState>(
142142
}
143143

144144
fn pressure_spinodal<U: EosUnit, E: EquationOfState>(
145-
eos: &Rc<E>,
145+
eos: &Arc<E>,
146146
temperature: QuantityScalar<U>,
147147
rho_init: QuantityScalar<U>,
148148
moles: &QuantityArray1<U>,

feos-core/src/equation_of_state.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ pub trait HelmholtzEnergy:
3636
+ HelmholtzEnergyDual<Dual3<DualVec64<2>, f64>>
3737
+ HelmholtzEnergyDual<Dual3<DualVec64<3>, f64>>
3838
+ fmt::Display
39+
+ Send
40+
+ Sync
3941
{
4042
}
4143

@@ -52,6 +54,8 @@ impl<T> HelmholtzEnergy for T where
5254
+ HelmholtzEnergyDual<Dual3<DualVec64<2>, f64>>
5355
+ HelmholtzEnergyDual<Dual3<DualVec64<3>, f64>>
5456
+ fmt::Display
57+
+ Send
58+
+ Sync
5559
{
5660
}
5761

@@ -144,7 +148,7 @@ pub trait MolarWeight<U: EosUnit> {
144148
}
145149

146150
/// A general equation of state.
147-
pub trait EquationOfState {
151+
pub trait EquationOfState: Send + Sync {
148152
/// Return the number of components of the equation of state.
149153
fn components(&self) -> usize;
150154

feos-core/src/errors.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ pub enum EosError {
3030
ParameterError(#[from] ParameterError),
3131
#[error(transparent)]
3232
LinAlgError(#[from] LinAlgError),
33+
#[cfg(feature = "rayon")]
34+
#[error(transparent)]
35+
RayonError(#[from] rayon::ThreadPoolBuildError),
3336
}
3437

3538
/// Convenience type for `Result<T, EosError>`.

feos-core/src/joback.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod tests {
165165
use approx::assert_relative_eq;
166166
use ndarray::arr1;
167167
use quantity::si::*;
168-
use std::rc::Rc;
168+
use std::sync::Arc;
169169

170170
use super::*;
171171

@@ -259,7 +259,7 @@ mod tests {
259259
);
260260
assert_relative_eq!(jr.e, 0.0);
261261

262-
let eos = Rc::new(Joback::new(vec![jr]));
262+
let eos = Arc::new(Joback::new(vec![jr]));
263263
let state = State::new_nvt(
264264
&eos,
265265
1000.0 * KELVIN,
@@ -281,7 +281,7 @@ mod tests {
281281
fn c_p_comparison() -> EosResult<()> {
282282
let record1 = JobackRecord::new(1.0, 0.2, 0.03, 0.004, 0.005);
283283
let record2 = JobackRecord::new(-5.0, 0.4, 0.03, 0.002, 0.001);
284-
let joback = Rc::new(Joback::new(vec![record1, record2]));
284+
let joback = Arc::new(Joback::new(vec![record1, record2]));
285285
let temperature = 300.0 * KELVIN;
286286
let volume = METER.powi(3);
287287
let moles = arr1(&[1.0, 3.0]) * MOL;

feos-core/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub use state::{Contributions, DensityInitialization, State, StateBuilder, State
4848
pub mod python;
4949

5050
/// Consistent conversions between quantities and reduced properties.
51-
pub trait EosUnit: Unit {
51+
pub trait EosUnit: Unit + Send + Sync {
5252
fn reference_temperature() -> QuantityScalar<Self>;
5353
fn reference_length() -> QuantityScalar<Self>;
5454
fn reference_density() -> QuantityScalar<Self>;

feos-core/src/phase_equilibria/bubble_dew.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ndarray::*;
1010
use num_dual::linalg::{norm, LU};
1111
use quantity::{QuantityArray1, QuantityScalar};
1212
use std::convert::TryFrom;
13-
use std::rc::Rc;
13+
use std::sync::Arc;
1414

1515
const MAX_ITER_INNER: usize = 5;
1616
const TOL_INNER: f64 = 1e-9;
@@ -61,7 +61,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
6161
/// Calculate a phase equilibrium for a given temperature
6262
/// or pressure and composition of the liquid phase.
6363
pub fn bubble_point(
64-
eos: &Rc<E>,
64+
eos: &Arc<E>,
6565
temperature_or_pressure: QuantityScalar<U>,
6666
liquid_molefracs: &Array1<f64>,
6767
tp_init: Option<QuantityScalar<U>>,
@@ -85,7 +85,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
8585
/// Calculate a phase equilibrium for a given temperature
8686
/// or pressure and composition of the vapor phase.
8787
pub fn dew_point(
88-
eos: &Rc<E>,
88+
eos: &Arc<E>,
8989
temperature_or_pressure: QuantityScalar<U>,
9090
vapor_molefracs: &Array1<f64>,
9191
tp_init: Option<QuantityScalar<U>>,
@@ -107,7 +107,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
107107
}
108108

109109
pub(super) fn bubble_dew_point(
110-
eos: &Rc<E>,
110+
eos: &Arc<E>,
111111
tp_spec: TPSpec<U>,
112112
tp_init: Option<QuantityScalar<U>>,
113113
molefracs_spec: &Array1<f64>,
@@ -183,7 +183,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
183183
}
184184

185185
fn iterate_bubble_dew(
186-
eos: &Rc<E>,
186+
eos: &Arc<E>,
187187
tp_spec: TPSpec<U>,
188188
tp_init: QuantityScalar<U>,
189189
molefracs_spec: &Array1<f64>,
@@ -204,7 +204,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
204204
}
205205

206206
fn starting_pressure_ideal_gas(
207-
eos: &Rc<E>,
207+
eos: &Arc<E>,
208208
temperature: QuantityScalar<U>,
209209
molefracs_spec: &Array1<f64>,
210210
bubble: bool,
@@ -220,7 +220,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
220220
}
221221

222222
pub(super) fn starting_pressure_ideal_gas_bubble(
223-
eos: &Rc<E>,
223+
eos: &Arc<E>,
224224
temperature: QuantityScalar<U>,
225225
liquid_molefracs: &Array1<f64>,
226226
) -> EosResult<(QuantityScalar<U>, Array1<f64>)> {
@@ -239,7 +239,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
239239
}
240240

241241
fn starting_pressure_ideal_gas_dew(
242-
eos: &Rc<E>,
242+
eos: &Arc<E>,
243243
temperature: QuantityScalar<U>,
244244
vapor_molefracs: &Array1<f64>,
245245
) -> EosResult<(QuantityScalar<U>, Array1<f64>)>
@@ -274,7 +274,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
274274
}
275275

276276
pub(super) fn starting_pressure_spinodal(
277-
eos: &Rc<E>,
277+
eos: &Arc<E>,
278278
temperature: QuantityScalar<U>,
279279
molefracs: &Array1<f64>,
280280
) -> EosResult<QuantityScalar<U>>
@@ -290,7 +290,7 @@ impl<U: EosUnit, E: EquationOfState> PhaseEquilibrium<U, E, 2> {
290290
}
291291

292292
fn starting_x2_bubble<U: EosUnit, E: EquationOfState>(
293-
eos: &Rc<E>,
293+
eos: &Arc<E>,
294294
temperature: QuantityScalar<U>,
295295
pressure: QuantityScalar<U>,
296296
liquid_molefracs: &Array1<f64>,
@@ -318,7 +318,7 @@ fn starting_x2_bubble<U: EosUnit, E: EquationOfState>(
318318
}
319319

320320
fn starting_x2_dew<U: EosUnit, E: EquationOfState>(
321-
eos: &Rc<E>,
321+
eos: &Arc<E>,
322322
temperature: QuantityScalar<U>,
323323
pressure: QuantityScalar<U>,
324324
vapor_molefracs: &Array1<f64>,

0 commit comments

Comments
 (0)