def __init__(self, initial_environment_symbol, expected_final_environment_symbol, morphing_description): self.initial_environment_symbol = initial_environment_symbol self.expected_final_environment_symbol = expected_final_environment_symbol self.morphing_description = morphing_description self.coordination_geometry = allcg.get_geometry_from_mp_symbol( initial_environment_symbol) self.abstract_geometry = AbstractGeometry.from_cg( self.coordination_geometry)
try: nperms = int(test) except Exception: raise ValueError('Could not turn {} into integer ...'.format(test)) perms_iterator = [] for ii in range(nperms): shuffle(myindices) perms_iterator.append(list(myindices)) iperm = 1 t1 = time.clock() for indices_perm in perms_iterator: lgf.setup_test_perfect_environment(cg_symbol, indices=indices_perm) lgf.perfect_geometry = AbstractGeometry.from_cg(cg=cg) points_perfect = lgf.perfect_geometry.points_wocs_ctwocc() print('Perm # {:d}/{:d} : '.format(iperm, nperms), indices_perm) algos_results = [] for algo in cg.algorithms: print(algo) if algo.algorithm_type == 'EXPLICIT_PERMUTATIONS': raise ValueError( 'Do something for the explicit ones ... (these should anyway be by far ok!)' ) results = lgf.coordination_geometry_symmetry_measures_separation_plane( coordination_geometry=cg, separation_plane_algo=algo,
def test_abstract_geometry(self): cg_ts3 = self.lgf.allcg["TS:3"] cg_tet = self.lgf.allcg["T:4"] abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type="central_site") self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.0]) abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type="centroid") self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.33333333333]) with self.assertRaises(ValueError) as cm: AbstractGeometry.from_cg( cg=cg_ts3, centering_type="central_site", include_central_site_in_centroid=True, ) self.assertEqual( str(cm.exception), "The center is the central site, no calculation of the centroid, " "variable include_central_site_in_centroid should be set to False", ) abstract_geom = AbstractGeometry.from_cg( cg=cg_ts3, centering_type="centroid", include_central_site_in_centroid=True) self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.25]) # WHY ARE WE TESTING STRINGS???? # self.assertEqual(abstract_geom.__str__(), # '\nAbstract Geometry with 3 points :\n' # ' [-1. 0. -0.25]\n' # ' [ 1. 0. -0.25]\n' # ' [ 0. 0. 0.75]\n' # 'Points are referenced to the centroid (calculated with the central site) :\n' # ' [ 0. 0. 0.25]\n') symm_dict = symmetry_measure([[0.0, 0.0, 0.0]], [1.1, 2.2, 3.3]) self.assertAlmostEqual(symm_dict["symmetry_measure"], 0.0) self.assertEqual(symm_dict["scaling_factor"], None) self.assertEqual(symm_dict["rotation_matrix"], None) tio2_struct = self.get_structure("TiO2") envs = self.lgf.compute_coordination_environments( structure=tio2_struct, indices=[0]) self.assertAlmostEqual(envs[0][0]["csm"], 1.5309987846957258) self.assertAlmostEqual(envs[0][0]["ce_fraction"], 1.0) self.assertEqual(envs[0][0]["ce_symbol"], "O:6") self.assertEqual(sorted(envs[0][0]["permutation"]), sorted([0, 4, 1, 5, 2, 3])) self.lgf.setup_random_structure(coordination=5) self.assertEqual(len(self.lgf.structure), 6) self.lgf.setup_random_indices_local_geometry(coordination=5) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(len(self.lgf.indices), 5) self.lgf.setup_ordered_indices_local_geometry(coordination=5) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(self.lgf.indices, list(range(1, 6))) self.lgf.setup_explicit_indices_local_geometry( explicit_indices=[3, 5, 2, 0, 1, 4]) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(self.lgf.indices, [4, 6, 3, 1, 2, 5]) LiFePO4_struct = self.get_structure("LiFePO4") isite = 10 envs_LiFePO4 = self.lgf.compute_coordination_environments( structure=LiFePO4_struct, indices=[isite]) self.assertAlmostEqual(envs_LiFePO4[isite][0]["csm"], 0.140355832317) nbs_coords = [ np.array([6.16700437, -4.55194317, -5.89031356]), np.array([4.71588167, -4.54248093, -3.75553856]), np.array([6.88012571, -5.79877503, -3.73177541]), np.array([6.90041188, -3.32797839, -3.71812416]), ] self.lgf.setup_structure(LiFePO4_struct) self.lgf.setup_local_geometry(isite, coords=nbs_coords) perfect_tet = AbstractGeometry.from_cg( cg=cg_tet, centering_type="centroid", include_central_site_in_centroid=False) points_perfect_tet = perfect_tet.points_wcs_ctwcc() res = self.lgf.coordination_geometry_symmetry_measures_fallback_random( coordination_geometry=cg_tet, NRANDOM=5, points_perfect=points_perfect_tet) ( permutations_symmetry_measures, permutations, algos, local2perfect_maps, perfect2local_maps, ) = res for perm_csm_dict in permutations_symmetry_measures: self.assertAlmostEqual(perm_csm_dict["symmetry_measure"], 0.140355832317)
def draw_cg(vis, site, neighbors, cg=None, perm=None, perfect2local_map=None, show_perfect=False, csm_info=None, symmetry_measure_type='csm_wcs_ctwcc', perfect_radius=0.1, show_distorted=True): if show_perfect: if csm_info is None: raise ValueError( 'Not possible to show perfect environment without csm_info') csm_suffix = symmetry_measure_type[4:] perf_radius = (perfect_radius - 0.2) / 0.002 if perm is not None and perfect2local_map is not None: raise ValueError( 'Only "perm" or "perfect2local_map" should be provided in draw_cg, not both' ) if show_distorted: vis.add_bonds(neighbors, site) for n in neighbors: vis.add_site(n) if len(neighbors) < 3: if show_distorted: vis.add_bonds(neighbors, site, color=[0.0, 1.0, 0.0], opacity=0.4, radius=0.175) if show_perfect: if len(neighbors) == 2: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info['other_symmetry_measures'][ 'translation_vector_{}'.format(csm_suffix)] rot = csm_info['other_symmetry_measures'][ 'rotation_matrix_{}'.format(csm_suffix)] scale = csm_info['other_symmetry_measures'][ 'scaling_factor_{}'.format(csm_suffix)] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale * pp + trans for pp in rotated_points] if 'wcs' in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input='coords') vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere(coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1) else: if show_distorted: if perm is not None: faces = cg.faces(neighbors, permutation=perm) edges = cg.edges(neighbors, permutation=perm) elif perfect2local_map is not None: faces = cg.faces(neighbors, perfect2local_map=perfect2local_map) edges = cg.edges(neighbors, perfect2local_map=perfect2local_map) else: faces = cg.faces(neighbors) edges = cg.edges(neighbors) symbol = list(site.species_and_occu.keys())[0].symbol mycolor = [float(i) / 255 for i in vis.el_color_mapping[symbol]] vis.add_faces(faces, mycolor, opacity=0.4) vis.add_edges(edges) if show_perfect: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info['other_symmetry_measures'][ 'translation_vector_{}'.format(csm_suffix)] rot = csm_info['other_symmetry_measures'][ 'rotation_matrix_{}'.format(csm_suffix)] scale = csm_info['other_symmetry_measures'][ 'scaling_factor_{}'.format(csm_suffix)] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale * pp + trans for pp in rotated_points] if 'wcs' in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input='coords') vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere(coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1)
if cg_symbol not in sepplane_cgs: print('Wrong geometry, try again ...') continue cg = allcg[cg_symbol] print('Getting explicit permutations for geometry "{}" (symbol : "{}")\n'.format(cg.name, cg_symbol)) # Setup of the local geometry finder lgf = LocalGeometryFinder() lgf.setup_parameters(structure_refinement=lgf.STRUCTURE_REFINEMENT_NONE) # Setup the random environment lgf.setup_test_perfect_environment(cg_symbol, randomness=True, indices=range(cg.coordination_number), max_random_dist=0.05) lgf.perfect_geometry = AbstractGeometry.from_cg(cg=cg) points_perfect = lgf.perfect_geometry.points_wocs_ctwocc() # 1. Check the algorithms defined for this coordination geometry and get the explicit permutations original_nexplicit_perms = [] original_nexplicit_optimized_perms = [] for ialgo, algo in enumerate(cg.algorithms): algo._permutations = algo.explicit_permutations if algo.algorithm_type == 'EXPLICIT_PERMUTATIONS': raise ValueError('Do something for the explicit ones ... (these should anyway be by far ok!)') if algo.explicit_optimized_permutations is None: eop = 'no' else: eop = str(len(algo.explicit_optimized_permutations)) print('For ialgo {:d}, plane_points are {}, ' 'side_0 is {} and '
def __init__(self, initial_environment_symbol, expected_final_environment_symbol, morphing_description): self.initial_environment_symbol = initial_environment_symbol self.expected_final_environment_symbol = expected_final_environment_symbol self.morphing_description = morphing_description self.coordination_geometry = allcg.get_geometry_from_mp_symbol(initial_environment_symbol) self.abstract_geometry = AbstractGeometry.from_cg(self.coordination_geometry)
def draw_cg(vis, site, neighbors, cg=None, perm=None, perfect2local_map=None, show_perfect=False, csm_info=None, symmetry_measure_type='csm_wcs_ctwcc', perfect_radius=0.1, show_distorted=True, faces_color_override=None): if show_perfect: if csm_info is None: raise ValueError('Not possible to show perfect environment without csm_info') csm_suffix = symmetry_measure_type[4:] perf_radius = (perfect_radius - 0.2) / 0.002 if perm is not None and perfect2local_map is not None: raise ValueError('Only "perm" or "perfect2local_map" should be provided in draw_cg, not both') if show_distorted: vis.add_bonds(neighbors, site) for n in neighbors: vis.add_site(n) if len(neighbors) < 3: if show_distorted: vis.add_bonds(neighbors, site, color=[0.0, 1.0, 0.0], opacity=0.4, radius=0.175) if show_perfect: if len(neighbors) == 2: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info['other_symmetry_measures']['translation_vector_{}'.format(csm_suffix)] rot = csm_info['other_symmetry_measures']['rotation_matrix_{}'.format(csm_suffix)] scale = csm_info['other_symmetry_measures']['scaling_factor_{}'.format(csm_suffix)] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale * pp + trans for pp in rotated_points] if 'wcs' in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input='coords') vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere(coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1) else: if show_distorted: if perm is not None: faces = cg.faces(neighbors, permutation=perm) edges = cg.edges(neighbors, permutation=perm) elif perfect2local_map is not None: faces = cg.faces(neighbors, perfect2local_map=perfect2local_map) edges = cg.edges(neighbors, perfect2local_map=perfect2local_map) else: faces = cg.faces(neighbors) edges = cg.edges(neighbors) symbol = list(site.species_and_occu.keys())[0].symbol if faces_color_override: mycolor = faces_color_override else: mycolor = [float(i) / 255 for i in vis.el_color_mapping[symbol]] vis.add_faces(faces, mycolor, opacity=0.4) vis.add_edges(edges) if show_perfect: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info['other_symmetry_measures']['translation_vector_{}'.format(csm_suffix)] rot = csm_info['other_symmetry_measures']['rotation_matrix_{}'.format(csm_suffix)] scale = csm_info['other_symmetry_measures']['scaling_factor_{}'.format(csm_suffix)] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale*pp + trans for pp in rotated_points] if 'wcs' in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input='coords') vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere(coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1)
def draw_cg( vis, site, neighbors, cg=None, perm=None, perfect2local_map=None, show_perfect=False, csm_info=None, symmetry_measure_type="csm_wcs_ctwcc", perfect_radius=0.1, show_distorted=True, faces_color_override=None, ): """ Draw cg. :param vis: :param site: :param neighbors: :param cg: :param perm: :param perfect2local_map: :param show_perfect: :param csm_info: :param symmetry_measure_type: :param perfect_radius: :param show_distorted: :param faces_color_override: :return: """ if show_perfect: if csm_info is None: raise ValueError("Not possible to show perfect environment without csm_info") csm_suffix = symmetry_measure_type[4:] perf_radius = (perfect_radius - 0.2) / 0.002 if perm is not None and perfect2local_map is not None: raise ValueError('Only "perm" or "perfect2local_map" should be provided in draw_cg, not both') if show_distorted: vis.add_bonds(neighbors, site) for n in neighbors: vis.add_site(n) if len(neighbors) < 3: if show_distorted: vis.add_bonds(neighbors, site, color=[0.0, 1.0, 0.0], opacity=0.4, radius=0.175) if show_perfect: if len(neighbors) == 2: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info["other_symmetry_measures"][f"translation_vector_{csm_suffix}"] rot = csm_info["other_symmetry_measures"][f"rotation_matrix_{csm_suffix}"] scale = csm_info["other_symmetry_measures"][f"scaling_factor_{csm_suffix}"] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale * pp + trans for pp in rotated_points] if "wcs" in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input="coords") vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere( coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1, ) else: if show_distorted: if perm is not None: faces = cg.faces(neighbors, permutation=perm) edges = cg.edges(neighbors, permutation=perm) elif perfect2local_map is not None: faces = cg.faces(neighbors, perfect2local_map=perfect2local_map) edges = cg.edges(neighbors, perfect2local_map=perfect2local_map) else: faces = cg.faces(neighbors) edges = cg.edges(neighbors) symbol = list(site.species.keys())[0].symbol if faces_color_override: mycolor = faces_color_override else: mycolor = [float(i) / 255 for i in vis.el_color_mapping[symbol]] vis.add_faces(faces, mycolor, opacity=0.4) vis.add_edges(edges) if show_perfect: perfect_geometry = AbstractGeometry.from_cg(cg) trans = csm_info["other_symmetry_measures"][f"translation_vector_{csm_suffix}"] rot = csm_info["other_symmetry_measures"][f"rotation_matrix_{csm_suffix}"] scale = csm_info["other_symmetry_measures"][f"scaling_factor_{csm_suffix}"] points = perfect_geometry.points_wcs_ctwcc() rotated_points = rotateCoords(points, rot) points = [scale * pp + trans for pp in rotated_points] if "wcs" in csm_suffix: ef_points = points[1:] else: ef_points = points edges = cg.edges(ef_points, input="coords") vis.add_edges(edges, color=[1.0, 0.0, 0.0]) for point in points: vis.add_partial_sphere( coords=point, radius=perf_radius, color=[0.0, 0.0, 0.0], start=0, end=360, opacity=1, )
def test_abstract_geometry(self): cg_ts3 = self.lgf.allcg['TS:3'] cg_tet = self.lgf.allcg['T:4'] abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type='central_site') self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.0]) abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type='centroid') self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.33333333333]) with self.assertRaises(ValueError) as cm: AbstractGeometry.from_cg(cg=cg_ts3, centering_type='central_site', include_central_site_in_centroid=True) self.assertEqual(str(cm.exception), 'The center is the central site, no calculation of the centroid, ' 'variable include_central_site_in_centroid should be set to False') abstract_geom = AbstractGeometry.from_cg(cg=cg_ts3, centering_type='centroid', include_central_site_in_centroid=True) self.assertArrayAlmostEqual(abstract_geom.centre, [0.0, 0.0, 0.25]) self.assertEqual(abstract_geom.__str__(), '\nAbstract Geometry with 3 points :\n' ' [-1. 0. -0.25]\n' ' [ 1. 0. -0.25]\n' ' [0. 0. 0.75]\n' 'Points are referenced to the centroid (calculated with the central site) :\n' ' [0. 0. 0.25]\n') symm_dict = symmetry_measure([[0.0, 0.0, 0.0]], [1.1, 2.2, 3.3]) self.assertAlmostEqual(symm_dict['symmetry_measure'], 0.0) self.assertEqual(symm_dict['scaling_factor'], None) self.assertEqual(symm_dict['rotation_matrix'], None) tio2_struct = self.get_structure('TiO2') envs = self.lgf.compute_coordination_environments(structure=tio2_struct, indices=[0]) self.assertAlmostEqual(envs[0][0]['csm'], 1.5309987846957258) self.assertAlmostEqual(envs[0][0]['ce_fraction'], 1.0) self.assertEqual(envs[0][0]['ce_symbol'], 'O:6') self.assertEqual(sorted(envs[0][0]['permutation']), sorted([0, 4, 1, 5, 2, 3])) self.lgf.setup_random_structure(coordination=5) self.assertEqual(len(self.lgf.structure), 6) self.lgf.setup_random_indices_local_geometry(coordination=5) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(len(self.lgf.indices), 5) self.lgf.setup_ordered_indices_local_geometry(coordination=5) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(self.lgf.indices, list(range(1, 6))) self.lgf.setup_explicit_indices_local_geometry(explicit_indices=[3, 5, 2, 0, 1, 4]) self.assertEqual(self.lgf.icentral_site, 0) self.assertEqual(self.lgf.indices, [4, 6, 3, 1, 2, 5]) LiFePO4_struct = self.get_structure('LiFePO4') isite = 10 envs_LiFePO4 = self.lgf.compute_coordination_environments(structure=LiFePO4_struct, indices=[isite]) self.assertAlmostEqual(envs_LiFePO4[isite][0]['csm'], 0.140355832317) nbs_coords = [np.array([6.16700437, -4.55194317, -5.89031356]), np.array([4.71588167, -4.54248093, -3.75553856]), np.array([6.88012571, -5.79877503, -3.73177541]), np.array([6.90041188, -3.32797839, -3.71812416])] self.lgf.setup_structure(LiFePO4_struct) self.lgf.setup_local_geometry(isite, coords=nbs_coords) perfect_tet = AbstractGeometry.from_cg(cg=cg_tet, centering_type='centroid', include_central_site_in_centroid=False) points_perfect_tet = perfect_tet.points_wcs_ctwcc() res = self.lgf.coordination_geometry_symmetry_measures_fallback_random(coordination_geometry=cg_tet, NRANDOM=5, points_perfect=points_perfect_tet) permutations_symmetry_measures, permutations, algos, local2perfect_maps, perfect2local_maps = res for perm_csm_dict in permutations_symmetry_measures: self.assertAlmostEqual(perm_csm_dict['symmetry_measure'], 0.140355832317)