From cfb44025f19df6b72fe3594620af4c4d57c3c4ee Mon Sep 17 00:00:00 2001 From: Philipp Rehner Date: Fri, 11 Mar 2022 11:36:07 +0100 Subject: [PATCH] Move creation of python module to build_wheel crate --- Cargo.toml | 2 - build_wheel/Cargo.toml | 4 ++ {src/python => build_wheel/src}/dft.rs | 13 ++++--- {src/python => build_wheel/src}/eos.rs | 15 +------ build_wheel/src/lib.rs | 50 ++++++++++++++++++++++-- src/{python/parameters.rs => python.rs} | 11 ++++++ src/python/mod.rs | 52 ------------------------- 7 files changed, 71 insertions(+), 76 deletions(-) rename {src/python => build_wheel/src}/dft.rs (90%) rename {src/python => build_wheel/src}/eos.rs (87%) rename src/{python/parameters.rs => python.rs} (93%) delete mode 100644 src/python/mod.rs diff --git a/Cargo.toml b/Cargo.toml index fc9d8d9..e310f24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -17,14 +17,12 @@ quantity = "0.5" feos-core = { git = "https://github.com/feos-org/feos-core" } feos-dft = { git = "https://github.com/feos-org/feos-dft", branch = "v0.2.0" } num-dual = "0.5" -num = "0.4" num-traits = "0.2" ndarray = { version = "0.15", features=["approx"] } serde = { version = "1.0", features = ["derive"] } serde_json = "1.0" indexmap = "1.8" lazy_static = "1.4" -thiserror = "1.0" numpy = { version = "0.16", optional = true } pyo3 = { version = "0.16", optional = true } diff --git a/build_wheel/Cargo.toml b/build_wheel/Cargo.toml index 0150627..443a5e6 100644 --- a/build_wheel/Cargo.toml +++ b/build_wheel/Cargo.toml @@ -9,5 +9,9 @@ edition = "2018" crate-type = ["cdylib"] [dependencies] +quantity = "0.5" +feos-core = { git = "https://github.com/feos-org/feos-core" } +feos-dft = { git = "https://github.com/feos-org/feos-dft", branch = "v0.2.0" } feos-pcsaft = { path = "..", features = ["python"] } pyo3 = { version = "0.16", features = ["extension-module", "abi3", "abi3-py37"] } +numpy = "0.16" diff --git a/src/python/dft.rs b/build_wheel/src/dft.rs similarity index 90% rename from src/python/dft.rs rename to build_wheel/src/dft.rs index d42a705..2ca56a3 100644 --- a/src/python/dft.rs +++ b/build_wheel/src/dft.rs @@ -1,14 +1,15 @@ -use super::parameters::*; -use crate::dft::PcSaftFunctional; use feos_core::utils::{ DataSet, EquilibriumLiquidDensity, Estimator, LiquidDensity, VaporPressure, }; use feos_core::*; use feos_dft::adsorption::*; +use feos_dft::fundamental_measure_theory::FMTVersion; use feos_dft::interface::*; use feos_dft::python::*; use feos_dft::solvation::*; use feos_dft::*; +use feos_pcsaft::python::*; +use feos_pcsaft::PcSaftFunctional; use numpy::*; use pyo3::exceptions::PyValueError; use pyo3::prelude::*; @@ -54,10 +55,10 @@ impl PyPcSaftFunctional { /// PcSaftFunctional #[staticmethod] #[pyo3(text_signature = "(parameters, fmt_version)")] - fn new_full(parameters: PyPcSaftParameters, fmt_version: PyFMTVersion) -> Self { + fn new_full(parameters: PyPcSaftParameters, fmt_version: FMTVersion) -> Self { Self(Rc::new(PcSaftFunctional::new_full( parameters.0, - fmt_version.0, + fmt_version, ))) } } @@ -88,7 +89,7 @@ pub fn dft(py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; - m.add_class::()?; + m.add_class::()?; m.add_class::()?; m.add_class::()?; m.add_class::()?; @@ -98,7 +99,7 @@ pub fn dft(py: Python<'_>, m: &PyModule) -> PyResult<()> { m.add_class::()?; m.add_class::()?; m.add_class::()?; - m.add_class::()?; + m.add_class::()?; let utils = PyModule::new(py, "utils")?; utils.add_class::()?; diff --git a/src/python/eos.rs b/build_wheel/src/eos.rs similarity index 87% rename from src/python/eos.rs rename to build_wheel/src/eos.rs index f3b6c5e..8578997 100644 --- a/src/python/eos.rs +++ b/build_wheel/src/eos.rs @@ -1,10 +1,9 @@ -use super::parameters::PyPcSaftParameters; -use crate::eos::polar::DQVariants; -use crate::eos::{PcSaft, PcSaftOptions}; use feos_core::utils::{ DataSet, EquilibriumLiquidDensity, Estimator, LiquidDensity, VaporPressure, }; use feos_core::*; +use feos_pcsaft::python::PyPcSaftParameters; +use feos_pcsaft::{PcSaft, PcSaftOptions}; use numpy::convert::ToPyArray; use numpy::{PyArray1, PyArray2}; use pyo3::exceptions::PyValueError; @@ -14,16 +13,6 @@ use quantity::si::*; use std::collections::HashMap; use std::rc::Rc; -impl From<&str> for DQVariants { - fn from(str: &str) -> Self { - match str { - "dq35" => Self::DQ35, - "dq44" => Self::DQ44, - _ => panic!("dq_variant must be either \"dq35\" or \"dq44\""), - } - } -} - /// Initialize PC-SAFT equation of state. /// /// Parameters diff --git a/build_wheel/src/lib.rs b/build_wheel/src/lib.rs index e1f5fbf..ef50f0c 100644 --- a/build_wheel/src/lib.rs +++ b/build_wheel/src/lib.rs @@ -1,7 +1,51 @@ -use feos_pcsaft::python::feos_pcsaft; +use feos_core::python::joback::PyJobackRecord; +use feos_core::python::parameter::*; +use feos_core::{Contributions, Verbosity}; +use feos_pcsaft::python::*; use pyo3::prelude::*; +use pyo3::wrap_pymodule; +use quantity::python::__PYO3_PYMODULE_DEF_QUANTITY; + +mod dft; +mod eos; +use dft::__PYO3_PYMODULE_DEF_DFT; +use eos::__PYO3_PYMODULE_DEF_EOS; #[pymodule] -pub fn build_wheel(py: Python<'_>, m: &PyModule) -> PyResult<()> { - feos_pcsaft(py, m) +pub fn feos_pcsaft(py: Python<'_>, m: &PyModule) -> PyResult<()> { + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + m.add_class::()?; + + m.add_wrapped(wrap_pymodule!(eos))?; + m.add_wrapped(wrap_pymodule!(dft))?; + m.add_wrapped(wrap_pymodule!(quantity))?; + + py.run( + "\ +import sys +sys.modules['feos_pcsaft.eos'] = eos +sys.modules['feos_pcsaft.eos.utils'] = eos.utils +sys.modules['feos_pcsaft.dft'] = dft +sys.modules['feos_pcsaft.dft.utils'] = dft.utils +quantity.SINumber.__module__ = 'feos_pcsaft.si' +quantity.SIArray1.__module__ = 'feos_pcsaft.si' +quantity.SIArray2.__module__ = 'feos_pcsaft.si' +quantity.SIArray3.__module__ = 'feos_pcsaft.si' +quantity.SIArray4.__module__ = 'feos_pcsaft.si' +sys.modules['feos_pcsaft.si'] = quantity + ", + None, + Some(m.dict()), + )?; + Ok(()) } diff --git a/src/python/parameters.rs b/src/python.rs similarity index 93% rename from src/python/parameters.rs rename to src/python.rs index 8b178d4..0643e15 100644 --- a/src/python/parameters.rs +++ b/src/python.rs @@ -1,3 +1,4 @@ +use crate::eos::polar::DQVariants; use crate::parameters::{PcSaftParameters, PcSaftRecord}; use feos_core::joback::JobackRecord; use feos_core::parameter::{ @@ -11,6 +12,16 @@ use pyo3::prelude::*; use std::convert::TryFrom; use std::rc::Rc; +impl From<&str> for DQVariants { + fn from(str: &str) -> Self { + match str { + "dq35" => Self::DQ35, + "dq44" => Self::DQ44, + _ => panic!("dq_variant must be either \"dq35\" or \"dq44\""), + } + } +} + /// Create a set of PC-Saft parameters from records. #[pyclass(name = "PcSaftRecord", unsendable)] #[pyo3( diff --git a/src/python/mod.rs b/src/python/mod.rs deleted file mode 100644 index 6dbfa1d..0000000 --- a/src/python/mod.rs +++ /dev/null @@ -1,52 +0,0 @@ -use feos_core::python::joback::PyJobackRecord; -use feos_core::python::parameter::*; -use feos_core::{Contributions, Verbosity}; -use pyo3::prelude::*; -use pyo3::wrap_pymodule; -use quantity::python::__PYO3_PYMODULE_DEF_QUANTITY; - -mod eos; -pub use eos::*; -mod dft; -use dft::*; -mod parameters; -use parameters::*; - -#[pymodule] -pub fn feos_pcsaft(py: Python<'_>, m: &PyModule) -> PyResult<()> { - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - m.add_class::()?; - - m.add_wrapped(wrap_pymodule!(eos))?; - m.add_wrapped(wrap_pymodule!(dft))?; - m.add_wrapped(wrap_pymodule!(quantity))?; - - py.run( - "\ -import sys -sys.modules['feos_pcsaft.eos'] = eos -sys.modules['feos_pcsaft.eos.utils'] = eos.utils -sys.modules['feos_pcsaft.dft'] = dft -sys.modules['feos_pcsaft.dft.utils'] = dft.utils -quantity.SINumber.__module__ = 'feos_pcsaft.si' -quantity.SIArray1.__module__ = 'feos_pcsaft.si' -quantity.SIArray2.__module__ = 'feos_pcsaft.si' -quantity.SIArray3.__module__ = 'feos_pcsaft.si' -quantity.SIArray4.__module__ = 'feos_pcsaft.si' -sys.modules['feos_pcsaft.si'] = quantity - ", - None, - Some(m.dict()), - )?; - Ok(()) -}