| 1 |
#!/usr/bin/env py.test |
| 2 |
|
| 3 |
"""Unit tests for MixedAssembler""" |
| 4 |
|
| 5 |
# Copyright (C) 2011 Johan Hake |
| 6 |
# |
| 7 |
# This file is part of DOLFIN. |
| 8 |
# |
| 9 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 10 |
# it under the terms of the GNU Lesser General Public License as published by |
| 11 |
# the Free Software Foundation, either version 3 of the License, or |
| 12 |
# (at your option) any later version. |
| 13 |
# |
| 14 |
# DOLFIN is distributed in the hope that it will be useful, |
| 15 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 |
# GNU Lesser General Public License for more details. |
| 18 |
# |
| 19 |
# You should have received a copy of the GNU Lesser General Public License |
| 20 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 21 |
# |
| 22 |
# Modified by Cecile Daversin-Catty 2018 |
| 23 |
|
| 24 |
import pytest |
| 25 |
import numpy as np |
| 26 |
from dolfin import * |
| 27 |
from ufl_legacy.log import UFLException |
| 28 |
|
| 29 |
from dolfin_utils.test import skip_in_parallel, fixture |
| 30 |
|
| 31 |
|
| 32 |
@fixture |
| 33 |
def one_element(): # Reference element |
| 34 |
mesh = UnitSquareMesh(1, 1, "left") |
| 35 |
cell_f = MeshFunction('size_t', mesh, mesh.topology().dim(), 0) |
| 36 |
cell_f[0] = 1 |
| 37 |
mesh = SubMesh(mesh, cell_f, 1) |
| 38 |
|
| 39 |
marker = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0) |
| 40 |
for f in facets(mesh): |
| 41 |
marker[f] = 0.0 - DOLFIN_EPS < f.midpoint().x() < 0.0 + DOLFIN_EPS |
| 42 |
|
| 43 |
return (mesh, marker) |
| 44 |
|
| 45 |
|
| 46 |
@fixture |
| 47 |
def two_elements(): |
| 48 |
mesh = UnitSquareMesh(1, 1, "left") |
| 49 |
|
| 50 |
marker = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0) |
| 51 |
for f in facets(mesh): |
| 52 |
marker[f] = 0.5 - DOLFIN_EPS < f.midpoint().x() < 0.5 + DOLFIN_EPS and 0.5 - \ |
| 53 |
DOLFIN_EPS < f.midpoint().y() < 0.5 + DOLFIN_EPS |
| 54 |
|
| 55 |
return (mesh, marker) |
| 56 |
|
| 57 |
|
| 58 |
@fixture |
| 59 |
def unitsquare_3x3(): |
| 60 |
mesh = UnitSquareMesh(3, 3) |
| 61 |
|
| 62 |
marker = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0) |
| 63 |
for f in facets(mesh): |
| 64 |
marker[f] = 0.5 - DOLFIN_EPS < f.midpoint().x() < 0.5 + DOLFIN_EPS and 0.5 - \ |
| 65 |
DOLFIN_EPS < f.midpoint().y() < 0.5 + DOLFIN_EPS |
| 66 |
|
| 67 |
return (mesh, marker) |
| 68 |
|
| 69 |
|
| 70 |
@fixture |
| 71 |
def two_elements_with_interface(): |
| 72 |
mesh = UnitSquareMesh(1, 1) |
| 73 |
|
| 74 |
markerc = MeshFunction('size_t', mesh, mesh.topology().dim(), 0) |
| 75 |
markerf = MeshFunction('size_t', mesh, mesh.topology().dim()-1, 0) |
| 76 |
for c in cells(mesh): |
| 77 |
markerc[c] = c.midpoint().y() > c.midpoint().x() |
| 78 |
for f in facets(mesh): |
| 79 |
markerf[f] = abs(f.midpoint().x() - f.midpoint().y()) < 1e-10 |
| 80 |
|
| 81 |
return (markerc, markerf) |
| 82 |
|
| 83 |
|
| 84 |
@fixture |
| 85 |
def unit_marker_2D2D(): |
| 86 |
n = 20 |
| 87 |
square = UnitSquareMesh(n, n) |
| 88 |
marker = MeshFunction("size_t", square, square.topology().dim(), 0) |
| 89 |
for c in cells(square): |
| 90 |
marker[c] = c.midpoint().x() < 0.5 |
| 91 |
return marker |
| 92 |
|
| 93 |
|
| 94 |
@fixture |
| 95 |
def unit_marker_3D2D(): |
| 96 |
n = 20 |
| 97 |
cube = UnitCubeMesh(n, n, n) |
| 98 |
marker = MeshFunction("size_t", cube, cube.topology().dim() - 1, 0) |
| 99 |
for f in facets(cube): |
| 100 |
marker[f] = 0.5 - DOLFIN_EPS < f.midpoint().z() < 0.5 + DOLFIN_EPS |
| 101 |
return marker |
| 102 |
|
| 103 |
|
| 104 |
@fixture |
| 105 |
def unit_marker_ext(): |
| 106 |
square = UnitSquareMesh(1, 1) |
| 107 |
marker = MeshFunction("size_t", square, square.topology().dim()-1, 0) |
| 108 |
for f in facets(square): |
| 109 |
marker[f] = abs(f.midpoint().y()) < 1e-10 |
| 110 |
return marker |
| 111 |
|
| 112 |
|
| 113 |
def meshview(marker, i): |
| 114 |
return MeshView.create(marker, i) |
| 115 |
|
| 116 |
|
| 117 |
def space(mesh): |
| 118 |
return FunctionSpace(mesh, "Lagrange", 1) |
| 119 |
|
| 120 |
|
| 121 |
def a(u, v): |
| 122 |
return inner(grad(u), grad(v))*dx |
| 123 |
|
| 124 |
|
| 125 |
def L(f, v): |
| 126 |
return f*v*dx |
| 127 |
|
| 128 |
|
| 129 |
def boundary(x): |
| 130 |
return x[0] < DOLFIN_EPS or x[0] > 1.0 - DOLFIN_EPS |
| 131 |
|
| 132 |
|
| 133 |
def boundary1(x): |
| 134 |
return x[0] > 1.0 - DOLFIN_EPS |
| 135 |
|
| 136 |
|
| 137 |
def boundary2(x): |
| 138 |
return x[0] < DOLFIN_EPS |
| 139 |
|
| 140 |
|
| 141 |
def params(): |
| 142 |
return dict({"linear_solver": "direct"}) |
| 143 |
|
| 144 |
|
| 145 |
@skip_in_parallel |
| 146 |
def test_mixed_assembly(one_element, two_elements): |
| 147 |
"""Off-diagonal blocks assembly""" |
| 148 |
|
| 149 |
# Test for a range of various FE types |
| 150 |
def _check_scalar(case, order): |
| 151 |
mesh = case[0] |
| 152 |
submesh = MeshView.create(case[1], 1) |
| 153 |
|
| 154 |
V = FunctionSpace(mesh, 'CG', order[0]) |
| 155 |
Q = FunctionSpace(submesh, 'CG', order[1]) |
| 156 |
W = MixedFunctionSpace(V, Q) |
| 157 |
|
| 158 |
f = Expression('x[0]+x[1]', degree=3) |
| 159 |
g = Expression('x[0]-x[1]', degree=3) |
| 160 |
|
| 161 |
(u, p) = TrialFunctions(W) |
| 162 |
(v, q) = TestFunctions(W) |
| 163 |
|
| 164 |
dx_ = Measure('dx', domain=W.sub_space(1).mesh()) |
| 165 |
# Reference value |
| 166 |
ref = assemble(inner(f, g)*dx_) |
| 167 |
|
| 168 |
trace_form = inner(u, q)*dx_ + inner(u, v)*dx + \ |
| 169 |
inner(p, v)*dx_ + inner(p, q)*dx_ |
| 170 |
rhs = inner(Constant(0), v)*dx + inner(Constant(0), q)*dx_ |
| 171 |
|
| 172 |
AA, bb, _ = assemble_mixed_system(trace_form == rhs, Function(W)) |
| 173 |
|
| 174 |
T = AA[2] |
| 175 |
Tt = AA[1] |
| 176 |
|
| 177 |
v = interpolate(f, V) |
| 178 |
q = interpolate(g, Q) |
| 179 |
|
| 180 |
Tv = Function(Q).vector() |
| 181 |
T.mult(v.vector(), Tv) |
| 182 |
qT = Function(V).vector() |
| 183 |
Tt.mult(q.vector(), qT) |
| 184 |
|
| 185 |
vqT = v.vector().inner(qT) |
| 186 |
qTv = q.vector().inner(Tv) |
| 187 |
|
| 188 |
assert (abs(vqT - ref) <= 1e-12) |
| 189 |
assert (abs(qTv - ref) <= 1e-12) |
| 190 |
assert (abs(qTv - vqT) <= 1e-12) |
| 191 |
|
| 192 |
def _check_vectorial(case, order): |
| 193 |
mesh = case[0] |
| 194 |
submesh = MeshView.create(case[1], 1) |
| 195 |
|
| 196 |
V = VectorFunctionSpace(mesh, 'CG', order[0]) |
| 197 |
Q = VectorFunctionSpace(submesh, 'CG', order[1]) |
| 198 |
W = MixedFunctionSpace(V, Q) |
| 199 |
|
| 200 |
f = Expression(('x[0]+x[1]', 'x[0]-x[1]'), degree=3) |
| 201 |
g = Expression(('x[0]+3*x[1]', 'x[0]-2*x[1]'), degree=3) |
| 202 |
|
| 203 |
(u, p) = TrialFunctions(W) |
| 204 |
(v, q) = TestFunctions(W) |
| 205 |
dx_ = Measure('dx', domain=W.sub_space(1).mesh()) |
| 206 |
# Reference value |
| 207 |
ref = assemble(inner(f, g)*dx_) |
| 208 |
|
| 209 |
trace_form = inner(u, q)*dx_ + inner(u, v)*dx + \ |
| 210 |
inner(p, v)*dx_ + inner(p, q)*dx_ |
| 211 |
rhs = inner(Constant((0, 0)), v)*dx + inner(Constant((0, 0)), q)*dx_ |
| 212 |
|
| 213 |
AA, bb, _ = assemble_mixed_system(trace_form == rhs, Function(W)) |
| 214 |
|
| 215 |
T = AA[2] |
| 216 |
Tt = AA[1] |
| 217 |
|
| 218 |
v = interpolate(f, V) |
| 219 |
q = interpolate(g, Q) |
| 220 |
|
| 221 |
Tv = Function(Q).vector() |
| 222 |
T.mult(v.vector(), Tv) |
| 223 |
qT = Function(V).vector() |
| 224 |
Tt.mult(q.vector(), qT) |
| 225 |
|
| 226 |
vqT = v.vector().inner(qT) |
| 227 |
qTv = q.vector().inner(Tv) |
| 228 |
|
| 229 |
assert (abs(vqT - ref) <= 1e-12) |
| 230 |
assert (abs(qTv - ref) <= 1e-12) |
| 231 |
assert (abs(qTv - vqT) <= 1e-12) |
| 232 |
|
| 233 |
# Scalar case - One element + Lagrange mult on exterior boundary |
| 234 |
_check_scalar(one_element, (1, 2)) # CG1 - CG2 |
| 235 |
_check_scalar(one_element, (2, 1)) # CG2 - CG1 |
| 236 |
# Scalar case - Two elements + Lagrange mult on interior facet |
| 237 |
_check_scalar(two_elements, (1, 2)) # CG1 - CG2 |
| 238 |
_check_scalar(two_elements, (2, 1)) # CG2 - CG1 |
| 239 |
# Vectorial case - One element + Lagrange mult on exterior boundary |
| 240 |
_check_vectorial(one_element, (1, 2)) # CG1 - CG2 |
| 241 |
_check_vectorial(one_element, (2, 1)) # CG2 - CG1 |
| 242 |
# Vectorial case - Two elements + Lagrange mult on interior facet |
| 243 |
_check_vectorial(two_elements, (1, 2)) # CG1 - CG2 |
| 244 |
_check_vectorial(two_elements, (2, 1)) # CG2 - CG1 |
| 245 |
|
| 246 |
|
| 247 |
@skip_in_parallel |
| 248 |
def test_mixed_assembly_interface(two_elements_with_interface): |
| 249 |
"""Off-diagonal blocks assembly""" |
| 250 |
|
| 251 |
# Test for a range of various FE types |
| 252 |
def _check_scalar(case, order): |
| 253 |
mesh0 = MeshView.create(case[0], 0) |
| 254 |
mesh1 = MeshView.create(case[0], 1) |
| 255 |
submesh = MeshView.create(case[1], 1) |
| 256 |
|
| 257 |
V0 = FunctionSpace(mesh0, 'CG', order[0]) |
| 258 |
V1 = FunctionSpace(mesh1, 'CG', order[1]) |
| 259 |
Q = FunctionSpace(submesh, 'CG', order[2]) |
| 260 |
W = MixedFunctionSpace(V0, V1, Q) |
| 261 |
|
| 262 |
f = Expression('x[0]+x[1]', degree=3) |
| 263 |
g = Expression('x[0]-x[1]', degree=3) |
| 264 |
|
| 265 |
(u0, u1, p) = TrialFunctions(W) |
| 266 |
(v0, v1, q) = TestFunctions(W) |
| 267 |
|
| 268 |
dx_ = Measure('dx', domain=W.sub_space(2).mesh()) |
| 269 |
# Reference value |
| 270 |
ref_fg = assemble(inner(f, g)*dx_) |
| 271 |
ref_ff = assemble(inner(f, f)*dx_) |
| 272 |
|
| 273 |
# Diagonal blocks |
| 274 |
trace_form = inner(u0, v0)*dx + inner(u1, v1)*dx + inner(p, q)*dx_ |
| 275 |
# Mixed-dimensional coupling |
| 276 |
trace_form += inner(u0, q)*dx_ + inner(u1, q)*dx_ + \ |
| 277 |
inner(p, v0)*dx_ + inner(p, v1)*dx_ |
| 278 |
# Integration of common |
| 279 |
trace_form += inner(u0, v1)*dx_ + inner(u1, v0)*dx_ |
| 280 |
rhs = inner(Constant(0), v0)*dx + inner(Constant(0), v1) * \ |
| 281 |
dx + inner(Constant(0), q)*dx_ |
| 282 |
|
| 283 |
AA, bb, _ = assemble_mixed_system(trace_form == rhs, Function(W)) |
| 284 |
|
| 285 |
# Mixed-dimensional coupling |
| 286 |
T0 = AA[6] # u0*q |
| 287 |
T1 = AA[7] # u1*q |
| 288 |
Tt0 = AA[2] # p*v0 |
| 289 |
Tt1 = AA[5] # p*v1 |
| 290 |
# Integration of common interface |
| 291 |
I = AA[3] # u0*v1 |
| 292 |
It = AA[1] # u1*v0 |
| 293 |
|
| 294 |
v0 = interpolate(f, W.sub_space(0)) |
| 295 |
v1 = interpolate(f, W.sub_space(1)) |
| 296 |
q = interpolate(g, W.sub_space(2)) |
| 297 |
|
| 298 |
Tv0 = Function(Q).vector() |
| 299 |
Tv1 = Function(Q).vector() |
| 300 |
T0.mult(v0.vector(), Tv0) |
| 301 |
T1.mult(v1.vector(), Tv1) |
| 302 |
qT0 = Function(V0).vector() |
| 303 |
qT1 = Function(V1).vector() |
| 304 |
Tt0.mult(q.vector(), qT0) |
| 305 |
Tt1.mult(q.vector(), qT1) |
| 306 |
|
| 307 |
Iv0 = Function(V1).vector() |
| 308 |
Iv1 = Function(V0).vector() |
| 309 |
I.mult(v0.vector(), Iv0) |
| 310 |
It.mult(v1.vector(), Iv1) |
| 311 |
|
| 312 |
v0qT0 = v0.vector().inner(qT0) |
| 313 |
v1qT1 = v1.vector().inner(qT1) |
| 314 |
qTv0 = q.vector().inner(Tv0) |
| 315 |
qTv1 = q.vector().inner(Tv1) |
| 316 |
|
| 317 |
v1Iv0 = v1.vector().inner(Iv0) |
| 318 |
v0Iv1 = v0.vector().inner(Iv1) |
| 319 |
|
| 320 |
assert (abs(v0qT0 - ref_fg) <= 1e-12) |
| 321 |
assert (abs(v1qT1 - ref_fg) <= 1e-12) |
| 322 |
assert (abs(qTv0 - ref_fg) <= 1e-12) |
| 323 |
assert (abs(qTv1 - ref_fg) <= 1e-12) |
| 324 |
assert (abs(qTv0 - v0qT0) <= 1e-12) |
| 325 |
assert (abs(qTv1 - v1qT1) <= 1e-12) |
| 326 |
|
| 327 |
assert (abs(v1Iv0 - ref_ff) <= 1e-12) |
| 328 |
assert (abs(v0Iv1 - ref_ff) <= 1e-12) |
| 329 |
|
| 330 |
# print("[scalar] v1Iv0 = ", v1Iv0, " while ref_ff = ", ref_ff) |
| 331 |
# print("[scalar] v0Iv1 = ", v0Iv1, " while ref_ff = ", ref_ff) |
| 332 |
|
| 333 |
def _check_vectorial(case, order): |
| 334 |
mesh0 = MeshView.create(case[0], 0) |
| 335 |
mesh1 = MeshView.create(case[0], 1) |
| 336 |
submesh = MeshView.create(case[1], 1) |
| 337 |
|
| 338 |
V0 = VectorFunctionSpace(mesh0, 'CG', order[0]) |
| 339 |
V1 = VectorFunctionSpace(mesh1, 'CG', order[1]) |
| 340 |
Q = VectorFunctionSpace(submesh, 'CG', order[2]) |
| 341 |
W = MixedFunctionSpace(V0, V1, Q) |
| 342 |
|
| 343 |
f = Expression(('x[0]+x[1]', 'x[0]-x[1]'), degree=3) |
| 344 |
g = Expression(('x[0]+3*x[1]', 'x[0]-2*x[1]'), degree=3) |
| 345 |
|
| 346 |
(u0, u1, p) = TrialFunctions(W) |
| 347 |
(v0, v1, q) = TestFunctions(W) |
| 348 |
dx_ = Measure('dx', domain=W.sub_space(2).mesh()) |
| 349 |
# Reference value |
| 350 |
ref_fg = assemble(inner(f, g)*dx_) |
| 351 |
ref_ff = assemble(inner(f, f)*dx_) |
| 352 |
|
| 353 |
# Diagonal blocks |
| 354 |
trace_form = inner(u0, v0)*dx + inner(u1, v1)*dx + inner(p, q)*dx_ |
| 355 |
# Mixed-dimensional coupling |
| 356 |
trace_form += inner(u0, q)*dx_ + inner(u1, q)*dx_ + \ |
| 357 |
inner(p, v0)*dx_ + inner(p, v1)*dx_ |
| 358 |
# Integration of common interface |
| 359 |
trace_form += inner(u0, v1)*dx_ + inner(u1, v0)*dx_ |
| 360 |
rhs = inner(Constant((0, 0)), v0)*dx + inner(Constant((0, 0)), |
| 361 |
v1)*dx + inner(Constant((0, 0)), q)*dx_ |
| 362 |
|
| 363 |
AA, bb, _ = assemble_mixed_system(trace_form == rhs, Function(W)) |
| 364 |
|
| 365 |
# Mixed-dimensional coupling |
| 366 |
T0 = AA[6] |
| 367 |
T1 = AA[7] |
| 368 |
Tt0 = AA[2] |
| 369 |
Tt1 = AA[5] |
| 370 |
# Integration of common interface |
| 371 |
I = AA[3] # u0*v1 |
| 372 |
It = AA[1] # u1*v0 |
| 373 |
|
| 374 |
v0 = interpolate(f, W.sub_space(0)) |
| 375 |
v1 = interpolate(f, W.sub_space(1)) |
| 376 |
q = interpolate(g, W.sub_space(2)) |
| 377 |
|
| 378 |
Tv0 = Function(Q).vector() |
| 379 |
Tv1 = Function(Q).vector() |
| 380 |
T0.mult(v0.vector(), Tv0) |
| 381 |
T1.mult(v1.vector(), Tv1) |
| 382 |
qT0 = Function(V0).vector() |
| 383 |
qT1 = Function(V1).vector() |
| 384 |
Tt0.mult(q.vector(), qT0) |
| 385 |
Tt1.mult(q.vector(), qT1) |
| 386 |
|
| 387 |
Iv0 = Function(V1).vector() |
| 388 |
Iv1 = Function(V0).vector() |
| 389 |
I.mult(v0.vector(), Iv0) |
| 390 |
It.mult(v1.vector(), Iv1) |
| 391 |
|
| 392 |
v0qT0 = v0.vector().inner(qT0) |
| 393 |
v1qT1 = v1.vector().inner(qT1) |
| 394 |
qTv0 = q.vector().inner(Tv0) |
| 395 |
qTv1 = q.vector().inner(Tv1) |
| 396 |
|
| 397 |
v1Iv0 = v1.vector().inner(Iv0) |
| 398 |
v0Iv1 = v0.vector().inner(Iv1) |
| 399 |
|
| 400 |
assert (abs(v0qT0 - ref_fg) <= 1e-12) |
| 401 |
assert (abs(v1qT1 - ref_fg) <= 1e-12) |
| 402 |
assert (abs(qTv0 - ref_fg) <= 1e-12) |
| 403 |
assert (abs(qTv1 - ref_fg) <= 1e-12) |
| 404 |
assert (abs(qTv0 - v0qT0) <= 1e-12) |
| 405 |
assert (abs(qTv1 - v1qT1) <= 1e-12) |
| 406 |
|
| 407 |
assert (abs(v1Iv0 - ref_ff) <= 1e-12) |
| 408 |
assert (abs(v0Iv1 - ref_ff) <= 1e-12) |
| 409 |
|
| 410 |
# print("[vectorial] v1Iv0 = ", v1Iv0, " while ref_ff = ", ref_ff) |
| 411 |
# print("[vectorial] v0Iv1 = ", v0Iv1, " while ref_ff = ", ref_ff) |
| 412 |
|
| 413 |
# Scalar case - Two elements + Lagrange mult on interior facet |
| 414 |
_check_scalar(two_elements_with_interface, (1, 1, 1)) # CG1 - CG1 - CG2 |
| 415 |
_check_scalar(two_elements_with_interface, (2, 2, 1)) # CG2 - CG2 - CG1 |
| 416 |
_check_scalar(two_elements_with_interface, (1, 1, 2)) # CG1 - CG1- CG2 |
| 417 |
_check_scalar(two_elements_with_interface, (2, 2, 2)) # CG2 - CG2- CG1 |
| 418 |
_check_scalar(two_elements_with_interface, (1, 1, 2)) # CG2 - CG1- CG1 |
| 419 |
_check_scalar(two_elements_with_interface, (1, 2, 1)) # CG1 - CG2- CG2 |
| 420 |
_check_scalar(two_elements_with_interface, (2, 1, 1)) # CG1 - CG2- CG2 |
| 421 |
# Vectorial case - Two elements + Lagrange mult on interior facet |
| 422 |
_check_vectorial(two_elements_with_interface, (1, 1, 2)) # CG1 - CG1 - CG2 |
| 423 |
_check_vectorial(two_elements_with_interface, (2, 2, 1)) # CG2 - CG2 - CG1 |
| 424 |
_check_vectorial(two_elements_with_interface, (1, 1, 2)) # CG1 - CG1- CG2 |
| 425 |
_check_vectorial(two_elements_with_interface, (2, 2, 1)) # CG2 - CG2- CG1 |
| 426 |
_check_vectorial(two_elements_with_interface, (2, 1, 1)) # CG2 - CG1- CG1 |
| 427 |
_check_vectorial(two_elements_with_interface, (1, 2, 1)) # CG1 - CG2- CG2 |
| 428 |
|
| 429 |
|
| 430 |
@skip_in_parallel |
| 431 |
def test_mixed_assembly_diag(unit_marker_2D2D, unit_marker_3D2D): |
| 432 |
def _compare_solutions(marker, boundaries): |
| 433 |
# Meshes |
| 434 |
_mesh1 = meshview(marker, 0) |
| 435 |
_mesh2 = meshview(marker, 1) |
| 436 |
# Spaces |
| 437 |
_space1 = space(_mesh1) |
| 438 |
_space2 = space(_mesh2) |
| 439 |
_space_mixed = MixedFunctionSpace(_space1, _space2) |
| 440 |
# Trial functions |
| 441 |
_u1 = TrialFunction(_space1) |
| 442 |
_u2 = TrialFunction(_space2) |
| 443 |
(_u1_m, _u2_m) = TrialFunctions(_space_mixed) |
| 444 |
# Test functions |
| 445 |
_v1 = TestFunction(_space1) |
| 446 |
_v2 = TestFunction(_space2) |
| 447 |
(_v1_m, _v2_m) = TestFunctions(_space_mixed) |
| 448 |
# Bilinear forms |
| 449 |
_a1 = a(_u1, _v1) |
| 450 |
_a2 = a(_u2, _v2) |
| 451 |
_am = a(_u1_m, _v1_m) + a(_u2_m, _v2_m) |
| 452 |
# Linear forms |
| 453 |
f = Expression( |
| 454 |
"10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) |
| 455 |
_L1 = L(f, _v1) |
| 456 |
_L2 = L(f, _v2) |
| 457 |
_Lm = L(f, _v1_m) + L(f, _v2_m) |
| 458 |
# Solution |
| 459 |
sol1 = Function(_space1) |
| 460 |
sol2 = Function(_space2) |
| 461 |
sol = Function(_space_mixed) |
| 462 |
# BCs |
| 463 |
bc1 = DirichletBC(_space1, Constant(0.0), boundaries[0]) |
| 464 |
bc2 = DirichletBC(_space2, Constant(0.0), boundaries[1]) |
| 465 |
# Resolution |
| 466 |
solve(_a1 == _L1, sol1, bcs=bc1) |
| 467 |
solve(_a2 == _L2, sol2, bcs=bc2) |
| 468 |
solve(_am == _Lm, sol, bcs=[bc1, bc2], solver_parameters=params()) |
| 469 |
|
| 470 |
sol1_m = sol.sub(0, deepcopy=True) |
| 471 |
sol2_m = sol.sub(1, deepcopy=True) |
| 472 |
assert len(sol1.vector()) == len(sol1_m.vector()) |
| 473 |
for i in range(len(sol1.vector())): |
| 474 |
assert abs(sol1.vector()[i] - sol1_m.vector()[i]) < 1e-10 |
| 475 |
|
| 476 |
assert len(sol2.vector()) == len(sol2_m.vector()) |
| 477 |
for i in range(len(sol2.vector())): |
| 478 |
assert abs(sol2.vector()[i] - sol2_m.vector()[i]) < 1e-10 |
| 479 |
|
| 480 |
_compare_solutions(unit_marker_2D2D, [boundary1, boundary2]) |
| 481 |
_compare_solutions(unit_marker_3D2D, [boundary, boundary]) |
| 482 |
|
| 483 |
|
| 484 |
@skip_in_parallel |
| 485 |
def test_mixed_assembly_rank0(unit_marker_ext): |
| 486 |
def _compare_solutions(marker): |
| 487 |
# Meshes |
| 488 |
mesh = marker.mesh() # The initial parent mesh |
| 489 |
submesh = meshview(marker, 1) |
| 490 |
|
| 491 |
Vh0 = FunctionSpace(mesh, "Lagrange", 1) |
| 492 |
Vh1 = FunctionSpace(submesh, "Lagrange", 1) |
| 493 |
|
| 494 |
v0 = Function(Vh0) |
| 495 |
m0 = Function(Vh0) |
| 496 |
m1 = Function(Vh1) |
| 497 |
|
| 498 |
v0.vector()[:] = np.random.rand(Vh0.dim()) |
| 499 |
m0.vector()[:] = np.ones(Vh0.dim()) |
| 500 |
m1.vector()[:] = np.ones(Vh1.dim()) |
| 501 |
|
| 502 |
ds = Measure("ds", domain=mesh, subdomain_data=marker) |
| 503 |
f0 = v0 * m0 * ds(1) |
| 504 |
|
| 505 |
ds1 = Measure("dx", domain=submesh) |
| 506 |
f1 = v0 * m1 * ds1 |
| 507 |
|
| 508 |
assert abs(assemble(f0) - assemble_mixed(f1)) < 1e-10 |
| 509 |
|
| 510 |
_compare_solutions(unit_marker_ext) |
| 511 |
|
| 512 |
|
| 513 |
@skip_in_parallel |
| 514 |
def test_function_spaces_consistency(one_element, two_elements, unitsquare_3x3): |
| 515 |
def _integrating_subf_over_parent(case, order): |
| 516 |
with pytest.raises(Exception, match="codim"): |
| 517 |
mesh = case[0] |
| 518 |
submesh = MeshView.create(case[1], 1) |
| 519 |
|
| 520 |
V = FunctionSpace(mesh, 'CG', order[0]) |
| 521 |
Q = FunctionSpace(submesh, 'CG', order[1]) |
| 522 |
W = MixedFunctionSpace(V, Q) |
| 523 |
|
| 524 |
(u, _) = TrialFunctions(W) |
| 525 |
(_, q) = TestFunctions(W) |
| 526 |
|
| 527 |
dx_ = Measure('dx', domain=W.sub_space(0).mesh()) |
| 528 |
|
| 529 |
assemble_mixed(u*q*dx_) |
| 530 |
|
| 531 |
_integrating_subf_over_parent(one_element, (1, 1)) |
| 532 |
_integrating_subf_over_parent(two_elements, (1, 1)) |
| 533 |
_integrating_subf_over_parent(unitsquare_3x3, (1, 1)) |
| 534 |
|
| 535 |
def _no_shared_parent(case, order): |
| 536 |
with pytest.raises(RuntimeError, match="Cannot find common parent mesh"): |
| 537 |
mesh = case[0] |
| 538 |
int_mesh = UnitSquareMesh(3, 3) |
| 539 |
|
| 540 |
V = FunctionSpace(mesh, 'CG', order[0]) |
| 541 |
Q = FunctionSpace(int_mesh, 'CG', order[1]) |
| 542 |
W = MixedFunctionSpace(V, Q) |
| 543 |
|
| 544 |
(u, p) = TrialFunctions(W) |
| 545 |
(v, q) = TestFunctions(W) |
| 546 |
|
| 547 |
dx_ = Measure('dx', domain=int_mesh) |
| 548 |
|
| 549 |
assemble_mixed(u*v*dx_) |
| 550 |
|
| 551 |
_no_shared_parent(unitsquare_3x3, (1, 1)) |