From 36f0e16bce0de66203fefed8bce4ee3bc40c5926 Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Wed, 27 Nov 2024 16:06:53 +0900 Subject: [PATCH 1/6] add testAngles --- src/force/harmonic_angle_force.rs | 117 +++++++++++++++++++++++++++--- src/force/harmonic_bond_force.rs | 21 ++++-- src/force/mod.rs | 5 +- src/state.rs | 9 ++- src/system.rs | 9 +++ 5 files changed, 139 insertions(+), 22 deletions(-) diff --git a/src/force/harmonic_angle_force.rs b/src/force/harmonic_angle_force.rs index f27bf74..9a806e7 100644 --- a/src/force/harmonic_angle_force.rs +++ b/src/force/harmonic_angle_force.rs @@ -3,20 +3,119 @@ use crate::force::Force; #[derive(Debug, Clone, PartialEq)] pub struct HarmonicAngleForce {} +impl HarmonicAngleForce { + pub fn new() -> Self { + Self {} + } + + pub fn add_angle( + &mut self, + particle1: usize, + particle2: usize, + particle3: usize, + angle: f64, + k: f64, + ) { + unimplemented!() + } + + pub fn set_angle_parameters( + &mut self, + angle_id: usize, + particle1: usize, + particle2: usize, + particle3: usize, + angle: f64, + k: f64, + ) { + unimplemented!() + } +} + impl Force for HarmonicAngleForce { - fn add_bond(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { - unimplemented!() - } - - fn set_bond_parameters(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { - unimplemented!() - } - fn update_parameters_in_context(&mut self, context: &mut Context) { unimplemented!() } - fn set_uses_periodic_boundary_conditions(&mut self, status: bool) { + fn set_periodic_boundary_conditions(&mut self, status: bool) { + unimplemented!() + } + + fn use_periodic_boundary_conditions(&self) -> bool { unimplemented!() } } + +#[cfg(test)] +mod tests { + use super::HarmonicAngleForce; + use crate::context::Context; + use crate::force::Force; + use crate::integrator::VerletIntegrator; + use crate::platform::CPU; + use crate::state::StateType; + use crate::system::System; + use approx::assert_relative_eq; + use nalgebra::Vector3; + use std::f64::consts::PI; + + fn test_harmonic_angle_force() { + let mut system = System::new(); + system.add_particle(1.0); + system.add_particle(1.0); + system.add_particle(1.0); + system.add_particle(1.0); + let integrator = VerletIntegrator { dt: 0.01 }; + + let mut forcefield = HarmonicAngleForce::new(); + forcefield.add_angle(0, 1, 2, PI / 3.0, 1.1); + forcefield.add_angle(1, 2, 3, PI / 2.0, 1.2); + + system.add_force(Box::new(forcefield.clone())); + assert_eq!(forcefield.use_periodic_boundary_conditions(), false); + assert_eq!(system.use_periodic_boundary_conditions(), false); + + let mut context = Context { + system, + integrator: Box::new(integrator), + platform: Box::new(CPU), + }; + + let mut positions = vec![Vector3::zeros(); 4]; + positions[0] = Vector3::new(0.0, 2.0, 0.0); + positions[1] = Vector3::new(0.0, 0.0, 0.0); + positions[2] = Vector3::new(1.0, 0.0, 0.0); + positions[3] = Vector3::new(2.0, 1.0, 0.0); + + context.set_positions(positions); + let state = context.get_state(StateType::Either); + let forces = state.get_forces(); + let torque1 = 1.1 * PI / 6.0; + let torque2 = 1.2 * PI / 4.0; + assert_relative_eq!(forces[0], Vector3::new(torque1, 0.0, 0.0)); + assert_relative_eq!(forces[3], Vector3::new(-0.5 * torque2, 0.5 * torque2, 0.0)); + assert_relative_eq!(forces[1], -forces[0] - forces[2]); + assert_relative_eq!( + state.get_potential_energy(), + 0.5 * 1.1 * PI / 6.0 * PI / 6.0 + 0.5 * 1.2 * PI / 4.0 * PI / 4.0 + ); + + forcefield.set_angle_parameters(0, 0, 1, 2, PI / 3.1, 1.3); + forcefield.set_angle_parameters(1, 1, 2, 3, PI / 2.1, 1.4); + forcefield.update_parameters_in_context(&mut context); + + let state = context.get_state(StateType::Either); + let forces = state.get_forces(); + let dtheta1 = (PI / 2.0) - (PI / 3.1); + let dtheta2 = (3.0 * PI / 4.0) - (PI / 2.1); + let torque1 = 1.3 * dtheta1; + let torque2 = 1.4 * dtheta2; + assert_relative_eq!(forces[0], Vector3::new(torque1, 0.0, 0.0)); + assert_relative_eq!(forces[3], Vector3::new(-0.5 * torque2, 0.5 * torque2, 0.0)); + assert_relative_eq!(forces[1], -forces[0] - forces[2]); + assert_relative_eq!( + state.get_potential_energy(), + 0.5 * 1.3 * dtheta1 * dtheta1 + 0.5 * 1.4 * dtheta2 * dtheta2 + ); + } +} diff --git a/src/force/harmonic_bond_force.rs b/src/force/harmonic_bond_force.rs index 82116a1..fda0b2c 100644 --- a/src/force/harmonic_bond_force.rs +++ b/src/force/harmonic_bond_force.rs @@ -3,7 +3,11 @@ use crate::force::Force; #[derive(Debug, Clone, PartialEq)] pub struct HarmonicBondForce {} -impl Force for HarmonicBondForce { +impl HarmonicBondForce { + pub fn new() -> Self { + Self {} + } + fn add_bond(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { unimplemented!() } @@ -11,12 +15,17 @@ impl Force for HarmonicBondForce { fn set_bond_parameters(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { unimplemented!() } - +} +impl Force for HarmonicBondForce { fn update_parameters_in_context(&mut self, context: &mut Context) { unimplemented!() } - fn set_uses_periodic_boundary_conditions(&mut self, status: bool) { + fn set_periodic_boundary_conditions(&mut self, status: bool) { + unimplemented!() + } + + fn use_periodic_boundary_conditions(&self) -> bool { unimplemented!() } } @@ -28,7 +37,7 @@ mod test { use crate::force::Force; use crate::integrator::VerletIntegrator; use crate::platform::CPU; - use crate::state::{State, StateType}; + use crate::state::StateType; use crate::system::System; use approx::assert_relative_eq; use nalgebra::Vector3; @@ -58,7 +67,7 @@ mod test { positions[2] = Vector3::new(1.0, 0.0, 0.0); context.set_positions(positions); - let state = context.get_state(StateType::Both); + let state = context.get_state(StateType::Either); let forces = state.get_forces(); assert_relative_eq!(forces[0], Vector3::new(0.0, -0.8 * 0.5, 0.0)); assert_relative_eq!(forces[2], Vector3::new(0.7 * 0.2, 0.0, 0.0)); @@ -95,7 +104,7 @@ mod test { let integrator = VerletIntegrator { dt: 0.01 }; let mut forcefield = HarmonicBondForce {}; forcefield.add_bond(0, 1, 1.2, 0.8); - forcefield.set_uses_periodic_boundary_conditions(true); + forcefield.set_periodic_boundary_conditions(true); system.add_force(Box::new(forcefield)); diff --git a/src/force/mod.rs b/src/force/mod.rs index 9121a1f..be482e4 100644 --- a/src/force/mod.rs +++ b/src/force/mod.rs @@ -3,10 +3,9 @@ use std::fmt::Debug; use crate::context::Context; pub trait Force: Debug { - fn add_bond(&mut self, particle1: usize, particle2: usize, length: f64, k: f64); - fn set_bond_parameters(&mut self, particle1: usize, particle2: usize, length: f64, k: f64); fn update_parameters_in_context(&mut self, context: &mut Context); - fn set_uses_periodic_boundary_conditions(&mut self, status: bool); + fn set_periodic_boundary_conditions(&mut self, status: bool); + fn use_periodic_boundary_conditions(&self) -> bool; } pub mod harmonic_angle_force; diff --git a/src/state.rs b/src/state.rs index 1fc8cca..daa4d30 100644 --- a/src/state.rs +++ b/src/state.rs @@ -2,22 +2,23 @@ use nalgebra::Vector3; pub enum StateType { Both, + Either, Forces, Energy, } pub struct State { pub state_type: StateType, - pub forces: Vec>, - pub energy: f32, + pub forces: Vec>, + pub energy: f64, } impl State { - pub fn get_forces(&self) -> &Vec> { + pub fn get_forces(&self) -> &Vec> { &self.forces } - pub fn get_potential_energy(&self) -> f32 { + pub fn get_potential_energy(&self) -> f64 { self.energy } } diff --git a/src/system.rs b/src/system.rs index 19448b9..280d70d 100644 --- a/src/system.rs +++ b/src/system.rs @@ -3,6 +3,10 @@ use crate::virtual_site::VirtualSite; use nalgebra::Vector3; pub struct System {} impl System { + pub fn new() -> Self { + Self {} + } + pub fn add_particle(&mut self, _mass: f32) { // Todo: Rust-OpenMM unimplemented!() @@ -92,6 +96,11 @@ impl System { // Todo: Rust-OpenMM unimplemented!() } + + pub fn use_periodic_boundary_conditions(&self) -> bool { + // Todo: Rust-OpenMM + unimplemented!() + } } #[cfg(test)] mod test { -- 2.49.1 From a433d213f2a5980b74b8a6127f19d3a98f92354a Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Fri, 17 Jan 2025 00:50:12 +0900 Subject: [PATCH 2/6] add ignore tag to tests --- src/force/harmonic_angle_force.rs | 15 +++++++++++++++ src/force/harmonic_bond_force.rs | 3 +++ 2 files changed, 18 insertions(+) diff --git a/src/force/harmonic_angle_force.rs b/src/force/harmonic_angle_force.rs index 9a806e7..7c497a2 100644 --- a/src/force/harmonic_angle_force.rs +++ b/src/force/harmonic_angle_force.rs @@ -59,6 +59,7 @@ mod tests { use nalgebra::Vector3; use std::f64::consts::PI; + #[test] fn test_harmonic_angle_force() { let mut system = System::new(); system.add_particle(1.0); @@ -118,4 +119,18 @@ mod tests { 0.5 * 1.3 * dtheta1 * dtheta1 + 0.5 * 1.4 * dtheta2 * dtheta2 ); } + + #[ignore] + #[test] + fn test_harmonic_angle_force_with_periodic_boundary_conditions() { + // TODO: Rust-OpenMM + unimplemented!() + } + + #[ignore] + #[test] + fn test_harmonic_angle_force_parallel_computation() { + // TODO: Rust-OpenMM + unimplemented!() + } } diff --git a/src/force/harmonic_bond_force.rs b/src/force/harmonic_bond_force.rs index fda0b2c..fe5369b 100644 --- a/src/force/harmonic_bond_force.rs +++ b/src/force/harmonic_bond_force.rs @@ -42,6 +42,7 @@ mod test { use approx::assert_relative_eq; use nalgebra::Vector3; + #[ignore] #[test] fn test_harmonic_bond_force() { let mut system = System {}; @@ -90,6 +91,7 @@ mod test { assert_relative_eq!(energy, 0.5 * 0.9 * 0.4 * 0.4 + 0.5 * 0.8 * 0.3 * 0.3); } + #[ignore] #[test] fn test_harmonic_bond_force_with_periodic_boundary_conditions() { let mut system = System {}; @@ -128,6 +130,7 @@ mod test { assert_relative_eq!(energy, 0.5 * 0.8 * 0.2 * 0.2); } + #[ignore] #[test] fn test_harmonic_bond_force_parallel_computation() { // TODO: Rust-OpenMM -- 2.49.1 From 393a909eb2c34c8734cd413e3816fdad0d0880fc Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Fri, 17 Jan 2025 00:58:39 +0900 Subject: [PATCH 3/6] add angle force test with periodic boundary condition --- src/force/harmonic_angle_force.rs | 41 +++++++++++++++++++++++++++++-- 1 file changed, 39 insertions(+), 2 deletions(-) diff --git a/src/force/harmonic_angle_force.rs b/src/force/harmonic_angle_force.rs index 7c497a2..3239a50 100644 --- a/src/force/harmonic_angle_force.rs +++ b/src/force/harmonic_angle_force.rs @@ -59,6 +59,7 @@ mod tests { use nalgebra::Vector3; use std::f64::consts::PI; + #[ignore] #[test] fn test_harmonic_angle_force() { let mut system = System::new(); @@ -123,8 +124,44 @@ mod tests { #[ignore] #[test] fn test_harmonic_angle_force_with_periodic_boundary_conditions() { - // TODO: Rust-OpenMM - unimplemented!() + let mut system = System {}; + system.add_particle(1.0); + system.add_particle(1.0); + system.add_particle(1.0); + system.set_default_periodic_box_vectors( + Vector3::new(3.0, 0.0, 0.0), + Vector3::new(0.0, 1.5, 0.0), + Vector3::new(0.0, 0.0, 3.0), + ); + + let integrator = VerletIntegrator { dt: 0.01 }; + let mut forcefield = HarmonicAngleForce {}; + forcefield.add_angle(0, 1, 2, PI / 3.0, 1.1); + forcefield.set_periodic_boundary_conditions(true); + + system.add_force(Box::new(forcefield)); + + let mut context = Context { + system, + integrator: Box::new(integrator), + platform: Box::new(CPU), + }; + + let mut positions = vec![Vector3::zeros(); 3]; + positions[0] = Vector3::new(0.0, 1.0, 0.0); + positions[1] = Vector3::new(0.0, 0.0, 0.0); + positions[2] = Vector3::new(1.0, 0.0, 0.0); + context.set_positions(positions); + + let state = context.get_state(StateType::Either); + let forces = state.get_forces(); + + let torque = 1.1 * PI / 6.0; + assert_relative_eq!(forces[0], Vector3::new(2.0 * torque, 0.0, 0.0)); + assert_relative_eq!(forces[2], Vector3::new(0.0, -torque, 0.0)); + + let energy = state.get_potential_energy(); + assert_relative_eq!(energy, 0.5 * 1.1 * (PI / 6.0) * (PI / 6.0)); } #[ignore] -- 2.49.1 From b679ccfbe6c44be0c703e37d894f92dfe1c88ddc Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Fri, 17 Jan 2025 01:05:27 +0900 Subject: [PATCH 4/6] fix clippy fails --- src/context.rs | 5 +++-- src/force/harmonic_angle_force.rs | 26 +++++++++++++------------- src/force/harmonic_bond_force.rs | 14 ++++++++++---- 3 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/context.rs b/src/context.rs index 94252bd..8cd2bb4 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,11 +10,12 @@ pub struct Context { pub platform: Box, } impl Context { - pub fn set_positions(&mut self, positions: Vec>) { + + pub fn set_positions(&mut self, _positions: Vec>) { unimplemented!() } - pub fn get_state(&self, state_type: StateType) -> State { + pub fn get_state(&self, _state_type: StateType) -> State { unimplemented!() } } diff --git a/src/force/harmonic_angle_force.rs b/src/force/harmonic_angle_force.rs index 3239a50..e15e50a 100644 --- a/src/force/harmonic_angle_force.rs +++ b/src/force/harmonic_angle_force.rs @@ -10,34 +10,34 @@ impl HarmonicAngleForce { pub fn add_angle( &mut self, - particle1: usize, - particle2: usize, - particle3: usize, - angle: f64, - k: f64, + _particle1: usize, + _particle2: usize, + _particle3: usize, + _angle: f64, + _k: f64, ) { unimplemented!() } pub fn set_angle_parameters( &mut self, - angle_id: usize, - particle1: usize, - particle2: usize, - particle3: usize, - angle: f64, - k: f64, + _angle_id: usize, + _particle1: usize, + _particle2: usize, + _particle3: usize, + _angle: f64, + _k: f64, ) { unimplemented!() } } impl Force for HarmonicAngleForce { - fn update_parameters_in_context(&mut self, context: &mut Context) { + fn update_parameters_in_context(&mut self, _context: &mut Context) { unimplemented!() } - fn set_periodic_boundary_conditions(&mut self, status: bool) { + fn set_periodic_boundary_conditions(&mut self, _status: bool) { unimplemented!() } diff --git a/src/force/harmonic_bond_force.rs b/src/force/harmonic_bond_force.rs index fe5369b..1621daa 100644 --- a/src/force/harmonic_bond_force.rs +++ b/src/force/harmonic_bond_force.rs @@ -8,20 +8,26 @@ impl HarmonicBondForce { Self {} } - fn add_bond(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { + pub fn add_bond(&mut self, _particle1: usize, _particle2: usize, _length: f64, _k: f64) { unimplemented!() } - fn set_bond_parameters(&mut self, particle1: usize, particle2: usize, length: f64, k: f64) { + pub fn set_bond_parameters( + &mut self, + _particle1: usize, + _particle2: usize, + _length: f64, + _k: f64, + ) { unimplemented!() } } impl Force for HarmonicBondForce { - fn update_parameters_in_context(&mut self, context: &mut Context) { + fn update_parameters_in_context(&mut self, _context: &mut Context) { unimplemented!() } - fn set_periodic_boundary_conditions(&mut self, status: bool) { + fn set_periodic_boundary_conditions(&mut self, _status: bool) { unimplemented!() } -- 2.49.1 From 849fc5de11fe5aa671d5cadab02f610a89d215d1 Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Fri, 17 Jan 2025 01:07:47 +0900 Subject: [PATCH 5/6] impl default trait --- src/force/harmonic_angle_force.rs | 6 ++++++ src/force/harmonic_bond_force.rs | 6 ++++++ src/system.rs | 7 +++++++ 3 files changed, 19 insertions(+) diff --git a/src/force/harmonic_angle_force.rs b/src/force/harmonic_angle_force.rs index e15e50a..1eeb650 100644 --- a/src/force/harmonic_angle_force.rs +++ b/src/force/harmonic_angle_force.rs @@ -46,6 +46,12 @@ impl Force for HarmonicAngleForce { } } +impl Default for HarmonicAngleForce { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod tests { use super::HarmonicAngleForce; diff --git a/src/force/harmonic_bond_force.rs b/src/force/harmonic_bond_force.rs index 1621daa..db3a364 100644 --- a/src/force/harmonic_bond_force.rs +++ b/src/force/harmonic_bond_force.rs @@ -36,6 +36,12 @@ impl Force for HarmonicBondForce { } } +impl Default for HarmonicBondForce { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod test { use super::HarmonicBondForce; diff --git a/src/system.rs b/src/system.rs index 280d70d..01116fc 100644 --- a/src/system.rs +++ b/src/system.rs @@ -102,6 +102,13 @@ impl System { unimplemented!() } } + +impl Default for System { + fn default() -> Self { + Self::new() + } +} + #[cfg(test)] mod test { use crate::force::{HarmonicAngleForce, HarmonicBondForce}; -- 2.49.1 From bbd57d034405fdcea27270c1ad840d8d33b6eeac Mon Sep 17 00:00:00 2001 From: Myeongseon Choi Date: Fri, 17 Jan 2025 01:09:33 +0900 Subject: [PATCH 6/6] fix rustfmt fails --- src/context.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/src/context.rs b/src/context.rs index 8cd2bb4..8fdf89c 100644 --- a/src/context.rs +++ b/src/context.rs @@ -10,7 +10,6 @@ pub struct Context { pub platform: Box, } impl Context { - pub fn set_positions(&mut self, _positions: Vec>) { unimplemented!() } -- 2.49.1