def MixedToMixedSpacesCopySubComponentToDifferentLocation(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_1 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element, components=[("uxx", "uxy"), ("uyx", "uyy")]) W = V return (V, W)
def get_elements_1(): return ( lambda mesh: FiniteElement("Lagrange", mesh.ufl_cell(), 1), pytest.mark.slow(lambda mesh: FiniteElement("Lagrange", mesh.ufl_cell(), 2)), lambda mesh: VectorElement("Lagrange", mesh.ufl_cell(), 1), pytest.mark.slow(lambda mesh: VectorElement("Lagrange", mesh.ufl_cell(), 2)), pytest.mark.slow(lambda mesh: TensorElement("Lagrange", mesh.ufl_cell(), 1)), pytest.mark.slow(lambda mesh: TensorElement("Lagrange", mesh.ufl_cell(), 2)), lambda mesh: StokesElement("Lagrange", mesh.ufl_cell(), 1), pytest.mark.slow(lambda mesh: StokesElement("Lagrange", mesh.ufl_cell(), 2)), lambda mesh: FiniteElement("Real", mesh.ufl_cell(), 0), pytest.mark.slow(lambda mesh: VectorElement("Real", mesh.ufl_cell(), 0)), pytest.mark.slow(lambda mesh: FunctionAndRealElement("Lagrange", mesh.ufl_cell(), 1)), pytest.mark.slow(lambda mesh: FunctionAndRealElement("Lagrange", mesh.ufl_cell(), 2)) )
def compute_error(problem, mesh_size): mesh = problem.mesh_generator(mesh_size) u = problem.solution['u'] u_sol = Expression((ccode(u['value'][0]), ccode(u['value'][1])), degree=u['degree']) p = problem.solution['p'] p_sol = Expression(ccode(p['value']), degree=p['degree']) f = Expression( (ccode(problem.f['value'][0]), ccode(problem.f['value'][1])), degree=problem.f['degree']) W = VectorElement('Lagrange', mesh.ufl_cell(), 2) P = FiniteElement('Lagrange', mesh.ufl_cell(), 1) WP = FunctionSpace(mesh, W * P) # Get Dirichlet boundary conditions u_bcs = DirichletBC(WP.sub(0), u_sol, 'on_boundary') p_bcs = DirichletBC(WP.sub(1), p_sol, 'on_boundary') u_approx, p_approx = flow.stokes.solve(WP, bcs=[u_bcs, p_bcs], mu=problem.mu, f=f, verbose=True, tol=1.0e-12) # compute errors u_error = errornorm(u_sol, u_approx) p_error = errornorm(p_sol, p_approx) return mesh.hmax(), u_error, p_error
def exact_solution(domain): P7 = VectorElement("Lagrange", "triangle", degree=8, dim=2) P2 = FiniteElement("Lagrange", "triangle", 3) coeff = (P_inlet-P_outlet)/(2*Length*visc) u_exact = Expression(("C*x[1]*(H - x[1])", "0.0"), C=coeff,H=Height, element=P7, domain=domain) p_exact = Expression("dP-dP/L*x[0]", dP=(P_inlet-P_outlet), L=Length, element=P2, domain=domain) return u_exact, p_exact
def test_taylor_hood_cube(): pytest.xfail("Problem with Mixed Function Spaces") meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Ve = VectorElement("CG", meshc.ufl_cell(), 2) Qe = FiniteElement("CG", meshc.ufl_cell(), 1) Ze = MixedElement([Ve, Qe]) Zc = FunctionSpace(meshc, Ze) Zf = FunctionSpace(meshf, Ze) z = Expression( ("x[0]*x[1]", "x[1]*x[2]", "x[2]*x[0]", "x[0] + 3*x[1] + x[2]"), degree=2) zc = interpolate(z, Zc) zf = interpolate(z, Zf) mat = PETScDMCollection.create_transfer_matrix(Zc, Zf) Zuc = Function(Zf) mat.mult(zc.vector(), Zuc.vector()) Zuc.vector().update_ghost_values() diff = Function(Zf) diff.assign(Zuc - zf) assert diff.vector().norm("l2") < 1.0e-12
def test_pointsource_mixed_space(mesh, point): """Tests point source when given constructor PointSource(V, point, mag) with a vector for a mixed function space that isn't placed at a node for 1D, 2D and 3D. Global points given to constructor from rank 0 processor. """ rank = MPI.rank(mesh.mpi_comm()) ele1 = FiniteElement("CG", mesh.ufl_cell(), 1) ele2 = FiniteElement("DG", mesh.ufl_cell(), 2) ele3 = VectorElement("CG", mesh.ufl_cell(), 2) V = FunctionSpace(mesh, MixedElement([ele1, ele2, ele3])) value_dimension = V.element().value_dimension(0) v = TestFunction(V) b = assemble(dot(Constant([0.0] * value_dimension), v) * dx) if rank == 0: ps = PointSource(V, point, 10.0) else: ps = PointSource(V, []) ps.apply(b) # Checks array sums to correct value b_sum = b.sum() assert round(b_sum - 10.0 * value_dimension) == 0
def _init_spaces(self): P2 = FiniteElement('P', self.mesh.ufl_cell(), 2) N = self.parameters['N'] if N == 1: elem = P2 else: elem = MixedElement([P2 for i in range(N)]) v_elem = VectorElement('P', self.mesh.ufl_cell(), 1) mesh = self.mesh self.state_space = FunctionSpace(mesh, elem) self.vector_space = FunctionSpace(mesh, v_elem) self.pressure_space = FunctionSpace(mesh, P2) self.state = Function(self.state_space, name="m") self.state_previous = Function(self.state_space) self.state_test = TestFunction(self.state_space) self.displacement = Function(self.vector_space, name="du") self.mech_velocity = Function(self.vector_space) self.pressure = [ Function(self.pressure_space, name="p{}".format(i)) for i in range(N) ] self.darcy_flow = [ Function(self.vector_space, name="w{}".format(i)) for i in range(N) ]
def test_mixed_parallel(): mesh = UnitSquareMesh(MPI.comm_world, 5, 8) V = VectorElement("Lagrange", triangle, 4) Q = FiniteElement("Lagrange", triangle, 5) W = FunctionSpace(mesh, Q * V) F = Function(W) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] values[:, 1] = x[:, 1] values[:, 2] = numpy.sin(x[:, 0] + x[:, 1]) F.interpolate(Expression(expr_eval, shape=(3, ))) # Generate random points in this mesh partition (one per cell) x = numpy.zeros(3) for c in Cells(mesh): x[0] = random() x[1] = random() * (1 - x[0]) x[2] = (1 - x[0] - x[1]) p = Point(0.0, 0.0) for i, v in enumerate(VertexRange(c)): p += v.point() * x[i] p = p.array()[:2] val = F(p) assert numpy.allclose(val[0], p[0]) assert numpy.isclose(val[1], p[1]) assert numpy.isclose(val[2], numpy.sin(p[0] + p[1]))
def CollapsedFunctionSpaces(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 2, dim=2) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) U = FunctionSpace(mesh, element) V = U.sub(0).collapse() return (V, U)
def test_taylor_hood_cube(): pytest.xfail("Problem with Mixed Function Spaces") meshc = UnitCubeMesh(MPI.comm_world, 2, 2, 2) meshf = UnitCubeMesh(MPI.comm_world, 3, 4, 5) Ve = VectorElement("CG", meshc.ufl_cell(), 2) Qe = FiniteElement("CG", meshc.ufl_cell(), 1) Ze = MixedElement([Ve, Qe]) Zc = FunctionSpace(meshc, Ze) Zf = FunctionSpace(meshf, Ze) @function.expression.numba_eval def expr_eval(values, x, cell_idx): values[:, 0] = x[:, 0] * x[:, 1] values[:, 1] = x[:, 1] * x[:, 2] values[:, 2] = x[:, 2] * x[:, 0] values[:, 3] = x[:, 0] + 3.0 * x[:, 1] + x[:, 2] z = Expression(expr_eval, shape=(4, )) zc = interpolate(z, Zc) zf = interpolate(z, Zf) mat = PETScDMCollection.create_transfer_matrix(Zc, Zf) Zuc = Function(Zf) mat.mult(zc.vector(), Zuc.vector()) Zuc.vector().update_ghost_values() diff = Function(Zf) diff.assign(Zuc - zf) assert diff.vector().norm("l2") < 1.0e-12
def __init__(self, mesh, mf, bc_dict, move_dict): """ Inititalize Stokes solver with a mesh, its corresponding facet function, a dictionary describing boundary conditions and a dictionary describing which boundaries are fixed in the shape optimization setting """ self.mesh = mesh self.backup = mesh.coordinates().copy() self.mf = mf V2 = VectorElement("CG", mesh.ufl_cell(), 2) S1 = FiniteElement("CG", mesh.ufl_cell(), 1) TH = V2 * S1 self.VQ = FunctionSpace(self.mesh, TH) self.w = Function(self.VQ) self.f = Constant([0.]*mesh.geometric_dimension()) self.S = VectorFunctionSpace(self.mesh, "CG", 1) self.move_dict = move_dict self.J = 0 self._init_bcs(bc_dict) self.solve() self.vfac = 1e4 self.bfac = 1e2 self._init_geometric_functions() self.eval_current_J() self.eval_current_dJ() self.outfile = File("output/u_singlemesh.pvd") self.outfile << self.u self.gradient_scale = 1 self.iteration_counter = 1 self.create_mapping_for_moving_boundary()
def test_tabulate_dofs(mesh_factory): func, args = mesh_factory mesh = func(*args) W0 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) W1 = VectorElement("Lagrange", mesh.ufl_cell(), 1) W = FunctionSpace(mesh, W0 * W1) L0 = W.sub(0) L1 = W.sub(1) L01 = L1.sub(0) L11 = L1.sub(1) for i, cell in enumerate(Cells(mesh)): dofs0 = L0.dofmap().cell_dofs(cell.index()) dofs1 = L01.dofmap().cell_dofs(cell.index()) dofs2 = L11.dofmap().cell_dofs(cell.index()) dofs3 = L1.dofmap().cell_dofs(cell.index()) assert np.array_equal(dofs0, L0.dofmap().cell_dofs(i)) assert np.array_equal(dofs1, L01.dofmap().cell_dofs(i)) assert np.array_equal(dofs2, L11.dofmap().cell_dofs(i)) assert np.array_equal(dofs3, L1.dofmap().cell_dofs(i)) assert len(np.intersect1d(dofs0, dofs1)) == 0 assert len(np.intersect1d(dofs0, dofs2)) == 0 assert len(np.intersect1d(dofs1, dofs2)) == 0 assert np.array_equal(np.append(dofs1, dofs2), dofs3)
def stokes(self): P2 = VectorElement("CG", self.mesh.ufl_cell(), 2) P1 = FiniteElement("CG", self.mesh.ufl_cell(), 1) TH = P2 * P1 VQ = FunctionSpace(self.mesh, TH) mf = self.mf self.no_slip = Constant((0., 0)) self.topflow = Expression(("-x[0] * (x[0] - 1.0) * 6.0 * m", "0.0"), m=self.U_m, degree=2) bc0 = DirichletBC(VQ.sub(0), self.topflow, mf, self.bc_dict["top"]) bc1 = DirichletBC(VQ.sub(0), self.no_slip, mf, self.bc_dict["left"]) bc2 = DirichletBC(VQ.sub(0), self.no_slip, mf, self.bc_dict["bottom"]) bc3 = DirichletBC(VQ.sub(0), self.no_slip, mf, self.bc_dict["right"]) # bc4 = DirichletBC(VQ.sub(1), Constant(0), mf, self.bc_dict["top"]) bcs = [bc0, bc1, bc2, bc3] vup = TestFunction(VQ) up = TrialFunction(VQ) # the solution will be in here: up_ = Function(VQ) u, p = split(up) # Trial vu, vp = split(vup) # Test u_, p_ = split(up_) # Function holding the solution F = self.mu*inner(grad(vu), grad(u))*dx - inner(div(vu), p)*dx \ - inner(vp, div(u))*dx + dot(self.g*self.rho, vu)*dx solve(lhs(F) == rhs(F), up_, bcs=bcs) self.u_.assign(project(u_, self.V)) self.p_.assign(project(p_, self.Q)) return
def test_save_and_checkpoint_vector(tempdir, encoding, fe_degree, fe_family, mesh_tdim, mesh_n): if invalid_config(encoding): pytest.skip("XDMF unsupported in current configuration") if invalid_fe(fe_family, fe_degree): pytest.skip("Trivial finite element") filename = os.path.join(tempdir, "u2_checkpoint.xdmf") mesh = mesh_factory(mesh_tdim, mesh_n) FE = VectorElement(fe_family, mesh.ufl_cell(), fe_degree) V = FunctionSpace(mesh, FE) u_in = Function(V) u_out = Function(V) if mesh.geometry.dim == 1: u_out.interpolate(Expression(("x[0]", ), degree=1)) elif mesh.geometry.dim == 2: u_out.interpolate(Expression(("x[0]*x[1]", "x[0]"), degree=2)) elif mesh.geometry.dim == 3: u_out.interpolate(Expression(("x[0]*x[1]", "x[0]", "x[2]"), degree=2)) with XDMFFile(mesh.mpi_comm(), filename, encoding=encoding) as file: file.write_checkpoint(u_out, "u_out", 0) with XDMFFile(mesh.mpi_comm(), filename) as file: u_in = file.read_checkpoint(V, "u_out", 0) u_in.vector().axpy(-1.0, u_out.vector()) assert u_in.vector().norm(cpp.la.Norm.l2) < 1.0e-12
def generate_collapsed_bilinear_form_space(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 2) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) U = FunctionSpace(mesh, element) V = U.sub(0).collapse() return (V, U)
def load_local_basis(h5file, lgroup, mesh, geo): if h5file.has_dataset(lgroup): # Get local bais functions local_basis_attrs = h5file.attributes(lgroup) lspace = local_basis_attrs["space"] family, order = lspace.split("_") namesstr = local_basis_attrs["names"] names = namesstr.split(":") elm = VectorElement(family=family, cell=mesh.ufl_cell(), degree=int(order), quad_scheme="default") V = FunctionSpace(mesh, elm) for name in names: lb = Function(V, name=name) h5file.read(lb, lgroup + "/{}".format(name)) setattr(geo, name, lb) else: setattr(geo, "circumferential", None) setattr(geo, "radial", None) setattr(geo, "longitudinal", None)
def setUp(self): from dolfin import (MixedElement as MixE, Function, FunctionSpace, FiniteElement, VectorElement, UnitSquareMesh) self.mesh = mesh = UnitSquareMesh(10, 10) muc = mesh.ufl_cell self.fe1 = FiniteElement("CG", muc(), 1) self.fe2 = FiniteElement("CG", muc(), 2) self.ve1 = VectorElement("CG", muc(), 2, 2) self.me = ( MixE([self.fe1, self.fe2]), MixE([MixE([self.ve1, self.fe2]), self.fe1, self.ve1, self.fe2]), # MixE([self.fe1]), # broken ) self.W = [FunctionSpace(mesh, me) for me in self.me] self.w = [Function(W) for W in self.W] self.reg = FunctionSubspaceRegistry() self.x = dolfin.SpatialCoordinate(mesh) for W in self.W: self.reg.register(W)
def MixedSpacesRestrictionAmbiguous(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element) W = FunctionSpace(mesh, element_0) return (V, W)
def MixedSpacesExtensionSolveAmbiguityWithComponents(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element_0) W = FunctionSpace(mesh, element, components=[["u", "s"], "p"]) return (V, W)
def MixedSpacesExtensionAutomatic(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 2) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element_0) W = FunctionSpace(mesh, element) return (V, W)
def __init__(self, grid, u_degree, p_degree, split=False): w_elem = VectorElement("CG", grid.mesh.ufl_cell(), u_degree) q_elem = FiniteElement("CG", grid.mesh.ufl_cell(), p_degree) self.W = FunctionSpace(grid.mesh, w_elem) self.Q = FunctionSpace(grid.mesh, q_elem) self.split = split if not self.split: self.mixedSpace = BlockFunctionSpace([self.W, self.Q])
def MixedSpacesRestrictionToSubElementSolveAmbiguityWithComponents(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_00 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element, components=[("ux", "uy"), "p"]) W = FunctionSpace(mesh, element_00) return (V, W)
def MixedSpacesRestrictionToSubElementAmbiguous(mesh): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 2) # Note that we need to use 2nd order FE otherwise element_00 = FiniteElement("Lagrange", mesh.ufl_cell(), 2) # the automatic detection would restrict the element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) # pressure component element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element) W = FunctionSpace(mesh, element_00) return (V, W)
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 MixedSpacesRestrictionSolveAmbiguityWithComponents(mesh, sub_element): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element, components=["u", "p"]) assert sub_element in (0, 1) if sub_element == 0: W = FunctionSpace(mesh, element_0) elif sub_element == 1: W = FunctionSpace(mesh, element_1) return (V, W)
def MixedSpacesRestrictionAutomatic(mesh, sub_element): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 2) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element) assert sub_element in (0, 1) if sub_element == 0: W = FunctionSpace(mesh, element_0) elif sub_element == 1: W = FunctionSpace(mesh, element_1) return (V, W)
def exact_solution(domain): P7 = VectorElement("Lagrange", "triangle", degree=8, dim=2) P2 = FiniteElement("Lagrange", "triangle", 3) u_exact = Expression(("x[0]*x[0]*(1.0 - x[0])*(1.0 - x[0])*(2.0*x[1] \ - 6.0*x[1]*x[1] + 4.0*x[1]*x[1]*x[1])", "-x[1]*x[1]*(1.0 - x[1])*(1.0 - x[1])*(2.0*x[0] \ - 6.0*x[0]*x[0] + 4.0*x[0]*x[0]*x[0])"), element=P7, domain=domain) p_exact = Expression("x[0]*(1.0 - x[0])", element=P2, domain=domain) return u_exact, p_exact
def make_space(V, shape, mesh): '''Tensor product space of right shape''' if not shape: elm = V elif len(shape) == 1: elm = VectorElement(V, len(shape)) elif len(shape) == 2: elm = TensorElement(V, shape) else: raise ValueError('No spaces for tensor of rank 3 and higher') return FunctionSpace(mesh, elm)
def MixedSpacesExtensionFromSubElementSolveAmbiguityWithComponents(mesh, components): element_0 = VectorElement("Lagrange", mesh.ufl_cell(), 1) element_00 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element_1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1) element = MixedElement(element_0, element_1) V = FunctionSpace(mesh, element_00) assert components in (tuple, str) if components is tuple: W = FunctionSpace(mesh, element) else: W = FunctionSpace(mesh, element, components=[("ux", "uy"), "p"]) return (V, W)
def test_global_dof_builder(mesh_factory): func, args = mesh_factory mesh = func(*args) V = VectorElement("CG", mesh.ufl_cell(), 1) Q = FiniteElement("CG", mesh.ufl_cell(), 1) R = FiniteElement("R", mesh.ufl_cell(), 0) W = FunctionSpace(mesh, MixedElement([Q, Q, Q, R])) W = FunctionSpace(mesh, MixedElement([Q, Q, R, Q])) W = FunctionSpace(mesh, V * R) W = FunctionSpace(mesh, R * V) assert (W)