Hello everyone, I want to add a constant initial expression for X below. However, when I add it does not show the correct result. It works when I use spatial coordinates like X = interpolate(Expression(“x[0]+x[1]”, degree=1, X0=X0), W) . Thank you!
from dolfin import *
from dolfin_adjoint import *
import moola
import matplotlib.pyplot as plt
import numpy as np
# Set log level
set_log_level(LogLevel.WARNING)
# Optimization options for the form compiler
parameters["form_compiler"]["cpp_optimize"] = True
ffc_options = {"optimize": True,
"eliminate_zeros": True,
"precompute_basis_const": True,
"precompute_ip_const": True}
n = 64
mesh = UnitSquareMesh(n, n)
V = FunctionSpace(mesh, "CG", 2)
W = FunctionSpace(mesh, "DG", 0)
V_vector = VectorFunctionSpace(mesh, "CG", 2)
def boundary_bot(x, on_boundary):
tol = 1E-7
return on_boundary and near(x[1], 0.0, tol)
bc_bot = DirichletBC(V_vector, [0.0,0.0], boundary_bot)
def boundary_top(x, on_boundary):
tol = 1E-7
return on_boundary and near(x[1], 1.0, tol)
max_disp = -0.09
disp = Constant([0.0, max_disp])
bc_top = DirichletBC(V_vector, disp, boundary_top)
bcs = [bc_bot, bc_top]
boundary_subdomains = MeshFunction("size_t", mesh, mesh.topology().dim() - 1)
boundary_subdomains.set_all(0)
AutoSubDomain(boundary_top).mark(boundary_subdomains, 1)
dss = ds(subdomain_data=boundary_subdomains)
X0 = 0.82
X = interpolate(Expression("X0", degree=1, X0=X0), W)
file = File("X0.pvd");
file << X
eps = Constant(1e-0)
J = assemble((((1/eps) * X ** 2 * (1-X)**2)) * dx )))
control = Control(X)
rf = ReducedFunctional(J, control)
problem = MoolaOptimizationProblem(rf)
f_moola = moola.DolfinPrimalVector(X)
solver = moola.NewtonCG(problem, f_moola, options={'gtol': 1e-3,
'maxiter': 50,
'display': 3,
'ncg_hesstol': 0})
sol = solver.solve()
f_opt = sol['control'].data
file = File("Xtrial.pvd");
file << f_opt