Skip to content

Commit

Permalink
fix a tiny bug in rep
Browse files Browse the repository at this point in the history
  • Loading branch information
qzhu2017 committed Jan 17, 2024
1 parent 74c6bf7 commit f521572
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 50 deletions.
29 changes: 14 additions & 15 deletions pyxtal/plane.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def search_close_packing_planes(self, N_max=10):
if dspacing/n > self.d_min:
hkl1 = n*np.array(hkl)
F = self.get_structure_factor(hkl1)
if np.abs(F) >= len(self.atoms) * self.cp_factor:
if np.abs(F) >= len(self.atoms) * self.cp_factor:
# Scan the plane with high density
plane = self.get_separation(hkl1)
if plane is not None:
Expand All @@ -113,7 +113,7 @@ def get_structure_factor(self, hkl):
def get_separation(self, hkl):
"""
Compute the separation for the given hkl plane
Args:
- hkl: three indices
"""
Expand All @@ -135,18 +135,18 @@ def get_separation(self, hkl):
center_hkl = np.dot(center_frac, hkl_reduced)
center_hkl -= np.floor(center_hkl)
coords_hkl = np.dot(coords, hkl_reduced)

lower, upper = center_hkl+coords_hkl.min(), center_hkl+coords_hkl.max()
slabs.append([center_hkl, lower, upper])
#if np.abs(hkl-np.array([0, 8, 0])).sum()==0:
#if np.abs(hkl-np.array([0, 8, 0])).sum()==0:
groups = self.group_slabs(slabs, 0.5/hkl_factor)
groups = self.group_slabs(groups, 0.5/hkl_factor)
groups = self.group_slabs(groups, 0.5/hkl_factor)
#print(hkl); print(groups)
separations = self.find_unique_separations(groups, d_spacing)
return (hkl, d_spacing/abs(hkl_factor), separations)

def group_slabs(self, slabs, tol):
groups = []
groups = []
for slab in slabs:
new = True
center, lower, upper = slab
Expand Down Expand Up @@ -174,7 +174,7 @@ def group_slabs(self, slabs, tol):
new = False
lower -= shift
upper -= shift

if not new:
# Update group
if lower < group[1]:
Expand All @@ -192,15 +192,15 @@ def find_unique_separations(self, groups, d):
groups[-1] = [g+1 for g in groups[-1]]
separations = []
for i in range(len(groups)-1):
separations.append(d*(groups[i+1][1] - groups[i][2]))
separations.append(d*(groups[i+1][1] - groups[i][2]))
separations = np.unique(-1*np.array(separations).round(decimals=3))
return -np.sort(separations)

def gather(self, planes, tol=-0.1):
output = []
for _plane in planes:
(hkl, d, separations) = _plane
if separations[0] <= d:
if separations[0] <= d:
if separations[0] > tol:
for separation in separations:
if separation > tol:
Expand All @@ -213,7 +213,7 @@ def gather(self, planes, tol=-0.1):

class plane():
"""
This simplest possible plane object
This simplest possible plane object
"""

def __init__(self, hkl, cell_reciprocal, separation=None):
Expand All @@ -227,21 +227,20 @@ def __str__(self):
s = "({:2d} {:2d} {:2d})".format(*self.indices)
s += " Spacing: {:6.3f}".format(self.d_spacing)
s += " Separation: {:6.3f}".format(self.separation)

return s

if __name__ == "__main__":
from pyxtal.db import database
import sys
try:
try:
from ccdc import io
from ccdc.particle import SlipPlanes
csd = io.EntryReader('CSD')
except:
csd = None
print("Cannot import ccdc to check")

db = database('pyxtal/database/mech.db')
db = database('pyxtal/database/mech.db')

if len(sys.argv) == 1:
codes = ['ANLINB02', 'ADIPAC', 'CHEXDC']
Expand Down Expand Up @@ -270,4 +269,4 @@ def __str__(self):
print(hkl, tuple(hkl) in p.planes)
p.set_xtal(db.get_pyxtal(code))
print(code, hkl, p.get_separation(hkl))

