| 1 |
"""This demo program solves Poisson's equation |
| 2 |
|
| 3 |
- div grad u(xi) = f(xi) |
| 4 |
|
| 5 |
on a unit interval embedded in 2D and rotated pi/4 radians |
| 6 |
anticlockwise from the x axis, where xi is the distance along the |
| 7 |
interval (ie the domain is 0<xi<1). The source f is given by |
| 8 |
|
| 9 |
f(xi) = 9*pi^2*sin(3*pi*xi) |
| 10 |
|
| 11 |
The boundary conditions are given by |
| 12 |
|
| 13 |
u(xi) = 0 for xi = 0 |
| 14 |
du/dxi = 0 for xi = 1 |
| 15 |
""" |
| 16 |
|
| 17 |
# Copyright (C) 2012 Imperial College London and others. |
| 18 |
# |
| 19 |
# This file is part of DOLFIN. |
| 20 |
# |
| 21 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 22 |
# it under the terms of the GNU Lesser General Public License as published by |
| 23 |
# the Free Software Foundation, either version 3 of the License, or |
| 24 |
# (at your option) any later version. |
| 25 |
# |
| 26 |
# DOLFIN is distributed in the hope that it will be useful, |
| 27 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 28 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 29 |
# GNU Lesser General Public License for more details. |
| 30 |
# |
| 31 |
# You should have received a copy of the GNU Lesser General Public License |
| 32 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 33 |
# |
| 34 |
# Modified by David Ham 2012 |
| 35 |
# |
| 36 |
# First added: 2012-12-04 |
| 37 |
# Last changed: 2012-12-04 |
| 38 |
|
| 39 |
from dolfin import * |
| 40 |
import numpy |
| 41 |
|
| 42 |
# Create mesh and function space |
| 43 |
squaremesh = UnitSquareMesh(50,2) |
| 44 |
|
| 45 |
boundarymesh = BoundaryMesh(squaremesh, "exterior") |
| 46 |
|
| 47 |
# Subdomain to extract bottom boundary. |
| 48 |
class BottomEdge(SubDomain): |
| 49 |
def inside(self, x, on_boundary): |
| 50 |
return near(x[1], 0.0) |
| 51 |
|
| 52 |
mesh = SubMesh(boundarymesh, BottomEdge()) |
| 53 |
|
| 54 |
V = FunctionSpace(mesh, "CG", 1) |
| 55 |
|
| 56 |
class Rotation(object): |
| 57 |
"""Class implementing rotations of a unit interval through the angle theta.""" |
| 58 |
def __init__(self, theta): |
| 59 |
self.theta = theta |
| 60 |
self.mat = self._mat(theta) |
| 61 |
self.invmat = self._mat(-theta) |
| 62 |
|
| 63 |
def _mat(self, theta): |
| 64 |
return numpy.matrix([[numpy.cos(theta), -numpy.sin(theta)], |
| 65 |
[numpy.sin(theta), numpy.cos(theta)]]) |
| 66 |
|
| 67 |
def to_interval(self, x): |
| 68 |
"""Map the point x back to the horizontal line.""" |
| 69 |
return numpy.dot(self.invmat[0,:], x) |
| 70 |
|
| 71 |
def to_interval_c(self): |
| 72 |
"""Return a c expression mapping x back to the line.""" |
| 73 |
return "(x[0]*%f + x[1]*%f)" % (numpy.cos(self.theta), numpy.sin(self.theta)) |
| 74 |
|
| 75 |
def rotate(self, mesh): |
| 76 |
"""Rotate mesh through theta.""" |
| 77 |
|
| 78 |
mesh.coordinates()[:,:] = \ |
| 79 |
numpy.dot(mesh.coordinates()[:,:], self.mat.T) |
| 80 |
|
| 81 |
rotation = Rotation(numpy.pi/4) |
| 82 |
|
| 83 |
rotation.rotate(mesh) |
| 84 |
|
| 85 |
# Sub domain for Dirichlet boundary condition |
| 86 |
class DirichletBoundary(SubDomain): |
| 87 |
def inside(self, x, on_boundary): |
| 88 |
return on_boundary and rotation.to_interval(x) < DOLFIN_EPS |
| 89 |
|
| 90 |
|
| 91 |
# Define variational problem |
| 92 |
u = TrialFunction(V) |
| 93 |
v = TestFunction(V) |
| 94 |
f = Expression("9.0*pi*pi*sin(3.0*pi*%s)" % rotation.to_interval_c(), degree=2) |
| 95 |
g = Expression("3.0*pi*cos(3.0*pi*%s)" % rotation.to_interval_c(), degree=2) |
| 96 |
|
| 97 |
a = dot(grad(u), grad(v))*dx |
| 98 |
L = f*v*dx + g*v*ds |
| 99 |
|
| 100 |
# Define boundary condition |
| 101 |
u0 = Constant(0.0) |
| 102 |
bc = DirichletBC(V, u0, DirichletBoundary()) |
| 103 |
|
| 104 |
# Compute solution |
| 105 |
u = Function(V) |
| 106 |
solve(a == L, u, bc) |
| 107 |
|
| 108 |
# Save solution to file |
| 109 |
file = File("poisson.pvd") |
| 110 |
file << u |