minitpr/
structures.rs

1// Released under Apache License 2.0 / MIT License.
2// Copyright (c) 2024-2025 Ladislav Bartos
3
4//! This file contains public data structures used in the `minitpr` library.
5
6pub use mendeleev::Element;
7
8use crate::DIM;
9
10/// Structure representing the TPR file.
11#[derive(Debug, Clone)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub struct TprFile {
14    /// TPR file header.
15    pub header: TprHeader,
16    /// Name of the molecular system.
17    pub system_name: String,
18    /// Dimensions of the simulation box.
19    pub simbox: Option<SimBox>,
20    /// System topology.
21    pub topology: TprTopology,
22}
23
24/// Structure representing the header of the TPR file.
25#[derive(Debug, Clone)]
26#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
27pub struct TprHeader {
28    /// Gromacs version used to write the tpr file.
29    pub gromacs_version: String,
30    /// Precision of the tpr file.
31    pub precision: Precision,
32    /// Version of the tpr file.
33    pub tpr_version: i32,
34    /// Generation of the tpr file.
35    pub tpr_generation: i32,
36    /// Tpr file tag.
37    pub file_tag: String,
38    /// Number of atoms.
39    pub n_atoms: i32,
40    /// Number of temperature coupling groups.
41    pub n_coupling_groups: i32,
42    /// Value of the alchemical state.
43    pub fep_state: i32,
44    /// Value of lambda.
45    pub lambda: f64,
46    /// Is input record present?
47    pub has_input_record: bool,
48    /// Is topology present?
49    pub has_topology: bool,
50    /// Are positions present?
51    pub has_positions: bool,
52    /// Are velocities present?
53    pub has_velocities: bool,
54    /// Are forces present?
55    pub has_forces: bool,
56    /// Is the simulation box present?
57    pub has_box: bool,
58    /// Size of the body of the tpr file.
59    pub body_size: Option<i64>,
60}
61
62/// Structure representing the topology of the TPR file.
63#[derive(Debug, Clone)]
64#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
65pub struct TprTopology {
66    /// List of atoms in the system.
67    pub atoms: Vec<Atom>,
68    /// List of bonds between atoms in the system.
69    /// The order of bonds is undefined.
70    pub bonds: Vec<Bond>,
71}
72
73/// Structure representing simulation box dimensions.
74#[derive(Debug, Clone)]
75#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
76pub struct SimBox {
77    pub simbox: [[f64; DIM]; DIM],
78    pub simbox_rel: [[f64; DIM]; DIM],
79    pub simbox_v: [[f64; DIM]; DIM],
80}
81
82/// Enum representing precision of the tpr file.
83#[derive(Debug, Clone, PartialEq, Eq, Copy)]
84#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
85pub enum Precision {
86    Single,
87    Double,
88}
89
90/// Structure representing an atom.
91#[derive(Debug, Clone)]
92#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
93pub struct Atom {
94    /// Name of the atom.
95    pub atom_name: String,
96    /// Atom number. All atoms are numbered sequentially, starting from 1.
97    pub atom_number: i32,
98    /// Name of the residue this atom is part of.
99    pub residue_name: String,
100    /// Residue number. All residues are numbered sequentially, starting from 1.
101    pub residue_number: i32,
102    /// Mass of the atom.
103    pub mass: f64,
104    /// Charge of the atom.
105    pub charge: f64,
106    /// Element this atom belongs to.
107    pub element: Option<Element>,
108    /// Position of the atom.
109    pub position: Option<[f64; 3]>,
110    /// Velocity of the atom.
111    pub velocity: Option<[f64; 3]>,
112    /// Force acting on the atom.
113    pub force: Option<[f64; 3]>,
114}
115
116/// Structure representing a bond between atoms.
117#[derive(Debug, Clone, PartialEq, Eq)]
118#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
119pub struct Bond {
120    /// Global index of the first atom involved in the bond.
121    /// Note the word is `index`, not `number`! Atom indices start from 0.
122    /// Indices correspond to indices of the atoms in the `TprTopology::atoms` vector.
123    pub atom1: usize,
124    /// Global index of the second atom involved in the bond.
125    pub atom2: usize,
126}