Time-dependent expressions and preassembled solves interact in a funny way

Bug #1190973 reported by Patrick Farrell
8
This bug affects 1 person
Affects Status Importance Assigned to Milestone
dolfin-adjoint
Fix Committed
High
Patrick Farrell

Bug Description

When the script below is run the replay_dolfin() function fails, but if the time-stepping information is added then the replay works! Doing the same thing with a series of variational problem solves without the timestepping information is ok as well. Is there something special about the matrix vector solves that requires them to always need time-stepping information?

from dolfin import *
from dolfin_adjoint import *

mesh = UnitSquareMesh(4,4)
V = FunctionSpace(mesh, "CG", 1)

u = TrialFunction(V)
v = TestFunction(V)

a = inner(grad(u), grad(v))*dx
u = Function(V, name = "solution")

bc = DirichletBC(V, 0, "on_boundary")

source = Expression("t*sin(x[0])*sin(x[1])", t = 0.0)
f = source*v*dx

t = 0.0
dt = 0.1

A = assemble(a)
#adj_start_timestep()
for i in range(2):
    t += dt
    source.t = t
    F = assemble(f)
    bc.apply(A)
    bc.apply(F)
    solve(A, u.vector(), F, "cg", "ilu")
    #adj_inc_timestep(t)

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

Revision history for this message
Patrick Farrell (pefarrell) wrote :

Interestingly, uncommenting those lines doesn't change that the replay fails, for me.

Revision history for this message
Patrick Farrell (pefarrell) wrote :

Hi Gabriel,

It was a race condition, well spotted!

dolfin-adjoint needs to bundle state (such as values of Expression parameters) with each equation. To do that, it takes a hash. If two equations had the same hash, then they would both fetch the same values for expression parameters, even if they should be different. To that end, I included time.time() in the hash -- but your example was so small that time.time() would (usually) return the same value, the expression parameters would be incorrect, and the replay would fail. Hmm!

That's why adding the adj_inc_timestep calls made a difference for you -- it made it so that time.time() returned something different.

I've fixed this by hashing with random.random() instead, in r726.

Patrick

Changed in dolfin-adjoint:
importance: Undecided → High
status: New → Fix Committed
assignee: nobody → Patrick Farrell (pefarrell)
Revision history for this message
Gabriel Balaban (gabrib) wrote :

Hi Patrick!
Great fix! I suspected that what I was seeing in the code example was a "smell" indicating some other issue. It's great to hear that you found the cause.
  Cheers,
   Gabriel

To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.