# How to define regional initial condition in latest dolfinx?

**URL:** <https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349>\
**Category:** dolfinx\
**Created:** [November 21, 2024, 3:10am UTC](https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349 "2024-11-21T03:10:52Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Deep\_Math](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/deep_math/32/6225_2.png) [@Deep\_Math](https://fenicsproject.discourse.group/u/Deep_Math)\
**Post date:** [November 21, 2024, 3:10am UTC](https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349/1 "2024-11-21T03:10:52Z")

</div>

```auto
import dolfinx
from basix.ufl import element
from dolfinx import mesh, fem, io
from dolfinx.fem import functionspace
from mpi4py import MPI
import numpy as np
from petsc4py import PETSc
import ufl
from mpi4py import MPI
from petsc4py import PETSc

import numpy as np

from basix.ufl import element, mixed_element
from dolfinx import fem, io, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import (Measure, SpatialCoordinate, TestFunctions, TrialFunctions,
                 div, exp, inner)
from mesh_and_plot import create_nested_rectangles

# Time stepping parameters
num_steps = 100
dt = 1 / num_steps
max_iter = 10
tol = 1e-6
domain, markers = create_nested_rectangles()
# Physical constants
R = fem.Constant(domain, np.float64(287.05))
c_v = fem.Constant(domain, np.float64(718.0))
c_p = fem.Constant(domain, np.float64(1005.0))
mu = fem.Constant(domain, np.float64(1.81e-5))
kappa = fem.Constant(domain, np.float64(0.025))

# Create mesh
length = 0.4
height = 0.4
nx = 40
ny = 40

# Create mesh and markers using the previously defined function

# Define function spaces
# Create elements
P1_pressure = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
P1_velocity = element("Lagrange", domain.basix_cell(), degree=1, shape=(domain.geometry.dim,), dtype=dolfinx.default_real_type)
P1_temperature = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
V_u, V_p, V_T = functionspace(domain, P1_velocity), functionspace(domain, P1_pressure),functionspace(domain, P1_temperature)
V_fluid = mixed_element([P1_pressure, P1_velocity, P1_temperature])
V_fluid_fs=functionspace(domain,V_fluid)
p_init = fem.Function(V_p)
u_init = fem.Function(V_u)
T_init = fem.Function(V_T)

```

this is the code that defines function space and mixed element space, then I want to define an initial condition, for example, for p\_init, such that when in a Circular Region( Definition:A circular region is defined with its center at (0.0, 0.2) and a radius of 0.06.This is determined by the equation: (x - center\_x)^2 + (y - center\_y)^2 \< radius^2), the pressure in this circular region is high pressure (6,746,268.65 Pa),Outside the circle is the Low pressure (100,000 Pa), how to do this in latest version of dolfinx?

---

<div class="post-metadata">

**Author:** ![Deep\_Math](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/deep_math/32/6225_2.png) [@Deep\_Math](https://fenicsproject.discourse.group/u/Deep_Math)\
**Post date:** [November 21, 2024, 3:54am UTC](https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349/2 "2024-11-21T03:54:00Z")

</div>

```auto
import dolfinx
from basix.ufl import element
from dolfinx import mesh, fem, io
from dolfinx.fem import functionspace
from mpi4py import MPI
import numpy as np
from petsc4py import PETSc
import ufl
from mpi4py import MPI
from petsc4py import PETSc

import numpy as np

from basix.ufl import element, mixed_element
from dolfinx import fem, io, mesh
from dolfinx.fem.petsc import LinearProblem
from ufl import (Measure, SpatialCoordinate, TestFunctions, TrialFunctions,
                 div, exp, inner)
from mesh_and_plot import create_nested_rectangles

# Time stepping parameters
num_steps = 100
dt = 1 / num_steps
max_iter = 10
tol = 1e-6
domain, markers = create_nested_rectangles()
# Physical constants
R = fem.Constant(domain, np.float64(287.05))
c_v = fem.Constant(domain, np.float64(718.0))
c_p = fem.Constant(domain, np.float64(1005.0))
mu = fem.Constant(domain, np.float64(1.81e-5))
kappa = fem.Constant(domain, np.float64(0.025))

# Create mesh
length = 0.4
height = 0.4
nx = 40
ny = 40

# Create mesh and markers using the previously defined function

# Define function spaces
# Create elements
P1_pressure = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
P1_velocity = element("Lagrange", domain.basix_cell(), degree=1, shape=(domain.geometry.dim,), dtype=dolfinx.default_real_type)
P1_temperature = element("Lagrange", domain.basix_cell(), degree=1, dtype=dolfinx.default_real_type)
V_u, V_p, V_T = functionspace(domain, P1_velocity), functionspace(domain, P1_pressure),functionspace(domain, P1_temperature)
V_fluid = mixed_element([P1_pressure, P1_velocity, P1_temperature])
V_fluid_fs=functionspace(domain,V_fluid)
p_init = fem.Function(V_p)
u_init = fem.Function(V_u)
T_init = fem.Function(V_T)

# Define expressions for each component
# Define initial condition parameters

center_x = 0.0
center_y = 0.2
radius = 0.06
high_pressure = 6746268.65
low_pressure = 100000.0
high_temp = 1465.0
low_temp = 290.0

def initial_condition_expr(x, high_val, low_val, center_x, center_y, radius):
    values = np.zeros(x.shape[1])
    r_squared = (x[0] - center_x)**2 + (x[1] - center_y)**2
    values = np.where(r_squared < radius**2, high_val, low_val)
    return values

# Create functions for individual fields

# Apply initial conditions to individual fields
p_init.interpolate(lambda x: initial_condition_expr(x, high_pressure, low_pressure,
                                               center_x, center_y, radius))
T_init.interpolate(lambda x: initial_condition_expr(x, high_temp, low_temp,
                                               center_x, center_y, radius))
u_init.interpolate(lambda x: np.zeros((2, x.shape[1])))

# Create the mixed function for the complete solution
solution = fem.Function(V_fluid_fs)

# Assign initial values to each component of the mixed function
solution.sub(0).interpolate(p_init) # Pressure component
solution.sub(1).interpolate(u_init) # Velocity component
solution.sub(2).interpolate(T_init) # Temperature component

```

---

<div class="post-metadata">

**Author:** ![Deep\_Math](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/deep_math/32/6225_2.png) [@Deep\_Math](https://fenicsproject.discourse.group/u/Deep_Math)\
**Post date:** [November 21, 2024, 3:54am UTC](https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349/3 "2024-11-21T03:54:15Z")

</div>

I find out how to do this

---

<div class="post-metadata">

**Author:** ![francesco-ballarin](https://avatars.discourse-cdn.com/v4/letter/f/f05b48/32.png) [@francesco-ballarin](https://fenicsproject.discourse.group/u/francesco-ballarin)\
**Post date:** [November 21, 2024, 7:48am UTC](https://fenicsproject.discourse.group/t/how-to-define-regional-initial-condition-in-latest-dolfinx/16349/4 "2024-11-21T07:48:07Z")

</div>

Consider using something similar to [dolfinx/python/test/unit/fem/test\_interpolation.py at 6443e3b27d29aec04ce83d8424b57c339e35f865 · FEniCS/dolfinx · GitHub](https://github.com/FEniCS/dolfinx/blob/6443e3b27d29aec04ce83d8424b57c339e35f865/python/test/unit/fem/test_interpolation.py#L706) , where you call interpolation on a subset of the cells of the mesh.
