Coverage for openxps/bounds/reflective.py: 100%
22 statements
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-13 22:08 +0000
« prev ^ index » next coverage.py v7.11.3, created at 2025-11-13 22:08 +0000
1"""
2Reflective boundary condition.
4.. module:: openxps.bounds.reflective
5 :platform: Linux, MacOS, Windows
6 :synopsis: Reflective boundary condition
8.. classauthor:: Charlles Abreu <craabreu@gmail.com>
10"""
12from .base import Bounds
15class ReflectiveBounds(Bounds):
16 """
17 A reflective boundary condition. The dynamical variable collides elastically
18 with the upper and lower bounds and is reflected back into the range.
20 Parameters
21 ----------
22 lower
23 The lower bound for the dynamical variable.
24 upper
25 The upper bound for the dynamical variable.
26 unit
27 The unity of measurement of the bounds. If the bounds do not have a unit, use
28 ``dimensionless``.
30 Example
31 -------
32 >>> import openxps as xps
33 >>> from openmm import unit
34 >>> bounds = xps.ReflectiveBounds(1, 10, unit.angstrom)
35 >>> bounds == xps.ReflectiveBounds(0.1, 1, unit.nanometer)
36 True
37 >>> print(bounds)
38 ReflectiveBounds(lower=1, upper=10, unit=A)
39 """
41 def __post_init__(self) -> None:
42 super().__post_init__()
43 self.period = 2 * (self.upper - self.lower)
45 def leptonExpression(self, variable: str) -> str:
46 scaled = f"scaled_{variable}"
47 wrapped = f"wrapped_{variable}"
48 if self.lower == 0:
49 shift = deshift = ""
50 elif self.lower > 0:
51 shift = f"-{self.lower}"
52 deshift = f"+{self.lower}"
53 else:
54 shift = f"+{-self.lower}"
55 deshift = f"-{-self.lower}"
56 return (
57 f"min({wrapped},1-{wrapped})*{self.period}{deshift}"
58 f";\n{wrapped}={scaled}-floor({scaled})"
59 f";\n{scaled}=({variable}{shift})/{self.period}"
60 )
62 def wrap(self, value: float, rate: float) -> tuple[float, float]:
63 x = (value - self.lower) % self.period
64 if x < self.period - x:
65 return x + self.lower, rate
66 return self.period - x + self.lower, -rate
69ReflectiveBounds.registerTag("!openxps.bounds.ReflectiveBounds")