Beispiel #1
0
from dolfin_adjoint.utils import homogenize
import ufl.algorithms

if dolfin.__version__ == "1.2.0":
    # work around UFL bug in dolfin 1.2.0
    expand = ufl.algorithms.expand_derivatives
else:
    expand = lambda x: x

parameters["adjoint"]["stop_annotating"] = True

mesh = UnitSquareMesh(10, 10)
Vu = VectorFunctionSpace(mesh, "CG", 2)
Vm = VectorFunctionSpace(mesh, "CG", 1)
bcs = [DirichletBC(Vu, (1.0, 1.0), "on_boundary")]
hbcs = [homogenize(bc) for bc in bcs]

# Work around some UFL bugs -- action(A, x) doesn't like it if A is null
ufl_action = action
def action(A, x):
    A = ufl.algorithms.expand_derivatives(A)
    if len(A.integrals()) != 0: # form is not empty:
        return ufl_action(A, x)
    else:
        return A # form is empty, doesn't matter anyway

def F(u, m):
    u_test = TestFunction(Vu)

    F = (inner(dot(grad(u), u), u_test)*dx +
         inner(grad(u), grad(u_test))*dx +
F = inner(grad(u), grad(v))*dx - v*dx
solve(F == 0, u, bcs)

du = Function(V)
ddu = Function(V)

epss = map(lambda x: interpolate(x, R), [Constant(1000), Constant(500)])
for i, eps in enumerate(epss):
  print "Solving Eikonal with eps == ", eps.vector()[0]
  F = inner(sqrt(inner(grad(u), grad(u))), v)*dx - v*dx + eps*inner(grad(u), grad(v))*dx
  solve(F == 0, u, bcs)

  if eps != epss[-1]: # we're not at the last eps
    # first-order continuation algorithm
    G = e(derivative(F, u, du) + derivative(F, eps, dr)) # tangent linearisation
    solve(G == 0, du, [homogenize(bc) for bc in bcs])
    deps = epss[i+1].vector()[0][0] - epss[i].vector()[0][0] # delta_epsilon

    #To do first-order continuation:
    #u.assign(1.0*u + deps*u) # update our guess of u for the next solve
    #continue

    H =  e(derivative(G, u, ddu) + derivative(G, eps, dr)) # tangent quadraticisation
    solve(H == 0, ddu, [homogenize(bc) for bc in bcs])

    # To do second-order continuation:
    u.assign(1.0*u + deps*du + 0.5*deps*deps*ddu)

dist = File("dist.xml")
dist << u
from dolfin_adjoint.utils import homogenize
import ufl.algorithms

if dolfin.__version__ == "1.2.0":
    # work around UFL bug in dolfin 1.2.0
    expand = ufl.algorithms.expand_derivatives
else:
    expand = lambda x: x

parameters["adjoint"]["stop_annotating"] = True

mesh = UnitSquareMesh(10, 10)
Vu = VectorFunctionSpace(mesh, "CG", 2)
Vm = VectorFunctionSpace(mesh, "CG", 1)
bcs = [DirichletBC(Vu, (1.0, 1.0), "on_boundary")]
hbcs = [homogenize(bc) for bc in bcs]

# Work around some UFL bugs -- action(A, x) doesn't like it if A is null
ufl_action = action
def action(A, x):
    A = ufl.algorithms.expand_derivatives(A)
    if len(A.integrals()) != 0: # form is not empty:
        return ufl_action(A, x)
    else:
        return A # form is empty, doesn't matter anyway

def F(u, m):
    u_test = TestFunction(Vu)

    F = (inner(dot(grad(u), u), u_test)*dx +
         inner(grad(u), grad(u_test))*dx +
F = inner(grad(u), grad(v))*dx - v*dx
solve(F == 0, u, bcs)

du = Function(V)
ddu = Function(V)

epss = [interpolate(x, R) for x in [Constant(1000), Constant(500)]]
for i, eps in enumerate(epss):
  print("Solving Eikonal with eps == ", eps.vector()[0])
  F = inner(sqrt(inner(grad(u), grad(u))), v)*dx - v*dx + eps*inner(grad(u), grad(v))*dx
  solve(F == 0, u, bcs)

  if eps != epss[-1]: # we're not at the last eps
    # first-order continuation algorithm
    G = e(derivative(F, u, du) + derivative(F, eps, dr)) # tangent linearisation
    solve(G == 0, du, [homogenize(bc) for bc in bcs])
    deps = epss[i+1].vector()[0][0] - epss[i].vector()[0][0] # delta_epsilon

    #To do first-order continuation:
    #u.assign(1.0*u + deps*u) # update our guess of u for the next solve
    #continue

    H =  e(derivative(G, u, ddu) + derivative(G, eps, dr)) # tangent quadraticisation
    solve(H == 0, ddu, [homogenize(bc) for bc in bcs])

    # To do second-order continuation:
    u.assign(1.0*u + deps*du + 0.5*deps*deps*ddu)

dist = File("dist.xml")
dist << u