Skip to content

Commit

Permalink
Lattice: add from_matrix method
Browse files Browse the repository at this point in the history
  • Loading branch information
ybyygu committed Feb 14, 2020
1 parent 8286882 commit bec376d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
8 changes: 4 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,23 @@
# [[file:~/Workspace/Programming/gchemol-rs/lattice/lattice.note::*cargo][cargo:1]]
[package]
name = "gchemol-lattice"
version = "0.0.5"
version = "0.0.6"
edition = "2018"
authors = ["Wenping Guo <ybyygu@gmail.com>"]
description = "Periodic 3D crystal lattice"
homepage = "https://github.com/gchemol/lattice"
repository = "https://github.com/gchemol/lattice"
license = "MIT OR Apache-2.0"
exclude = ["bin/*", "docs/*", "ltxpng/*", "data/*"]

[dependencies]
serde = {version="1", features = ["derive"]}
vecfx = {version="0.0.8", features = ["nalgebra"]}
gchemol-gut = "0.0.8"
gchemol-gut = "0.1"

[dev-dependencies]
approx = "0.3"

[features]
# for adhoc hacking
adhoc = []
adhoc = [] # for adhoc hacking
# cargo:1 ends here
12 changes: 10 additions & 2 deletions 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: <2020-02-07 Fri 15:54>
// UPDATED: <2020-02-14 Fri 12:19>
//===============================================================================#
// header:1 ends here

Expand Down Expand Up @@ -62,7 +62,15 @@ impl Default for Lattice {

// [[file:~/Workspace/Programming/gchemol-rs/lattice/lattice.note::*api][api:1]]
impl Lattice {
pub fn new<T: Into<Matrix3f>>(tvs: T) -> Self {
/// Construct `Lattice` from three lattice vectors.
pub fn new<T: Into<Vector3f> + Copy>(tvs: [T; 3]) -> Self {
let vectors = [tvs[0].into(), tvs[1].into(), tvs[2].into()];
let matrix = Matrix3f::from_columns(&vectors);
Self::from_matrix(matrix)
}

/// Construct `Lattice` from lattice matrix (3x3).
pub fn from_matrix<T: Into<Matrix3f>>(tvs: T) -> Self {
let matrix = tvs.into();
let inv_matrix = get_inv_matrix(&matrix);
Lattice {
Expand Down
5 changes: 5 additions & 0 deletions tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@ fn test_lattice_construct() {
let lat = Lattice::from_params(a, b, c, alpha, beta, gamma);
assert_eq!([a, b, c], lat.lengths());
assert_eq!([alpha, beta, gamma], lat.angles());

let vectors = lat.vectors();
let _ = Lattice::new(vectors);
let mat = lat.matrix();
let _ = Lattice::from_matrix(mat);
}

#[test]
Expand Down

0 comments on commit bec376d

Please sign in to comment.