def testAppendAndFlushTime(self): """ Test that the append function flushes when enough time has passed. """ # Setup data. simulation_time = 12.3 step = 14 class Config: def __init__(self): pass configuration = Config() # Instantiate a trajectory. t = Trajectory(trajectory_filename="filename") # Add the needed functions. def storeData(*args, **kwargs): pass t._storeData = storeData ret_buffer = 10.0 def bufferSize(*args, **kwargs): return ret_buffer t._bufferSize = bufferSize class Dummy: self.fc = False d = Dummy() def flush(): d.fc = True t.flush = flush # Append. t.append(simulation_time, step, configuration) # The first time allways flushes. self.assertTrue(d.fc) # Append again. d.fc = False t.append(simulation_time, step, configuration) self.assertFalse(d.fc) # Change the value of the max buffer time so that flush() gets called. t._Trajectory__max_buffer_time = 0.0 # Append. t.append(simulation_time, step, configuration) # Check. self.assertTrue(d.fc)
def testAppendTimeUpdate(self): """ Test that the append function updates the time only when it flushes. """ # Setup data. simulation_time = 12.3 step = 14 class Config: def __init__(self): pass configuration = Config() # Instantiate a trajectory. t = Trajectory(trajectory_filename="filename") # Add the needed functions. def storeData(*args, **kwargs): pass t._storeData = storeData ret_buffer = 10.0 def bufferSize(*args, **kwargs): return ret_buffer t._bufferSize = bufferSize class Dummy: self.fc = False d = Dummy() def flush(): d.fc = True t.flush = flush # Append. t.append(simulation_time, step, configuration) # The first time allways flushes. self.assertTrue(d.fc) # This updates the time last dump variable. ref_time_last_dump = t._Trajectory__time_last_dump # Sleep 10 ms. time.sleep(0.01) # Append again. d.fc = False t.append(simulation_time, step, configuration) self.assertFalse(d.fc) # This does not update the time last dump. self.assertAlmostEqual(ref_time_last_dump, t._Trajectory__time_last_dump, 10) # Change the value of the max buffer time so that flush() gets called. t._Trajectory__max_buffer_time = 0.0 # Sleep 10 ms. time.sleep(0.01) # Append. t.append(simulation_time, step, configuration) # Check. self.assertTrue(d.fc) # The time last dump should have been updated. self.assertTrue( (t._Trajectory__time_last_dump - ref_time_last_dump) > 0.0001 )
def testAppend(self): """ Test appending to the trajectory. """ # Setup input. sites = [[0.0,1.0,2.3], [1.0,0.0,2.3], [1.0,1.0,0.3], [1.0,1.0,2.3], [3.4,4.5,4.3], [3.4,4.3,4.3], [3.4,5.5,4.3], [3.7,7.5,6.5]] name = os.path.abspath(os.path.dirname(__file__)) name = os.path.join(name, "..", "..") name = os.path.join(name, "TestUtilities", "Scratch") trajectory_filename = os.path.join(name, "tmp_trajectory_file.py") self.__files_to_remove.append(trajectory_filename) # Construct. t = Trajectory(trajectory_filename, sites) # Append times, steps and typers. t.append(1.10045, 12, ["A", "A", "A", "A", "A", "A"] ) # Since this was the first time it should have triggered a dump to file. global_dict = {} local_dict = {} execfile(trajectory_filename, global_dict, local_dict) # Needed to prevent test failure. MPICommons.barrier() # Check the types. ret_types = local_dict['types'] ref_types = [["A", "A", "A", "A", "A", "A"]] self.assertEqual( ret_types, ref_types ) # Check the steps. ret_steps = local_dict['steps'] ref_steps = [12] self.assertEqual( ret_steps, ref_steps ) # Check the times. ret_times = local_dict['times'] ref_times = [1.10045] self.assertEqual( ret_times, ref_times ) # Appending again directly makes no dump. t.append(1.1993, 14, ["B", "B", "B", "B", "B", "B"] ) global_dict = {} local_dict = {} execfile(trajectory_filename, global_dict, local_dict) # Needed to prevent test failure. MPICommons.barrier() # Check. ret_types = local_dict['types'] self.assertEqual( ret_types, ref_types ) ret_steps = local_dict['steps'] self.assertEqual( ret_steps, ref_steps ) ret_times = local_dict['times'] self.assertEqual( ret_times, ref_times ) # But if we dump again and set the time limit to zero we will trigger a dump. t._Trajectory__max_buffer_time = 0.0 t.append(1.199, 19, ["C", "C", "C", "C", "C", "C"] ) # Reset the time to some thing large. t._Trajectory__max_buffer_time = 100000000000.0 global_dict = {} local_dict = {} execfile(trajectory_filename, global_dict, local_dict) # Check the types. ret_types = local_dict['types'] ref_types = [["A","A","A","A","A","A"], ["B","B","B","B","B","B"], ["C","C","C","C","C","C"]] # Needed to prevent test failure. MPICommons.barrier() self.assertEqual( ret_types, ref_types ) # Check the steps. ret_steps = local_dict['steps'] ref_steps = [12,14,19] self.assertEqual( ret_steps, ref_steps ) # Check the times. ret_times = local_dict['times'] ref_times = [1.10045, 1.1993, 1.199] self.assertEqual( ret_times, ref_times ) # The buffers are reset after each dump. If we make the # max size limit smaller than the size of the appended types # list this must trigger a dump. types = ["A","A","A","A","A","B"] size = sys.getsizeof(types) t._Trajectory__max_buffer_size = size # Append. t.append(1.1995, 43, types ) # Check. global_dict = {} local_dict = {} execfile(trajectory_filename, global_dict, local_dict) ret_types = local_dict['types'] ref_types = [["A","A","A","A","A","A"], ["B","B","B","B","B","B"], ["C","C","C","C","C","C"]] # Needed to prevent test failure. MPICommons.barrier() self.assertEqual( ret_types, ref_types ) # Append. t.append(1.1995, 43, types ) t.append(1.1995, 43, types ) t.append(1.1995, 43, types ) # This last one triggers the dump. t.append(1.1995, 43, types ) # Check. global_dict = {} local_dict = {} execfile(trajectory_filename, global_dict, local_dict) ret_types = local_dict['types'] ref_types = [["A","A","A","A","A","A"], ["B","B","B","B","B","B"], ["C","C","C","C","C","C"], ["A","A","A","A","A","B"], ["A","A","A","A","A","B"], ["A","A","A","A","A","B"], ["A","A","A","A","A","B"], ["A","A","A","A","A","B"]] # Needed to prevent test failure. MPICommons.barrier() self.assertEqual( ret_types, ref_types )
def testAppendTimeUpdate(self): """ Test that the append function updates the time only when it flushes. """ # Setup data. simulation_time = 12.3 step = 14 class Config: def __init__(self): pass configuration = Config() # Instantiate a trajectory. t = Trajectory(trajectory_filename="filename") # Add the needed functions. def storeData(*args, **kwargs): pass t._storeData = storeData ret_buffer = 10.0 def bufferSize(*args, **kwargs): return ret_buffer t._bufferSize = bufferSize class Dummy: self.fc = False d = Dummy() def flush(): d.fc = True t.flush = flush # Append. t.append(simulation_time, step, configuration) # The first time allways flushes. self.assertTrue(d.fc) # This updates the time last dump variable. ref_time_last_dump = t._Trajectory__time_last_dump # Sleep 10 ms. time.sleep(0.01) # Append again. d.fc = False t.append(simulation_time, step, configuration) self.assertFalse(d.fc) # This does not update the time last dump. self.assertAlmostEqual(ref_time_last_dump, t._Trajectory__time_last_dump, 10) # Change the value of the max buffer time so that flush() gets called. t._Trajectory__max_buffer_time = 0.0 # Sleep 10 ms. time.sleep(0.01) # Append. t.append(simulation_time, step, configuration) # Check. self.assertTrue(d.fc) # The time last dump should have been updated. self.assertTrue( (t._Trajectory__time_last_dump - ref_time_last_dump) > 0.0001)