Initialize function to 0 in dolfinx

Hello,

I am working on a 2D hyperelasticity problem.

L = 1. # total length
d = L/10. # thickness
h = d/6. # size of a cell

my_domain = dolfinx.mesh.create_rectangle(comm=MPI.COMM_WORLD,
                            points=((0.0, -0.5*d), (L, 0.5*d)), n=(int(L/h), int(d/h)),
                            cell_type=dolfinx.mesh.CellType.triangle)
V = dolfinx.fem.VectorFunctionSpace(my_domain, ("CG", 2))
u = dolfinx.fem.Function(V)

I used to initialize my displacement function to zero using

u.interpolate(dolfinx.fem.Constant((0.,0.)))

But this now yields an error

TypeError: __init__() missing 1 required positional argument: 'c'

Many thanks in advance for your help with this!

You need to provide the domain to dolfinx.fem.Constant, i.e.

u.interpolate(dolfinx.fem.Constant(my_domain, (0., 0.)))

See dolfinx/function.py at main · FEniCS/dolfinx · GitHub

1 Like

thanks! now I get this strange warning:

WARNING:UFL:Couldn't map 'c_4' to a float, returning ufl object without evaluation.

I’ll try to write a minimal code to reproduce the error…

Did you take care to be consistent between real and complex numbers ? If you have PETSc compiled for real numbers, and feed it a complex it will understandably complain.

If you simply want to initialize it to zero, I would suggest
u.x.set(0)

3 Likes