def test_trivtx_has_bad_shape(self): trivtx = STEP.trivtx.copy() NT = trivtx.shape[0] trivtx.shape = (3, NT) msg = 'trivtx must be an array of shape \(NT, 3\), but got: \(3, 6\)' with self.assertRaisesRegex(ValueError, msg): ga.Triangulation2D(STEP.x, STEP.y, trivtx)
def test_normal(self): def linear_function(x, y): """Use a linear function, so linear interpolation will be exact""" return -4 * np.asarray(x) + 7 * np.asarray(y) - 16 # Create triangulation and a locator. TG = ga.Triangulation2D(HOLE.x, HOLE.y, HOLE.trivtx) locator = ga.TriangulationLocator(TG) # Create interpolator to triangle centers. interpolator = ga.TriangulationInterpolator(TG, locator, TG.NT) xcenter, ycenter = ga.triangulation.compute_centers(TG) nout = interpolator.set_points(xcenter, ycenter) self.assertEqual(nout, 0) # Compute data on mesh vertices, and expected data on triangle # centers. vertdata = linear_function(TG.x, TG.y) expected_pointdata = linear_function(xcenter, ycenter) # Interpolate data. pointdata = np.empty(TG.NT, dtype='d') interpolator.interpolate(vertdata, pointdata) # Check interpolated data. self.assertEqual(nout, 0) np.testing.assert_allclose(pointdata, expected_pointdata)
def test_x_y_have_different_length(self): x = np.array(STEP.x.tolist() + [ 0, ]) msg = 'Vector x and y must have the same length, but got 9 and 8' with self.assertRaisesRegex(ValueError, msg): ga.Triangulation2D(x, STEP.y, STEP.trivtx)
def test_compute_bounding_box(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) bb = ga.triangulation.compute_bounding_box(TG) self.assertEqual(bb.xmin, 0) self.assertEqual(bb.xmax, 2.5) self.assertEqual(bb.ymin, 10) self.assertEqual(bb.ymax, 12)
def test_centers(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) xcenter, ycenter = ga.triangulation.compute_centers(TG) a = 1 / 3 b = 2 / 3 assert_allclose(xcenter, [a, 1.5, b, 2, a, b]) assert_allclose(ycenter, [10 + a, 10 + a, 10 + b, 10 + b, 11 + a, 11 + b])
def test_signed_area(self): # make some triangle clockwise to have negative signed area trivtx = STEP.trivtx.copy() trivtx[0, 0], trivtx[0, 1] = trivtx[0, 1], trivtx[0, 0] trivtx[1, 1], trivtx[1, 2] = trivtx[1, 2], trivtx[1, 1] TG = ga.Triangulation2D(STEP.x, STEP.y, trivtx) signed_area = ga.triangulation.compute_signed_area(TG) assert_allclose(signed_area, [-0.5, -0.75, 0.5, 0.75, 0.5, 0.5])
def test_four_cells(self): """ 25 +-----+-----+-----+-----+ | | | | | | | B | | | | | /|\| | | |8 | / | \ | 11| 20 +-----+/--|-+\----+-----+ | / | | \ | | | /| 1|0| \ | | | C-----E-----A | | Triangles Center |4 \| 2|3| / | 7| 0 ABE (12, 19) 15 +-----\---|-+-/---+-----+ 1 BCE ( 8, 19) | |\ | |/ | | 2 CDE ( 8, 15) | | \ | / | | 3 DAE (12, 15) | | \|/| | | |0 | D | | 3| 10 +-----+-----+-----+-----+ 0 6 12 18 24 """ A = ga.Point2D(16, 17, 0) B = ga.Point2D(10, 23, 1) C = ga.Point2D(4, 17, 2) D = ga.Point2D(10, 11, 3) E = ga.Point2D(10, 17, 4) points = [A, B, C, D, E] x = np.asarray([P.x for P in points]) y = np.asarray([P.y for P in points]) triangles = [(A, B, E), (B, C, E), (C, D, E), (D, A, E)] trivtx = np.asarray([[P.index for P in vertices] for vertices in triangles], dtype='int32') TG = ga.Triangulation2D(x, y, trivtx) grid = ga.Grid2D(0, 24, 4, 10, 25, 3) bounds = np.zeros((4, 4), dtype='int32') ga.build_triangle_to_cell(bounds, TG, grid, 0.01) assert_equal(bounds, [[1, 3, 1, 3], [0, 2, 1, 3], [0, 2, 0, 2], [1, 3, 0, 2]])
def test_get(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) triangle = TG[3] self.assertEqual(triangle.index, 3) self.assertEqual(triangle.A.x, 2.5) self.assertEqual(triangle.A.y, 10) self.assertEqual(triangle.B.x, 2.5) self.assertEqual(triangle.B.y, 11) self.assertEqual(triangle.C.x, 1) self.assertEqual(triangle.C.y, 11) self.assertAlmostEqual(triangle.area, 0.75)
def test_to_matplotlib(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) tri = TG.to_matplotlib()
def test_to_numpy(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) x, y, trivtx = TG.to_numpy()
def test_compute_edge_min_max(self): TG = ga.Triangulation2D(STEP.x, STEP.y, STEP.trivtx) edge_min, edge_max = ga.triangulation.compute_edge_min_max(TG) self.assertAlmostEqual(edge_min, 1) self.assertAlmostEqual(edge_max, sqrt(3.25))