-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add final_c2m_driver and modify codes accordingly
- Loading branch information
Showing
9 changed files
with
421 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import os | ||
import sys | ||
from cell2mol.helper import parsing_arguments | ||
from cell2mol.refcell import process_refcell | ||
from cell2mol.unitcell import process_unitcell | ||
from cell2mol.xyz_molecule import get_molecule | ||
|
||
def main(): | ||
input, system_type, cell_para, debug_mode = parsing_arguments() | ||
current_dir = os.getcwd() | ||
input_path = os.path.normpath(input) | ||
dir, file = os.path.split(input_path) | ||
name, extension = os.path.splitext(file) | ||
|
||
print(input, input_path, system_type, cell_para, debug_mode, name, extension) | ||
if not os.path.exists(input_path): | ||
raise FileNotFoundError(f"Input file not found: {input_path}") | ||
|
||
if extension == ".cif": | ||
handle_cif_file(input_path, system_type, name, current_dir, debug_mode) | ||
elif extension == ".xyz": | ||
handle_xyz_file(input_path, system_type, name, cell_para, current_dir, debug_mode) | ||
else: | ||
sys.exit("Invalid file extension") | ||
|
||
|
||
def handle_cif_file(input_path, system_type, name, current_dir, debug_mode): | ||
if system_type == "reference": | ||
print("Processing reference (Wyckoff sites) from .cif file") | ||
process_refcell(input_path, name, current_dir, debug_mode) | ||
elif system_type == "unitcell": | ||
print("Processing unit cell from .cif file") | ||
process_unitcell(input_path, name, current_dir, debug_mode) | ||
else: | ||
sys.exit("Invalid system type for .cif file") | ||
|
||
|
||
def handle_xyz_file(input_path, system_type, name, cell_para, current_dir, debug_mode): | ||
if system_type == "unitcell": | ||
if cell_para is None: | ||
sys.exit("Cell parameters must be provided for .xyz file of a unit cell") | ||
else: | ||
print("Processing unit cell from .xyz file") | ||
# users should provide chemical formula (Fe-O2-H3) of refmoleculist | ||
# if users provide smiles, we check compare_species in connectivity module | ||
elif system_type == "molecule": | ||
print("Processing molecule from .xyz file") | ||
get_molecule(input_path, name, current_dir, debug_mode) | ||
else: | ||
sys.exit("Invalid system type for .xyz file") | ||
|
||
def write_error(error_obj, prefix): | ||
if hasattr(error_obj, "error_case"): | ||
case = error_obj.error_case | ||
error_filepath = os.path.join(current_dir, f"{prefix}_error_{case}.out") | ||
write_to_file(error_filepath, lambda: handle_error(case)) | ||
|
||
if __name__ == "__main__" or __name__ == "cell2mol.final_c2m_driver": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
import sys | ||
from ase.io import read | ||
from contextlib import redirect_stdout | ||
from cell2mol.classes import cell | ||
from cell2mol.read_write import get_wyckoff_positions | ||
from cell2mol.cell_operations import frac2cart_fromparam | ||
from cell2mol.new_cell_reconstruction import modify_cov_factor_due_to_H, modify_cov_factor_due_to_possible_charges | ||
from cell2mol.other import handle_error | ||
|
||
# Constants | ||
VERSION = "2.0" | ||
COV_FACTOR = 1.3 | ||
METAL_FACTOR = 1.0 | ||
|
||
def process_refcell(input_path, name, current_dir, debug=0): | ||
# Set up filenames | ||
cell_fname = os.path.join(current_dir, f"Cell_{name}.cell") | ||
ref_cell_fname = os.path.join(current_dir, f"Ref_Cell_{name}.cell") | ||
output_fname = os.path.join(current_dir, "cell2mol.out") | ||
summary_fname = os.path.join(current_dir, "summary.out") | ||
|
||
|
||
# Redirect stdout to the output file for logging | ||
with open(output_fname, "w") as output: | ||
with redirect_stdout(output): | ||
print(f"cell2mol version {VERSION}") | ||
print(f"INITIATING cell object from input path: {input_path}") | ||
print(f"Debug level: {debug}") | ||
|
||
# # Read .cif file | ||
structure = read(input_path) | ||
cell_vector = structure.cell.array | ||
cell_param = structure.cell.cellpar() | ||
|
||
# Create the reference cell | ||
refcell = create_reference(input_path, name, cell_vector, cell_param, debug) | ||
|
||
# Finalize and save the reference cell object if no errors | ||
if refcell.error_case == 0: | ||
get_unique_species_in_reference(refcell, debug) | ||
else: | ||
print(f"Error occurred in processing reference cell: error case {refcell.error_case}") | ||
refcell.save(ref_cell_fname) | ||
|
||
error_fname = os.path.join(current_dir, f"reference_error_{refcell.error_case}.out") | ||
with open(error_fname, "w") as error_output: | ||
with redirect_stdout(error_output): | ||
handle_error(refcell.error_case) | ||
return refcell | ||
|
||
def create_reference (input_path, name, cell_vector, cell_param, debug): | ||
"""Create the reference cell object.""" | ||
|
||
ref_labels, ref_fracs = get_wyckoff_positions(input_path) | ||
ref_pos = frac2cart_fromparam(ref_fracs, cell_param) | ||
|
||
refcell = cell(name, ref_labels, ref_pos, ref_fracs, cell_vector, cell_param) | ||
refcell.get_subtype("reference") | ||
refcell.get_reference_molecules(ref_labels, ref_fracs, cov_factor=COV_FACTOR, debug=debug) | ||
refcell = modify_cov_factor_due_to_H(refcell, debug=debug) | ||
|
||
return refcell | ||
|
||
def get_unique_species_in_reference (refcell, debug): | ||
"""Processes the reference cell to obtain unique species and handle any errors.""" | ||
|
||
refcell.get_unique_species(debug=debug) | ||
if debug >= 1: | ||
print(f"Unique species: {[specie.formula for specie in refcell.unique_species]}") | ||
print(f"Species list: {[specie.formula for specie in refcell.species_list]}\n") | ||
|
||
refcell = modify_cov_factor_due_to_possible_charges(refcell, debug=debug) | ||
refcell.get_selected_cs(debug=debug) | ||
refcell.assess_errors(mode="possible_charges") | ||
|
||
# Run the main function | ||
if __name__ == "__main__": | ||
|
||
input = sys.argv[1] | ||
current_dir = os.getcwd() | ||
input_path = os.path.normpath(input) | ||
dir, file = os.path.split(input_path) | ||
name, extension = os.path.splitext(file) | ||
|
||
# Example usage, replace with actual arguments | ||
process_refcell(input_path, name, current_dir, debug=1) |
Oops, something went wrong.