Ejemplo n.º 1
0
    def test_filter_actual_energy(self):
        generator = wallpaper.generate_wallpaper((1, 1),
                                                 place_max=5,
                                                 sample_count=100)
        result = [None]

        def callback(energies):
            result[0] = energies

        potentials = {
            (0, 0): potential.LennardJonesType(lambda_=-1),
            (1, 1): potential.LennardJonesType(lambda_=-1),
            (0, 1): potential.LennardJonesType(lambda_=1)
        }
        distance = 4.0
        filter = minimization.filter(generator,
                                     potentials,
                                     distance,
                                     20,
                                     histogram_callback=callback)

        # Make sure true energies are actually less than cutoff
        filter_results = list(filter)  # Exhaust generator to trigger callback
        energies = result[0]
        for cell in filter_results:
            self.assertLessEqual(
                potential._evaluate_fast(cell[1], potentials, distance)[0],
                min(energies[20:]))
Ejemplo n.º 2
0
    def test_filter_callback(self):
        generator = wallpaper.generate_wallpaper((1, 1),
                                                 place_max=5,
                                                 sample_count=100)
        result = [None]

        def callback(energies):
            result[0] = energies

        filter = minimization.filter(
            generator, {
                (0, 0): potential.LennardJonesType(lambda_=-1),
                (1, 1): potential.LennardJonesType(lambda_=-1),
                (0, 1): potential.LennardJonesType(lambda_=1)
            },
            4.0,
            10,
            histogram_callback=callback)

        # Check for proper filtering behavior
        self.assertEqual(len(list(filter)), 10)
        # Get energies (callback should have been triggered)
        energies = result[0]
        sorted_energies = sorted(energies)
        # Check partitioning
        self.assertEqual(energies[10],
                         sorted_energies[10])  # Did partition get target?
        self.assertLessEqual(max(energies[:10]), min(
            energies[10:]))  # Did partition move elements?
Ejemplo n.º 3
0
 def test_filter_radii(self):
     generator = wallpaper.generate_wallpaper((1, 1),
                                              place_max=5,
                                              sample_count=100)
     filter = minimization.filter(
         generator, {
             (0, 0): potential.LennardJonesType(lambda_=-1),
             (1, 1): potential.LennardJonesType(lambda_=-1),
             (0, 1): potential.LennardJonesType(lambda_=1)
         }, 4.0, 10, (0.5, 0.5))
     self.assertEqual(len(list(filter)), 10)
Ejemplo n.º 4
0
    def test_filter_generator(self):
        count = 100
        distance = 3.0
        radii = (2.**(1. / 6.) / 2., 2.**(1. / 6.) / 2.)
        potentials = {
            (0, 0): potential.LennardJonesType(epsilon=0),
            (1, 1): potential.LennardJonesType(epsilon=1),
            (0, 1): potential.LennardJonesType(epsilon=0.5)
        }
        mm = similarity.Histogram(distance,
                                  0.05,
                                  0.99,
                                  norm=similarity.Minimum())

        # Generate an ensemble of candidates
        generator = wallpaper.generate_wallpaper(
            (1, 1),
            place_max=4,
            random_seed=0,
            sample_count=count,
            merge_sets=False,
            sample_groups=[wallpaper.WallpaperGroup(name="p3")])
        results = list(generator)

        # All "count" structures should be provided
        self.assertEqual(len(results), count)

        # Scale cells to "contact"
        scale_cell = lambda cell: crystal.CellTools.scale(
            cell, cell.vectors / cell.scale_factor(radii))
        cells = [scale_cell(c) for g, c in results]
        energies = numpy.array([
            potential._evaluate_fast(c, potentials, distance)[0] for c in cells
        ])
        res = zip(cells, energies)

        # Find "unique" ones
        reduced_cells = similarity.reduce(res, mm)
        manually_reduced = len(reduced_cells)
        self.assertEqual(manually_reduced, 89)

        # Do in an automated fashion to compare
        generator2 = wallpaper.generate_wallpaper(
            (1, 1),
            place_max=4,
            random_seed=0,
            sample_count=count,
            merge_sets=False,
            sample_groups=[wallpaper.WallpaperGroup(name="p3")])
        filter2 = minimization.filter(generator2,
                                      potentials,
                                      distance,
                                      count,
                                      radii,
                                      similarity_metric=mm)
        self.assertEqual(len(list(filter2)), manually_reduced)

        # Confirm that cells from automatic reduction have not been rescaled
        for idx, (g, c) in enumerate(list(filter2)):
            self.assertTrue(c != reduced_cells[idx][0])
            self.assertTrue(scale_cell(c) == reduced_cells[idx][0])
            self.assertTrue(
                potential._evaluate_fast(scale_cell(c), potentials, distance)
                [0] == reduced_cells[idx][1])