Debian Codegraph @

python/test/unit/function/test_function_assigner.py

raw
1 """Unit tests for the FunctionAssigner class"""
2
3 # Copyright (C) 2013 Johan Hake
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 # First added: 2013-11-07
21 # Last changed: 2013-11-07
22
23 import pytest
24 from dolfin import *
25 import numpy as np
26
27 from dolfin_utils.test import fixture as fixt
28
29 @fixt
30 def mesh():
31 return UnitCubeMesh(8, 8, 8)
32
33 @fixt
34 def V(mesh):
35 return FunctionSpace(mesh, "CG", 1)
36
37 @fixt
38 def Q(mesh):
39 return FunctionSpace(mesh, "CG", 2)
40
41 @fixt
42 def W(mesh):
43 return VectorFunctionSpace(mesh, "CG", 1)
44
45 @fixt
46 def QQ(mesh):
47 return VectorFunctionSpace(mesh, "CG", 2)
48
49 @fixt
50 def R(mesh):
51 return FunctionSpace(mesh, "R", 0)
52
53 @fixt
54 def RR(mesh):
55 return VectorFunctionSpace(mesh, "R", 0)
56
57 @fixt
58 def QQV(mesh):
59 QQ = VectorElement("CG", mesh.ufl_cell(), 2)
60 V = FiniteElement("CG", mesh.ufl_cell(), 1)
61 return FunctionSpace(mesh, QQ*V)
62
63 @fixt
64 def WW(mesh):
65 W = VectorElement("CG", mesh.ufl_cell(), 1)
66 return FunctionSpace(mesh, W*W)
67
68 @fixt
69 def WR(mesh):
70 W = VectorElement("CG", mesh.ufl_cell(), 1)
71 R = FiniteElement("R", mesh.ufl_cell(), 0)
72 return FunctionSpace(mesh, W*R)
73
74 @fixt
75 def WRR(mesh):
76 W = VectorElement("CG", mesh.ufl_cell(), 1)
77 RR = VectorElement("R", mesh.ufl_cell(), 0)
78 return FunctionSpace(mesh, W*RR)
79
80 @fixt
81 def u0(V):
82 u0_ = Function(V)
83 u0_.vector()[:] = 1.
84 return u0_
85
86 @fixt
87 def u1(V):
88 u1_ = Function(V)
89 u1_.vector()[:] = 2.
90 return u1_
91
92 @fixt
93 def u2(V):
94 u2_ = Function(V)
95 u2_.vector()[:] = 3.
96 return u2_
97
98 @fixt
99 def r(R):
100 r_ = Function(R)
101 r_.vector()[:] = 3.
102 return r_
103
104 @fixt
105 def rr(RR):
106 rr_ = Function(RR)
107 rr_.vector()[:] = 2.
108 return rr_
109
110 @fixt
111 def w(W):
112 w_ = Function(W)
113 w_.vector()[:] = 4.
114 return w_
115
116 @fixt
117 def ww(WW):
118 ww_ = Function(WW)
119 ww_.vector()[:] = 5.
120 return ww_
121
122 @fixt
123 def wr(WR):
124 wr_ = Function(WR)
125 wr_.vector()[:] = 6.
126 return wr_
127
128 @fixt
129 def wrr(WRR):
130 wrr_ = Function(WRR)
131 wrr_.vector()[:] = 7.
132 return wrr_
133
134 @fixt
135 def q(Q):
136 q_ = Function(Q)
137 q_.vector()[:] = 1.
138 return q_
139
140 @fixt
141 def qq(QQ):
142 qq_ = Function(QQ)
143 qq_.vector()[:] = 2.
144 return qq_
145
146 @fixt
147 def qqv(QQV):
148 qqv_ = Function(QQV)
149 qqv_.vector()[:] = 3.
150 return qqv_
151
152
153 def test_1_1_assigner(w, ww, wr, wrr, q, r, qqv, u0, u1, u2, W, V, WW):
154
155 assigner = FunctionAssigner(W.sub(0), V)
156 assigner.assign(w.sub(0), u0)
157
158 assert np.all(w.sub(0, deepcopy=True).vector().get_local() == u0.vector().get_local())
159
160 assign(w.sub(2), u2)
161 assert np.all(w.sub(2, deepcopy=True).vector().get_local() == u2.vector().get_local())
162
163 assigner = FunctionAssigner(V, W.sub(2))
164 assigner.assign(u0, w.sub(2))
165
166 assert np.all(u0.vector().get_local() == w.sub(2, deepcopy=True).vector().get_local())
167
168 assign(u1, w.sub(1))
169 assert np.all(u1.vector().get_local() == w.sub(1, deepcopy=True).vector().get_local())
170
171 assigner = FunctionAssigner(WW.sub(0), W)
172 assigner.assign(ww.sub(0), w)
173
174 assign(wr.sub(0), w)
175 assert np.all(wr.sub(0, deepcopy=True).vector().get_local() == w.vector().get_local())
176
177 assign(wr.sub(1), r)
178 assert np.all(wr.sub(1, deepcopy=True).vector().get_local() == r.vector().get_local())
179
180 assign(qqv.sub(0).sub(0), q)
181 assert np.all(qqv.sub(0).sub(0, deepcopy=True).vector().get_local() == q.vector().get_local())
182
183 with pytest.raises(RuntimeError):
184 assign(qqv.sub(0), q)
185 with pytest.raises(RuntimeError):
186 assign(qqv.sub(1), q)
187 with pytest.raises(RuntimeError):
188 assign(wrr.sub(1), w)
189
190
191 def test_N_1_assigner(u0, u1, u2, qq, qqv, rr, w, wrr, r, W, V):
192
193 vv = Function(W)
194 assigner = FunctionAssigner(W, [V,V,V])
195 assigner.assign(vv, [u0, u1, u2])
196
197 assert np.all(vv.sub(0, deepcopy=True).vector().get_local() == u0.vector().get_local())
198 assert np.all(vv.sub(1, deepcopy=True).vector().get_local() == u1.vector().get_local())
199 assert np.all(vv.sub(2, deepcopy=True).vector().get_local() == u2.vector().get_local())
200
201 assign(qqv, [qq, u1])
202 assert np.all(qqv.sub(0, deepcopy=True).vector().get_local() == qq.vector().get_local())
203 assert np.all(qqv.sub(1, deepcopy=True).vector().get_local() == u1.vector().get_local())
204
205 assign(wrr, [w, rr])
206 assert np.all(wrr.sub(0, deepcopy=True).vector().get_local() == w.vector().get_local())
207 assert np.all(wrr.sub(1, deepcopy=True).vector().get_local() == rr.vector().get_local())
208
209 with pytest.raises(RuntimeError):
210 assign(qqv, [qq, u1, u1])
211
212 with pytest.raises(RuntimeError):
213 assign(wrr, [w, r, r])
214
215 def test_1_N_assigner(u0, u1, u2, w, qq, qqv, V, W):
216
217 assigner = FunctionAssigner([V,V,V], W)
218 assigner.assign([u0, u1, u2], w)
219
220 assert np.all(w.sub(0, deepcopy=True).vector().get_local() == u0.vector().get_local())
221 assert np.all(w.sub(1, deepcopy=True).vector().get_local() == u1.vector().get_local())
222 assert np.all(w.sub(2, deepcopy=True).vector().get_local() == u2.vector().get_local())
223
224 assign([qq, u1], qqv)
225
226 assert np.all(qqv.sub(0, deepcopy=True).vector().get_local() == qq.vector().get_local())
227 assert np.all(qqv.sub(1, deepcopy=True).vector().get_local() == u1.vector().get_local())