uff_relax/lib.rs
1//! # uff-relax
2//!
3//! `uff-relax` is a fast, parallelized molecular structure optimizer using the
4//! Universal Force Field (UFF) and the FIRE (Fast Iterative Relaxation Engine) algorithm.
5//!
6//! ## Features
7//! - **Fast**: Optimized force calculations with spatial partitioning (Cell Lists).
8//! - **Parallel**: Automatic multi-threading using Rayon for large systems.
9//! - **Flexible**: Supports periodic boundary conditions (Orthorhombic and Triclinic).
10//! - **Easy to use**: Simple API for defining atoms, bonds, and running optimizations.
11//!
12//! ## Quick Start
13//!
14//! ```rust
15//! use uff_relax::{System, Atom, Bond, UnitCell, UffOptimizer};
16//! use glam::DVec3;
17//!
18//! // Define atoms
19//! let atoms = vec![
20//! Atom::new(6, DVec3::new(0.0, 0.0, 0.0)),
21//! Atom::new(6, DVec3::new(1.5, 0.0, 0.0)),
22//! ];
23//! // Define bonds
24//! let bonds = vec![Bond { atom_indices: (0, 1), order: 1.0 }];
25//! // Setup system
26//! let mut system = System::new(atoms, bonds, UnitCell::new_none());
27//! // Optimize
28//! UffOptimizer::new(100, 1e-2).optimize(&mut system);
29//! ```
30
31pub mod atom;
32pub mod cell;
33pub mod forcefield;
34pub mod math;
35pub mod optimizer;
36pub mod params;
37pub mod spatial;
38
39pub use atom::{Atom, Bond, UffAtomType};
40pub use cell::{UnitCell, CellType};
41pub use forcefield::{System, EnergyTerms};
42pub use optimizer::UffOptimizer;
43pub use params::{get_uff_params, element_symbol};
44
45use std::sync::Once;
46static START: Once = Once::new();
47
48/// Initializes the Rayon thread pool.
49/// If `num_threads` is Some(n), it sets that specific number.
50/// If `num_threads` is None, it checks `RAYON_NUM_THREADS` env var or defaults to 4.
51pub fn init_parallelism(num_threads: Option<usize>) {
52 let threads = match num_threads {
53 Some(n) => n,
54 None => std::env::var("RAYON_NUM_THREADS")
55 .ok()
56 .and_then(|s| s.parse().ok())
57 .unwrap_or(4),
58 };
59
60 START.call_once(|| {
61 let _ = rayon::ThreadPoolBuilder::new()
62 .num_threads(threads)
63 .build_global();
64 });
65}