Regarding the use of dolfinx.fem.assemble_scalar

Hello everyone,

I am trying to use the assemble_scalar operation as show below in code.

from dolfinx import fem, plot, mesh, cpp , la 
from mpi4py import MPI
import numpy as np
from ufl import TestFunction, TrialFunction,Measure, grad, dot, inner
import dolfinx
from dolfinx.fem import FunctionSpace, Function, locate_dofs_geometrical, dirichletbc, Constant
from petsc4py.PETSc import ScalarType


length = 200.
num_elem = 1 
mesh = mesh.create_interval(MPI.COMM_WORLD,num_elem,[0., length])

V_u = FunctionSpace(mesh, ('Lagrange', 1)) 
u, du, v = Function(V_u), TrialFunction(V_u), TestFunction(V_u) 
dx = Measure("dx",domain=mesh)

def on_boundary_L(x):		
    return np.isclose(x[0],0.0)

dof_left_u = locate_dofs_geometrical(V_u, on_boundary_L)
bcl = dirichletbc(ScalarType(0), dof_left_u, V_u)

E = Constant(mesh,ScalarType(30000.0)) 

def eps(u):
    return grad(u)

def sigma(u):
    return E*(eps(u))

f_ext = Constant(mesh,0.0)
external_work = dot(f_ext, u) * dx 
elastic_energy = inner(sigma(u), eps(u))*dx

T_E = dolfinx.fem.assemble_scalar(elastic_energy)

I am getting the following error

Can someone please tell me, what I am doing wrong and how I can fix this error.

Thank you,
Manish

manish_07,

You need to wrap elastic_energy with fem.form()

T_E = dolfinx.fem.assemble_scalar(fem.form(elastic_energy))

as per your error message.

-Sven

4 Likes

@Sven Thank you for the help. It worked for me. :slight_smile: