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

13 statements  

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

1""" 

2Circular boundary condition. 

3 

4.. module:: openxps.bounds.circular 

5 :platform: Linux, MacOS, Windows 

6 :synopsis: Circular boundary condition 

7 

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

9 

10""" 

11 

12import math 

13 

14import numpy as np 

15from openmm import unit as mmunit 

16 

17from .periodic import PeriodicBounds 

18 

19 

20class CircularBounds(PeriodicBounds): 

21 """ 

22 A circular boundary condition equivalent to PeriodicBounds(-π, π, radians). 

23 

24 This is a convenience class for the common case of a periodic boundary condition 

25 spanning from -π to π radians, which corresponds to a full circle. 

26 

27 Parameters 

28 ---------- 

29 lower, upper, unit 

30 Optional arguments for internal use (e.g., during deserialization). 

31 If not provided, defaults to -π, π, radians respectively. 

32 

33 Example 

34 ------- 

35 >>> import openxps as xps 

36 >>> bounds = xps.bounds.CircularBounds() 

37 >>> bounds.lower == -3.141592653589793 

38 True 

39 >>> bounds.upper == 3.141592653589793 

40 True 

41 >>> bounds.unit == xps.bounds.CircularBounds().unit 

42 True 

43 >>> from openmm import unit 

44 >>> bounds.convert(unit.degree) 

45 CircularBounds(lower=-180.0, upper=180.0, unit=deg) 

46 """ 

47 

48 def __init__( 

49 self, 

50 lower: float = -np.pi, 

51 upper: float = np.pi, 

52 unit: mmunit.Unit = mmunit.radians, 

53 ) -> None: 

54 super().__init__(lower, upper, unit) 

55 if not self.unit.is_compatible(mmunit.radians): 

56 raise ValueError("Circular bounds must have an angle unit.") 

57 factor = self.unit.conversion_factor_to(mmunit.radians) 

58 if not ( 

59 math.isclose(self.lower * factor, -np.pi, rel_tol=1e-9) 

60 and math.isclose(self.upper * factor, np.pi, rel_tol=1e-9) 

61 ): 

62 raise ValueError("Circular bounds must span from -π to π radians.") 

63 

64 

65CircularBounds.registerTag("!openxps.bounds.CircularBounds")