1818
1919line_file = xb ._pkg_root .parent / 'tests' / 'data' / 'sequence_lhc_run3_b1.json'
2020num_turns = 1000
21+ input_filename = 'xboinc_input.bin'
22+ output_filename = 'sim_state_out.bin'
23+ checkpoint_filename = 'checkpoint.bin'
2124
2225
2326def test_compilation ():
@@ -48,13 +51,13 @@ def test_generate_input():
4851 xb ._skip_xsuite_version_check = True
4952 line , part = _make_input ()
5053 xb .SimConfig .build (line = line , particles = part , num_turns = num_turns , checkpoint_every = 10 ,
51- filename = 'xboinc_input.bin' )
52- input_file = Path .cwd () / "xboinc_input.bin"
54+ filename = input_filename )
55+ input_file = Path .cwd () / input_filename
5356 assert input_file .exists ()
5457 xb ._skip_xsuite_version_check = False
5558
5659
57- def test_track ():
60+ def test_track (request ):
5861 xb ._skip_xsuite_version_check = True
5962 # If no executable is present, make one
6063 exec_file = list (Path .cwd ().glob (f'xboinc_{ xb .app_version } -*' ))
@@ -64,19 +67,22 @@ def test_track():
6467 exec_file = exec_file [0 ]
6568
6669 # If no input file is present, make one
67- input_file = Path .cwd () / 'xboinc_input.bin'
70+ input_file = Path .cwd () / input_filename
6871 if not input_file .exists ():
6972 test_generate_input ()
7073
7174 # run xboinc tracker
7275 t1 = time .time ()
7376 cmd = subprocess .run ([exec_file ])
74- print (round (time .time () - t1 , 1 ))
77+ calculation_time = round (time .time () - t1 , 1 )
78+ request .config .cache .set ('calculation_time' , calculation_time )
7579 if cmd .returncode != 0 :
7680 raise RuntimeError (f"Tracking failed." )
81+ else :
82+ print (f"Tracking done in { calculation_time } s." )
7783
7884 # Read output
79- output_file = Path .cwd () / 'sim_state_out.bin'
85+ output_file = Path .cwd () / output_filename
8086 assert output_file .exists ()
8187 sim_state = xb .SimState .from_binary (output_file )
8288
@@ -93,11 +99,11 @@ def test_track():
9399 assert not np .allclose (part_xboinc .py , part_xboinc .py [0 ], rtol = 1e-4 , atol = 0 )
94100
95101 # Rename file for comparison in next test
96- output_file .rename (output_file .parent / f' { output_file . name } _2' )
102+ output_file .rename (output_file .parent / f" { output_filename } _2" )
97103 xb ._skip_xsuite_version_check = False
98104
99105
100- def test_checkpoint ():
106+ def test_checkpoint (request ):
101107 xb ._skip_xsuite_version_check = True
102108 # If no executable is present, make one
103109 exec_file = list (Path .cwd ().glob (f'xboinc_{ xb .app_version } -*' ))
@@ -107,25 +113,28 @@ def test_checkpoint():
107113 exec_file = exec_file [0 ]
108114
109115 # If no input file is present, make one
110- input_file = Path .cwd () / 'xboinc_input.bin'
116+ input_file = Path .cwd () / input_filename
111117 if not input_file .exists ():
112118 test_generate_input ()
113119
114120 # If previous output file not present, we need to regenerate it (to be able to compare)
115- output_file = Path .cwd () / 'sim_state_out.bin_2'
121+ output_file = Path .cwd () / f" { output_filename } _2"
116122 if not output_file .exists ():
117123 test_track ()
118124
119125 # run xboinc tracker and interrupt halfway
120126 interrupted = False
121- timeout = 25
127+ timeout = 0.6 * request . config . cache . get ( 'calculation_time' , 25 )
122128 print (f"Will interrupt after { timeout } s." )
129+ t1 = time .time ()
130+ calculation_time = round (time .time () - t1 , 1 )
123131 try :
124132 cmd = subprocess .run (exec_file , timeout = timeout )
125133 except subprocess .TimeoutExpired :
126134 interrupted = True
127- print ("Interrupted calculation. Now trying to continue." )
128- checkpoint_file = Path .cwd () / 'checkpoint.bin'
135+ t2 = time .time ()
136+ print (f"Interrupted calculation after { round (t2 - t1 , 1 )} s. Now trying to continue." )
137+ checkpoint_file = Path .cwd () / checkpoint_filename
129138 assert checkpoint_file .exists ()
130139 if not interrupted :
131140 raise ValueError ("Timeout was too short. Adapt the test 'test_checkpoint'." )
@@ -134,18 +143,21 @@ def test_checkpoint():
134143 cmd = subprocess .run (exec_file )
135144 if cmd .returncode != 0 :
136145 raise RuntimeError (f"Tracking failed." )
146+ else :
147+ t3 = time .time ()
148+ print (f"Continued tracking done in { round (t3 - t2 , 1 )} s (total tracking time { round (t3 - t1 , 1 )} s)." )
137149
138150 # Compare file to previous result
139- output_file = Path .cwd () / 'sim_state_out.bin'
151+ output_file = Path .cwd () / output_filename
140152 assert output_file .exists ()
141- assert filecmp .cmp (output_file , output_file .parent / f' { output_file .name } _2' , shallow = False )
153+ assert filecmp .cmp (output_file , output_file .parent / f" { output_file .name } _2" , shallow = False )
142154 xb ._skip_xsuite_version_check = False
143155
144156
145157def test_vs_xtrack ():
146158 xb ._skip_xsuite_version_check = True
147159 # If no output is present, make one
148- output_file = Path .cwd () / 'sim_state_out.bin_2'
160+ output_file = Path .cwd () / f" { output_filename } _2"
149161 if not output_file .exists ():
150162 test_track ()
151163 sim_state = xb .SimState .from_binary (output_file )
0 commit comments