Skip to content

Commit 77e8a52

Browse files
committed
Success in YOBCUO
1 parent 0cfeccf commit 77e8a52

15 files changed

+2402
-2234
lines changed

cell2mol/cell_reconstruction.py

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
import itertools
33
from cell2mol.cell_operations import translate
44
from cell2mol.other import additem, absolute_value
5-
from cell2mol.connectivity import compare_species, count_species, split_species
5+
from cell2mol.connectivity import compare_species, count_species, split_species, arrange_data_for_reorder
6+
from cell2mol.hungarian import reorder
67
from cell2mol.elementdata import ElementData
8+
from cell2mol.read_write import writexyz
9+
710
elemdatabase = ElementData()
811

912
#######################################################
@@ -208,6 +211,9 @@ def fragments_reconstruct(moleclist: list, fraglist: list, Hlist: list, refmolec
208211
moleclist.extend(finalmols)
209212
print(f"{finalmols=}")
210213
print(f"{remfrag=}")
214+
for i, g in enumerate(finalmols):
215+
if debug == 1: writexyz("/Users/ycho/cell2mol/cell2mol/test/YOBCUO/", f"reorder_molec_{i}.xyz", g.labels, g.coord)
216+
211217
if len(remfrag) > 0: Warning = True; print("FRAG_RECONSTRUCT. Remaining after Hydrogen reconstruction",remfrag)
212218
else: Warning = False; print("FRAG_RECONSTRUCT. No remaining Molecules after Hydrogen reconstruction")
213219
elif len(remfrag) > 0 and len(Hlist) == 0:
@@ -221,6 +227,7 @@ def fragments_reconstruct(moleclist: list, fraglist: list, Hlist: list, refmolec
221227
# The former were identified in the cell.get_moleclist() function, and have cell as parent.
222228
# The latter have been constructed by merging fragments, and do not have cell as parent, but have the cell_indices stored in mol.cell_indices
223229
# Here we homogenize the situation by adding the cell_indices variable to all molecules
230+
# TODO : why cell_indices is needed?
224231
for mol in moleclist:
225232
if not hasattr(mol,"cell_indices"):
226233
if mol.check_parent("cell"):
@@ -466,6 +473,8 @@ def sequential(fragmentlist: list, refmoleclist: list, cellvec: list, factor: fl
466473

467474
#######################################################
468475
def combine(tobemerged: list, references: list, cellvec: list, threshold_tmat: float, cov_factor: float, metal_factor: float, debug: int=0):
476+
from cell2mol.classes import molecule
477+
469478
goodlist = [] ## List of molecules coming from the two fragments received
470479
avglist = [] ## List of bigger fragments coming from the two fragments received
471480
badlist = [] ## List of fragments as they entered the function
@@ -488,12 +497,33 @@ def combine(tobemerged: list, references: list, cellvec: list, threshold_tmat: f
488497
found = False
489498
for ref in references:
490499
if not found:
491-
issame = compare_species(newmolec, ref)
492-
if issame: ## Then is a molecule that appears in the reference list
493-
found = True
494-
newmolec.subtype = ref.subtype
495-
goodlist.append(newmolec)
496-
if debug >= 1: print(f"COMBINE: Fragment {newmolec.formula} added to goodlist with {newmolec.cell_indices=}")
500+
if (newmolec.natoms == ref.natoms) and (newmolec.eleccount == ref.eleccount) and (newmolec.formula == ref.formula):
501+
dummy1, dummy2, map12 = reorder(ref.labels, newmolec.labels, ref.coord, newmolec.coord)
502+
503+
reordered_labels = [newmolec.labels[i] for i in map12]
504+
reordered_coord = [newmolec.coord[i] for i in map12]
505+
reordered_radii = [newmolec.radii[i] for i in map12]
506+
reordered_frac_cood = [newmolec.frac_coord[i] for i in map12]
507+
reordered_cell_indices = [newmolec.cell_indices[i] for i in map12]
508+
509+
reordered_newmolec = molecule(reordered_labels, reordered_coord, reordered_radii)
510+
reordered_newmolec.cell_indices = reordered_cell_indices
511+
reordered_newmolec.set_fractional_coord(reordered_frac_cood)
512+
reordered_newmolec.set_atoms(create_adjacencies=True, debug=2)
513+
if reordered_newmolec.iscomplex:
514+
reordered_newmolec.split_complex()
515+
reordered_newmolec.get_hapticity(debug=debug)
516+
for lig in reordered_newmolec.ligands:
517+
lig.get_denticity(debug=debug)
518+
519+
print(f"{reordered_newmolec=}")
520+
521+
issame = compare_species(reordered_newmolec, ref, debug=2)
522+
if issame: ## Then is a molecule that appears in the reference list
523+
found = True
524+
reordered_newmolec.subtype = ref.subtype
525+
goodlist.append(reordered_newmolec)
526+
if debug >= 1: print(f"COMBINE: Fragment {reordered_newmolec.formula} added to goodlist with {reordered_newmolec.cell_indices=}")
497527
if not found: ## Then it is a fragment. A bigger one, but still a fragment
498528
newmolec.subtype = "Rec. Fragment"
499529
avglist.append(newmolec)

cell2mol/charge_assignment.py

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -864,40 +864,6 @@ def prepare_unresolved(unique_indices: list, unique_species: list, distributions
864864

865865
return list_molecules, list_indices, list_options
866866

867-
#######################################################
868-
def arrange_data_for_reorder(reference: object, target: object, debug: int=0):
869-
# To do the reorder, we create new tags that include as much information as possible.
870-
# Ideally, we aim to include the label + the connectivity + the metal connectivity
871-
t_totconnec = 0
872-
t_totmconnec = 0
873-
for a in target.atoms:
874-
t_totconnec += a.connec
875-
t_totmconnec += a.mconnec
876-
r_totconnec = 0
877-
r_totmconnec = 0
878-
for a in reference.atoms:
879-
r_totconnec += a.connec
880-
r_totmconnec += a.mconnec
881-
if t_totconnec == r_totconnec: useconec = True
882-
else: useconec = False
883-
if t_totmconnec == r_totmconnec: usemconec = True
884-
else: usemconec = False
885-
# For target
886-
target_data = []
887-
for a in target.atoms:
888-
data = a.label
889-
if useconec: data += str(a.connec)
890-
if usemconec: data += str(a.mconnec)
891-
target_data.append(data)
892-
# For reference
893-
ref_data = []
894-
for a in reference.atoms:
895-
data = a.label
896-
if useconec: data += str(a.connec)
897-
if usemconec: data += str(a.mconnec)
898-
ref_data.append(data)
899-
return ref_data, target_data
900-
901867
#######################################################
902868
def set_charges_create_bonds (specie, unique_indices, unique_species, final_charge_distribution, debug):
903869

cell2mol/classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1356,7 +1356,7 @@ def reconstruct(self, cov_factor: float=None, metal_factor: float=None, debug: i
13561356
newmolec = molecule(mol.labels, mol.coord)
13571357
newmolec.origin = "cell.reconstruct"
13581358
newmolec.set_atoms(create_adjacencies=True, debug=debug)
1359-
newmolec.add_parent(self,mol.cell_indices)
1359+
newmolec.add_parent(self, mol.cell_indices)
13601360
newmolec.set_fractional_coord(mol.frac_coord)
13611361
if newmolec.iscomplex: newmolec.split_complex()
13621362
self.moleclist.append(newmolec)

cell2mol/connectivity.py

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from typing import Tuple
77
from cell2mol.other import inv
88
from cell2mol.elementdata import ElementData
9+
from cell2mol.read_write import writexyz
910
elemdatabase = ElementData()
1011

1112
#######################################################
@@ -374,6 +375,12 @@ def compare_species(mol1, mol2, check_coordinates: bool=False, debug: int=0):
374375
if debug == 1: print(mol2.formula)
375376
if debug == 2: print(mol1)
376377
if debug == 2: print(mol2)
378+
if debug == 2: print(mol1.labels)
379+
if debug == 2: print(mol2.labels)
380+
381+
if debug == 2: writexyz("/Users/ycho/cell2mol/cell2mol/test/YOBCUO/", "reorder_molec.xyz", mol1.labels, mol1.coord)
382+
if debug == 2: writexyz("/Users/ycho/cell2mol/cell2mol/test/YOBCUO/", "ref.xyz", mol2.labels, mol2.coord)
383+
377384
# a pair of species is compared on the basis of:
378385
# 1) the total number of atoms
379386
if (mol1.natoms != mol2.natoms):
@@ -396,10 +403,16 @@ def compare_species(mol1, mol2, check_coordinates: bool=False, debug: int=0):
396403
# 4) the number of adjacencies between each pair of element types
397404
if not hasattr(mol1,"adj_types"): mol1.set_adj_types()
398405
if not hasattr(mol2,"adj_types"): mol2.set_adj_types()
406+
if debug == 2: print(f"{mol1.adj_types=}")
407+
if debug == 2: print(f"{mol2.adj_types=}")
408+
if debug == 2: np.save("/Users/ycho/cell2mol/cell2mol/test/YOBCUO/adj_types1.npy", mol1.adj_types)
409+
if debug == 2: np.save("/Users/ycho/cell2mol/cell2mol/test/YOBCUO/adj_types2.npy", mol2.adj_types)
399410
for kdx, elem in enumerate(mol1.adj_types):
400411
for ldx, elem2 in enumerate(elem):
401412
if elem2 != mol2.adj_types[kdx, ldx]:
402413
if debug > 0: print(f"COMPARE_SPECIES. FALSE, different adjacency count")
414+
if debug > 0: print(f"{kdx=} {ldx=} {elem=} {elem2=} {mol2.adj_types[kdx]=} {mol2.adj_types[kdx, ldx]=}")
415+
if debug > 0: print(f"{mol1.labels[kdx]=} {mol1.labels[ldx]=} {mol2.labels[kdx]=} {mol2.labels[ldx]=}")
403416
return False
404417

405418
if check_coordinates:
@@ -411,4 +424,35 @@ def compare_species(mol1, mol2, check_coordinates: bool=False, debug: int=0):
411424
return True
412425

413426
#################################
414-
427+
def arrange_data_for_reorder(reference: object, target: object, debug: int=0):
428+
# To do the reorder, we create new tags that include as much information as possible.
429+
# Ideally, we aim to include the label + the connectivity + the metal connectivity
430+
t_totconnec = 0
431+
t_totmconnec = 0
432+
for a in target.atoms:
433+
t_totconnec += a.connec
434+
t_totmconnec += a.mconnec
435+
r_totconnec = 0
436+
r_totmconnec = 0
437+
for a in reference.atoms:
438+
r_totconnec += a.connec
439+
r_totmconnec += a.mconnec
440+
if t_totconnec == r_totconnec: useconec = True
441+
else: useconec = False
442+
if t_totmconnec == r_totmconnec: usemconec = True
443+
else: usemconec = False
444+
# For target
445+
target_data = []
446+
for a in target.atoms:
447+
data = a.label
448+
if useconec: data += str(a.connec)
449+
if usemconec: data += str(a.mconnec)
450+
target_data.append(data)
451+
# For reference
452+
ref_data = []
453+
for a in reference.atoms:
454+
data = a.label
455+
if useconec: data += str(a.connec)
456+
if usemconec: data += str(a.mconnec)
457+
ref_data.append(data)
458+
return ref_data, target_data

cell2mol/test/YOBCUO/YOBCUO.cell

1.9 MB
Binary file not shown.

0 commit comments

Comments
 (0)