Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions GBOpt/GBMinimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import math
import shutil
import sys
import uuid
from collections.abc import Callable
from time import time
Expand Down Expand Up @@ -32,21 +31,25 @@ def mutate(self, local_random: np.random.default_rng, GB: GBMaker, manipulator:
:param GBManipulator: GBManipulator object to perform the mutation on.
:return: Atom positions after the mutation."""
choice_key = local_random.choice(self.choices_keys)
mutation = None
match choice_key:
case "insert_atoms":
new_system = manipulator.insert_atoms(
method="grid", num_to_insert=1)
mutation = "add1"

case "remove_atoms":
new_system = manipulator.remove_atoms(num_to_remove=1)
mutation = "remove1"

case "translate_right_grain":
dz = (GB.z_dim / GB.repeat_factor[1]
) * local_random.uniform(0, 1)
dy = (GB.z_dim / GB.repeat_factor[0]
) * local_random.uniform(0, 1)
new_system = manipulator.translate_right_grain(dy=dy, dz=dz)
return new_system
mutation = f"shift{dy:.8f}dy{dz:.8f}dz"
return mutation, new_system


class MonteCarloMinimizer:
Expand All @@ -66,7 +69,8 @@ def __init__(self, GB: GBMaker, gb_energy_func: Callable, choices: list, seed=ti
self.manipulator = GBManipulator(self.GB)
self.mutator = Mutator(choices, self.manipulator)
self.accepted_idx = [0] # Initial guess is accepted by definition
self.__operation_list__ = ["START"]
# The list of mutators, and whether or not they were accepted
self.operation_list = [["START", True]]
self.local_random = np.random.default_rng(seed)
self.manipulator.rng = self.local_random
self.GBE_vals = []
Expand Down Expand Up @@ -109,7 +113,7 @@ def run_MC(self, E_accept: float = 1e-1, max_steps: int = 50, E_tol: float = 1e-
prev_gbe = self.GBE_vals[-1]

# Generate a random mutation on the current GB atom structure
new_system = self.mutator.mutate(
mutation, new_system = self.mutator.mutate(
self.local_random, self.GB, self.manipulator)

# Evaluate the energy of this new structure and append it to the GBE values list
Expand All @@ -126,6 +130,7 @@ def run_MC(self, E_accept: float = 1e-1, max_steps: int = 50, E_tol: float = 1e-
0, 1) <= math.exp(-(new_gbe - prev_gbe) / T)

if accepted:
self.operation_list.append([mutation, True])
# Generate a new GB manipulator using the new structure from the dump file
self.manipulator = GBManipulator(
dump_file_name,
Expand All @@ -150,6 +155,7 @@ def run_MC(self, E_accept: float = 1e-1, max_steps: int = 50, E_tol: float = 1e-
break
min_gbe = new_gbe
else:
self.operation_list.append([mutation, False])
rejection_count += 1
# If too many structures are rejected back-to-back, we prematurely stop the MC iterations since we are stuck
if rejection_count > max_rejections:
Expand Down