Skip to content

Commit

Permalink
attr test
Browse files Browse the repository at this point in the history
  • Loading branch information
TL231 committed Dec 6, 2024
1 parent af5606f commit e8e61e8
Showing 1 changed file with 28 additions and 30 deletions.
58 changes: 28 additions & 30 deletions src/qibochem/driver/molecule.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down Expand Up @@ -75,39 +71,41 @@ 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
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):
"""
Expand Down

0 comments on commit e8e61e8

Please sign in to comment.