Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Removed parallel impls in estimator
  • Loading branch information
g-bauer committed Oct 17, 2022
commit 5ccff9ca8d0087b56fff8bd69446b9271338735b
2 changes: 1 addition & 1 deletion feos-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ pub fn initialize_global_thread_pool_py(num_threads: usize) -> pyo3::PyResult<()
}

/// Consistent conversions between quantities and reduced properties.
pub trait EosUnit: Unit + Send {
pub trait EosUnit: Unit + Send + Sync {
fn reference_temperature() -> QuantityScalar<Self>;
fn reference_length() -> QuantityScalar<Self>;
fn reference_density() -> QuantityScalar<Self>;
Expand Down
2 changes: 1 addition & 1 deletion feos-dft/src/convolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use transform::*;
/// Helmholtz energy functional.
///
/// Parametrized over data types `T` and dimension of the problem `D`.
pub trait Convolver<T, D: Dimension>: Send {
pub trait Convolver<T, D: Dimension>: Send + Sync {
/// Convolve the profile with the given weight function.
fn convolve(&self, profile: Array<T, D>, weight_function: &WeightFunction<T>) -> Array<T, D>;

Expand Down
2 changes: 1 addition & 1 deletion feos-dft/src/convolver/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl SinCosTransform {
}
}

pub(super) trait FourierTransform<T: DualNum<f64>>: Send {
pub(super) trait FourierTransform<T: DualNum<f64>>: Send + Sync {
fn forward_transform(&self, f_r: ArrayView1<T>, f_k: ArrayViewMut1<T>, scalar: bool);

fn back_transform(&self, f_k: ArrayViewMut1<T>, f_r: ArrayViewMut1<T>, scalar: bool);
Expand Down
2 changes: 1 addition & 1 deletion feos-dft/src/functional.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ pub enum MoleculeShape<'a> {
}

/// A general Helmholtz energy functional.
pub trait HelmholtzEnergyFunctional: Sized + Send {
pub trait HelmholtzEnergyFunctional: Sized + Send + Sync {
/// Return a slice of [FunctionalContribution]s.
fn contributions(&self) -> &[Box<dyn FunctionalContribution>];

Expand Down
2 changes: 1 addition & 1 deletion feos-dft/src/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) const CUTOFF_RADIUS: f64 = 14.0;
/// In the most basic case, the chemical potential is specified in a DFT calculation,
/// for more general systems, this trait provides the possibility to declare additional
/// equations for the calculation of the chemical potential during the iteration.
pub trait DFTSpecification<U, D: Dimension, F>: Send {
pub trait DFTSpecification<U, D: Dimension, F>: Send + Sync {
fn calculate_chemical_potential(
&self,
profile: &DFTProfile<U, D, F>,
Expand Down
31 changes: 1 addition & 30 deletions src/estimator/dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use std::sync::Arc;
///
/// Functionalities in the context of optimizations of
/// parameters of equations of state.
pub trait DataSet<U: EosUnit, E: EquationOfState>: Send {
pub trait DataSet<U: EosUnit, E: EquationOfState>: Send + Sync {
/// Return target quantity.
fn target(&self) -> &QuantityArray1<U>;

Expand Down Expand Up @@ -71,35 +71,6 @@ pub trait DataSet<U: EosUnit, E: EquationOfState>: Send {
.enumerate()
.fold(0.0, |mean, (i, x)| mean + (x.abs() - mean) / (i + 1) as f64))
}

// /// Parallel evaluation of the equation of state for the target quantity for each data point.
// fn par_predict(&self, eos: &Arc<E>) -> Result<QuantityArray1<U>, EstimatorError>
// where
// QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
// {
// self.predict(eos)
// }

// /// Parallel evaluation the cost function.
// fn par_cost(&self, eos: &Arc<E>, loss: Loss) -> Result<Array1<f64>, EstimatorError>
// where
// QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
// {
// let mut cost = self.par_relative_difference(eos)?;
// loss.apply(&mut cost);
// let datapoints = cost.len();
// Ok(cost / datapoints as f64)
// }

// /// Returns the relative difference between the equation of state and the experimental values.
// fn par_relative_difference(&self, eos: &Arc<E>) -> Result<Array1<f64>, EstimatorError>
// where
// QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
// {
// let prediction = &self.par_predict(eos)?;
// let target = self.target();
// Ok(((prediction - target) / target).into_value()?)
// }
}

impl<U: EosUnit, E: EquationOfState> fmt::Display for dyn DataSet<U, E> {
Expand Down
10 changes: 3 additions & 7 deletions src/estimator/diffusion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,13 +64,9 @@ impl<U: EosUnit, E: EquationOfState + EntropyScaling<U>> DataSet<U, E> for Diffu
.unwrap();
let ps = self.pressure.to_reduced(U::reference_pressure()).unwrap();

#[cfg(not(feature = "rayon"))]
let tp_iter = ts.iter().zip(ps.iter());
#[cfg(feature = "rayon")]
let tp_iter = (ts.as_slice().unwrap(), ps.as_slice().unwrap())
.into_par_iter();

let res = tp_iter
let res = ts
.iter()
.zip(ps.iter())
.map(|(&t, &p)| {
State::new_npt(
eos,
Expand Down
27 changes: 0 additions & 27 deletions src/estimator/estimator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,27 +57,11 @@ where
Ok(concatenate(Axis(0), &aview)?)
}

// pub fn par_cost(&self, eos: &Arc<E>) -> Result<Array1<f64>, EstimatorError> {
// let w = arr1(&self.weights) / self.weights.iter().sum::<f64>();
// let predictions = self
// .data
// .iter()
// .enumerate()
// .flat_map(|(i, d)| d.par_cost(eos, self.losses[i]).unwrap() * w[i])
// .collect::<Array1<f64>>();
// Ok(predictions)
// }

/// Returns the properties as computed by the equation of state for each `DataSet`.
pub fn predict(&self, eos: &Arc<E>) -> Result<Vec<QuantityArray1<U>>, EstimatorError> {
self.data.iter().map(|d| d.predict(eos)).collect()
}

// /// Returns the properties as computed by the equation of state for each `DataSet`.
// pub fn par_predict(&self, eos: &Arc<E>) -> Result<Vec<QuantityArray1<U>>, EstimatorError> {
// self.data.iter().map(|d| d.par_predict(eos)).collect()
// }

/// Returns the relative difference for each `DataSet`.
pub fn relative_difference(&self, eos: &Arc<E>) -> Result<Vec<Array1<f64>>, EstimatorError> {
self.data
Expand All @@ -86,17 +70,6 @@ where
.collect()
}

// /// Returns the relative difference for each `DataSet`.
// pub fn par_relative_difference(
// &self,
// eos: &Arc<E>,
// ) -> Result<Vec<Array1<f64>>, EstimatorError> {
// self.data
// .iter()
// .map(|d| d.par_relative_difference(eos))
// .collect()
// }

/// Returns the mean absolute relative difference for each `DataSet`.
pub fn mean_absolute_relative_difference(
&self,
Expand Down
63 changes: 1 addition & 62 deletions src/estimator/liquid_density.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use feos_core::{
DensityInitialization, EosUnit, EquationOfState, MolarWeight, PhaseEquilibrium, SolverOptions,
State,
};
use ndarray::{arr1, Array1};
use ndarray::arr1;
use quantity::{QuantityArray1, QuantityScalar};
#[cfg(feature = "rayon")]
use rayon_::prelude::*;
Expand Down Expand Up @@ -76,36 +76,6 @@ impl<U: EosUnit, E: EquationOfState + MolarWeight<U>> DataSet<U, E> for LiquidDe
.collect())
}

// fn par_predict(&self, eos: &Arc<E>) -> Result<QuantityArray1<U>, EstimatorError> {
// let moles = arr1(&[1.0]) * U::reference_moles();
// let ts = self
// .temperature
// .to_reduced(U::reference_temperature())
// .unwrap();
// let ps = self.pressure.to_reduced(U::reference_pressure()).unwrap();

// let res = (ts.as_slice().unwrap(), ps.as_slice().unwrap())
// .into_par_iter()
// .map(|(&t, &p)| {
// let state = State::new_npt(
// eos,
// t * U::reference_temperature(),
// p * U::reference_pressure(),
// &moles,
// DensityInitialization::Liquid,
// );
// if let Ok(s) = state {
// s.mass_density()
// .to_reduced(U::reference_mass() / U::reference_volume())
// .unwrap()
// } else {
// f64::NAN
// }
// })
// .collect::<Vec<f64>>();
// Ok(Array1::from_vec(res) * U::reference_mass() / U::reference_volume())
// }

fn get_input(&self) -> HashMap<String, QuantityArray1<U>> {
let mut m = HashMap::with_capacity(2);
m.insert("temperature".to_owned(), self.temperature());
Expand Down Expand Up @@ -174,37 +144,6 @@ impl<U: EosUnit, E: EquationOfState + MolarWeight<U>> DataSet<U, E>
.collect())
}

// fn par_predict(&self, eos: &Arc<E>) -> Result<QuantityArray1<U>, EstimatorError>
// where
// QuantityScalar<U>: std::fmt::Display + std::fmt::LowerExp,
// {
// let ts = self
// .temperature
// .to_reduced(U::reference_temperature())
// .unwrap();

// let res = ts
// .into_par_iter()
// .map(|&t| {
// if let Ok(state) = PhaseEquilibrium::pure(
// eos,
// t * U::reference_temperature(),
// None,
// self.solver_options,
// ) {
// state
// .liquid()
// .mass_density()
// .to_reduced(U::reference_mass() / U::reference_volume())
// .unwrap()
// } else {
// f64::NAN
// }
// })
// .collect::<Vec<f64>>();
// Ok(Array1::from_vec(res) * U::reference_mass() / U::reference_volume())
// }

fn get_input(&self) -> HashMap<String, QuantityArray1<U>> {
let mut m = HashMap::with_capacity(2);
m.insert("temperature".to_owned(), self.temperature());
Expand Down
73 changes: 0 additions & 73 deletions src/estimator/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -504,29 +504,6 @@ macro_rules! impl_estimator {
Ok(self.0.cost(&eos.0)?.view().to_pyarray(py))
}

// /// Compute the cost function for each ``DataSet`` in parallel.
// ///
// /// The cost function is:
// /// - The relative difference between prediction and target value,
// /// - to which a loss function is applied,
// /// - and which is weighted according to the number of datapoints,
// /// - and the relative weights as defined in the Estimator object.
// ///
// /// Parameters
// /// ----------
// /// eos : PyEos
// /// The equation of state that is used.
// ///
// /// Returns
// /// -------
// /// numpy.ndarray[Float]
// /// The cost function evaluated for each experimental data point
// /// of each ``DataSet``.
// #[pyo3(text_signature = "($self, eos)")]
// fn par_cost<'py>(&self, eos: &$py_eos, py: Python<'py>) -> PyResult<&'py PyArray1<f64>> {
// Ok(self.0.par_cost(&eos.0)?.view().to_pyarray(py))
// }

/// Return the properties as computed by the
/// equation of state for each `DataSet`.
///
Expand All @@ -548,27 +525,6 @@ macro_rules! impl_estimator {
.collect())
}

// /// Return the properties as computed by the
// /// equation of state for each `DataSet` in parallel.
// ///
// /// Parameters
// /// ----------
// /// eos : PyEos
// /// The equation of state that is used.
// ///
// /// Returns
// /// -------
// /// List[SIArray1]
// #[pyo3(text_signature = "($self, eos)")]
// fn par_predict(&self, eos: &$py_eos) -> PyResult<Vec<PySIArray1>> {
// Ok(self
// .0
// .par_predict(&eos.0)?
// .iter()
// .map(|d| PySIArray1::from(d.clone()))
// .collect())
// }

/// Return the relative difference between experimental data
/// and prediction of the equation of state for each ``DataSet``.
///
Expand Down Expand Up @@ -598,35 +554,6 @@ macro_rules! impl_estimator {
.collect())
}

// /// Return the relative difference between experimental data
// /// and prediction of the equation of state for each ``DataSet``.
// ///
// /// The relative difference is computed as:
// ///
// /// .. math:: \text{Relative Difference} = \frac{x_i^\text{prediction} - x_i^\text{experiment}}{x_i^\text{experiment}}
// ///
// /// Parameters
// /// ----------
// /// eos : PyEos
// /// The equation of state that is used.
// ///
// /// Returns
// /// -------
// /// List[numpy.ndarray[Float]]
// #[pyo3(text_signature = "($self, eos)")]
// fn par_relative_difference<'py>(
// &self,
// eos: &$py_eos,
// py: Python<'py>,
// ) -> PyResult<Vec<&'py PyArray1<f64>>> {
// Ok(self
// .0
// .par_relative_difference(&eos.0)?
// .iter()
// .map(|d| d.view().to_pyarray(py))
// .collect())
// }

/// Return the mean absolute relative difference for each ``DataSet``.
///
/// The mean absolute relative difference is computed as:
Expand Down
9 changes: 3 additions & 6 deletions src/estimator/thermal_conductivity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,9 @@ impl<U: EosUnit, E: EquationOfState + EntropyScaling<U>> DataSet<U, E> for Therm
/ U::reference_temperature()
/ U::reference_length();

#[cfg(not(feature = "rayon"))]
let tp_iter = ts.iter().zip(ps.iter());
#[cfg(feature = "rayon")]
let tp_iter = (ts.as_slice().unwrap(), ps.as_slice().unwrap()).into_par_iter();

let res = tp_iter
let res = ts
.iter()
.zip(ps.iter())
.map(|(&t, &p)| {
State::new_npt(
eos,
Expand Down
Loading