Skip to content

Commit

Permalink
clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
ybyygu committed Dec 23, 2019
1 parent a1240b4 commit 1c7f10f
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 94 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// AUTHOR: Wenping Guo <ybyygu@gmail.com>
// LICENCE: GPL version 3
// CREATED: <2018-04-29 14:27>
// UPDATED: <2019-12-20 Fri 14:15>
// UPDATED: <2019-12-23 Mon 10:26>
//===============================================================================#
// header:1 ends here

Expand Down
81 changes: 24 additions & 57 deletions src/mic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,27 +78,26 @@ impl Lattice {
pmic.norm()
}

/// Return the relevant periodic images required for neighborhood search
/// within cutoff radius
#[cfg(feature = "adhoc")]
pub fn relevant_images(&self, radius: f64) -> Vec<Vector3f> {
let ns = self.n_min_images(radius);
let na = ns[0] as isize;
let nb = ns[1] as isize;
let nc = ns[2] as isize;

let mut images = vec![];
for i in -na..=na {
for j in -nb..=nb {
for k in -nc..=nc {
let v = Vector3f::from([i as f64, j as f64, k as f64]);
images.push(v);
}
}
}

images
}
// /// Return the relevant periodic images required for neighborhood search
// /// within cutoff radius
// fn relevant_images(&self, radius: f64) -> Vec<Vector3f> {
// let ns = self.n_min_images(radius);
// let na = ns[0] as isize;
// let nb = ns[1] as isize;
// let nc = ns[2] as isize;

// let mut images = vec![];
// for i in -na..=na {
// for j in -nb..=nb {
// for k in -nc..=nc {
// let v = Vector3f::from([i as f64, j as f64, k as f64]);
// images.push(v);
// }
// }
// }

// images
// }

