Anisotropy on masked cells

I would like to apply an anisotropic thermal conductivity only on a selection of cells in the mesh.

I am trying the following method below but the solver diverges:

VC_mask = dolfinx.mesh.meshtags(mesh, mesh.topology.dim, cells, np.full_like(cells, 2)) # mark everything as isotropic

def marker_VC(x):
    mask = np.isclose(x[0], 2.*x[0].max()) # fill mask with False
    bb_mask = ((x[0] >= 10) & (x[0] <= 15)) & ((x[1] >= 10) & (x[1] <= 13)) & ((x[2] >= 7) & (x[2] <= 14))
    mask = mask | bb_mask
    return mask

cells_VC_loc = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim, marker_VC)

el = ufl.TensorElement("DG", mesh.ufl_cell(), 0)
Q = dolfinx.fem.FunctionSpace(mesh, el)
bs = Q.dofmap.bs
k = dolfinx.fem.Function(Q)
# isotropic thermral conductivity value
kx = 1
# anisotropic thermral conductivity value
kx_vc = 10
ky_vc = 20
kz_vc = 30

def isotropic(x):
    values = np.zeros((3, 3, x.shape[1]), dtype=np.float64)
    values[0, 0] = kx
    values[1, 1] = kx
    values[2, 2] = kx
    return values.reshape((bs, x.shape[1]))
            
def anisotropic(x):
     values = np.zeros((3, 3, x.shape[1]), dtype=np.float64)
     values[0, 0] = kx_vc
     values[1, 1] = ky_vc
     values[2, 2] = kz_vc
     return values.reshape((bs, x.shape[1]))

# ISOTROPIC REGION           
cells_notVC = porosity_mask.indices[vapor_chambers_mask.values==2]
k.interpolate(isotropic, cells=cells_notVC)                    

# ANISOTROPIC REGION
cells_VC = vapor_chambers_mask.indices[np.isin(cell_tags.indices, cells_VC_loc)]
k.interpolate(anisotropic, cells=cells_VC)

k.vector.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD)

Could somebody guide me in the right direction please?

Actually I had forgotten to set a 3 components k to the rest of the domain too. So the code above seems to work.

I have just upgraded from Fenicsx 0.5 to Fenicxs 0.9 and I have adapted the code above in this way:

VC_mask = dolfinx.mesh.meshtags(mesh, mesh.topology.dim, cells, np.full_like(cells, 2)) # mark everything as isotropic

def marker_VC(x):
    mask = np.isclose(x[0], 2.*x[0].max()) # fill mask with False
    bb_mask = ((x[0] >= 10) & (x[0] <= 15)) & ((x[1] >= 10) & (x[1] <= 13)) & ((x[2] >= 7) & (x[2] <= 14))
    mask = mask | bb_mask
    return mask

cells_VC_loc = dolfinx.mesh.locate_entities(mesh, mesh.topology.dim, marker_VC)

el = basix.ufl.element(family="DG", cell=mesh.topology.cell_name(), degree=0, shape=(3, 3))
Q = dolfinx.fem.functionspace(mesh, el) 
bs = Q.dofmap.bs
k = dolfinx.fem.Function(Q)
# isotropic thermral conductivity value
kx = 1
# anisotropic thermal conductivity value
kx_vc = 10
ky_vc = 20
kz_vc = 30

def isotropic(x):
    values = np.zeros((3, 3, x.shape[1]), dtype=np.float64)
    values[0, 0] = kx
    values[1, 1] = kx
    values[2, 2] = kx
    return values.reshape((bs, x.shape[1]))
            
def anisotropic(x):
     values = np.zeros((3, 3, x.shape[1]), dtype=np.float64)
     values[0, 0] = kx_vc
     values[1, 1] = ky_vc
     values[2, 2] = kz_vc
     return values.reshape((bs, x.shape[1]))

# ISOTROPIC REGION           
cells_notVC = porosity_mask.indices[VC_mask.values==2]
k.interpolate(isotropic, cells0=cells_notVC, cells1=np.arange(len(cells_notVC)))                    
k.x.petsc_vec.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD)

# ANISOTROPIC REGION
cells_VC = vapor_chambers_mask.indices[np.isin(cell_tags.indices, cells_VC_loc)]
k.interpolate(anisotropic, cells0=cells_VC, cells1=np.arange(len(cells_VC)))
k.x.petsc_vec.ghostUpdate(addv=PETSc.InsertMode.INSERT, mode=PETSc.ScatterMode.FORWARD)

Although the code runs without error, the anisotropic effect seems not to be here anymore, because I don’t see it on the temperature distributions as shown below (from a real application, not directly from the code above).

Is there anything obvious I am missing?

Fenicsx 0.5:

Fenicsx 0.9:

It was a mistake on my side:
using
k.interpolate(anisotropic, cells0=cells_notVC, cells1=np.arange(len(cells_notVC)))
instead of
k.interpolate(isotropic, cells0=cells_notVC, cells1=np.arange(len(cells_notVC)))