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//! | Van der Waals | [`SplinedBuckingham`](potentials::nonbonded::SplinedBuckingham) | $C^2$ continuous Exp-6 |
39//! | Electrostatics | [`Coulomb`](potentials::nonbonded::Coulomb) | Standard $1/r$ potential |
40//! | H-Bond | [`HydrogenBond`](potentials::nonbonded::HydrogenBond) | 12-10 with angular term |
41//!
42//! ### Bonded Interactions ([`potentials::bonded`])
43//!
44//! | Category | Kernel | Description |
45//! |----------|--------|-------------|
46//! | Stretch | [`Harmonic`](potentials::bonded::Harmonic) | Harmonic spring |
47//! | Stretch | [`Morse`](potentials::bonded::Morse) | Anharmonic with dissociation |
48//! | Angle | [`CosineHarmonic`](potentials::bonded::CosineHarmonic) | Harmonic in $\cos\theta$ |
49//! | Angle | [`CosineLinear`](potentials::bonded::CosineLinear) | Linear for $\theta_0 = 180°$ |
50//! | Angle | [`ThetaHarmonic`](potentials::bonded::ThetaHarmonic) | Harmonic in $\theta$ |
51//! | Torsion | [`Torsion`](potentials::bonded::Torsion) | Periodic cosine series |
52//! | Inversion | [`PlanarInversion`](potentials::bonded::PlanarInversion) | Planar constraint |
53//! | Inversion | [`UmbrellaInversion`](potentials::bonded::UmbrellaInversion) | Umbrella constraint |
54//!
55//! ## Quick Start
56//!
57//! ### Van der Waals Interaction
58//!
59//! ```
60//! use dreid_kernel::{potentials::nonbonded::LennardJones, PairKernel};
61//!
62//! // Parameters: (D0, R0^2) - well depth and equilibrium distance squared
63//! let params = (0.1, 16.0);
64//!
65//! // Squared distance between atoms
66//! let r_sq = 14.44;
67//!
68//! // Compute energy
69//! let energy = LennardJones::energy(r_sq, params);
70//!
71//! // Compute force prefactor: D = -(1/r) * dE/dr
72//! let diff = LennardJones::diff(r_sq, params);
73//!
74//! // Compute both (optimized, shares intermediate calculations)
75//! let result = LennardJones::compute(r_sq, params);
76//! ```
77//!
78//! ### Torsion Angle
79//!
80//! ```
81//! use dreid_kernel::{potentials::bonded::Torsion, TorsionKernel};
82//!
83//! // Parameters: (V_half, n, cos(n*phi0), sin(n*phi0))
84//! // V_half = V/2 where V is barrier height
85//! let params = (2.5, 3, 1.0, 0.0); // V=5 kcal/mol, n=3, phase=0
86//!
87//! // Dihedral angle as (cos(phi), sin(phi))
88//! let cos_phi = 0.5;
89//! let sin_phi = 0.866025;
90//!
91//! let energy = Torsion::energy(cos_phi, sin_phi, params);
92//! ```
93//!
94//! ## Architecture
95//!
96//! The force calculation follows a two-layer design:
97//!
98//! 1. **Kernel Layer** (this crate): Computes scalar energy and derivative factors.
99//! 2. **Geometry Layer** (your code): Applies `F += -Factor * GeometricVector`.
100//!
101//! This separation allows the kernel to be completely geometry-agnostic,
102//! enabling use in periodic boundary conditions, minimization, or MD without modification.
103//!
104//! ## Performance
105//!
106//! Benchmarked on Intel Core i7-13620H (Single Threaded):
107//!
108//! | Kernel | Combined Time | Throughput |
109//! |--------|---------------|------------|
110//! | Cosine Linear | 0.67 ns | ~1.5 Billion ops/sec |
111//! | Lennard-Jones | 1.19 ns | ~840 Million ops/sec |
112//! | Torsion (n=3) | 2.55 ns | ~390 Million ops/sec |
113//!
114//! See [`BENCHMARKS.md`](https://github.com/caltechmsc/dreid-kernel/blob/main/BENCHMARKS.md) for complete data.
115//!
116//! **Made with ❤️ for the scientific computing community**
117
118mod math;
119mod traits;
120mod types;
121
122pub mod potentials;
123
124pub use math::Real;
125
126pub use traits::{AngleKernel, HybridKernel, PairKernel, TorsionKernel};
127pub use types::{EnergyDiff, HybridEnergyDiff};