SyFi 0.3
|
00001 """This demo program solves Poisson's equation 00002 00003 - div grad u(x, y) = f(x, y) 00004 00005 on the unit square with source f given by 00006 00007 f(x, y) = 500*exp(-((x - 0.5)^2 + (y - 0.5)^2) / 0.02) 00008 00009 and boundary conditions given by 00010 00011 u(x, y) = 0 for x = 0 or x = 1 00012 """ 00013 00014 __author__ = "Anders Logg (logg@simula.no)" 00015 __date__ = "2007-08-16 -- 2008-12-13" 00016 __copyright__ = "Copyright (C) 2007-2008 Anders Logg" 00017 __license__ = "GNU LGPL Version 2.1" 00018 00019 # modified to work with SFC by Kent-Andre Mardal 00020 00021 from dolfin import * 00022 parameters["form_compiler"]["name"] = "sfc" 00023 00024 00025 # Create mesh and define function space 00026 mesh = UnitSquare(32, 32) 00027 V = FunctionSpace(mesh, "CG", 1) 00028 00029 # Define Dirichlet boundary (x = 0 or x = 1) 00030 class DirichletBoundary(SubDomain): 00031 def inside(self, x, on_boundary): 00032 return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS 00033 00034 # Define boundary condition 00035 u0 = Constant(0.0) 00036 bc = DirichletBC(V, u0, DirichletBoundary()) 00037 00038 # Define variational problem 00039 v = TestFunction(V) 00040 u = TrialFunction(V) 00041 f = Expression("500.0 * exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)") 00042 ff = Function(V) 00043 ff.interpolate(f) 00044 a = dot(grad(v), grad(u))*dx 00045 L = v*ff*dx 00046 00047 # Compute solution 00048 problem = VariationalProblem(a, L, bc) 00049 u = problem.solve() 00050 00051 00052 import sys 00053 if (u.vector().norm("l2") - 142.420764968) < 10**-4: 00054 sys.exit(0) 00055 else: 00056 sys.exit(1) 00057 00058