Skip to main content

dreid_kernel/
lib.rs

1#![cfg_attr(not(feature = "std"), no_std)]
2
3//! # DREID-Kernel
4//!
5//! **A high-performance, no-std Rust library providing pure mathematical primitives
6//! and stateless energy kernels for the DREIDING force field.**
7//!
8//! ## Features
9//!
10//! - **⚡ Bare Metal Ready**: Strict `#![no_std]` support. Zero heap allocations.
11//!   Use this in embedded devices, OS kernels, or WASM environments.
12//! - **📐 Stateless Design**: Pure mathematical functions. You provide the state
13//!   (coordinates/parameters), we return the forces. No object lifecycle management.
14//! - **🔬 DREIDING Compliant**: Implements the exact functional forms defined in
15//!   Mayo et al. (1990), including specific hydrogen bonding and inversion terms.
16//! - **🛡️ Type Safe**: Uses Rust's trait system to enforce compile-time correctness
17//!   for potential parameters and inputs.
18//!
19//! ## Kernel Traits
20//!
21//! This library provides four kernel trait families:
22//!
23//! | Trait | Body | Input | Output |
24//! |-------|------|-------|--------|
25//! | [`PairKernel`] | 2-body | $r^2$ | $(E, D)$ where $D = -\frac{1}{r}\frac{dE}{dr}$ |
26//! | [`AngleKernel`] | 3-body | $\cos\theta$ | $(E, \Gamma)$ where $\Gamma = \frac{dE}{d(\cos\theta)}$ |
27//! | [`TorsionKernel`] | 4-body | $(\cos\phi, \sin\phi)$ | $(E, T)$ where $T = \frac{dE}{d\phi}$ |
28//! | [`HybridKernel`] | Mixed | $(r^2, \cos\theta)$ | $(E, D_{rad}, D_{ang})$ |
29//!
30//! ## Available Potentials
31//!
32//! ### Non-Bonded Interactions ([`potentials::nonbonded`])
33//!
34//! | Category | Kernel | Description |
35//! |----------|--------|-------------|
36//! | Van der Waals | [`LennardJones`](potentials::nonbonded::LennardJones) | Classic 12-6 potential |
37//! | Van der Waals | [`Buckingham`](potentials::nonbonded::Buckingham) | Exponential-6 potential |
38//! | Electrostatics | [`Coulomb`](potentials::nonbonded::Coulomb) | Standard $1/r$ potential |
39//! | H-Bond | [`HydrogenBond`](potentials::nonbonded::HydrogenBond) | 12-10 with angular term |
40//!
41//! ### Bonded Interactions ([`potentials::bonded`])
42//!
43//! | Category | Kernel | Description |
44//! |----------|--------|-------------|
45//! | Stretch | [`Harmonic`](potentials::bonded::Harmonic) | Harmonic spring |
46//! | Stretch | [`Morse`](potentials::bonded::Morse) | Anharmonic with dissociation |
47//! | Angle | [`CosineHarmonic`](potentials::bonded::CosineHarmonic) | Harmonic in $\cos\theta$ |
48//! | Angle | [`CosineLinear`](potentials::bonded::CosineLinear) | Linear for $\theta_0 = 180°$ |
49//! | Angle | [`ThetaHarmonic`](potentials::bonded::ThetaHarmonic) | Harmonic in $\theta$ |
50//! | Torsion | [`Torsion`](potentials::bonded::Torsion) | Periodic cosine series |
51//! | Inversion | [`PlanarInversion`](potentials::bonded::PlanarInversion) | Planar constraint |
52//! | Inversion | [`UmbrellaInversion`](potentials::bonded::UmbrellaInversion) | Umbrella constraint |
53//!
54//! ## Quick Start
55//!
56//! ### Van der Waals Interaction
57//!
58//! ```
59//! use dreid_kernel::{potentials::nonbonded::LennardJones, PairKernel};
60//!
61//! // Pre-compute parameters from physical constants: (D0, R0) -> (D0, R0^2)
62//! let params = LennardJones::precompute(0.1, 4.0);
63//!
64//! // Squared distance between atoms (r^2 = 3.8^2)
65//! let r_sq = 14.44;
66//!
67//! // Compute Energy
68//! let energy = LennardJones::energy(r_sq, params);
69//!
70//! // Compute Force Prefactor (-1/r * dE/dr)
71//! let diff = LennardJones::diff(r_sq, params);
72//!
73//! // Compute Both (Optimized)
74//! let result = LennardJones::compute(r_sq, params);
75//! ```
76//!
77//! ### Torsion Angle
78//!
79//! ```
80//! use dreid_kernel::{potentials::bonded::Torsion, TorsionKernel};
81//!
82//! // Pre-compute parameters from physical constants:
83//! // (V, n, phi0_deg) -> (V/2, n, cos(n*phi0), sin(n*phi0))
84//! let params = Torsion::precompute(5.0, 3, 0.0); // V=5 kcal/mol, n=3, phi0=0°
85//!
86//! // Dihedral angle input (cos(phi), sin(phi))
87//! let cos_phi = 0.5;
88//! let sin_phi = 0.866025;
89//!
90//! let energy = Torsion::energy(cos_phi, sin_phi, params);
91//! ```
92//!
93//! ## Architecture
94//!
95//! The force calculation follows a two-layer design:
96//!
97//! 1. **Kernel Layer** (this crate): Computes scalar energy and derivative factors.
98//! 2. **Geometry Layer** (your code): Applies `F += -Factor * GeometricVector`.
99//!
100//! This separation allows the kernel to be completely geometry-agnostic,
101//! enabling use in periodic boundary conditions, minimization, or MD without modification.
102//!
103//! ## Performance
104//!
105//! Benchmarked on Intel Core i7-13620H (Single Threaded):
106//!
107//! | Kernel | Combined Time | Throughput |
108//! |--------|---------------|------------|
109//! | Cosine Linear | 0.67 ns | ~1.5 Billion ops/sec |
110//! | Lennard-Jones | 1.19 ns | ~840 Million ops/sec |
111//! | Torsion (n=3) | 2.55 ns | ~390 Million ops/sec |
112//!
113//! See [`BENCHMARKS.md`](https://github.com/caltechmsc/dreid-kernel/blob/main/BENCHMARKS.md) for complete data.
114//!
115//! **Made with ❤️ for the scientific computing community**
116
117mod math;
118mod traits;
119mod types;
120
121pub mod potentials;
122
123pub use math::Real;
124
125pub use traits::{AngleKernel, HybridKernel, PairKernel, TorsionKernel};
126pub use types::{EnergyDiff, HybridEnergyDiff};