Coverage for tests/test_z_examples.py: 96%

97 statements  

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

1import pytest 

2 

3# These are flags 

4timing = False # Timing of examples 

5rm_out = True # Removing output created by examples 

6if timing: fout = open("timing_examples.txt", "w") 

7 

8@pytest.mark.slow 

9class Test_examples: 

10 ''' This test is testing examples ''' 

11 # LC: examples excluded are LJchain_wall.py, minimal_cpu.py 

12 

13 import sys, os 

14 import numpy as np 

15 main_dir = os.getcwd() 

16 

17 import matplotlib 

18 matplotlib.use('Agg') # Static backend that does not halt on plt.show() 

19 os.environ['MPLBACKEND'] = 'Agg' # Reduced warnings (LC: not anymore, no idea why)  

20 

21 # This function get name and make a test for example with that name 

22 # The function also includes timing 

23 # The list toremove contains files created from the script which need to be removed 

24 def make_one(self, name="", toremove=[]): 

25 import os 

26 import importlib 

27 os.chdir(os.path.join(self.main_dir, "examples")) 

28 if timing: # save start time 

29 import time 

30 start = time.time() 

31 importlib.__import__(f"examples.{name}") 

32 if timing: # save end time  

33 end = time.time() 

34 fout.write(f"{name:30s}\t{end-start:>8.2f}\n") 

35 if rm_out: # remove created files 

36 [os.remove(filename) for filename in toremove] 

37 

38 #def test_analyze_structure(self): # Test does not work because reading from command-line 

39 # self.make_one("analyze_structure", ["Data/LJ_r0.973_T0.70_toread_rdf.pdf", "Data/LJ_r0.973_T0.70_toread_rdf.pkl"]) 

40 

41 # Tests are ordered try to go fast to slow 

42 def test_D2(self): 

43 self.make_one("D2") 

44 

45 def test_bcc_lattice(self): 

46 self.make_one("bcc_lattice") 

47 

48 #def test_blocks(self): Example removed 

49 # self.make_one("blocks") 

50 

51 def test_minimal(self): 

52 self.make_one("minimal") 

53 

54 def test_minimal_NPT(self): 

55 self.make_one("minimal_NPT") 

56 

57 def test_yukawa(self): 

58 self.make_one("yukawa") 

59 

60 def test_calc_rdf_from_h5(self): 

61 self.make_one("calc_rdf_from_h5", ["rdf.dat", "ptype_rdf.dat"]) 

62 

63# def test_calc_rdf_from_rumd3(self): 

64# self.make_one("calc_rdf_from_rumd3", ["rdf_rumd3.dat", "ptype_rdf_rumd3.dat"]) 

65 

66 def test_calc_sq_from_h5(self): 

67 self.make_one("calc_sq_from_h5", ["sq.dat"]) 

68 

69 def test_analyze_thermodynamics(self): 

70 self.make_one("analyze_thermodynamics", ["Data/LJ_r0.973_T0.70_toread_thermodynamics.pdf"]) 

71 

72 def test_analyze_dynamics(self): 

73 self.make_one("analyze_dynamics", ["Data/LJ_r0.973_T0.70_toread_dynamics.pdf","Data/LJ_r0.973_T0.70_toread_dynamics.pkl"]) 

74 

75 def test_analyze_structure(self): 

76 self.make_one("analyze_structure", ["Data/LJ_r0.973_T0.70_toread_rdf.pdf","Data/LJ_r0.973_T0.70_toread_rdf.pkl"]) 

77 

78 def test_ASD(self): 

79 self.make_one("ASD", ["Data/ASD_rho1.863_T0.465.h5"]) 

80 

81 #def test_NVU_RT_kob_andersen(self): 

82 # self.make_one("NVU_RT_kob_andersen") 

83 

84 def test_evaluator_einstein_crystal(self): 

85 self.make_one("evaluator_einstein_crystal") 

86 

87 def test_evaluator_inverse_powerlaw(self): 

88 self.make_one("evaluator_inverse_powerlaw") 

89 

90 def test_structure_factor(self): 

91 self.make_one("structure_factor") 

92 

93 def test_switching_integrator(self): 

94 self.make_one("switching_integrator") 

95 

96 def test_tethered_particles(self): 

97 self.make_one("tethered_particles", ["initial.xyz", "final.xyz"]) 

98 

99 def test_widoms_particle_insertion(self): 

100 self.make_one("widoms_particle_insertion") 

101 

102 def test_LJchain(self): 

103 self.make_one("LJchain", ["Data/LJchain10_Rho1.00_T0.700.h5"]) 

104 

105 def test_write_to_lammps(self): 

106 self.make_one("write_to_lammps", ["dump.initial", "dump.lammps"]) 

107 

108 def test_thermodynamics(self): 

109 self.make_one("thermodynamics") 

110 

111 def test_generic_molecules(self): 

112 self.make_one("generic_molecules") 

113 

114 def test_LJ(self): 

115 self.make_one("LJ") 

116 

117 def test_D4(self): 

118 self.make_one("D4") 

119 

120 def test_D8(self): 

121 self.make_one("D8") 

122 

123 def test_poiseuille(self): 

124 self.make_one("poiseuille", ["HydrodynamicProfile.dat", "initial.xyz", "final.xyz"]) 

125 

126 def test_isochore(self): 

127 self.make_one("isochore", ["Data/LJ_r0.973_T0.70.h5", "Data/LJ_r0.973_T1.10.h5", "Data/LJ_r0.973_T1.50.h5"]) 

128 

129 def test_shear_SLLOD(self): 

130 self.make_one("shear_SLLOD", ["shear_run.txt"]) 

131 

132 def test_hydrocorr(self): 

133 self.make_one("hydrocorr", ["jacf.dat", "dacf.dat"]) 

134 

135 def test_isomorph(self): # Note: this script produces Data/isomorph.pkl needed by next two. File gets remove in next 

136 self.make_one("isomorph") 

137 

138 def test_plot_isomorph_rdf(self): 

139 self.make_one("plot_isomorph_rdf", ["isomorph_rdf.pdf"]) 

140 

141 def test_plot_isomorph_dynamics(self): 

142 self.make_one("plot_isomorph_dynamics", ["Data/isomorph.pkl", "isomorph_dynamics.pdf"]) 

143 

144 def test_consistency_NPT(self): 

145 self.make_one("consistency_NPT") 

146 

147 def test_kablj(self): 

148 self.make_one("kablj", ["Data/KABLJ_Rho1.200_T0.800.h5"]) 

149 

150 def test_molecules(self): 

151 self.make_one("molecules", ["Data/molecules.h5", "Data/dump.lammps", "Data/dump_compress.lammps", "molecule.pdf"]) 

152 

153 def test_rubber_cube(self): 

154 self.make_one("rubber_cube", ['Data/rubber_cube.lammps']) 

155 

156 def test_molecules_polydisperse(self): 

157 self.make_one("molecules_polydisperse", ["Data/chains_poly.h5", "Data/dump.lammps", "Data/dump_compress.lammps", "chain10.pdf", "chain5.pdf"]) 

158 

159