Unable to set Initial Conditions for vector in a subdomain

Dear Community,

I’m encountering an issue while trying to set initial conditions for a vector within a specific subdomain of the mesh. I’m working on a project where I need to define different initial conditions for different subdomains, but I’m having trouble achieving this. I have used a Mixed Element to define a vector and a scalar. The method, which I have used to set initial condition for the scalar worked well, but it doesn’t work for the setting of vector.

Here’s the part of my code:

import numpy as np
import ufl
from dolfinx import default_scalar_type
from dolfinx.mesh import *
from dolfinx.mesh import CellType, locate_entities
from dolfinx.fem import (Function, functionspace)
from mpi4py import MPI
from basix.ufl import element
from petsc4py import PETSc

##### Mesh Part #####

domain = create_unit_square(MPI.COMM_WORLD, 5, 5, CellType.triangle)
# Mixed element
P2 = element("Lagrange", domain.basix_cell(), 1, shape = (2,))    # Mixed element for tensor , shape = (2,)
P1 = element("Lagrange", domain.basix_cell(), 1)

# Functionspace for mixed element
TH = ufl.MixedElement([P2, P1])
ME = functionspace(domain, TH)

v_u, v_pi = ufl.TestFunctions(ME)
Variable = Function(ME)
u, pi = ufl.split(Variable)

##### Subdomains #####

Omega = {}
Omega[0] = lambda x : np.logical_and(x[0] >= 0.5, x[0] <= 1.0)
Omega[1] = lambda x : np.logical_and(x[0] >= 0.0, x[0] <= 0.5)
Subdomain1 = locate_entities(domain, domain.topology.dim, Omega[0])

##### Initial Conditions #####

V2 = functionspace(domain, P2)
Value = {}
Value[0] = Function(V2)

initial_u = PETSc.ScalarType((0, 0))
Value[0].x.array[Subdomain1]= np.full_like(Subdomain1, initial_u, dtype=default_scalar_type)

Variable.sub(0).interpolate(Value[0])
Variable.x.scatter_forward()

Here’s the Error I got:

ValueError: could not broadcast input array from shape (2,) into shape (20,)

I’ve searched through the entire Dolfinx tutorial and tried lots of methods but I couldn’t find a solution to my problem. I would greatly appreciate any suggestions on how to approach this problem.

Thank you in advance for your help!

Consider the following MWE (using non-zero data to illustrate where it is placed in the array):

import numpy as np
import ufl
from dolfinx import default_scalar_type
from dolfinx.mesh import CellType, locate_entities, create_unit_square
from dolfinx.fem import (Function, functionspace)
from mpi4py import MPI
from basix.ufl import element

##### Mesh Part #####

domain = create_unit_square(MPI.COMM_WORLD, 5, 5, CellType.triangle)
# Mixed element
P2 = element("Lagrange", domain.basix_cell(), 1, shape = (2,))    # Mixed element for tensor , shape = (2,)
P1 = element("Lagrange", domain.basix_cell(), 1)

# Functionspace for mixed element
TH = ufl.MixedElement([P2, P1])
ME = functionspace(domain, TH)

v_u, v_pi = ufl.TestFunctions(ME)
Variable = Function(ME)
u, pi = ufl.split(Variable)

##### Subdomains #####

Omega = {}
Omega[0] = lambda x : np.logical_and(x[0] >= 0.5, x[0] <= 1.0)
Omega[1] = lambda x : np.logical_and(x[0] >= 0.0, x[0] <= 0.5)
Subdomain1 = locate_entities(domain, domain.topology.dim, Omega[0])

##### Initial Conditions #####

V2 = functionspace(domain, P2)
Value = {}
Value[0] = Function(V2)

initial_u = (10, 13)
def initial_condition(x):
    values = np.zeros((2, x.shape[1]), dtype=default_scalar_type)
    values[0] = initial_u[0]
    values[1] = initial_u[1]
    return values
Value[0].interpolate(initial_condition, cells=Subdomain1)
Value[0].x.scatter_forward()
print(Value[0].x.array)

Variable.sub(0).interpolate(Value[0])
Variable.x.scatter_forward()

Thank you! Now the problem is solved.