69 changes: 35 additions & 34 deletions pyxtal/representation.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
from pyxtal.lattice import Lattice
from pyxtal.wyckoff_site import mol_site, atom_site
from pyxtal.molecule import find_rotor_from_smile

class representation_atom():
"""
A class to handle the 1D representation of atomic crystal
A class to handle the 1D representation of atomic crystal
Works for Zprime > 1
Args:
Expand Down Expand Up @@ -39,11 +39,11 @@ def from_pyxtal(cls, struc, standard=False):
vector.append(site.encode())
x = vector
return cls(x)

def to_standard_setting(self):
xtal = self.to_pyxtal()
self.x = representation.from_pyxtal(xtal, standard=True).x

def to_pyxtal(self):
"""
Export the pyxtal structure
Expand All @@ -58,7 +58,7 @@ def to_pyxtal(self):
v = self.x[0]
struc = pyxtal()
struc.group, number = Group(v[0], use_hall=True), v[0]

# lattice
ltype = struc.group.lattice_type
if ltype == 'triclinic':
Expand All @@ -78,13 +78,13 @@ def to_pyxtal(self):
except:
print(a, b, c, alpha, beta, gamma, ltype)
raise ValueError("Problem in Lattice")

# sites
struc.numIons = [0] * len(smiles)
struc.atom_sites = []
struc.numIons = [0] * len(smiles)
struc.atom_sites = []

count = 1
for i, comp in enumerate(composition):
for i, comp in enumerate(composition):
for j in range(comp):
v = self.x[count]
dicts = {}
Expand All @@ -109,7 +109,7 @@ def to_pyxtal(self):
struc.standard_setting = site.wp.is_standard_setting()

return struc

def to_array(self):
"""
Export only varibles to a 1d numpy array
Expand All @@ -118,7 +118,7 @@ def to_array(self):
x = cells
for xyz in xyzs: x = np.hstack((x, xyz[2:]))
return x

def to_string(self, time=None, eng=None, tag=None):
"""
Export string representation
Expand All @@ -143,30 +143,30 @@ def to_string(self, time=None, eng=None, tag=None):
strs += "{:5.2f} ".format(c)
for c in x[0][num:]:
strs += "{:5.1f} ".format(c)

# data for atoms
strs += "{:d} ".format(len(x)-1) # Number of sites
for i in range(1, len(x)):
strs += "{:s} ".format(x[i][0])
strs += "{:d} ".format(x[i][1])
for v in x[i][2:]:
strs += "{:4.2f} ".format(v)
strs += "{:4.2f} ".format(v)

if time is not None:
strs += "{:5.2f}".format(time)

if eng is not None:
strs += "{:11.3f}".format(eng)

if tag is not None:
strs += " {:s}".format(tag)

return strs


class representation():
"""
A class to handle the 1D representation of molecular crystal
A class to handle the 1D representation of molecular crystal
Works for Zprime > 1
Args:
Expand All @@ -178,7 +178,7 @@ def __init__(self, x, smiles=None):
if smiles is not None:
self.smiles = []
for i, smile in enumerate(smiles):
if smile.endswith('.smi'):
if smile.endswith('.smi'):
smile = smile[:-4]
self.smiles.append(smile)
else:
Expand Down Expand Up @@ -209,14 +209,14 @@ def from_pyxtal(cls, struc, standard=False):
smiles.append(site.molecule.smile)
x = vector
return cls(x, smiles)

@classmethod
def from_string(cls, inputs, smiles, composition=None):
"""
Initialize 1D rep. from the string
Args:
inputs: input string
inputs: input string
smiles: list of smiles
"""
#parse the cell
Expand All @@ -236,7 +236,7 @@ def from_string(cls, inputs, smiles, composition=None):
else:
n_cell = 3 #cubic
cell = [hn] + inputs[1:n_cell-1]

x = [cell]
n_site = int(inputs[n_cell-1])
if n_site != sum(composition):
Expand All @@ -246,7 +246,7 @@ def from_string(cls, inputs, smiles, composition=None):
n_cell += 1

for i, smile in enumerate(smiles):
if smile.endswith('.smi'):
if smile.endswith('.smi'):
smile=smile[:-4]
for c in range(composition[i]):
if smile in ["Cl-"]:
Expand All @@ -264,7 +264,7 @@ def to_standard_setting(self):
xtal = self.to_pyxtal()
rep0 = representation.from_pyxtal(xtal, standard=True)
self.x = rep0.x

def to_pyxtal(self, smiles=None, composition=None):
"""
Export the pyxtal structure
Expand All @@ -290,7 +290,7 @@ def to_pyxtal(self, smiles=None, composition=None):
v = self.x[0]
struc = pyxtal(molecular=True)
struc.group, number = Group(v[0], use_hall=True), v[0]

