Skip to content

Commit

Permalink
fix the issue in update_row_dftb/gulp
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhu2017 committed May 12, 2024
1 parent 2d76593 commit 8bd34d0
Showing 1 changed file with 68 additions and 25 deletions.
93 changes: 68 additions & 25 deletions pyxtal/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def dftb_opt_par(ids, xtals, skf_dir, steps, folder, criteria):
for id, xtal in zip(ids, xtals):
res = dftb_opt_single(id, xtal, skf_dir, steps, criteria)
(xtal, eng, status) = res
if not status:
if status:
results.append((id, xtal, eng))
os.chdir(cwd)
return results
Expand Down Expand Up @@ -82,7 +82,7 @@ def gulp_opt_par(ids, xtals, ff, path, criteria):
for id, xtal in zip(ids, xtals):
res = gulp_opt_single(id, xtal, ff, path, criteria)
(xtal, eng, status) = res
if not status:
if status:
results.append((id, xtal, eng))
return results

Expand Down Expand Up @@ -554,7 +554,7 @@ def clean_structures_spg_topology(self):
self.db.delete(to_delete)


def clean_structures(self, dtol=2e-3, etol=1e-3):
def clean_structures(self, ids=(None, None), dtol=2e-3, etol=1e-3, criteria=None):
"""
Clean up the db by removing the duplicate structures
Here we check the follow criteria
Expand All @@ -565,38 +565,74 @@ def clean_structures(self, dtol=2e-3, etol=1e-3):
Args:
dtol (float): tolerance of density
etol (float): tolerance of energy
criteria (dict): including
"""

unique_rows = []
to_delete = []

for row in self.db.select():
ids, xtals = self.select_xtals(ids)
for id, xtal in zip(ids, xtals):
row = self.db.get(id)
xtal = self.get_pyxtal(id)
unique = True
for prop in unique_rows:
(natoms, den, ff_energy) = prop
if natoms==row.natoms and abs(den-row.density) < dtol:
if hasattr(row, 'ff_energy'):
if abs(row.ff_energy-ff_energy) < etol:
if criteria is not None:
if not xtal.check_validity(criteria, True):
unique = False
print('Found unsatisfied criteria', row.id, row.space_group_number, row.wps)

if unique:
if 'MAX_energy' in criteria and hasattr(row, 'ff_energy') \
and row.ff_energy > criteria['MAX_energy']:
unique = False
print('Unsatisfied energy', row.id, row.ff_energy, row.space_group_number, row.wps)
if unique:
if 'MAX_similarity' in criteria and hasattr(row, 'similarity') \
and row.similarity > criteria['MAX_similarity']:
unique = False
print('Unsatisfied similarity', row.id, row.similarity, row.space_group_number, row.wps)
if unique:
if 'BAD_topology' in criteria and hasattr(row, 'topology') \
and row.topology[:3] in criteria['BAD_topology']:
unique = False
print('Unsatisfied topology', row.id, row.topology, row.space_group_number, row.wps)
if unique:
if 'BAD_dimension' in criteria and hasattr(row, 'dimension') \
and row.dimension in criteria['BAD_dimension']:
unique = False
print('Unsatisfied dimension', row.id, row.topology, row.space_group_number, row.wps)


if unique:
for prop in unique_rows:
(natoms, spg, wps, den, ff_energy) = prop
if natoms==row.natoms and spg==row.space_group_number and wps==row.wps:
if hasattr(row, 'ff_energy') and ff_energy is not None:
if abs(row.ff_energy-ff_energy) < etol:
unique = False
break
elif abs(den-row.density) < dtol:
unique = False
break
else:
unique = False
break

if unique:
if hasattr(row, 'ff_energy'):
unique_rows.append((row.natoms,
row.space_group_number,
row.wps,
row.density,
row.ff_energy))
else:
unique_rows.append((row.natoms,
row.space_group_number,
row.wps,
row.density,
None))
else:
to_delete.append(row.id)
print("The following structures were deleted", to_delete)
self.db.delete(to_delete)

def clean_structures_pmg(self, dtol=5e-2, criteria=None):
def clean_structures_pmg(self, ids=(None, None), min_id=None, dtol=5e-2, criteria=None):
"""
Clean up the db by removing the duplicate structures
Here we check the follow criteria
Expand All @@ -620,11 +656,15 @@ def clean_structures_pmg(self, dtol=5e-2, criteria=None):
unique_rows = []
to_delete = []

for row in self.db.select():
xtal = self.get_pyxtal(id=row.id)
ids, xtals = self.select_xtals(ids)
if min_id is None: min_id = min(ids)

for id, xtal in zip(ids, xtals):
row = self.db.get(id)
xtal = self.get_pyxtal(id)
unique = True

if criteria is not None:
if id > min_id and criteria is not None:
if not xtal.check_validity(criteria, True):
unique = False
print('Found unsatisfied criteria', row.id, row.space_group_number, row.wps)
Expand All @@ -651,7 +691,7 @@ def clean_structures_pmg(self, dtol=5e-2, criteria=None):
print('Unsatisfied dimension', row.id, row.topology, row.space_group_number, row.wps)


if unique:
if unique and id > min_id:
for prop in unique_rows:
(rowid, den) = prop
if abs(den-row.density) < dtol:
Expand All @@ -669,7 +709,7 @@ def clean_structures_pmg(self, dtol=5e-2, criteria=None):
self.db.delete(to_delete)


def select_xtals(self, ids, overwrite, attribute):
def select_xtals(self, ids, overwrite=False, attribute=None):
"""
Extract xtals based on attribute name.
Mostly called by update_row_ff_energy or update_row_dftb_energy.
Expand All @@ -680,11 +720,13 @@ def select_xtals(self, ids, overwrite, attribute):

ids, xtals = [], []
for row in self.db.select():
if overwrite or not hasattr(row, 'ff_energy'):
if overwrite or attribute is None or not hasattr(row, attribute):
if min_id <= row.id <= max_id:
xtal = self.get_pyxtal(row.id)
ids.append(row.id)
xtals.append(xtal)
if len(xtals) % 100 == 0:
print('Loading xtals from db', len(xtals))
return ids, xtals

def update_row_ff_energy(self, ff='reaxff', ids=(None, None), ncpu=1,
Expand Down Expand Up @@ -728,13 +770,14 @@ def update_row_ff_energy(self, ff='reaxff', ids=(None, None), ncpu=1,
for result in results:
gulp_results.extend(result.result())

# Wrap up the final results and update db
print("Wrap up the final results and update db", len(gulp_results))
for gulp_result in gulp_results:
(id, xtal, eng) = gulp_result
self.db.update(id,
ff_energy=eng,
ff_lib=ff,
ff_relaxed=xtal.to_file())
if xtal is not None:
self.db.update(id,
ff_energy=eng,
ff_lib=ff,
ff_relaxed=xtal.to_file())

def update_row_dftb_energy(self, skf_dir, steps=500,
ids=(None, None),
Expand Down

0 comments on commit 8bd34d0

Please sign in to comment.