/// Return the mic vector and its length. This algorithm will loop over all
/// relevant images.
Expand All @@ -111,8 +110,10 @@ impl Lattice {
let [na, nb, nc] = self.n_min_images(cutoff);

// search the MIC point with minimum length among relevant image points
let points = vec![[p[0], p[1], p[2]]];
let relevant_points = self.replicate(&points, -na..=na, -nb..=nb, -nc..=nc);
let relevant_points: Vec<Vector3f> = self
.replicate(-na..=na, -nb..=nb, -nc..=nc)
.map(|image| p + self.to_cart(image))
.collect();
let distances2: Vec<_> = relevant_points
.iter()
.map(|&p| Vector3f::from(p).norm_squared())
Expand Down Expand Up @@ -204,39 +205,5 @@ fn test_neighborhood() {
assert_eq!([2, 1, 2], lat.n_min_images(19.));
assert_eq!([2, 1, 2], lat.n_min_images(20.));
assert_eq!([2, 2, 2], lat.n_min_images(20.6));

let expected = [
Vector3f::new(-1.0, -1.0, -1.0),
Vector3f::new(-1.0, -1.0, 0.0),
Vector3f::new(-1.0, -1.0, 1.0),
Vector3f::new(-1.0, 0.0, -1.0),
Vector3f::new(-1.0, 0.0, 0.0),
Vector3f::new(-1.0, 0.0, 1.0),
Vector3f::new(-1.0, 1.0, -1.0),
Vector3f::new(-1.0, 1.0, 0.0),
Vector3f::new(-1.0, 1.0, 1.0),
Vector3f::new(0.0, -1.0, -1.0),
Vector3f::new(0.0, -1.0, 0.0),
Vector3f::new(0.0, -1.0, 1.0),
Vector3f::new(0.0, 0.0, -1.0),
Vector3f::new(0.0, 0.0, 0.0),
Vector3f::new(0.0, 0.0, 1.0),
Vector3f::new(0.0, 1.0, -1.0),
Vector3f::new(0.0, 1.0, 0.0),
Vector3f::new(0.0, 1.0, 1.0),
Vector3f::new(1.0, -1.0, -1.0),
Vector3f::new(1.0, -1.0, 0.0),
Vector3f::new(1.0, -1.0, 1.0),
Vector3f::new(1.0, 0.0, -1.0),
Vector3f::new(1.0, 0.0, 0.0),
Vector3f::new(1.0, 0.0, 1.0),
Vector3f::new(1.0, 1.0, -1.0),
Vector3f::new(1.0, 1.0, 0.0),
Vector3f::new(1.0, 1.0, 1.0),
];

let images = lat.relevant_images(3.0);
assert_eq!(expected.len(), images.len());
assert_eq!(expected[1][2], images[1][2]);
}
// test:1 ends here
50 changes: 22 additions & 28 deletions src/supercell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,38 +7,33 @@ use vecfx::Vector3f;

impl Lattice {
/// Create a supercell along three cell directions.
pub fn replicate<T: Into<Vector3f> + Copy>(
pub fn replicate(
&self,
points: &[T],
ra: impl Iterator<Item = isize> + Clone,
rb: impl Iterator<Item = isize> + Clone,
rc: impl Iterator<Item = isize> + Clone,
) -> Vec<Vector3f> {
iproduct!(ra, rb, rc)
.flat_map(|(i, j, k)| {
let v = [i as f64, j as f64, k as f64];
let tv = self.to_cart(v);
points.iter().map(move |&p| tv + p.into())
})
.collect()
}

#[cfg(feature = "adhoc")]
/// Create a supercell along three cell directions.
pub fn replicate_images(
&self,
ra: impl Iterator<Item = isize> + Clone,
rb: impl Iterator<Item = isize> + Clone,
rc: impl Iterator<Item = isize> + Clone,
) -> impl Iterator<Item = Image> {
iproduct!(ra, rb, rc).map(|(i, j, k)| Image(i, j, k))
) -> impl Iterator<Item = Vector3f> {
iproduct!(ra, rb, rc).map(|(i, j, k)| Vector3f::from([i as f64, j as f64, k as f64]))
}
}

#[cfg(feature = "adhoc")]
/// Helper struct for periodic image
#[derive(Clone, Copy, Debug)]
pub struct Image(pub isize, pub isize, pub isize);
// #[cfg(feature = "adhoc")]
// /// Helper struct for periodic image
// #[derive(Clone, Copy, Debug)]
// pub struct Image(pub(crate) isize, pub(crate) isize, pub(crate) isize);

// #[cfg(feature = "adhoc")]
// impl Image {
// /// Return fractional translation vector for moving a point into this image.
// pub fn translation_vector(&self) -> Vector3f {
// [self.0 as f64, self.1 as f64, self.2 as f64].into()
// }

// /// Return image location relative to origin cell.
// pub fn location(&self) -> [isize; 3] {
// [self.0, self.1, self.2]
// }
// }
// base:1 ends here

// test
Expand All @@ -55,8 +50,7 @@ fn test_supercell() {
];

let lattice = Lattice::new(cell);
let points = vec![[0.0; 3]];
let new_points = lattice.replicate(&points, -1..=1, -1..=1, -1..=1);
assert_eq!(new_points.len(), 27);
let cell_images = lattice.replicate(-1..=1, -1..=1, -1..=1);
assert_eq!(cell_images.count(), 27);
}
// test:1 ends here
16 changes: 8 additions & 8 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ pub(crate) fn get_cell_angles(mat: Matrix3f) -> [f64; 3] {
]
}

// for debug purpose
pub(crate) fn print_xyz(points: &[[f64; 3]]) {
println!("{}", points.len());
println!("xyz");
for [x, y, z] in points.iter() {
println!("He {:10.4}\t{:10.4}\t{:10.4}", x, y, z);
}
}
// // for debug purpose
// fn print_xyz(points: &[[f64; 3]]) {
// println!("{}", points.len());
// println!("xyz");
// for [x, y, z] in points.iter() {
// println!("He {:10.4}\t{:10.4}\t{:10.4}", x, y, z);
// }
// }
// core:1 ends here

0 comments on commit 1c7f10f

Please sign in to comment.