Hi, I am trying to solve an optimal control problem using an example code I copied from here. However, I am getting errors that I don’t understand. Your help regarding this would be much appreciated. Thank you. Below is the code
from fenics import *
import numpy as np
import matplotlib.pyplot as plt
from fenics_adjoint import *
from ufl import replace
Time parameters
N = 30
T = 2.0
num_steps = 5
dt = T/num_steps
Physical parameters
T_abs = 546.30 # Air absolute temperature in Kelvin
R = 8.31 # The ideal gas constant
w = 0.35 # Relative humidity of the air
P = 10e5; Air pressure
Summary
This text will be hidden
xa = (0.62198)/(0.62198 + w)
xw = w/(0.62198 + w)
rho_a = (28.9645P)(xa + 0.62198xw)/(RT_abs)
cp_a = 1 + 1.88*w
mesh = UnitSquareMesh(nx,ny)
V = FunctionSpace(mesh, “P”, 1)
W = FunctionSpace(mesh, “DG”, 0)
u = Function (V, name=“State”)
u_old = Function(V, name=“OldState”)
u_d = Function(V, name=“Desired state”)
x = SpatialCoordinate(mesh)
Q = [Function(W, name=“Control_”+str(t)) for t in range(N)]
v = TestFunction(V)
v_air = Function(W)
Define the initial solution and the air velocity vector valued
v_air = Constant((0.2, 0.2))
u_old = Constant(293.15)
Define boundary conditions
bc = DirichletBC(V, u_old, “on_boundary”)
As k_a depends upon the trial function T_a, it will be defined separately in the program as a function of T_a
def k_a (u):
return 0.02387 + 7.590e-5*u
def run(Q, annotate=False):
bc = DirichletBC(V, u_old, “on_boundary”)
F = uvdx + dt*(k_a(u)/(rho_acp_a))dot(v_air, grad(u))vdx + dt(k_a(u)/(rho_acp_a))dot(grad(u), grad(v))dx - dt(1/(rho_acp_a))Q[0]vdx - u_oldv*dx
for t in range(N):
F_t = replace(F,{Q[0]:Q[t]})
k_a(u_old)
solve(F_t == 0, u, bc, annotate=annotate)
u_old.assign(u)
return u_old
u_d = Constant(294.15)
alpha = Constant(1e-7)
gamma = Constant(1e-5)
y_min = Constant(20.0)
y_max = Constant(24.444)
u_old = run(Q,annotate=True)
J = dt0.5inner(u_old-u_d,u_old -u_d)dx + dt0.25gammainner(y_min - u_old + sqrt((y_min - u_old)**2), y_min - u_old + sqrt((y_min - u_old)**2))dx + dt0.25gammainner(u_old - y_max + sqrt((u_old - y_max)**2), u_old - y_max + sqrt((u_old - y_max)**2))dx
for i in range(N):
J+= 0.5alpha*inner(Q[i],Q[i])*dx
J = assemble(J)
controls = [Control(Q[t]) for t in range(N)]
rf = ReducedFunctional(J, controls)
Q_opt = minimize(rf,
bounds = [
[1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0, 1000.0],
[2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0, 2000.0]],
options={“disp”: True})
u_opt = run(Q_opt)
fig1=plt.figure()
plt.subplot(1,2,1)
p = plot(u_d, title=“desired solution”)
cb = fig1.colorbar(p)
fig2=plt.figure()
plt.subplot(1,2,2)
p1 = plot(u_opt, title= “optmised solution”)
cb1 = fig2.colorbar(p)
#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)]
#fig=plt.figure()
#p = plot(u_d, title=“desired solution”)