-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_gaminimizer.py
More file actions
65 lines (52 loc) · 1.83 KB
/
test_gaminimizer.py
File metadata and controls
65 lines (52 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# Copyright 2025, Battelle Energy Alliance, LLC, ALL RIGHTS RESERVED
import math
import tempfile
import unittest
from pathlib import Path
import numpy as np
from GBOpt.GBMaker import GBMaker
from GBOpt.GBMinimizer import GeneticAlgorithmMinimizer
class TestGeneticAlgorithmMinimizer(unittest.TestCase):
def setUp(self):
theta = math.radians(36.869898)
misorientation = np.array([theta, 0.0, 0.0, 0.0, -theta / 2.0])
self.gb = GBMaker(
3.52,
"fcc",
10.0,
misorientation,
"Ni",
repeat_factor=2,
x_dim_min=30.0,
vacuum=8.0,
interaction_distance=8.0,
)
self.tmpdir = tempfile.TemporaryDirectory()
def tearDown(self):
self.tmpdir.cleanup()
def test_run_ga_returns_best_energy_and_dump(self):
def fake_energy_func(GB, manipulator, atom_positions, unique_id):
dump_file = Path(self.tmpdir.name) / f"{unique_id}.data"
GB.write_lammps(
str(dump_file),
atom_positions,
manipulator.parents[0].box_dims,
)
energy = float(np.mean(atom_positions["x"]))
return energy, str(dump_file)
minimizer = GeneticAlgorithmMinimizer(
self.gb,
fake_energy_func,
["insert_atoms", "remove_atoms", "translate_right_grain"],
seed=0,
population_size=4,
generations=2,
keep_top_pct=25,
intermediate_pct=75,
)
best_energy, best_dump = minimizer.run_GA(unique_id=1)
self.assertIsInstance(best_energy, float)
self.assertTrue(Path(best_dump).exists())
self.assertEqual(len(minimizer.GBE_vals), minimizer.generations + 1)
if __name__ == "__main__":
unittest.main()