def homogenize_boundaries(bcs): if isinstance(bcs, dla.DirichletBC): bcs = [bcs] hbcs = [dla.DirichletBC(bc) for bc in bcs] for hbc in hbcs: hbc.homogenize() return hbcs
def get_boundary_indices(function_space): bc_map = dla.Function(function_space) bc = dla.DirichletBC(function_space, dla.Constant(1.0), 'on_boundary') bc.apply(bc_map.vector()) indices = np.arange( bc_map.vector().size())[bc_map.vector().get_local() == 1.0] return indices
def fenics_solve(f): u = fa.Function(V, name="State") v = fn.TestFunction(V) F = (ufl.inner(ufl.grad(u), ufl.grad(v)) - f * v) * ufl.dx bcs = [fa.DirichletBC(V, 0.0, "on_boundary")] fa.solve(F == 0, u, bcs) return u
def forward(rho): """Solve the forward problem for a given fluid distribution rho(x).""" w = fenics_adjoint.Function(W) (u, p) = fenics.split(w) (v, q) = fenics.TestFunctions(W) inner, grad, dx, div = ufl.inner, ufl.grad, ufl.dx, ufl.div F = ( alpha(rho) * inner(u, v) * dx + inner(grad(u), grad(v)) * dx + inner(grad(p), v) * dx + inner(div(u), q) * dx ) bcs = [ fenics_adjoint.DirichletBC(W.sub(0).sub(1), 0, "on_boundary"), fenics_adjoint.DirichletBC(W.sub(0).sub(0), inflow_outflow_bc, "on_boundary"), ] fenics_adjoint.solve(F == 0, w, bcs=bcs) return w
def collect_dirichlet_boundaries(function_space, boundary_conditions, boundaries): num_bndrys = len(boundary_conditions) dirichlet_bcs = [] for ii in range(num_bndrys): if boundary_conditions[ii][0] == 'dirichlet': bc_expr = boundary_conditions[ii][2] #ii must be same marker number as used in mark_boundaries() dirichlet_bcs.append( dla.DirichletBC(function_space, bc_expr, boundaries, ii)) return dirichlet_bcs
def solve_fenics(kappa0, kappa1): f = fa.Expression( "10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) u = fa.Function(V) bcs = [fa.DirichletBC(V, fa.Constant(0.0), "on_boundary")] inner, grad, dx = ufl.inner, ufl.grad, ufl.dx JJ = 0.5 * inner(kappa0 * grad(u), grad(u)) * dx - kappa1 * f * u * dx v = fenics.TestFunction(V) F = fenics.derivative(JJ, u, v) fa.solve(F == 0, u, bcs=bcs) return u
def get_num_subdomain_dofs(Vh, subdomain): """ Get the number of dofs on a subdomain """ temp = dla.Function(Vh) bc = dla.DirichletBC(Vh, dla.Constant(1.0), subdomain) # warning applying bc does not just apply subdomain.inside to all coordinates # it does some boundary points more than once and other inside points not # at all. bc.apply(temp.vector()) vec = temp.vector().get_local() dl.plot(temp) import matplotlib.pyplot as plt plt.show() return np.where(vec > 0)[0].shape[0]
fn.CellType.Type.triangle) V = fn.VectorFunctionSpace(mesh, "CG", 1) # Displacements C = fn.FunctionSpace(mesh, "CG", 1) # Control # Volumetric Load q = -10.0 / t b = fa.Constant((0.0, q)) def Left_boundary(x, on_boundary): return on_boundary and abs(x[0]) < fn.DOLFIN_EPS u_L = fa.Constant((0.0, 0.0)) bcs = [fa.DirichletBC(V, u_L, Left_boundary)] @build_jax_fem_eval((fa.Function(C), )) def forward(x): u = fn.TrialFunction(V) w = fn.TestFunction(V) sigma = lmbda * tr(sym(grad(u))) * Identity(2) + 2 * G * sym( grad(u)) # Stress R = simp(x) * inner(sigma, grad(w)) * dx - dot(b, w) * dx a, L = ufl.lhs(R), ufl.rhs(R) u = fa.Function(V) fa.solve(a == L, u, bcs) return u