Skip to content

Commit 6989367

Browse files
committed
sign
1 parent 9e00711 commit 6989367

4 files changed

Lines changed: 9 additions & 88 deletions

File tree

Cargo.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ edition = "2021"
99
rand = "0.8.5"
1010
statistical = "1.0.0"
1111
linreg = "0.2.0"
12+
num-traits = "0.2.17"

src/particle.rs

Lines changed: 4 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,12 @@ impl Particle {
4040
}
4141

4242
/// Calculates energy of particle-surface interaction.
43+
/// Only supports 1D simulations.
4344
pub fn energy_surface(&self) -> f64 {
44-
self.binding * (1.0 +
45-
(2.0 * PI * (self.position[0] / self.wells_distance + self.sin_shift)).sin() +
46-
(2.0 * PI * (self.position[1] / self.wells_distance + self.cos_shift)).cos()
47-
)
45+
self.binding * num_traits::sign::signum((2.0 * PI * (self.position[0] / self.wells_distance + self.sin_shift)).sin())
4846
}
4947

48+
5049
/// Proposes a translation move for a particle in 2D space.
5150
///
5251
/// ## Details
@@ -104,72 +103,10 @@ mod tests {
104103
fn test_particle_surface_energy() {
105104

106105
let system = parse_input(INPUT_FILE).expect("Could not find input file.");
107-
let expected = [-1.0, 3.0, -0.0, 0.25, 0.08244];
106+
let expected = [-1.0, 3.0, 0.0, 0.5, -0.2];
108107
for (i, particle) in system.particles.iter().enumerate() {
109108
assert!((particle.energy_surface() - expected[i]).abs() < 0.0001);
110109
}
111110
}
112-
113-
#[test]
114-
fn test_particle_propose_move_1d() {
115-
116-
let mut rng = rand::thread_rng();
117-
let n_moves = 10_000usize;
118-
119-
let displacements = [0.1, 0.5, 1.0];
120-
121-
for i in 0..3 {
122-
let mut particle = Particle::new([0.0, 0.0], 0.0, displacements[i], 0.0, 1.0, 0.0, 0.0);
123-
124-
let orig_pos_x = particle.position[0];
125-
126-
for _ in 0..n_moves {
127-
let old_pos_x = particle.position[0];
128-
129-
particle.propose_move_1d(&mut rng);
130-
131-
assert!((particle.position[0] - old_pos_x).abs() < particle.max_disp);
132-
assert_eq!(particle.position[1], 0.0);
133-
}
134-
135-
//println!("Total difference: {}", particle.position[0] - orig_pos_x);
136-
// this may in very rare cases fail
137-
assert!((particle.position[0] - orig_pos_x).abs() < particle.max_disp * 200.0);
138-
}
139-
}
140-
141-
#[test]
142-
fn test_particle_propose_move_2d() {
143-
144-
let mut rng = rand::thread_rng();
145-
let n_moves = 10_000usize;
146-
147-
let displacements = [0.1, 0.5, 1.0];
148-
149-
for i in 0..3 {
150-
let mut particle = Particle { position: [0.0, 0.0], binding: 0.0, max_disp: displacements[i], size: 0.0, wells_distance: 1.0, sin_shift: 0.0, cos_shift: 0.0 };
151-
152-
let orig_pos_x = particle.position[0];
153-
let orig_pos_y = particle.position[1];
154-
155-
for _ in 0..n_moves {
156-
let old_pos_x = particle.position[0];
157-
let old_pos_y = particle.position[1];
158-
159-
particle.propose_move_2d(&mut rng);
160-
161-
let dx = particle.position[0] - old_pos_x;
162-
let dy = particle.position[1] - old_pos_y;
163-
164-
assert!((dx * dx + dy * dy).sqrt().abs() < particle.max_disp);
165-
}
166-
167-
//println!("Total difference: {} {}", particle.position[0] - orig_pos_x, particle.position[1] - orig_pos_y);
168-
// these two may in very rare cases fail
169-
assert!((particle.position[0] - orig_pos_x).abs() < particle.max_disp * 200.0);
170-
assert!((particle.position[1] - orig_pos_y).abs() < particle.max_disp * 200.0);
171-
172-
}
173-
}
174111

175112
}

src/simulation.rs

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ mod tests {
744744
#[test]
745745
fn test_energy_full() {
746746

747-
let expected_energy = -1.1892628;
747+
let expected_energy = 2.7053452405257734;
748748

749749
let mut system = parse_input(INPUT_FILE).expect("Could not find input file.");
750750

@@ -770,8 +770,6 @@ mod tests {
770770

771771
assert!((full_energy - system.energy_full()).abs() < 0.0001);
772772
assert!((expected_energy - system.energy_full()).abs() < 0.0001);
773-
774-
println!("{full_energy}");
775773

776774
// hard spheres off
777775
system.hard_spheres = false;
@@ -804,14 +802,6 @@ mod tests {
804802
assert_eq!(system.particles[0].position[1], original_positions[0].position[1]);
805803
assert_eq!(system.statistics.accepted[0], 1);
806804
assert_eq!(system.statistics.rejected[0], 0);
807-
808-
// particle 1 shouldn't move as it interacts strongly and is in local minimum
809-
// this may however still fail, albeit rarely
810-
system.move_particle(1);
811-
assert_eq!(system.particles[1].position[0], original_positions[1].position[0]);
812-
assert_eq!(system.particles[1].position[1], original_positions[1].position[1]);
813-
assert_eq!(system.statistics.accepted[1], 0);
814-
assert_eq!(system.statistics.rejected[1], 1);
815805
}
816806

817807
#[test]
@@ -829,14 +819,6 @@ mod tests {
829819
assert_ne!(system.particles[0].position[1], original_positions[0].position[1]);
830820
assert_eq!(system.statistics.accepted[0], 1);
831821
assert_eq!(system.statistics.rejected[0], 0);
832-
833-
// particle 1 shouldn't move as it interacts strongly and is in local minimum
834-
// this may however still fail, albeit rarely
835-
system.move_particle(1);
836-
assert_eq!(system.particles[1].position[0], original_positions[1].position[0]);
837-
assert_eq!(system.particles[1].position[1], original_positions[1].position[1]);
838-
assert_eq!(system.statistics.accepted[1], 0);
839-
assert_eq!(system.statistics.rejected[1], 1);
840822
}
841823

842824
#[test]

0 commit comments

Comments
 (0)