# User defined initial condition in heat equation?

**URL:** <https://fenicsproject.discourse.group/t/user-defined-initial-condition-in-heat-equation/1444>\
**Category:** Uncategorized\
**Created:** [September 1, 2019, 4:10pm UTC](https://fenicsproject.discourse.group/t/user-defined-initial-condition-in-heat-equation/1444 "2019-09-01T16:10:33Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Arjun\_Chakrawal](https://avatars.discourse-cdn.com/v4/letter/a/b4bc9f/32.png) [@Arjun\_Chakrawal](https://fenicsproject.discourse.group/u/Arjun_Chakrawal)\
**Post date:** [September 1, 2019, 4:10pm UTC](https://fenicsproject.discourse.group/t/user-defined-initial-condition-in-heat-equation/1444/1 "2019-09-01T16:10:33Z")

</div>

Hello,

I was trying to run heat equation from the tutorial and have this question.

How to give a user defined initial condition which is just some random 2d data. I mean, I could generate a random number matrix in 2d which I want to give as initial condition.

Here is the code from tutorial:  
nx = ny = 8  
mesh = UnitSquareMesh(nx, ny)  
V = FunctionSpace(mesh, ‘P’, 1)

#Define boundary condition  
u\_D = Expression(‘1 + x[0]_x[0] + alpha_x[1]_x[1] + beta_t’,  
degree=2, alpha=alpha, beta=beta, t=0)

def boundary(x, on\_boundary):  
return on\_boundary

bc = DirichletBC(V, u\_D, boundary)

#Define initial value  
u\_n = interpolate(u\_D, V)  
#u\_n = project(u\_D, V)

So in the above code instead of defining u\_n using Expression and interpolate, I want to provide a matrix. Perhaps something like this:

1 1 1 1 1 1 1 1  
1 1 1 1 1 1 1 1  
1 1 1 1 1 1 1 1  
1 1 1 2 2 2 1 1  
1 1 1 2 2 2 1 1  
1 1 1 1 1 1 1 1  
1 1 1 1 1 1 1 1  
1 1 1 1 1 1 1 1

So how can I do this?

Thanks!

---

<div class="post-metadata">

**Author:** ![dokken](https://yyz2.discourse-cdn.com/free1/user_avatar/fenicsproject.discourse.group/dokken/32/1560_2.png) [@dokken](https://fenicsproject.discourse.group/u/dokken)\
**Post date:** [September 1, 2019, 8:09pm UTC](https://fenicsproject.discourse.group/t/user-defined-initial-condition-in-heat-equation/1444/2 "2019-09-01T20:09:12Z")

</div>

Hi,  
You can do this by creating a `UserExpression`.  
If you are using dolfin\<2018.1.0, see:  
[https://fenicsproject.org/qa/3397/define-a-piecewise-constant-source/](https://fenicsproject.org/qa/3397/define-a-piecewise-constant-source/)  
In newer versions, the syntax for Expressions has slightly changed, see:  
[https://bitbucket.org/fenics-project/dolfin/src/ec57db53f2b13f1768214829e8b4f80fc32205cf/python/demo/undocumented/sym-dirichlet-bc/demo\_sym-dirichlet-bc.py?at=master#demo\_sym-dirichlet-bc.py-35,42](https://bitbucket.org/fenics-project/dolfin/src/ec57db53f2b13f1768214829e8b4f80fc32205cf/python/demo/undocumented/sym-dirichlet-bc/demo_sym-dirichlet-bc.py?at=master#demo_sym-dirichlet-bc.py-35,42)
