Solving the adjoint problem with respect the variable which isnt explicitly in equation

Asked by Bahram

I am trying to solve an optimization problem with dolfin- adjoint. my Elastic madules disribution is a RBF function and it is a function of the coordinate. when I try to find one of this parameters I recive this error.

Traceback (most recent call last):
  File "/home/bahram/SampleWork/Adjoint/inverseprobelmRBF.py", line 134, in <module>
    tol=2e-044, bounds = [(0,0.01),(0.1,1),(0,1),(0,1)], options = {"disp": True})
  File "/usr/lib/python2.7/dist-packages/dolfin_adjoint/optimization.py", line 170, in minimize
    opt = algorithm(reduced_func.eval_array, dj, [p.data() for p in reduced_func.parameter], H = reduced_func.hessian_array, **kwargs)
AttributeError: 'float' object has no attribute 'data'

my goal is to find the XCenter which is in the experesion which define E. the

 I can solve this problem with the constant E but I cant solve it when E is function.here is the code : the first part of the problem is forward problem and the second part is inverse problem.

from __future__ import division
from dolfin import *
from dolfin_adjoint import *
import pickle
import numpy, scipy.io
import csv
print scipy.__version__
parameters["num_threads"] = 2;
#######FORWARD PROBLEM

Magnitude=-1000000000000000
po=2700
nu = 0.3
NumMeshX=100
NumMeshY=NumMeshX
NumMeshZ=1
Divide=1/NumMeshX
NumofSensor=10
DivideSensor=1/NumofSensor
lengthplate=1
LoadVector = 0.7
BCLocVec=0.6
w=[100]
matrixH = numpy.zeros((len(w),121))
mesh=UnitSquareMesh(NumMeshX, NumMeshY)
V = VectorFunctionSpace(mesh, "Lagrange", 1)
XCenterT=0.4
YCenterT=0.4
DepthT=0.001;
PercentT=0.9;
E0T=69000000000-69000000000*PercentT;

for ww in range(0,len(w)):
                LoadLoc=LoadVector
                BCLoc=BCLocVec

                class Bottom(SubDomain):
                    def inside(self, x, on_boundary):
                        return (near(x[1], 0.0) and abs(x[0]) < BCLoc)

                class Top(SubDomain):
                    def inside(self, x, on_boundary):
                        return (near(x[1], 1.0) and abs(x[0] - LoadLoc) < 0.1)

                top = Top()
                bottom = Bottom()
                # Initialize mesh function for boundary domains
                boundaries = FacetFunction("uint", mesh)
                boundaries.set_all(0)
                top.mark(boundaries, 2)
                bottom.mark(boundaries, 4)
                bc = DirichletBC(V, (0.0, 0.0), boundaries,4)
                # Define new measures associated with the interior domains and exterior boundaries
                ds = Measure("ds")[boundaries]
                # Define trial and test functions
                u1 = TrialFunction(V)
                v = TestFunction(V)
                f = Expression(("0.0", "scale"), w0 = w[ww], scale = Magnitude )
                E1=E=Expression( ("Base+Base*P*round((x[0]-aa)*10)*round((x[1]-bb)*10)"), aa= XCenterT,bb=YCenterT,Base=E0T,C=DepthT,P=PercentT)
                mu1 = E1 / (2.0*(1.0 + nu))
                lmbda = E1*nu / ((1.0 + nu)*(1.0 - 2.0*nu))
                def sigma(u):
                    return 2.0*mu1*sym(grad(u)) + lmbda*tr(sym(grad(u1)))*Identity(v.cell().d)
                a = inner(sigma(u1), sym(grad(v)))*dx -po*w[ww]*w[ww] *inner(v,u1)*dx
                L = inner(f,v)*ds(2)
                        # Compute solution

                u1 = Function(V)
                solve(a == L, u1, bc)

#####INVERSE PROBLEM####################################################################
#######################################################################################

XCenter=Constant(0.1)
YCenter=Constant(0.2)
Depth=0.005;
Percent=0.1;
E0=69000000000-69000000000*PercentT;

for ww in range(0,len(w)):
                LoadLoc=LoadVector
                BCLoc=BCLocVec

                class Bottom(SubDomain):
                    def inside(self, x, on_boundary):
                        return (near(x[1], 0.0) and abs(x[0]) < BCLoc)

                class Top(SubDomain):
                    def inside(self, x, on_boundary):
                        return (near(x[1], 1.0) and abs(x[0] - LoadLoc) < 0.1)

                top = Top()
                bottom = Bottom()
                # Initialize mesh function for boundary domains
                boundaries = FacetFunction("uint", mesh)
                boundaries.set_all(0)
                top.mark(boundaries, 2)
                bottom.mark(boundaries, 4)
                bc = DirichletBC(V, (0.0, 0.0), boundaries,4)
                # Define new measures associated with the interior domains and exterior boundaries
                ds = Measure("ds")[boundaries]
                # Define trial and test functions
                u2 = TrialFunction(V)
                v = TestFunction(V)
                f = Expression(("0.0", "scale"), w0 = w[ww], scale = Magnitude )
               # here is the term that Cause the problem##########
               E=Expression( ("Base+Base*P*round((x[0]-aa)*10)*round((x[1]-bb)*10)"), aa= XCenter,bb=YCenter,Base=E0,C=Depth,P=Percent)
                mu = E / (2.0*(1.0 + nu))
                lmbda = E*nu / ((1.0 + nu)*(1.0 - 2.0*nu))
                def sigma(u2):
                    return 2.0*mu*sym(grad(u2)) + lmbda*tr(sym(grad(u2)))*Identity(v.cell().d)
                a = inner(sigma(u2), sym(grad(v)))*dx -po*w[ww]*w[ww] *inner(v,u2)*dx
                L = inner(f,v)*ds(2)
                        # Compute solution

                u2 = Function(V)
                parameters["adjoint"]["test_derivative"] = True
                solve(a == L, u2, bc)
cc=u2-u1
print cc
J = Functional(inner(cc,cc)*dx)
reduced_functional = ReducedFunctional(J, ScalarParameter(XCenter))

m_opt = minimize(reduced_functional, method = "L-BFGS-B",
                 tol=2e-044, bounds = (0,1), options = {"disp": True})

plot(m_opt, mesh,interactive=True, title="Control")

any suggestion how can I solve this Error?

Question information

Language:
English Edit question
Status:
Solved
For:
dolfin-adjoint Edit question
Assignee:
No assignee Edit question
Solved by:
Simon Funke
Solved:
Last query:
Last reply:
Revision history for this message
Best Simon Funke (simon-funke) said :
#1

Hi Barhan,

currently dolfin-adjoint can not compute the functional derivative with respect to parameters that are part of an expression (as in your case XCenter in the expression "E"). The problem is that Expression are not available symbolically (in contrast to ufl), and hence dolfin_adjoint does not know how to take its derivative.

One workaround for you is to replace the expression "E" by a ufl formula, i.e. replace your "E" Expression with:

                x = triangle.x
                E = E0 + E0*Percent*(x[0]-XCenter)*10*(x[1]-YCenter)*10

Hope this helps,

Simon

Revision history for this message
Bahram (behinoo) said :
#2

Thank you so much. it solved my problem.