User defined initial condition in heat equation?

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] + alphax[1]x[1] + betat’,
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!

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/
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