#![deny(missing_docs)]
pub use seams_sys as ffi;
#[cfg(doctest)]
#[doc = include_str!("../README.md")]
struct ReadmeDoctests;
use std::ffi::{CStr, CString};
use std::os::raw::{c_char, c_int, c_void};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Status {
Ok = 0,
Null = 1,
Range = 2,
Cell = 3,
Argument = 4,
State = 5,
Memory = 6,
Internal = 7,
}
impl Status {
fn from_code(code: i32) -> Option<Self> {
Some(match code {
0 => Self::Ok,
1 => Self::Null,
2 => Self::Range,
3 => Self::Cell,
4 => Self::Argument,
5 => Self::State,
6 => Self::Memory,
7 => Self::Internal,
_ => return None,
})
}
fn as_str(self) -> &'static str {
match self {
Self::Ok => "ok",
Self::Null => "null pointer",
Self::Range => "out of range",
Self::Cell => "bad cell",
Self::Argument => "bad argument",
Self::State => "no frame",
Self::Memory => "out of memory",
Self::Internal => "internal error",
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Graph {
Cutoff = ffi::SEAMS_GRAPH_CUTOFF,
Mutual = ffi::SEAMS_GRAPH_MUTUAL,
Seeded = ffi::SEAMS_GRAPH_SEEDED,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum ChillRule {
Published = ffi::SEAMS_CHILL_PUBLISHED,
Ovito = ffi::SEAMS_CHILL_OVITO,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Label {
Cubic = ffi::SEAMS_LABEL_CUBIC,
Hexagonal = ffi::SEAMS_LABEL_HEXAGONAL,
Water = ffi::SEAMS_LABEL_WATER,
Interfacial = ffi::SEAMS_LABEL_INTERFACIAL,
Clathrate = ffi::SEAMS_LABEL_CLATHRATE,
Interclathrate = ffi::SEAMS_LABEL_INTERCLATHRATE,
Unclassified = ffi::SEAMS_LABEL_UNCLASSIFIED,
Recubic = ffi::SEAMS_LABEL_RECUBIC,
Rehex = ffi::SEAMS_LABEL_REHEX,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum RingSymmetry {
Unclassified = ffi::SEAMS_RING_UNCLASSIFIED,
Ddc = ffi::SEAMS_RING_DDC,
HcBasal = ffi::SEAMS_RING_HC_BASAL,
HcPrismatic = ffi::SEAMS_RING_HC_PRISMATIC,
BothBasal = ffi::SEAMS_RING_BOTH_BASAL,
BothPrismatic = ffi::SEAMS_RING_BOTH_PRISMATIC,
Prism = ffi::SEAMS_RING_PRISM,
DeformedPrism = ffi::SEAMS_RING_DEFORMED_PRISM,
MixedPrism = ffi::SEAMS_RING_MIXED_PRISM,
Pentagon = ffi::SEAMS_RING_PENTAGON,
Hexagon = ffi::SEAMS_RING_HEXAGON,
Other = ffi::SEAMS_RING_OTHER,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum CageLabel {
Water = ffi::SEAMS_CAGE_WATER,
Hex = ffi::SEAMS_CAGE_HEX,
Cubic = ffi::SEAMS_CAGE_CUBIC,
Both = ffi::SEAMS_CAGE_BOTH,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum SiteKind {
Unspecified = ffi::SEAMS_SITE_UNSPECIFIED,
Cation = ffi::SEAMS_SITE_CATION,
Anion = ffi::SEAMS_SITE_ANION,
Tail = ffi::SEAMS_SITE_TAIL,
DonorH = ffi::SEAMS_SITE_DONOR_H,
Acceptor = ffi::SEAMS_SITE_ACCEPTOR,
Polar = ffi::SEAMS_SITE_POLAR,
Apolar = ffi::SEAMS_SITE_APOLAR,
WaterO = ffi::SEAMS_SITE_WATER_O,
WaterH = ffi::SEAMS_SITE_WATER_H,
Solvent = ffi::SEAMS_SITE_SOLVENT,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum Axis {
X = 0,
Y = 1,
Z = 2,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(i32)]
pub enum DomainSubset {
Apolar = 0,
Polar = 1,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
Library {
status: Status,
message: String,
},
Other {
code: i32,
message: String,
},
Argument {
message: String,
},
}
impl Error {
fn argument(message: impl Into<String>) -> Self {
Self::Argument {
message: message.into(),
}
}
fn from_status(code: i32, message: String) -> Self {
match Status::from_code(code) {
Some(status) => Self::Library { status, message },
None => Self::Other { code, message },
}
}
#[must_use]
pub fn status(&self) -> Option<Status> {
match self {
Self::Library { status, .. } => Some(*status),
Self::Other { .. } | Self::Argument { .. } => None,
}
}
#[must_use]
pub fn code(&self) -> i32 {
match self {
Self::Library { status, .. } => *status as i32,
Self::Other { code, .. } => *code,
Self::Argument { .. } => ffi::SEAMS_ERR_ARG,
}
}
#[must_use]
pub fn message(&self) -> &str {
match self {
Self::Library { message, .. }
| Self::Other { message, .. }
| Self::Argument { message } => message,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Library { status, message } => {
if message.is_empty() {
write!(f, "{}", status.as_str())
} else {
write!(f, "{message} ({})", status.as_str())
}
}
Self::Other { code, message } => {
if message.is_empty() {
write!(f, "status {code}")
} else {
write!(f, "{message} (status {code})")
}
}
Self::Argument { message } => write!(f, "{message}"),
}
}
}
impl std::error::Error for Error {}
fn message_of(handle: *const c_void) -> String {
unsafe {
let ptr = ffi::seams_handle_error(handle);
if ptr.is_null() {
return String::new();
}
CStr::from_ptr(ptr).to_string_lossy().into_owned()
}
}
fn check(code: c_int, handle: *const c_void) -> Result<(), Error> {
if code == ffi::SEAMS_OK {
return Ok(());
}
Err(Error::from_status(code, message_of(handle)))
}
fn c_text(text: &str) -> Result<CString, Error> {
CString::new(text).map_err(|_| Error::argument("text contains a nul"))
}
fn static_text(ptr: *const c_char) -> String {
if ptr.is_null() {
return String::new();
}
unsafe { CStr::from_ptr(ptr).to_string_lossy().into_owned() }
}
fn atom_count(n_coords: usize) -> Result<i32, Error> {
if n_coords % 3 != 0 {
return Err(Error::argument("coordinates are not a multiple of 3"));
}
i32::try_from(n_coords / 3).map_err(|_| Error::argument("too many points"))
}
#[must_use]
pub fn diamond8() -> (Vec<f64>, f64) {
let a = 2.75 * 4.0 / 3.0_f64.sqrt();
let fcc = [
[0.0, 0.0, 0.0],
[0.5, 0.5, 0.0],
[0.5, 0.0, 0.5],
[0.0, 0.5, 0.5],
];
let basis = [[0.0, 0.0, 0.0], [0.25, 0.25, 0.25]];
let mut xyz = Vec::with_capacity(24);
for shift in fcc {
for site in basis {
xyz.push((shift[0] + site[0]) * a);
xyz.push((shift[1] + site[1]) * a);
xyz.push((shift[2] + site[2]) * a);
}
}
(xyz, a)
}
#[must_use]
pub fn version() -> String {
static_text(unsafe { ffi::seams_version_string() })
}
#[must_use]
pub fn abi() -> i32 {
unsafe { ffi::seams_abi() }
}
#[must_use]
pub fn has_chemfiles() -> bool {
env!("DSEAMS_HAS_CHEMFILES") == "1"
}
#[must_use]
pub fn has_readcon() -> bool {
env!("DSEAMS_HAS_READCON") == "1"
}
pub fn set_num_threads(n: i32) -> Result<(), Error> {
check(unsafe { ffi::seams_set_num_threads(n) }, std::ptr::null())
}
#[must_use]
pub fn num_threads() -> i32 {
unsafe { ffi::seams_get_num_threads() }
}
pub fn nframes(path: &str) -> Result<usize, Error> {
let text = c_text(path)?;
let mut count = 0usize;
check(
unsafe { ffi::seams_path_nframes(text.as_ptr(), &mut count) },
std::ptr::null(),
)?;
Ok(count)
}
#[must_use]
pub fn label_name(label: Label) -> String {
static_text(unsafe { ffi::seams_label_name(label as c_int) })
}
#[must_use]
pub fn ring_symmetry_name(symmetry: RingSymmetry) -> String {
static_text(unsafe { ffi::seams_ring_symmetry_name(symmetry as c_int) })
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct ChillOptions {
pub cutoff: f64,
pub species: i32,
pub graph: Graph,
pub rule: ChillRule,
}
impl Default for ChillOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
graph: Graph::Cutoff,
rule: ChillRule::Published,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SteinhardtOptions {
pub cutoff: f64,
pub species: i32,
pub order: i32,
pub average: bool,
}
impl Default for SteinhardtOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
order: 6,
average: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct OrderOptions {
pub cutoff: f64,
pub species: i32,
pub order: i32,
pub graph: Graph,
pub average: bool,
}
impl Default for OrderOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
order: 6,
graph: Graph::Cutoff,
average: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Q6BarOptions {
pub cutoff: f64,
pub species: i32,
pub ice_cut: f64,
}
impl Default for Q6BarOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
ice_cut: 0.36,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct NeighbourOptions {
pub cutoff: f64,
pub species: i32,
pub graph: Graph,
}
impl Default for NeighbourOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
graph: Graph::Cutoff,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct KnnOptions {
pub k: i32,
pub cutoff: f64,
pub species: i32,
pub mutual: bool,
}
impl Default for KnnOptions {
fn default() -> Self {
Self {
k: 4,
cutoff: 3.5,
species: 0,
mutual: false,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct RingOptions {
pub cutoff: f64,
pub species: i32,
pub graph: Graph,
pub max_size: i32,
}
impl Default for RingOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
graph: Graph::Cutoff,
max_size: 6,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct CageOptions {
pub cutoff: f64,
pub species: i32,
pub graph: Graph,
pub k: i32,
pub complete: bool,
}
impl Default for CageOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
graph: Graph::Seeded,
k: 4,
complete: false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct SignatureOptions {
pub cutoff: f64,
pub species: i32,
pub graph: Graph,
pub spec: String,
}
impl Default for SignatureOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
graph: Graph::Cutoff,
spec: "512".to_string(),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DistributionOptions {
pub rmax: f64,
pub bins: i32,
pub type_i: i32,
pub type_j: i32,
}
impl Default for DistributionOptions {
fn default() -> Self {
Self {
rmax: 6.0,
bins: 0,
type_i: 0,
type_j: 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BondOptions {
pub cutoff: f64,
pub oxygen: i32,
pub hydrogen: i32,
pub distance: f64,
pub angle: f64,
}
impl Default for BondOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
oxygen: 1,
hydrogen: 2,
distance: 2.42,
angle: 30.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct DensityOptions {
pub species: i32,
pub bins: i32,
pub axis: Axis,
}
impl Default for DensityOptions {
fn default() -> Self {
Self {
species: 0,
bins: 0,
axis: Axis::Z,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct FingerprintOptions {
pub cutoff: f64,
pub species: i32,
pub k: i32,
pub graph: Graph,
pub hops: i32,
pub color_types: bool,
}
impl Default for FingerprintOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
k: 4,
graph: Graph::Cutoff,
hops: 2,
color_types: false,
}
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DomainOptions {
pub cutoff: f64,
pub spec: String,
pub subset: DomainSubset,
}
impl Default for DomainOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
spec: String::new(),
subset: DomainSubset::Apolar,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct PrismOptions {
pub cutoff: f64,
pub species: i32,
}
impl Default for PrismOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct SheetOptions {
pub cutoff: f64,
pub species: i32,
pub max_size: i32,
pub sheet_area: f64,
}
impl Default for SheetOptions {
fn default() -> Self {
Self {
cutoff: 3.5,
species: 0,
max_size: 6,
sheet_area: 0.0,
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct BrineOptions {
pub water_type: i32,
pub ion_type: i32,
pub bins: i32,
pub axis: Axis,
}
impl Default for BrineOptions {
fn default() -> Self {
Self {
water_type: 0,
ion_type: 0,
bins: 0,
axis: Axis::Z,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Classification {
pub labels: Vec<i32>,
pub counts: [i32; 9],
}
#[derive(Debug, Clone, PartialEq)]
pub struct Steinhardt {
pub local: Vec<f64>,
pub average: Option<Vec<f64>>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Q4Q6 {
pub q4: Vec<f64>,
pub q6: Vec<f64>,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Q6Bar {
pub values: Vec<f64>,
pub mean: f64,
pub median: f64,
pub n_ice: i32,
}
#[derive(Debug, Clone, PartialEq)]
pub struct Distribution {
pub radius: Vec<f64>,
pub g: Vec<f64>,
pub count: Vec<i32>,
pub volume: f64,
}
#[derive(Debug, Clone, PartialEq)]
pub struct RunningCoordination {
pub radius: Vec<f64>,
pub coordination: Vec<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct CageCounts {
pub hexagonal: i32,
pub cubic: i32,
pub water: i32,
pub both: i32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct SignatureMatch {
pub cages: usize,
pub atoms: usize,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Contacts {
pub pairs: usize,
pub cations: i32,
pub anions: i32,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct DomainReport {
pub subset: i32,
pub largest: i32,
pub percolation: f64,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct PrismCensus {
pub perfect: [i32; 7],
pub imperfect: [i32; 7],
}
#[derive(Debug, Clone, PartialEq)]
pub struct DensityProfile {
pub coordinate: Vec<f64>,
pub density: Vec<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct BrineFronts {
pub enter: f64,
pub exit: f64,
pub thickness: f64,
pub found: bool,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Rejection {
pub ice: f64,
pub liquid: f64,
pub k: f64,
pub defined: bool,
}
#[derive(Debug, Clone, PartialEq)]
pub struct BrineProfile {
pub coordinate: Vec<f64>,
pub molality: Vec<f64>,
pub ice_fraction: Vec<f64>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Pockets {
pub pockets: i32,
pub reservoir_waters: i32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct NeighbourPair {
pub atom: i32,
pub neighbour: i32,
}
pub struct NeighbourPairs<'a> {
offsets: &'a [i32],
cols: &'a [i32],
atom: usize,
cursor: usize,
}
impl Iterator for NeighbourPairs<'_> {
type Item = NeighbourPair;
fn next(&mut self) -> Option<Self::Item> {
while self.atom + 1 < self.offsets.len() {
let end = self.offsets[self.atom + 1];
let end = usize::try_from(end).unwrap_or(0);
if self.cursor < end {
let index = self.cursor;
self.cursor += 1;
let neighbour = self.cols.get(index).copied().unwrap_or(-1);
if neighbour != i32::try_from(self.atom).unwrap_or(i32::MAX) {
return Some(NeighbourPair {
atom: i32::try_from(self.atom).unwrap_or(0),
neighbour,
});
}
continue;
}
self.atom += 1;
if self.atom < self.offsets.len() {
self.cursor = usize::try_from(self.offsets[self.atom]).unwrap_or(0);
}
}
None
}
}
pub struct Neighbours {
ptr: *mut ffi::seams_neighbours,
offsets: Vec<i32>,
cols: Vec<i32>,
}
unsafe impl Send for Neighbours {}
impl Drop for Neighbours {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_neighbours_destroy(self.ptr) };
}
}
}
impl Neighbours {
fn adopt(ptr: *mut ffi::seams_neighbours) -> Result<Self, Error> {
let handle = ptr as *const c_void;
let mut n_offsets = 0usize;
let mut nnz = 0usize;
check(
unsafe {
ffi::seams_neighbours_copy(
ptr,
std::ptr::null_mut(),
0,
&mut n_offsets,
std::ptr::null_mut(),
0,
&mut nnz,
)
},
handle,
)?;
let mut offsets = vec![0; n_offsets];
let mut cols = vec![0; nnz];
check(
unsafe {
ffi::seams_neighbours_copy(
ptr,
offsets.as_mut_ptr(),
offsets.len(),
&mut n_offsets,
cols.as_mut_ptr(),
cols.len(),
&mut nnz,
)
},
handle,
)?;
offsets.truncate(n_offsets);
cols.truncate(nnz);
Ok(Self { ptr, offsets, cols })
}
pub fn nnz(&self) -> Result<usize, Error> {
Ok(self.pairs().count())
}
pub fn degrees(&self) -> Result<Vec<i32>, Error> {
let mut degree = Vec::new();
if self.offsets.len() >= 2 {
for window in self.offsets.windows(2) {
degree.push(window[1] - window[0]);
}
}
Ok(degree)
}
#[must_use]
pub fn pairs(&self) -> NeighbourPairs<'_> {
let cursor = self
.offsets
.first()
.copied()
.and_then(|v| usize::try_from(v).ok())
.unwrap_or(0);
NeighbourPairs {
offsets: &self.offsets,
cols: &self.cols,
atom: 0,
cursor,
}
}
}
pub struct Rings {
ptr: *mut ffi::seams_rings,
}
unsafe impl Send for Rings {}
impl Drop for Rings {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_rings_destroy(self.ptr) };
}
}
}
impl Rings {
#[must_use]
pub fn size(&self) -> i32 {
unsafe { ffi::seams_rings_count(self.ptr) }
}
pub fn vertices(&self, index: i32) -> Result<Vec<i32>, Error> {
let handle = self.ptr as *const c_void;
let mut n_out = 0usize;
check(
unsafe {
ffi::seams_rings_vertices(self.ptr, index, std::ptr::null_mut(), 0, &mut n_out)
},
handle,
)?;
let mut atoms = vec![0; n_out];
check(
unsafe {
ffi::seams_rings_vertices(self.ptr, index, atoms.as_mut_ptr(), n_out, &mut n_out)
},
handle,
)?;
atoms.truncate(n_out);
Ok(atoms)
}
pub fn histogram(&self, up_to: i32) -> Result<Vec<i32>, Error> {
let len = usize::try_from(up_to).map_err(|_| Error::argument("ring size is negative"))? + 1;
let mut counts = vec![0; len];
check(
unsafe { ffi::seams_rings_histogram(self.ptr, counts.as_mut_ptr(), up_to + 1) },
self.ptr as *const c_void,
)?;
Ok(counts)
}
pub fn fraction(&self, ring_size: i32) -> Result<f64, Error> {
let mut value = 0.0;
check(
unsafe { ffi::seams_rings_fraction(self.ptr, ring_size, &mut value) },
self.ptr as *const c_void,
)?;
Ok(value)
}
#[must_use]
pub fn iter(&self) -> RingIter<'_> {
RingIter {
rings: self,
index: 0,
}
}
}
pub struct RingIter<'a> {
rings: &'a Rings,
index: i32,
}
impl Iterator for RingIter<'_> {
type Item = Result<Vec<i32>, Error>;
fn next(&mut self) -> Option<Self::Item> {
if self.index >= self.rings.size() {
return None;
}
let index = self.index;
self.index += 1;
Some(self.rings.vertices(index))
}
}
pub struct Cages {
ptr: *mut ffi::seams_cage_set,
}
unsafe impl Send for Cages {}
impl Drop for Cages {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_cages_destroy(self.ptr) };
}
}
}
impl Cages {
pub fn counts(&self) -> Result<CageCounts, Error> {
let mut out = CageCounts::default();
check(
unsafe {
ffi::seams_cages_counts(
self.ptr,
&mut out.hexagonal,
&mut out.cubic,
&mut out.water,
&mut out.both,
)
},
self.ptr as *const c_void,
)?;
Ok(out)
}
pub fn labels(&self, n_atoms: usize) -> Result<Vec<i32>, Error> {
let mut per_atom = vec![0; n_atoms];
check(
unsafe { ffi::seams_cages_labels(self.ptr, per_atom.as_mut_ptr(), n_atoms) },
self.ptr as *const c_void,
)?;
Ok(per_atom)
}
}
pub struct Fingerprint {
ptr: *mut ffi::seams_topo,
}
unsafe impl Send for Fingerprint {}
impl Drop for Fingerprint {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_fingerprint_destroy(self.ptr) };
}
}
}
impl Fingerprint {
pub fn key(&self) -> Result<String, Error> {
let handle = self.ptr as *const c_void;
let mut n_out = 0usize;
check(
unsafe { ffi::seams_fingerprint_key(self.ptr, std::ptr::null_mut(), 0, &mut n_out) },
handle,
)?;
let mut buf = vec![0u8; n_out + 1];
check(
unsafe {
ffi::seams_fingerprint_key(self.ptr, buf.as_mut_ptr().cast(), buf.len(), &mut n_out)
},
handle,
)?;
buf.truncate(n_out);
Ok(String::from_utf8_lossy(&buf).into_owned())
}
pub fn atom(&self, index: i32) -> Result<String, Error> {
let handle = self.ptr as *const c_void;
let mut n_out = 0usize;
check(
unsafe {
ffi::seams_fingerprint_atom(self.ptr, index, std::ptr::null_mut(), 0, &mut n_out)
},
handle,
)?;
let mut buf = vec![0u8; n_out + 1];
check(
unsafe {
ffi::seams_fingerprint_atom(
self.ptr,
index,
buf.as_mut_ptr().cast(),
buf.len(),
&mut n_out,
)
},
handle,
)?;
buf.truncate(n_out);
Ok(String::from_utf8_lossy(&buf).into_owned())
}
pub fn rings(&self) -> Result<Vec<i32>, Error> {
let mut counts = vec![0; 13];
check(
unsafe { ffi::seams_fingerprint_rings(self.ptr, counts.as_mut_ptr(), counts.len()) },
self.ptr as *const c_void,
)?;
Ok(counts)
}
}
pub struct Brine {
ptr: *mut ffi::seams_brine,
}
unsafe impl Send for Brine {}
impl Drop for Brine {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_brine_destroy(self.ptr) };
}
}
}
impl Brine {
pub fn fronts(&self) -> Result<BrineFronts, Error> {
let mut out = BrineFronts {
enter: 0.0,
exit: 0.0,
thickness: 0.0,
found: false,
};
let mut found = 0;
check(
unsafe {
ffi::seams_brine_fronts(
self.ptr,
&mut out.enter,
&mut out.exit,
&mut out.thickness,
&mut found,
)
},
self.ptr as *const c_void,
)?;
out.found = found != 0;
Ok(out)
}
pub fn rejection(&self) -> Result<Rejection, Error> {
let mut out = Rejection {
ice: 0.0,
liquid: 0.0,
k: 0.0,
defined: false,
};
let mut defined = 0;
check(
unsafe {
ffi::seams_brine_rejection(
self.ptr,
&mut out.ice,
&mut out.liquid,
&mut out.k,
&mut defined,
)
},
self.ptr as *const c_void,
)?;
out.defined = defined != 0;
Ok(out)
}
pub fn profile(&self) -> Result<BrineProfile, Error> {
let handle = self.ptr as *const c_void;
let mut n_out = 0usize;
check(
unsafe {
ffi::seams_brine_profile(
self.ptr,
std::ptr::null_mut(),
std::ptr::null_mut(),
std::ptr::null_mut(),
0,
&mut n_out,
)
},
handle,
)?;
let mut coordinate = vec![0.0; n_out];
let mut molality = vec![0.0; n_out];
let mut ice_fraction = vec![0.0; n_out];
check(
unsafe {
ffi::seams_brine_profile(
self.ptr,
coordinate.as_mut_ptr(),
molality.as_mut_ptr(),
ice_fraction.as_mut_ptr(),
n_out,
&mut n_out,
)
},
handle,
)?;
coordinate.truncate(n_out);
molality.truncate(n_out);
ice_fraction.truncate(n_out);
Ok(BrineProfile {
coordinate,
molality,
ice_fraction,
})
}
pub fn pockets(&self) -> Result<Pockets, Error> {
let mut out = Pockets::default();
check(
unsafe {
ffi::seams_brine_pockets(self.ptr, &mut out.pockets, &mut out.reservoir_waters)
},
self.ptr as *const c_void,
)?;
Ok(out)
}
}
pub struct System {
ptr: *mut ffi::seams_system,
}
unsafe impl Send for System {}
impl Drop for System {
fn drop(&mut self) {
if !self.ptr.is_null() {
unsafe { ffi::seams_system_destroy(self.ptr) };
}
}
}
impl System {
pub fn new() -> Result<Self, Error> {
let ptr = unsafe { ffi::seams_system_create() };
if ptr.is_null() {
return Err(Error::from_status(
ffi::SEAMS_ERR_NOMEM,
message_of(std::ptr::null()),
));
}
Ok(Self { ptr })
}
fn handle(&self) -> *const c_void {
self.ptr as *const c_void
}
pub fn set_ortho(&mut self, xyz: &[f64], lx: f64, ly: f64, lz: f64) -> Result<(), Error> {
let n = atom_count(xyz.len())?;
check(
unsafe {
ffi::seams_system_set_ortho(
self.ptr,
xyz.as_ptr(),
n,
0.0,
0.0,
0.0,
lx,
ly,
lz,
std::ptr::null(),
std::ptr::null(),
)
},
self.handle(),
)
}
pub fn set_frame(&mut self, xyz: &[f64], box9: &[f64; 9]) -> Result<(), Error> {
let n = atom_count(xyz.len())?;
check(
unsafe {
ffi::seams_system_set_frame(
self.ptr,
xyz.as_ptr(),
n,
box9.as_ptr(),
std::ptr::null(),
std::ptr::null(),
)
},
self.handle(),
)
}
pub fn set_atoms(
&mut self,
xyz: &[f64],
box9: &[f64],
types: &[i32],
ids: &[i32],
) -> Result<(), Error> {
if xyz.len() % 3 != 0 {
return Err(Error::argument("coordinates are not a multiple of 3"));
}
let n = xyz.len() / 3;
if !types.is_empty() && types.len() != n {
return Err(Error::argument(
"types length does not match the atom count",
));
}
if !ids.is_empty() && ids.len() != n {
return Err(Error::argument("ids length does not match the atom count"));
}
check(
unsafe {
ffi::seams_system_set_atoms(
self.ptr,
xyz.as_ptr(),
n,
box9.as_ptr(),
box9.len(),
if types.is_empty() {
std::ptr::null()
} else {
types.as_ptr()
},
if ids.is_empty() {
std::ptr::null()
} else {
ids.as_ptr()
},
)
},
self.handle(),
)
}
pub fn read(&mut self, path: &str, frame: i32, type_filter: i32) -> Result<(), Error> {
let text = c_text(path)?;
check(
unsafe { ffi::seams_system_read(self.ptr, text.as_ptr(), frame, type_filter) },
self.handle(),
)
}
#[must_use]
pub fn count(&self) -> i32 {
unsafe { ffi::seams_system_count(self.ptr) }
}
pub fn chill(&mut self, options: &ChillOptions) -> Result<Classification, Error> {
self.classify(false, options)
}
pub fn chill_plus(&mut self, options: &ChillOptions) -> Result<Classification, Error> {
self.classify(true, options)
}
pub fn steinhardt(&mut self, options: &SteinhardtOptions) -> Result<Steinhardt, Error> {
let n = self.count() as usize;
let mut local = vec![0.0; n];
let mut average = vec![0.0; n];
let bar = if options.average {
average.as_mut_ptr()
} else {
std::ptr::null_mut()
};
check(
unsafe {
ffi::seams_steinhardt(
self.ptr,
options.cutoff,
options.species,
options.order,
local.as_mut_ptr(),
n,
bar,
n,
)
},
self.handle(),
)?;
Ok(Steinhardt {
local,
average: if options.average { Some(average) } else { None },
})
}
pub fn ql(&mut self, options: &OrderOptions) -> Result<Steinhardt, Error> {
let n = self.count() as usize;
let mut local = vec![0.0; n];
let mut average = vec![0.0; n];
let bar = if options.average {
average.as_mut_ptr()
} else {
std::ptr::null_mut()
};
check(
unsafe {
ffi::seams_ql(
self.ptr,
options.cutoff,
options.species,
options.order,
options.graph as c_int,
local.as_mut_ptr(),
n,
bar,
n,
)
},
self.handle(),
)?;
Ok(Steinhardt {
local,
average: if options.average { Some(average) } else { None },
})
}
pub fn q4q6(&mut self, cutoff: f64) -> Result<Q4Q6, Error> {
let n = self.count() as usize;
let mut q4 = vec![0.0; n];
let mut q6 = vec![0.0; n];
check(
unsafe { ffi::seams_q4q6(self.ptr, cutoff, 0, q4.as_mut_ptr(), q6.as_mut_ptr()) },
self.handle(),
)?;
Ok(Q4Q6 { q4, q6 })
}
pub fn q6bar(&mut self, options: &Q6BarOptions) -> Result<Q6Bar, Error> {
let n = self.count() as usize;
let mut values = vec![0.0; n];
let mut mean = 0.0;
let mut median = 0.0;
let mut n_ice = 0;
check(
unsafe {
ffi::seams_q6bar(
self.ptr,
options.cutoff,
options.species,
options.ice_cut,
values.as_mut_ptr(),
n,
&mut mean,
&mut median,
&mut n_ice,
)
},
self.handle(),
)?;
Ok(Q6Bar {
values,
mean,
median,
n_ice,
})
}
pub fn neighbours(&mut self, options: &NeighbourOptions) -> Result<Neighbours, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_neighbours_build(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
&mut out,
)
},
self.handle(),
)?;
Neighbours::adopt(out)
}
pub fn knn(&mut self, options: &KnnOptions) -> Result<Neighbours, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_neighbours_knn(
self.ptr,
options.k,
options.cutoff,
options.species,
i32::from(options.mutual),
&mut out,
)
},
self.handle(),
)?;
Neighbours::adopt(out)
}
pub fn neighbour_degree(&mut self, cutoff: f64) -> Result<Vec<i32>, Error> {
self.neighbours(&NeighbourOptions {
cutoff,
..NeighbourOptions::default()
})?
.degrees()
}
pub fn rings(&mut self, options: &RingOptions) -> Result<Rings, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_rings_find(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
options.max_size,
&mut out,
)
},
self.handle(),
)?;
Ok(Rings { ptr: out })
}
pub fn six_ring_fraction(&mut self, cutoff: f64) -> Result<(i32, f64), Error> {
let rings = self.rings(&RingOptions {
cutoff,
max_size: 6,
..RingOptions::default()
})?;
let hist = rings.histogram(6)?;
let fraction = rings.fraction(6)?;
Ok((hist[6], fraction))
}
pub fn cages(&mut self, options: &CageOptions) -> Result<Cages, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_cages(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
options.k,
i32::from(options.complete),
&mut out,
)
},
self.handle(),
)?;
Ok(Cages { ptr: out })
}
pub fn signature_cages(&mut self, options: &SignatureOptions) -> Result<SignatureMatch, Error> {
let text = c_text(&options.spec)?;
let mut out = SignatureMatch::default();
check(
unsafe {
ffi::seams_signature_cages(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
text.as_ptr(),
&mut out.cages,
&mut out.atoms,
)
},
self.handle(),
)?;
Ok(out)
}
pub fn rdf(&mut self, options: &DistributionOptions) -> Result<Distribution, Error> {
let mut n_out = 0usize;
let mut volume = 0.0;
check(
unsafe {
ffi::seams_rdf(
self.ptr,
options.rmax,
options.bins,
options.type_i,
options.type_j,
std::ptr::null_mut(),
std::ptr::null_mut(),
std::ptr::null_mut(),
0,
&mut n_out,
&mut volume,
)
},
self.handle(),
)?;
let mut radius = vec![0.0; n_out];
let mut g = vec![0.0; n_out];
let mut count = vec![0; n_out];
check(
unsafe {
ffi::seams_rdf(
self.ptr,
options.rmax,
options.bins,
options.type_i,
options.type_j,
radius.as_mut_ptr(),
g.as_mut_ptr(),
count.as_mut_ptr(),
n_out,
&mut n_out,
std::ptr::null_mut(),
)
},
self.handle(),
)?;
radius.truncate(n_out);
g.truncate(n_out);
count.truncate(n_out);
Ok(Distribution {
radius,
g,
count,
volume,
})
}
pub fn running_cn(
&mut self,
options: &DistributionOptions,
) -> Result<RunningCoordination, Error> {
let mut n_out = 0usize;
check(
unsafe {
ffi::seams_running_cn(
self.ptr,
options.rmax,
options.bins,
options.type_i,
options.type_j,
std::ptr::null_mut(),
std::ptr::null_mut(),
0,
&mut n_out,
)
},
self.handle(),
)?;
let mut radius = vec![0.0; n_out];
let mut coordination = vec![0.0; n_out];
check(
unsafe {
ffi::seams_running_cn(
self.ptr,
options.rmax,
options.bins,
options.type_i,
options.type_j,
radius.as_mut_ptr(),
coordination.as_mut_ptr(),
n_out,
&mut n_out,
)
},
self.handle(),
)?;
radius.truncate(n_out);
coordination.truncate(n_out);
Ok(RunningCoordination {
radius,
coordination,
})
}
pub fn coordination(&mut self, options: &DistributionOptions) -> Result<f64, Error> {
let mut cn = 0.0;
check(
unsafe {
ffi::seams_coordination(
self.ptr,
options.rmax,
options.bins,
options.type_i,
options.type_j,
&mut cn,
)
},
self.handle(),
)?;
Ok(cn)
}
pub fn hbonds(&mut self, options: &BondOptions) -> Result<i32, Error> {
let mut n_bonds = 0;
check(
unsafe {
ffi::seams_hbonds(
self.ptr,
options.cutoff,
options.oxygen,
options.hydrogen,
options.distance,
options.angle,
&mut n_bonds,
)
},
self.handle(),
)?;
Ok(n_bonds)
}
pub fn density(&mut self, options: &DensityOptions) -> Result<DensityProfile, Error> {
let mut n_out = 0usize;
check(
unsafe {
ffi::seams_density_axis(
self.ptr,
options.species,
options.bins,
options.axis as c_int,
std::ptr::null_mut(),
std::ptr::null_mut(),
0,
&mut n_out,
)
},
self.handle(),
)?;
let mut coordinate = vec![0.0; n_out];
let mut density = vec![0.0; n_out];
check(
unsafe {
ffi::seams_density_axis(
self.ptr,
options.species,
options.bins,
options.axis as c_int,
coordinate.as_mut_ptr(),
density.as_mut_ptr(),
n_out,
&mut n_out,
)
},
self.handle(),
)?;
coordinate.truncate(n_out);
density.truncate(n_out);
Ok(DensityProfile {
coordinate,
density,
})
}
pub fn fingerprint(&mut self, options: &FingerprintOptions) -> Result<Fingerprint, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_fingerprint(
self.ptr,
options.cutoff,
options.species,
options.k,
options.graph as c_int,
options.hops,
i32::from(options.color_types),
&mut out,
)
},
self.handle(),
)?;
Ok(Fingerprint { ptr: out })
}
pub fn contact_pairs(&mut self, spec: &str) -> Result<Contacts, Error> {
let text = c_text(spec)?;
let mut out = Contacts::default();
check(
unsafe {
ffi::seams_contact_pairs(
self.ptr,
text.as_ptr(),
&mut out.pairs,
&mut out.cations,
&mut out.anions,
)
},
self.handle(),
)?;
Ok(out)
}
pub fn domains(&mut self, options: &DomainOptions) -> Result<DomainReport, Error> {
let text = c_text(&options.spec)?;
let mut subset = 0;
let mut largest = 0;
let mut percolation = 0.0;
check(
unsafe {
ffi::seams_domains(
self.ptr,
options.cutoff,
text.as_ptr(),
options.subset as c_int,
&mut subset,
&mut largest,
&mut percolation,
)
},
self.handle(),
)?;
Ok(DomainReport {
subset,
largest,
percolation,
})
}
pub fn prisms(&mut self, options: &PrismOptions) -> Result<PrismCensus, Error> {
let mut out = PrismCensus::default();
check(
unsafe {
ffi::seams_prisms(
self.ptr,
options.cutoff,
options.species,
out.perfect.as_mut_ptr(),
out.imperfect.as_mut_ptr(),
out.perfect.len(),
)
},
self.handle(),
)?;
Ok(out)
}
pub fn monolayer(&mut self, options: &SheetOptions) -> Result<Vec<i32>, Error> {
let len = usize::try_from(options.max_size)
.map_err(|_| Error::argument("ring size is negative"))?
+ 1;
let mut counts = vec![0; len];
check(
unsafe {
ffi::seams_monolayer(
self.ptr,
options.cutoff,
options.species,
options.max_size,
options.sheet_area,
counts.as_mut_ptr(),
len,
)
},
self.handle(),
)?;
Ok(counts)
}
pub fn site_table(&mut self, spec: &str) -> Result<Vec<i32>, Error> {
let text = c_text(spec)?;
let n = self.count() as usize;
let mut kinds = vec![0; n];
check(
unsafe { ffi::seams_site_table(self.ptr, text.as_ptr(), kinds.as_mut_ptr(), n) },
self.handle(),
)?;
Ok(kinds)
}
pub fn symmetry(&mut self, shell_rmsd: f64) -> Result<Vec<i32>, Error> {
let n = self.count() as usize;
let mut labels = vec![0; n];
check(
unsafe { ffi::seams_system_symmetry(self.ptr, shell_rmsd, labels.as_mut_ptr(), n) },
self.handle(),
)?;
Ok(labels)
}
pub fn brine(&mut self, ice: &[i32], options: &BrineOptions) -> Result<Brine, Error> {
let mut out = std::ptr::null_mut();
check(
unsafe {
ffi::seams_brine_analyse(
self.ptr,
ice.as_ptr(),
ice.len(),
options.water_type,
options.ion_type,
options.bins,
options.axis as c_int,
&mut out,
)
},
self.handle(),
)?;
Ok(Brine { ptr: out })
}
fn classify(&mut self, plus: bool, options: &ChillOptions) -> Result<Classification, Error> {
let n = self.count() as usize;
let mut labels = vec![0; n];
let mut counts = [0; 9];
if plus {
check(
unsafe {
ffi::seams_chill_plus(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
options.rule as c_int,
labels.as_mut_ptr(),
counts.as_mut_ptr(),
)
},
self.handle(),
)?;
} else {
check(
unsafe {
ffi::seams_chill(
self.ptr,
options.cutoff,
options.species,
options.graph as c_int,
options.rule as c_int,
labels.as_mut_ptr(),
n,
counts.as_mut_ptr(),
)
},
self.handle(),
)?;
}
Ok(Classification { labels, counts })
}
}