Пример #1
0
    def testConstructionWithCustomRates(self):
        """ Test construction with custom rates. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        types0 = ["A", "*", "B"]
        types1 = ["B", "*", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Test that it fails with the wrong rate calculator.
        interactions = KMCInteractions(processes=processes,
                                       implicit_wildcards=True)

        # Test that it fails if the rate calculator is of wrong type.
        self.assertRaises(
            Error, lambda: interactions.setRateCalculator(
                rate_calculator="CustomRateCalculator"))

        # Test that it fails if the rate calculator is instantiated.
        self.assertRaises(
            Error, lambda: interactions.setRateCalculator(
                rate_calculator=CustomRateCalculator("DummyConfig")))

        # Construct the interactions object with a custom rate calculator class.
        kmc_interactions = KMCInteractions(processes=processes,
                                           implicit_wildcards=True)
        kmc_interactions.setRateCalculator(
            rate_calculator=CustomRateCalculator)

        # Test the stored name.
        self.assertEqual(
            kmc_interactions._KMCInteractions__rate_calculator_str,
            "CustomRateCalculator")

        # Test the stored rate calculator class.
        self.assertTrue(
            kmc_interactions._KMCInteractions__rate_calculator_class ==
            CustomRateCalculator)
Пример #2
0
    def testConstruction(self):
        """ Test that the KMCInteractions class can be constructed. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        types0 = ["A", "*", "B"]
        types1 = ["B", "*", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        # Check the default implicit wildcard.
        self.assertTrue(kmc_interactions.implicitWildcards())

        # Construct again with non default value.
        kmc_interactions = KMCInteractions(processes=processes,
                                           implicit_wildcards=False)

        # Check the wildcard again.
        self.assertFalse(kmc_interactions.implicitWildcards())

        # Check the processes stored on the object.
        stored_processes = kmc_interactions._KMCInteractions__processes

        # Checks that the address is the same.
        self.assertTrue(stored_processes == processes)
Пример #3
0
    def testConstructionBuckets(self):
        """ Test that the KMCInteractions class can be constructed. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        minimum_match = ["A", "*", "B"]
        update = [[(-1, "A"), (1, "B")], "*", [(1, "A"), (-1, "B")]]
        rate_0_1 = 3.5
        process_0 = KMCBucketProcess(coordinates=coords,
                                     minimum_match=minimum_match,
                                     update=update,
                                     basis_sites=[0, 1, 3],
                                     rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        minimum_match = ["A", "*", "C"]
        update = [[(-1, "A"), (1, "C")], "*", [(1, "A"), (-1, "C")]]
        rate_0_1 = 1.5
        process_1 = KMCBucketProcess(coordinates=coords,
                                     minimum_match=minimum_match,
                                     update=update,
                                     basis_sites=[0, 1, 3],
                                     rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        # Check the default implicit wildcard.
        self.assertTrue(kmc_interactions.implicitWildcards())

        # Construct again with non default value.
        kmc_interactions = KMCInteractions(processes=processes,
                                           implicit_wildcards=False)

        # Check the wildcard again.
        self.assertFalse(kmc_interactions.implicitWildcards())

        # Check the processes stored on the object.
        stored_processes = kmc_interactions._KMCInteractions__processes

        # Checks that the address is the same.
        self.assertTrue(stored_processes == processes)
Пример #4
0
    def testConstructionFailsWrongWildcard(self):
        """ Test that we fail to construct with other than bool input for the implicit wildcard flag """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        types0 = ["A", "*", "B"]
        types1 = ["B", "*", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct and fail.
        self.assertRaises(
            Error,
            lambda: KMCInteractions(processes=processes, implicit_wildcards=0))

        self.assertRaises(
            Error, lambda: KMCInteractions(processes=processes,
                                           implicit_wildcards="True"))

        self.assertRaises(
            Error, lambda: KMCInteractions(processes=processes,
                                           implicit_wildcards=[False]))
Пример #5
0
 def testConstructionFailNoList(self):
     """ Test that the construction fails if the interactions list is not a list. """
     # Setup.
     coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
     types0 = ["A", "*", "B"]
     types1 = ["B", "*", "A"]
     rate_0_1 = 3.5
     process_0 = KMCProcess(coords,
                            types0,
                            types1,
                            basis_sites=[0, 1, 3],
                            rate_constant=rate_0_1)
     # Construct and fail.
     self.assertRaises(Error, lambda: KMCInteractions(processes=process_0))
Пример #6
0
    def testBackendNoFailWrongBasisMatch(self):
        """ Test for no failure when constructing backend with wrong n_basis """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "B"]
        types1 = ["B", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 4],
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        # Setup a dict with the possible types.
        possible_types = {
            "A": 13,
            "D": 2,
            "B": 3,
            "J": 4,
            "C": 5,
        }

        # Use a dummy argument for the configuration.
        config = "DummyConfig"

        # Get the backend - The [0,4] sites list for process_0 is simply ignored.
        cpp_interactions = kmc_interactions._backend(possible_types, 2, config)

        self.assertEqual(len(cpp_interactions.processes()[0].basisSites()), 1)
        self.assertEqual(cpp_interactions.processes()[0].basisSites()[0], 0)

        self.assertEqual(len(cpp_interactions.processes()[1].basisSites()), 2)
        self.assertEqual(cpp_interactions.processes()[1].basisSites()[0], 0)
        self.assertEqual(cpp_interactions.processes()[1].basisSites()[1], 1)
Пример #7
0
    def testBackendBuckets(self):
        """
        Test that the backend behaves as expected with bucket processes.
        """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        minimum_match = ["A", "*", "B"]
        update = [[(-1, "A"), (1, "B")], [(0, "*")], [(1, "A"), (-1, "B")]]
        rate_0_1 = 3.5
        process_0 = KMCBucketProcess(coordinates=coords,
                                     minimum_match=minimum_match,
                                     update=update,
                                     basis_sites=[0, 1, 3],
                                     rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        minimum_match = ["A", "*", "C"]
        update = [[(-1, "A"), (1, "C")], [(0, "*")], [(1, "A"), (-1, "C")]]
        rate_0_1 = 1.5
        process_1 = KMCBucketProcess(coordinates=coords,
                                     minimum_match=minimum_match,
                                     update=update,
                                     basis_sites=[0, 1, 3],
                                     rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        # Setup a dict with the possible types.
        possible_types = {
            "A": 0,
            "B": 1,
            "J": 2,
            "C": 3,
        }

        # Use a dummy argument for the configuration.
        config = "DummyConfig"

        # Check that setting up the backend fails if we have types in the processes that do
        # not corresponds to a type in the list of possible types.
        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        possible_types = {
            "A": 0,
            "B": 1,
            "D": 2,
            "J": 3,
            "C": 4,
        }

        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        possible_types = {
            "A": 0,
            "B": 1,
            "F": 2,
            "J": 3,
            "C": 4,
        }

        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        # Both "D" and "F" must be present, and the widcard "*".
        possible_types["D"] = 5
        possible_types["*"] = 6

        # Get the backend.
        cpp_interactions = kmc_interactions._backend(possible_types, 2, config)

        # Check the type.
        self.assertTrue(isinstance(cpp_interactions, Backend.Interactions))

        # Get the processes out.
        cpp_processes = cpp_interactions.processes()

        # Check the length of the processes.
        self.assertEqual(cpp_processes.size(), 2)

        # Get the elements out of the second process.
        update_types = cpp_processes[1].processMatchList()[0].update_types

        # This is an "A"
        match_types = cpp_processes[1].processMatchList()[0].match_types
        self.assertEqual(match_types[0], 1)
        self.assertEqual(match_types[1], 0)
        self.assertEqual(match_types[2], 0)
        self.assertEqual(match_types[3], 0)
        self.assertEqual(match_types[4], 0)
        self.assertEqual(match_types[5], 0)
        self.assertEqual(match_types[6], 0)

        # This is an "C"
        match_types = cpp_processes[1].processMatchList()[1].match_types
        self.assertEqual(match_types[0], 0)
        self.assertEqual(match_types[1], 0)
        self.assertEqual(match_types[2], 0)
        self.assertEqual(match_types[3], 0)
        self.assertEqual(match_types[4], 1)
        self.assertEqual(match_types[5], 0)
        self.assertEqual(match_types[6], 0)

        # This is a "*"
        match_types = cpp_processes[1].processMatchList()[2].match_types
        self.assertEqual(match_types[0], 0)
        self.assertEqual(match_types[1], 0)
        self.assertEqual(match_types[2], 0)
        self.assertEqual(match_types[3], 0)
        self.assertEqual(match_types[4], 0)
        self.assertEqual(match_types[5], 0)
        self.assertEqual(match_types[6], 1)
Пример #8
0
    def testBackend(self):
        """
        Test that the generated backend object is what we expect.
        """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [12.0, 13.0, -1.0], [1.1, 1.2, 1.3]]
        types0 = ["A", "D", "B"]
        types1 = ["B", "F", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0, 1, 3],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        # Setup a dict with the possible types.
        possible_types = {
            "A": 13,
            "B": 3,
            "J": 4,
            "C": 5,
        }

        # Use a dummy argument for the configuration.
        config = "DummyConfig"

        # Check that setting up the backend fails if we have types in the processes that do
        # not corresponds to a type in the list of possible types.
        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        possible_types = {
            "A": 13,
            "B": 3,
            "D": 2,
            "J": 4,
            "C": 5,
        }

        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        possible_types = {
            "A": 13,
            "B": 3,
            "F": 2,
            "J": 4,
            "C": 5,
        }

        self.assertRaises(
            Error,
            lambda: kmc_interactions._backend(possible_types, 2, config))

        # Both "D" and "F" must be present.
        possible_types["D"] = 123

        # Get the backend.
        cpp_interactions = kmc_interactions._backend(possible_types, 2, config)

        # Check the type.
        self.assertTrue(isinstance(cpp_interactions, Backend.Interactions))

        # Get the processes out.
        cpp_processes = cpp_interactions.processes()

        # Check the length of the processes.
        self.assertEqual(cpp_processes.size(), 2)

        # Get the elements out of the second process.
        match_types = cpp_processes[1].processMatchList()[0].match_types
        update_types = cpp_processes[1].processMatchList()[0].update_types

        # Match type should be "A" -> 15 and update type "C" -> 5
        self.assertEqual(match_types[13], 1)
        self.assertEqual(update_types[5], 1)

        # Get the elements out of the second process.
        match_types = cpp_processes[1].processMatchList()[1].match_types
        update_types = cpp_processes[1].processMatchList()[1].update_types

        # Match type should be "C" -> 5 and update type "A" -> 13
        self.assertEqual(match_types[5], 1)
        self.assertEqual(update_types[13], 1)
Пример #9
0
    def testBackendWithCustomRates(self):
        """ Test that we can construct the backend object with custom rates. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "B"]
        types1 = ["B", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Set the custom rates class to use.
        custom_rates_class = CustomRateCalculator

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes,
                                           implicit_wildcards=False)
        kmc_interactions.setRateCalculator(rate_calculator=custom_rates_class)

        # Set the rate function on the custom rates calculator for testing.
        ref_rnd = numpy.random.uniform(0.0, 1.0)

        def testRateFunction(coords, types_before, types_after, rate_const,
                             process_number, global_coordinate):
            return ref_rnd

        kmc_interactions._KMCInteractions__rate_calculator.rate = testRateFunction

        # Setup a dict with the possible types.
        possible_types = {
            "A": 13,
            "D": 2,
            "B": 3,
            "J": 4,
            "C": 5,
        }

        # Construct the backend.
        cpp_interactions = kmc_interactions._backend(possible_types, 2)

        # Get the rate calculator reference out of the C++ object and
        # check that a call from C++ calls the Python extension.
        cpp_coords = Backend.StdVectorDouble()
        cpp_types1 = Backend.StdVectorString()
        cpp_types2 = Backend.StdVectorString()
        rate_constant = 543.2211
        process_number = 33
        coordinate = (1.2, 3.4, 5.6)

        self.assertAlmostEqual(
            cpp_interactions.rateCalculator().backendRateCallback(
                cpp_coords,
                cpp_coords.size() / 3, cpp_types1, cpp_types2, rate_constant,
                process_number, coordinate[0], coordinate[1], coordinate[2]),
            ref_rnd, 12)
        self.assertAlmostEqual(
            kmc_interactions._KMCInteractions__rate_calculator.
            backendRateCallback(cpp_coords,
                                cpp_coords.size() / 3, cpp_types1, cpp_types2,
                                rate_constant, process_number, coordinate[0],
                                coordinate[1], coordinate[2]), ref_rnd, 12)

        # Construct a C++ RateCalculator object directly and check that this object
        # returns the rate given to it.
        cpp_rate_calculator = Backend.RateCalculator()
        self.assertAlmostEqual(
            cpp_rate_calculator.backendRateCallback(
                cpp_coords,
                cpp_coords.size() / 3, cpp_types1, cpp_types2, rate_constant,
                process_number, coordinate[0], coordinate[1], coordinate[2]),
            rate_constant, 12)
