Dear community,
I recently switched from dolfin to dolfinx and I am trying to udpate the following “Expression” class.
V = VectorFunctionSpace(mesh,'Lagrange', 1)
U_app = Expression( ('t', '0.'), t = 0.0, degree = 1 ) # Constant((0.,0.))
bcu = [DirichletBC( V , Constant((0.,0.)), "near(x[1],0) && on_boundary") ,
DirichletBC( V , U_app, "near(x[1],1.) && on_boundary" )]
In the following script, I tried to follow what it already did in the discussion https://fenicsproject.discourse.group/t/equivalent-for-expression-in-dolfinx/2641/12, but I met trouble at the interpolation :
import ufl
import ffcx
import matplotlib.pyplot as plt
import numpy as np
from dolfinx import log, io, mesh, fem, cpp
from mpi4py import MPI
from petsc4py.PETSc import ScalarType
log.set_log_level(log.LogLevel.OFF)
comm = MPI.COMM_WORLD
msh = mesh.create_unit_square(MPI.COMM_WORLD, 151, 151, mesh.CellType.quadrilateral)
ndim = msh.topology.dim
# Time discretization
T = 0.02 # final time
num_steps = 200 # number of time steps
dt = T / num_steps # time step size
time_step = np.linspace(0, T, num_steps)
# BC
V_u = fem.VectorFunctionSpace(msh,('CG', 1))
V_alpha = fem.FunctionSpace(msh,('CG', 1))
class MyExpression:
def __init__(self):
self.t = 0.0
def evaluate(self, x):
return np.full(x.shape[1], self.t)
u_app = MyExpression()
u_app.t = 0.0
u_app_func = fem.Function(V_u)
# len(u_app_func.x.array) = 46208 = 2* len(msh.geometry.x)
#u_app_func = fem.Function(V_alpha)
u_app_func.interpolate(u_app.evaluate)
boundaries = [(1, lambda x: np.isclose(x[0], 0)),
(2, lambda x: np.isclose(x[1], 1))]
facet_indices, facet_markers = [], []
bcu = []
fdim = ndim - 1
for (marker, locator) in boundaries:
facets = mesh.locate_entities(msh, fdim, locator)
if marker == 1:
facets_dofs = fem.locate_dofs_geometrical(V_u,locator)
u_zero = np.array((0. ,)*msh.geometry.dim, dtype=ScalarType) # print(u_zeros) = [0.0, 0.]
bc = fem.dirichletbc( u_zero,facets_dofs, V_u)
if marker == 2:
facets_dofs = fem.locate_dofs_topological(V_u.sub(0), fdim, marker)
bc = fem.dirichletbc( u_app_func, facets_dofs,V_u.sub(0))
bcu.append(bc)
facet_indices.append(facets)
facet_markers.append(np.full(len(facets), marker))
facet_indices = np.array(np.hstack(facet_indices), dtype=np.int32)
facet_markers = np.array(np.hstack(facet_markers), dtype=np.int32)
sorted_facets = np.argsort(facet_indices)
facet_tag = mesh.meshtags(msh, fdim, facet_indices[sorted_facets], facet_markers[sorted_facets])
I get an error about the dimension of the array :
Traceback (most recent call last):
File "/stck/lmersel/CODE_PROGRAMME/anaconda3/envs/fenics/lib/python3.10/site-packages/dolfinx/fem/function.py", line 318, in _interpolate
self._cpp_object.interpolate(u._cpp_object, cells)
AttributeError: 'function' object has no attribute '_cpp_object'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "/scratchm/lmersel/FEniCSx/mesh_quad_newton_stagg_AT2_dlfx/test.py", line 47, in <module>
u_app_func.interpolate(u_app.evaluate)
File "/stck/lmersel/CODE_PROGRAMME/anaconda3/envs/fenics/lib/python3.10/site-packages/dolfinx/fem/function.py", line 336, in interpolate
_interpolate(u, cells)
File "/stck/lmersel/CODE_PROGRAMME/anaconda3/envs/fenics/lib/python3.10/functools.py", line 889, in wrapper
return dispatch(args[0].__class__)(*args, **kw)
File "/stck/lmersel/CODE_PROGRAMME/anaconda3/envs/fenics/lib/python3.10/site-packages/dolfinx/fem/function.py", line 320, in _interpolate
self._cpp_object.interpolate(u, cells)
RuntimeError: Expected 2D array of data
I checked the length of u_app_func = fem.Function(V_u)
and I noticed that it is twice the number of node even if I try u_app_func.sub(0)
or u_app_func.sub(1)
In summary, I just want to apply ux(t)=t and uy = 0 on the top edge of the square.
Could you help me to understand the mistake ?
Thanks,
Best regards,
Lamia