Skip to content

Commit

Permalink
qol improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
prehner committed Jan 17, 2025
1 parent a7b66a5 commit 1a3251e
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 32 deletions.
11 changes: 8 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "feos-campd"
version = "0.2.1"
version = "0.2.2"
authors = ["Philipp Rehner <[email protected]>"]
edition = "2021"
readme = "README.md"
Expand All @@ -24,7 +24,12 @@ serde = "1.0"
serde_json = "1.0"
itertools = "0.12"
knitro_rs = { version = "0.2", path = "knitro_rs", optional = true }
pyo3 = { version = "0.20", features = ["extension-module", "abi3", "abi3-py37"], optional = true }
pyo3 = { version = "0.20", features = [
"extension-module",
"abi3",
"abi3-py37",
"indexmap",
], optional = true }
numpy = { version = "0.20", optional = true }
quantity = { version = "0.7", optional = true }
typenum = "1.17"
Expand All @@ -38,4 +43,4 @@ anyhow = "1.0"
default = []
knitro_12 = ["knitro_rs/knitro_12"]
knitro_13 = ["knitro_rs/knitro_13"]
python = ["pyo3", "numpy", "knitro_12", "quantity/python", "feos-core/python"]
python = ["pyo3", "numpy", "knitro_12", "quantity/python", "feos-core/python"]
5 changes: 5 additions & 0 deletions src/python/molecule.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use crate::CoMTCAMD;
use crate::{molecule::Disjunction, MolecularRepresentation, SuperMolecule};
use indexmap::IndexMap;
use pyo3::prelude::*;
use std::collections::HashMap;

Expand Down Expand Up @@ -32,6 +33,10 @@ impl PyCoMTCAMD {
fn get_initial_values_molecules(&self, molecule: &str) -> Vec<f64> {
self.0.get_initial_values_molecules(molecule)
}

fn parameter_variables(&self, y: Vec<f64>) -> IndexMap<String, f64> {
self.0.evaluate_feature_variables(&y)
}
}

#[derive(Clone)]
Expand Down
19 changes: 17 additions & 2 deletions src/python/property.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::PySuperMolecule;
use crate::{GcPcSaftPropertyModel, PcSaftPropertyModel};
use super::{eos::pcsaft::PyPcSaft, PySuperMolecule};
use crate::{GcPcSaftPropertyModel, PcSaftPropertyModel, PropertyModel};
use feos_core::parameter::IdentifierOption;
use indexmap::IndexMap;
use pyo3::prelude::*;

#[pyclass(name = "PcSaftPropertyModel")]
Expand Down Expand Up @@ -42,6 +43,20 @@ impl PyPcSaftPropertyModel {
symmetry_constraints,
)?))
}

fn build_eos_pure(&self, feature_variables: [IndexMap<String, f64>; 1]) -> PyPcSaft {
let p = self.0.evaluate_parameter_variables(&feature_variables);
PyPcSaft(<PcSaftPropertyModel as PropertyModel<1>>::build_eos(
&self.0, &p,
))
}

fn build_eos_binary(&self, feature_variables: [IndexMap<String, f64>; 2]) -> PyPcSaft {
let p = self.0.evaluate_parameter_variables(&feature_variables);
PyPcSaft(<PcSaftPropertyModel as PropertyModel<2>>::build_eos(
&self.0, &p,
))
}
}

#[pyclass(name = "GcPcSaftPropertyModel")]
Expand Down
6 changes: 2 additions & 4 deletions src/solver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,11 @@ impl<'a, R, P, const N: usize> OptimizationProblemCallback<'a, R, P, N> {
}

impl<
'a,
E: Residual + IdealGas,
R: PropertyModel<N, EquationOfState = E>,
P: ProcessModel<E>,
const N: usize,
> EvalCallback for OptimizationProblemCallback<'a, R, P, N>
> EvalCallback for OptimizationProblemCallback<'_, R, P, N>
{
fn callback(&self, x: &[f64], obj: &mut f64, c: &mut [f64]) -> i32 {
match self.evaluate(x) {
Expand All @@ -59,12 +58,11 @@ impl<
}

impl<
'a,
E: Residual + IdealGas,
R: PropertyModel<N, EquationOfState = E>,
P: ProcessModel<E>,
const N: usize,
> OptimizationProblemCallback<'a, R, P, N>
> OptimizationProblemCallback<'_, R, P, N>
{
fn evaluate(&self, vars: &[f64]) -> EosResult<(f64, Vec<f64>)> {
let x = &vars[..self.process_vars];
Expand Down
4 changes: 2 additions & 2 deletions src/solver/process_optimization.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ impl<'a, R, P, const N: usize> ProcessCallback<'a, R, P, N> {
}
}

impl<'a, R: PropertyModel<N>, P: ProcessModel<R::EquationOfState>, const N: usize> EvalCallback
for ProcessCallback<'a, R, P, N>
impl<R: PropertyModel<N>, P: ProcessModel<R::EquationOfState>, const N: usize> EvalCallback
for ProcessCallback<'_, R, P, N>
{
fn callback(&self, x: &[f64], obj: &mut f64, c: &mut [f64]) -> i32 {
let (x, p) = x.split_at(self.process.variables().len());
Expand Down
22 changes: 1 addition & 21 deletions tests/orc.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use anyhow::Result;
use feos::core::parameter::{IdentifierOption, Parameter};
use feos::core::{EosResult, EquationOfState};
use feos::core::EquationOfState;
use feos::ideal_gas::{Joback, JobackRecord};
use feos::pcsaft::{PcSaft, PcSaftParameters};
use feos_campd::process::{OrganicRankineCycle, ProcessModel};
use feos_campd::ProcessVariables;
#[cfg(feature = "knitro_rs")]
use indexmap::IndexMap;
use std::fs::File;
Expand Down Expand Up @@ -334,22 +333,3 @@ fn test_supermolecule_disjunct_oa() -> Result<()> {
);
Ok(())
}

struct NoModel;
impl<E> ProcessModel<E> for NoModel {
fn variables(&self) -> ProcessVariables {
vec![].into()
}

fn equality_constraints(&self) -> usize {
0
}

fn inequality_constraints(&self) -> usize {
0
}

fn solve(&self, _: &Arc<E>, _: &[f64]) -> EosResult<(f64, Vec<f64>, Vec<f64>)> {
Ok((0.0, vec![], vec![]))
}
}

0 comments on commit 1a3251e

Please sign in to comment.