Hi,
I try to minimize of the cost function with regulatization term
min_{u,m} 1/2 || u(T)-u_d ||^2 +\alpha/2 || m ||^2
subject to \frac{\partial u} {\partial t } - \nu*\Delta u = m
u=0 at time t=0 and u=0 on the boundary
I run the code and get the following error
F = ((u-u_old)*v/dt + nu*inner(grad(u), grad(v)) - m[0]*v)*dx
Traceback (most recent call last):
File "<ipython-input-66-4d4ef0e38cd7>", line 1, in <module>
F = ((u-u_old)*v/dt + nu*inner(grad(u), grad(v)) - m[0]*v)*dx
File "/usr/lib/python3/dist-packages/ufl/measure.py", line 437, in __rmul__
domains = extract_domains(integrand)
File "/usr/lib/python3/dist-packages/ufl/domain.py", line 351, in extract_domains
return sorted(join_domains(domainlist))
TypeError: '<' not supported between instances of 'Mesh' and 'Mesh'
also get error for dt[FINISH_TIME]. It does not work neither. How can I make it work and find the plots.
the version of python is 3.6.9 and install on ubuntu.
The Code is
from fenics import *
import numpy as np
import matplotlib.pyplot as plt
from fenics_adjoint import *
from ufl import replace
N =30
dt = 0.2
T=1.0
nx = ny = 30
mesh = UnitSquareMesh(nx,ny)
V = FunctionSpace(mesh, "CG", 1)
W = FunctionSpace(mesh, "DG", 0)
u = Function (V, name="State")
u_old = Function(V, name="OldState")
x = SpatialCoordinate(mesh)
u_d =Expression("16*x[0]*(x[0]-1)*x[1]*(x[1]-1)*sin(pi*t)", t=0, degree=4)
nu= interpolate(Expression("1e-5",degree=2),V)
m = [Function(W, name="Control_"+str(t)) for t in range(N)]
v = TestFunction(V)
def run(m,annotate=False):
u_old.assign(u_d)
bc = DirichletBC(V, 0, "on_boundary")
F = ((u-u_old)*v/dt + nu*inner(grad(u), grad(v)) - m[0]*v)*dx
for t in range(N):
F_t = replace(F,{m[0]:m[t]})
solve(F_t == 0, u, bc, annotate=annotate)
u_old.assign(u)
u_de = Expression("16*x[0]*(x[0]-1)*x[1]*(x[1]-1)*sin(pi*t)", t=1.0, degree=4)
u_d = interpolate(u_de, V)
alpha = Constant(1e-7)
u_old=run(m,annotate=True)
J = assemble((0.5*inner(u_old-u_d,u_old -u_d))*dx*dt[FINISH_TIME] +0.5*alpha*sum(inner(m[i],m[i])*dx for i in range(N)))
controls = [Control(m[t]) for t in range(N)]
rf = ReducedFunctional(J, controls)
m_opt = minimize(rf, options={"disp": True})
u_opt = run(m_opt)
plot(u_d, title="optimised solution")
#plot(u_d, title="desired")
#plot(u-u_d, title="difference between optimal and desired state")
#[plot(m_opt[t], title="optimal control, t=%i" %t) for t in range(N)]
er = errornorm(u,u_d)
print(er)