| 1 |
# Copyright (C) 2011 Garth N. Wells |
| 2 |
# |
| 3 |
# This file is part of DOLFIN. |
| 4 |
# |
| 5 |
# DOLFIN is free software: you can redistribute it and/or modify |
| 6 |
# it under the terms of the GNU Lesser General Public License as published by |
| 7 |
# the Free Software Foundation, either version 3 of the License, or |
| 8 |
# (at your option) any later version. |
| 9 |
# |
| 10 |
# DOLFIN is distributed in the hope that it will be useful, |
| 11 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
# GNU Lesser General Public License for more details. |
| 14 |
# |
| 15 |
# You should have received a copy of the GNU Lesser General Public License |
| 16 |
# along with DOLFIN. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
|
| 18 |
import pytest |
| 19 |
from dolfin import * |
| 20 |
import os |
| 21 |
from dolfin_utils.test import skip_in_parallel, fixture, tempdir |
| 22 |
|
| 23 |
# VTK file options |
| 24 |
@fixture |
| 25 |
def file_options(): |
| 26 |
return ["ascii", "base64", "compressed"] |
| 27 |
|
| 28 |
@fixture |
| 29 |
def mesh_function_types(): |
| 30 |
return ["size_t", "int", "double", "bool"] |
| 31 |
|
| 32 |
@fixture |
| 33 |
def type_conv(): |
| 34 |
return dict(size_t=int, int=int, double=float, bool=bool) |
| 35 |
|
| 36 |
@pytest.fixture(scope="function") |
| 37 |
def tempfile(tempdir, request): |
| 38 |
return os.path.join(tempdir, request.function.__name__) |
| 39 |
|
| 40 |
def test_save_1d_meshfunctions(tempfile, |
| 41 |
mesh_function_types, file_options, type_conv): |
| 42 |
mesh = UnitIntervalMesh(32) |
| 43 |
for d in range(mesh.topology().dim()+1): |
| 44 |
for t in mesh_function_types: |
| 45 |
mf = MeshFunction(t, mesh, mesh.topology().dim()-d, type_conv[t](1)) |
| 46 |
File(tempfile + "mf.pvd") << mf |
| 47 |
f = File(tempfile + "mf.pvd") |
| 48 |
f << (mf, 0.) |
| 49 |
f << (mf, 1.) |
| 50 |
for file_option in file_options: |
| 51 |
File(tempfile + "mf.pvd", file_option) << mf |
| 52 |
|
| 53 |
def test_save_2d_meshfunctions(tempfile, |
| 54 |
mesh_function_types, file_options, type_conv): |
| 55 |
mesh = UnitSquareMesh(32, 32) |
| 56 |
for d in range(mesh.topology().dim()+1): |
| 57 |
for t in mesh_function_types: |
| 58 |
mf = MeshFunction(t, mesh, mesh.topology().dim()-d, type_conv[t](1)) |
| 59 |
File(tempfile + "mf.pvd") << mf |
| 60 |
f = File(tempfile + "mf.pvd") |
| 61 |
f << (mf, 0.) |
| 62 |
f << (mf, 1.) |
| 63 |
for file_option in file_options: |
| 64 |
File(tempfile + "mf.pvd", file_option) << mf |
| 65 |
|
| 66 |
def test_save_3d_meshfunctions(tempfile, |
| 67 |
mesh_function_types, file_options, type_conv): |
| 68 |
mesh = UnitCubeMesh(8, 8, 8) |
| 69 |
for d in range(mesh.topology().dim()+1): |
| 70 |
for t in mesh_function_types: |
| 71 |
mf = MeshFunction(t, mesh, mesh.topology().dim()-d, type_conv[t](1)) |
| 72 |
File(tempfile + "mf.pvd") << mf |
| 73 |
f = File(tempfile + "mf.pvd") |
| 74 |
f << (mf, 0.) |
| 75 |
f << (mf, 1.) |
| 76 |
for file_option in file_options: |
| 77 |
File(tempfile + "mf.pvd", file_option) << mf |
| 78 |
|
| 79 |
def test_save_1d_mesh(tempfile, file_options): |
| 80 |
mesh = UnitIntervalMesh(32) |
| 81 |
File(tempfile + "mesh.pvd") << mesh |
| 82 |
f = File(tempfile + "mesh.pvd") |
| 83 |
f << (mesh, 0.) |
| 84 |
f << (mesh, 1.) |
| 85 |
for file_option in file_options: |
| 86 |
File(tempfile + "mesh.pvd", file_option) << mesh |
| 87 |
|
| 88 |
def test_save_2d_mesh(tempfile, file_options): |
| 89 |
mesh = UnitSquareMesh(32, 32) |
| 90 |
File(tempfile + "mesh.pvd") << mesh |
| 91 |
f = File(tempfile + "mesh.pvd") |
| 92 |
f << (mesh, 0.) |
| 93 |
f << (mesh, 1.) |
| 94 |
for file_option in file_options: |
| 95 |
File(tempfile + "mesh.pvd", file_option) << mesh |
| 96 |
|
| 97 |
def test_save_3d_mesh(tempfile, file_options): |
| 98 |
mesh = UnitCubeMesh(8, 8, 8) |
| 99 |
File(tempfile + "mesh.pvd") << mesh |
| 100 |
f = File(tempfile + "mesh.pvd") |
| 101 |
f << (mesh, 0.) |
| 102 |
f << (mesh, 1.) |
| 103 |
for file_option in file_options: |
| 104 |
File(tempfile + "mesh.pvd", file_option) << mesh |
| 105 |
|
| 106 |
def test_save_1d_scalar(tempfile, file_options): |
| 107 |
mesh = UnitIntervalMesh(32) |
| 108 |
u = Function(FunctionSpace(mesh, "Lagrange", 2)) |
| 109 |
u.vector()[:] = 1.0 |
| 110 |
File(tempfile + "u.pvd") << u |
| 111 |
f = File(tempfile + "u.pvd") |
| 112 |
f << (u, 0.) |
| 113 |
f << (u, 1.) |
| 114 |
for file_option in file_options: |
| 115 |
File(tempfile + "u.pvd", file_option) << u |
| 116 |
|
| 117 |
def test_save_2d_scalar(tempfile, file_options): |
| 118 |
mesh = UnitSquareMesh(16, 16) |
| 119 |
u = Function(FunctionSpace(mesh, "Lagrange", 2)) |
| 120 |
u.vector()[:] = 1.0 |
| 121 |
File(tempfile + "u.pvd") << u |
| 122 |
f = File(tempfile + "u.pvd") |
| 123 |
f << (u, 0.) |
| 124 |
f << (u, 1.) |
| 125 |
for file_option in file_options: |
| 126 |
File(tempfile + "u.pvd", file_option) << u |
| 127 |
|
| 128 |
def test_save_3d_scalar(tempfile, file_options): |
| 129 |
mesh = UnitCubeMesh(8, 8, 8) |
| 130 |
u = Function(FunctionSpace(mesh, "Lagrange", 2)) |
| 131 |
u.vector()[:] = 1.0 |
| 132 |
File(tempfile + "u.pvd") << u |
| 133 |
f = File(tempfile + "u.pvd") |
| 134 |
f << (u, 0.) |
| 135 |
f << (u, 1.) |
| 136 |
for file_option in file_options: |
| 137 |
File(tempfile + "u.pvd", file_option) << u |
| 138 |
|
| 139 |
@pytest.mark.xfail(reason="FFC fails for tensor spaces in 1D") |
| 140 |
@skip_in_parallel |
| 141 |
def test_save_1d_vector(tempfile, file_options): |
| 142 |
mesh = UnitIntervalMesh(32) |
| 143 |
u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) |
| 144 |
u.vector()[:] = 1.0 |
| 145 |
File(tempfile + "u.pvd") << u |
| 146 |
for file_option in file_options: |
| 147 |
File(tempfile + "u.pvd", file_option) << u |
| 148 |
|
| 149 |
def test_save_2d_vector(tempfile, file_options): |
| 150 |
mesh = UnitSquareMesh(16, 16) |
| 151 |
u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) |
| 152 |
u.vector()[:] = 1.0 |
| 153 |
File(tempfile + "u.pvd") << u |
| 154 |
f = File(tempfile + "u.pvd") |
| 155 |
f << (u, 0.) |
| 156 |
f << (u, 1.) |
| 157 |
for file_option in file_options: |
| 158 |
File(tempfile + "u.pvd", file_option) << u |
| 159 |
|
| 160 |
def test_save_3d_vector(tempfile, file_options): |
| 161 |
mesh = UnitCubeMesh(8, 8, 8) |
| 162 |
u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) |
| 163 |
u.vector()[:] = 1.0 |
| 164 |
File(tempfile + "u.pvd") << u |
| 165 |
f = File(tempfile + "u.pvd") |
| 166 |
f << (u, 0.) |
| 167 |
f << (u, 1.) |
| 168 |
for file_option in file_options: |
| 169 |
File(tempfile + "u.pvd", file_option) << u |
| 170 |
|
| 171 |
@pytest.mark.xfail(reason="FFC fails for tensor spaces in 1D") |
| 172 |
@skip_in_parallel |
| 173 |
def test_save_1d_tensor(tempfile, file_options): |
| 174 |
mesh = UnitIntervalMesh(32) |
| 175 |
u = Function(TensorFunctionSpace(mesh, "Lagrange", 2)) |
| 176 |
u.vector()[:] = 1.0 |
| 177 |
File(tempfile + "u.pvd") << u |
| 178 |
for file_option in file_options: |
| 179 |
File(tempfile + "u.pvd", file_option) << u |
| 180 |
|
| 181 |
def test_save_2d_tensor(tempfile, file_options): |
| 182 |
mesh = UnitSquareMesh(16, 16) |
| 183 |
u = Function(TensorFunctionSpace(mesh, "Lagrange", 2)) |
| 184 |
u.vector()[:] = 1.0 |
| 185 |
File(tempfile + "u.pvd") << u |
| 186 |
f = File(tempfile + "u.pvd") |
| 187 |
f << (u, 0.) |
| 188 |
f << (u, 1.) |
| 189 |
for file_option in file_options: |
| 190 |
File(tempfile + "u.pvd", file_option) << u |
| 191 |
|
| 192 |
def test_save_3d_tensor(tempfile, file_options): |
| 193 |
mesh = UnitCubeMesh(8, 8, 8) |
| 194 |
u = Function(TensorFunctionSpace(mesh, "Lagrange", 2)) |
| 195 |
u.vector()[:] = 1.0 |
| 196 |
File(tempfile + "u.pvd") << u |
| 197 |
f = File(tempfile + "u.pvd") |
| 198 |
f << (u, 0.) |
| 199 |
f << (u, 1.) |
| 200 |
for file_option in file_options: |
| 201 |
File(tempfile + "u.pvd", file_option) << u |