Debian Codegraph @

python/demo/undocumented/conditional/demo_conditional.py

raw
1 """This demo program solves Poisson's equation
2
3 - div grad u(x, y) = f(x, y)
4
5 on the unit square with source f given by
6
7 f(x, y) = -1.0 if (x - 0.33)^2 + (y - 0.67)^2 < 0.015
8 5.0 if 0.015 < (x - 0.33)^2 + (y - 0.67)^2 < 0.025
9 -1.0 if (x,y) in triangle( (0.55, 0.05), (0.95, 0.45), (0.55, 0.45) )
10 0.0 otherwise
11
12 and homogeneous Dirichlet boundary conditions.
13 """
14
15 # Copyright (C) 2010 Kristian B. Oelgaard
16 #
17 # This file is part of DOLFIN.
18 #
19 # DOLFIN is free software: you can redistribute it and/or modify
20 # it under the terms of the GNU Lesser General Public License as published by
21 # the Free Software Foundation, either version 3 of the License, or
22 # (at your option) any later version.
23 #
24 # DOLFIN is distributed in the hope that it will be useful,
25 # but WITHOUT ANY WARRANTY; without even the implied warranty of
26 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 # GNU Lesser General Public License for more details.
28 #
29 # You should have received a copy of the GNU Lesser General Public License
30 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
31 #
32 # Modified by Anders Logg 2011
33 #
34 # First added: 2010-07-23
35 # Last changed: 2012-11-12
36
37 from dolfin import *
38 import matplotlib.pyplot as plt
39
40
41 # Create mesh and define function space
42 mesh = UnitSquareMesh(64, 64)
43 V = FunctionSpace(mesh, "CG", 2)
44
45 # Define Dirichlet boundary (x = 0 or x = 1)
46 def boundary(x):
47 return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS or x[1] < DOLFIN_EPS or x[1] > 1.0 - DOLFIN_EPS
48
49 # Define boundary condition
50 u0 = Constant(0.0)
51 bc = DirichletBC(V, u0, boundary)
52
53 # Define variational problem
54 u = TrialFunction(V)
55 v = TestFunction(V)
56 x = SpatialCoordinate(mesh)
57
58 c0 = conditional(le( (x[0]-0.33)**2 + (x[1]-0.67)**2, 0.015), -1.0, 5.0)
59 c = conditional( le( (x[0]-0.33)**2 + (x[1]-0.67)**2, 0.025), c0, 0.0 )
60
61 t0 = conditional(ge( x[0], 0.55), -1.0, 0.0)
62 t1 = conditional(le( x[0], 0.95), t0, 0.0)
63 t2 = conditional(ge( x[1], 0.05), t1, 0.0)
64 t3 = conditional(le( x[1], 0.45), t2, 0.0)
65 t = conditional(ge( x[1] - x[0] - 0.05 + 0.55, 0.0), t3, 0.0)
66 f = c + t
67 a = inner(grad(u), grad(v))*dx
68 L = f*v*dx
69
70 # Compute solution
71 u = Function(V)
72 solve(a == L, u, bc)
73
74 # Save solution in VTK format
75 file = File("conditional.pvd")
76 file << u
77
78 # Plot solution
79 plot(u)
80 plt.show()