Coverage for tests/test_simbox.py: 97%

68 statements  

« prev     ^ index     » next       coverage.py v7.9.1, created at 2025-06-14 15:55 +0200

1def test_Simbox(): 

2 import gamdpy as gp 

3 import numpy as np 

4 

5 simbox = gp.Orthorhombic(D=3, lengths=np.array([3,4,5])) 

6 assert isinstance(simbox, gp.Orthorhombic), "Problem with Orthorhombic __init__" 

7 assert np.all(simbox.get_lengths() == np.array([3,4,5])), "Problem with Orthorhombic.get_lengths()" 

8 

9 ## Test dist_sq_dr_function 

10 ri = np.array([2.5,3,4]) 

11 rj = np.array([0.5,0.5,0.5]) 

12 dr = 0*ri 

13 result = simbox.get_dist_sq_dr_function()(ri, rj, simbox.data_array, dr) 

14 assert result == 5.5, "Problem with Simbox.dist_sq_dr_function result" 

15 assert np.all( dr == np.array([-1, -1.5, -1.5]) ), "Problem with Orthorhombic.dist_sq_dr_function dr" 

16 

17 ## Test dist_sq_function 

18 result = simbox.get_dist_sq_function()(ri, rj, simbox.data_array) 

19 assert result == 5.5, "Problem with Orthorhombic.dist_sq_function result" 

20 

21 ## Test apply_PBC 

22 # Here the r is out of the box and PBC are applied by increasing img count 

23 r, img = np.array([4,5,6]), np.array([0,1,2]) 

24 simbox.get_apply_PBC()(r, img, simbox.data_array) 

25 assert np.all( r == np.array([1,1,1])), "Problem with Orthorhombic.apply_PBC:r positive img" 

26 assert np.all( img == np.array([1,2,3])), "Problem with Orthorhombic.apply_PBC:img positive img" 

27 # Here the r is inside the box and PBC are applied by decreasing img count 

28 r, img = -np.array([4,5,6]), np.array([0,1,2]) 

29 simbox.get_apply_PBC()(r, img, simbox.data_array) 

30 assert np.all( r == np.array([-1,-1,-1])), "Problem with Orthorhombic.apply_PBC:r negative img" 

31 assert np.all( img == np.array([-1,0,1])), "Problem with Orthorhombic.apply_PBC:img negative img" 

32 

33 ## Test volume 

34 assert simbox.get_volume_function()(simbox.get_lengths())==3*4*5, "Problem with Orthorhombic.volume" 

35 

36 ## Test dist_moved_sq_function 

37 #r_current = np.array([1,1,1]) 

38 #r_last = r_current + 0.75*simbox.get_lengths() 

39 #result = simbox.get_dist_moved_sq_function()(r_current, r_last, simbox.data_array, simbox.data_array) 

40 #assert result == 3.125, "Problem with Orthorhombic.dist_moved_sq_function" 

41 

42 ## Test dist_moved_exceeds_limit_function 

43 r_current = np.array([1,1,1]) 

44 r_last = r_current + 0.50e0 

45 skin, cut = 2.e0, 1.e0 

46 result = simbox.get_dist_moved_exceeds_limit_function()(r_current, r_last, simbox.data_array, simbox.data_array, skin, cut) 

47 assert result == False, "Problem with Orthorhombic.dist_moved_exceeds_limit_function call 1" 

48 r_last = r_current + 0.75e0 

49 result = simbox.get_dist_moved_exceeds_limit_function()(r_current, r_last, simbox.data_array, simbox.data_array, skin, cut) 

50 assert result == True, "Problem with Orthorhombic.dist_moved_exceeds_limit_function call 2" 

51 

52def test_LeesEdwards(): 

53 import pytest 

54 import gamdpy as gp 

55 import numpy as np 

56 import math 

57 

58 # Test 1D error  

59 # https://stackoverflow.com/questions/23337471/how-do-i-properly-assert-that-an-exception-gets-raised-in-pytest 

60 with pytest.raises(ValueError) as e_info: 

61 simbox = gp.LeesEdwards(D=1, lengths=np.array([3,4]), box_shift=1.0) 

62 assert e_info.type is ValueError 

63 

64 # Test normal case 

65 simbox = gp.LeesEdwards(D=3, lengths=np.array([3,4,5]), box_shift=1.0) 

66 assert isinstance(simbox, gp.LeesEdwards), "Problem with LeesEdwards __init__" 

67 assert np.all(simbox.get_lengths() == np.array([3,4,5])), "Problem with LeesEdwards.get_lengths()" 

68 

69 

70 ## Test dist_sq_dr_function 

71 ri = np.array([1.5,0.5,0.5]) 

72 rj = np.array([1.5,3.5,0.5]) 

73 dr = 0*ri 

74 result = simbox.get_dist_sq_dr_function()(ri, rj, simbox.data_array, dr) 

75 assert result == 2., "Problem with LeesEdwards.dist_sq_dr_function result" 

76 assert np.all( dr == np.array([1., 1., 0.]) ), "Problem with LeesEdwards.dist_sq_dr_function dr" 

77 

78 ## Test dist_sq_function 

79 result = simbox.get_dist_sq_function()(ri, rj, simbox.data_array) 

80 assert result == 2., "Problem with LeesEdwards.dist_sq_function result" 

81 

82 

83 ## Test apply_PBC 

84 # Here the r is out of the box and PBC are applied by increasing img count 

85 r, img = np.array([2.0,3.,6.]), np.array([0,1,2]) 

86 simbox.get_apply_PBC()(r, img, simbox.data_array) 

87 assert np.all( r == np.array([1.,-1.,1])), "Problem with LeesEdwards.apply_PBC:r positive img" 

88 assert np.all( img == np.array([0,2,3])), "Problem with LeesEdwards.apply_PBC:img positive img" 

89 

90 

91 ## Test volume 

92 assert simbox.get_volume_function()(simbox.get_lengths())==3*4*5, "Problem with LeesEdwards.volume" 

93 

94 

95 ## Test dist_moved_exceeds_limit_function 

96 r_current = np.array([1,1,1]) 

97 r_last = r_current + 0.50e0 

98 last_data_array = simbox.data_array.copy() 

99 last_data_array[3] -= 0.1 # (assume boxshift has changed by 0.1) 

100 skin, cut = 2.e0, 1.e0 

101 result = simbox.get_dist_moved_exceeds_limit_function()(r_current, r_last, simbox.data_array, last_data_array, skin, cut) 

102 assert result == False, "Problem with LeesEdwards.dist_moved_exceeds_limit_function call 1" 

103 r_last = r_current + 0.75e0 

104 result = simbox.get_dist_moved_exceeds_limit_function()(r_current, r_last, simbox.data_array, last_data_array, skin, cut) 

105 assert result == True, "Problem with LeesEdwards.dist_moved_exceeds_limit_function call 2" 

106 

107 

108if __name__ == '__main__': 

109 test_Simbox() 

110 test_LeesEdwards()