Coverage for openxps/bounds/reflective.py: 100%

24 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-22 22:23 +0000

1""" 

2Reflective boundary condition. 

3 

4.. module:: openxps.bounds.reflective 

5 :platform: Linux, MacOS, Windows 

6 :synopsis: Reflective boundary condition 

7 

8.. classauthor:: Charlles Abreu <craabreu@gmail.com> 

9 

10""" 

11 

12import typing as t 

13 

14from .base import Bounds 

15 

16 

17class ReflectiveBounds(Bounds): 

18 """ 

19 A reflective boundary condition. The dynamical variable collides elastically 

20 with the upper and lower bounds and is reflected back into the range. 

21 

22 Parameters 

23 ---------- 

24 lower 

25 The lower bound for the dynamical variable. 

26 upper 

27 The upper bound for the dynamical variable. 

28 unit 

29 The unit of measurement of the bounds. If the bounds do not have a unit, use 

30 ``dimensionless``. 

31 

32 Example 

33 ------- 

34 >>> import openxps as xps 

35 >>> from openmm import unit 

36 >>> bounds = xps.ReflectiveBounds(1, 10, unit.angstrom) 

37 >>> bounds == xps.ReflectiveBounds(0.1, 1, unit.nanometer) 

38 True 

39 >>> print(bounds) 

40 ReflectiveBounds(lower=1.0, upper=10.0, unit=A) 

41 """ 

42 

43 kind: t.ClassVar[str] = "reflective" 

44 

45 def __post_init__(self) -> None: 

46 super().__post_init__() 

47 self.period = 2 * (self.upper - self.lower) 

48 

49 def leptonExpression(self, variable: str) -> str: 

50 scaled = f"scaled_{variable}" 

51 wrapped = f"wrapped_{variable}" 

52 if self.lower == 0: 

53 shift = deshift = "" 

54 elif self.lower > 0: 

55 shift = f"-{self.lower}" 

56 deshift = f"+{self.lower}" 

57 else: 

58 shift = f"+{-self.lower}" 

59 deshift = f"-{-self.lower}" 

60 return ( 

61 f"min({wrapped},1-{wrapped})*{self.period}{deshift}" 

62 f";\n{wrapped}={scaled}-floor({scaled})" 

63 f";\n{scaled}=({variable}{shift})/{self.period}" 

64 ) 

65 

66 def wrap(self, value: float, rate: float) -> tuple[float, float]: 

67 x = (value - self.lower) % self.period 

68 if x < self.period - x: 

69 return x + self.lower, rate 

70 return self.period - x + self.lower, -rate 

71 

72 

73ReflectiveBounds.registerTag("!openxps.bounds.ReflectiveBounds")