コード例 #1
0
ファイル: TrajectoryTest.py プロジェクト: lulzzz/KMCLib
    def testWriteToFile(self):
        """ Test writing the buffers to file. """
        # 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)

        # Write times, steps and typers.
        times = [1.10045, 2.334156, 3.4516410]
        steps = [12, 25, 52]
        types = [["ThisIsTheLongestTypeNameIHaveEverEncounteredPerhaps",
                  "here", "is", "Next", "Long", "List", "Offffffff", "Names", "now", "this", "one", "is", "longer", "still"],
                 ["A", "B", "C", "D", "E", "F", "G", "H"],
                 ["1", "2", "4", "5", "6", "5" ,"43", "243r2424"]]

        # Needed to prevent test failure.
        MPICommons.barrier()

        # Check that the time is zero before we start.
        self.assertAlmostEqual( t._Trajectory__time_last_dump, 0.0, 10 )

        t._Trajectory__writeToFile(times, steps, types)

        # Needed to prevent test failure.
        MPICommons.barrier()

        # Check that the time stamp was updated.
        self.assertTrue( 1357651850 < t._Trajectory__time_last_dump )
        last_time = t._Trajectory__time_last_dump

        # Check the info stored in the file.
        global_dict = {}
        local_dict  = {}
        execfile(trajectory_filename, global_dict, local_dict)

        # Check the types.
        ret_types = local_dict['types']
        ref_types = [['ThisIsTheLongestTypeNameIHaveEverEncounteredPerhaps',
                      'here', 'is', 'Next', 'Long', 'List', 'Offffffff',
                      'Names', 'now', 'this', 'one', 'is', 'longer', 'still'],
                     ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                     ['1', '2', '4', '5', '6', '5', '43', '243r2424']]

        # Needed to prevent test failure.
        MPICommons.barrier()

        self.assertEqual( ret_types, ref_types )

        # Check the steps.
        ret_steps = local_dict['steps']
        ref_steps = [12, 25, 52]
        self.assertEqual( ret_steps, ref_steps )

        # Check the times.
        ret_times = local_dict['times']
        ref_times = [1.10045, 2.334156, 3.451641]
        self.assertEqual( ret_times, ref_times )

        # Sleep for two seconds before we add again.
        time.sleep(1)
        t._Trajectory__writeToFile(times, steps, types)

        # Check the time.
        self.assertTrue( (t._Trajectory__time_last_dump - last_time > 1) )

        # Now, check the file again.
        global_dict = {}
        local_dict  = {}
        execfile(trajectory_filename, global_dict, local_dict)

        # Check the types.
        ret_types = local_dict['types']
        ref_types = [['ThisIsTheLongestTypeNameIHaveEverEncounteredPerhaps',
                      'here', 'is', 'Next', 'Long', 'List', 'Offffffff',
                      'Names', 'now', 'this', 'one', 'is', 'longer', 'still'],
                     ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                     ['1', '2', '4', '5', '6', '5', '43', '243r2424'],
                     ['ThisIsTheLongestTypeNameIHaveEverEncounteredPerhaps',
                      'here', 'is', 'Next', 'Long', 'List', 'Offffffff',
                      'Names', 'now', 'this', 'one', 'is', 'longer', 'still'],
                     ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
                     ['1', '2', '4', '5', '6', '5', '43', '243r2424']]

        # Needed to prevent test failure.
        MPICommons.barrier()

        self.assertEqual( ret_types, ref_types )

        # Check the steps.
        ret_steps = local_dict['steps']
        ref_steps = [12, 25, 52, 12, 25, 52]
        self.assertEqual( ret_steps, ref_steps )

        # Check the times.
        ret_times = local_dict['times']
        ref_times = [1.10045, 2.334156, 3.451641, 1.10045, 2.334156, 3.451641]
        self.assertEqual( ret_times, ref_times )