Skip to content

Commit

Permalink
Fixed Mac compilation/Rc+RefCell work done
Browse files Browse the repository at this point in the history
- Mac compilation working now, needed specific rustc flags for it to work.
- Rc/RefCell is done, works with the SingleData Structs.
  • Loading branch information
2AUK committed Nov 1, 2023
1 parent 5b3aa97 commit 309b65a
Show file tree
Hide file tree
Showing 7 changed files with 80 additions and 56 deletions.
10 changes: 9 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,10 @@
[target.x86_64-unknown-linux-gnu]
rustflags = ["-Ctarget-cpu=native"]
rustflags = ["-C", "target-cpu=native"]

[target.x86_64-apple-darwin]
rustflags = [
"-C", "link-arg=-undefined",
"-C", "link-arg=dynamic_lookup",
"-C", "target-cpu=native",
]

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ features = ["source"]
python-source = "pyrism"

[profile.release]
codegen-units = 1
incremental = true
debug = true
lto = false
debug = false
lto = "fat"
opt-level = "z"
panic = "abort"
strip = "debuginfo"
86 changes: 51 additions & 35 deletions src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,6 @@ impl RISMDriver {
self.operator.clone(),
vv.interactions.clone(),
vv.correlations.clone(),
vv.data_b.wk.clone(),
None,
);
if compress {
trace!("Compressing solvent-solvent data");
Expand Down Expand Up @@ -260,8 +258,6 @@ impl RISMDriver {
self.operator.clone(),
uv.interactions.clone(),
uv.correlations.clone(),
uv.data_b.wk.clone(),
Some(uv.data_a.wk.clone()),
);
Self::restore_renormalised_functions(&mut uv_solution, uv.system.beta);
Some(uv_solution)
Expand Down Expand Up @@ -313,23 +309,23 @@ impl RISMDriver {
let shape = (data.npts, data.nsv, data.nsv);

// Construct the solvent-solvent problem
solvent = SingleData::new(
solvent = Rc::new(RefCell::new(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(
solute = Some(Rc::new(RefCell::new(SingleData::new(
data.solute_atoms.as_ref().unwrap().clone(),
data.solute_species.as_ref().unwrap().clone(),
shape,
));
))));
}
}

Expand Down Expand Up @@ -393,14 +389,14 @@ impl RISMDriver {
);
self.data.nsv = data.data_config.nsv;
self.data.nspv = data.data_config.nspv;
self.solvent.sites = data.data_config.solvent_atoms.clone();
self.solvent.species = data.data_config.solvent_species.clone();
self.solvent.borrow_mut().sites = data.data_config.solvent_atoms.clone();
self.solvent.borrow_mut().species = data.data_config.solvent_species.clone();
let new_shape = (
data.data_config.npts,
data.data_config.nsv,
data.data_config.nsv,
);
self.solvent.density = {
self.solvent.borrow_mut().density = {
let mut dens_vec: Vec<f64> = Vec::new();
for i in data.data_config.solvent_species.clone().into_iter() {
for _j in i.atom_sites {
Expand All @@ -409,7 +405,7 @@ impl RISMDriver {
}
Array2::from_diag(&Array::from_vec(dens_vec))
};
self.solvent.wk = Array::zeros(new_shape);
self.solvent.borrow_mut().wk = Array::zeros(new_shape);
let dielectric = self.compute_dielectrics(&grid);
vv_problem = DataRs::new(
system.clone(),
Expand Down Expand Up @@ -464,7 +460,7 @@ impl RISMDriver {
fn compute_dielectrics(&mut self, grid: &Grid) -> Option<DielectricData> {
trace!("Checking for dipole moment");
let mut dm_vec = Vec::new();
for species in self.solvent.species.iter() {
for species in self.solvent.borrow().species.iter() {
dm_vec.push(dipole_moment(&species.atom_sites));
}
let (dm, _): (Vec<_>, Vec<_>) = dm_vec.into_iter().partition(Result::is_ok);
Expand All @@ -477,7 +473,7 @@ impl RISMDriver {
match self.operator.integral_equation {
IntegralEquationKind::DRISM => {
trace!("Aligning dipole moment to z-axis");
for species in self.solvent.species.iter_mut() {
for species in self.solvent.borrow_mut().species.iter_mut() {
let tot_charge = total_charge(&species.atom_sites);
let mut coc = centre_of_charge(&species.atom_sites);
coc /= tot_charge;
Expand All @@ -491,6 +487,7 @@ impl RISMDriver {
let mut k_exp_term = grid.kgrid.clone();
let total_density = self
.solvent
.borrow()
.species
.iter()
.fold(0.0, |acc, species| acc + species.dens);
Expand All @@ -500,12 +497,17 @@ impl RISMDriver {
.expect("damping parameter for DRISM set");
let diel = self.data.dielec.expect("dielectric constant set");
k_exp_term.par_mapv_inplace(|x| (-1.0 * (drism_damping * x / 2.0).powf(2.0)).exp());
let dipole_density = self.solvent.species.iter().fold(0.0, |acc, species| {
match dipole_moment(&species.atom_sites) {
Ok((_, dm)) => acc + species.dens * dm * dm,
_ => acc + 0.0,
}
});
let dipole_density =
self.solvent
.borrow()
.species
.iter()
.fold(0.0, |acc, species| {
match dipole_moment(&species.atom_sites) {
Ok((_, dm)) => acc + species.dens * dm * dm,
_ => acc + 0.0,
}
});
let y = 4.0 * PI * dipole_density / 9.0;
let hc0 = (((diel - 1.0) / y) - 3.0) / total_density;
let hck = hc0 * k_exp_term;
Expand All @@ -519,7 +521,7 @@ impl RISMDriver {
let mut chi = Array::zeros((self.data.npts, self.data.nsv, self.data.nsv));
for (ki, k) in grid.kgrid.iter().enumerate() {
let mut i = 0;
for species in self.solvent.species.iter() {
for species in self.solvent.borrow().species.iter() {
for atm in species.atom_sites.iter() {
let k_coord = *k * Array::from_vec(atm.coords.clone());
if k_coord[0] == 0.0 {
Expand Down Expand Up @@ -604,21 +606,21 @@ impl RISMDriver {
let potential = Potential::new(&self.potential);
// set up total potential
let npts = problem.grid.npts;
let num_sites_a = problem.data_a.sites.len();
let num_sites_b = problem.data_b.sites.len();
let num_sites_a = problem.data_a.borrow().sites.len();
let num_sites_b = problem.data_b.borrow().sites.len();
let shape = (npts, num_sites_a, num_sites_b);
let (mut u_nb, mut u_c) = (Array::zeros(shape), Array::zeros(shape));
// compute nonbonded interaction
(potential.nonbonded)(
&problem.data_a.sites,
&problem.data_b.sites,
&problem.data_a.borrow().sites,
&problem.data_b.borrow().sites,
&problem.grid.rgrid,
&mut u_nb,
);
// compute electrostatic interaction
(potential.coulombic)(
&problem.data_a.sites,
&problem.data_b.sites,
&problem.data_a.borrow().sites,
&problem.data_b.borrow().sites,
&problem.grid.rgrid,
&mut u_c,
);
Expand All @@ -628,14 +630,14 @@ impl RISMDriver {

// compute renormalised potentials
(potential.renormalisation_real)(
&problem.data_a.sites,
&problem.data_b.sites,
&problem.data_a.borrow().sites,
&problem.data_b.borrow().sites,
&problem.grid.rgrid,
&mut problem.interactions.ur_lr,
);
(potential.renormalisation_fourier)(
&problem.data_a.sites,
&problem.data_b.sites,
&problem.data_a.borrow().sites,
&problem.data_b.borrow().sites,
&problem.grid.kgrid,
&mut problem.interactions.uk_lr,
);
Expand All @@ -648,11 +650,25 @@ impl RISMDriver {
}

fn build_intramolecular_correlation(&mut self, problem: &mut DataRs) {
let distances_a = Self::distance_matrix(&problem.data_a.species, &problem.data_a.species);
let distances_b = Self::distance_matrix(&problem.data_b.species, &problem.data_b.species);
let distances_a = Self::distance_matrix(
&problem.data_a.borrow().species,
&problem.data_a.borrow().species,
);
let distances_b = Self::distance_matrix(
&problem.data_b.borrow().species,
&problem.data_b.borrow().species,
);

Self::intramolecular_corr_impl(&distances_a, &problem.grid.kgrid, &mut problem.data_a.wk);
Self::intramolecular_corr_impl(&distances_b, &problem.grid.kgrid, &mut problem.data_b.wk);
Self::intramolecular_corr_impl(
&distances_a,
&problem.grid.kgrid,
&mut problem.data_a.borrow_mut().wk,
);
Self::intramolecular_corr_impl(
&distances_b,
&problem.grid.kgrid,
&mut problem.data_b.borrow_mut().wk,
);
}

fn intramolecular_corr_impl(
Expand Down
18 changes: 9 additions & 9 deletions src/integralequation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,18 +62,18 @@ pub fn rism_uv(problem: &mut DataRs) {
problem.grid.dr,
problem.grid.dk,
problem.correlations.cr.view(),
problem.data_a.wk.view(),
problem.data_b.density.view(),
problem.data_a.borrow().wk.view(),
problem.data_b.borrow().density.view(),
problem.system.beta,
problem.interactions.uk_lr.view(),
problem.interactions.ur_lr.view(),
h_vv.view(),
problem.data_b.wk.view(),
problem.data_b.borrow().wk.view(),
)
}

pub fn xrism_vv(problem: &mut DataRs) {
let nsv = problem.data_a.sites.len();
let nsv = problem.data_a.borrow().sites.len();
(problem.correlations.hk, problem.correlations.tr) = rism_vv_equation_impl(
nsv,
problem.grid.npts,
Expand All @@ -82,8 +82,8 @@ pub fn xrism_vv(problem: &mut DataRs) {
problem.grid.dr,
problem.grid.dk,
problem.correlations.cr.view(),
problem.data_a.wk.view(),
problem.data_a.density.view(),
problem.data_a.borrow().wk.view(),
problem.data_a.borrow().density.view(),
problem.system.beta,
problem.interactions.uk_lr.view(),
problem.interactions.ur_lr.view(),
Expand All @@ -92,7 +92,7 @@ pub fn xrism_vv(problem: &mut DataRs) {
}

pub fn drism_vv(problem: &mut DataRs) {
let nsv = problem.data_a.sites.len();
let nsv = problem.data_a.borrow().sites.len();
(problem.correlations.hk, problem.correlations.tr) = rism_vv_equation_impl(
nsv,
problem.grid.npts,
Expand All @@ -101,8 +101,8 @@ pub fn drism_vv(problem: &mut DataRs) {
problem.grid.dr,
problem.grid.dk,
problem.correlations.cr.view(),
problem.data_a.wk.view(),
problem.data_a.density.view(),
problem.data_a.borrow().wk.view(),
problem.data_a.borrow().density.view(),
problem.system.beta,
problem.interactions.uk_lr.view(),
problem.interactions.ur_lr.view(),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ pub mod writer;
/// import the module.
#[pymodule]
fn librism(_py: Python<'_>, m: &PyModule) -> PyResult<()> {
m.add_class::<RISMDriver>()?;
//m.add_class::<RISMDriver>()?;
Ok(())
}
6 changes: 4 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,14 @@ fn main() -> Result<(), lexopt::Error> {
Array2::from_diag(&Array::from_vec(dens_vec))
};
let grid = Grid::new(vv.data_config.npts, vv.data_config.radius);
let wv = &driver.solvent.borrow().wk;
let wu = &driver.solute.as_ref().unwrap().borrow().wk;
SFEs::new(
1.0 / vv.data_config.kt / vv.data_config.temp,
vv.data_config.ku,
&uv.correlations,
&uv.wu.as_ref().unwrap(),
&uv.wv,
wu,
wv,
&density,
&grid.rgrid,
&grid.kgrid,
Expand Down
6 changes: 0 additions & 6 deletions src/solution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ pub struct SolvedData {
pub operator_config: OperatorConfig,
pub interactions: Interactions,
pub correlations: Correlations,
pub wv: Array3<f64>,
pub wu: Option<Array3<f64>>,
}

impl SolvedData {
Expand All @@ -34,8 +32,6 @@ impl SolvedData {
operator_config: OperatorConfig,
interactions: Interactions,
correlations: Correlations,
wv: Array3<f64>,
wu: Option<Array3<f64>>,
) -> Self {
SolvedData {
data_config,
Expand All @@ -44,8 +40,6 @@ impl SolvedData {
operator_config,
interactions,
correlations,
wv,
wu,
}
}
}
Expand Down

0 comments on commit 309b65a

Please sign in to comment.