Hello everyone, while studying “Defining subdomains for different materials” in Defining subdomains for different materials — FEniCSx tutorial, I encountered an issue. The code is as follows:
from dolfinx import default_scalar_type
from dolfinx.fem import (Constant, dirichletbc, Function, functionspace, assemble_scalar,
form, locate_dofs_geometrical, locate_dofs_topological)
from dolfinx.mesh import create_unit_square, locate_entities, create_unit_cube
from mpi4py import MPI
import numpy as np
num_mesh = 10
mesh = create_unit_square(MPI.COMM_WORLD, num_mesh, num_mesh)
Q = functionspace(mesh, ("DG", 0))
def Omega_0(x):
return x[1] <= 0.5
def Omega_1(x):
return x[1] >= 0.5
kappa = Function(Q)
cells_0 = locate_entities(mesh, mesh.topology.dim, Omega_0)
print('cells_0', len(cells_0))
cells_1 = locate_entities(mesh, mesh.topology.dim, Omega_1)
print('cells_1', len(cells_1))
# print("kappa array before assignment:", kappa.x.array)
kappa.x.array[cells_0] = np.full_like(cells_0, 1, dtype=default_scalar_type)
kappa.x.array[cells_1] = np.full_like(cells_1, 0.1, dtype=default_scalar_type)
# print("kappa array after assignment:", kappa.x.array)
print('length of the array', len(kappa.x.array))
print('total number of cells', len(cells_0) + len(cells_1))
if np.any(kappa.x.array == 0):
print("Warning: Some cells have not been assigned values.")
I found that when num_mesh is even, the values in kappa.x.array are all correctly set. However, when num_mesh is odd, some values in kappa.x.array remain 0 and are not set correctly. Then, I used gmsh to generate the mesh for testing, and the problem still persists. The elements in kappa.x.array are not all set to the correct values, which may cause errors in subsequent calculations. My test code is as follows:
import gmsh
import numpy as np
from dolfinx.io import XDMFFile, gmshio
from mpi4py import MPI
from dolfinx.mesh import locate_entities
from dolfinx import fem, default_scalar_type
len_side = 50
lc = 8
split = len_side / 2
def get_device():
# Initialize Gmsh
gmsh.initialize()
model = gmsh.model()
# Create a new model
model.add("Device")
p1 = model.occ.addPoint(0, 0, 0, lc, 1)
p2 = model.occ.addPoint(len_side, 0, 0, lc, 2)
p3 = model.occ.addPoint(len_side, len_side, 0, lc, 3)
p4 = model.occ.addPoint(0, len_side, 0, lc, 4)
line1 = model.occ.addLine(p1, p2, 1)
line2 = model.occ.addLine(p2, p3, 2)
line3 = model.occ.addLine(p3, p4, 3)
line4 = model.occ.addLine(p4, p1, 4)
loop = model.occ.addCurveLoop([line1, line2, line3, line4], 5)
surface = model.occ.addPlaneSurface([loop], 1)
model.occ.synchronize()
model.add_physical_group(dim=2, tags=[surface])
# Generate the 3D mesh
model.mesh.generate(2)
return model
def volume0(x):
return x[1] >= split
def volume1(x):
return x[1] <= split
model = get_device()
# Interfacing with dolfinx
mesh, cell_markers, facet_markers = gmshio.model_to_mesh(model, MPI.COMM_WORLD, 0, gdim=2)
Q = fem.functionspace(mesh, ("DG", 0))
epsilon = fem.Function(Q)
cells0 = locate_entities(mesh, mesh.topology.dim, volume0)
print('cells1', len(cells0), MPI.COMM_WORLD.rank)
cells1 = locate_entities(mesh, mesh.topology.dim, volume1)
print('cells2', len(cells1), MPI.COMM_WORLD.rank)
print('length of array', len(epsilon.x.array), MPI.COMM_WORLD.rank)
print('total', len(cells0) + len(cells1), MPI.COMM_WORLD.rank)
if len(cells0) + len(cells1) == len(epsilon.x.array):
print("All cells are included.")
else:
print("Some cells are missing or duplicated.")
epsilon.x.array[cells0] = np.full_like(cells0, 1, dtype=default_scalar_type)
epsilon.x.array[cells1] = np.full_like(cells1, 2, dtype=default_scalar_type)
if np.any(epsilon.x.array == 0):
print("Warning: Some cells have not been assigned values.")
Could you please help me identify the issue in my code?