Time varying boundary conditions

Issue #6 resolved
Former user created an issue

I want to optimize a time dependent boundary condition for heat conduction. I tried using a list of Constants (one for each time step) as parameters for the solution and a list of Controls for each of the parameters.

This kind of works (for Neumann BC), but if i use smaller time steps i get the following error:

File "/usr/lib/python2.7/dist-packages/dolfin_adjoint/utils.py", line 32, in scale scaled_obj = factor * obj TypeError: unsupported operand type(s) for *: 'float' and 'NoneType'

How can i optimize time dependent boundary conditions? (Of corse with much smaller time steps and also Dirichlet boundary conditions) Are there some examples of this? (I've looked into this: https://gist.github.com/funsim/6775785#file-optimal_control_transient-py but couldn't get it to work for the bc's)

Minimal working example:

from dolfin import *
from dolfin_adjoint import *
import numpy as np 
import time

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

def forward(m, step_time, end_time):
  t = 0.0 

  u = TrialFunction(V)
  v = TestFunction(V)
  u0 = Constant(2.0)
  u_1 = interpolate(u0, V)

  f = interpolate(Constant(20.0), V)
  a = u*v*dx(mesh) + step*inner(nabla_grad(u), nabla_grad(v))*dx(mesh)

  u = Function(V)

  adj_start_timestep()
  while t <= end_time:
    L = (u_1+step_time*f)*v*dx(mesh) + m[int(t/step_time)]*v*ds(1) 
    A, b = assemble_system(a, L)
    solve(A, u.vector(), b)

    plot(u, title="Loesung") 
    time.sleep(0.2)

    u_1.assign(u)
    t += step
    adj_inc_timestep(t)
  return u

end_time = 1.0
step = 0.05
num_steps = int(end_time/step)

mesh = UnitIntervalMesh(5)
V = FunctionSpace(mesh, "CG", 1) 

facet_marker = FacetFunction("size_t", mesh)
facet_marker.set_all(0)
Rand().mark(facet_marker, 1)

m = [Constant(1.0) for t in range(num_steps)]

ds = Measure("ds")[facet_marker]

u = forward(m, step, end_time)
interactive()

#adj_html("forward.html", "forward")
#assert(replay_dolfin())
#adj_html("adjoint.html", "adjoint")

J = Functional(inner(u-50.0,u-50.0)*dx*dt[0:FINISH_TIME])
controls = [Control(m[t]) for t in range(num_steps)]

rJ = ReducedFunctional(J, controls)
q_opt = minimize(rJ, method = "L-BFGS-B", options = {"gtol": 1e-3,"ftol": 1e-3,"disp": True})
forward(q_opt, step, end_time)
interactive()

Comments (3)

  1. Peter Kungurtsev

    How can we extend the example in the link by Simon, such that for each time step there's an independent control variable? As far as I can see, the example optimises for a single control parameter (omega) and I want to have a list of controls [omega_0, ..., omega_N] for each t_i = [t_0, ..., t_N] Thanks in advance!

  2. Log in to comment