Exemplo n.º 1
0
    def testUsage(self):
        """ Test that the KMCRateCalculatorPlugin can be used in a simulation. """
        # To get the random numbers returned.
        ref_randoms = []

        # Define a derrived class.
        class RateCalc(KMCRateCalculatorPlugin):
            # Overload the initialize function.
            def initialize(self):
                # Save something on the class here.
                self._times_called = 0
            # Overload the rate function.
            def rate(self, coords, types_befpre, types_after, rate_constant):
                # Do some simple counting and return the random number.
                self._times_called += 1
                rnd = numpy.random.uniform(0.0,1.0)
                ref_randoms.append(rnd)
                return rnd
            # Overload the additive rate function.
            def useAdditiveRate(self):
                return False

        # Construct.
        calculator = RateCalc()

        # Send it to C++ to get the rate out, call it 4 times.
        cpp_coords = Backend.StdVectorCoordinate()
        cpp_coords.push_back(Backend.Coordinate(1.0,2.9,3.4))
        cpp_types1 = Backend.StdVectorString()
        cpp_types1.push_back("A")
        cpp_types2 = Backend.StdVectorString()
        cpp_types2.push_back("B")
        rate_constant = 3.1415927

        ret_randoms = []
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant))

        # Check that it was called 4 times.
        self.assertEqual( calculator._times_called, 4 )

        # Check the values.
        self.assertAlmostEqual( ret_randoms, ref_randoms, 12 )
