From 281a19f9f972f0aa4ee943914d49e3e7aeaca5b3 Mon Sep 17 00:00:00 2001 From: Yuman Hordijk Date: Wed, 7 Feb 2024 14:20:47 +0100 Subject: [PATCH] Fixed an issue where charge and spinpol were not being read if the second method of fragment specification is used --- examples/job/NaCl.xyz | 7 +++++-- src/tcutility/molecule.py | 13 +++++++------ test/fixtures/xyz/NaCl.xyz | 7 +++++-- 3 files changed, 17 insertions(+), 10 deletions(-) diff --git a/examples/job/NaCl.xyz b/examples/job/NaCl.xyz index 3cfc0a53..c7477722 100644 --- a/examples/job/NaCl.xyz +++ b/examples/job/NaCl.xyz @@ -1,7 +1,10 @@ 2 -Na 0 0 2 frag=Na -Cl 0 0 0 frag=Cl +Na 0 0 2 +Cl 0 0 0 + +frag_Na = 1 +frag_Cl = 2 charge_Na = 1 charge_Cl = -1 \ No newline at end of file diff --git a/src/tcutility/molecule.py b/src/tcutility/molecule.py index 543e4746..5bf056ba 100644 --- a/src/tcutility/molecule.py +++ b/src/tcutility/molecule.py @@ -194,6 +194,7 @@ def guess_fragments(mol: plams.Molecule) -> Dict[str, plams.Molecule]: if len(fragment_flags) > 0: fragment_mols = {frag.removeprefix('frag_'): plams.Molecule() for frag in fragment_flags} for frag in fragment_flags: + frag_name = frag.removeprefix('frag_') indices = [] index_line = ensure_list(mol.flags[frag]) for indx in index_line: @@ -204,12 +205,12 @@ def guess_fragments(mol: plams.Molecule) -> Dict[str, plams.Molecule]: else: raise ValueError(f'Fragment index {indx} could not be parsed.') - [fragment_mols[frag.removeprefix('frag_')].add_atom(mol[i]) for i in indices] - fragment_mols[frag.removeprefix('frag_')].flags = {'tags': set()} - if f'charge_{frag}' in mol.flags: - fragment_mols[frag.removeprefix('frag_')].flags['charge'] = mol.flags[f'charge_{frag}'] - if f'spinpol_{frag}' in mol.flags: - fragment_mols[frag.removeprefix('frag_')].flags['spinpol'] = mol.flags[f'spinpol_{frag}'] + [fragment_mols[frag_name].add_atom(mol[i]) for i in indices] + fragment_mols[frag_name].flags = {'tags': set()} + if f'charge_{frag_name}' in mol.flags: + fragment_mols[frag_name].flags['charge'] = mol.flags[f'charge_{frag_name}'] + if f'spinpol_{frag_name}' in mol.flags: + fragment_mols[frag_name].flags['spinpol'] = mol.flags[f'spinpol_{frag_name}'] return fragment_mols diff --git a/test/fixtures/xyz/NaCl.xyz b/test/fixtures/xyz/NaCl.xyz index 3cfc0a53..c7477722 100644 --- a/test/fixtures/xyz/NaCl.xyz +++ b/test/fixtures/xyz/NaCl.xyz @@ -1,7 +1,10 @@ 2 -Na 0 0 2 frag=Na -Cl 0 0 0 frag=Cl +Na 0 0 2 +Cl 0 0 0 + +frag_Na = 1 +frag_Cl = 2 charge_Na = 1 charge_Cl = -1 \ No newline at end of file