# Initial condition on part of the domain

**URL:** <https://fenicsproject.discourse.group/t/initial-condition-on-part-of-the-domain/7294>\
**Category:** variational formulation\
**Created:** [December 9, 2021, 1:19pm UTC](https://fenicsproject.discourse.group/t/initial-condition-on-part-of-the-domain/7294 "2021-12-09T13:19:05Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![JLorenteMacias](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/jlorentemacias/32/4742_2.png) [@JLorenteMacias](https://fenicsproject.discourse.group/u/JLorenteMacias)\
**Post date:** [December 9, 2021, 1:19pm UTC](https://fenicsproject.discourse.group/t/initial-condition-on-part-of-the-domain/7294/1 "2021-12-09T13:19:05Z")

</div>

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.

```auto
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)

```

---

<div class="post-metadata">

**Author:** ![Calva](https://avatars.discourse-cdn.com/v4/letter/c/b9e5f3/32.png) [@Calva](https://fenicsproject.discourse.group/u/Calva)\
**Post date:** [December 9, 2021, 1:32pm UTC](https://fenicsproject.discourse.group/t/initial-condition-on-part-of-the-domain/7294/2 "2021-12-09T13:32:06Z")

</div>

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

```python
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](https://jorgensd.github.io/dolfinx-tutorial/chapter2/diffusion_code.html) for a complete example.

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