firedrakeproject / firedrake

Firedrake is an automated system for the portable solution of partial differential equations using the finite element method (FEM)
https://firedrakeproject.org
Other
512 stars 160 forks source link

expression test for firedrake-adjoint #956

Closed APaganini closed 4 years ago

APaganini commented 7 years ago

I'm trying to make the following test run, but I get the complaint

if expr._hash is not None:
AttributeError: _hash 

(see below for the whole message)

from firedrake import *
from firedrake_adjoint import *
dt_meas = dt

fexp = Expression("x[0]*(x[0]-1)*x[1]*(x[1]-1)*sin(t)", t=0, degree=4)
mesh = UnitSquareMesh(4, 4)
V = FunctionSpace(mesh, "CG", 1)

dt = Constant(0.1)
T = 1.0

def main():
    u = TrialFunction(V)
    v = TestFunction(V)
    f = Function(V)

    # Create a list with all control functions
    ctrls = {}
    t = float(dt)
    while t <= T:
        fexp.t = t
        ctrls[t] = project(fexp, V, name="f_{}".format(t), annotate=True)
        t += float(dt)

    u_0 = Function(V, name="Solution")
    u_1 = Function(V, name="NextSolution")

    F = ( (u - u_0)/dt*v + inner(grad(u), grad(v)) + f*v)*dx
    a, L = lhs(F), rhs(F)
    #import ipdb
    #ipdb.set_trace()
    bc = DirichletBC(V, 1.0, "on_boundary")

    t = float(dt)
    adj_start_timestep(time=t)
    while t <= T:
        f.assign(ctrls[t], annotate=True)
        solve(a == L, u_0, bc)
        t += float(dt)
        adj_inc_timestep(time=t, finished=t>T)

    return u_0, list(ctrls.values())

def test_heat():
    u, ctrls = main()

    regularisation = sum([(new-old)**2 for new, old in zip(ctrls[1:], ctrls[:-1])])
    regularisation = regularisation*dx*dt_meas[START_TIME]

    alpha = Constant(1e0)
    J = Functional(u**2*dx*dt_meas + alpha*regularisation)
    m = [Control(c) for c in ctrls]

    rf = ReducedFunctional(J, m)
    minconv = rf.taylor_test(ctrls, seed=1e4)

    assert minconv > 1.9

if __name__ == "__main__":
    test_heat()

The error message reads:

Traceback (most recent call last):
  File "expression_derivative.py", line 42, in <module>
    taylor_test_expression(f, V)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/dolfin-adjoint/dolfin_adjoint/util
    J = functional.Functional(Jform)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/dolfin-adjoint/dolfin_adjoint/func
    if not timeform.is_functional():
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/dolfin-adjoint/dolfin_adjoint/time
    if term.form.arguments():
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/form.py", line 225, in arg
    self._analyze_form_arguments()
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/form.py", line 451, in _an
    arguments, coefficients = extract_arguments_and_coefficients(self)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/algorithms/analysis.py", l
    terminals = extract_type(a, FormArgument)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/algorithms/analysis.py", l
    return set(o for e in iter_expressions(a)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/algorithms/analysis.py", l
    for o in traverse_unique_terminals(e)
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/corealg/traversal.py", lin
    if expr not in visited:
  File "/Volumes/Scratch/Users/paalbert/Documents/FIREDRAKE/firedrakeAdjoint/src/ufl/ufl/core/compute_expr_hash.py"
    if expr._hash is not None:
AttributeError: _hash
wence- commented 7 years ago

So we should produce a hash of expressions. This is tricky, we need to do TRT with respect to equality as well.

danshapero commented 4 years ago

Closing this because string expressions have been removed.