def set_poisson_eq_2d(n=8): ''' Basic example where only the mesh refinement is controlled. ''' # mesh mesh = UnitSquareMesh(n, n) # function space V = FunctionSpace(mesh, 'P', 1) # bcs u_D = Expression('1 + x[0]*x[0] + 2*x[1]*x[1]', degree=2) def boundary(x, on_boundary): return on_boundary bc = DirichletBC(V, u_D, boundary) # variational problem u = TrialFunction(V) v = TestFunction(V) f = Constant(-6.0) a = dot(grad(u), grad(v)) * dx L = f * v * dx # define unknown u = Function(V) return a, L, u, bc
def formulate_laplacian(mesh, V=None): if V is None: V = FunctionSpace(mesh, 'CG', 1) u_h = TrialFunction(V) v = TestFunction(V) a = inner(grad(u_h), grad(v)) * dx b = inner(u_h, v) * dx dummy = inner(1., v) * dx return V, a, b, dummy
def get_formulation(V, u_initial, f, dt, var_name='Temperature'): # interpolate initial condition u_n = Function(V, name=var_name) u_n.interpolate(u_initial) # function spaces u_h = TrialFunction(V) v = TestFunction(V) # formulation a = u_h * v * dx + dt * dot(grad(u_h), grad(v)) * dx L = (u_n + dt * f) * v * dx return u_n, a, L
def get_formulation_constant_props(V, u_initial, f, dt, conductivity, density, specific_heat, var_name='Temperature'): # medium properties k = Constant(conductivity) rho = Constant(density) c_p = Constant(specific_heat) # interpolate initial condition u_n = Function(V, name=var_name) u_n.interpolate(u_initial) # function spaces u_h = TrialFunction(V) v = TestFunction(V) # formulation param1 = rho * c_p param2 = k / param1 a = u_h * v * dx + param2 * dt * dot(grad(u_h), grad(v)) * dx L = (u_n + param1 * dt * f) * v * dx return u_n, a, L
def project(v, V=None, bcs=None, mesh=None, function=None, solver_type="lu", preconditioner_type="default", form_compiler_parameters=None): """Return projection of given expression *v* onto the finite element space *V*. *Arguments* v a :py:class:`Function <dolfin.functions.function.Function>` or an :py:class:`Expression <dolfin.functions.expression.Expression>` bcs Optional argument :py:class:`list of DirichletBC <dolfin.fem.bcs.DirichletBC>` V Optional argument :py:class:`FunctionSpace <dolfin.functions.functionspace.FunctionSpace>` mesh Optional argument :py:class:`mesh <dolfin.cpp.Mesh>`. solver_type see :py:func:`solve <dolfin.fem.solving.solve>` for options. preconditioner_type see :py:func:`solve <dolfin.fem.solving.solve>` for options. form_compiler_parameters see :py:class:`Parameters <dolfin.cpp.Parameters>` for more information. *Example of usage* .. code-block:: python v = Expression("sin(pi*x[0])") V = FunctionSpace(mesh, "Lagrange", 1) Pv = project(v, V) This is useful for post-processing functions or expressions which are not readily handled by visualization tools (such as for example discontinuous functions). """ # Try figuring out a function space if not specified if V is None: # Create function space based on Expression element if trying # to project an Expression if isinstance(v, dolfin.function.expression.Expression): if mesh is not None and isinstance(mesh, cpp.mesh.Mesh): V = FunctionSpace(mesh, v.ufl_element()) # else: # cpp.dolfin_error("projection.py", # "perform projection", # "Expected a mesh when projecting an Expression") else: # Otherwise try extracting function space from expression V = _extract_function_space(v, mesh) # Projection into a MultiMeshFunctionSpace if isinstance(V, MultiMeshFunctionSpace): # Create the measuresum of uncut and cut-cells dX = ufl.dx() + ufl.dC() # Define variational problem for projection w = TestFunction(V) Pv = TrialFunction(V) a = ufl.inner(w, Pv) * dX L = ufl.inner(w, v) * dX # Assemble linear system A = assemble_multimesh(a, form_compiler_parameters=form_compiler_parameters) b = assemble_multimesh(L, form_compiler_parameters=form_compiler_parameters) # Solve linear system for projection if function is None: function = MultiMeshFunction(V) cpp.la.solve(A, function.vector(), b, solver_type, preconditioner_type) return function # Ensure we have a mesh and attach to measure if mesh is None: mesh = V.mesh() dx = ufl.dx(mesh) # Define variational problem for projection w = TestFunction(V) Pv = TrialFunction(V) a = ufl.inner(w, Pv) * dx L = ufl.inner(w, v) * dx # Assemble linear system A, b = assemble_system(a, L, bcs=bcs, form_compiler_parameters=form_compiler_parameters) # Solve linear system for projection if function is None: function = Function(V) cpp.la.solve(A, function.vector(), b, solver_type, preconditioner_type) return function