Skip to content

Rust

Use the Rust API when packing is part of a Rust program, when you need structured convergence diagnostics, or when you are extending molpack itself.

use std::sync::Arc;
use molpack::{GencanPack, PackEngine, RegionRestraint, Target};
use molrs::core::Cuboid;
use ndarray::array;

let positions = [[0.0, 0.0, 0.0], [0.96, 0.0, 0.0], [-0.24, 0.93, 0.0]];
let radii = [1.52, 1.20, 1.20];

let water = Target::from_coords(&positions, &radii, 100)
    .with_name("water")
    .with_restraint(RegionRestraint(Arc::new(Cuboid::new(array![0.0, 0.0, 0.0], array![40.0, 40.0, 40.0]))));

let result = GencanPack::new().with_seed(42).run(&[water], 200)?;
let frame = result.frame;

The shared builders (with_seed, with_tolerance, callbacks, boxes, …) and the terminal run come from the PackEngine trait, so it has to be in scope. GencanPack is the rigid-body engine; CbmcGrow is the chain-growth one.

Each engine is a single-stage preset: calling .run(...) on GencanPack or CbmcGrow drives exactly one packing algorithm end to end (internally, Pipeline::single(self).run(...)). When a pack needs more than one algorithm in sequence — grow a chain, then push it apart with rigid-body descent — compose stages directly with Pipeline instead of chaining separate runs:

use molpack::{CbmcGrow, GencanPack, PackEngine, Pipeline};

let result = Pipeline::new()
    .with_stage(CbmcGrow::new(prior))
    .with_stage(GencanPack::new())
    .run(&targets, max_loops)?;

Pipeline drives every stage through the same lifecycle a preset uses, continuing from the first stage's placements rather than re-placing from scratch. See Composing stages for the full walkthrough, including the shared-settings rule and the two stage combinators (with_repeat, with_guarded).

Install

cargo add molcrafts-molpack

Feature flags:

Feature Enables
io Template reading and output writing through the molrs reader and writer of each file's format (script::StructureFormat), and XyzTrajectoryCallback.
cli The molpack binary plus io.
rayon Parallel objective evaluation.

molpack has no ff feature: a force-field optimizer bound through with_optimizer (molrs's Lbfgs) needs molrs's ff feature on your own molcrafts-molrs dependency.

Pages

  • Quickstart walks through a first target and run.
  • Restraints and PBC explains target-level, atom-subset, global, and periodic restraints.
  • Callbacks and Optimizers covers progress output, observers, early stop, trajectory dumping, and in-loop conformation sampling.
  • Examples lists the checked-in Rust workloads.