Пример #10
0
    def testRun2(self):
        """ Test the run of an A-B flip model. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(10,10,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*100
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the model.
        ab_flip_model = KMCLatticeModel(configuration, interactions)

        # Run the model with a trajectory file.
        name = os.path.abspath(os.path.dirname(__file__))
        name = os.path.join(name, "..", "TestUtilities", "Scratch")
        trajectory_filename = os.path.join(name, "ab_flip_traj.py")
        self.__files_to_remove.append(trajectory_filename)

        # The control parameters.
        control_parameters = KMCControlParameters(number_of_steps=1000,
                                                  dump_interval=500,
                                                  seed=2013)

        # Run the model for 1000 steps.
        ab_flip_model.run(control_parameters,
                          trajectory_filename=trajectory_filename)

        # Read the first last frames from the trajectory file and check that
        # the fraction of A is close to 20% in the last, and 0 in the first.
        if MPICommons.isMaster():
            global_dict = {}
            local_dict  = {}
            execfile(trajectory_filename, global_dict, local_dict)

            # Count the first frame.
            elem = local_dict["types"][0]
            nA = len([ ee for ee in elem if ee == "A" ])
            nB = len([ ee for ee in elem if ee == "B" ])
            self.assertEqual(nA, 0)
            self.assertEqual(nB, 100)

            # Count the last frame.
            elem = local_dict["types"][-1]
            nA = len([ ee for ee in elem if ee == "A" ])
            nB = len([ ee for ee in elem if ee == "B" ])

            # Note that the average should be 20.0% over a long run.
            # It is pure luck that it is exact at this particular
            # step with the presently used random number seed.
            fraction =  nA * 100.0 / (nA + nB)
            target = 20.0
            self.assertAlmostEqual(fraction, target, 3)
Пример #11
0
    def testRunImplicitWildcards(self):
        """ Test that ta valid model can run for a few steps. """
        # Setup a unitcell.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[1.0,0.0,0.0],
                                                          [0.0,1.0,0.0],
                                                          [0.0,0.0,1.0]]),
                                basis_points=[[0.0,0.0,0.0]])
        # And a lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(10,10,1),
                             periodic=(True,True,False))

        # Set the stating configuration types.
        types = ['B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'A', 'B', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                 'B', 'A', 'B', 'B', 'B', 'B', 'B', 'A', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'A', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B',
                 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'A', 'B', 'B',
                 'A', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'B', 'A']

        # Setup the configuration.
        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=["A","B"])

        # Generate the interactions with a distance so large that we get a
        # layer of implicite wildcards in the C++ matchlists.
        sites = [0]
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
                       [   1.000000e+00,   0.000000e+00,   0.000000e+00],
                       [  -1.000000e+00,   0.000000e+00,   0.000000e+00],
                       [   0.000000e+00,  -1.000000e+00,   0.000000e+00],
                       [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                       [   2.000000e+00,   2.000000e+00,   0.000000e+00]]

        types0 = ['A', 'B', 'B', 'B', 'B', 'A']
        types1 = ['B', 'B', 'A', 'B', 'B', 'A']
        process_0 = KMCProcess(coordinates, types0, types1, None, sites, 1.0)

        types0 = ['A', 'B', 'B', 'B', 'B', 'B']
        types1 = ['B', 'B', 'A', 'B', 'B', 'B']
        process_1 = KMCProcess(coordinates, types0, types1, None, sites, 1.0)

        types0 = ['A', 'B', 'B', 'B', 'B', 'B']
        types1 = ['B', 'B', 'B', 'A', 'B', 'B']
        process_2 = KMCProcess(coordinates, types0, types1, None, sites, 1.0)

        types0 = ['A', 'B', 'B', 'B', 'B', 'B']
        types1 = ['B', 'B', 'B', 'B', 'A', 'B']
        process_3 = KMCProcess(coordinates, types0, types1, None, sites, 1.0)

        # Processes.
        processes = [process_0,
                     process_1,
                     process_2,
                     process_3]

        # No implicit wildcards.
        interactions = KMCInteractions(processes=processes,
                                       implicit_wildcards=False)

        # Create the model.
        model = KMCLatticeModel(config, interactions)

        # Get the match types out.
        match_types = [ l.match_type for l in model._backend().interactions().processes()[0].minimalMatchList() ]

        # This does not have wildcards added.
        ref_match_types = [1, 2, 2, 2, 2, 1]
        self.assertEqual( match_types, ref_match_types )

        # Create with implicit wildcards - this is default behavior.
        interactions = KMCInteractions(processes=processes)

        # Create the model.
        model = KMCLatticeModel(config, interactions)

        # Check the process matchlists again.
        match_types = [ l.match_type for l in model._backend().interactions().processes()[0].minimalMatchList() ]

        ref_match_types = [1, 2, 2, 2, 2,
                           0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0,
                           0, 0, 0, 0, 0,
                           0, 0, 0, 0, 1]

        # This one has the wildcards (zeroes) added.
        self.assertEqual( match_types, ref_match_types )

        # Setup the run paramters.
        control_parameters = KMCControlParameters(number_of_steps=10,
                                                  dump_interval=1)
        model.run(control_parameters)
Пример #12
0
    def testRunFailAnalysis(self):
        """ Test that the analyis plugins get called correctly. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(10,10,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*100
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the model.
        ab_flip_model = KMCLatticeModel(configuration, interactions)

        # The control parameters.
        control_parameters = KMCControlParameters(number_of_steps=1000,
                                                  dump_interval=500)

        # Setup a valid minimal analysis object.
        class AnalysisProxy1(KMCAnalysisPlugin):
            def __init__(self):
                pass

        # Fail because of not instantitated analysis.
        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      analysis=[AnalysisProxy1]) )
        # Fail because of empty analyis list.
        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      analysis=[]) )
        # Fail because of not list.
        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      analysis=AnalysisProxy1()) )
        # Fail because of wrong type.
        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      analysis=[AnalysisProxy1(), "AP3"]) )
