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 | [`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//! // Parameters: (D0, R0^2) - well depth and equilibrium distance squared
62//! let params = (0.1, 16.0);
63//!
64//! // Squared distance between atoms
65//! let r_sq = 14.44;
66//!
67//! // Compute energy
68//! let energy = LennardJones::energy(r_sq, params);
69//!
70//! // Compute force prefactor: D = -(1/r) * dE/dr
71//! let diff = LennardJones::diff(r_sq, params);
72//!
73//! // Compute both (optimized, shares intermediate calculations)
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//! // Parameters: (V_half, n, cos(n*phi0), sin(n*phi0))
83//! // V_half = V/2 where V is barrier height
84//! let params = (2.5, 3, 1.0, 0.0); // V=5 kcal/mol, n=3, phase=0
85//!
86//! // Dihedral angle as (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 Harmonic | 0.70 ns | ~1.4 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};