Initial condition on part of the domain

Hi all,

I need to implement an initial condition on part of the domain.
I know how to do that for the full domain, but I am having problems when I try to implement the initial condition on a subdomain.

Below you can see a minimal example of the initial condition that I have implemented for the full domain. I also know the DOFs of the surface where I want to implement this initial condition (represented by the function left).

Does anybody know how I can solve this problem?

Thanks.

import dolfinx.fem as fem
from dolfinx.io import XDMFFile
import ufl
import dolfinx
import numpy as np
from dolfinx.generation import RectangleMesh
from dolfinx.mesh import  locate_entities_boundary
from mpi4py import MPI
from petsc4py import PETSc
t = 0
# Mesh
gdim = 3
mesh = dolfinx.UnitCubeMesh(MPI.COMM_WORLD, 10, 10, 10)
# Element space
v_cg2 = ufl.VectorElement('CG', mesh.ufl_cell(), 2)
V = dolfinx.FunctionSpace(mesh, v_cg2)
# Create initial condition
def initial_condition(x):
    values = np.zeros((gdim, x.shape[1]),dtype=PETSc.ScalarType)
    values[0] = 1
    return values
# Define initial condition for the full domain
u_n = fem.Function(V)
u_n.name = "u_n"
u_n.interpolate(initial_condition)

# DOFS for the left plane: I want to implement the previous initial condition just on these DOFS
def left(x):
    return np.isclose(x[0], -0.5)
dofs = dolfinx.fem.locate_dofs_geometrical(V, left)

You need to use a DirichletBC object to enforce the boundary conditions.

bc = dolfinx.DirichletBC(u_n, dofs, V)

This will effectively apply the BC on the left plane only given your definition of dofs.

See also e.g. Diffusion of a Gaussian function — FEniCSx tutorial for a complete example.

You then need to pass the bc to the problem solver.