From e8e61e865c1040f2414991eb1add6865083b7d0e Mon Sep 17 00:00:00 2001 From: Tan Le Date: Fri, 6 Dec 2024 09:01:05 +0800 Subject: [PATCH] attr test --- src/qibochem/driver/molecule.py | 58 ++++++++++++++++----------------- 1 file changed, 28 insertions(+), 30 deletions(-) diff --git a/src/qibochem/driver/molecule.py b/src/qibochem/driver/molecule.py index 6d1fbfa..8f5b76c 100644 --- a/src/qibochem/driver/molecule.py +++ b/src/qibochem/driver/molecule.py @@ -37,10 +37,6 @@ class Molecule: charge = attr.ib(default=0, validator=attr.validators.instance_of(int)) multiplicity = attr.ib(default=1, validator=attr.validators.instance_of(int)) xyz_file = attr.ib(default=1, validator=attr.validators.instance_of(str)) - if xyz_file is not None: - # Check if xyz_file exists, then fill in the Molecule attributes - assert Path(f"{xyz_file}").exists(), f"{xyz_file} not found!" - self._process_xyz_file(xyz_file, charge, multiplicity) basis = attr.ib(default="sto-3g", validator=attr.validators.instance_of(str)) nelec = attr.ib(default=None, validator=attr.validators.instance_of(int)) @@ -75,7 +71,7 @@ class Molecule: n_active_e = attr.ib(default=None, validator=attr.validators.instance_of(int)) n_active_orbs = attr.ib(default=None, validator=attr.validators.instance_of(int)) - def _process_xyz_file(self, xyz_file, charge, multiplicity): + def __attrs_post_init__(self): """ Reads a .xyz file to obtain and set the molecular coordinates (in OpenFermion format), charge, and multiplicity @@ -83,31 +79,33 @@ def _process_xyz_file(self, xyz_file, charge, multiplicity): Args: xyz_file: .xyz file for molecule. Comment line should follow "{charge} {multiplicity}" """ - with open(xyz_file, encoding="utf-8") as file_handler: - # First two lines: # atoms and comment line (charge, multiplicity) - _n_atoms = int(file_handler.readline()) # Not needed/used - - # Try to read charge and multiplicity from comment line - split_line = [int(_num) for _num in file_handler.readline().split()] - if len(split_line) == 2: - # Format of comment line matches (charge, multiplicity): - _charge, _multiplicity = split_line - else: - # Otherwise, use the default (from __init__) values of 0 and 1 - _charge, _multiplicity = charge, multiplicity - - # Start reading xyz coordinates from the 3rd line onwards - _geometry = [] - for line in file_handler: - split_line = line.split() - # OpenFermion format: [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7)), ...] - atom_xyz = [split_line[0], tuple(float(_xyz) for _xyz in split_line[1:4])] - _geometry.append(tuple(atom_xyz)) - - # Set the class attributes - self.charge = _charge - self.multiplicity = _multiplicity - self.geometry = _geometry + assert Path(f"{xyz_file}").exists(), f"{xyz_file} not found!" + if xyz_file is not None: + with open(xyz_file, encoding="utf-8") as file_handler: + # First two lines: # atoms and comment line (charge, multiplicity) + _n_atoms = int(file_handler.readline()) # Not needed/used + + # Try to read charge and multiplicity from comment line + split_line = [int(_num) for _num in file_handler.readline().split()] + if len(split_line) == 2: + # Format of comment line matches (charge, multiplicity): + _charge, _multiplicity = split_line + else: + # Otherwise, use the default (from __init__) values of 0 and 1 + _charge, _multiplicity = charge, multiplicity + + # Start reading xyz coordinates from the 3rd line onwards + _geometry = [] + for line in file_handler: + split_line = line.split() + # OpenFermion format: [('H', (0.0, 0.0, 0.0)), ('H', (0.0, 0.0, 0.7)), ...] + atom_xyz = [split_line[0], tuple(float(_xyz) for _xyz in split_line[1:4])] + _geometry.append(tuple(atom_xyz)) + + # Set the class attributes + self.charge = _charge + self.multiplicity = _multiplicity + self.geometry = _geometry def run_pyscf(self, max_scf_cycles=50): """