Esempio n. 1
0
    def test_uniqueness_count_multiple(self):
        groups = [wallpaper.WallpaperGroup(name="p1")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,2,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=groups)
        self.assertEqual(len([c for c in enumerate(gen)]), 12 + 588)

        groups = [wallpaper.WallpaperGroup(name="p1"), wallpaper.WallpaperGroup(name="p2")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=groups)
        self.assertEqual(len([c for c in enumerate(gen)]), 12+52)
Esempio n. 2
0
    def test_minimum_configurations_single_group(self):
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,1,1), length_ratios=(1.,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=[wallpaper.WallpaperGroup(name='p2')],
            congruent=False,log_level=0,
            minimum_configurations=96)
        self.assertEqual(len([c for c in enumerate(gen)]), 96)

        gen = wallpaper.generate_wallpaper(stoichiometry=(1,1,1), length_ratios=(1.,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=[wallpaper.WallpaperGroup(name='p2')],
            congruent=False,log_level=0,
            minimum_configurations=97)
        self.assertEqual(len([c for c in enumerate(gen)]), 9744)
Esempio n. 3
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:]))
Esempio n. 4
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?
Esempio n. 5
0
 def _test_generator_wallpaper_overall(self, **kwargs):
     results = list(wallpaper.generate_wallpaper(sample_count=5, log_level=1, **kwargs))
     for group, result in results:
         # Check stoichiometry
         expected_stoichiometry = numpy.array(kwargs["stoichiometry"], dtype=float)
         actual_stoichiometry = numpy.array(result.atom_counts, dtype=float)
         expected_stoichiometry /= numpy.sum(expected_stoichiometry)
         actual_stoichiometry /= numpy.sum(actual_stoichiometry)
         self.assertTrue(numpy.all(numpy.isclose(expected_stoichiometry, actual_stoichiometry)))
Esempio n. 6
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)
Esempio n. 7
0
    def test_uniqueness_set(self):
        groups = [wallpaper.WallpaperGroup(name="p1")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=groups, debug=True)

        res = set()
        for (idx, (g, struct)) in enumerate(gen):
            self.assertTrue(struct not in res)
            res.add(struct)
        self.assertEqual(len(res), 12)

        groups = [wallpaper.WallpaperGroup(name="p2")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=groups, debug=True)

        res = set()
        for (idx, (g, struct)) in enumerate(gen):
            self.assertTrue(struct not in res)
            res.add(struct)
        self.assertEqual(len(res), 52)
Esempio n. 8
0
    def test_uniqueness_count_single_congruent(self):
        # p1 should be unaffected
        groups = [wallpaper.WallpaperGroup(name="p1")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=3, sample_groups=groups, log_level=0,
            congruent=True)
        self.assertEqual(len([c for c in enumerate(gen)]), 12)

        # p2 with lr = 1 should have same as Ng = 3 if congruent to Ng = 5 (3 = floor(5/sqrt(2*1)))
        groups = [wallpaper.WallpaperGroup(name="p2")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=5, sample_groups=groups, log_level=0,
            congruent=True)
        self.assertEqual(len([c for c in enumerate(gen)]), 52)

        # p6m with lr = 1 should have same as Ng = 8 if congruent to Ng = 3 (3 = floor(8/sqrt(6)))
        groups = [wallpaper.WallpaperGroup(name="p6m")]
        gen = wallpaper.generate_wallpaper(stoichiometry=(1,2), length_ratios=(1,), \
            angles=(numpy.pi/2,), grid_count=8, sample_groups=groups, log_level=0,
            congruent=True)
        self.assertEqual(len([c for c in enumerate(gen)]), 29)
Esempio n. 9
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])