Skip to content

Instantly share code, notes, and snippets.

Created February 25, 2014 09:42
Show Gist options
  • Save anonymous/9205919 to your computer and use it in GitHub Desktop.
Save anonymous/9205919 to your computer and use it in GitHub Desktop.
Simple 1D advection-diffusion problem in FEniCS.
"""Steady-state diffusion equation,
- div grad u(x) = 0
and boundary conditions given by
u(0) = 1 (inflow)
u(1) = 0 (outflow).
"""
from dolfin import *
import numpy as np
# Create mesh and define function space
mesh = UnitIntervalMesh(50)
V = FunctionSpace(mesh, "CG", 1)
# Location of Dirichlet boundaries,
# x = 0, x = 1.
def left_boundary(x):
return np.isclose(x[0], 0.0)
def right_boundary(x):
return np.isclose(x[0], 1.0)
# Define boundary condition
# u(0) = 1, u(1) = 0
bc_left = DirichletBC(V, Constant(1.0), left_boundary)
bc_right = DirichletBC(V, Constant(0.0), right_boundary)
# Define variational problem
u = TrialFunction(V)
v = TestFunction(V)
f = Expression("0")
g = Expression("0")
# advection term diffusion term
a = div(Constant(100)*grad(u))*v*dx + inner(Constant(1.0)*grad(u), grad(v))*dx
L = f*v*dx + g*v*ds
# Compute solution
u = Function(V)
solve(a == L, u, bcs=[bc_left, bc_right])
# Save solution in VTK format
file = File("diffusion.pvd")
file << u
# Plot solution
plot(u, interactive=True)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment