def test_newton_solver():
    mesh, _, boundaries, dx, ds, _ = cashocs.regular_mesh(5)
    V = FunctionSpace(mesh, 'CG', 1)

    u = Function(V)
    u_fen = Function(V)
    v = TestFunction(V)

    F = inner(grad(u), grad(v)) * dx + Constant(1e2) * pow(
        u, 3) * v * dx - Constant(1) * v * dx
    bcs = cashocs.create_bcs_list(V, Constant(0), boundaries, [1, 2, 3, 4])

    solve(F == 0, u, bcs)
    u_fen.vector()[:] = u.vector()[:]
    u.vector()[:] = 0.0
    cashocs.damped_newton_solve(F,
                                u,
                                bcs,
                                rtol=1e-9,
                                atol=1e-10,
                                max_iter=50,
                                convergence_type='combined',
                                norm_type='l2',
                                damped=False,
                                verbose=True)

    assert np.allclose(u.vector()[:], u_fen.vector()[:])
示例#2
0
def test_create_bcs():
    trial = fenics.TrialFunction(V)
    test = fenics.TestFunction(V)
    a = fenics.inner(fenics.grad(trial), fenics.grad(test)) * dx
    L = fenics.Constant(1) * test * dx

    bc_val = np.random.rand()
    bc1 = fenics.DirichletBC(V, fenics.Constant(bc_val), boundaries, 1)
    bc2 = fenics.DirichletBC(V, fenics.Constant(bc_val), boundaries, 2)
    bc3 = fenics.DirichletBC(V, fenics.Constant(bc_val), boundaries, 3)
    bc4 = fenics.DirichletBC(V, fenics.Constant(bc_val), boundaries, 4)
    bcs_ex = [bc1, bc2, bc3, bc4]
    bcs = cashocs.create_bcs_list(V, fenics.Constant(bc_val), boundaries,
                                  [1, 2, 3, 4])

    u_ex = fenics.Function(V)
    u = fenics.Function(V)

    fenics.solve(a == L, u_ex, bcs_ex)
    fenics.solve(a == L, u, bcs)

    assert np.allclose(u_ex.vector()[:], u.vector()[:])
    assert abs(u(0, 0) - bc_val) < 1e-14
p = Function(W)
u = Function(V)

# Set up a discontinuous Galerkin method (SIPG) (needed for the adjoint system,
# reduces to the classical Galerkin method for CG elements)
n = FacetNormal(mesh)
h = CellDiameter(mesh)
h_avg = (h('+') + h('-')) / 2
flux = 1 / 2 * (inner(grad(u)('+') + grad(u)('-'), n('+')))
alpha = 1e3
gamma = 1e3
e = dot(grad(p), grad(y))*dx \
 - dot(avg(grad(p)), jump(y, n))*dS \
 - dot(jump(p, n), avg(grad(y)))*dS \
 + Constant(alpha)/h_avg*dot(jump(p, n), jump(y, n))*dS \
 - dot(grad(p), y*n)*ds \
 - dot(p*n, grad(y))*ds \
 + (Constant(gamma)/h)*p*y*ds - u*p*dx

bcs = cashocs.create_bcs_list(V, Constant(0), boundaries, [1, 2, 3, 4])

lambd = 1e-6
y_d = Expression('sin(2*pi*x[0])*sin(2*pi*x[1])', degree=1)

J = Constant(0.5) * (y - y_d) * (y - y_d) * dx + Constant(
    0.5 * lambd) * u * u * dx

optimization_problem = cashocs.OptimalControlProblem(e, bcs, J, y, u, p,
                                                     config)
optimization_problem.solve()
示例#4
0
elem_2 = FiniteElement('CG', mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, MixedElement([elem_1, elem_2]))

U = FunctionSpace(mesh, 'CG', 1)

state = Function(V)
adjoint = Function(V)
y, z = split(state)
p, q = split(adjoint)

u = Function(U)
v = Function(U)
controls = [u, v]

e_y = inner(grad(y), grad(p)) * dx + z * p * dx - u * p * dx
bcs_y = cashocs.create_bcs_list(V.sub(0), Constant(0), boundaries,
                                [1, 2, 3, 4])

e_z = inner(grad(z), grad(q)) * dx + y * q * dx - v * q * dx
bcs_z = cashocs.create_bcs_list(V.sub(1), Constant(0), boundaries,
                                [1, 2, 3, 4])

e = e_y + e_z
bcs = bcs_y + bcs_z

y_d = Expression('sin(2*pi*x[0])*sin(2*pi*x[1])', degree=1)
z_d = Expression('sin(4*pi*x[0])*sin(4*pi*x[1])', degree=1)
alpha = 1e-6
beta = 1e-6
J = Constant(0.5)*(y - y_d)*(y - y_d)*dx + Constant(0.5)*(z - z_d)*(z - z_d)*dx \
 + Constant(0.5*alpha)*u*u*dx + Constant(0.5*beta)*v*v*dx
示例#5
0
mesh, subdomains, boundaries, dx, ds, dS = cashocs.import_mesh('./mesh/mesh.xdmf')

v_elem = VectorElement('CG', mesh.ufl_cell(), 2)
p_elem = FiniteElement('CG', mesh.ufl_cell(), 1)
V = FunctionSpace(mesh, MixedElement([v_elem, p_elem]))

up = Function(V)
u, p = split(up)
vq = Function(V)
v, q = split(vq)

e = inner(grad(u), grad(v))*dx - p*div(v)*dx - q*div(u)*dx

u_in = Expression(('-1.0/4.0*(x[1] - 2.0)*(x[1] + 2.0)', '0.0'), degree=2)
bc_in = DirichletBC(V.sub(0), u_in, boundaries, 1)
bc_no_slip = cashocs.create_bcs_list(V.sub(0), Constant((0,0)), boundaries, [2,4])
bcs = [bc_in] + bc_no_slip

J = inner(grad(u), grad(u))*dx

sop = cashocs.ShapeOptimizationProblem(e, bcs, J, up, vq, boundaries, config)
sop.solve()



### Post Processing

import matplotlib.pyplot as plt
plt.figure(figsize=(15,3))

ax_mesh = plt.subplot(1, 3, 1)
示例#6
0
y = Function(V)
z = Function(V)
p = Function(V)
q = Function(V)
states = [y, z]
adjoints = [p, q]

u = Function(V)
v = Function(V)
controls = [u, v]

e1 = inner(grad(y), grad(p)) * dx + z * p * dx - u * p * dx
e2 = inner(grad(z), grad(q)) * dx + y * q * dx - v * q * dx
e = [e1, e2]

bcs1 = cashocs.create_bcs_list(V, Constant(0), boundaries, [1, 2, 3, 4])
bcs2 = cashocs.create_bcs_list(V, Constant(0), boundaries, [1, 2, 3, 4])
bcs = [bcs1, bcs2]

y_d = Expression('sin(2*pi*x[0])*sin(2*pi*x[1])', degree=1)
z_d = Expression('sin(4*pi*x[0])*sin(4*pi*x[1])', degree=1)
alpha = 1e-4
beta = 1e-4
J = Constant(0.5)*(y - y_d)*(y - y_d)*dx + Constant(0.5)*(z - z_d)*(z - z_d)*dx \
 + Constant(0.5*alpha)*u*u*dx + Constant(0.5*beta)*v*v*dx

ocp = cashocs.OptimalControlProblem(e, bcs, J, states, controls, adjoints,
                                    config)

elem = FiniteElement('CG', mesh.ufl_cell(), 1)
Mixed = FunctionSpace(mesh, MixedElement([elem, elem]))
示例#7
0
J = Constant(0.5) * (y - y_d) * (y - y_d) * dx + Constant(
    0.5 * alpha) * u * u * ds

scalar_product = TrialFunction(V) * TestFunction(V) * ds

ocp = cashocs.OptimalControlProblem(e,
                                    bcs,
                                    J,
                                    y,
                                    u,
                                    p,
                                    config,
                                    riesz_scalar_products=scalar_product)
ocp.solve()

bcs = cashocs.create_bcs_list(V, 1, boundaries, [1, 2, 3, 4])
bdry_idx = Function(V)
[bc.apply(bdry_idx.vector()) for bc in bcs]
mask = np.where(bdry_idx.vector()[:] == 1)[0]

y_bdry = Function(V)
u_bdry = Function(V)
y_bdry.vector()[mask] = y.vector()[mask]
u_bdry.vector()[mask] = u.vector()[mask]

error_inf = np.max(np.abs(y_bdry.vector()[:] - u_bdry.vector()[:])) / np.max(
    np.abs(u_bdry.vector()[:])) * 100
error_l2 = np.sqrt(assemble(
    (y - u) * (y - u) * ds)) / np.sqrt(assemble(u * u * ds)) * 100

print('Error regarding the (weak) imposition of the boundary values')
示例#8
0
up = Function(V)
u, p = split(up)
vq = Function(V)
v, q = split(vq)
c = Function(U)

e = inner(grad(u), grad(v)) * dx - p * div(v) * dx - q * div(u) * dx - inner(
    c, v) * dx


def pressure_point(x, on_boundary):
    return near(x[0], 0) and near(x[1], 0)


no_slip_bcs = cashocs.create_bcs_list(V.sub(0), Constant((0, 0)), boundaries,
                                      [1, 2, 3])
lid_velocity = Expression(('4*x[0]*(1-x[0])', '0.0'), degree=2)
bc_lid = DirichletBC(V.sub(0), lid_velocity, boundaries, 4)
bc_pressure = DirichletBC(V.sub(1),
                          Constant(0),
                          pressure_point,
                          method='pointwise')
bcs = no_slip_bcs + [bc_lid, bc_pressure]

alpha = 1e-5
u_d = Expression(('sqrt(pow(x[0], 2) + pow(x[1], 2))*cos(2*pi*x[1])',
                  '-sqrt(pow(x[0], 2) + pow(x[1], 2))*sin(2*pi*x[0])'),
                 degree=2)
J = Constant(0.5) * inner(u - u_d, u - u_d) * dx + Constant(
    0.5 * alpha) * inner(c, c) * dx