def test_from_npoints_plane(self): best_fits = ['least_square_distance', 'maximum_distance'] delta = 0.0001 for best_fit in best_fits: plane = Plane.from_npoints([self.p1, self.p2, self.p3], best_fit=best_fit) self.assertTrue(self.plane.is_same_plane_as(plane)) points = [np.array([5.1, 0.3, -2.3]), np.array([-2.0, 4.3, -6.3]), np.array([3.1, 2.3, -21.3]), np.array([-2, -0.5, 0.05]), np.array([11, 12, -13]), np.array([10, 8.3, -6.32])] plane = Plane.from_npoints(points, best_fit=best_fit) fit_error_plane = plane.fit_error(points, fit=best_fit) coeffs = [[1.0+delta, 1, 1, 1], [1, 1.0+delta, 1, 1], [1, 1, 1.0+delta, 1], [1, 1, 1, 1.0+delta], [1.0-delta, 1.0+delta, 1.0-delta, 1.0+delta]] for coeff in coeffs: plane_changed = Plane.from_coefficients(coeff[0]*plane.a, coeff[1]*plane.b, coeff[2]*plane.c, coeff[3]*plane.d) fit_error = plane_changed.fit_error(points, fit=best_fit) self.assertGreater(fit_error, fit_error_plane) coeff = [-2.1, -2.1, -2.1, -2.1] plane_not_changed = Plane.from_coefficients(coeff[0]*plane.a, coeff[1]*plane.b, coeff[2]*plane.c, coeff[3]*plane.d) fit_error = plane_not_changed.fit_error(points, fit=best_fit) self.assertAlmostEqual(fit_error, fit_error_plane)
def test_from_npoints_plane(self): best_fits = ['least_square_distance', 'maximum_distance'] delta = 0.0001 for best_fit in best_fits: plane = Plane.from_npoints([self.p1, self.p2, self.p3], best_fit=best_fit) self.assertTrue(self.plane.is_same_plane_as(plane)) points = [ np.array([5.1, 0.3, -2.3]), np.array([-2.0, 4.3, -6.3]), np.array([3.1, 2.3, -21.3]), np.array([-2, -0.5, 0.05]), np.array([11, 12, -13]), np.array([10, 8.3, -6.32]) ] plane = Plane.from_npoints(points, best_fit=best_fit) fit_error_plane = plane.fit_error(points, fit=best_fit) coeffs = [[1.0 + delta, 1, 1, 1], [1, 1.0 + delta, 1, 1], [1, 1, 1.0 + delta, 1], [1, 1, 1, 1.0 + delta], [1.0 - delta, 1.0 + delta, 1.0 - delta, 1.0 + delta]] for coeff in coeffs: plane_changed = Plane.from_coefficients( coeff[0] * plane.a, coeff[1] * plane.b, coeff[2] * plane.c, coeff[3] * plane.d) fit_error = plane_changed.fit_error(points, fit=best_fit) self.assertGreater(fit_error, fit_error_plane) coeff = [-2.1, -2.1, -2.1, -2.1] plane_not_changed = Plane.from_coefficients( coeff[0] * plane.a, coeff[1] * plane.b, coeff[2] * plane.c, coeff[3] * plane.d) fit_error = plane_not_changed.fit_error(points, fit=best_fit) self.assertAlmostEqual(fit_error, fit_error_plane)
def test_distances(self): # Test with the common test plane point_1 = np.array([0.0, 0.0, 0.0], np.float) point_2 = np.array([0.0, 0.0, 0.75], np.float) point_3 = np.array([-0.75, 0.0, 0.0], np.float) point_4 = np.array([1.0, 0.0, 0.0], np.float) point_5 = np.array([0.0, -1.5, 0.0], np.float) point_6 = np.array([10.0, 2.0, -20.0], np.float) point_7 = np.array([10.0, 10.0, 10.0], np.float) point_8 = np.array([100.0, 0.0, 0.0], np.float) plist = [point_1, point_2, point_4, point_6, point_7, point_8] distances, indices_sorted = self.plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, 0.0, 1.16666666666666, 21.1666666666666, 3.8333333333333, 67.1666666666666]) self.assertArrayEqual(indices_sorted, [1, 0, 2, 4, 3, 5]) # Plane 2y+1=0 (perpendicular to y) plane = Plane.from_coefficients(0, 2, 0, 1) plist = [point_1, point_5, point_6, point_7] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [0, 1, 2, 3]) plist = [point_1, point_5, point_6, point_7] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [0, 1, 2, 3]) distances, indices_sorted = plane.distances_indices_sorted(plist, sign=True) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [(0, 1), (1, -1), (2, 1), (3, 1)]) plist = [point_5, point_1, point_6, point_7] distances, indices_sorted, groups = plane.distances_indices_groups(plist) self.assertArrayAlmostEqual(distances, [-1.0, 0.5, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [1, 0, 2, 3]) self.assertArrayEqual(groups, [[1, 0], [2], [3]]) plist = [point_1, point_2, point_3, point_4, point_5, point_6, point_7, point_8] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, 0.5, 0.5, 0.5, -1.0, 2.5, 10.5, 0.5]) self.assertEqual(set(indices_sorted[:5]), {0, 1, 2, 3, 7}) # Plane z=0 (plane xy) plane = Plane.from_coefficients(0, 0, 1, 0) zzs = [0.1, -0.2, 0.7, -2.1, -1.85, 0.0, -0.71, -0.82, -6.5, 1.8] plist = [] for zz in zzs: plist.append([random.uniform(-20.0, 20.0), random.uniform(-20.0, 20.0), zz]) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta=0.25) self.assertArrayEqual(indices_sorted, [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]) self.assertArrayEqual(groups, [[5, 0, 1], [2, 6, 7], [9, 4, 3], [8]]) self.assertArrayAlmostEqual(distances, zzs) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1) self.assertArrayEqual(indices_sorted, [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]) self.assertArrayEqual(groups, [[5, 0, 1, 2, 6, 7], [9, 4, 3], [8]]) self.assertArrayAlmostEqual(distances, zzs) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1, sign=True) self.assertArrayEqual(indices_sorted, [(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1), (9, 1), (4, -1), (3, -1), (8, -1)]) self.assertArrayEqual(groups, [[(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1)], [(9, 1), (4, -1), (3, -1)], [(8, -1)]]) self.assertArrayAlmostEqual(distances, zzs)
def test_distances(self): # Test with the common test plane point_1 = np.array([0.0, 0.0, 0.0], np.float) point_2 = np.array([0.0, 0.0, 0.75], np.float) point_3 = np.array([-0.75, 0.0, 0.0], np.float) point_4 = np.array([1.0, 0.0, 0.0], np.float) point_5 = np.array([0.0, -1.5, 0.0], np.float) point_6 = np.array([10.0, 2.0, -20.0], np.float) point_7 = np.array([10.0, 10.0, 10.0], np.float) point_8 = np.array([100.0, 0.0, 0.0], np.float) plist = [point_1, point_2, point_4, point_6, point_7, point_8] distances, indices_sorted = self.plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, 0.0, 1.16666666666666, 21.1666666666666, 3.8333333333333, 67.1666666666666]) self.assertArrayEqual(indices_sorted, [1, 0, 2, 4, 3, 5]) # Plane 2y+1=0 (perpendicular to y) plane = Plane.from_coefficients(0, 2, 0, 1) plist = [point_1, point_5, point_6, point_7] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [0, 1, 2, 3]) plist = [point_1, point_5, point_6, point_7] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [0, 1, 2, 3]) distances, indices_sorted = plane.distances_indices_sorted(plist, sign=True) self.assertArrayAlmostEqual(distances, [0.5, -1.0, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [(0, 1), (1, -1), (2, 1), (3, 1)]) plist = [point_5, point_1, point_6, point_7] distances, indices_sorted, groups = plane.distances_indices_groups(plist) self.assertArrayAlmostEqual(distances, [-1.0, 0.5, 2.5, 10.5]) self.assertArrayEqual(indices_sorted, [1, 0, 2, 3]) self.assertArrayEqual(groups, [[1, 0], [2], [3]]) plist = [point_1, point_2, point_3, point_4, point_5, point_6, point_7, point_8] distances, indices_sorted = plane.distances_indices_sorted(plist) self.assertArrayAlmostEqual(distances, [0.5, 0.5, 0.5, 0.5, -1.0, 2.5, 10.5, 0.5]) self.assertEqual(set(indices_sorted[:5]), {0, 1, 2, 3, 7}) # Plane z=0 (plane xy) plane = Plane.from_coefficients(0, 0, 1, 0) zzs = [0.1, -0.2, 0.7, -2.1, -1.85, 0.0, -0.71, -0.82, -6.5, 1.8] plist = [] for zz in zzs: plist.append([random.uniform(-20.0, 20.0), random.uniform(-20.0, 20.0), zz]) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta=0.25) self.assertArrayEqual(indices_sorted, [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]) self.assertArrayEqual(groups, [[5, 0, 1], [2, 6, 7], [9, 4, 3], [8]]) self.assertArrayAlmostEqual(distances, zzs) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1) self.assertArrayEqual(indices_sorted, [5, 0, 1, 2, 6, 7, 9, 4, 3, 8]) self.assertArrayEqual(groups, [[5, 0, 1, 2, 6, 7], [9, 4, 3], [8]]) self.assertArrayAlmostEqual(distances, zzs) distances, indices_sorted, groups = plane.distances_indices_groups(points=plist, delta_factor=0.1, sign=True) self.assertArrayEqual(indices_sorted, [(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1), (9, 1), (4, -1), (3, -1), (8, -1)]) self.assertArrayEqual(groups, [[(5, 0), (0, 1), (1, -1), (2, 1), (6, -1), (7, -1)], [(9, 1), (4, -1), (3, -1)], [(8, -1)]]) self.assertArrayAlmostEqual(distances, zzs)
def test_plane_comparison(self): plane_test_1 = Plane.from_coefficients(4, 2, -4, 3) self.assertTrue(self.plane.is_same_plane_as(plane_test_1)) plane_test_2 = Plane.from_coefficients(-4, -2, 4, -3) self.assertTrue(self.plane.is_same_plane_as(plane_test_2)) plane_test_3 = Plane.from_coefficients(-12, -6, 12, -9) self.assertTrue(self.plane.is_same_plane_as(plane_test_3)) plane_test_4 = Plane.from_coefficients(3, 0, 2, 4) self.assertFalse(self.plane.is_same_plane_as(plane_test_4))
def test_plane_is_in_list_of_planes(self): plane_test_1 = Plane.from_coefficients(-8.1, 2, -4, 3) plane_test_2 = Plane.from_coefficients(0, -2, 4, 0) plane_test_3 = Plane.from_coefficients(-12, -6, 12, -9) plane_test_4 = Plane.from_coefficients(3, 0, 0, 4) plane_list = [plane_test_1, plane_test_2, plane_test_3, plane_test_4] self.assertTrue(self.plane.is_in_list(plane_list)) plane_list = [plane_test_1, plane_test_2, plane_test_4] self.assertFalse(self.plane.is_in_list(plane_list))
def test_plane_is_in_list_of_planes(self): plane_test_1 = Plane.from_coefficients(-8.1, 2, -4, 3) plane_test_2 = Plane.from_coefficients(0, -2, 4, 0) plane_test_3 = Plane.from_coefficients(-12, -6, 12, -9) plane_test_4 = Plane.from_coefficients(3, 0, 0, 4) plane_list = [plane_test_1, plane_test_2, plane_test_3, plane_test_4] self.assertTrue(self.plane.is_in_list(plane_list)) plane_list = [plane_test_1, plane_test_2, plane_test_4] self.assertFalse(self.plane.is_in_list(plane_list))
def test_plane_comparison(self): plane_test_1 = Plane.from_coefficients(4, 2, -4, 3) self.assertTrue(self.plane.is_same_plane_as(plane_test_1)) plane_test_2 = Plane.from_coefficients(-4, -2, 4, -3) self.assertTrue(self.plane.is_same_plane_as(plane_test_2)) plane_test_3 = Plane.from_coefficients(-12, -6, 12, -9) self.assertTrue(self.plane.is_same_plane_as(plane_test_3)) plane_test_4 = Plane.from_coefficients(3, 0, 2, 4) self.assertFalse(self.plane.is_same_plane_as(plane_test_4))
def test_plane_2_coefficients(self): plane_1 = Plane.from_coefficients(-21, 0, 0, 3) self.assertTrue(plane_1.is_in_plane(plane_1.p1, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p2, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p3, 0.000001)) plane_2 = Plane.from_coefficients(0, 4, 0, -4) self.assertTrue(plane_2.is_in_plane(plane_2.p1, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p2, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p3, 0.000001)) plane_3 = Plane.from_coefficients(0, 0, 3, 1) self.assertTrue(plane_3.is_in_plane(plane_3.p1, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p2, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p3, 0.000001))
def test_plane_3_coefficients(self): plane_1 = Plane.from_coefficients(0, 2, -1, 3) self.assertTrue(plane_1.is_in_plane(plane_1.p1, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p2, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p3, 0.000001)) plane_2 = Plane.from_coefficients(12, 0, 2, -4) self.assertTrue(plane_2.is_in_plane(plane_2.p1, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p2, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p3, 0.000001)) plane_3 = Plane.from_coefficients(-8, 8, 0, 0) self.assertTrue(plane_3.is_in_plane(plane_3.p1, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p2, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p3, 0.000001))
def test_plane_3_coefficients(self): plane_1 = Plane.from_coefficients(0, 2, -1, 3) self.assertTrue(plane_1.is_in_plane(plane_1.p1, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p2, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p3, 0.000001)) plane_2 = Plane.from_coefficients(12, 0, 2, -4) self.assertTrue(plane_2.is_in_plane(plane_2.p1, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p2, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p3, 0.000001)) plane_3 = Plane.from_coefficients(-8, 8, 0, 0) self.assertTrue(plane_3.is_in_plane(plane_3.p1, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p2, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p3, 0.000001))
def test_plane_2_coefficients(self): plane_1 = Plane.from_coefficients(-21, 0, 0, 3) self.assertTrue(plane_1.is_in_plane(plane_1.p1, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p2, 0.000001)) self.assertTrue(plane_1.is_in_plane(plane_1.p3, 0.000001)) plane_2 = Plane.from_coefficients(0, 4, 0, -4) self.assertTrue(plane_2.is_in_plane(plane_2.p1, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p2, 0.000001)) self.assertTrue(plane_2.is_in_plane(plane_2.p3, 0.000001)) plane_3 = Plane.from_coefficients(0, 0, 3, 1) self.assertTrue(plane_3.is_in_plane(plane_3.p1, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p2, 0.000001)) self.assertTrue(plane_3.is_in_plane(plane_3.p3, 0.000001))
def setUp(self): # Test of plane 4x + 2y - 4z + 3 = 0 (used in most test cases) self.expected_coefficients = np.array([4.0, 2.0, -4.0, 3.0], np.float_) self.p1 = np.array([0.0, 0.0, 0.75]) self.p2 = np.array([-0.75, 0.0, 0.0]) self.p3 = np.array([0.0, -1.5, 0.0]) self.plane = Plane.from_3points(self.p1, self.p2, self.p3)
def setUp(self): #Test of plane 4x + 2y - 4z + 3 = 0 (used in most test cases) self.expected_coefficients = np.array([4.0, 2.0, -4.0, 3.0], np.float) self.p1 = np.array([0.0, 0.0, 0.75]) self.p2 = np.array([-0.75, 0.0, 0.0]) self.p3 = np.array([0.0, -1.5, 0.0]) self.plane = Plane.from_3points(self.p1, self.p2, self.p3)
# Setting up the plane of separation local_plane = None found = False for npoints in range(sepplanealgo.minimum_number_of_points, min(sepplanealgo.maximum_number_of_points, 4) + 1): if found: break for ipoints in itertools.combinations(sepplanealgo.plane_points, npoints): points_combination = [lgf.local_geometry.coords[ipoint] for ipoint in ipoints] if npoints == 2: if collinear( points_combination[0], points_combination[1], lgf.local_geometry.central_site, tolerance=0.25 ): continue local_plane = Plane.from_3points( points_combination[0], points_combination[1], lgf.local_geometry.central_site ) found = True break elif npoints == 3: if collinear(points_combination[0], points_combination[1], points_combination[2], tolerance=0.25): continue local_plane = Plane.from_3points( points_combination[0], points_combination[1], points_combination[2] ) found = True break elif npoints > 3: local_plane = Plane.from_npoints(points_combination, best_fit="least_square_distance") found = True break
algo._permutations = permutations print('Safe permutations found ({:d})'.format(len(permutations))) # Definition of the planes all_planes_point_indices = [algo.plane_points] if algo.other_plane_points is not None: all_planes_point_indices.extend(algo.other_plane_points) # Loop on the planes explicit_permutations_per_plane = [] for iplane, plane_point_indices in enumerate(all_planes_point_indices): prt1(string='In plane {:d} ({})'.format(iplane, '-'.join(str(pp) for pp in plane_point_indices)), printing_volume=printing_volume) points_combination = [lgf.local_geometry._coords[ii] for ii in plane_point_indices] local_plane = Plane.from_npoints(points_combination, best_fit='least_square_distance') # Actual test of the permutations csms, perms, algos, sep_perms = lgf._cg_csm_separation_plane(coordination_geometry=cg, sepplane=algo, local_plane=local_plane, plane_separations=[], dist_tolerances=[0.05, 0.1, 0.2, 0.3], testing=True, points_perfect=points_perfect) prt1(string='Continuous symmetry measures', printing_volume=printing_volume) prt1(string=csms, printing_volume=printing_volume) csms_with_recorded_permutation = [] explicit_permutations = []
algo._permutations = permutations print('Safe permutations found ({:d})'.format(len(permutations))) # Definition of the facets all_planes_point_indices = [algo.plane_points] if algo.other_plane_points is not None: all_planes_point_indices.extend(algo.other_plane_points) # Loop on the facets explicit_permutations_per_plane = [] for iplane, plane_point_indices in enumerate(all_planes_point_indices): prt1(string='In plane {:d} ({})'.format(iplane, '-'.join(str(pp) for pp in plane_point_indices)), printing_volume=printing_volume) points_combination = [lgf.local_geometry._coords[ii] for ii in plane_point_indices] local_plane = Plane.from_npoints(points_combination, best_fit='least_square_distance') # Actual test of the permutations csms, perms, algos, sep_perms = lgf._cg_csm_separation_plane(coordination_geometry=cg, sepplane=algo, local_plane=local_plane, plane_separations=[], dist_tolerances=[0.05, 0.1, 0.2, 0.3, 0.5], testing=True, points_perfect=points_perfect) mycsms = [c['symmetry_measure'] for c in csms] prt1(string='Continuous symmetry measures', printing_volume=printing_volume) prt1(string=mycsms, printing_volume=printing_volume) csms_with_recorded_permutation = []
# testing=True) # Setting up the plane of separation local_plane = None found = False for npoints in range(sepplanealgo.minimum_number_of_points, min(sepplanealgo.maximum_number_of_points, 4)+1): if found: break for ipoints in itertools.combinations(sepplanealgo.plane_points, npoints): points_combination = [lgf.local_geometry.coords[ipoint] for ipoint in ipoints] if npoints == 2: if collinear(points_combination[0], points_combination[1], lgf.local_geometry.central_site, tolerance=0.25): continue local_plane = Plane.from_3points(points_combination[0], points_combination[1], lgf.local_geometry.central_site) found = True break elif npoints == 3: if collinear(points_combination[0], points_combination[1], points_combination[2], tolerance=0.25): continue local_plane = Plane.from_3points(points_combination[0], points_combination[1], points_combination[2]) found = True break elif npoints > 3: local_plane = Plane.from_npoints(points_combination, best_fit='least_square_distance') found = True break else: raise ValueError('Wrong number of points to initialize separation plane')
def test_indices_separate(self): # Test with the common test plane point_1 = np.array([0.0, 0.0, 0.0], np.float_) point_2 = np.array([0.0, 0.0, 0.75], np.float_) point_3 = np.array([-0.75, 0.0, 0.0], np.float_) point_4 = np.array([1.0, 0.0, 0.0], np.float_) point_5 = np.array([0.0, -1.5, 0.0], np.float_) point_6 = np.array([10.0, 2.0, -20.0], np.float_) point_7 = np.array([10.0, 10.0, 10.0], np.float_) point_8 = np.array([100.0, 0.0, 0.0], np.float_) plist = [ point_1, point_2, point_3, point_4, point_5, point_6, point_7, point_8 ] sep = self.plane.indices_separate(plist, 0.000001) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 3) self.assertEqual(len(sep[2]), 5) self.assertTrue(np.allclose(sep[1], [1, 2, 4])) self.assertTrue(np.allclose(sep[2], [0, 3, 5, 6, 7])) sep = self.plane.indices_separate(plist, 10) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 6) self.assertEqual(len(sep[2]), 2) self.assertTrue(np.allclose(sep[1], [0, 1, 2, 3, 4, 6])) self.assertTrue(np.allclose(sep[2], [5, 7])) sep = self.plane.indices_separate(plist, 100000) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 8) self.assertEqual(len(sep[2]), 0) # Test with 2 coeff facets (Normal vector = [1, 0, 0] or [0, 1, 0] or [0, 0, 1]) # Plane x-2=0 (perpendicular to x) plane = Plane.from_coefficients(-4, 0, 0, 8) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 3, 4]) self.assertEqual(sep[1], []) self.assertEqual(sep[2], [5, 6, 7]) sep = plane.indices_separate(plist, 1.0) self.assertEqual(sep[0], [0, 1, 2, 4]) self.assertEqual(sep[1], [3]) self.assertEqual(sep[2], [5, 6, 7]) # Plane 2y+1=0 (perpendicular to y) plane = Plane.from_coefficients(0, 2, 0, 1) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [4]) self.assertEqual(sep[1], []) self.assertEqual(sep[2], [0, 1, 2, 3, 5, 6, 7]) sep = plane.indices_separate(plist, 1.0) self.assertEqual(sep[0], []) self.assertEqual(sep[1], [0, 1, 2, 3, 4, 7]) self.assertEqual(sep[2], [5, 6]) # Plane 4z-3=0 (perpendicular to z) plane = Plane.from_coefficients(0, 0, -4, 3) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 2, 3, 4, 5, 7]) self.assertEqual(sep[1], [1]) self.assertEqual(sep[2], [6]) sep = plane.indices_separate(plist, 0.75) self.assertEqual(sep[0], [5]) self.assertEqual(sep[1], [0, 1, 2, 3, 4, 7]) self.assertEqual(sep[2], [6]) # Test with 3 coeff facets (Normal vector = [0, a, b] or [a, 0, b] or [a, b, 0]) # Plane 2y-z+4=0 plane = Plane.from_coefficients(0, 2, -1, 0) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [1, 4]) self.assertEqual(sep[1], [0, 2, 3, 7]) self.assertEqual(sep[2], [5, 6]) sep = plane.indices_separate(plist, 0.75) self.assertEqual(sep[0], [4]) self.assertEqual(sep[1], [0, 1, 2, 3, 7]) self.assertEqual(sep[2], [5, 6]) # Plane 2y-z+4=0 plane = Plane.from_coefficients(4, 0, -2, -20) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 3, 4]) self.assertEqual(sep[1], [6]) self.assertEqual(sep[2], [5, 7]) # Plane 2y-z+4=0 plane = Plane.from_coefficients(-2, 9, 0, 2) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 6]) self.assertEqual(sep[1], [3, 5]) self.assertEqual(sep[2], [4, 7])
def test_indices_separate(self): #Test with the common test plane point_1 = np.array([0.0, 0.0, 0.0], np.float) point_2 = np.array([0.0, 0.0, 0.75], np.float) point_3 = np.array([-0.75, 0.0, 0.0], np.float) point_4 = np.array([1.0, 0.0, 0.0], np.float) point_5 = np.array([0.0, -1.5, 0.0], np.float) point_6 = np.array([10.0, 2.0, -20.0], np.float) point_7 = np.array([10.0, 10.0, 10.0], np.float) point_8 = np.array([100.0, 0.0, 0.0], np.float) plist = [point_1, point_2, point_3, point_4, point_5, point_6, point_7, point_8] sep = self.plane.indices_separate(plist, 0.000001) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 3) self.assertEqual(len(sep[2]), 5) self.assertTrue(np.allclose(sep[1], [1, 2, 4])) self.assertTrue(np.allclose(sep[2], [0, 3, 5, 6, 7])) sep = self.plane.indices_separate(plist, 10) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 6) self.assertEqual(len(sep[2]), 2) self.assertTrue(np.allclose(sep[1], [0, 1, 2, 3, 4, 6])) self.assertTrue(np.allclose(sep[2], [5, 7])) sep = self.plane.indices_separate(plist, 100000) self.assertEqual(len(sep[0]), 0) self.assertEqual(len(sep[1]), 8) self.assertEqual(len(sep[2]), 0) #Test with 2 coeff facets (Normal vector = [1, 0, 0] or [0, 1, 0] or [0, 0, 1]) #Plane x-2=0 (perpendicular to x) plane = Plane.from_coefficients(-4, 0, 0, 8) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 3, 4]) self.assertEqual(sep[1], []) self.assertEqual(sep[2], [5, 6, 7]) sep = plane.indices_separate(plist, 1.0) self.assertEqual(sep[0], [0, 1, 2, 4]) self.assertEqual(sep[1], [3]) self.assertEqual(sep[2], [5, 6, 7]) #Plane 2y+1=0 (perpendicular to y) plane = Plane.from_coefficients(0, 2, 0, 1) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [4]) self.assertEqual(sep[1], []) self.assertEqual(sep[2], [0, 1, 2, 3, 5, 6, 7]) sep = plane.indices_separate(plist, 1.0) self.assertEqual(sep[0], []) self.assertEqual(sep[1], [0, 1, 2, 3, 4, 7]) self.assertEqual(sep[2], [5, 6]) #Plane 4z-3=0 (perpendicular to z) plane = Plane.from_coefficients(0, 0, -4, 3) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 2, 3, 4, 5, 7]) self.assertEqual(sep[1], [1]) self.assertEqual(sep[2], [6]) sep = plane.indices_separate(plist, 0.75) self.assertEqual(sep[0], [5]) self.assertEqual(sep[1], [0, 1, 2, 3, 4, 7]) self.assertEqual(sep[2], [6]) #Test with 3 coeff facets (Normal vector = [0, a, b] or [a, 0, b] or [a, b, 0]) #Plane 2y-z+4=0 plane = Plane.from_coefficients(0, 2, -1, 0) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [1, 4]) self.assertEqual(sep[1], [0, 2, 3, 7]) self.assertEqual(sep[2], [5, 6]) sep = plane.indices_separate(plist, 0.75) self.assertEqual(sep[0], [4]) self.assertEqual(sep[1], [0, 1, 2, 3, 7]) self.assertEqual(sep[2], [5, 6]) #Plane 2y-z+4=0 plane = Plane.from_coefficients(4, 0, -2, -20) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 3, 4]) self.assertEqual(sep[1], [6]) self.assertEqual(sep[2], [5, 7]) #Plane 2y-z+4=0 plane = Plane.from_coefficients(-2, 9, 0, 2) sep = plane.indices_separate(plist, 0.00001) self.assertEqual(sep[0], [0, 1, 2, 6]) self.assertEqual(sep[1], [3, 5]) self.assertEqual(sep[2], [4, 7])
cg_points = [myfactor * np.array(pp) for pp in cg.points] cg_central_site = myfactor * np.array(cg.central_site) if sepplane: pts = [cg_points[ii] for ii in algo.plane_points] if algo.minimum_number_of_points == 2: pts.append(cg_central_site) centre = cg_central_site else: centre = np.sum(pts, axis=0) / len(pts) factor = 1.5 target_dist = max( [np.dot(pp - centre, pp - centre) for pp in cg_points]) current_dist = np.dot(pts[0] - centre, pts[0] - centre) factor = factor * target_dist / current_dist plane = Plane.from_npoints(points=pts) p1 = centre + factor * (pts[0] - centre) perp = factor * np.cross(pts[0] - centre, plane.normal_vector) p2 = centre + perp p3 = centre - factor * (pts[0] - centre) p4 = centre - perp vis.add_faces([[p1, p2, p3, p4]], [1.0, 0.0, 0.0], opacity=0.5) target_radius = 0.25 radius = myfactor * ( target_radius - 0.2 ) / 0.002 # due to how the radius is obtained in add_partial_sphere # of pymatgen if algo.minimum_number_of_points == 2:
vis = visualize(cg=cg, vis=vis, myfactor=myfactor) cg_points = [myfactor*np.array(pp) for pp in cg.points] cg_central_site = myfactor*np.array(cg.central_site) if sepplane: pts = [cg_points[ii] for ii in algo.plane_points] if algo.minimum_number_of_points == 2: pts.append(cg_central_site) centre = cg_central_site else: centre = np.sum(pts, axis=0) / len(pts) factor = 1.5 target_dist = max([np.dot(pp-centre, pp-centre) for pp in cg_points]) current_dist = np.dot(pts[0] - centre, pts[0] - centre) factor = factor * target_dist / current_dist plane = Plane.from_npoints(points=pts) p1 = centre + factor * (pts[0] - centre) perp = factor * np.cross(pts[0] - centre, plane.normal_vector) p2 = centre + perp p3 = centre - factor * (pts[0] - centre) p4 = centre - perp vis.add_faces([[p1, p2, p3, p4]], [1.0, 0.0, 0.0], opacity=0.5) target_radius = 0.25 radius = 1.5 * target_radius if algo.minimum_number_of_points == 2: vis.add_partial_sphere(coords=cg_central_site, radius=radius, color=[1.0, 0.0, 0.0], start=0, end=360, opacity=0.5)