Пример #13
0
    def testCustomRatesRun(self):
        """ Test the run of an A-B flip model with custom rates. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(10,10,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*100
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates, ['A'], ['B'], None, [0], 4.0)
        process_1 = KMCProcess(coordinates, ['B'], ['A'], None, [0], 1.0)
        processes = [process_0, process_1]
        interactions = KMCInteractions(processes,
                                       implicit_wildcards=True)

        # Custom rates.
        rate_calculator = CustomRateCalculator
        interactions.setRateCalculator(rate_calculator)

        # Setup the model.
        ab_flip_model = KMCLatticeModel(configuration, interactions)

        # Run the model with a trajectory file.
        name = os.path.abspath(os.path.dirname(__file__))
        name = os.path.join(name, "..", "TestUtilities", "Scratch")
        trajectory_filename = os.path.join(name, "ab_flip_traj_custom.py")
        self.__files_to_remove.append(trajectory_filename)

        # The control parameters.
        control_parameters = KMCControlParameters(number_of_steps=1000,
                                                  dump_interval=500,
                                                  seed=2013)

        # Run the model for 1000 steps.
        ab_flip_model.run(control_parameters,
                          trajectory_filename=trajectory_filename)

        # Read the first last frames from the trajectory file and check that
        # the fraction of A is close to 75% in the last, and 0 in the first.
        if MPICommons.isMaster():
            global_dict = {}
            local_dict  = {}
            execfile(trajectory_filename, global_dict, local_dict)

            # Count the first frame.
            elem = local_dict["types"][0]
            nA = len([ ee for ee in elem if ee == "A" ])
            nB = len([ ee for ee in elem if ee == "B" ])
            self.assertEqual(nA, 0)
            self.assertEqual(nB, 100)

            # Count the last frame.
            elem = local_dict["types"][-1]
            nA = len([ ee for ee in elem if ee == "A" ])
            nB = len([ ee for ee in elem if ee == "B" ])

            # Note that the average over a long simulation should be
            # 75% A using the modified rate function. In this particular
            # step the A population is 74%.
            value = 1.0 * nA / (nA + nB)
            self.assertAlmostEqual(0.74, value, 2)
Пример #14
0
    def testCalculationNonOrthogonal(self):
        """ Test a calculation with the on-the-fly MSD analysis. """
        # Setup a system, a periodic 10 atoms long 1D chain.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.0, 1.0, 0.0],
                                                          [0.0, 1.0, 0.0],
                                                          [0.0, 2.0, 1.0]]),
                                basis_points=[[0.0, 0.0, 0.0]])

        # And a lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(10, 10, 10),
                             periodic=(True, True, True))

        # Setup an initial configuration with one B in a sea of A:s.
        types = ["A"] * 10 * 10 * 10
        types[5] = "B"

        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=["A", "B"])

        # Setup a diffusion process to the left.
        coordinates_p0 = [[0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]]
        p0 = KMCProcess(coordinates=coordinates_p0,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p1 = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]
        p1 = KMCProcess(coordinates=coordinates_p1,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p2 = [[0.0, 0.0, 0.0], [0.0, -1.0, 0.0]]
        p2 = KMCProcess(coordinates=coordinates_p2,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p3 = [[0.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
        p3 = KMCProcess(coordinates=coordinates_p3,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p4 = [[0.0, 0.0, 0.0], [0.0, 0.0, -1.0]]
        p4 = KMCProcess(coordinates=coordinates_p4,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p5 = [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
        p5 = KMCProcess(coordinates=coordinates_p5,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        interactions = KMCInteractions(processes=[p0, p1, p2, p3, p4, p5],
                                       implicit_wildcards=True)

        model = KMCLatticeModel(configuration=config,
                                interactions=interactions)

        # Setup the analysis.
        msd = OnTheFlyMSD(history_steps=400,
                          n_bins=10,
                          t_max=25.0,
                          track_type="B")

        # Setup the control parameters.
        control_parameters = KMCControlParameters(number_of_steps=4000,
                                                  dump_interval=100,
                                                  analysis_interval=1,
                                                  seed=2013)
        # Run the model.
        model.run(control_parameters=control_parameters, analysis=[msd])

        # Get the results out.
        results = msd.results()
        time_steps = msd.timeSteps()
        std_dev = msd.stdDev()
        bin_counters = msd.binCounters()

        # Compare against references.
        ref_results = numpy.array(
            [[
                10.36856672, 28.37170047, 44.38285764, 58.94374243,
                72.11121737, 85.02318805, 98.58352498, 115.27324368,
                126.49573433, 138.47100039
            ],
             [
                 16.05138132, 44.90792913, 68.59720617, 93.35093194,
                 123.68147594, 148.63693439, 172.54794759, 203.0082052,
                 232.5195128, 267.73366007
             ],
             [
                 2.59976568, 7.35085681, 11.8568078, 16.56678774, 21.77700882,
                 26.34223625, 30.40786814, 35.09199292, 40.24434182,
                 46.48414664
             ],
             [
                 26.41994804, 73.2796296, 112.98006381, 152.29467437,
                 195.79269332, 233.66012244, 271.13147258, 318.28144888,
                 359.01524713, 406.20466046
             ],
             [
                 12.9683324, 35.72255728, 56.23966543, 75.51053018,
                 93.88822619, 111.3654243, 128.99139313, 150.3652366,
                 166.74007615, 184.95514703
             ],
             [
                 18.651147, 52.25878594, 80.45401397, 109.91771968,
                 145.45848476, 174.97917064, 202.95581574, 238.10019812,
                 272.76385461, 314.21780672
             ],
             [
                 29.01971372, 80.63048641, 124.8368716, 168.86146212,
                 217.56970213, 260.0023587, 301.53934072, 353.3734418,
                 399.25958894, 452.68880711
             ]])

        diff = numpy.linalg.norm(ref_results - results)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_time_steps = numpy.array(
            [1.25, 3.75, 6.25, 8.75, 11.25, 13.75, 16.25, 18.75, 21.25, 23.75])
        diff = numpy.linalg.norm(ref_time_steps - time_steps)
        self.assertAlmostEqual(diff, 0.0, 8)

        ref_std_dev = numpy.array([
            [
                0.55062327, 2.49729593, 4.98805342, 7.82646477, 10.85497401,
                14.13906215, 17.83387379, 22.42069464, 26.20482295, 30.35650154
            ],
            [
                0.85240944, 3.95282576, 7.70942988, 12.39500157, 18.61789684,
                24.71780818, 31.21412347, 39.48518175, 48.16868093, 58.69429153
            ],
            [
                0.13806069, 0.6470273, 1.33255031, 2.19971409, 3.27811502,
                4.38062279, 5.50081855, 6.82540746, 8.33700723, 10.19055301
            ],
            [
                0.99209394, 4.56092478, 8.97847654, 14.29873598, 20.84046684,
                27.47595651, 34.68217147, 43.77406499, 52.59000894, 62.96841965
            ],
            [
                0.4869731, 2.22337228, 4.46934176, 7.08957906, 9.99360309,
                13.09539481, 16.50011919, 20.68011712, 24.42476236, 28.67109723
            ],
            [
                0.70036814, 3.25258729, 6.3936455, 10.32002241, 15.48281847,
                20.57569786, 25.96138447, 32.74653167, 39.95555528, 48.70894069
            ],
            [
                0.88975069, 4.09754088, 8.10024368, 12.94487456, 18.90879051,
                24.96317829, 31.49377349, 39.68202523, 47.75293586, 57.29701788
            ]
        ])

        diff = numpy.linalg.norm(ref_std_dev - std_dev)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_bin_counters = (58893, 58531, 57985, 58641, 57509, 57659, 57396,
                            57037, 56732, 56518)
        self.assertEqual(bin_counters, ref_bin_counters)
Пример #15
0
    def testCalculationNonOrthogonal(self):
        """ Test a calculation with the on-the-fly MSD analysis. """
        # Setup a system, a periodic 10 atoms long 1D chain.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.0,1.0,0.0],
                                                          [0.0,1.0,0.0],
                                                          [0.0,2.0,1.0]]),
                                basis_points=[[0.0,0.0,0.0]])

        # And a lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(10,10,10),
                             periodic=(True,True,True))

        # Setup an initial configuration with one B in a sea of A:s.
        types = ["A"]*10*10*10
        types[5] = "B"

        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=["A","B"])

        # Setup a diffusion process to the left.
        coordinates_p0 = [[0.0, 0.0, 0.0],[-1.0, 0.0, 0.0]]
        p0 = KMCProcess(coordinates=coordinates_p0,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p1 = [[0.0, 0.0, 0.0],[1.0, 0.0, 0.0]]
        p1 = KMCProcess(coordinates=coordinates_p1,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p2 = [[0.0, 0.0, 0.0],[0.0,-1.0, 0.0]]
        p2 = KMCProcess(coordinates=coordinates_p2,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p3 = [[0.0, 0.0, 0.0],[0.0, 1.0, 0.0]]
        p3 = KMCProcess(coordinates=coordinates_p3,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p4 = [[0.0, 0.0, 0.0],[0.0, 0.0,-1.0]]
        p4 = KMCProcess(coordinates=coordinates_p4,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p5 = [[0.0, 0.0, 0.0],[0.0, 0.0, 1.0]]
        p5 = KMCProcess(coordinates=coordinates_p5,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        interactions = KMCInteractions(processes=[p0, p1, p2, p3, p4, p5],
                                       implicit_wildcards=True)

        model = KMCLatticeModel(configuration=config,
                                interactions=interactions)

        # Setup the analysis.
        msd = OnTheFlyMSD(history_steps=400,
                          n_bins=10,
                          t_max=25.0,
                          track_type="B")

        # Setup the control parameters.
        control_parameters = KMCControlParameters(number_of_steps=4000,
                                                  dump_interval=100,
                                                  analysis_interval=1,
                                                  seed=2013)
        # Run the model.
        model.run(control_parameters=control_parameters,
                  analysis=[msd])

        # Get the results out.
        results      = msd.results()
        time_steps   = msd.timeSteps()
        std_dev      = msd.stdDev()
        bin_counters = msd.binCounters()

        # Compare against references.
        ref_results = numpy.array([[  11.10849324 ,  32.89332622 ,  56.86314552 ,  84.47799379 , 110.30394149,
                                      135.26081658,  162.25612518,  189.40268295,  219.32538873,  252.00244119],
                                   [  17.33153679 ,  50.69982999 ,  82.74958435 , 111.33382274 , 136.44824258,
                                      164.84499628,  193.94367236,  234.25340515,  272.64453361,  302.94924061],
                                   [   2.98786918 ,   8.65022668 ,  14.01808716 ,  18.62678885 ,  22.65708384,
                                       26.03107861,   30.03937616,   35.1483    ,   40.68319007,   47.21074474],
                                   [  28.44003004 ,  83.59315621 , 139.61272987 , 195.81181652 , 246.75218407,
                                      300.10581285,  356.19979754,  423.65608811,  491.96992234,  554.95168181],
                                   [  14.09636242 ,  41.5435529  ,  70.88123268 , 103.10478264 , 132.96102533,
                                      161.29189519,  192.29550134,  224.55098295,  260.00857879,  299.21318593],
                                   [  20.31940597 ,  59.35005667 ,  96.76767151 , 129.96061158 , 159.10532643,
                                      190.87607489,  223.98304852,  269.40170515,  313.32772368,  350.15998535],
                                   [  31.42789922 ,  92.24338289 , 153.63081703 , 214.43860537 , 269.40926791,
                                      326.13689146,  386.2391737 ,  458.8043881 ,  532.65311241,  602.16242655]])

        diff = numpy.linalg.norm(ref_results - results)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_time_steps = numpy.array([  1.25,   3.75,   6.25,   8.75,  11.25,  13.75,  16.25,  18.75,  21.25,  23.75])
        diff = numpy.linalg.norm(ref_time_steps - time_steps)
        self.assertAlmostEqual(diff, 0.0, 8)

        ref_std_dev = numpy.array([[  0.59348826 ,  2.92235801 ,  6.47651272 , 11.36067446 , 16.79740936,
                                      22.78018427,  29.72954492,  37.27728411,  45.97782684,  55.88661276],
                                   [  0.92596389 ,  4.50435001 ,  9.42488725 , 14.97226982 , 20.7787406,
                                      27.76265504,  35.53552825,  46.10457783,  57.15527613,  67.18509081],
                                   [  0.15963149 ,  0.76851636 ,  1.59661093 ,  2.50494685 ,  3.45028752,
                                      4.38406911 ,  5.5039955  ,  6.91771175 ,  8.52853689 , 10.46993274],
                                   [  1.07441492 ,  5.2514756  , 11.24398775 , 18.62020347 , 26.57035045,
                                      35.73918442,  46.14937581,  58.95988   ,  72.92611647,  87.02483617],
                                   [  0.53253608 ,  2.60984229 ,  5.70856048 ,  9.80447485 , 14.31728377,
                                      19.20802777,  24.91387536,  31.25058126,  38.54181941,  46.9211633 ],
                                   [  0.76763185 ,  3.72847956 ,  7.7933761  , 12.35825842 , 17.13251009,
                                      22.73116664,  29.01932554,  37.49242051,  46.4454696 ,  54.91039375],
                                   [  0.96941939 ,  4.731515   , 10.1024813  , 16.6495642  , 23.68662473,
                                      31.71206536,  40.85854085,  52.13448317,  64.46787783,  77.10029967]])

        diff = numpy.linalg.norm(ref_std_dev - std_dev)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_bin_counters = (59930, 59996, 59545, 59256, 59064, 59076, 58284, 58294, 57817, 57349)
        self.assertEqual(bin_counters, ref_bin_counters)
Пример #16
0
    def testRunRngTypeDevice(self):
        """ Test to use the PRNG DEVICE. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(4,4,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*16
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the model.
        ab_flip_model_DEVICE   = KMCLatticeModel(configuration, interactions)

        support_device = False

        if (not support_device):
            # If DEVICE is not supported on your system this is the test you should run.
            self.assertRaises( Error,
                               lambda: ab_flip_model_DEVICE.run(KMCControlParameters(number_of_steps=10000,
                                                                                     dump_interval=5000,
                                                                                     seed=2013,
                                                                                     rng_type="DEVICE")))

        else:
            # If DEVICE is supported the aboove test will fail, and you should run this tests instead.

            # Run the model for 10000 steps with DEVICE.
            ab_flip_model_DEVICE.run(KMCControlParameters(number_of_steps=10000,
                                                          dump_interval=5000,
                                                          seed=2013,
                                                          rng_type="DEVICE"))
            # Get the simulation time out.
            t_DEVICE = ab_flip_model_DEVICE._KMCLatticeModel__cpp_timer.simulationTime()
            self.assertTrue(t_DEVICE < 410.0 and t_DEVICE > 370.0)
Пример #17
0
    def testScript(self):
        """ Test that the KMCInteractions can generate its own script. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "B"]
        types1 = ["B", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Construct the interactions object.
        kmc_interactions = KMCInteractions(processes=processes)

        script = kmc_interactions._script()

        ref_script = """
# -----------------------------------------------------------------------------
# Interactions

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','B']
elements_after  = ['B','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    3.500000e+00

process_0 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','C']
elements_after  = ['C','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    1.500000e+00

process_1 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

processes = [process_0,
             process_1]

interactions = KMCInteractions(
    processes=processes,
    implicit_wildcards=True)
"""
        self.assertEqual(script, ref_script)

        # Get a script with another name and another number of processes.

        processes = [process_0]
        kmc_interactions = KMCInteractions(processes=processes,
                                           implicit_wildcards=False)

        script = kmc_interactions._script(variable_name="my_kmc_interactions")

        ref_script = """
# -----------------------------------------------------------------------------
# Interactions

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','B']
elements_after  = ['B','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    3.500000e+00

process_0 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

processes = [process_0]

my_kmc_interactions = KMCInteractions(
    processes=processes,
    implicit_wildcards=False)
"""
        self.assertEqual(script, ref_script)
Пример #18
0
    def testRunRngType(self):
        """ Test that it is possible to run with each of the supported PRNG:s. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(4,4,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*16
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the models.
        ab_flip_model_MT       = KMCLatticeModel(configuration, interactions)
        ab_flip_model_RANLUX24 = KMCLatticeModel(configuration, interactions)
        ab_flip_model_RANLUX48 = KMCLatticeModel(configuration, interactions)
        ab_flip_model_MINSTD   = KMCLatticeModel(configuration, interactions)
        ab_flip_model_DEVICE   = KMCLatticeModel(configuration, interactions)

        # Run the model for 10000 steps with MT.
        ab_flip_model_MT.run(KMCControlParameters(number_of_steps=10000,
                                                  dump_interval=5000,
                                                  seed=2013,
                                                  rng_type="MT"))

        # Get the simulation time out.
        t_MT = ab_flip_model_MT._KMCLatticeModel__cpp_timer.simulationTime()

        # Run the model for 10000 steps with RANLUX24.
        ab_flip_model_RANLUX24.run(KMCControlParameters(number_of_steps=10000,
                                                        dump_interval=5000,
                                                        seed=2013,
                                                        rng_type="RANLUX24"))
        # Get the simulation time out.
        t_RANLUX24 = ab_flip_model_RANLUX24._KMCLatticeModel__cpp_timer.simulationTime()


        # Run the model for 10000 steps with RANLUX48.
        ab_flip_model_RANLUX48.run(KMCControlParameters(number_of_steps=10000,
                                                        dump_interval=5000,
                                                        seed=2013,
                                                        rng_type="RANLUX48"))
        # Get the simulation time out.
        t_RANLUX48 = ab_flip_model_RANLUX48._KMCLatticeModel__cpp_timer.simulationTime()

        # Run the model for 10000 steps with MINSTD.
        ab_flip_model_MINSTD.run(KMCControlParameters(number_of_steps=10000,
                                                      dump_interval=5000,
                                                      seed=2013,
                                                      rng_type="MINSTD"))
        # Get the simulation time out.
        t_MINSTD = ab_flip_model_MINSTD._KMCLatticeModel__cpp_timer.simulationTime()

        # These values should be simillar but not equal. Check against hardcoded values.
        self.assertAlmostEqual(t_MT,       394.569398158, 5)
        self.assertAlmostEqual(t_RANLUX24, 389.712162523, 5)
        self.assertAlmostEqual(t_RANLUX48, 390.544423280, 5)
        self.assertAlmostEqual(t_MINSTD,   384.712302086, 5)
Пример #19
0
    def testRunTrajectory(self):
        """ Test the run of an A-B flip model with trajectory output. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(4,4,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*16
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the model.
        ab_flip_model = KMCLatticeModel(configuration, interactions)

        # Construct the trajectory fileames.
        name = os.path.abspath(os.path.dirname(__file__))
        name = os.path.join(name, "..", "TestUtilities", "Scratch")

        lattice_trajectory_filename = os.path.join(name, "ab_flip_traj_lattice.py")
        xyz_trajectory_filename = os.path.join(name, "ab_flip_traj_xyz.xyz")

        self.__files_to_remove.append(lattice_trajectory_filename)
        self.__files_to_remove.append(xyz_trajectory_filename)

        # The control parameters.
        control_parameters = KMCControlParameters(number_of_steps=1000,
                                                  dump_interval=500,
                                                  seed=2013)

        # Run the model for 1000 steps with a lattice trajectory.
        ab_flip_model.run(control_parameters,
                          trajectory_filename=lattice_trajectory_filename,
                          trajectory_type='lattice')

        # Check the file content.
        ref_lattice = """
sites=[[       0.000000,       0.000000,       0.000000],
       [       0.000000,       1.000000,       0.000000],
       [       0.000000,       2.000000,       0.000000],
       [       0.000000,       3.000000,       0.000000],
       [       1.000000,       0.000000,       0.000000],
       [       1.000000,       1.000000,       0.000000],
       [       1.000000,       2.000000,       0.000000],
       [       1.000000,       3.000000,       0.000000],
       [       2.000000,       0.000000,       0.000000],
       [       2.000000,       1.000000,       0.000000],
       [       2.000000,       2.000000,       0.000000],
       [       2.000000,       3.000000,       0.000000],
       [       3.000000,       0.000000,       0.000000],
       [       3.000000,       1.000000,       0.000000],
       [       3.000000,       2.000000,       0.000000],
       [       3.000000,       3.000000,       0.000000]]
times=[]
steps=[]
types=[]
times.append(  0.0000000000e+00)
steps.append(0)
types.append(["B","B","B","B","B","B","B","B","B","B","B","B","B","B","B","B"])
times.append(  1.9093684175e+01)
steps.append(500)
types.append(["B","A","B","B","A","B","B","B","A","B","B","B","B","B","B","A"])
times.append(  4.0622006972e+01)
steps.append(1000)
types.append(["B","A","A","B","B","B","B","A","B","B","B","A","B","B","B","B"])
"""



        with open(lattice_trajectory_filename, "r") as t:
            lattice_data = t.read()

        # Check with "in" to avoid comparing dates.
        self.assertTrue(ref_lattice in lattice_data)

        # Run the model for 1000 steps with an xyz trajectory.
        ab_flip_model.run(control_parameters,
                          trajectory_filename=xyz_trajectory_filename,
                          trajectory_type='xyz')


        ref_xyz = """KMCLib XYZ FORMAT VERSION 2013.10.15

CELL VECTORS
a: 1.0000000000e+00 0.0000000000e+00 0.0000000000e+00
b: 0.0000000000e+00 1.0000000000e+00 0.0000000000e+00
c: 0.0000000000e+00 0.0000000000e+00 1.0000000000e+00

REPETITIONS 4 1 1

PERIODICITY True True False

STEP 0
          16
    TIME 4.0622006972e+01
                B   0.0000000000e+00 0.0000000000e+00 0.0000000000e+00  0
                A   0.0000000000e+00 1.0000000000e+00 0.0000000000e+00  1
                A   0.0000000000e+00 2.0000000000e+00 0.0000000000e+00  2
                B   0.0000000000e+00 3.0000000000e+00 0.0000000000e+00  3
                B   1.0000000000e+00 0.0000000000e+00 0.0000000000e+00  4
                B   1.0000000000e+00 1.0000000000e+00 0.0000000000e+00  5
                B   1.0000000000e+00 2.0000000000e+00 0.0000000000e+00  6
                A   1.0000000000e+00 3.0000000000e+00 0.0000000000e+00  7
                B   2.0000000000e+00 0.0000000000e+00 0.0000000000e+00  8
                B   2.0000000000e+00 1.0000000000e+00 0.0000000000e+00  9
                B   2.0000000000e+00 2.0000000000e+00 0.0000000000e+00  10
                A   2.0000000000e+00 3.0000000000e+00 0.0000000000e+00  11
                B   3.0000000000e+00 0.0000000000e+00 0.0000000000e+00  12
                B   3.0000000000e+00 1.0000000000e+00 0.0000000000e+00  13
                B   3.0000000000e+00 2.0000000000e+00 0.0000000000e+00  14
                B   3.0000000000e+00 3.0000000000e+00 0.0000000000e+00  15
STEP 500
          16
    TIME 5.9660935122e+01
                B   0.0000000000e+00 0.0000000000e+00 0.0000000000e+00  0
                A   0.0000000000e+00 1.0000000000e+00 0.0000000000e+00  1
                B   0.0000000000e+00 2.0000000000e+00 0.0000000000e+00  2
                B   0.0000000000e+00 3.0000000000e+00 0.0000000000e+00  3
                B   1.0000000000e+00 0.0000000000e+00 0.0000000000e+00  4
                B   1.0000000000e+00 1.0000000000e+00 0.0000000000e+00  5
                B   1.0000000000e+00 2.0000000000e+00 0.0000000000e+00  6
                A   1.0000000000e+00 3.0000000000e+00 0.0000000000e+00  7
                B   2.0000000000e+00 0.0000000000e+00 0.0000000000e+00  8
                B   2.0000000000e+00 1.0000000000e+00 0.0000000000e+00  9
                B   2.0000000000e+00 2.0000000000e+00 0.0000000000e+00  10
                B   2.0000000000e+00 3.0000000000e+00 0.0000000000e+00  11
                A   3.0000000000e+00 0.0000000000e+00 0.0000000000e+00  12
                A   3.0000000000e+00 1.0000000000e+00 0.0000000000e+00  13
                B   3.0000000000e+00 2.0000000000e+00 0.0000000000e+00  14
                B   3.0000000000e+00 3.0000000000e+00 0.0000000000e+00  15
STEP 1000
          16
    TIME 8.1189257919e+01
                A   0.0000000000e+00 0.0000000000e+00 0.0000000000e+00  0
                B   0.0000000000e+00 1.0000000000e+00 0.0000000000e+00  1
                B   0.0000000000e+00 2.0000000000e+00 0.0000000000e+00  2
                B   0.0000000000e+00 3.0000000000e+00 0.0000000000e+00  3
                B   1.0000000000e+00 0.0000000000e+00 0.0000000000e+00  4
                A   1.0000000000e+00 1.0000000000e+00 0.0000000000e+00  5
                B   1.0000000000e+00 2.0000000000e+00 0.0000000000e+00  6
                B   1.0000000000e+00 3.0000000000e+00 0.0000000000e+00  7
                B   2.0000000000e+00 0.0000000000e+00 0.0000000000e+00  8
                B   2.0000000000e+00 1.0000000000e+00 0.0000000000e+00  9
                A   2.0000000000e+00 2.0000000000e+00 0.0000000000e+00  10
                A   2.0000000000e+00 3.0000000000e+00 0.0000000000e+00  11
                B   3.0000000000e+00 0.0000000000e+00 0.0000000000e+00  12
                B   3.0000000000e+00 1.0000000000e+00 0.0000000000e+00  13
                B   3.0000000000e+00 2.0000000000e+00 0.0000000000e+00  14
                B   3.0000000000e+00 3.0000000000e+00 0.0000000000e+00  15
"""

        with open(xyz_trajectory_filename, "r") as t:
            xyz_data = t.read()

        self.assertEqual(ref_xyz, xyz_data)

        # Running with wrong trajectory_type input fails.
        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      trajectory_filename=xyz_trajectory_filename,
                                                      trajectory_type='abc') )

        self.assertRaises( Error,
                           lambda : ab_flip_model.run(control_parameters,
                                                      trajectory_filename=xyz_trajectory_filename,
                                                      trajectory_type=123) )
Пример #20
0
    def testConstruction(self):
        """ Test the construction of the lattice model """
        # Setup a unitcell.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8,0.0,0.0],
                                                          [0.0,3.2,0.0],
                                                          [0.0,0.5,3.0]]),
                                basis_points=[[0.0,0.0,0.0],
                                              [0.5,0.5,0.5],
                                              [0.25,0.25,0.75]])

        # Setup the lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(4,4,1),
                             periodic=(True,True,False))

        types = ['A','A','A','A','B','B',
                 'A','A','A','B','B','B',
                 'B','B','A','A','B','A',
                 'B','B','B','A','B','A',
                 'B','A','A','A','B','B',
                 'B','B','B','B','B','B',
                 'A','A','A','A','B','B',
                 'B','B','A','B','B','A']

        # Setup the configuration.
        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=['A','C','B'])

        # A first process.
        coords = [[1.0,2.0,3.4],[1.1,1.2,1.3]]
        types0 = ["A","B"]
        types1 = ["B","A"]
        sites  = [0,1,2]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=sites,
                               rate_constant=rate_0_1)

        # A second process.
        coords = [[1.0,2.0,3.4],[1.1,1.2,1.3]]
        types0 = ["A","C"]
        types1 = ["C","A"]
        sites  = [0,1,2]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=sites,
                               rate_constant=rate_0_1)

        # Construct the interactions object.
        processes = [process_0, process_1]
        interactions = KMCInteractions(processes=processes)

        # Construct the model.
        model = KMCLatticeModel(config, interactions)

        # Check that it has the attribute _backend which is None
        self.assertTrue(hasattr(model,"_KMCLatticeModel__backend"))
        self.assertTrue(model._KMCLatticeModel__backend is None)

        # Check that it has the correct interactions stored.
        self.assertTrue(model._KMCLatticeModel__interactions == interactions)

        # Check that it has the correct configuration stored.
        self.assertTrue(model._KMCLatticeModel__configuration == config)
Пример #21
0
    def testBackendWithCustomRates(self):
        """ Test that we can construct the backend object with custom rates. """
        # A first process.
        coords = [[1.0, 2.0, 3.4], [1.1, 1.2, 1.3]]
        types0 = ["A", "B"]
        types1 = ["B", "A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A", "C"]
        types1 = ["C", "A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        processes = [process_0, process_1]

        # Set the custom rates class to use.
        custom_rates_class = CustomRateCalculator

        # Set the rate function on the custom rates calculator for testing.
        ref_rnd = numpy.random.uniform(0.0, 1.0)

        def testRateFunction(obj, coords, types_before, types_after,
                             rate_const, process_number, global_coordinate):
            return ref_rnd

        # Store the original.
        custom_rate_function = custom_rates_class.rate
        try:
            custom_rates_class.rate = testRateFunction

            # Construct the interactions object.
            kmc_interactions = KMCInteractions(processes=processes,
                                               implicit_wildcards=False)

            # Setup a dict with the possible types.
            possible_types = {
                "A": 13,
                "D": 2,
                "B": 3,
                "J": 4,
                "C": 5,
            }

            # Use a dummy argument for the configuration.
            config = "DummyConfig"

            # Test that it fails if the rate calculator is of wrong class.
            kmc_interactions.setRateCalculator(rate_calculator=Error)
            self.assertRaises(
                Error,
                lambda: kmc_interactions._backend(possible_types, 2, config))

            # Test that it fails if the rate calculator is the base class.
            kmc_interactions.setRateCalculator(
                rate_calculator=KMCRateCalculatorPlugin)
            self.assertRaises(
                Error,
                lambda: kmc_interactions._backend(possible_types, 2, config))

            # But this should work.
            kmc_interactions.setRateCalculator(
                rate_calculator=custom_rates_class)

            # Construct the backend.
            cpp_interactions = kmc_interactions._backend(
                possible_types, 2, config)

            # Test that the configuration on the custom rate class is the one given.
            self.assertTrue(
                kmc_interactions._KMCInteractions__rate_calculator.
                configuration == config)  #  <-  Check by reference.

            # Get the rate calculator reference out of the C++ object and
            # check that a call from C++ calls the Python extension.
            cpp_coords = Backend.StdVectorDouble()
            cpp_types1 = Backend.StdVectorString()
            cpp_types2 = Backend.StdVectorString()
            rate_constant = 543.2211
            process_number = 33
            coordinate = (1.2, 3.4, 5.6)

            self.assertAlmostEqual(
                cpp_interactions.rateCalculator().backendRateCallback(
                    cpp_coords,
                    cpp_coords.size() / 3, cpp_types1, cpp_types2,
                    rate_constant, process_number, coordinate[0],
                    coordinate[1], coordinate[2]), ref_rnd, 12)
            self.assertAlmostEqual(
                kmc_interactions._KMCInteractions__rate_calculator.
                backendRateCallback(cpp_coords,
                                    cpp_coords.size() / 3, cpp_types1,
                                    cpp_types2, rate_constant, process_number,
                                    coordinate[0], coordinate[1],
                                    coordinate[2]), ref_rnd, 12)

        finally:
            # Reset the class.
            custom_rates_class.rate = custom_rate_function

        # Construct a C++ RateCalculator object directly and check that this object
        # returns the rate given to it.
        cpp_rate_calculator = Backend.RateCalculator()
        self.assertAlmostEqual(
            cpp_rate_calculator.backendRateCallback(
                cpp_coords,
                cpp_coords.size() / 3, cpp_types1, cpp_types2, rate_constant,
                process_number, coordinate[0], coordinate[1], coordinate[2]),
            rate_constant, 12)
Пример #22
0
    def testCalculation(self):
        """ Test a calculation with the on-the-fly MSD analysis. """
        # Setup a system, a periodic 10 atoms long 1D chain.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[1.0,0.0,0.0],
                                                          [0.0,1.0,0.0],
                                                          [0.0,0.0,1.0]]),
                                basis_points=[[0.0,0.0,0.0]])

        # And a lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(10,10,10),
                             periodic=(True,True,True))

        # Setup an initial configuration with one B in a sea of A:s.
        types = ["A"]*10*10*10
        types[5] = "B"

        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=["A","B"])

        # Setup a diffusion process to the left.
        coordinates_p0 = [[0.0, 0.0, 0.0],[-1.0, 0.0, 0.0]]
        p0 = KMCProcess(coordinates=coordinates_p0,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p1 = [[0.0, 0.0, 0.0],[1.0, 0.0, 0.0]]
        p1 = KMCProcess(coordinates=coordinates_p1,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p2 = [[0.0, 0.0, 0.0],[0.0,-1.0, 0.0]]
        p2 = KMCProcess(coordinates=coordinates_p2,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p3 = [[0.0, 0.0, 0.0],[0.0, 1.0, 0.0]]
        p3 = KMCProcess(coordinates=coordinates_p3,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p4 = [[0.0, 0.0, 0.0],[0.0, 0.0,-1.0]]
        p4 = KMCProcess(coordinates=coordinates_p4,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p5 = [[0.0, 0.0, 0.0],[0.0, 0.0, 1.0]]
        p5 = KMCProcess(coordinates=coordinates_p5,
                        elements_before=["B","A"],
                        elements_after=["A","B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        interactions = KMCInteractions(processes=[p0, p1, p2, p3, p4, p5],
                                       implicit_wildcards=True)

        model = KMCLatticeModel(configuration=config,
                                interactions=interactions)

        # Setup the analysis.
        msd = OnTheFlyMSD(history_steps=400,
                          n_bins=10,
                          t_max=25.0,
                          track_type="B")

        # Setup the control parameters.
        control_parameters = KMCControlParameters(number_of_steps=4000,
                                                  dump_interval=100,
                                                  analysis_interval=1,
                                                  seed=2013)
        # Run the model.
        model.run(control_parameters=control_parameters,
                  analysis=[msd])

        # Get the results out.
        results      = msd.results()
        time_steps   = msd.timeSteps()
        std_dev      = msd.stdDev()
        bin_counters = msd.binCounters()

        # Compare against references.
        ref_results = numpy.array([[   2.77712331 ,   8.22333156 ,  14.21578638 ,  21.11949845 ,  27.57598537,
                                       33.81520414,   40.5640313 ,   47.35067074,   54.83134718,   63.0006103 ],
                                   [   2.80810946 ,   8.48503234 ,  13.7997313  ,  17.62812205 ,  22.37202018,
                                       29.12157221,   35.40750463,   41.44944591,   48.94392653,   56.67111894],
                                   [   2.98786918 ,   8.65022668 ,  14.01808716 ,  18.62678885 ,  22.65708384,
                                       26.03107861,   30.03937616,   35.1483    ,   40.68319007,   47.21074474],
                                   [   5.58523277 ,  16.70836389 ,  28.01551768 ,  38.74762049 ,  49.94800555,
                                       62.93677636,   75.97153593,   88.80011665,  103.77527371,  119.67172924],
                                   [   5.76499249 ,  16.87355824 ,  28.23387354 ,  39.7462873  ,  50.23306921,
                                       59.84628275,   70.60340745,   82.49897073,   95.51453725,  110.21135504],
                                   [   5.79597864 ,  17.13525902 ,  27.81781846 ,  36.2549109  ,  45.02910402,
                                       55.15265082,   65.44688079,   76.59774591,   89.62711659,  103.88186368],
                                   [   8.57310195 ,  25.35859057 ,  42.03360484 ,  57.37440934 ,  72.60508939,
                                       88.96785497,  106.01091209,  123.94841665,  144.45846377,  166.88247398]])

        diff = numpy.linalg.norm(ref_results - results)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_time_steps = numpy.array([  1.25,   3.75,   6.25,   8.75,  11.25,  13.75,  16.25,  18.75,  21.25,  23.75])
        diff = numpy.linalg.norm(ref_time_steps - time_steps)
        self.assertAlmostEqual(diff, 0.0, 8)

        ref_std_dev = numpy.array( [[  0.14837207,   0.7305895 ,   1.61912818,   2.84016862,   4.19935234,
                                       5.69504607,   7.43238623,   9.31932103,  11.49445671,  13.97165319],
                                    [  0.15002755,   0.75383991,   1.57174096,   2.37064526,   3.40687718,
                                       4.90455993,   6.48757634,   8.15787162,  10.26025939,  12.5679611 ],
                                    [  0.15963149,   0.76851636,   1.59661093,   2.50494685,   3.45028752,
                                       4.38406911,   5.5039955 ,   6.91771175,   8.52853689,  10.46993274],
                                    [  0.21100039,   1.04965011,   2.25628521,   3.68460183,   5.37841647,
                                       7.49505328,   9.84289993,  12.35824143,  15.38290727,  18.76634124],
                                    [  0.2177914 ,   1.06002792,   2.27387093,   3.77956739,   5.40911222,
                                       7.12701069,   9.14740325,  11.48131598,  14.15839455,  17.28281115],
                                    [  0.218962  ,   1.07646844,   2.24036311,   3.44756425,   4.84874766,
                                       6.56805258,   8.47932177,  10.66004723,  13.28568526,  16.29025096],
                                    [  0.26444438,   1.30073885,   2.76405291,   4.45469653,   6.38348309,
                                       8.65082886,  11.21442742,  14.08440462,  17.48404426,  21.36747194]])

        diff = numpy.linalg.norm(ref_std_dev - std_dev)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_bin_counters = (59930, 59996, 59545, 59256, 59064, 59076, 58284, 58294, 57817, 57349)
        self.assertEqual(bin_counters, ref_bin_counters)
Пример #23
0
    def testBackend(self):
        """ Test that the backend object is correctly constructed. """
        # Setup a unitcell.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8,0.0,0.0],
                                                          [0.0,3.2,0.0],
                                                          [0.0,0.5,3.0]]),
                                basis_points=[[0.0,0.0,0.0],
                                              [0.5,0.5,0.5],
                                              [0.25,0.25,0.75]])

        # Setup the lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(4,4,1),
                             periodic=(True,True,False))

        types = ['A','A','A','A','B','B',
                 'A','A','A','B','B','B',
                 'B','B','A','A','B','A',
                 'B','B','B','A','B','A',
                 'B','A','A','A','B','B',
                 'B','B','B','B','B','B',
                 'A','A','A','A','B','B',
                 'B','B','A','B','B','A']

        # Setup the configuration.
        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=['A','C','B'])


        # A first process.
        coords = [[1.0,2.0,3.4],[1.1,1.2,1.3]]
        types0 = ["A","B"]
        types1 = ["B","A"]
        sites  = [0,1,2,3,4]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=sites,
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A","C"]
        types1 = ["C","A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=sites,
                               rate_constant=rate_0_1)

        # Construct the interactions object.
        processes = [process_0, process_1]
        interactions = KMCInteractions(processes=processes)

        # Construct the model.
        model = KMCLatticeModel(config, interactions)

        # Get the c++ backend out.
        cpp_model = model._backend()

        # Check that this backend object is stored on the class.
        self.assertTrue(model._KMCLatticeModel__backend == cpp_model)
Пример #24
0
    def testRunWithAnalysis(self):
        """ Test that the analyis plugins get called correctly. """
        # Cell.
        cell_vectors = [[   1.000000e+00,   0.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   1.000000e+00,   0.000000e+00],
                        [   0.000000e+00,   0.000000e+00,   1.000000e+00]]

        basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]

        unit_cell = KMCUnitCell(
            cell_vectors=cell_vectors,
            basis_points=basis_points)

        # Lattice.
        lattice = KMCLattice(
            unit_cell=unit_cell,
            repetitions=(10,10,1),
            periodic=(True, True, False))

        # Configuration.
        types = ['B']*100
        possible_types = ['A','B']
        configuration = KMCConfiguration(
            lattice=lattice,
            types=types,
            possible_types=possible_types)

        # Interactions.
        coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00]]
        process_0 = KMCProcess(coordinates,
                               ['A'],
                               ['B'],
                               basis_sites=[0],
                               rate_constant=4.0)
        process_1 = KMCProcess(coordinates,
                               ['B'],
                               ['A'],
                               basis_sites=[0],
                               rate_constant=1.0)

        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Setup the model.
        ab_flip_model = KMCLatticeModel(configuration, interactions)

        # Run the model with a trajectory file.
        name = os.path.abspath(os.path.dirname(__file__))
        name = os.path.join(name, "..", "TestUtilities", "Scratch")
        trajectory_filename = os.path.join(name, "ab_flip_traj.py")
        self.__files_to_remove.append(trajectory_filename)

        # The control parameters.
        control_parameters = KMCControlParameters(number_of_steps=1000,
                                                  dump_interval=500,
                                                  analysis_interval=300)


        # Setup a valid minimal analysis object.
        class AnalysisProxy1(KMCAnalysisPlugin):
            def __init__(self):
                pass

        # Setup a slightly larger analyis object.
        class AnalysisProxy2(KMCAnalysisPlugin):
            def __init__(self):
                self.setup_called = False
                self.finalize_called = False
                self.register_step_counts = 0

            def setup(self, step, time, configuration):
                self.setup_called = True

            def registerStep(self, step, time, configuration):
                self.register_step_counts += 1

            def finalize(self):
                self.finalize_called = True

        ap2 = AnalysisProxy2()
        analysis = [ AnalysisProxy1(), ap2 ]

        # Run the model for 1000 steps with the analysis objects.
        # With dump interval 500 the analysis objects should  be
        # called on startup, the at step 300, 600 and step 900 and
        # a finalization after that.
        ab_flip_model.run(control_parameters,
                          trajectory_filename=trajectory_filename,
                          analysis=analysis)

        self.assertTrue(ap2.setup_called)
        self.assertTrue(ap2.finalize_called)
        self.assertEqual(ap2.register_step_counts, 3)
Пример #25
0
    def testScript(self):
        """ Test that a script can be created. """
        # Setup a unitcell.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[2.8,0.0,0.0],
                                                          [0.0,3.2,0.0],
                                                          [0.0,0.5,3.0]]),
                                basis_points=[[0.0,0.0,0.0],
                                              [0.5,0.5,0.5],
                                              [0.25,0.25,0.75]])

        # Setup the lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(4,4,1),
                             periodic=(True,True,False))

        types = ['A','A','A','A','B','B',
                 'A','A','A','B','B','B',
                 'B','B','A','A','B','A',
                 'B','B','B','A','B','A',
                 'B','A','A','A','B','B',
                 'B','B','B','B','B','B',
                 'A','A','A','A','B','B',
                 'B','B','A','B','B','A']

        # Setup the configuration.
        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=['A','C','B'])

        # A first process.
        coords = [[1.0,2.0,3.4],[1.1,1.2,1.3]]
        types0 = ["A","B"]
        types1 = ["B","A"]
        rate_0_1 = 3.5
        process_0 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        # A second process.
        types0 = ["A","C"]
        types1 = ["C","A"]
        rate_0_1 = 1.5
        process_1 = KMCProcess(coords,
                               types0,
                               types1,
                               basis_sites=[0],
                               rate_constant=rate_0_1)

        # Construct the interactions object.
        processes = [process_0, process_1]
        interactions = KMCInteractions(processes)

        # Construct the model.
        model = KMCLatticeModel(config, interactions)

        # Get the script.
        script = model._script()

        ref_script = """
# -----------------------------------------------------------------------------
# Unit cell

cell_vectors = [[   2.800000e+00,   0.000000e+00,   0.000000e+00],
                [   0.000000e+00,   3.200000e+00,   0.000000e+00],
                [   0.000000e+00,   5.000000e-01,   3.000000e+00]]

basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
                [   5.000000e-01,   5.000000e-01,   5.000000e-01],
                [   2.500000e-01,   2.500000e-01,   7.500000e-01]]

unit_cell = KMCUnitCell(
    cell_vectors=cell_vectors,
    basis_points=basis_points)

# -----------------------------------------------------------------------------
# Lattice

lattice = KMCLattice(
    unit_cell=unit_cell,
    repetitions=(4,4,1),
    periodic=(True, True, False))

# -----------------------------------------------------------------------------
# Configuration

types = ['A','A','A','A','B','B','A','A','A','B','B','B','B',
         'B','A','A','B','A','B','B','B','A','B','A','B','A',
         'A','A','B','B','B','B','B','B','B','B','A','A','A',
         'A','B','B','B','B','A','B','B','A']

possible_types = ['A','C','B']

configuration = KMCConfiguration(
    lattice=lattice,
    types=types,
    possible_types=possible_types)

# -----------------------------------------------------------------------------
# Interactions

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','B']
elements_after  = ['B','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    3.500000e+00

process_0 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','C']
elements_after  = ['C','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    1.500000e+00

process_1 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

processes = [process_0,
             process_1]

interactions = KMCInteractions(
    processes=processes,
    implicit_wildcards=True)

# -----------------------------------------------------------------------------
# Lattice model

model = KMCLatticeModel(
    configuration=configuration,
    interactions=interactions)
"""
        self.assertEqual(script, ref_script)

        # Get the script again, with a different variable name.
        script = model._script(variable_name="my_model")

        ref_script = """
# -----------------------------------------------------------------------------
# Unit cell

cell_vectors = [[   2.800000e+00,   0.000000e+00,   0.000000e+00],
                [   0.000000e+00,   3.200000e+00,   0.000000e+00],
                [   0.000000e+00,   5.000000e-01,   3.000000e+00]]

basis_points = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
                [   5.000000e-01,   5.000000e-01,   5.000000e-01],
                [   2.500000e-01,   2.500000e-01,   7.500000e-01]]

unit_cell = KMCUnitCell(
    cell_vectors=cell_vectors,
    basis_points=basis_points)

# -----------------------------------------------------------------------------
# Lattice

lattice = KMCLattice(
    unit_cell=unit_cell,
    repetitions=(4,4,1),
    periodic=(True, True, False))

# -----------------------------------------------------------------------------
# Configuration

types = ['A','A','A','A','B','B','A','A','A','B','B','B','B',
         'B','A','A','B','A','B','B','B','A','B','A','B','A',
         'A','A','B','B','B','B','B','B','B','B','A','A','A',
         'A','B','B','B','B','A','B','B','A']

possible_types = ['A','C','B']

configuration = KMCConfiguration(
    lattice=lattice,
    types=types,
    possible_types=possible_types)

# -----------------------------------------------------------------------------
# Interactions

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','B']
elements_after  = ['B','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    3.500000e+00

process_0 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

coordinates = [[   0.000000e+00,   0.000000e+00,   0.000000e+00],
               [   1.000000e-01,  -8.000000e-01,  -2.100000e+00]]

elements_before = ['A','C']
elements_after  = ['C','A']
move_vectors    = [(  0,[   1.000000e-01,  -8.000000e-01,  -2.100000e+00]),
                   (  1,[  -1.000000e-01,   8.000000e-01,   2.100000e+00])]
basis_sites     = [0]
rate_constant   =    1.500000e+00

process_1 = KMCProcess(
    coordinates=coordinates,
    elements_before=elements_before,
    elements_after=elements_after,
    move_vectors=move_vectors,
    basis_sites=basis_sites,
    rate_constant=rate_constant)

processes = [process_0,
             process_1]

interactions = KMCInteractions(
    processes=processes,
    implicit_wildcards=True)

# -----------------------------------------------------------------------------
# Lattice model

my_model = KMCLatticeModel(
    configuration=configuration,
    interactions=interactions)
"""

        # Check.
        self.assertEqual(script, ref_script)
Пример #26
0
    def testCalculation(self):
        """ Test a calculation with the on-the-fly MSD analysis. """
        # Setup a system, a periodic 10 atoms long 1D chain.
        unit_cell = KMCUnitCell(cell_vectors=numpy.array([[1.0, 0.0, 0.0],
                                                          [0.0, 1.0, 0.0],
                                                          [0.0, 0.0, 1.0]]),
                                basis_points=[[0.0, 0.0, 0.0]])

        # And a lattice.
        lattice = KMCLattice(unit_cell=unit_cell,
                             repetitions=(10, 10, 10),
                             periodic=(True, True, True))

        # Setup an initial configuration with one B in a sea of A:s.
        types = ["A"] * 10 * 10 * 10
        types[5] = "B"

        config = KMCConfiguration(lattice=lattice,
                                  types=types,
                                  possible_types=["A", "B"])

        # Setup a diffusion process to the left.
        coordinates_p0 = [[0.0, 0.0, 0.0], [-1.0, 0.0, 0.0]]
        p0 = KMCProcess(coordinates=coordinates_p0,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p1 = [[0.0, 0.0, 0.0], [1.0, 0.0, 0.0]]
        p1 = KMCProcess(coordinates=coordinates_p1,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p2 = [[0.0, 0.0, 0.0], [0.0, -1.0, 0.0]]
        p2 = KMCProcess(coordinates=coordinates_p2,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p3 = [[0.0, 0.0, 0.0], [0.0, 1.0, 0.0]]
        p3 = KMCProcess(coordinates=coordinates_p3,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p4 = [[0.0, 0.0, 0.0], [0.0, 0.0, -1.0]]
        p4 = KMCProcess(coordinates=coordinates_p4,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        coordinates_p5 = [[0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]
        p5 = KMCProcess(coordinates=coordinates_p5,
                        elements_before=["B", "A"],
                        elements_after=["A", "B"],
                        move_vectors=None,
                        basis_sites=[0],
                        rate_constant=1.0)

        interactions = KMCInteractions(processes=[p0, p1, p2, p3, p4, p5],
                                       implicit_wildcards=True)

        model = KMCLatticeModel(configuration=config,
                                interactions=interactions)

        # Setup the analysis.
        msd = OnTheFlyMSD(history_steps=400,
                          n_bins=10,
                          t_max=25.0,
                          track_type="B")

        # Setup the control parameters.
        control_parameters = KMCControlParameters(number_of_steps=4000,
                                                  dump_interval=100,
                                                  analysis_interval=1,
                                                  seed=2013)
        # Run the model.
        model.run(control_parameters=control_parameters, analysis=[msd])

        # Get the results out.
        results = msd.results()
        time_steps = msd.timeSteps()
        std_dev = msd.stdDev()
        bin_counters = msd.binCounters()

        # Compare against references.
        ref_results = numpy.array([
            [
                2.59214168, 7.09292512, 11.09571441, 14.73593561, 18.02780434,
                21.25579701, 24.64588125, 28.81831092, 31.62393358, 34.6177501
            ],
            [
                2.65374493, 7.10317609, 10.90626886, 14.87948705, 18.25169973,
                21.00603548, 22.97611332, 25.57822115, 28.87848128, 32.2240879
            ],
            [
                2.59976568, 7.35085681, 11.8568078, 16.56678774, 21.77700882,
                26.34223625, 30.40786814, 35.09199292, 40.24434182, 46.48414664
            ],
            [
                5.24588661, 14.19610121, 22.00198327, 29.61542266, 36.27950408,
                42.2618325, 47.62199456, 54.39653208, 60.50241486, 66.841838
            ],
            [
                5.19190736, 14.44378193, 22.9525222, 31.30272335, 39.80481316,
                47.59803326, 55.05374939, 63.91030384, 71.8682754, 81.10189674
            ],
            [
                5.2535106, 14.45403291, 22.76307666, 31.44627479, 40.02870855,
                47.34827174, 53.38398146, 60.67021407, 69.1228231, 78.70823454
            ],
            [
                7.84565228, 21.54695802, 33.85879107, 46.1822104, 58.05651289,
                68.60406875, 78.02986271, 89.48852499, 100.74675668,
                113.32598464
            ]
        ])

        diff = numpy.linalg.norm(ref_results - results)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_time_steps = numpy.array(
            [1.25, 3.75, 6.25, 8.75, 11.25, 13.75, 16.25, 18.75, 21.25, 23.75])
        diff = numpy.linalg.norm(ref_time_steps - time_steps)
        self.assertAlmostEqual(diff, 0.0, 8)

        ref_std_dev = numpy.array(
            [[
                0.13765582, 0.62432398, 1.24701335, 1.95661619, 2.7137435,
                3.53476554, 4.45846845, 5.60517366, 6.55120574, 7.58912539
            ],
             [
                 0.14092726, 0.62522628, 1.22572215, 1.97567674, 2.7474467,
                 3.49323106, 4.1564055, 4.97497483, 5.98245856, 7.06437139
             ],
             [
                 0.13806069, 0.6470273, 1.33255031, 2.19971409, 3.27811502,
                 4.38062279, 5.50081855, 6.82540746, 8.33700723, 10.19055301
             ],
             [
                 0.19698798, 0.88356546, 1.74848804, 2.780551, 3.86164462,
                 4.96954405, 6.09163579, 7.48129474, 8.86263902, 10.36158694
             ],
             [
                 0.19496101, 0.89898111, 1.82402696, 2.93896933, 4.23688379,
                 5.59702476, 7.04227937, 8.7897482, 10.52755635, 12.57213116
             ],
             [
                 0.19727427, 0.89961913, 1.8089718, 2.95244717, 4.26071555,
                 5.56765545, 6.82868861, 8.34413034, 10.12539137, 12.20107405
             ],
             [
                 0.24054939, 1.09498957, 2.19698279, 3.54031591, 5.04564022,
                 6.58676948, 8.14969886, 10.04910241, 12.04968783, 14.34371883
             ]])

        diff = numpy.linalg.norm(ref_std_dev - std_dev)
        self.assertAlmostEqual(diff, 0.0, 6)

        ref_bin_counters = (58893, 58531, 57985, 58641, 57509, 57659, 57396,
                            57037, 56732, 56518)
        self.assertEqual(bin_counters, ref_bin_counters)