| 1 |
"""This demo demonstrate how to assemble a linear system including |
| 2 |
boundary conditions.""" |
| 3 |
|
| 4 |
# Copyright (C) 2008 Kent-Andre Mardal |
| 5 |
# |
| 6 |
# This file is part of DOLFIN. |
| 7 |
# |
| 8 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 9 |
# it under the terms of the GNU Lesser General Public License as published by |
| 10 |
# the Free Software Foundation, either version 3 of the License, or |
| 11 |
# (at your option) any later version. |
| 12 |
# |
| 13 |
# DOLFIN is distributed in the hope that it will be useful, |
| 14 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 |
# GNU Lesser General Public License for more details. |
| 17 |
# |
| 18 |
# You should have received a copy of the GNU Lesser General Public License |
| 19 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 20 |
# |
| 21 |
# Modified by Kristian Oelgaard 2008 |
| 22 |
# Modified by Anders Logg 2011 |
| 23 |
# |
| 24 |
# First added: 2008-08-13 |
| 25 |
# Last changed: 2012-11-12 |
| 26 |
|
| 27 |
from dolfin import * |
| 28 |
import matplotlib.pyplot as plt |
| 29 |
|
| 30 |
# Create mesh and finite element |
| 31 |
mesh = UnitSquareMesh(32, 32) |
| 32 |
V = FunctionSpace(mesh, "CG", 1) |
| 33 |
|
| 34 |
# Source term |
| 35 |
class Source(UserExpression): |
| 36 |
def eval(self, values, x): |
| 37 |
dx = x[0] - 0.5 |
| 38 |
dy = x[1] - 0.5 |
| 39 |
values[0] = 500.0*exp(-(dx*dx + dy*dy)/0.02) |
| 40 |
|
| 41 |
# Neumann boundary condition |
| 42 |
class Flux(UserExpression): |
| 43 |
def eval(self, values, x): |
| 44 |
if x[0] > DOLFIN_EPS: |
| 45 |
values[0] = 25.0*sin(5.0*DOLFIN_PI*x[1]) |
| 46 |
else: |
| 47 |
values[0] = 0.0 |
| 48 |
|
| 49 |
# Sub domain for Dirichlet boundary condition |
| 50 |
class DirichletBoundary(SubDomain): |
| 51 |
def inside(self, x, on_boundary): |
| 52 |
return on_boundary and x[0] < DOLFIN_EPS |
| 53 |
|
| 54 |
# Define variational problem |
| 55 |
u = TrialFunction(V) |
| 56 |
v = TestFunction(V) |
| 57 |
f = Source(degree=2) |
| 58 |
g = Flux(degree=2) |
| 59 |
a = inner(grad(u), grad(v))*dx |
| 60 |
L = f*v*dx + g*v*ds |
| 61 |
|
| 62 |
# Define boundary condition |
| 63 |
u0 = Constant(0.0) |
| 64 |
bc = DirichletBC(V, u0, DirichletBoundary()) |
| 65 |
|
| 66 |
# Compute solution |
| 67 |
u = Function(V) |
| 68 |
solve(a == L, u, bc) |
| 69 |
|
| 70 |
# Save solution to file |
| 71 |
file = File("poisson.pvd") |
| 72 |
file << u |
| 73 |
|
| 74 |
# Plot solution |
| 75 |
plot(u) |
| 76 |
plt.show() |