Debian Codegraph @

python/test/unit/function/test_constrained_function_space.py

raw
1 """Unit tests for FunctionSpace with constrained domain"""
2
3 # Copyright (C) 2012-2014 Garth N. Wells
4 #
5 # This file is part of DOLFIN.
6 #
7 # DOLFIN is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU Lesser General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # DOLFIN is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU Lesser General Public License for more details.
16 #
17 # You should have received a copy of the GNU Lesser General Public License
18 # along with DOLFIN. If not, see <http://www.gnu.org/licenses/>.
19 #
20 # Modified by Mikael Mortensen 2013
21
22 import pytest
23 import numpy
24 from dolfin import *
25
26
27 class PeriodicBoundary2(SubDomain):
28 def __init__(self, tolerance=DOLFIN_EPS):
29 SubDomain.__init__(self, tolerance)
30 self.tol = tolerance
31
32 def inside(self, x, on_boundary):
33 return bool(x[0] < self.tol and x[0] > -self.tol and on_boundary)
34
35 def map(self, x, y):
36 y[0] = x[0] - 1.0
37 y[1] = x[1]
38
39
40 class PeriodicBoundary3(SubDomain):
41 def inside(self, x, on_boundary):
42 return bool(x[0] < DOLFIN_EPS and x[0] > -DOLFIN_EPS and on_boundary)
43
44 def map(self, x, y):
45 y[0] = x[0] - 1.0
46 y[1] = x[1]
47 y[2] = x[2]
48
49
50 def test_instantiation():
51 """ A rudimentary test for instantiation"""
52
53 sub_domain = PeriodicBoundary3()
54 mesh = UnitCubeMesh(8, 8, 8)
55 V = FunctionSpace(mesh, "CG", 1, constrained_domain=sub_domain)
56
57
58 def test_instantiation_mixed_element():
59 """A rudimentary test for instantiation with mixed elements"""
60
61 pbc = PeriodicBoundary2()
62 mesh = UnitSquareMesh(8, 8)
63 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
64 VV = FunctionSpace(mesh, P1*P1, constrained_domain=pbc)
65
66
67 def test_instantiation_mixed_element_real():
68 """A rudimentary test for instantiation with mixed elements that
69 include a real space
70 """
71
72 pbc = PeriodicBoundary2()
73 mesh = UnitSquareMesh(8, 8)
74 P1 = FiniteElement("Lagrange", mesh.ufl_cell(), 1)
75 R = FiniteElement("Real", mesh.ufl_cell(), 0)
76 VV = FunctionSpace(mesh, P1*R, constrained_domain=pbc)
77 VV = FunctionSpace(mesh, R*P1, constrained_domain=pbc)
78
79
80 def test_instantiation_no_vertex_element_2D():
81 """ A rudimentary test for instantiation for element that does
82 not require number of vertices (2D)"""
83
84 pbc = PeriodicBoundary2()
85 mesh = UnitSquareMesh(8, 8)
86 V = FunctionSpace(mesh, "BDM", 1, constrained_domain=pbc)
87
88
89 def test_instantiation_no_vertex_element_3D():
90 """ A rudimentary test for instantiation for element that does
91 not require number of vertices (3D)"""
92
93 pbc = PeriodicBoundary3()
94 mesh = UnitCubeMesh(8, 8, 9)
95 V = FunctionSpace(mesh, "BDM", 1, constrained_domain=pbc)
96
97
98 def test_director_lifetime():
99 """Test for problems with objects with directors going out
100 of scope"""
101
102 mesh = UnitSquareMesh(8, 8)
103 V = FunctionSpace(mesh, "Lagrange", 1,
104 constrained_domain=PeriodicBoundary2())
105
106
107 def test_tolerance():
108 """Test tolerance for matching periodic mesh entities"""
109 shift = 0.0001
110 mesh = UnitSquareMesh(8, 8)
111
112 # Randomly perturb mesh vertex coordinates
113 mesh_perturb = Mesh(mesh)
114 import random
115 for x in mesh_perturb.coordinates():
116 x[0] += random.uniform(-shift, shift)
117 x[1] += random.uniform(-shift, shift)
118
119 pbc = PeriodicBoundary2()
120 pbc_tol = PeriodicBoundary2(2*shift)
121
122 for dim in range(mesh.geometry().dim()):
123 periodic_pairs = PeriodicBoundaryComputation.compute_periodic_pairs(mesh, pbc, dim)
124 num_periodic_pairs0 = len(periodic_pairs)
125
126 periodic_pairs = PeriodicBoundaryComputation.compute_periodic_pairs(mesh_perturb,
127 pbc_tol, dim)
128 num_periodic_pairs1 = len(periodic_pairs)
129 assert num_periodic_pairs0 == num_periodic_pairs1
130
131
132 def test_solution():
133 """Test periodic constrained domain by checking solution to a PDE."""
134
135 # Create mesh and constrained FunctionSpace
136 mesh = UnitSquareMesh(8, 8)
137 pbc = PeriodicBoundary2()
138 V = FunctionSpace(mesh, "Lagrange", 1, constrained_domain=pbc)
139
140 class DirichletBoundary(SubDomain):
141 def inside(self, x, on_boundary):
142 return bool((x[1] < DOLFIN_EPS or x[1] > (1.0 - DOLFIN_EPS)) and
143 on_boundary)
144
145 # Dirichlet boundary condition
146 dirichlet_boundary = DirichletBoundary()
147 bc0 = DirichletBC(V, 0.0, dirichlet_boundary)
148 bcs = [bc0]
149
150 # Define variational problem, linear formulation
151 u, v = TrialFunction(V), TestFunction(V)
152 f = Expression("sin(x[0])", degree=2)
153 a = dot(grad(u), grad(v))*dx
154 L = f*v*dx
155
156 # Compute solution
157 u = Function(V)
158 solve(a == L, u, bcs)
159
160 assert round(u.vector().norm("l2") - 0.3368694028630991, 10) == 0
161
162 # Define variational problem, nonlinear formulation
163 u, v = Function(V), TestFunction(V)
164 f = Expression("sin(x[0])", degree=2)
165 a = dot(grad(u), grad(v))*dx
166 L = f*v*dx
167 F = a - L
168
169 # Compute solution
170 solve(F == 0, u, bcs)
171
172 assert round(u.vector().norm("l2") - 0.3368694028630991, 10) == 0