Example #1
0
def xtest_wrt_function_dirichlet_boundary():
    mesh = UnitSquareMesh(10, 10)

    V = FunctionSpace(mesh, "CG", 1)
    u = TrialFunction(V)
    u_ = Function(V)
    v = TestFunction(V)

    class Up(SubDomain):
        def inside(self, x, on_boundary):
            return near(x[1], 1)

    class Down(SubDomain):
        def inside(self, x, on_boundary):
            return near(x[1], 0)

    class Left(SubDomain):
        def inside(self, x, on_boundary):
            return near(x[0], 0)

    class Right(SubDomain):
        def inside(self, x, on_boundary):
            return near(x[0], 1)

    left = Left()
    right = Right()
    up = Up()
    down = Down()

    boundary = MeshFunction("size_t", mesh, mesh.geometric_dimension() - 1)
    boundary.set_all(0)
    up.mark(boundary, 1)
    down.mark(boundary, 2)
    ds = Measure("ds", subdomain_data=boundary)

    bc_func = project(Expression("sin(x[1])", degree=1), V)
    bc1 = DirichletBC(V, bc_func, left)
    bc2 = DirichletBC(V, 2, right)
    bc = [bc1, bc2]

    g1 = Constant(2)
    g2 = Constant(1)
    f = Function(V)
    f.vector()[:] = 10

    def J(bc):
        a = inner(grad(u), grad(v)) * dx
        L = inner(f, v) * dx + inner(g1, v) * ds(1) + inner(g2, v) * ds(2)

        solve(a == L, u_, [bc, bc2])

        return assemble(u_**2 * dx)

    _test_adjoint_function_boundary(J, bc1, bc_func)
Example #2
0
def test_solver_ident_zeros():
    """
    Test using ident zeros to restrict half of the domain
    """
    from fenics_adjoint import (UnitSquareMesh, Function, assemble, solve,
                                project, Expression, DirichletBC)
    mesh = UnitSquareMesh(10, 10)
    cf = MeshFunction("size_t", mesh, mesh.topology().dim(), 0)
    top_half().mark(cf, 1)

    ff = MeshFunction("size_t", mesh, mesh.topology().dim() - 1, 0)
    top_boundary().mark(ff, 1)

    dx = Measure("dx", domain=mesh, subdomain_data=cf)

    V = FunctionSpace(mesh, "CG", 1)
    u, v = TrialFunction(V), TestFunction(V)
    a = inner(grad(u), grad(v)) * dx(1)
    w = Function(V)

    with stop_annotating():
        w.assign(project(Expression("x[0]", degree=1), V))
    rhs = w**3 * v * dx(1)
    A = assemble(a, keep_diagonal=True)
    A.ident_zeros()
    b = assemble(rhs)
    bc = DirichletBC(V, Constant(1), ff, 1)
    bc.apply(A, b)
    uh = Function(V)
    solve(A, uh.vector(), b, "umfpack")

    J = assemble(inner(uh, uh) * dx(1))

    Jhat = ReducedFunctional(J, Control(w))
    with stop_annotating():
        w1 = project(Expression("x[0]*x[1]", degree=2), V)
    results = taylor_to_dict(Jhat, w, w1)
    assert (min(results["R0"]["Rate"]) > 0.95)
    assert (min(results["R1"]["Rate"]) > 1.95)
    assert (min(results["R2"]["Rate"]) > 2.95)
Example #3
0
def _test_wrt_function_dirichlet_boundary():
    mesh = IntervalMesh(10, 0, 1)
    V = FunctionSpace(mesh, "Lagrange", 1)

    c = Constant(1)

    u = TrialFunction(V)
    u_ = Function(V)
    v = TestFunction(V)
    f = project(Expression("1", degree=1), V)
    bc = DirichletBC(V, f, "on_boundary")

    def J(bc):
        a = inner(grad(u), grad(v)) * dx
        L = c * v * dx
        solve(a == L, u_, bc)
        return assemble(u_**2 * dx)

    _test_adjoint_function_boundary(J, bc, f)
A = fenics.FunctionSpace(mesh, "CG", 1)  # control function space

U_h = fenics.VectorElement("CG", mesh.ufl_cell(), 2)
P_h = fenics.FiniteElement("CG", mesh.ufl_cell(), 1)
W = fenics.FunctionSpace(mesh, U_h * P_h)  # mixed Taylor-Hood function space

# Define the boundary condition on velocity
(x, y) = ufl.SpatialCoordinate(mesh)
l = 1.0 / 6.0  # noqa: E741
gbar = 1.0
cond1 = ufl.And(ufl.gt(y, (1.0 / 4 - l / 2)), ufl.lt(y, (1.0 / 4 + l / 2)))
val1 = gbar * (1 - (2 * (y - 0.25) / l) ** 2)
cond2 = ufl.And(ufl.gt(y, (3.0 / 4 - l / 2)), ufl.lt(y, (3.0 / 4 + l / 2)))
val2 = gbar * (1 - (2 * (y - 0.75) / l) ** 2)
inflow_outflow = ufl.conditional(cond1, val1, ufl.conditional(cond2, val2, 0))
inflow_outflow_bc = fenics_adjoint.project(inflow_outflow, W.sub(0).sub(0).collapse())

solve_templates = (fenics_adjoint.Function(A),)
assemble_templates = (fenics_adjoint.Function(W), fenics_adjoint.Function(A))


@build_jax_fem_eval(solve_templates)
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