Exemplo n.º 2
0
    def testUsage(self):
        """ Test that the KMCRateCalculatorPlugin can be used in a simulation. """
        # To get the random numbers and process numbers returned.
        ref_randoms = []
        ref_process_numbers = []
        ref_coordinates = []
        # Define a derrived class.
        class RateCalc(KMCRateCalculatorPlugin):
            # Overload the initialize function.
            def initialize(self):
                # Save something on the class here.
                self._times_called = 0
            # Overload the rate function.
            def rate(self, coords, types_befpre, types_after, rate_constant, process_number, coordinate):
                # Do some simple counting and return the random number.
                self._times_called += 1
                rnd = numpy.random.uniform(0.0,1.0)
                ref_randoms.append(rnd)
                ref_process_numbers.append(process_number)
                ref_coordinates.append(coordinate)
                return rnd
            # Overload the additive rate function.
            def useAdditiveRate(self):
                return False

        # Construct.
        calculator = RateCalc("DummyConfig")

        # Send it to C++ to get the rate out, call it 4 times.
        cpp_coords = Backend.StdVectorCoordinate()
        cpp_coords.push_back(Backend.Coordinate(1.0,2.9,3.4))
        cpp_types1 = Backend.StdVectorString()
        cpp_types1.push_back("A")
        cpp_types2 = Backend.StdVectorString()
        cpp_types2.push_back("B")
        rate_constant = 3.1415927
        ret_randoms = []
        process_numbers = [21, 12, 10, 2]
        global_xyz = numpy.array([[0.2,0.4,0.5],
                                  [1.1,1.3,1.4],
                                  [3.4,4.3,3.3],
                                  [4.2,3.2,1.9]])


        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant,
                                           process_numbers[0],
                                           global_xyz[0,0],
                                           global_xyz[0,1],
                                           global_xyz[0,2]))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant,
                                           process_numbers[1],
                                           global_xyz[1,0],
                                           global_xyz[1,1],
                                           global_xyz[1,2]))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant,
                                           process_numbers[2],
                                           global_xyz[2,0],
                                           global_xyz[2,1],
                                           global_xyz[2,2]))
        ret_randoms.append(Backend.getRate(calculator,
                                           cpp_coords,
                                           cpp_types1,
                                           cpp_types2,
                                           rate_constant,
                                           process_numbers[3],
                                           global_xyz[3,0],
                                           global_xyz[3,1],
                                           global_xyz[3,2]))

        # Check that it was called 4 times.
        self.assertEqual( calculator._times_called, 4 )

        # Check the values.
        self.assertAlmostEqual( ret_randoms, ref_randoms, 12 )

        # Check the reference process numbers.
        self.assertEqual( ref_process_numbers[0], 21 )
        self.assertEqual( ref_process_numbers[1], 12 )
        self.assertEqual( ref_process_numbers[2], 10 )
        self.assertEqual( ref_process_numbers[3], 2  )

        # Check the reference coordinate.
        self.assertAlmostEqual( numpy.linalg.norm( global_xyz - numpy.array(ref_coordinates)), 0.0, 10 )
    def testUsage(self):
        """ Test that the KMCRateCalculatorPlugin can be used in a simulation. """
        # To get the random numbers and process numbers returned.
        ref_randoms = []
        ref_process_numbers = []
        ref_coordinates = []

        # Define a derrived class.
        class RateCalc(KMCRateCalculatorPlugin):
            # Overload the initialize function.
            def initialize(self):
                # Save something on the class here.
                self._times_called = 0

            # Overload the rate function.
            def rate(self, coords, types_befpre, types_after, rate_constant,
                     process_number, coordinate):
                # Do some simple counting and return the random number.
                self._times_called += 1
                rnd = numpy.random.uniform(0.0, 1.0)
                ref_randoms.append(rnd)
                ref_process_numbers.append(process_number)
                ref_coordinates.append(coordinate)
                return rnd

            # Overload the additive rate function.
            def useAdditiveRate(self):
                return False

        # Construct.
        calculator = RateCalc("DummyConfig")

        # Send it to C++ to get the rate out, call it 4 times.
        cpp_coords = Backend.StdVectorCoordinate()
        cpp_coords.push_back(Backend.Coordinate(1.0, 2.9, 3.4))
        cpp_types1 = Backend.StdVectorString()
        cpp_types1.push_back("A")
        cpp_types2 = Backend.StdVectorString()
        cpp_types2.push_back("B")
        rate_constant = 3.1415927
        ret_randoms = []
        process_numbers = [21, 12, 10, 2]
        global_xyz = numpy.array([[0.2, 0.4, 0.5], [1.1, 1.3, 1.4],
                                  [3.4, 4.3, 3.3], [4.2, 3.2, 1.9]])

        ret_randoms.append(
            Backend.getRate(calculator, cpp_coords, cpp_types1, cpp_types2,
                            rate_constant, process_numbers[0],
                            global_xyz[0, 0], global_xyz[0, 1], global_xyz[0,
                                                                           2]))
        ret_randoms.append(
            Backend.getRate(calculator, cpp_coords, cpp_types1, cpp_types2,
                            rate_constant, process_numbers[1],
                            global_xyz[1, 0], global_xyz[1, 1], global_xyz[1,
                                                                           2]))
        ret_randoms.append(
            Backend.getRate(calculator, cpp_coords, cpp_types1, cpp_types2,
                            rate_constant, process_numbers[2],
                            global_xyz[2, 0], global_xyz[2, 1], global_xyz[2,
                                                                           2]))
        ret_randoms.append(
            Backend.getRate(calculator, cpp_coords, cpp_types1, cpp_types2,
                            rate_constant, process_numbers[3],
                            global_xyz[3, 0], global_xyz[3, 1], global_xyz[3,
                                                                           2]))

        # Check that it was called 4 times.
        self.assertEqual(calculator._times_called, 4)

        # Check the values.
        self.assertAlmostEqual(ret_randoms, ref_randoms, 12)

        # Check the reference process numbers.
        self.assertEqual(ref_process_numbers[0], 21)
        self.assertEqual(ref_process_numbers[1], 12)
        self.assertEqual(ref_process_numbers[2], 10)
        self.assertEqual(ref_process_numbers[3], 2)

        # Check the reference coordinate.
        self.assertAlmostEqual(
            numpy.linalg.norm(global_xyz - numpy.array(ref_coordinates)), 0.0,
            10)