def from_constraints(self, v1, c1, v2, c2): """ Geneate an orientation object given two constraint vectors Args: v1: a 1x3 vector in the original reference frame c1: a corresponding axis which v1 must be mapped to v1: a second 1x3 vector in the original reference frame c1: a corresponding axis which v2 must be mapped to Returns: an orientation object consistent with the supplied constraints """ T = rotate_vector(v1, c1) phi = angle(c1, c2) phi2 = angle(c1, (np.dot(T, v2))) if not np.isclose(phi, phi2, rtol=0.01): printx("Error: constraints and vectors do not match.", priority=1) return r = np.sin(phi) c = np.linalg.norm(np.dot(T, v2) - c2) theta = np.arccos(1 - (c ** 2) / (2 * (r ** 2))) Rot = R.from_rotvec(theta * c1) T2 = np.dot(R, T) a = angle(np.dot(T2, v2), c2) if not np.isclose(a, 0, rtol=0.01): T2 = np.dot(np.linalg.inv(R), T) a = angle(np.dot(T2, v2), c2) if not np.isclose(a, 0, rtol=0.01): printx("Error: Generated incorrect rotation: " + str(theta), priority=1) return Orientation(T2, degrees=0)
def rotate_by_matrix(self, matrix, ignore_constraint=True): """ rotate Args: matrix: 3*3 rotation matrix """ if not ignore_constraint: if self.degrees == 0: raise ValueError("cannot rotate") elif self.degrees == 1: axis = self.axis vec = Rotation.from_matrix(matrix).as_rotvec() if angle(vec, self.axis) > 1e-2 and angle(vec, -self.axis) > 1e-2: raise ValueError("must rotate along the given axis") else: axis = None matrix = matrix.dot(self.matrix) return Orientation(matrix, self.degrees, axis)
def orientation_in_wyckoff_position( mol, wyckoff_position, randomize=True, exact_orientation=False, already_oriented=False, allow_inversion=True, rtol=1e-2, ): """ Tests if a molecule meets the symmetry requirements of a Wyckoff position, and returns the valid orientations. Args: mol: a Molecule object. Orientation is arbitrary wyckoff_position: a pyxtal.symmetry.Wyckoff_position object randomize: whether or not to apply a random rotation consistent with the symmetry requirements exact_orientation: whether to only check compatibility for the provided orientation of the molecule. Used within general case for checking. If True, this function only returns True or False already_oriented: whether or not to reorient the principle axes when calling get_symmetry. Setting to True can remove redundancy, but is not necessary allow_inversion: whether or not to allow chiral molecules to be inverted. Should only be True if the chemical and biological properties of the mirror image are known to be suitable for the desired application Returns: a list of operations.Orientation objects which can be applied to the molecule while allowing it to satisfy the symmetry requirements of the Wyckoff position. If no orientations are found, returns False. """ # For single atoms, there are no constraints if len(mol) == 1: return [Orientation([[1, 0, 0], [0, 1, 0], [0, 0, 1]], degrees=2)] wyckoffs = wyckoff_position.ops w_symm = wyckoff_position.symmetry_m # Obtain the Wyckoff symmetry symm_w = w_symm[0] pga = PointGroupAnalyzer(mol) # Check exact orientation if exact_orientation is True: mo = deepcopy(mol) valid = True for op in symm_w: if not pga.is_valid_op(op): valid = False if valid is True: return True elif valid is False: return False # Obtain molecular symmetry, exact_orientation==False symm_m = get_symmetry(mol, already_oriented=already_oriented) # Store OperationAnalyzer objects for each molecular SymmOp chiral = True opa_m = [] for op_m in symm_m: opa = OperationAnalyzer(op_m) opa_m.append(opa) if opa.type == "rotoinversion": chiral = False elif opa.type == "inversion": chiral = False # If molecule is chiral and allow_inversion is False, # check if WP breaks symmetry if chiral is True: if allow_inversion is False: for op in wyckoffs: if np.linalg.det(op.rotation_matrix) < 0: printx( "Warning: cannot place chiral molecule in spagegroup", priority=2, ) return False # Store OperationAnalyzer objects for each Wyckoff symmetry SymmOp opa_w = [] for op_w in symm_w: opa_w.append(OperationAnalyzer(op_w)) # Check for constraints from the Wyckoff symmetry... # If we find ANY two constraints (SymmOps with unique axes), the molecule's # point group MUST contain SymmOps which can be aligned to these particular # constraints. However, there may be multiple compatible orientations of the # molecule consistent with these constraints constraint1 = None constraint2 = None for i, op_w in enumerate(symm_w): if opa_w[i].axis is not None: constraint1 = opa_w[i] for j, op_w in enumerate(symm_w): if opa_w[j].axis is not None: dot = np.dot(opa_w[i].axis, opa_w[j].axis) if (not np.isclose(dot, 1, rtol=rtol)) and (not np.isclose( dot, -1, rtol=rtol)): constraint2 = opa_w[j] break break # Indirectly store the angle between the constraint axes if constraint1 is not None and constraint2 is not None: dot_w = np.dot(constraint1.axis, constraint2.axis) # Generate 1st consistent molecular constraints constraints_m = [] if constraint1 is not None: for i, opa1 in enumerate(opa_m): if opa1.is_conjugate(constraint1): constraints_m.append([opa1, []]) # Generate 2nd constraint in opposite direction extra = deepcopy(opa1) extra.axis = [ opa1.axis[0] * -1, opa1.axis[1] * -1, opa1.axis[2] * -1 ] constraints_m.append([extra, []]) # Remove redundancy for the first constraints list_i = list(range(len(constraints_m))) list_j = list(range(len(constraints_m))) copy = deepcopy(constraints_m) for i, c1 in enumerate(copy): if i in list_i: for j, c2 in enumerate(copy): if i > j and j in list_j and j in list_i: # Check if axes are colinear if np.isclose(np.dot(c1[0].axis, c2[0].axis), 1, rtol=rtol): list_i.remove(j) list_j.remove(j) # Check if axes are symmetrically equivalent else: cond1 = False # cond2 = False for opa in opa_m: if opa.type == "rotation": op = opa.op if np.isclose( np.dot(op.operate(c1[0].axis), c2[0].axis), 1, rtol=5 * rtol, ): cond1 = True break if cond1 is True: # or cond2 is True: list_i.remove(j) list_j.remove(j) c_m = deepcopy(constraints_m) constraints_m = [] for i in list_i: constraints_m.append(c_m[i]) # Generate 2nd consistent molecular constraints valid = list(range(len(constraints_m))) if constraint2 is not None: for i, c in enumerate(constraints_m): opa1 = c[0] for j, opa2 in enumerate(opa_m): if opa2.is_conjugate(constraint2): dot_m = np.dot(opa1.axis, opa2.axis) # Ensure that the angles are equal if abs(dot_m - dot_w) < 0.02 or abs(dot_m + dot_w) < 0.02: constraints_m[i][1].append(opa2) # Generate 2nd constraint in opposite direction extra = deepcopy(opa2) extra.axis = [ opa2.axis[0] * -1, opa2.axis[1] * -1, opa2.axis[2] * -1, ] constraints_m[i][1].append(extra) # If no consistent constraints are found, remove first constraint if constraints_m[i][1] == []: valid.remove(i) copy = deepcopy(constraints_m) constraints_m = [] for i in valid: constraints_m.append(copy[i]) # Generate orientations consistent with the possible constraints orientations = [] # Loop over molecular constraint sets for c1 in constraints_m: v1 = c1[0].axis v2 = constraint1.axis T = rotate_vector(v1, v2) # If there is only one constraint if c1[1] == []: o = Orientation(T, degrees=1, axis=constraint1.axis) orientations.append(o) else: # Loop over second molecular constraints for opa in c1[1]: phi = angle(constraint1.axis, constraint2.axis) phi2 = angle(constraint1.axis, np.dot(T, opa.axis)) if np.isclose(phi, phi2, rtol=rtol): r = np.sin(phi) c = np.linalg.norm(np.dot(T, opa.axis) - constraint2.axis) theta = np.arccos(1 - (c**2) / (2 * (r**2))) # R = aa2matrix(constraint1.axis, theta) R = Rotation.from_rotvec(theta * constraint1.axis).as_matrix() T2 = np.dot(R, T) a = angle(np.dot(T2, opa.axis), constraint2.axis) if not np.isclose(a, 0, rtol=rtol): T2 = np.dot(np.linalg.inv(R), T) o = Orientation(T2, degrees=0) orientations.append(o) # Ensure the identity orientation is checked if no constraints are found if constraints_m == []: o = Orientation(np.identity(3), degrees=2) orientations.append(o) # Remove redundancy from orientations list_i = list(range(len(orientations))) list_j = list(range(len(orientations))) for i, o1 in enumerate(orientations): if i in list_i: for j, o2 in enumerate(orientations): if i > j and j in list_j and j in list_i: # m1 = o1.get_matrix(angle=0) # m2 = o2.get_matrix(angle=0) m1 = o1.matrix m2 = o2.matrix new_op = SymmOp.from_rotation_and_translation( np.dot(m2, np.linalg.inv(m1)), [0, 0, 0]) P = SymmOp.from_rotation_and_translation( np.linalg.inv(m1), [0, 0, 0]) old_op = P * new_op * P.inverse if pga.is_valid_op(old_op): list_i.remove(j) list_j.remove(j) #copies = deepcopy(orientations) orientations_new = [] for i in list_i: orientations_new.append(orientations[i]) #Check each of the found orientations for consistency with the Wyckoff pos. #If consistent, put into an array of valid orientations allowed = [] for o in orientations_new: if randomize is True: op = o.get_op() elif randomize is False: op = o.get_op() #do not change mo = deepcopy(mol) mo.apply_operation(op) if orientation_in_wyckoff_position(mo, wyckoff_position, exact_orientation=True, randomize=False, allow_inversion=allow_inversion): allowed.append(o) if allowed == []: return False else: return allowed
def test_modules(): print("====== Testing functionality for pyXtal version 0.1dev ======") global failed_package failed_package = False # Record if errors occur at any level reset() print("Importing sys...") try: import sys print("Success!") except Exception as e: fail(e) sys.exit(0) print("Importing numpy...") try: import numpy as np print("Success!") except Exception as e: fail(e) sys.exit(0) I = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) print("Importing pymatgen...") try: import pymatgen print("Success!") except Exception as e: fail(e) sys.exit(0) try: from pymatgen.core.operations import SymmOp except Exception as e: fail(e) sys.exit(0) print("Importing pandas...") try: import pandas print("Success!") except Exception as e: fail(e) sys.exit(0) print("Importing spglib...") try: import spglib print("Success!") except Exception as e: fail(e) sys.exit(0) print("Importing openbabel...") try: import ase print("Success!") except: print( "Error: could not import openbabel. Try reinstalling the package.") print("Importing pyxtal...") try: import pyxtal print("Success!") except Exception as e: fail(e) sys.exit(0) print("=== Testing modules ===") # =====database.element===== print("pyxtal.database.element") reset() try: import pyxtal.database.element except Exception as e: fail(e) print(" class Element") try: from pyxtal.database.element import Element except Exception as e: fail(e) if passed(): for i in range(1, 95): if passed(): try: ele = Element(i) except: fail("Could not access Element # " + str(i)) try: y = ele.sf y = ele.z y = ele.short_name y = ele.long_name y = ele.valence y = ele.valence_electrons y = ele.covalent_radius y = ele.vdw_radius y = ele.get_all(0) except: fail("Could not access attribute for element # " + str(i)) try: ele.all_z() ele.all_short_names() ele.all_long_names() ele.all_valences() ele.all_valence_electrons() ele.all_covalent_radii() ele.all_vdw_radii() except: fail("Could not access class methods") check() # =====database.hall===== print("pyxtal.database.hall") reset() try: import pyxtal.database.hall except Exception as e: fail(e) print(" hall_from_hm") try: from pyxtal.database.hall import hall_from_hm except Exception as e: fail(e) if passed(): for i in range(1, 230): if passed(): try: hall_from_hm(i) except: fail("Could not access hm # " + str(i)) check() # =====database.collection===== print("pyxtal.database.collection") reset() try: import pyxtal.database.collection except Exception as e: fail(e) print(" Collection") try: from pyxtal.database.collection import Collection except Exception as e: fail(e) if passed(): for i in range(1, 230): if passed(): try: molecule_collection = Collection("molecules") except: fail("Could not access hm # " + str(i)) check() # =====operations===== print("pyxtal.operations") reset() try: import pyxtal.operations except Exception as e: fail(e) print(" random_vector") try: from pyxtal.operations import random_vector except Exception as e: fail(e) if passed(): try: for i in range(10): random_vector() except Exception as e: fail(e) check() print(" angle") try: from pyxtal.operations import angle except Exception as e: fail(e) if passed(): try: for i in range(10): v1 = random_vector() v2 = random_vector() angle(v1, v2) except Exception as e: fail(e) check() print(" random_shear_matrix") try: from pyxtal.operations import random_shear_matrix except Exception as e: fail(e) if passed(): try: for i in range(10): random_shear_matrix() except Exception as e: fail(e) check() print(" is_orthogonal") try: from pyxtal.operations import is_orthogonal except Exception as e: fail(e) if passed(): try: a = is_orthogonal([[1, 0, 0], [0, 1, 0], [0, 0, 1]]) b = is_orthogonal([[0, 0, 1], [1, 0, 0], [1, 0, 0]]) if a is True and b is False: pass else: fail() except Exception as e: fail(e) check() print(" aa2matrix") try: from pyxtal.operations import aa2matrix except Exception as e: fail(e) if passed(): try: for i in range(10): aa2matrix(1, 1, random=True) except Exception as e: fail(e) check() print(" matrix2aa") try: from pyxtal.operations import matrix2aa except Exception as e: fail(e) if passed(): try: for i in range(10): m = aa2matrix(1, 1, random=True) aa = matrix2aa(m) except Exception as e: fail(e) check() print(" rotate_vector") try: from pyxtal.operations import rotate_vector except Exception as e: fail(e) if passed(): try: for i in range(10): v1 = random_vector() v2 = random_vector() rotate_vector(v1, v2) except Exception as e: fail(e) check() print(" are_equal") try: from pyxtal.operations import are_equal except Exception as e: fail(e) if passed(): try: op1 = SymmOp.from_xyz_string("x,y,z") op2 = SymmOp.from_xyz_string("x,y,z+1") a = are_equal(op1, op2, PBC=[0, 0, 1]) b = are_equal(op1, op2, PBC=[1, 0, 0]) if a is True and b is False: pass else: fail() except Exception as e: fail(e) check() print(" class OperationAnalyzer") try: from pyxtal.operations import OperationAnalyzer except Exception as e: fail(e) if passed(): try: for i in range(10): m = aa2matrix(1, 1, random=True) t = random_vector() op1 = SymmOp.from_rotation_and_translation(m, t) OperationAnalyzer(op1) except Exception as e: fail(e) check() print(" class Orientation") try: from pyxtal.operations import Orientation except Exception as e: fail(e) if passed(): try: for i in range(10): v1 = random_vector() c1 = random_vector() o = Orientation.from_constraint(v1, c1) except Exception as e: fail(e) check() # =====symmetry===== print("pyxtal.symmetry") reset() try: import pyxtal.symmetry except Exception as e: fail(e) print(" get_wyckoffs (may take a moment)") try: from pyxtal.symmetry import get_wyckoffs except Exception as e: fail(e) if passed(): try: for i in [1, 2, 229, 230]: get_wyckoffs(i) get_wyckoffs(i, organized=True) except: fail(" Could not access Wyckoff positions for space group # " + str(i)) check() print(" get_wyckoff_symmetry (may take a moment)") try: from pyxtal.symmetry import get_wyckoff_symmetry except Exception as e: fail(e) if passed(): try: for i in [1, 2, 229, 230]: get_wyckoff_symmetry(i) get_wyckoff_symmetry(i, molecular=True) except: fail("Could not access Wyckoff symmetry for space group # " + str(i)) check() print(" get_wyckoffs_generators (may take a moment)") try: from pyxtal.symmetry import get_wyckoff_generators except Exception as e: fail(e) if passed(): try: for i in [1, 2, 229, 230]: get_wyckoff_generators(i) except: fail("Could not access Wyckoff generators for space group # " + str(i)) check() print(" letter_from_index") try: from pyxtal.symmetry import letter_from_index except Exception as e: fail(e) if passed(): try: if letter_from_index(0, get_wyckoffs(47)) == "A": pass else: fail() except Exception as e: fail(e) check() print(" index_from_letter") try: from pyxtal.symmetry import index_from_letter except Exception as e: fail(e) if passed(): try: if index_from_letter("A", get_wyckoffs(47)) == 0: pass else: fail() except Exception as e: fail(e) check() print(" jk_from_i") try: from pyxtal.symmetry import jk_from_i except Exception as e: fail(e) if passed(): try: w = get_wyckoffs(2, organized=True) j, k = jk_from_i(1, w) if j == 1 and k == 0: pass else: print(j, k) fail() except Exception as e: fail(e) check() print(" i_from_jk") try: from pyxtal.symmetry import i_from_jk except Exception as e: fail(e) if passed(): try: w = get_wyckoffs(2, organized=True) j, k = jk_from_i(1, w) i = i_from_jk(j, k, w) if i == 1: pass else: print(j, k) fail() except Exception as e: fail(e) check() print(" ss_string_from_ops") try: from pyxtal.symmetry import ss_string_from_ops except Exception as e: fail(e) if passed(): try: strings = ["1", "4 . .", "2 3 ."] for i, sg in enumerate([1, 75, 195]): ops = get_wyckoffs(sg)[0] ss_string_from_ops(ops, sg, dim=3) except Exception as e: fail(e) check() print(" Wyckoff_position") try: from pyxtal.symmetry import Wyckoff_position except Exception as e: fail(e) if passed(): try: wp = Wyckoff_position.from_group_and_index(20, 1) except Exception as e: fail(e) check() print(" Group") try: from pyxtal.symmetry import Group except Exception as e: fail(e) if passed(): try: g3 = Group(230) g2 = Group(80, dim=2) g1 = Group(75, dim=1) except Exception as e: fail(e) check() # =====crystal===== print("pyxtal.crystal") reset() try: import pyxtal.crystal except Exception as e: fail(e) print(" random_crystal") try: from pyxtal.crystal import random_crystal except Exception as e: fail(e) if passed(): try: c = random_crystal(1, ["H"], [1], 10.0) if c.valid is True: pass else: fail() except Exception as e: fail(e) check() print(" random_crystal_2D") try: from pyxtal.crystal import random_crystal_2D except Exception as e: fail(e) if passed(): try: c = random_crystal_2D(1, ["H"], [1], 10.0) if c.valid is True: pass else: fail() except Exception as e: fail(e) check() # =====molecule===== print("pyxtal.molecule") reset() try: import pyxtal.molecule except Exception as e: fail(e) check() print(" Collections") try: from pyxtal.molecule import mol_from_collection except Exception as e: fail(e) if passed(): try: h2o = mol_from_collection("H2O") ch4 = mol_from_collection("CH4") except Exception as e: fail(e) print(" get_inertia_tensor") try: from pyxtal.molecule import get_inertia_tensor except Exception as e: fail(e) if passed(): try: get_inertia_tensor(h2o) get_inertia_tensor(ch4) except Exception as e: fail(e) check() print(" get_moment_of_inertia") try: from pyxtal.molecule import get_moment_of_inertia except Exception as e: fail(e) if passed(): try: v = random_vector() get_moment_of_inertia(h2o, v) get_moment_of_inertia(ch4, v) except Exception as e: fail(e) check() print(" reoriented_molecule") try: from pyxtal.molecule import reoriented_molecule except Exception as e: fail(e) if passed(): try: reoriented_molecule(h2o) reoriented_molecule(ch4) except Exception as e: fail(e) check() print(" orientation_in_wyckoff_position") try: from pyxtal.molecule import orientation_in_wyckoff_position except Exception as e: fail(e) if passed(): try: w = get_wyckoffs(20) ws = get_wyckoff_symmetry(20, molecular=True) wp = Wyckoff_position.from_group_and_index(20, 1) orientation_in_wyckoff_position(h2o, wp) orientation_in_wyckoff_position(ch4, wp) except Exception as e: fail(e) check() # =====molecular_crystal===== print("pyxtal.molecular_crystal") reset() try: import pyxtal.crystal except Exception as e: fail(e) print(" molecular_crystal") try: from pyxtal.molecular_crystal import molecular_crystal except Exception as e: fail(e) if passed(): try: c = molecular_crystal(1, ["H2O"], [1], 10.0) if c.valid is True: pass else: fail() except Exception as e: fail(e) check() print(" molecular_crystal_2D") try: from pyxtal.molecular_crystal import molecular_crystal_2D except Exception as e: fail(e) if passed(): try: c = molecular_crystal_2D(1, ["H2O"], [1], 10.0) if c.valid is True: pass else: fail() except Exception as e: fail(e) check() end(condition=2)