Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1import pytest, torch 

2from DebyeCalculator.debye_calculator import DebyeCalculator 

3import numpy as np 

4 

5def test_init_defaults(): 

6 calc = DebyeCalculator() 

7 assert calc.qmin == 1.0, f"Expected qmin to be 1.0, but got {calc.qmin}" 

8 assert calc.qmax == 30.0, f"Expected qmax to be 30.0, but got {calc.qmax}" 

9 assert calc.qstep == 0.1, f"Expected qstep to be 0.1, but got {calc.qstep}" 

10 assert calc.qdamp == 0.04, f"Expected qdamp to be 0.04, but got {calc.qdamp}" 

11 assert calc.rmin == 0.0, f"Expected rmin to be 0.0, but got {calc.rmin}" 

12 assert calc.rmax == 20.0, f"Expected rmax to be 20.0, but got {calc.rmax}" 

13 assert calc.rstep == 0.01, f"Expected rstep to be 0.01, but got {calc.rstep}" 

14 assert calc.rthres == 0.0, f"Expected rthres to be 0.0, but got {calc.rthres}" 

15 assert calc.biso == 0.3, f"Expected biso to be 0.3, but got {calc.biso}" 

16 assert calc.device == 'cuda' if torch.cuda.is_available() else 'cpu', f"Expected device to be 'cuda' or 'cpu', but got {calc.device}" 

17 assert calc.batch_size == 10000, f"Expected batch_size to be 10000, but got {calc.batch_size}" 

18 assert calc.lorch_mod == False, f"Expected lorch_mod to be False, but got {calc.lorch_mod}" 

19 assert calc.radiation_type == 'xray', f"Expected radiation_type to be 'xray', but got {calc.radiation_type}" 

20 assert calc.profile == False, f"Expected profile to be False, but got {calc.profile}" 

21 

22def test_iq(): 

23 # Load the expected scattering intensity from a file 

24 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Iq.dat') 

25 Q_expected, Iq_expected = ph[:,0], ph[:,1] 

26 

27 # Calculate the scattering intensity using the DebyeCalculator 

28 calc = DebyeCalculator() 

29 Q, Iq = calc.iq('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz') 

30 

31 # Check that the calculated scattering intensity matches the expected value 

32 assert np.allclose(Q, Q_expected), f"Expected Q to be {Q_expected}, but got {Q}" 

33 assert np.allclose(Iq, Iq_expected), f"Expected Iq to be {Iq_expected}, but got {Iq}" 

34 

35def test_sq(): 

36 # Load the expected structure factor from a file 

37 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Sq.dat') 

38 Q_expected, sq_expected = ph[:,0], ph[:,1] 

39 

40 # Calculate the structure factor using the DebyeCalculator 

41 calc = DebyeCalculator() 

42 Q, sq = calc.sq('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz') 

43 

44 # Check that the calculated structure factor matches the expected value 

45 assert np.allclose(Q, Q_expected), f"Expected Q to be {Q_expected}, but got {Q}" 

46 assert np.allclose(sq, sq_expected), f"Expected Sq to be {sq_expected}, but got {sq}" 

47 

48def test_fq(): 

49 # Load the expected atomic form factor from a file 

50 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Fq.dat') 

51 Q_expected, fq_expected = ph[:,0], ph[:,1] 

52 

53 # Calculate the atomic form factor using the DebyeCalculator 

54 calc = DebyeCalculator() 

55 Q, fq = calc.fq('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz') 

56 

57 # Check that the calculated atomic form factor matches the expected value 

58 assert np.allclose(Q, Q_expected), f"Expected Q to be {Q_expected}, but got {Q}" 

59 assert np.allclose(fq, fq_expected), f"Expected fq to be {fq_expected}, but got {fq}" 

60 

61def test_gr(): 

62 # Load the expected radial distribution function from a file 

63 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Gr.dat') 

64 r_expected, gr_expected = ph[:,0], ph[:,1] 

65 

66 # Calculate the radial distribution function using the DebyeCalculator 

67 calc = DebyeCalculator() 

68 r, gr = calc.gr('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz') 

69 

70 # Check that the calculated radial distribution function matches the expected value 

71 assert np.allclose(r, r_expected), f"Expected r to be {r_expected}, but got {r}" 

72 assert np.allclose(gr, gr_expected), f"Expected Gr to be {gr_expected}, but got {gr}" 

73 

74def test_get_all(): 

75 # Calculate Iq, Fq, Sq, and Gr using the DebyeCalculator 

76 calc = DebyeCalculator() 

77 r, q, iq, sq, fq, gr = calc._get_all('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz') 

78 

79 # Check that the calculated Iq matches the expected value 

80 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Iq.dat') 

81 q_expected, iq_expected = ph[:,0], ph[:,1] 

82 assert np.allclose(q, q_expected), f"Expected q to be {q_expected}, but got {q}" 

83 assert np.allclose(iq, iq_expected), f"Expected Iq to be {iq_expected}, but got {iq}" 

84 

85 # Check that the calculated Sq matches the expected value 

86 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Sq.dat') 

87 q_expected, sq_expected = ph[:,0], ph[:,1] 

88 assert np.allclose(q, q_expected), f"Expected q to be {q_expected}, but got {q}" 

89 assert np.allclose(sq, sq_expected), f"Expected Sq to be {sq_expected}, but got {sq}" 

90 

91 # Check that the calculated Fq matches the expected value 

92 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Fq.dat') 

93 q_expected, fq_expected = ph[:,0], ph[:,1] 

94 assert np.allclose(q, q_expected), f"Expected q to be {q_expected}, but got {q}" 

95 assert np.allclose(fq, fq_expected), f"Expected Fq to be {fq_expected}, but got {fq}" 

96 

97 # Check that the calculated Gr matches the expected value 

98 ph = np.loadtxt('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal_Gr.dat') 

99 r_expected, gr_expected = ph[:,0], ph[:,1] 

100 assert np.allclose(gr, gr_expected), f"Expected r to be {gr_expected}, but got {gr}" 

101 assert np.allclose(gr, gr_expected), f"Expected Gr to be {gr_expected}, but got {gr}" 

102 

103def test_invalid_input(): 

104 # Test that the DebyeCalculator raises a FileNotFoundError when given a non-existent file 

105 with pytest.raises(FileNotFoundError): 

106 calc = DebyeCalculator() 

107 calc.iq('non_existent_file.xyz') 

108 

109 # Test that the DebyeCalculator raises a ValueError when given invalid input parameters in the iq method 

110 with pytest.raises(ValueError): 

111 calc = DebyeCalculator() 

112 calc.update_parameters(qmin=-1.0) 

113 calc.iq('unittests_files/icsd_001504_cc_r6_lc_2.85_6_tetragonal.xyz')