### A simple hello word in FEniCS?

289

views

0

I am trying to start using `FEniCS`, but have a problem with the simple hello world examples given in the books. Could you please give me the simplest hello world example for solving the Fick's second law.

$\frac{\partial c}{\partial t}=D\frac{\partial^2c}{\partial x^2}$∂

How can we apply the boundary condition to the simplest example of `FEniCS` for solving this PDE (e.g., drawing the concentration profiles at given times)?

$\frac{\partial c}{\partial t}=D\frac{\partial^2c}{\partial x^2}$∂

`c`∂`t`=`D`∂^{2}`c`∂`x`^{2}How can we apply the boundary condition to the simplest example of `FEniCS` for solving this PDE (e.g., drawing the concentration profiles at given times)?

Community: FEniCS Project

### 2 Answers

0

0

you can do something like this:

```
from fenics import *
mesh = UnitSquareMesh(20,20)
D = 1.0
V = FunctionSpace(mesh, 'CG', 1)
# for previous solution
W_ = Function(V)
#------------------------------------------------------
# Classes
#------------------------------------------------------
class Left(SubDomain):
def inside(self,x,on_boundary):
tol = 1e-10
return (near(x[0],0.0)) and on_boundary
left = Left()
#--------------------------------------------------------------
# Applying BC
#--------------------------------------------------------------
lc = DirichletBC(V,Constant(1.0),left)
bc_c = [lc]
#---------------------------------------------------
# time steps...
#---------------------------------------------------
deltaT = 1e-3
t_ini = 0
t_fin = 0.5
t = 0
dt = 1e-3
u,v = TrialFunction(V), TestFunction(V)
du = Function(V)
while t < t_fin:
F = (inner(grad(u), D*grad(v)) + (1./deltaT)*u*v
- (1./deltaT)*v*W_ )*dx
solve(lhs(F)==rhs(F), du, bc_c)
W_.assign(du)
t+=dt
plot(du, key = 'du', title = '%f' %(t))
interactive()
```

Please login to add an answer/comment or follow this question.