Skip to content

Commit

Permalink
enable zp case for check_stable
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhu2017 committed Jul 30, 2024
1 parent 5486e63 commit 096d5ff
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 20 deletions.
12 changes: 6 additions & 6 deletions pyxtal/optimize/DFS.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,17 +235,17 @@ def run(self, ref_pmg=None, ref_eng=None, ref_pxrd=None):
self.engs.append(xtal.energy / sum(xtal.numMols))
# print(output)

strs = f"Generation {gen:d} finishes" # ; import sys; sys.exit()
strs = f"Generation {gen:d} finishes: {len(self.engs):d} strucs" # ; import sys; sys.exit()
print(strs)
self.logging.info(strs)
t1 = time()

# Apply Gaussian (sometimes time consuming)
#if ref_pxrd is None:
# engs = self._apply_gaussian(current_reps, current_engs)
#else:
# engs = self._apply_gaussian(current_reps, -1 * np.array(current_matches))
engs = current_engs
if ref_pxrd is None:
engs = self._apply_gaussian(current_reps, current_engs)
else:
engs = self._apply_gaussian(current_reps, -1 * np.array(current_matches))
#engs = current_engs

# Store the best structures
count = 0
Expand Down
10 changes: 6 additions & 4 deletions pyxtal/optimize/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,15 @@ def __init__(
self.skip_ani = skip_ani
self.randomizer = randomizer
self.optimizer = optimizer
self.check_stable = check_stable
# setup timeout for each optimization call
if max_time is None:
if self.skip_ani:
max_time = 60.0
else:
if not self.skip_ani:
max_time = 300.0
elif self.check_stable:
max_time = 300.0
else:
max_time = 60.0
self.timeout = max_time * self.N_pop / self.ncpu

self.ff_opt = ff_opt
Expand Down Expand Up @@ -201,7 +204,6 @@ def __init__(
self.logging = logging

# Some neccessary trackers
self.check_stable = check_stable
self.matches = []
self.best_reps = []
self.reps = []
Expand Down
25 changes: 15 additions & 10 deletions pyxtal/optimize/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def check_stable_structure(xtal, c_info, w_dir, job_tag, skip_ani, optimizer, di
"""
Check the stability of input xtal based on lattice mutation
"""
comp = xtal.get_1D_comp()
disp_cell, disp_ang = disps[0], disps[1]
res = optimizer(xtal, c_info, w_dir, job_tag, skip_ani=skip_ani)
smiles = [m.smile for m in xtal.molecules]#; print(smiles)
Expand All @@ -32,7 +33,7 @@ def check_stable_structure(xtal, c_info, w_dir, job_tag, skip_ani, optimizer, di
rep0 = xtal0.get_1D_representation()
if verbose: print("Optimize", rep0.to_string(eng0))
cell0 = np.array(xtal.lattice.encode())
wp0 = xtal.mol_sites[0].encode()
wps = [site.encode() for site in xtal.mol_sites]

for i, c in enumerate(cell0):
if i <= 2:
Expand All @@ -42,18 +43,19 @@ def check_stable_structure(xtal, c_info, w_dir, job_tag, skip_ani, optimizer, di
for disp in disps:
cell = cell0.copy()
cell[i] += disp
x = [[xtal.group.hall_number] + cell.tolist(), wp0]#; print(x)
x = [[xtal.group.hall_number] + cell.tolist()]
x.extend(wps)#; print(x)
rep1 = representation(x, smiles)
xtal1 = rep1.to_pyxtal()#; print(i, j, xtal1.lattice)
res = optimizer(xtal1, c_info, w_dir, skip_ani=skip_ani)
xtal1 = rep1.to_pyxtal(composition=comp)#; print(i, j, xtal1.lattice)
res = optimizer(xtal1, c_info, w_dir, job_tag, skip_ani=skip_ani)
if res is not None:
xtal2, eng = res["xtal"], res["energy"] #/sum(xtal1.numMols)
if eng < eng0:
rep2 = xtal2.get_1D_representation()
eng0 = eng
xtal0 = xtal2
if eng < eng0 + 1e-4:
#rep2 = xtal2.get_1D_representation()
xtal0, eng0 = xtal2, eng
if verbose: print("Update ", rep2.to_string(eng0))
return xtal0, eng0
return xtal0, eng0, True
return xtal0, eng0, False
else:
raise RuntimeError("Error in optimization")

Expand Down Expand Up @@ -434,7 +436,10 @@ def optimizer_single(
if res is not None:
xtal, eng = res["xtal"], res["energy"]
if check_stable and eng < 9999.:
xtal, eng = check_stable_structure(xtal, atom_info, workdir, job_tag, skip_ani, optimizer)
xtal, eng, status = check_stable_structure(xtal, atom_info, workdir, job_tag, skip_ani, optimizer)
if status:
xtal, eng, status = check_stable_structure(xtal, atom_info, workdir, job_tag, skip_ani, optimizer)

rep = xtal.get_1D_representation()
N = sum(xtal.numMols)
strs = rep.to_string(None, eng / N, tag) # print(strs)
Expand Down

0 comments on commit 096d5ff

Please sign in to comment.