hoomd_mc/translate.rs
1// Copyright (c) 2024-2026 The Regents of the University of Michigan.
2// Part of hoomd-rs, released under the BSD 3-Clause License.
3
4//! Implement Translate
5
6use serde::{Deserialize, Serialize};
7use std::{fmt, marker::PhantomData};
8
9use hoomd_utility::valid::PositiveReal;
10
11use crate::Adjust;
12
13mod cartesian;
14mod hyperboloid;
15mod sphere;
16
17/// Move the position of a body by a small distance.
18///
19/// `Translate` proposes local trial moves that translate the position of a body
20/// in space by up to a maximum distance, given by a [`PositiveReal`].
21///
22/// [`PositiveReal`]: hoomd_utility::valid::PositiveReal
23///
24/// The generic type names are:
25/// * `P`: The type of the point to translate.
26///
27/// # Example
28///
29/// ```
30/// use hoomd_mc::Translate;
31/// use hoomd_vector::Cartesian;
32///
33/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
34/// let d = 0.1;
35/// let translate =
36/// Translate::<Cartesian<2>>::with_maximum_distance(d.try_into()?);
37/// # Ok(())
38/// # }
39/// ```
40#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
41pub struct Translate<P> {
42 /// The maximum distance a body can be translated in one trial move.
43 maximum_distance: PositiveReal,
44 /// Mark the type of the point to be translated.
45 marker: PhantomData<P>,
46}
47
48impl<P> Translate<P> {
49 /// Construct a [`Translate`] move with the given maximum distance.
50 ///
51 /// # Example
52 ///
53 /// ```
54 /// use hoomd_mc::Translate;
55 /// use hoomd_vector::Cartesian;
56 ///
57 /// # fn main() -> Result<(), Box<dyn std::error::Error>> {
58 /// let d = 0.1;
59 /// let translate =
60 /// Translate::<Cartesian<2>>::with_maximum_distance(d.try_into()?);
61 /// # Ok(())
62 /// # }
63 /// ```
64 #[must_use]
65 #[inline]
66 pub fn with_maximum_distance(maximum_distance: PositiveReal) -> Self {
67 Self {
68 maximum_distance,
69 marker: PhantomData,
70 }
71 }
72
73 /// Get the maximum distance.
74 #[must_use]
75 #[inline]
76 pub fn maximum_distance(&self) -> &PositiveReal {
77 &self.maximum_distance
78 }
79
80 /// Get the maximum distance.
81 #[inline]
82 pub fn maximum_distance_mut(&mut self) -> &mut PositiveReal {
83 &mut self.maximum_distance
84 }
85}
86
87impl<P> Adjust for Translate<P> {
88 /// Change the maximum trial move size by the given scale factor.
89 #[inline]
90 fn adjust(&mut self, factor: PositiveReal) {
91 self.maximum_distance *= factor;
92 }
93}
94
95impl<P> fmt::Display for Translate<P> {
96 /// Format a [`Translate`] as `{maximum_distance}`.
97 #[inline]
98 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
99 self.maximum_distance.fmt(f)
100 }
101}