From 7dcf5d735b107d90474e063be4a29fb545d00c83 Mon Sep 17 00:00:00 2001 From: Abdullah Ahmad Date: Sun, 15 Oct 2023 02:14:59 +0100 Subject: [PATCH] toml preserves order, consistent calculations --- Cargo.lock | 1 + Cargo.toml | 6 +-- cSPCE_XRISM_methane.toml | 8 +-- pyrism/rism_ctrl.py | 2 +- src/driver.rs | 112 +++++++++++++++++++++++++++++---------- src/input.rs | 4 +- src/main.rs | 3 +- 7 files changed, 97 insertions(+), 39 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 1d76ca70..25c1366b 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1644,6 +1644,7 @@ version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "185d8ab0dfbb35cf1399a6344d8484209c088f75f8f68230da55d48d95d43e3d" dependencies = [ + "indexmap", "serde", "serde_spanned", "toml_datetime", diff --git a/Cargo.toml b/Cargo.toml index 10e4e3d2..2a8e09ce 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,8 +7,8 @@ edition = "2021" [[bin]] -name = "pyrism" -src = "src/main.rs" +name = "rism" +path = "src/main.rs" [lib] # The name of the native library. This is the name which will be used in Python to import the @@ -37,7 +37,7 @@ simple_logger = "4.2.0" gnuplot = "0.0.39" serde = { version = "1.0.189", features = ["derive"] } bincode = "1.3.3" -toml = { version = "0.8.2", features = ["parse"] } +toml = { version = "0.8.2", features = ["parse", "preserve_order"] } bzip2 = "0.4.4" lexopt = "0.3.0" diff --git a/cSPCE_XRISM_methane.toml b/cSPCE_XRISM_methane.toml index df1e63a3..58a52a3f 100644 --- a/cSPCE_XRISM_methane.toml +++ b/cSPCE_XRISM_methane.toml @@ -12,7 +12,7 @@ potential = "LJ" closure = "HNC" IE = "XRISM" solver = "MDIIS" -depth = 12 +depth = 8 picard_damping = 0.5 mdiis_damping = 0.5 itermax = 10000 @@ -31,17 +31,17 @@ nspu = 1 [solvent.water] dens = 0.03334 ns = 3 -"O" = [ +O = [ [78.15, 3.1657, -0.8476000010975563], [0.00000000e+00, 0.00000000e+00, 0.00000000e+00] ] -"H1" = [ +H1 = [ [7.815, 1.1657, 0.4238], [1.00000000e+00, 0.00000000e+00, 0.00000000e+00] ] -"H2" = [ +H2 = [ [7.815, 1.1657, 0.4238], [-3.33314000e-01, 9.42816000e-01, 0.00000000e+00] ] diff --git a/pyrism/rism_ctrl.py b/pyrism/rism_ctrl.py index de3514b7..a72c6870 100644 --- a/pyrism/rism_ctrl.py +++ b/pyrism/rism_ctrl.py @@ -244,7 +244,7 @@ def read_input(self): rism_job = RISMDriver( data_config, operator_config, potential_config, solver_config ) - vv = rism_job.execute() + vv = rism_job.do_rism() print(vv) diff --git a/src/driver.rs b/src/driver.rs index 07ff616e..617e9e13 100644 --- a/src/driver.rs +++ b/src/driver.rs @@ -3,6 +3,7 @@ use crate::data::{ SystemState, }; use crate::dipole::*; +use crate::input::{Configuration, InputTOMLHandler}; use crate::integralequation::IntegralEquationKind; use crate::operator::{Operator, OperatorConfig}; use crate::potential::{Potential, PotentialConfig}; @@ -17,6 +18,7 @@ use pyo3::prelude::*; use std::f64::consts::PI; use std::fs; use std::io::prelude::*; +use std::path::PathBuf; pub enum Verbosity { Quiet, @@ -98,7 +100,39 @@ impl RISMDriver { }) } - pub fn execute<'py>(&'py mut self, py: Python<'py>) -> PyResult> { + pub fn do_rism<'py>(&'py mut self, py: Python<'py>) { + // -> PyResult> { + self.execute(); + // Ok(PyCorrelations::new( + // uv.clone().unwrap().correlations.cr, + // uv.clone().unwrap().correlations.tr, + // uv.clone().unwrap().correlations.hr, + // gr_uv, + // py, + // ) + // .into_py(py)) + } + + // pub fn extract<'py>( + // &'py self, + // py: Python<'py>, + // ) -> PyResult<( + // &PyArray3, + // &PyArray3, + // &PyArray3, + // &PyArray3, + // )> { + // Ok(( + // self.data.cr.clone().into_pyarray(py), + // self.data.tr.clone().into_pyarray(py), + // self.data.hr.clone().into_pyarray(py), + // self.data.hk.clone().into_pyarray(py), + // )) + // } +} + +impl RISMDriver { + pub fn execute(&mut self) { self.print_header(); simple_logger::init_with_env().unwrap(); // set up operator(RISM equation and Closure) @@ -109,6 +143,11 @@ impl RISMDriver { ..self.operator.clone() }); + println!( + "{:#?}\n\n{:#?}\n\n{:#?}\n\n{:#?}", + self.data, self.operator, self.solver, self.potential + ); + let (mut vv, mut uv) = self.problem_setup(); let mut solver = self.solver.solver.set(&self.solver.settings); @@ -155,36 +194,53 @@ impl RISMDriver { let encoded_vv: Vec = bincode::serialize(&vv_solution).expect("encode solvent-solvent results to binary"); let compressor = BzEncoder::new(encoded_vv.as_slice(), Compression::best()); - - Ok(PyCorrelations::new( - uv.clone().unwrap().correlations.cr, - uv.clone().unwrap().correlations.tr, - uv.clone().unwrap().correlations.hr, - gr_uv, - py, - ) - .into_py(py)) } - // pub fn extract<'py>( - // &'py self, - // py: Python<'py>, - // ) -> PyResult<( - // &PyArray3, - // &PyArray3, - // &PyArray3, - // &PyArray3, - // )> { - // Ok(( - // self.data.cr.clone().into_pyarray(py), - // self.data.tr.clone().into_pyarray(py), - // self.data.hr.clone().into_pyarray(py), - // self.data.hk.clone().into_pyarray(py), - // )) - // } -} + pub fn from_toml(fname: PathBuf) -> Self { + let config: Configuration = InputTOMLHandler::construct_configuration(&fname); + let data = config.data_config; + let (solvent, solute); + let shape = (data.npts, data.nsv, data.nsv); -impl RISMDriver { + // Construct the solvent-solvent problem + solvent = SingleData::new( + data.solvent_atoms.clone(), + data.solvent_species.clone(), + shape, + ); + + // Check if a solute-solvent problem exists + match data.nsu { + None => solute = None, + _ => { + let shape = (data.npts, data.nsu.unwrap(), data.nsu.unwrap()); + // Construct the solute-solvent problem + solute = Some(SingleData::new( + data.solute_atoms.as_ref().unwrap().clone(), + data.solute_species.as_ref().unwrap().clone(), + shape, + )); + } + } + + // Extract operator information + let operator: OperatorConfig = config.operator_config; + + // Extract potential information + let potential: PotentialConfig = config.potential_config; + + // Extract solver information + let solver: SolverConfig = config.solver_config; + + RISMDriver { + solvent, + solute, + data, + operator, + potential, + solver, + } + } fn problem_setup(&mut self) -> (DataRs, Option) { let (mut vv_problem, uv_problem); info!("Defining solvent-solvent problem"); diff --git a/src/input.rs b/src/input.rs index 3806a611..265b37fc 100644 --- a/src/input.rs +++ b/src/input.rs @@ -1,6 +1,6 @@ use log::info; use serde::{Deserialize, Serialize}; -use std::collections::HashMap; +use std::collections::{BTreeMap, HashMap}; use std::fs; use std::path::PathBuf; @@ -261,7 +261,7 @@ pub struct Params { pub enum ProblemInfo { Preconverged(String), Length(usize), - Data(HashMap), + Data(BTreeMap), } impl ProblemInfo {} diff --git a/src/main.rs b/src/main.rs index f452c1e2..6a985983 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,6 +32,7 @@ fn main() -> Result<(), lexopt::Error> { let args = parse_args()?; println!("{}", args.verbosity); println!("{}", args.input_file.display()); - + let mut driver = RISMDriver::from_toml(args.input_file); + driver.execute(); Ok(()) }