Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
ColdSnaap committed Jun 2, 2024
1 parent 9fd7ffd commit a9c4757
Show file tree
Hide file tree
Showing 6 changed files with 234 additions and 80 deletions.
8 changes: 4 additions & 4 deletions sgrcsp/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
def print_logo():
print(
"""
Package name is not decided yet
|\__/,| (`
_.|o o |_ ) )
-(((---(((--------
Space Group Restricted Crystal Structure Prediction
"""
)
print("-------------------(version", __version__, ")--------------------\n")
print("-------------------(version", __version__,")--------------------\n")
print("A Python package for symmerty restricted crystal structure prediction")
print("The source code is available at ..")
print("Developed by ColdSnaaap \n\n")
print("The source code is available at https://github.com/ColdSnaap/sgrcsp.git")
print("Developed by ColdSnaap \n\n")
sys.stdout.flush()


Expand Down
1 change: 1 addition & 0 deletions sgrcsp/optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,7 @@ def simulated_annealing(
print("-------------------------------------------------")
print(f"Current state: {current_energy} eV, {current_energy_atom} eV/atom")
print(f"New state: {next_energy} eV, {next_energy_atom} eV/atom")
sys.stdout.flush()

# Determine if we should accept the new state
accept = False
Expand Down
122 changes: 79 additions & 43 deletions sgrcsp/routine.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,10 @@ def read_continue_config():
config["count"] = int(config["count"])
config["best_energy"] = float(config["best_energy"])
config["accept_list"] = ast.literal_eval(config["accept_list"])
current_state = Poscar.from_file(continue_path+"/POSCAR_current")
# current_state = Poscar.from_file(continue_path+"/POSCAR_current")
best_state = Poscar.from_file(continue_path+"/POSCAR_best")

return config["step_current"], config["accept_list"], config["count"], config["best_energy"], \
current_state, best_state
return config["step_current"], config["accept_list"], config["count"], config["best_energy"], best_state


class Routine:
Expand Down Expand Up @@ -142,34 +141,52 @@ def __init__(self) -> None:
print(f"MaxStep: {self.max_step}\n")
sys.stdout.flush()
# generate initial structure
if self.initial_relax:
while True:
try:
self.structure = structure_generation(
file_path = os.getcwd() + "/Input/initial.cif"
if not self.job_contine:
if os.path.isfile(file_path):
self.structure = structure_generation(
self.sg,
self.mol_list,
self.mol_number,
self.sites,
factor=self.factor
)
print("Relaxing the initial structure ...")
sys.stdout.flush()
cal = CalculateEnergy(self.structure)
cal_struc = cal.energy_calculate("VASP", True, self.sub_command)[0]
self.structure = structure_type_converter(cal_struc, "pyxtal")
print("Initial structure generated")
sys.stdout.flush()
break
except ReadSeedError:
print("Molecule not intact, trying to generate a new structure to relax")
else:
self.structure = structure_generation(
self.sg,
self.mol_list,
self.mol_number,
self.sites,
factor=self.factor
)
else:
while True:
try:
self.structure = structure_generation(
self.sg,
self.mol_list,
self.mol_number,
self.sites,
factor=self.factor
)
print("Relaxing the initial structure ...")
sys.stdout.flush()
cal = CalculateEnergy(self.structure)
cal_struc = cal.energy_calculate("VASP", True, self.sub_command)[0]
self.structure = structure_type_converter(cal_struc, "pyxtal")
print("Initial structure generated")
sys.stdout.flush()
break
except ReadSeedError:
print("Molecule not intact, trying to generate a new structure to relax")

elif self.job_contine:
print("Continue ...")
file_path = os.getcwd() + "/StrucBackup/POSCAR"
self.structure = structure_type_converter(file_path, "pyxtal")
print("Backup structure read")
sys.stdout.flush()

# write backup
backup_dir = os.getcwd() + "/StrucBackup"
if not os.path.isdir(backup_dir):
os.mkdir(backup_dir)
struc_py = structure_type_converter(self.structure, "pymatgen")
poscar = Poscar(struc_py)
poscar.write_file(f"{backup_dir}/POSCAR")


def log(
self,
Expand All @@ -188,6 +205,7 @@ def log(
result_dir = os.getcwd() + "/Result"
struc_log = os.getcwd() + "/Result/BestStrucsList"
struc_file = os.getcwd() + "/Result/BestStrucs"
backup_dir = os.getcwd() + "/StrucBackup"

sga = SpacegroupAnalyzer(structure, symprec=0.1)
refined_structure = sga.get_refined_structure()
Expand All @@ -199,8 +217,14 @@ def log(
# raise NameError("Result directory not exist")
if not os.path.isdir(struc_file):
os.makedirs(struc_file)
if not os.path.isdir(backup_dir):
os.mkdir(backup_dir)

name_en = abs(energy)
# backup structure
poscar = Poscar(structure)
poscar.write_file(f"{backup_dir}/POSCAR")

if not os.path.isfile(f"{struc_file}/{name_en}.cif"):
# refined_structure.to(f'{struc_file}/{name_en}.cif', fmt="cif")
cif_writer = CifWriter(refined_structure, symprec=0.1)
Expand Down Expand Up @@ -308,32 +332,44 @@ def routine3(self):

step_current = 0
accept_list = None
current_state = self.structure
count = None
best_energy = None
best_state = None
rate_scale = None
else:
step_current, accept_list, count, best_energy, current_state, best_state = \
read_continue_config()
continue_path = os.getcwd() + "/Result/Continue"
if os.path.isdir(continue_path):
step_current, accept_list, count, best_energy, best_state = read_continue_config()
else:
step_current = 0
accept_list = None
count = None
best_energy = None
best_state = None
rate_scale = None

current_state = self.structure

for step in range(self.loop_number):

while True:
try:
current_state_ori = structure_type_converter(current_state, "pymatgen")
current_state = copy.deepcopy(current_state_ori)
cal1 = CalculateEnergy(current_state)
cal = cal1.energy_calculate("VASP", True, self.sub_command)
current_state, relax_energy, relax_energy_atom = cal
current_state = structure_type_converter(current_state, "pyxtal")
break
except ReadSeedError:
print("Molecule not intact, trying to generate a new structure to relax")
sys.stdout.flush()
for i in range(10):
current_state_ori = apply_perturbation(current_state_ori)

try:
current_state_ori = structure_type_converter(current_state, "pymatgen")
current_state = copy.deepcopy(current_state_ori)
cal1 = CalculateEnergy(current_state)
cal = cal1.energy_calculate("VASP", True, self.sub_command)
current_state, relax_energy, relax_energy_atom = cal
current_state = structure_type_converter(current_state, "pyxtal")
except ReadSeedError:
print("Molecule not intact, use backup structure")
sys.stdout.flush()
backup_file = os.getcwd()+"/StrucBackup/POSCAR"
current_state_ori = Structure.from_file(backup_file)
current_state = copy.deepcopy(current_state_ori)
cal1 = CalculateEnergy(current_state)
cal = cal1.energy_calculate("VASP", True, self.sub_command)
current_state, relax_energy, relax_energy_atom = cal
current_state = structure_type_converter(current_state, "pyxtal")

self.log(step_current, current_state, relax_energy)

# small perturbation
Expand Down
Loading

0 comments on commit a9c4757

Please sign in to comment.