def _test_eigen_solver_sparse(callback_type): from rbnics.backends.dolfin import EigenSolver # Define mesh mesh = UnitSquareMesh(10, 10) # Define function space V_element = VectorElement("Lagrange", mesh.ufl_cell(), 2) Q_element = FiniteElement("Lagrange", mesh.ufl_cell(), 1) W_element = MixedElement(V_element, Q_element) W = FunctionSpace(mesh, W_element) # Create boundaries class Wall(SubDomain): def inside(self, x, on_boundary): return on_boundary and (x[1] < 0 + DOLFIN_EPS or x[1] > 1 - DOLFIN_EPS) boundaries = MeshFunction("size_t", mesh, mesh.topology().dim() - 1) boundaries.set_all(0) wall = Wall() wall.mark(boundaries, 1) # Define variational problem vq = TestFunction(W) (v, q) = split(vq) up = TrialFunction(W) (u, p) = split(up) lhs = inner(grad(u), grad(v)) * dx - div(v) * p * dx - div(u) * q * dx rhs = -inner(p, q) * dx # Define boundary condition bc = [DirichletBC(W.sub(0), Constant((0., 0.)), boundaries, 1)] # Define eigensolver depending on callback type assert callback_type in ("form callbacks", "tensor callbacks") if callback_type == "form callbacks": solver = EigenSolver(W, lhs, rhs, bc) elif callback_type == "tensor callbacks": LHS = assemble(lhs) RHS = assemble(rhs) solver = EigenSolver(W, LHS, RHS, bc) # Solve the eigenproblem solver.set_parameters({ "linear_solver": "mumps", "problem_type": "gen_non_hermitian", "spectrum": "target real", "spectral_transform": "shift-and-invert", "spectral_shift": 1.e-5 }) solver.solve(1) r, c = solver.get_eigenvalue(0) assert abs(c) < 1.e-10 assert r > 0., "r = " + str(r) + " is not positive" print("Sparse inf-sup constant: ", sqrt(r)) return (sqrt(r), solver.condensed_A, solver.condensed_B)
def mesh_generator(n): mesh = UnitSquareMesh(n, n, "left/right") dim = mesh.topology().dim() domains = MeshFunction("size_t", mesh, dim) domains.set_all(0) dx = Measure("dx", subdomain_data=domains) boundaries = MeshFunction("size_t", mesh, dim - 1) boundaries.set_all(0) ds = Measure("ds", subdomain_data=boundaries) return mesh, dx, ds
def test_compute_first_collision_2d(): # FIXME: This test should not use facet indices as there are no guarantees # on how DOLFIN numbers facets reference = {1: [226], 2: [136, 137]} p = Point(0.3, 0.3) mesh = UnitSquareMesh(16, 16) for dim in range(1, 3): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) # FIXME: Facet test is excluded because it mistakingly relies in the # facet indices if dim != mesh.topology().dim() - 1: assert first in reference[dim] tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) assert first in reference[mesh.topology().dim()]
def square_with_obstacle(): # Create classes for defining parts of the boundaries and the interior # of the domain class Left(SubDomain): def inside(self, x, on_boundary): return near(x[0], 0.0) class Right(SubDomain): def inside(self, x, on_boundary): return near(x[0], 1.0) class Bottom(SubDomain): def inside(self, x, on_boundary): return near(x[1], 0.0) class Top(SubDomain): def inside(self, x, on_boundary): return near(x[1], 1.0) class Obstacle(SubDomain): def inside(self, x, on_boundary): return between(x[1], (0.5, 0.7)) and between(x[0], (0.2, 1.0)) # Initialize sub-domain instances left = Left() top = Top() right = Right() bottom = Bottom() obstacle = Obstacle() # Define mesh mesh = UnitSquareMesh(100, 100, "crossed") # Initialize mesh function for interior domains domains = CellFunction("size_t", mesh) domains.set_all(0) obstacle.mark(domains, 1) # Initialize mesh function for boundary domains boundaries = MeshFunction("size_t", mesh, mesh.topology().dim() - 1) boundaries.set_all(0) left.mark(boundaries, 1) top.mark(boundaries, 2) right.mark(boundaries, 3) bottom.mark(boundaries, 4) boundary_indices = {"left": 1, "top": 2, "right": 3, "bottom": 4} f = Constant(0.0) theta0 = Constant(293.0) return mesh, f, boundaries, boundary_indices, theta0
def test_compute_first_collision_2d(): reference = {1: [226], 2: [136, 137]} p = Point(0.3, 0.3) mesh = UnitSquareMesh(16, 16) for dim in range(1, 3): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) assert first in reference[dim] tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) assert first in reference[mesh.topology().dim()]
def test_compute_first_collision_2d(self): reference = {1: [226], 2: [136, 137]} p = Point(0.3, 0.3) mesh = UnitSquareMesh(16, 16) for dim in range(1, 3): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) if MPI.num_processes() == 1: self.assertIn(first, reference[dim]) tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) if MPI.num_processes() == 1: self.assertIn(first, reference[mesh.topology().dim()])
def test_compute_first_collision_2d(self): reference = {1: [226], 2: [136, 137]} p = Point(0.3, 0.3) mesh = UnitSquareMesh(16, 16) for dim in range(1, 3): tree = BoundingBoxTree() tree.build(mesh, dim) first = tree.compute_first_collision(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertIn(first, reference[dim]) tree = mesh.bounding_box_tree() first = tree.compute_first_collision(p) if MPI.size(mesh.mpi_comm()) == 1: self.assertIn(first, reference[mesh.topology().dim()])
def test_compute_collisions_2d(self): reference = {1: [226], 2: [136, 137]} p = Point(0.3, 0.3) mesh = UnitSquareMesh(16, 16) for dim in range(1, 3): tree = BoundingBoxTree() tree.build(mesh, dim) entities = tree.compute_collisions(p) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference[dim]) tree = mesh.bounding_box_tree() entities = tree.compute_collisions(p) if MPI.num_processes() == 1: self.assertEqual(sorted(entities), reference[mesh.topology().dim()])
def test_tiling(self): tile = UnitSquareMesh(2, 2) mf = MeshFunction('size_t', tile, tile.topology().dim() - 1, 0) CompiledSubDomain('near(x[0], 0.5) || near(x[1], 0.5)').mark(mf, 1) mesh_data = {} mesh_data = load_data(tile, mf, dim=1, data=mesh_data) mesh, mesh_data = TileMesh(tile, shape=(23, 13), mesh_data=mesh_data) f = mf_from_data(mesh, mesh_data)[1] self.assertTrue( np.linalg.norm(mesh.coordinates().min(axis=0) - np.zeros(2)) < 1E-13) self.assertTrue( np.linalg.norm(mesh.coordinates().max(axis=0) - np.array([23., 13.])) < 1E-13) self.assertTrue(23 * 13 * 4 == sum(1 for _ in SubsetIterator(f, 1)))
def test_ale(): # Create some mesh mesh = UnitSquareMesh(4, 5) # Make some cell function # FIXME: Initialization by array indexing is probably # not a good way for parallel test cellfunc = MeshFunction('size_t', mesh, mesh.topology().dim()) cellfunc.array()[0:4] = 0 cellfunc.array()[4:] = 1 # Create submeshes - this does not work in parallel submesh0 = SubMesh(mesh, cellfunc, 0) submesh1 = SubMesh(mesh, cellfunc, 1) # Move submesh0 disp = Constant(("0.1", "-0.1")) ALE.move(submesh0, disp) # Move and smooth submesh1 accordignly ALE.move(submesh1, submesh0) # Move mesh accordingly parent_vertex_indices_0 = \ submesh0.data().array('parent_vertex_indices', 0) parent_vertex_indices_1 = \ submesh1.data().array('parent_vertex_indices', 0) mesh.coordinates()[parent_vertex_indices_0[:]] = \ submesh0.coordinates()[:] mesh.coordinates()[parent_vertex_indices_1[:]] = \ submesh1.coordinates()[:] # If test passes here then it is probably working # Check for cell quality for sure magic_number = 0.28 rmin = MeshQuality.radius_ratio_min_max(mesh)[0] assert rmin > magic_number