mirror of
https://github.com/dnlbauer/xdrfile.git
synced 2026-09-11 14:45:31 +00:00
Format with rustfmt
This commit is contained in:
@@ -1,5 +1,5 @@
|
|||||||
|
pub mod xdr_seek;
|
||||||
#[allow(non_upper_case_globals)]
|
#[allow(non_upper_case_globals)]
|
||||||
pub mod xdrfile;
|
pub mod xdrfile;
|
||||||
pub mod xdrfile_trr;
|
pub mod xdrfile_trr;
|
||||||
pub mod xdrfile_xtc;
|
pub mod xdrfile_xtc;
|
||||||
pub mod xdr_seek;
|
|
||||||
@@ -103,12 +103,11 @@ extern "C" {
|
|||||||
pub fn xdr_flush(xd: *mut XDRFILE) -> ::std::os::raw::c_int;
|
pub fn xdr_flush(xd: *mut XDRFILE) -> ::std::os::raw::c_int;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
use super::*;
|
|
||||||
use super::super::xdrfile_xtc::*;
|
use super::super::xdrfile_xtc::*;
|
||||||
|
use super::*;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -129,10 +128,15 @@ mod tests {
|
|||||||
let tell = xdr_tell(xdr);
|
let tell = xdr_tell(xdr);
|
||||||
assert!(tell == 0, "{}", tell);
|
assert!(tell == 0, "{}", tell);
|
||||||
|
|
||||||
read_xtc(xdr, num_atoms, &mut step, &mut time,
|
read_xtc(
|
||||||
|
xdr,
|
||||||
|
num_atoms,
|
||||||
|
&mut step,
|
||||||
|
&mut time,
|
||||||
box_vec.as_ptr() as *mut Matrix,
|
box_vec.as_ptr() as *mut Matrix,
|
||||||
x_p,
|
x_p,
|
||||||
&mut prec);
|
&mut prec,
|
||||||
|
);
|
||||||
|
|
||||||
let tell = xdr_tell(xdr);
|
let tell = xdr_tell(xdr);
|
||||||
assert!(tell > 0, "{}", tell);
|
assert!(tell > 0, "{}", tell);
|
||||||
|
|||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
pub const DIM: u32 = 3;
|
pub const DIM: u32 = 3;
|
||||||
|
|
||||||
#[repr(C)]
|
#[repr(C)]
|
||||||
@@ -7,7 +6,6 @@ pub struct XDRFILE {
|
|||||||
_unused: [u8; 0],
|
_unused: [u8; 0],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
pub type BindgenTy1 = u32;
|
pub type BindgenTy1 = u32;
|
||||||
pub const exdrOK: BindgenTy1 = 0;
|
pub const exdrOK: BindgenTy1 = 0;
|
||||||
pub const exdrHEADER: BindgenTy1 = 1;
|
pub const exdrHEADER: BindgenTy1 = 1;
|
||||||
|
|||||||
@@ -1,6 +1,5 @@
|
|||||||
use super::xdrfile::*;
|
use super::xdrfile::*;
|
||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn read_trr_natoms(
|
pub fn read_trr_natoms(
|
||||||
fn_: *const ::std::os::raw::c_char,
|
fn_: *const ::std::os::raw::c_char,
|
||||||
@@ -89,11 +88,17 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let mode = CString::new("w").unwrap();
|
let mode = CString::new("w").unwrap();
|
||||||
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
||||||
let write_code = write_trr(xdr, natoms, step, time, lambda,
|
let write_code = write_trr(
|
||||||
|
xdr,
|
||||||
|
natoms,
|
||||||
|
step,
|
||||||
|
time,
|
||||||
|
lambda,
|
||||||
box_vec.as_ptr() as *mut Matrix,
|
box_vec.as_ptr() as *mut Matrix,
|
||||||
x.as_ptr() as *mut Rvec,
|
x.as_ptr() as *mut Rvec,
|
||||||
v.as_ptr() as *mut Rvec,
|
v.as_ptr() as *mut Rvec,
|
||||||
f.as_ptr() as *mut Rvec);
|
f.as_ptr() as *mut Rvec,
|
||||||
|
);
|
||||||
assert!(write_code as u32 == exdrOK);
|
assert!(write_code as u32 == exdrOK);
|
||||||
xdrfile_close(xdr);
|
xdrfile_close(xdr);
|
||||||
}
|
}
|
||||||
@@ -111,11 +116,17 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let mode = CString::new("r").unwrap();
|
let mode = CString::new("r").unwrap();
|
||||||
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
||||||
let read_code = read_trr(xdr, natoms, &mut step2, &mut time2,
|
let read_code = read_trr(
|
||||||
&mut lambda2, box_vec2.as_ptr() as *mut Matrix,
|
xdr,
|
||||||
|
natoms,
|
||||||
|
&mut step2,
|
||||||
|
&mut time2,
|
||||||
|
&mut lambda2,
|
||||||
|
box_vec2.as_ptr() as *mut Matrix,
|
||||||
x2.as_ptr() as *mut Rvec,
|
x2.as_ptr() as *mut Rvec,
|
||||||
v2.as_ptr() as *mut Rvec,
|
v2.as_ptr() as *mut Rvec,
|
||||||
f2.as_ptr() as *mut Rvec);
|
f2.as_ptr() as *mut Rvec,
|
||||||
|
);
|
||||||
assert!(read_code as u32 == exdrOK);
|
assert!(read_code as u32 == exdrOK);
|
||||||
xdrfile_close(xdr);
|
xdrfile_close(xdr);
|
||||||
}
|
}
|
||||||
@@ -129,5 +140,4 @@ mod tests {
|
|||||||
assert!(v2 == v);
|
assert!(v2 == v);
|
||||||
assert!(f2 == f);
|
assert!(f2 == f);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,6 +1,5 @@
|
|||||||
use super::xdrfile::*;
|
use super::xdrfile::*;
|
||||||
|
|
||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
pub fn read_xtc_natoms(
|
pub fn read_xtc_natoms(
|
||||||
fn_: *const ::std::os::raw::c_char,
|
fn_: *const ::std::os::raw::c_char,
|
||||||
@@ -81,9 +80,15 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let mode = CString::new("w").unwrap();
|
let mode = CString::new("w").unwrap();
|
||||||
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
||||||
let write_code = write_xtc(xdr, natoms, step, time,
|
let write_code = write_xtc(
|
||||||
box_vec.as_ptr() as *mut Matrix, x.as_ptr() as *mut Rvec,
|
xdr,
|
||||||
1000.0);
|
natoms,
|
||||||
|
step,
|
||||||
|
time,
|
||||||
|
box_vec.as_ptr() as *mut Matrix,
|
||||||
|
x.as_ptr() as *mut Rvec,
|
||||||
|
1000.0,
|
||||||
|
);
|
||||||
assert!(write_code as u32 == exdrOK);
|
assert!(write_code as u32 == exdrOK);
|
||||||
xdrfile_close(xdr);
|
xdrfile_close(xdr);
|
||||||
}
|
}
|
||||||
@@ -98,9 +103,15 @@ mod tests {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let mode = CString::new("r").unwrap();
|
let mode = CString::new("r").unwrap();
|
||||||
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
|
||||||
let read_code = read_xtc(xdr, natoms, &mut step2, &mut time2,
|
let read_code = read_xtc(
|
||||||
box_vec2.as_ptr() as *mut Matrix, x2.as_ptr() as *mut Rvec,
|
xdr,
|
||||||
&mut prec);
|
natoms,
|
||||||
|
&mut step2,
|
||||||
|
&mut time2,
|
||||||
|
box_vec2.as_ptr() as *mut Matrix,
|
||||||
|
x2.as_ptr() as *mut Rvec,
|
||||||
|
&mut prec,
|
||||||
|
);
|
||||||
assert!(read_code as u32 == exdrOK);
|
assert!(read_code as u32 == exdrOK);
|
||||||
xdrfile_close(xdr);
|
xdrfile_close(xdr);
|
||||||
}
|
}
|
||||||
|
|||||||
24
src/frame.rs
24
src/frame.rs
@@ -26,23 +26,28 @@ impl Default for Frame {
|
|||||||
step: 0,
|
step: 0,
|
||||||
time: 0.0,
|
time: 0.0,
|
||||||
box_vector: [[0.0; 3]; 3],
|
box_vector: [[0.0; 3]; 3],
|
||||||
coords: Vec::with_capacity(0)
|
coords: Vec::with_capacity(0),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for Frame {
|
impl fmt::Debug for Frame {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "Frame {{ atoms: {}, step: {}, time: {}, \
|
write!(
|
||||||
box: {:?}, coords: {:?} }}", self.num_atoms, self.step, self.time,
|
f,
|
||||||
self.box_vector, self.coords)
|
"Frame {{ atoms: {}, step: {}, time: {}, \
|
||||||
|
box: {:?}, coords: {:?} }}",
|
||||||
|
self.num_atoms, self.step, self.time, self.box_vector, self.coords
|
||||||
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Frame {
|
impl Frame {
|
||||||
/// Creates an empty frame with a capacity of 0
|
/// Creates an empty frame with a capacity of 0
|
||||||
pub fn new() -> Frame {
|
pub fn new() -> Frame {
|
||||||
Frame{ ..Default::default() }
|
Frame {
|
||||||
|
..Default::default()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a frame with the given capacity
|
/// Creates a frame with the given capacity
|
||||||
@@ -52,12 +57,13 @@ impl Frame {
|
|||||||
coords: vec![[0.0, 0.0, 0.0]; num_atoms as usize],
|
coords: vec![[0.0, 0.0, 0.0]; num_atoms as usize],
|
||||||
..Default::default()
|
..Default::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Filters the frame by removing all atoms not matching the given indeces.
|
/// Filters the frame by removing all atoms not matching the given indeces.
|
||||||
pub fn filter_coords(self: &mut Frame, indeces: &[usize]) {
|
pub fn filter_coords(self: &mut Frame, indeces: &[usize]) {
|
||||||
self.coords = self.coords.iter()
|
self.coords = self
|
||||||
|
.coords
|
||||||
|
.iter()
|
||||||
.map(|elem| elem.clone())
|
.map(|elem| elem.clone())
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.filter(|&(i, _)| indeces.contains(&i))
|
.filter(|&(i, _)| indeces.contains(&i))
|
||||||
@@ -72,8 +78,6 @@ impl Frame {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
@@ -91,7 +95,7 @@ mod tests {
|
|||||||
frame.coords[0] = [1.0, 2.0, 3.0];
|
frame.coords[0] = [1.0, 2.0, 3.0];
|
||||||
frame.coords[1] = [4.0, 5.0, 6.0];
|
frame.coords[1] = [4.0, 5.0, 6.0];
|
||||||
frame.coords[2] = [7.0, 8.0, 9.0];
|
frame.coords[2] = [7.0, 8.0, 9.0];
|
||||||
let filter: Vec<usize> = vec![1,2];
|
let filter: Vec<usize> = vec![1, 2];
|
||||||
let mut frame_new = frame.clone();
|
let mut frame_new = frame.clone();
|
||||||
frame_new.filter_coords(&filter);
|
frame_new.filter_coords(&filter);
|
||||||
assert!(frame_new.num_atoms as usize == filter.len());
|
assert!(frame_new.num_atoms as usize == filter.len());
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
use crate::*;
|
|
||||||
use crate::c_abi::xdrfile::exdrENDOFFILE;
|
use crate::c_abi::xdrfile::exdrENDOFFILE;
|
||||||
|
use crate::*;
|
||||||
use failure::Error;
|
use failure::Error;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@@ -13,7 +13,7 @@ impl IntoIterator for XTCTrajectory {
|
|||||||
XTCTrajectoryIterator {
|
XTCTrajectoryIterator {
|
||||||
trajectory: self,
|
trajectory: self,
|
||||||
item: Rc::new(Frame::with_capacity(num_atoms)),
|
item: Rc::new(Frame::with_capacity(num_atoms)),
|
||||||
has_error: false
|
has_error: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,18 +32,19 @@ pub struct XTCTrajectoryIterator {
|
|||||||
impl Iterator for XTCTrajectoryIterator {
|
impl Iterator for XTCTrajectoryIterator {
|
||||||
type Item = Result<Rc<Frame>, Error>;
|
type Item = Result<Rc<Frame>, Error>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> { // Reuse old frame
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
// Reuse old frame
|
||||||
if self.has_error {
|
if self.has_error {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let item: &mut Frame = match Rc::get_mut(&mut self.item) {
|
let item: &mut Frame = match Rc::get_mut(&mut self.item) {
|
||||||
Some(item) => item,
|
Some(item) => item,
|
||||||
None => { // caller kept frame. Create new one
|
None => {
|
||||||
|
// caller kept frame. Create new one
|
||||||
self.item = Rc::new(Frame::with_capacity(self.item.num_atoms));
|
self.item = Rc::new(Frame::with_capacity(self.item.num_atoms));
|
||||||
Rc::get_mut(&mut self.item).unwrap()
|
Rc::get_mut(&mut self.item).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
match self.trajectory.read(item) {
|
match self.trajectory.read(item) {
|
||||||
Ok(()) => Some(Ok(Rc::clone(&self.item))),
|
Ok(()) => Some(Ok(Rc::clone(&self.item))),
|
||||||
@@ -69,7 +70,7 @@ impl IntoIterator for TRRTrajectory {
|
|||||||
TRRTrajectoryIterator {
|
TRRTrajectoryIterator {
|
||||||
trajectory: self,
|
trajectory: self,
|
||||||
item: Rc::new(Frame::with_capacity(num_atoms)),
|
item: Rc::new(Frame::with_capacity(num_atoms)),
|
||||||
has_error: false
|
has_error: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -88,18 +89,19 @@ pub struct TRRTrajectoryIterator {
|
|||||||
impl Iterator for TRRTrajectoryIterator {
|
impl Iterator for TRRTrajectoryIterator {
|
||||||
type Item = Result<Rc<Frame>, Error>;
|
type Item = Result<Rc<Frame>, Error>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> { // Reuse old frame
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
|
// Reuse old frame
|
||||||
if self.has_error {
|
if self.has_error {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let item: &mut Frame = match Rc::get_mut(&mut self.item) {
|
let item: &mut Frame = match Rc::get_mut(&mut self.item) {
|
||||||
Some(item) => item,
|
Some(item) => item,
|
||||||
None => { // caller kept frame. Create new one
|
None => {
|
||||||
|
// caller kept frame. Create new one
|
||||||
self.item = Rc::new(Frame::with_capacity(self.item.num_atoms));
|
self.item = Rc::new(Frame::with_capacity(self.item.num_atoms));
|
||||||
Rc::get_mut(&mut self.item).unwrap()
|
Rc::get_mut(&mut self.item).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
match self.trajectory.read(item) {
|
match self.trajectory.read(item) {
|
||||||
Ok(()) => Some(Ok(Rc::clone(&self.item))),
|
Ok(()) => Some(Ok(Rc::clone(&self.item))),
|
||||||
|
|||||||
123
src/lib.rs
123
src/lib.rs
@@ -58,33 +58,33 @@
|
|||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
#[macro_use] extern crate assert_approx_eq;
|
#[macro_use]
|
||||||
|
extern crate assert_approx_eq;
|
||||||
extern crate lazy_init;
|
extern crate lazy_init;
|
||||||
|
|
||||||
|
pub mod c_abi;
|
||||||
mod frame;
|
mod frame;
|
||||||
mod iterator;
|
mod iterator;
|
||||||
pub mod c_abi;
|
|
||||||
pub use frame::Frame;
|
pub use frame::Frame;
|
||||||
pub use iterator::*;
|
pub use iterator::*;
|
||||||
|
|
||||||
|
use c_abi::xdr_seek;
|
||||||
use c_abi::xdrfile;
|
use c_abi::xdrfile;
|
||||||
use c_abi::xdrfile::XDRFILE;
|
use c_abi::xdrfile::XDRFILE;
|
||||||
use c_abi::xdr_seek;
|
|
||||||
use c_abi::xdrfile_xtc;
|
|
||||||
use c_abi::xdrfile_trr;
|
use c_abi::xdrfile_trr;
|
||||||
|
use c_abi::xdrfile_xtc;
|
||||||
|
|
||||||
|
use failure::{err_msg, Error};
|
||||||
|
use lazy_init::Lazy;
|
||||||
|
use std::cell::Cell;
|
||||||
use std::ffi::CString;
|
use std::ffi::CString;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
use failure::{Error,err_msg};
|
|
||||||
use std::cell::Cell;
|
|
||||||
use lazy_init::Lazy;
|
|
||||||
|
|
||||||
pub enum FileMode {
|
pub enum FileMode {
|
||||||
Write,
|
Write,
|
||||||
Append,
|
Append,
|
||||||
Read
|
Read,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl FileMode {
|
impl FileMode {
|
||||||
@@ -99,7 +99,7 @@ impl FileMode {
|
|||||||
|
|
||||||
fn path_to_cstring(path: &Path) -> CString {
|
fn path_to_cstring(path: &Path) -> CString {
|
||||||
CString::new(path.to_str().unwrap()).unwrap()
|
CString::new(path.to_str().unwrap()).unwrap()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// A safe wrapper around the c implementation of an XDRFile
|
/// A safe wrapper around the c implementation of an XDRFile
|
||||||
struct XDRFile {
|
struct XDRFile {
|
||||||
@@ -109,7 +109,6 @@ struct XDRFile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl XDRFile {
|
impl XDRFile {
|
||||||
|
|
||||||
pub fn open(path: &Path, filemode: FileMode) -> Result<XDRFile, Error> {
|
pub fn open(path: &Path, filemode: FileMode) -> Result<XDRFile, Error> {
|
||||||
let path_p = path_to_cstring(path).into_raw();
|
let path_p = path_to_cstring(path).into_raw();
|
||||||
let mode_p = CString::new(filemode.value()).unwrap().into_raw();
|
let mode_p = CString::new(filemode.value()).unwrap().into_raw();
|
||||||
@@ -117,10 +116,15 @@ impl XDRFile {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let xdrfile = xdrfile::xdrfile_open(path_p, mode_p);
|
let xdrfile = xdrfile::xdrfile_open(path_p, mode_p);
|
||||||
|
|
||||||
if ! xdrfile.is_null() {
|
if !xdrfile.is_null() {
|
||||||
let path = String::from(path.to_str().unwrap());
|
let path = String::from(path.to_str().unwrap());
|
||||||
Ok(XDRFile { xdrfile, filemode, path })
|
Ok(XDRFile {
|
||||||
} else { // Something went wrong. But the C api does not tell us what
|
xdrfile,
|
||||||
|
filemode,
|
||||||
|
path,
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
// Something went wrong. But the C api does not tell us what
|
||||||
Err(err_msg("Failed to open trajectory file"))
|
Err(err_msg("Failed to open trajectory file"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -138,7 +142,6 @@ impl Drop for XDRFile {
|
|||||||
|
|
||||||
/// The trajectory trait defines shared methods for xtc and trr trajectories
|
/// The trajectory trait defines shared methods for xtc and trr trajectories
|
||||||
pub trait Trajectory {
|
pub trait Trajectory {
|
||||||
|
|
||||||
/// Read the next step of the trajectory into the frame object
|
/// Read the next step of the trajectory into the frame object
|
||||||
fn read(&mut self, frame: &mut Frame) -> Result<(), Error>;
|
fn read(&mut self, frame: &mut Frame) -> Result<(), Error>;
|
||||||
|
|
||||||
@@ -156,25 +159,29 @@ pub trait Trajectory {
|
|||||||
pub struct XTCTrajectory {
|
pub struct XTCTrajectory {
|
||||||
handle: XDRFile,
|
handle: XDRFile,
|
||||||
precision: Cell<f32>, // internal mutability required for read method
|
precision: Cell<f32>, // internal mutability required for read method
|
||||||
num_atoms: Lazy<Result<u32, Error>>
|
num_atoms: Lazy<Result<u32, Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl XTCTrajectory {
|
impl XTCTrajectory {
|
||||||
pub fn open(path: &Path, filemode: FileMode) -> Result<XTCTrajectory, Error> {
|
pub fn open(path: &Path, filemode: FileMode) -> Result<XTCTrajectory, Error> {
|
||||||
let xdr = XDRFile::open(path, filemode)?;
|
let xdr = XDRFile::open(path, filemode)?;
|
||||||
Ok(XTCTrajectory { handle: xdr, precision: Cell::new(1000.0), num_atoms: Lazy::new() })
|
Ok(XTCTrajectory {
|
||||||
|
handle: xdr,
|
||||||
|
precision: Cell::new(1000.0),
|
||||||
|
num_atoms: Lazy::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Trajectory for XTCTrajectory {
|
impl Trajectory for XTCTrajectory {
|
||||||
|
|
||||||
fn read(&mut self, frame: &mut Frame) -> Result<(), Error> {
|
fn read(&mut self, frame: &mut Frame) -> Result<(), Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
// C lib requires an i32 to be passed, but step is exposed it as u32
|
// C lib requires an i32 to be passed, but step is exposed it as u32
|
||||||
// (A step cannot be negative, can it?). So we need to create a step
|
// (A step cannot be negative, can it?). So we need to create a step
|
||||||
// variable to pass to read_xtc and cast it afterwards to u32
|
// variable to pass to read_xtc and cast it afterwards to u32
|
||||||
let mut step: i32 = 0;
|
let mut step: i32 = 0;
|
||||||
let code = xdrfile_xtc::read_xtc(self.handle.xdrfile,
|
let code = xdrfile_xtc::read_xtc(
|
||||||
|
self.handle.xdrfile,
|
||||||
frame.num_atoms as i32,
|
frame.num_atoms as i32,
|
||||||
&mut step,
|
&mut step,
|
||||||
&mut frame.time,
|
&mut frame.time,
|
||||||
@@ -185,23 +192,31 @@ impl Trajectory for XTCTrajectory {
|
|||||||
frame.step = step as u32;
|
frame.step = step as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to read trajectory. Error code: {}", code))),
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to read trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, frame: &Frame) -> Result<(), Error> {
|
fn write(&mut self, frame: &Frame) -> Result<(), Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let code = xdrfile_xtc::write_xtc(self.handle.xdrfile,
|
let code = xdrfile_xtc::write_xtc(
|
||||||
|
self.handle.xdrfile,
|
||||||
frame.num_atoms as i32,
|
frame.num_atoms as i32,
|
||||||
frame.step as i32,
|
frame.step as i32,
|
||||||
frame.time,
|
frame.time,
|
||||||
frame.box_vector.as_ptr() as *mut [[f32; 3]; 3],
|
frame.box_vector.as_ptr() as *mut [[f32; 3]; 3],
|
||||||
frame.coords[..].as_ptr() as *mut [f32; 3],
|
frame.coords[..].as_ptr() as *mut [f32; 3],
|
||||||
1000.0) as u32;
|
1000.0,
|
||||||
|
) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to write trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to write trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -211,7 +226,10 @@ impl Trajectory for XTCTrajectory {
|
|||||||
let code = xdr_seek::xdr_flush(self.handle.xdrfile) as u32;
|
let code = xdr_seek::xdr_flush(self.handle.xdrfile) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to flush trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to flush trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -222,37 +240,42 @@ impl Trajectory for XTCTrajectory {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let path = CString::new(self.handle.path.as_str()).unwrap();
|
let path = CString::new(self.handle.path.as_str()).unwrap();
|
||||||
let path_p = path.into_raw();
|
let path_p = path.into_raw();
|
||||||
let code = xdrfile_xtc::read_xtc_natoms(path_p, &mut num_atoms as *const i32) as u32;
|
let code =
|
||||||
|
xdrfile_xtc::read_xtc_natoms(path_p, &mut num_atoms as *const i32) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(num_atoms as u32),
|
xdrfile::exdrOK => Ok(num_atoms as u32),
|
||||||
_ => Err(err_msg(format!("Failed to read atom number from trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to read atom number from trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
match result {
|
match result {
|
||||||
Ok(val) => Ok(*val),
|
Ok(val) => Ok(*val),
|
||||||
// ugly hack because failure::Error is not "Clone"
|
// ugly hack because failure::Error is not "Clone"
|
||||||
Err(err) => Err(err_msg(format!("{}", err)))
|
Err(err) => Err(err_msg(format!("{}", err))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/// Read/Write TRR Trajectories
|
/// Read/Write TRR Trajectories
|
||||||
pub struct TRRTrajectory {
|
pub struct TRRTrajectory {
|
||||||
handle: XDRFile,
|
handle: XDRFile,
|
||||||
num_atoms: Lazy<Result<u32, Error>>
|
num_atoms: Lazy<Result<u32, Error>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TRRTrajectory {
|
impl TRRTrajectory {
|
||||||
pub fn open(path: &Path, filemode: FileMode) -> Result<TRRTrajectory, Error> {
|
pub fn open(path: &Path, filemode: FileMode) -> Result<TRRTrajectory, Error> {
|
||||||
let xdr = XDRFile::open(path, filemode)?;
|
let xdr = XDRFile::open(path, filemode)?;
|
||||||
Ok(TRRTrajectory { handle: xdr, num_atoms: Lazy::new() })
|
Ok(TRRTrajectory {
|
||||||
|
handle: xdr,
|
||||||
|
num_atoms: Lazy::new(),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Trajectory for TRRTrajectory {
|
impl Trajectory for TRRTrajectory {
|
||||||
|
|
||||||
fn read(&mut self, frame: &mut Frame) -> Result<(), Error> {
|
fn read(&mut self, frame: &mut Frame) -> Result<(), Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
// C lib requires an i32 to be passed, but step is exposed it as u32
|
// C lib requires an i32 to be passed, but step is exposed it as u32
|
||||||
@@ -261,7 +284,8 @@ impl Trajectory for TRRTrajectory {
|
|||||||
// Similar for lambda.
|
// Similar for lambda.
|
||||||
let mut step: i32 = 0;
|
let mut step: i32 = 0;
|
||||||
let mut lambda: f32 = 0.0;
|
let mut lambda: f32 = 0.0;
|
||||||
let code = xdrfile_trr::read_trr(self.handle.xdrfile,
|
let code = xdrfile_trr::read_trr(
|
||||||
|
self.handle.xdrfile,
|
||||||
frame.num_atoms as i32,
|
frame.num_atoms as i32,
|
||||||
&mut step,
|
&mut step,
|
||||||
&mut frame.time,
|
&mut frame.time,
|
||||||
@@ -269,19 +293,23 @@ impl Trajectory for TRRTrajectory {
|
|||||||
&mut frame.box_vector,
|
&mut frame.box_vector,
|
||||||
frame.coords.as_ptr() as *mut [f32; 3],
|
frame.coords.as_ptr() as *mut [f32; 3],
|
||||||
std::ptr::null_mut(),
|
std::ptr::null_mut(),
|
||||||
std::ptr::null_mut()
|
std::ptr::null_mut(),
|
||||||
) as u32;
|
) as u32;
|
||||||
frame.step = step as u32;
|
frame.step = step as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to read trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to read trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write(&mut self, frame: &Frame) -> Result<(), Error> {
|
fn write(&mut self, frame: &Frame) -> Result<(), Error> {
|
||||||
unsafe {
|
unsafe {
|
||||||
let code = xdrfile_trr::write_trr(self.handle.xdrfile,
|
let code = xdrfile_trr::write_trr(
|
||||||
|
self.handle.xdrfile,
|
||||||
frame.num_atoms as i32,
|
frame.num_atoms as i32,
|
||||||
frame.step as i32,
|
frame.step as i32,
|
||||||
frame.time,
|
frame.time,
|
||||||
@@ -289,10 +317,14 @@ impl Trajectory for TRRTrajectory {
|
|||||||
frame.box_vector.as_ptr() as *mut [[f32; 3]; 3],
|
frame.box_vector.as_ptr() as *mut [[f32; 3]; 3],
|
||||||
frame.coords[..].as_ptr() as *mut [f32; 3],
|
frame.coords[..].as_ptr() as *mut [f32; 3],
|
||||||
std::ptr::null_mut(),
|
std::ptr::null_mut(),
|
||||||
std::ptr::null_mut()) as u32;
|
std::ptr::null_mut(),
|
||||||
|
) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to write trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to write trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -302,7 +334,10 @@ impl Trajectory for TRRTrajectory {
|
|||||||
let code = xdr_seek::xdr_flush(self.handle.xdrfile) as u32;
|
let code = xdr_seek::xdr_flush(self.handle.xdrfile) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(()),
|
xdrfile::exdrOK => Ok(()),
|
||||||
_ => Err(err_msg(format!("Failed to flush trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to flush trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -313,22 +348,25 @@ impl Trajectory for TRRTrajectory {
|
|||||||
unsafe {
|
unsafe {
|
||||||
let path = CString::new(self.handle.path.as_str()).unwrap();
|
let path = CString::new(self.handle.path.as_str()).unwrap();
|
||||||
let path_p = path.into_raw();
|
let path_p = path.into_raw();
|
||||||
let code = xdrfile_trr::read_trr_natoms(path_p, &mut num_atoms as *const i32) as u32;
|
let code =
|
||||||
|
xdrfile_trr::read_trr_natoms(path_p, &mut num_atoms as *const i32) as u32;
|
||||||
match code {
|
match code {
|
||||||
xdrfile::exdrOK => Ok(num_atoms as u32),
|
xdrfile::exdrOK => Ok(num_atoms as u32),
|
||||||
_ => Err(err_msg(format!("Failed to read atom number from trajectory. Error code: {}", code)))
|
_ => Err(err_msg(format!(
|
||||||
|
"Failed to read atom number from trajectory. Error code: {}",
|
||||||
|
code
|
||||||
|
))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
match result {
|
match result {
|
||||||
Ok(val) => Ok(*val),
|
Ok(val) => Ok(*val),
|
||||||
// ugly hack because failure::Error is not "Clone"
|
// ugly hack because failure::Error is not "Clone"
|
||||||
Err(err) => Err(err_msg(format!("{}", err)))
|
Err(err) => Err(err_msg(format!("{}", err))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
|
||||||
@@ -413,4 +451,3 @@ mod tests {
|
|||||||
assert_eq!(new_frame.coords, frame.coords);
|
assert_eq!(new_frame.coords, frame.coords);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,9 +1,9 @@
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod integration {
|
mod integration {
|
||||||
|
|
||||||
|
use std::path::Path;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use xdrfile::*;
|
use xdrfile::*;
|
||||||
use std::path::Path;
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_use_library() {
|
fn test_use_library() {
|
||||||
@@ -25,8 +25,7 @@ mod integration {
|
|||||||
let trj = XTCTrajectory::open(path, FileMode::Read).unwrap();
|
let trj = XTCTrajectory::open(path, FileMode::Read).unwrap();
|
||||||
let frames: Vec<Rc<Frame>> = trj.into_iter().filter_map(Result::ok).collect();
|
let frames: Vec<Rc<Frame>> = trj.into_iter().filter_map(Result::ok).collect();
|
||||||
for (idx, frame) in frames.iter().enumerate() {
|
for (idx, frame) in frames.iter().enumerate() {
|
||||||
assert_eq!(frame.step as usize, idx+1);
|
assert_eq!(frame.step as usize, idx + 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user