This is a read only copy of the old FEniCS QA forum. Please visit the new QA forum to ask questions

inner product

0 votes

Dear all,
i tried to calculate inner product between this function, but i I can not understand the type of error:

# Mesh

mesh = UnitSquareMesh(20,20)

#Function space

if version(dolfin_version()) < version("1.7.0"):
    V = VectorFunctionSpace(mesh, "Lagrange", 2)
    Q = FunctionSpace(mesh, "Lagrange", 1)
    W = MixedFunctionSpace([V, Q])
else:
V_element = VectorElement('Lagrange', mesh.ufl_cell(), 2)
Q_element = FiniteElement('Lagrange', mesh.ufl_cell(), 1)
W_element = MixedElement(V_element, Q_element)
W = FunctionSpace(mesh, W_element)

# Boundary conditions
noslip = DirichletBC(W.sub(0), (0, 0), "x[0] < DOLFIN_EPS || x[0] > 1.0 - DOLFIN_EPS || x[1] < DOLFIN_EPS")
lid  = DirichletBC(W.sub(0), (-1,0), "x[1] > 1.0 - DOLFIN_EPS")
pref = DirichletBC(W.sub(1), 0, "x[0] < DOLFIN_EPS && x[1] < DOLFIN_EPS", "pointwise")
bc_list = [noslip, lid, pref]

u = TrialFunction(W.sub(0).collapse())
v = TestFunction(W.sub(0).collapse())
phi_i = Function(W.sub(0).collapse())
phi_j = Function(W.sub(0).collapse())
phi_k = Function(W.sub(0).collapse())

#Scalar product

S_U = assemble(inner(u,v)*dx + inner(grad(u), grad(v))*dx)

#define matrix Br
Br = np.zeros((Nc,Nc), dtype=np.float)
Cr = np.zeros((Nmax,Nmax,Nmax), dtype=np.float)


#at this point i introduce a matrix: Z_U(Nr x Nc) containing basis functions, previously calculated

for i in range(Nc):
    phi_j.vector()[:]=np.array(Z_U[:, j],dtype=np.float_) # extract basis function
        for i in range(Nmax):
            phi_i.vector()[:]=np.array(Z_U[:, i],dtype=np.float_)
            Br[j,i] = grad(phi_j).vector().inner(S_U*grad(phi_i).vector())


for j in range(Nc):
        phi_j.vector()[:]=np.array(Z_U[:, j],dtype=np.float_)
        for k in range(Nc):
            phi_k.vector()[:]=np.array(Z_U[:, k],dtype=np.float_)
            for i in range(Nc):
                phi_i.vector()[:]=np.array(Z_U[:, i],dtype=np.float_)
                #Cr[j,k,i] = assemble(inner(phi_j,div(phi_k)*phi_i)*dx)
                Cr[j,k,i] = phi_j.vector().inner(S_U*div(phi_k)*phi_i)

AttributeError: 'Grad' object has no attribute 'vector'

Best reguards.

Francesco

asked Oct 29, 2016 by Francesco_nuke_91 FEniCS Novice (280 points)

1 Answer

0 votes

The result of grad(phi_j) is not a Function anymore, so you cannot expect it to have a vector() method.

However, you can use assemble, e.g.:

   Br[j,i] = assemble(inner(grad(phi_j), grad(phi_i))*dx)
answered Oct 29, 2016 by francesco.ballarin FEniCS User (4,070 points)
...