This commit is contained in:
daniel
2020-04-16 13:03:33 +02:00
commit 43d2d74468
22 changed files with 5437 additions and 0 deletions

5
src/c_abi/mod.rs Normal file
View File

@@ -0,0 +1,5 @@
#[allow(non_upper_case_globals)]
pub mod xdrfile;
pub mod xdrfile_trr;
pub mod xdrfile_xtc;
pub mod xdr_seek;

160
src/c_abi/xdr_seek.rs Normal file
View File

@@ -0,0 +1,160 @@
use super::xdrfile::*;
pub const _STDINT_H: u32 = 1;
pub const _FEATURES_H: u32 = 1;
pub const _DEFAULT_SOURCE: u32 = 1;
pub const __USE_ISOC11: u32 = 1;
pub const __USE_ISOC99: u32 = 1;
pub const __USE_ISOC95: u32 = 1;
pub const __USE_POSIX_IMPLICITLY: u32 = 1;
pub const _POSIX_SOURCE: u32 = 1;
pub const _POSIX_C_SOURCE: u32 = 200809;
pub const __USE_POSIX: u32 = 1;
pub const __USE_POSIX2: u32 = 1;
pub const __USE_POSIX199309: u32 = 1;
pub const __USE_POSIX199506: u32 = 1;
pub const __USE_XOPEN2K: u32 = 1;
pub const __USE_XOPEN2K8: u32 = 1;
pub const _ATFILE_SOURCE: u32 = 1;
pub const __USE_MISC: u32 = 1;
pub const __USE_ATFILE: u32 = 1;
pub const __USE_FORTIFY_LEVEL: u32 = 0;
pub const _STDC_PREDEF_H: u32 = 1;
pub const __STDC_IEC_559__: u32 = 1;
pub const __STDC_IEC_559_COMPLEX__: u32 = 1;
pub const __STDC_ISO_10646__: u32 = 201605;
pub const __STDC_NO_THREADS__: u32 = 1;
pub const __GNU_LIBRARY__: u32 = 6;
pub const __GLIBC__: u32 = 2;
pub const __GLIBC_MINOR__: u32 = 24;
pub const _SYS_CDEFS_H: u32 = 1;
pub const __WORDSIZE: u32 = 64;
pub const __WORDSIZE_TIME64_COMPAT32: u32 = 1;
pub const __SYSCALL_WORDSIZE: u32 = 64;
pub const _BITS_WCHAR_H: u32 = 1;
pub const INT8_MIN: i32 = -128;
pub const INT16_MIN: i32 = -32768;
pub const INT32_MIN: i32 = -2147483648;
pub const INT8_MAX: u32 = 127;
pub const INT16_MAX: u32 = 32767;
pub const INT32_MAX: u32 = 2147483647;
pub const UINT8_MAX: u32 = 255;
pub const UINT16_MAX: u32 = 65535;
pub const UINT32_MAX: u32 = 4294967295;
pub const INT_LEAST8_MIN: i32 = -128;
pub const INT_LEAST16_MIN: i32 = -32768;
pub const INT_LEAST32_MIN: i32 = -2147483648;
pub const INT_LEAST8_MAX: u32 = 127;
pub const INT_LEAST16_MAX: u32 = 32767;
pub const INT_LEAST32_MAX: u32 = 2147483647;
pub const UINT_LEAST8_MAX: u32 = 255;
pub const UINT_LEAST16_MAX: u32 = 65535;
pub const UINT_LEAST32_MAX: u32 = 4294967295;
pub const INT_FAST8_MIN: i32 = -128;
pub const INT_FAST16_MIN: i64 = -9223372036854775808;
pub const INT_FAST32_MIN: i64 = -9223372036854775808;
pub const INT_FAST8_MAX: u32 = 127;
pub const INT_FAST16_MAX: u64 = 9223372036854775807;
pub const INT_FAST32_MAX: u64 = 9223372036854775807;
pub const UINT_FAST8_MAX: u32 = 255;
pub const UINT_FAST16_MAX: i32 = -1;
pub const UINT_FAST32_MAX: i32 = -1;
pub const INTPTR_MIN: i64 = -9223372036854775808;
pub const INTPTR_MAX: u64 = 9223372036854775807;
pub const UINTPTR_MAX: i32 = -1;
pub const PTRDIFF_MIN: i64 = -9223372036854775808;
pub const PTRDIFF_MAX: u64 = 9223372036854775807;
pub const SIG_ATOMIC_MIN: i32 = -2147483648;
pub const SIG_ATOMIC_MAX: u32 = 2147483647;
pub const SIZE_MAX: i32 = -1;
pub const WINT_MIN: u32 = 0;
pub const WINT_MAX: u32 = 4294967295;
pub const DIM: u32 = 3;
pub type int_least8_t = ::std::os::raw::c_schar;
pub type int_least16_t = ::std::os::raw::c_short;
pub type int_least32_t = ::std::os::raw::c_int;
pub type int_least64_t = ::std::os::raw::c_long;
pub type uint_least8_t = ::std::os::raw::c_uchar;
pub type uint_least16_t = ::std::os::raw::c_ushort;
pub type uint_least32_t = ::std::os::raw::c_uint;
pub type uint_least64_t = ::std::os::raw::c_ulong;
pub type int_fast8_t = ::std::os::raw::c_schar;
pub type int_fast16_t = ::std::os::raw::c_long;
pub type int_fast32_t = ::std::os::raw::c_long;
pub type int_fast64_t = ::std::os::raw::c_long;
pub type uint_fast8_t = ::std::os::raw::c_uchar;
pub type uint_fast16_t = ::std::os::raw::c_ulong;
pub type uint_fast32_t = ::std::os::raw::c_ulong;
pub type uint_fast64_t = ::std::os::raw::c_ulong;
pub type intmax_t = ::std::os::raw::c_long;
pub type uintmax_t = ::std::os::raw::c_ulong;
extern "C" {
pub fn xdr_tell(xd: *mut XDRFILE) -> i64;
}
extern "C" {
pub fn xdr_seek(
xd: *mut XDRFILE,
pos: i64,
whence: ::std::os::raw::c_int,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn xdr_flush(xd: *mut XDRFILE) -> ::std::os::raw::c_int;
}
#[cfg(test)]
mod tests {
use super::*;
use super::super::xdrfile_xtc::*;
use std::ffi::CString;
#[test]
fn test_xdr_tell() {
let path = CString::new("tests/1l2y.xtc").unwrap();
let num_atoms = 304;
let mut time: f32 = 2.0;
let mut step: i32 = 5;
let box_vec = [[0.0; 3]; 3];
let x_p = std::ptr::null_mut();
let mut prec: f32 = 0.0;
unsafe {
let mode = CString::new("r").unwrap();
let xdr = xdrfile_open(path.as_ptr(), mode.as_ptr());
assert!(!xdr.is_null());
let tell = xdr_tell(xdr);
assert!(tell == 0, "{}", tell);
read_xtc(xdr, num_atoms, &mut step, &mut time,
box_vec.as_ptr() as *mut Matrix,
x_p,
&mut prec);
let tell = xdr_tell(xdr);
assert!(tell > 0, "{}", tell);
}
}
#[test]
fn test_xdr_seek() {
let path = CString::new("tests/1l2y.xtc").unwrap();
unsafe {
let mode = CString::new("r").unwrap();
let xdr = xdrfile_open(path.as_ptr(), mode.as_ptr());
assert!(!xdr.is_null());
let tell = xdr_tell(xdr);
assert!(tell == 0, "{}", tell);
xdr_seek(xdr, 500, 0);
let tell = xdr_tell(xdr);
assert!(tell == 500, "{}", tell);
}
}
}

520
src/c_abi/xdrfile.rs Normal file
View File

@@ -0,0 +1,520 @@
pub const DIM: u32 = 3;
#[repr(C)]
#[derive(Debug, Copy, Clone)]
pub struct XDRFILE {
_unused: [u8; 0],
}
pub type BindgenTy1 = u32;
pub const exdrOK: BindgenTy1 = 0;
pub const exdrHEADER: BindgenTy1 = 1;
pub const exdrSTRING: BindgenTy1 = 2;
pub const exdrDOUBLE: BindgenTy1 = 3;
pub const exdrINT: BindgenTy1 = 4;
pub const exdrFLOAT: BindgenTy1 = 5;
pub const exdrUINT: BindgenTy1 = 6;
pub const exdr3DX: BindgenTy1 = 7;
pub const exdrCLOSE: BindgenTy1 = 8;
pub const exdrMAGIC: BindgenTy1 = 9;
pub const exdrNOMEM: BindgenTy1 = 10;
pub const exdrENDOFFILE: BindgenTy1 = 11;
pub const exdrFILENOTFOUND: BindgenTy1 = 12;
pub const exdrNR: BindgenTy1 = 13;
extern "C" {
pub static mut exdr_message: [*mut ::std::os::raw::c_char; 13usize];
}
pub type Matrix = [[::std::os::raw::c_float; 3usize]; 3usize];
pub type Rvec = [::std::os::raw::c_float; 3usize];
pub type Mybool = ::std::os::raw::c_int;
extern "C" {
#[doc = " \\brief Open a portable binary file, just like fopen()"]
#[doc = ""]
#[doc = " Use this routine much like calls to the standard library function"]
#[doc = " fopen(). The only difference is that the returned pointer should only"]
#[doc = " be used with routines defined in this header."]
#[doc = ""]
#[doc = " \\param path Full or relative path (including name) of the file"]
#[doc = " \\param mode \"r\" for reading, \"w\" for writing, \"a\" for append."]
#[doc = ""]
#[doc = " \\return Pointer to abstract xdr file datatype, or NULL if an error occurs."]
#[doc = ""]
pub fn xdrfile_open(
path: *const ::std::os::raw::c_char,
mode: *const ::std::os::raw::c_char,
) -> *mut XDRFILE;
}
extern "C" {
#[doc = " \\brief Close a previously opened portable binary file, just like fclose()"]
#[doc = ""]
#[doc = " Use this routine much like calls to the standard library function"]
#[doc = " fopen(). The only difference is that it is used for an XDRFILE handle"]
#[doc = " instead of a FILE handle."]
#[doc = ""]
#[doc = " \\param xfp Pointer to an abstract XDRFILE datatype"]
#[doc = ""]
#[doc = " \\return 0 on success, non-zero on error."]
pub fn xdrfile_close(xfp: *mut XDRFILE) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a char type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of characters to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of characters read"]
pub fn xdrfile_read_char(
ptr: *mut ::std::os::raw::c_char,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a characters type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of characters to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of characters written"]
pub fn xdrfile_write_char(
ptr: *mut ::std::os::raw::c_char,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a unsigned \\a char type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of unsigned characters to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned characters read"]
pub fn xdrfile_read_uchar(
ptr: *mut ::std::os::raw::c_uchar,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a unsigned \\a characters type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of unsigned characters to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned characters written"]
pub fn xdrfile_write_uchar(
ptr: *mut ::std::os::raw::c_uchar,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a short type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of shorts to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of shorts read"]
pub fn xdrfile_read_short(
ptr: *mut ::std::os::raw::c_short,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a short type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of shorts to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of shorts written"]
pub fn xdrfile_write_short(
ptr: *mut ::std::os::raw::c_short,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a unsigned \\a short type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of unsigned shorts to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned shorts read"]
pub fn xdrfile_read_ushort(
ptr: *mut ::std::os::raw::c_ushort,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a unsigned \\a short type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of unsigned shorts to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned shorts written"]
pub fn xdrfile_write_ushort(
ptr: *mut ::std::os::raw::c_ushort,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a integer type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of integers to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of integers read"]
#[doc = ""]
#[doc = " The integer data type is assumed to be less than or equal to 32 bits."]
#[doc = ""]
#[doc = " We do not provide any routines for reading/writing 64-bit integers, since"]
#[doc = " - Not all XDR implementations support it"]
#[doc = " - Not all machines have 64-bit integers"]
#[doc = ""]
#[doc = " Split your 64-bit data into two 32-bit integers for portability!"]
pub fn xdrfile_read_int(
ptr: *mut ::std::os::raw::c_int,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a integer type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of integers to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of integers written"]
#[doc = ""]
#[doc = " The integer data type is assumed to be less than or equal to 32 bits."]
#[doc = ""]
#[doc = " We do not provide any routines for reading/writing 64-bit integers, since"]
#[doc = " - Not all XDR implementations support it"]
#[doc = " - Not all machines have 64-bit integers"]
#[doc = ""]
#[doc = " Split your 64-bit data into two 32-bit integers for portability!"]
pub fn xdrfile_write_int(
ptr: *mut ::std::os::raw::c_int,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a unsigned \\a integers type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of unsigned integers to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned integers read"]
#[doc = ""]
#[doc = " The integer data type is assumed to be less than or equal to 32 bits."]
#[doc = ""]
#[doc = " We do not provide any routines for reading/writing 64-bit integers, since"]
#[doc = " - Not all XDR implementations support it"]
#[doc = " - Not all machines have 64-bit integers"]
#[doc = ""]
#[doc = " Split your 64-bit data into two 32-bit integers for portability!"]
pub fn xdrfile_read_uint(
ptr: *mut ::std::os::raw::c_uint,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a unsigned \\a integer type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of unsigned integers to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of unsigned integers written"]
#[doc = ""]
#[doc = " The integer data type is assumed to be less than or equal to 32 bits."]
#[doc = ""]
#[doc = " We do not provide any routines for reading/writing 64-bit integers, since"]
#[doc = " - Not all XDR implementations support it"]
#[doc = " - Not all machines have 64-bit integers"]
#[doc = ""]
#[doc = " Split your 64-bit data into two 32-bit integers for portability!"]
pub fn xdrfile_write_uint(
ptr: *mut ::std::os::raw::c_uint,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a float type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of floats to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of floats read"]
pub fn xdrfile_read_float(
ptr: *mut ::std::os::raw::c_float,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a float type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of floats to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of floats written"]
pub fn xdrfile_write_float(
ptr: *mut ::std::os::raw::c_float,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read one or more \\a double type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param ndata Number of doubles to read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of doubles read"]
pub fn xdrfile_read_double(
ptr: *mut ::std::os::raw::c_double,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write one or more \\a double type variable(s)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param ndata Number of double to write."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of doubles written"]
pub fn xdrfile_write_double(
ptr: *mut ::std::os::raw::c_double,
ndata: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read a string (array of characters)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param maxlen Maximum length of string. If no end-of-string is encountered,"]
#[doc = " one byte less than this is read and end-of-string appended."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of characters read, including end-of-string"]
pub fn xdrfile_read_string(
ptr: *mut ::std::os::raw::c_char,
maxlen: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write a string (array of characters)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of characters written, including end-of-string"]
pub fn xdrfile_write_string(
ptr: *mut ::std::os::raw::c_char,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Read raw bytes from file (unknown datatype)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be written"]
#[doc = " \\param nbytes Number of bytes to read. No conversion whatsoever is done."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of bytes read from file"]
pub fn xdrfile_read_opaque(
ptr: *mut ::std::os::raw::c_char,
nbytes: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Write raw bytes to file (unknown datatype)"]
#[doc = ""]
#[doc = " \\param ptr Pointer to memory where data should be read"]
#[doc = " \\param nbytes Number of bytes to write. No conversion whatsoever is done."]
#[doc = " \\param xfp Handle to portable binary file, created with xdrfile_open()"]
#[doc = ""]
#[doc = " \\return Number of bytes written to file"]
pub fn xdrfile_write_opaque(
ptr: *mut ::std::os::raw::c_char,
nbytes: ::std::os::raw::c_int,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Compress coordiates in a float array to XDR file"]
#[doc = ""]
#[doc = " This routine will perform \\a lossy compression on the three-dimensional"]
#[doc = " coordinate data data specified and store it in the XDR file."]
#[doc = ""]
#[doc = " The lossy part of the compression consists of multiplying each"]
#[doc = " coordinate with the precision argument and then rounding to integers."]
#[doc = " We suggest a default value of 1000.0, which means you are guaranteed"]
#[doc = " three decimals of accuracy. The only limitation is that scaled coordinates"]
#[doc = " must still fit in an integer variable, so if the precision is 1000.0 the"]
#[doc = " coordinate magnitudes must be less than +-2e6."]
#[doc = ""]
#[doc = " \\param ptr Pointer to coordinates to compress (length 3*ncoord)"]
#[doc = " \\param ncoord Number of coordinate triplets in data"]
#[doc = " \\param precision Scaling factor for lossy compression. If it is <=0,"]
#[doc = " the default value of 1000.0 is used."]
#[doc = " \\param xfp Handle to portably binary file"]
#[doc = ""]
#[doc = " \\return Number of coordinate triplets written."]
#[doc = " IMPORTANT: Check that this is equal to ncoord - if it is"]
#[doc = " negative, an error occured. This should not happen with"]
#[doc = "\t \t normal data, but if your coordinates are NaN or very"]
#[doc = " large (>1e6) it is not possible to use the compression."]
#[doc = ""]
#[doc = " \\warning The compression algorithm is not part of the XDR standard,"]
#[doc = " and very complicated, so you will need this xdrfile module"]
#[doc = " to read it later."]
pub fn xdrfile_compress_coord_float(
ptr: *mut ::std::os::raw::c_float,
ncoord: ::std::os::raw::c_int,
precision: ::std::os::raw::c_float,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Decompress coordiates from XDR file to array of floats"]
#[doc = ""]
#[doc = " This routine will decompress three-dimensional coordinate data previously"]
#[doc = " stored in an XDR file and store it in the specified array of floats."]
#[doc = ""]
#[doc = " The precision used during the earlier compression is read from the file"]
#[doc = " and returned - you cannot adjust the accuracy at this stage."]
#[doc = ""]
#[doc = " \\param ptr Pointer to coordinates to compress (length>= 3*ncoord)"]
#[doc = " \\param ncoord Max number of coordinate triplets to read on input, actual"]
#[doc = " number of coordinate triplets read on return. If this"]
#[doc = " is smaller than the number of coordinates in the frame an"]
#[doc = " error will occur."]
#[doc = " \\param precision The precision used in the previous compression will be"]
#[doc = " written to this variable on return."]
#[doc = " \\param xfp Handle to portably binary file"]
#[doc = ""]
#[doc = " \\return Number of coordinate triplets read. If this is negative,"]
#[doc = " an error occured."]
#[doc = ""]
#[doc = " \\warning Since we cannot count on being able to set/get the"]
#[doc = " position of large files (>2Gb), it is not possible to"]
#[doc = " recover from errors by re-reading the frame if the"]
#[doc = " storage area you provided was too small. To avoid this"]
#[doc = " from happening, we recommend that you store the number of"]
#[doc = " coordinates triplet as an integer either in a header or"]
#[doc = " just before the compressed coordinate data, so you can"]
#[doc = " read it first and allocated enough memory."]
pub fn xdrfile_decompress_coord_float(
ptr: *mut ::std::os::raw::c_float,
ncoord: *mut ::std::os::raw::c_int,
precision: *mut ::std::os::raw::c_float,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Compress coordiates in a double array to XDR file"]
#[doc = ""]
#[doc = " This routine will perform \\a lossy compression on the three-dimensional"]
#[doc = " coordinate data data specified and store it in the XDR file. Double will"]
#[doc = " NOT give you any extra precision since the coordinates are compressed. This"]
#[doc = " routine just avoids allocating a temporary array of floats."]
#[doc = ""]
#[doc = " The lossy part of the compression consists of multiplying each"]
#[doc = " coordinate with the precision argument and then rounding to integers."]
#[doc = " We suggest a default value of 1000.0, which means you are guaranteed"]
#[doc = " three decimals of accuracy. The only limitation is that scaled coordinates"]
#[doc = " must still fit in an integer variable, so if the precision is 1000.0 the"]
#[doc = " coordinate magnitudes must be less than +-2e6."]
#[doc = ""]
#[doc = " \\param ptr Pointer to coordinates to compress (length 3*ncoord)"]
#[doc = " \\param ncoord Number of coordinate triplets in data"]
#[doc = " \\param precision Scaling factor for lossy compression. If it is <=0, the"]
#[doc = " default value of 1000.0 is used."]
#[doc = " \\param xfp Handle to portably binary file"]
#[doc = ""]
#[doc = " \\return Number of coordinate triplets written."]
#[doc = " IMPORTANT: Check that this is equal to ncoord - if it is"]
#[doc = " negative, an error occured. This should not happen with"]
#[doc = " normal data, but if your coordinates are NaN or very"]
#[doc = " large (>1e6) it is not possible to use the compression."]
#[doc = ""]
#[doc = " \\warning The compression algorithm is not part of the XDR standard,"]
#[doc = " and very complicated, so you will need this xdrfile module"]
#[doc = " to read it later."]
pub fn xdrfile_compress_coord_double(
ptr: *mut ::std::os::raw::c_double,
ncoord: ::std::os::raw::c_int,
precision: ::std::os::raw::c_double,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}
extern "C" {
#[doc = " \\brief Decompress coordiates from XDR file to array of doubles"]
#[doc = ""]
#[doc = " This routine will decompress three-dimensional coordinate data previously"]
#[doc = " stored in an XDR file and store it in the specified array of doubles."]
#[doc = " Double will NOT give you any extra precision since the coordinates are"]
#[doc = " compressed. This routine just avoids allocating a temporary array of floats."]
#[doc = ""]
#[doc = " The precision used during the earlier compression is read from the file"]
#[doc = " and returned - you cannot adjust the accuracy at this stage."]
#[doc = ""]
#[doc = " \\param ptr Pointer to coordinates to compress (length>= 3*ncoord)"]
#[doc = " \\param ncoord Max number of coordinate triplets to read on input, actual"]
#[doc = " number of coordinate triplets read on return. If this"]
#[doc = " is smaller than the number of coordinates in the frame an"]
#[doc = " error will occur."]
#[doc = " \\param precision The precision used in the previous compression will be"]
#[doc = " written to this variable on return."]
#[doc = " \\param xfp Handle to portably binary file"]
#[doc = ""]
#[doc = " \\return Number of coordinate triplets read. If this is negative,"]
#[doc = " an error occured."]
#[doc = ""]
#[doc = " \\warning Since we cannot count on being able to set/get the"]
#[doc = " position of large files (>2Gb), it is not possible to"]
#[doc = " recover from errors by re-reading the frame if the"]
#[doc = " storage area you provided was too small. To avoid this"]
#[doc = " from happening, we recommend that you store the number of"]
#[doc = " coordinates triplet as an integer either in a header or"]
#[doc = " just before the compressed coordinate data, so you can"]
#[doc = " read it first and allocated enough memory."]
pub fn xdrfile_decompress_coord_double(
ptr: *mut ::std::os::raw::c_double,
ncoord: *mut ::std::os::raw::c_int,
precision: *mut ::std::os::raw::c_double,
xfp: *mut XDRFILE,
) -> ::std::os::raw::c_int;
}

133
src/c_abi/xdrfile_trr.rs Normal file
View File

@@ -0,0 +1,133 @@
use super::xdrfile::*;
extern "C" {
pub fn read_trr_natoms(
fn_: *const ::std::os::raw::c_char,
natoms: *const ::std::os::raw::c_int,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn read_trr_nframes(
fn_: *const ::std::os::raw::c_char,
nframes: *const ::std::os::raw::c_ulong,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn read_trr(
xd: *mut XDRFILE,
natoms: ::std::os::raw::c_int,
step: *mut ::std::os::raw::c_int,
t: *mut ::std::os::raw::c_float,
lambda: *mut ::std::os::raw::c_float,
box_vec: *mut Matrix,
x: *mut Rvec,
v: *mut Rvec,
f: *mut Rvec,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn write_trr(
xd: *mut XDRFILE,
natoms: ::std::os::raw::c_int,
step: ::std::os::raw::c_int,
t: ::std::os::raw::c_float,
lambda: ::std::os::raw::c_float,
box_vec: *const Matrix,
x: *const Rvec,
v: *const Rvec,
f: *const Rvec,
) -> ::std::os::raw::c_int;
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CString;
use tempfile::NamedTempFile;
#[test]
fn test_read_trr_natoms() {
let path = CString::new("tests/1l2y.trr").unwrap();
let mut natoms = 0;
unsafe {
read_trr_natoms(path.as_ptr() as *const i8, &mut natoms);
}
assert!(natoms == 304);
}
#[test]
fn test_read_trr_nframes() {
let path = CString::new("tests/1l2y.trr").unwrap();
let mut nframes: u64 = 0;
unsafe {
let code = read_trr_nframes(path.as_ptr() as *const i8, &mut nframes);
assert!(code as u32 == exdrOK);
}
assert!(nframes == 38, "{:?}", nframes);
}
#[test]
fn test_read_write_trr() {
let tempfile = NamedTempFile::new().unwrap();
let tmp_path = CString::new(tempfile.path().to_str().unwrap()).unwrap();
// write atoms to tempfile
let natoms: i32 = 2;
let time: f32 = 2.0;
let lambda: f32 = 1.0;
let step: i32 = 5;
let box_vec: Matrix = [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 2.0, 1.0]];
let x: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
let v: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
let f: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
unsafe {
let mode = CString::new("w").unwrap();
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let write_code = write_trr(xdr, natoms, step, time, lambda,
box_vec.as_ptr() as *mut Matrix,
x.as_ptr() as *mut Rvec,
v.as_ptr() as *mut Rvec,
f.as_ptr() as *mut Rvec);
assert!(write_code as u32 == exdrOK);
xdrfile_close(xdr);
}
// read atoms from tempfile
let mut time2: f32 = 0.0;
let mut lambda2: f32 = 0.0;
let mut step2: i32 = 0;
let box_vec2: Matrix = [[0.0, 0.0, 0.0]; 3];
let x2: Vec<Rvec> = vec![[0.0, 0.0, 0.0]; 2];
let v2: Vec<Rvec> = vec![[0.0, 0.0, 0.0]; 2];
let f2: Vec<Rvec> = vec![[0.0, 0.0, 0.0]; 2];
unsafe {
let mode = CString::new("r").unwrap();
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let read_code = read_trr(xdr, natoms, &mut step2, &mut time2,
&mut lambda2, box_vec2.as_ptr() as *mut Matrix,
x2.as_ptr() as *mut Rvec,
v2.as_ptr() as *mut Rvec,
f2.as_ptr() as *mut Rvec);
assert!(read_code as u32 == exdrOK);
xdrfile_close(xdr);
}
// make sure everything is still the same
assert!(step2 == step);
assert!(time2 == time);
assert!(lambda == lambda2);
assert!(box_vec2 == box_vec);
assert!(x2 == x);
assert!(v2 == v);
assert!(f2 == f);
}
}

114
src/c_abi/xdrfile_xtc.rs Normal file
View File

@@ -0,0 +1,114 @@
use super::xdrfile::*;
extern "C" {
pub fn read_xtc_natoms(
fn_: *const ::std::os::raw::c_char,
natoms: *const ::std::os::raw::c_int,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn read_xtc_nframes(
fn_: *const ::std::os::raw::c_char,
nframes: *const ::std::os::raw::c_ulong,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn read_xtc(
xd: *mut XDRFILE,
natoms: ::std::os::raw::c_int,
step: *mut ::std::os::raw::c_int,
time: *mut ::std::os::raw::c_float,
box_vec: *mut Matrix,
x: *mut Rvec,
prec: *mut ::std::os::raw::c_float,
) -> ::std::os::raw::c_int;
}
extern "C" {
pub fn write_xtc(
xd: *mut XDRFILE,
natoms: ::std::os::raw::c_int,
step: ::std::os::raw::c_int,
time: ::std::os::raw::c_float,
box_vec: *mut Matrix,
x: *mut Rvec,
prec: ::std::os::raw::c_float,
) -> ::std::os::raw::c_int;
}
#[cfg(test)]
mod tests {
use super::*;
use std::ffi::CString;
use tempfile::NamedTempFile;
#[test]
fn test_read_xtc_natoms() {
let path = CString::new("tests/1l2y.xtc").unwrap();
let mut natoms = 0;
unsafe {
read_xtc_natoms(path.as_ptr() as *mut i8, &mut natoms);
}
assert!(natoms == 304);
}
#[test]
fn test_read_xtc_nframes() {
let path = CString::new("tests/1l2y.xtc").unwrap();
let mut nframes: u64 = 0;
unsafe {
let code = read_xtc_nframes(path.as_ptr() as *const i8, &mut nframes);
assert!(code as u32 == exdrOK);
}
assert!(nframes == 38, "{:?}", nframes);
}
#[test]
fn test_read_write_xtc() {
let tempfile = NamedTempFile::new().unwrap();
let tmp_path = CString::new(tempfile.path().to_str().unwrap()).unwrap();
// write atoms to tempfile
let natoms: i32 = 2;
let time: f32 = 2.0;
let step: i32 = 5;
let box_vec: Matrix = [[1.0, 2.0, 3.0], [2.0, 1.0, 3.0], [3.0, 2.0, 1.0]];
let x: Vec<Rvec> = vec![[1.0, 1.0, 1.0], [1.0, 1.0, 1.0]];
unsafe {
let mode = CString::new("w").unwrap();
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let write_code = write_xtc(xdr, natoms, step, time,
box_vec.as_ptr() as *mut Matrix, x.as_ptr() as *mut Rvec,
1000.0);
assert!(write_code as u32 == exdrOK);
xdrfile_close(xdr);
}
// read atoms from tempfile
let mut time2: f32 = 0.0;
let mut step2: i32 = 0;
let box_vec2: Matrix = [[0.0, 0.0, 0.0]; 3];
let x2: Vec<Rvec> = vec![[0.0, 0.0, 0.0]; 2];
let mut prec: f32 = 0.0;
unsafe {
let mode = CString::new("r").unwrap();
let xdr = xdrfile_open(tmp_path.as_ptr(), mode.as_ptr());
let read_code = read_xtc(xdr, 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);
xdrfile_close(xdr);
}
// make sure everything is still the same
assert!(step2 == step);
assert!(time2 == time);
assert!(box_vec2 == box_vec);
assert!(x2 == x);
}
}

1
src/lib.rs Normal file
View File

@@ -0,0 +1 @@
pub mod c_abi;