| 1 |
#!/usr/bin/env py.test |
| 2 |
|
| 3 |
"""Unit tests for the extract_blocks function (based on FormSplitter ufl class)""" |
| 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 2017 |
| 23 |
# |
| 24 |
# First added: 2017-07-25 |
| 25 |
# Last changed: 2017-07-25 |
| 26 |
|
| 27 |
import pytest |
| 28 |
from dolfin import * |
| 29 |
from ufl_legacy.log import UFLException |
| 30 |
|
| 31 |
from dolfin_utils.test import fixture |
| 32 |
|
| 33 |
from dolfin.fem.assembling import _create_dolfin_form |
| 34 |
|
| 35 |
@fixture |
| 36 |
def marker(): |
| 37 |
cube = UnitCubeMesh(8, 8, 8) |
| 38 |
marker = CellFunction("size_t", cube, 0) |
| 39 |
for c in cells(cube): |
| 40 |
marker[c] = c.midpoint().x() < 0.5 |
| 41 |
return marker |
| 42 |
|
| 43 |
@fixture |
| 44 |
def mesh1(marker): |
| 45 |
return MeshViewMapping.create_from_marker(marker, 1) |
| 46 |
|
| 47 |
@fixture |
| 48 |
def mesh2(marker): |
| 49 |
return MeshViewMapping.create_from_marker(marker, 0) |
| 50 |
|
| 51 |
@fixture |
| 52 |
def space(mesh1,mesh2): |
| 53 |
W1 = FunctionSpace(mesh1, "Lagrange", 1) |
| 54 |
W2 = FunctionSpace(mesh2, "Lagrange", 2) |
| 55 |
return FunctionSpaceProduct( W1, W2 ) |
| 56 |
|
| 57 |
@fixture |
| 58 |
def u(space): |
| 59 |
return TrialFunction(space) |
| 60 |
|
| 61 |
@fixture |
| 62 |
def v(space): |
| 63 |
return TestFunction(space) |
| 64 |
|
| 65 |
@fixture |
| 66 |
def a(u,v): |
| 67 |
return inner(grad(u[0]), grad(v[0]))*dx + inner(grad(u[1]), grad(v[1]))*dx |
| 68 |
|
| 69 |
# TODO : bilinear form with interface terms (non-diagonal blocks) |
| 70 |
# @fixture |
| 71 |
# def a2(u,v): |
| 72 |
# return ... |
| 73 |
|
| 74 |
@fixture |
| 75 |
def f(): |
| 76 |
return Expression("10*exp(-(pow(x[0] - 0.5, 2) + pow(x[1] - 0.5, 2)) / 0.02)", degree=2) |
| 77 |
|
| 78 |
@fixture |
| 79 |
def L(f,v): |
| 80 |
return f*v[0]*dx + f*v[1]*dx |
| 81 |
|
| 82 |
@fixture |
| 83 |
def a_blocks(a): |
| 84 |
return extract_blocks(a) |
| 85 |
|
| 86 |
@fixture |
| 87 |
def L_blocks(L): |
| 88 |
return extract_blocks(L) |
| 89 |
|
| 90 |
def test_size(space, a_blocks, L_blocks): |
| 91 |
assert len(L_blocks) == space.num_sub_spaces() |
| 92 |
assert len(a_blocks) == len(L_blocks)*len(L_blocks) |
| 93 |
|
| 94 |
def test_indices(space, a, L, a_blocks, L_blocks): |
| 95 |
for i in range(space.num_sub_spaces()): |
| 96 |
assert extract_blocks(L,i) == L_blocks[i] |
| 97 |
for j in range(space.num_sub_spaces()): |
| 98 |
assert extract_blocks(a,i,j) == a_blocks[i*space.num_sub_spaces()+j] |
| 99 |
|
| 100 |
def test_spaces(space, a_blocks, L_blocks): |
| 101 |
for i in range(space.num_sub_spaces()): |
| 102 |
L_form = _create_dolfin_form(L_blocks[i]) |
| 103 |
for j in range(space.num_sub_spaces()): |
| 104 |
if i == j : ## TEMPORARY : Do not conider non-diagonal blocks for now |
| 105 |
a_form = _create_dolfin_form(a_blocks[i*space.num_sub_spaces() + j]) |
| 106 |
assert a_form.function_space(0) == L_form.function_space(0) |
| 107 |
# TODO : When non-diagonal blocks will be ok |
| 108 |
# if i < space.num_sub_spaces()-1 : |
| 109 |
# a_form_next_row = _create_dolfin_form(a_blocks[(i+1)*space.num_sub_spaces() + j]) |
| 110 |
# assert a_form_next_row.function_space(1) == a_form.function_space(1) |
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|