# lattice
ltype = struc.group.lattice_type
if ltype == 'triclinic':
Expand All @@ -310,14 +310,14 @@ def to_pyxtal(self, smiles=None, composition=None):
except:
print(a, b, c, alpha, beta, gamma, ltype)
raise ValueError("Problem in Lattice")

# sites
struc.numMols = [0] * len(smiles)
struc.numMols = [0] * len(smiles)
struc.molecules = []
struc.mol_sites = []
struc.mol_sites = []

count = 1
for i, comp in enumerate(composition):
for i, comp in enumerate(composition):
smile = smiles[i]
if smile.endswith('.smi'): smile=smile[:-4]
for j in range(comp):
Expand All @@ -337,6 +337,7 @@ def to_pyxtal(self, smiles=None, composition=None):
dicts['orientation'] = np.array(v[4:7])
dicts['rotor'] = v[7:-1]
dicts['reflect'] = int(v[-1])
print(dicts)
site = mol_site.from_1D_dicts(dicts)
site.type = i
struc.mol_sites.append(site)
Expand All @@ -351,7 +352,7 @@ def to_pyxtal(self, smiles=None, composition=None):
struc.standard_setting = site.wp.is_standard_setting()

return struc

def to_string(self, time=None, eng=None, tag=None):
"""
Export string representation
Expand All @@ -376,26 +377,26 @@ def to_string(self, time=None, eng=None, tag=None):
strs += "{:5.2f} ".format(c)
for c in x[0][num:]:
strs += "{:5.1f} ".format(c)

# data for molecule
strs += "{:d} ".format(len(x)-1)
for i in range(1, len(x)):
strs += "{:d} ".format(x[i][0])
for v in x[i][1:4]:
strs += "{:4.2f} ".format(v)
strs += "{:4.2f} ".format(v)
for v in x[i][4:-1]:
strs += "{:6.1f} ".format(v)
strs += "{:6.1f} ".format(v)
strs += "{:d} ".format(int(x[i][-1]))

if time is not None:
strs += "{:5.2f}".format(time)

if eng is not None:
strs += "{:11.3f}".format(eng)

if tag is not None:
strs += " {:s}".format(tag)

return strs

def same_smiles(self, smiles):
Expand All @@ -406,7 +407,7 @@ def same_smiles(self, smiles):
return True
else:
return False

def get_dist(self, rep):
"""
get distance with the other rep1
Expand Down
2 changes: 1 addition & 1 deletion pyxtal/wyckoff_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def from_1D_dicts(cls, dicts):
orientation = Orientation(np.eye(3))

g = dicts["hn"]
index = dicts["index"]
index = int(dicts["index"])
dim = dicts["dim"]
wp = Wyckoff_position.from_group_and_index(g, index, dim, dicts["PBC"])
lattice = Lattice.from_matrix(dicts["lattice"], ltype=dicts["lattice_type"])
Expand Down

0 comments on commit f521572

Please sign in to comment.