pyscivr.save module
Saving and loading of trajectories and Hessians in the .npz format.
See sections II B 2 and II C of the paper for details.
- Notation:
N: number of atoms.T: number of time steps of the trajectory.K: number of constraints.S: number of steps at which the Hessian is evaluated.m: number of active coordinates.
- pyscivr.save.save_trajectory(fpath, traj, **meta)[source]
Save a trajectory.
Only the keys
(posi, mome, force, potential_energy, total_energy, dt, mass)are saved. The Lagrange multiplierslamandmuare not saved, since they can be restored byload_trajectory(). Additional metadata supplied via**metais also saved.
- pyscivr.save.restore_multipliers(coord_const, posi, mome, force, mass, dt, *, atol=1e-10)[source]
Restore the Lagrange multipliers of SHAKE/RATTLE from a trajectory.
The multipliers are obtained by solving (Eqs. (25)-(27) in the paper)
\[ \begin{align}\begin{aligned}q_{n+1} - \tilde{q}_{n+1} &= \Delta t M^{-1} \mathcal{G}(q_{n})^T \lambda_{n}, \quad \tilde{q}_{n+1} = q_n + \Delta t M^{-1} p_n + \frac{\Delta t^2}{2} M^{-1} F(q_n)\\p_{n+1} - \tilde{p}_{n+1} &= \mathcal{G}(q_{n+1})^T \mu_{n+1}, \quad \tilde{p}_{n+1} = p_n + \frac{\Delta t}{2} (F(q_n) + F(q_{n+1})) + \mathcal{G}(q_{n})^T \lambda_{n}\end{aligned}\end{align} \]in the least-squares sense, and the residuals are checked to be small.
- Parameters:
coord_const (InternalCoordinates) – InternalCoordinates defining the
Kconstraints, the same as used inpyscivr.integrators.ShakeRattle.posi (ArrayLike) –
(T, N, 3)or(T, 3N)positions.mome (ArrayLike) –
(T, N, 3)or(T, 3N)momenta.force (ArrayLike) –
(T, N, 3)or(T, 3N)forces.mass (ArrayLike) –
(N,)atomic masses.dt (float) – Time step.
atol (float) – Absolute tolerance for checking the residuals.
- Returns:
(lam, mu), wherelam:(T, K)Lagrange multipliers for position constraints.mu:(T, K)Lagrange multipliers for momentum constraints.
- Raises:
AssertionError – If the residuals exceed
atol, i.e., the trajectory is not consistent with SHAKE/RATTLE undercoord_const.- Return type:
tuple[NDArray[np.float64], NDArray[np.float64]]
- pyscivr.save.load_trajectory(fpath, coord_const=None)[source]
Load a trajectory saved by
save_trajectory().- Parameters:
fpath (str) – Path to the saved trajectory.
coord_const (InternalCoordinates | None) – InternalCoordinates defining the constraints, the same as used in
pyscivr.integrators.ShakeRattle. If provided, the Lagrange multiplierslamandmuare restored byrestore_multipliers().
- Returns:
Trajectory with keys of
(posi, mome, force, potential_energy, total_energy, dt, mass), and additional data saved asmeta. Ifcoord_constis provided, the Lagrange multiplierslamandmuare also included.- Return type:
- pyscivr.save.save_hessian(fpath, steps, hess, posi, coord_all, active_idx, **meta)[source]
Save the Hessians or projected Hessians in a compressed form.
To reduce storage requirements, the
(S, 3N, 3N)full Hessians \(K\) are saved asK A_Aof shape(S, 3N, m)(see alsopyscivr.hessian.eval_hess_active()). The projected Hessians\mathfrak{P}^T K \mathfrak{P}can be restored byload_hessian().- Parameters:
fpath (str) – Path to save the Hessians.
steps (ArrayLike) –
(S,)indices of the steps where the Hessians are evaluated.hess (ArrayLike) –
(S, 3N, 3N)Hessians or projected Hessians.posi (ArrayLike) –
(T, N, 3)or(T, 3N)positions of the trajectory.coord_all (InternalCoordinates) – InternalCoordinates for the full system. It must define
3Ncoordinates for coordinate transformation.active_idx (ArrayLike) –
(m,)row indices of the active coordinates incoord_all, in the row order afterbuild().**meta (Any) – Additional metadata to save with the Hessians.
- Return type:
None
Note
The file contains the keys
steps,hessandactive_idx, in addition tometa.
- pyscivr.save.load_hessian(fpath, coord_all, posi, n_step=None, symmetrize=True)[source]
Restore the projected Hessians saved by
save_hessian().The projected Hessians are restored as \(\mathfrak{P}^T K \mathfrak{P}\), where \(\mathfrak{P} = A_A B_A\). Since \(\mathfrak{P}\) is idempotent, the same Hessians can be restored whether the Hessians are projected or not.
- Parameters:
fpath (str) – Path to the saved Hessians.
coord_all (InternalCoordinates) – InternalCoordinates for the full system. It must define
3Ncoordinates for coordinate transformation.posi (ArrayLike) –
(T, N, 3)or(T, 3N)positions of the trajectory.n_step – If given, the Hessians are interpolated (cubic spline) to all steps.
(0, 1, ..., n_step-1)(seepyscivr.hessian.interpolate_hessian()).symmetrize – If True, the Hessians are symmetrized.
- Returns:
(steps, hess), wheresteps:(S,)indices of the steps where the Hessians are evaluated.hess:(S, 3N, 3N)projected Hessians, or(n_step, 3N, 3N)ifn_stepis given.
- Return type:
tuple[NDArray[np.int64], NDArray[np.float64]]