| 1 |
# Copyright (C) 2012 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 |
import os |
| 20 |
from dolfin import * |
| 21 |
from dolfin_utils.test import skip_in_parallel, fixture, tempdir |
| 22 |
|
| 23 |
|
| 24 |
# Supported XDMF file encoding |
| 25 |
encodings = (XDMFFile.Encoding.HDF5, XDMFFile.Encoding.ASCII) |
| 26 |
|
| 27 |
# Data types supported in templating |
| 28 |
data_types = (('int', int), ('size_t', int), ('double', float), ('bool', bool)) |
| 29 |
|
| 30 |
# Finite elements tested |
| 31 |
fe_1d_shapes = ["interval"] |
| 32 |
fe_2d_shapes = ["triangle"] |
| 33 |
fe_3d_shapes = ["tetrahedron"] |
| 34 |
fe_families = ["CG", "DG"] |
| 35 |
fe_degrees = [0, 1, 3] |
| 36 |
mesh_tdims = [1, 2, 3] |
| 37 |
mesh_ns = [4, 11] |
| 38 |
|
| 39 |
# Meshes tested |
| 40 |
def mesh_factory(tdim, n): |
| 41 |
if tdim == 1: |
| 42 |
return UnitIntervalMesh(n) |
| 43 |
elif tdim == 2: |
| 44 |
return UnitSquareMesh(n, n) |
| 45 |
elif tdim == 3: |
| 46 |
return UnitCubeMesh(n, n, n) |
| 47 |
|
| 48 |
|
| 49 |
def invalid_config(encoding): |
| 50 |
return (not has_hdf5() and encoding == XDMFFile.Encoding.HDF5) \ |
| 51 |
or (encoding == XDMFFile.Encoding.ASCII and MPI.size(MPI.comm_world) > 1) \ |
| 52 |
or (not has_hdf5_parallel() and MPI.size(MPI.comm_world) > 1) |
| 53 |
|
| 54 |
|
| 55 |
def invalid_fe(fe_family, fe_degree): |
| 56 |
return (fe_family == "CG" and fe_degree == 0) |
| 57 |
|
| 58 |
|
| 59 |
@pytest.fixture |
| 60 |
def worker_id(request): |
| 61 |
"""Return worker ID when using pytest-xdist to run tests in |
| 62 |
parallell |
| 63 |
|
| 64 |
""" |
| 65 |
if hasattr(request.config, 'slaveinput'): |
| 66 |
return request.config.slaveinput['slaveid'] |
| 67 |
else: |
| 68 |
return 'master' |
| 69 |
|
| 70 |
|
| 71 |
@pytest.mark.parametrize("encoding", encodings) |
| 72 |
def test_save_and_load_1d_mesh(tempdir, encoding): |
| 73 |
if invalid_config(encoding): |
| 74 |
pytest.skip("XDMF unsupported in current configuration") |
| 75 |
filename = os.path.join(tempdir, "mesh.xdmf") |
| 76 |
mesh = UnitIntervalMesh(32) |
| 77 |
|
| 78 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 79 |
file.write(mesh, encoding) |
| 80 |
|
| 81 |
mesh2 = Mesh() |
| 82 |
with XDMFFile(MPI.comm_world, filename) as file: |
| 83 |
file.read(mesh2) |
| 84 |
assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) |
| 85 |
dim = mesh.topology().dim() |
| 86 |
assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim) |
| 87 |
|
| 88 |
|
| 89 |
@pytest.mark.parametrize("encoding", encodings) |
| 90 |
def test_save_and_load_2d_mesh(tempdir, encoding): |
| 91 |
if invalid_config(encoding): |
| 92 |
pytest.skip("XDMF unsupported in current configuration") |
| 93 |
filename = os.path.join(tempdir, "mesh_2D.xdmf") |
| 94 |
mesh = UnitSquareMesh(32, 32) |
| 95 |
|
| 96 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 97 |
file.write(mesh, encoding) |
| 98 |
|
| 99 |
mesh2 = Mesh() |
| 100 |
with XDMFFile(MPI.comm_world, filename) as file: |
| 101 |
file.read(mesh2) |
| 102 |
assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) |
| 103 |
dim = mesh.topology().dim() |
| 104 |
assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim) |
| 105 |
|
| 106 |
|
| 107 |
@pytest.mark.parametrize("encoding", encodings) |
| 108 |
def test_save_and_load_2d_quad_mesh(tempdir, encoding): |
| 109 |
if invalid_config(encoding): |
| 110 |
pytest.skip("XDMF unsupported in current configuration") |
| 111 |
filename = os.path.join(tempdir, "mesh_2D_quad.xdmf") |
| 112 |
mesh = UnitSquareMesh.create(32, 32, CellType.Type.quadrilateral) |
| 113 |
|
| 114 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 115 |
file.write(mesh, encoding) |
| 116 |
|
| 117 |
mesh2 = Mesh() |
| 118 |
with XDMFFile(MPI.comm_world, filename) as file: |
| 119 |
file.read(mesh2) |
| 120 |
assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) |
| 121 |
dim = mesh.topology().dim() |
| 122 |
assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim) |
| 123 |
|
| 124 |
|
| 125 |
@pytest.mark.parametrize("encoding", encodings) |
| 126 |
def test_save_and_load_3d_mesh(tempdir, encoding): |
| 127 |
if invalid_config(encoding): |
| 128 |
pytest.skip("XDMF unsupported in current configuration") |
| 129 |
filename = os.path.join(tempdir, "mesh_3D.xdmf") |
| 130 |
mesh = UnitCubeMesh(8, 8, 8) |
| 131 |
|
| 132 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 133 |
file.write(mesh, encoding) |
| 134 |
|
| 135 |
mesh2 = Mesh() |
| 136 |
with XDMFFile(MPI.comm_world, filename) as file: |
| 137 |
file.read(mesh2) |
| 138 |
assert mesh.num_entities_global(0) == mesh2.num_entities_global(0) |
| 139 |
dim = mesh.topology().dim() |
| 140 |
assert mesh.num_entities_global(dim) == mesh2.num_entities_global(dim) |
| 141 |
|
| 142 |
|
| 143 |
@pytest.mark.parametrize("encoding", encodings) |
| 144 |
def test_save_1d_scalar(tempdir, encoding): |
| 145 |
if invalid_config(encoding): |
| 146 |
pytest.skip("XDMF unsupported in current configuration") |
| 147 |
filename1 = os.path.join(tempdir, "u1.xdmf") |
| 148 |
filename2 = os.path.join(tempdir, "u1_.xdmf") |
| 149 |
mesh = UnitIntervalMesh(32) |
| 150 |
V = FunctionSpace(mesh, "Lagrange", 2) # FIXME: This randomly hangs in parallel |
| 151 |
u = Function(V) |
| 152 |
u.vector()[:] = 1.0 |
| 153 |
|
| 154 |
with XDMFFile(mesh.mpi_comm(), filename2) as file: |
| 155 |
file.write(u, encoding) |
| 156 |
|
| 157 |
|
| 158 |
@pytest.mark.parametrize("encoding", encodings) |
| 159 |
@pytest.mark.parametrize("fe_degree", fe_degrees) |
| 160 |
@pytest.mark.parametrize("fe_family", fe_families) |
| 161 |
@pytest.mark.parametrize("mesh_tdim", mesh_tdims) |
| 162 |
@pytest.mark.parametrize("mesh_n", mesh_ns) |
| 163 |
def test_save_and_checkpoint_scalar(tempdir, encoding, fe_degree, fe_family, |
| 164 |
mesh_tdim, mesh_n): |
| 165 |
if invalid_config(encoding): |
| 166 |
pytest.skip("XDMF unsupported in current configuration") |
| 167 |
|
| 168 |
if invalid_fe(fe_family, fe_degree): |
| 169 |
pytest.skip("Trivial finite element") |
| 170 |
|
| 171 |
filename = os.path.join(tempdir, "u1_checkpoint.xdmf") |
| 172 |
mesh = mesh_factory(mesh_tdim, mesh_n) |
| 173 |
FE = FiniteElement(fe_family, mesh.ufl_cell(), fe_degree) |
| 174 |
V = FunctionSpace(mesh, FE) |
| 175 |
u_in = Function(V) |
| 176 |
u_out = Function(V) |
| 177 |
|
| 178 |
u_out.interpolate(Expression("x[0]", degree=1)) |
| 179 |
|
| 180 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 181 |
file.write_checkpoint(u_out, "u_out", 0, encoding, append=False) |
| 182 |
|
| 183 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 184 |
file.read_checkpoint(u_in, "u_out", 0) |
| 185 |
|
| 186 |
result = u_in.vector() - u_out.vector() |
| 187 |
assert all([near(x, 0.0) for x in result.get_local()]) |
| 188 |
|
| 189 |
|
| 190 |
@pytest.mark.parametrize("encoding", encodings) |
| 191 |
@pytest.mark.parametrize("fe_degree", fe_degrees) |
| 192 |
@pytest.mark.parametrize("fe_family", fe_families) |
| 193 |
@pytest.mark.parametrize("mesh_tdim", mesh_tdims) |
| 194 |
@pytest.mark.parametrize("mesh_n", mesh_ns) |
| 195 |
def test_save_and_checkpoint_vector(tempdir, encoding, fe_degree, fe_family, |
| 196 |
mesh_tdim, mesh_n): |
| 197 |
if invalid_config(encoding): |
| 198 |
pytest.skip("XDMF unsupported in current configuration") |
| 199 |
|
| 200 |
if invalid_fe(fe_family, fe_degree): |
| 201 |
pytest.skip("Trivial finite element") |
| 202 |
|
| 203 |
filename = os.path.join(tempdir, "u2_checkpoint.xdmf") |
| 204 |
mesh = mesh_factory(mesh_tdim, mesh_n) |
| 205 |
FE = VectorElement(fe_family, mesh.ufl_cell(), fe_degree) |
| 206 |
V = FunctionSpace(mesh, FE) |
| 207 |
u_in = Function(V) |
| 208 |
u_out = Function(V) |
| 209 |
|
| 210 |
if mesh.geometry().dim() == 1: |
| 211 |
u_out.interpolate(Expression(("x[0]", ), degree=1)) |
| 212 |
elif mesh.geometry().dim() == 2: |
| 213 |
u_out.interpolate(Expression(("x[0]*x[1]", "x[0]"), degree=2)) |
| 214 |
elif mesh.geometry().dim() == 3: |
| 215 |
u_out.interpolate(Expression(("x[0]*x[1]", "x[0]", "x[2]"), degree=2)) |
| 216 |
|
| 217 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 218 |
file.write_checkpoint(u_out, "u_out", 0, encoding, append=False) |
| 219 |
|
| 220 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 221 |
file.read_checkpoint(u_in, "u_out", 0) |
| 222 |
|
| 223 |
result = u_in.vector() - u_out.vector() |
| 224 |
assert all([near(x, 0.0) for x in result.get_local()]) |
| 225 |
|
| 226 |
|
| 227 |
@pytest.mark.parametrize("encoding", encodings) |
| 228 |
def test_save_and_checkpoint_timeseries(tempdir, encoding): |
| 229 |
if invalid_config(encoding): |
| 230 |
pytest.skip("XDMF unsupported in current configuration") |
| 231 |
|
| 232 |
mesh = UnitSquareMesh(16, 16) |
| 233 |
filename = os.path.join(tempdir, "u2_checkpoint.xdmf") |
| 234 |
FE = FiniteElement("CG", mesh.ufl_cell(), 2) |
| 235 |
V = FunctionSpace(mesh, FE) |
| 236 |
|
| 237 |
times = [0.5, 0.2, 0.1] |
| 238 |
u_out = [None]*len(times) |
| 239 |
u_in = [None]*len(times) |
| 240 |
|
| 241 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 242 |
for i, p in enumerate(times): |
| 243 |
u_out[i] = interpolate(Expression("x[0]*p", p=p, degree=1), V) |
| 244 |
file.write_checkpoint(u_out[i], "u_out", p, encoding, append=True) |
| 245 |
|
| 246 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 247 |
for i, p in enumerate(times): |
| 248 |
u_in[i] = Function(V) |
| 249 |
file.read_checkpoint(u_in[i], "u_out", i) |
| 250 |
|
| 251 |
for i, p in enumerate(times): |
| 252 |
result = u_in[i].vector() - u_out[i].vector() |
| 253 |
assert all([near(x, 0.0) for x in result.get_local()]) |
| 254 |
|
| 255 |
# test reading last |
| 256 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 257 |
u_in_last = Function(V) |
| 258 |
file.read_checkpoint(u_in_last, "u_out", -1) |
| 259 |
|
| 260 |
result = u_out[-1].vector() - u_in_last.vector() |
| 261 |
assert all([near(x, 0.0) for x in result.get_local()]) |
| 262 |
|
| 263 |
|
| 264 |
@pytest.mark.parametrize("encoding", encodings) |
| 265 |
def test_save_2d_scalar(tempdir, encoding): |
| 266 |
if invalid_config(encoding): |
| 267 |
pytest.skip("XDMF unsupported in current configuration") |
| 268 |
filename = os.path.join(tempdir, "u2.xdmf") |
| 269 |
mesh = UnitSquareMesh(16, 16) |
| 270 |
V = FunctionSpace(mesh, "Lagrange", 2) # FIXME: This randomly hangs in parallel |
| 271 |
u = Function(V) |
| 272 |
u.vector()[:] = 1.0 |
| 273 |
|
| 274 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 275 |
file.write(u, encoding) |
| 276 |
|
| 277 |
|
| 278 |
@pytest.mark.parametrize("encoding", encodings) |
| 279 |
def test_save_3d_scalar(tempdir, encoding): |
| 280 |
if invalid_config(encoding): |
| 281 |
pytest.skip("XDMF unsupported in current configuration") |
| 282 |
filename = os.path.join(tempdir, "u3.xdmf") |
| 283 |
mesh = UnitCubeMesh(8, 8, 8) |
| 284 |
V = FunctionSpace(mesh, "Lagrange", 2) |
| 285 |
u = Function(V) |
| 286 |
u.vector()[:] = 1.0 |
| 287 |
|
| 288 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 289 |
file.write(u, encoding) |
| 290 |
|
| 291 |
|
| 292 |
@pytest.mark.parametrize("encoding", encodings) |
| 293 |
def test_save_2d_vector(tempdir, encoding): |
| 294 |
if invalid_config(encoding): |
| 295 |
pytest.skip("XDMF unsupported in current configuration") |
| 296 |
filename = os.path.join(tempdir, "u_2dv.xdmf") |
| 297 |
mesh = UnitSquareMesh(16, 16) |
| 298 |
V = VectorFunctionSpace(mesh, "Lagrange", 2) |
| 299 |
u = Function(V) |
| 300 |
c = Constant((1.0, 2.0)) |
| 301 |
u.interpolate(c) |
| 302 |
|
| 303 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 304 |
file.write(u, encoding) |
| 305 |
|
| 306 |
|
| 307 |
@pytest.mark.parametrize("encoding", encodings) |
| 308 |
def test_save_3d_vector(tempdir, encoding): |
| 309 |
if invalid_config(encoding): |
| 310 |
pytest.skip("XDMF unsupported in current configuration") |
| 311 |
filename = os.path.join(tempdir, "u_3Dv.xdmf") |
| 312 |
mesh = UnitCubeMesh(2, 2, 2) |
| 313 |
u = Function(VectorFunctionSpace(mesh, "Lagrange", 1)) |
| 314 |
c = Constant((1.0, 2.0, 3.0)) |
| 315 |
u.interpolate(c) |
| 316 |
|
| 317 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 318 |
file.write(u, encoding) |
| 319 |
|
| 320 |
|
| 321 |
@pytest.mark.parametrize("encoding", encodings) |
| 322 |
def test_save_3d_vector_series(tempdir, encoding): |
| 323 |
if invalid_config(encoding): |
| 324 |
pytest.skip("XDMF unsupported in current configuration") |
| 325 |
filename = os.path.join(tempdir, "u_3D.xdmf") |
| 326 |
mesh = UnitCubeMesh(8, 8, 8) |
| 327 |
u = Function(VectorFunctionSpace(mesh, "Lagrange", 2)) |
| 328 |
|
| 329 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 330 |
u.vector()[:] = 1.0 |
| 331 |
file.write(u, 0.1, encoding) |
| 332 |
|
| 333 |
u.vector()[:] = 2.0 |
| 334 |
file.write(u, 0.2, encoding) |
| 335 |
|
| 336 |
u.vector()[:] = 3.0 |
| 337 |
file.write(u, 0.3, encoding) |
| 338 |
|
| 339 |
|
| 340 |
@pytest.mark.parametrize("encoding", encodings) |
| 341 |
def test_save_2d_tensor(tempdir, encoding): |
| 342 |
if invalid_config(encoding): |
| 343 |
pytest.skip("XDMF unsupported in current configuration") |
| 344 |
filename = os.path.join(tempdir, "tensor.xdmf") |
| 345 |
mesh = UnitSquareMesh(16, 16) |
| 346 |
u = Function(TensorFunctionSpace(mesh, "Lagrange", 2)) |
| 347 |
u.vector()[:] = 1.0 |
| 348 |
|
| 349 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 350 |
file.write(u, encoding) |
| 351 |
|
| 352 |
|
| 353 |
@pytest.mark.parametrize("encoding", encodings) |
| 354 |
def test_save_3d_tensor(tempdir, encoding): |
| 355 |
if invalid_config(encoding): |
| 356 |
pytest.skip("XDMF unsupported in current configuration") |
| 357 |
filename = os.path.join(tempdir, "u3t.xdmf") |
| 358 |
mesh = UnitCubeMesh(8, 8, 8) |
| 359 |
u = Function(TensorFunctionSpace(mesh, "Lagrange", 2)) |
| 360 |
u.vector()[:] = 1.0 |
| 361 |
|
| 362 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 363 |
file.write(u, encoding) |
| 364 |
|
| 365 |
|
| 366 |
@pytest.mark.parametrize("encoding", encodings) |
| 367 |
def test_save_1d_mesh(tempdir, encoding): |
| 368 |
if invalid_config(encoding): |
| 369 |
pytest.skip("XDMF unsupported in current configuration") |
| 370 |
filename = os.path.join(tempdir, "mf_1D.xdmf") |
| 371 |
mesh = UnitIntervalMesh(32) |
| 372 |
mf = MeshFunction("size_t", mesh, mesh.topology().dim()) |
| 373 |
for cell in cells(mesh): |
| 374 |
mf[cell] = cell.index() |
| 375 |
|
| 376 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 377 |
file.write(mf, encoding) |
| 378 |
|
| 379 |
|
| 380 |
@pytest.mark.parametrize("encoding", encodings) |
| 381 |
@pytest.mark.parametrize("data_type", data_types) |
| 382 |
def test_save_2D_cell_function(tempdir, encoding, data_type): |
| 383 |
if invalid_config(encoding): |
| 384 |
pytest.skip("XDMF unsupported in current configuration") |
| 385 |
|
| 386 |
dtype_str, dtype = data_type |
| 387 |
|
| 388 |
filename = os.path.join(tempdir, "mf_2D_%s.xdmf" % dtype_str) |
| 389 |
mesh = UnitSquareMesh(32, 32) |
| 390 |
mf = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 391 |
mf.rename("cells", "cells") |
| 392 |
for cell in cells(mesh): |
| 393 |
mf[cell] = dtype(cell.index()) |
| 394 |
|
| 395 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 396 |
file.write(mf, encoding) |
| 397 |
|
| 398 |
mf_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 399 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 400 |
xdmf.read(mf_in, "cells") |
| 401 |
|
| 402 |
diff = 0 |
| 403 |
for cell in cells(mesh): |
| 404 |
diff += (mf_in[cell] - mf[cell]) |
| 405 |
assert diff == 0 |
| 406 |
|
| 407 |
|
| 408 |
@pytest.mark.parametrize("encoding", encodings) |
| 409 |
@pytest.mark.parametrize("data_type", data_types) |
| 410 |
def test_save_3D_cell_function(tempdir, encoding, data_type): |
| 411 |
if invalid_config(encoding): |
| 412 |
pytest.skip("XDMF unsupported in current configuration") |
| 413 |
|
| 414 |
dtype_str, dtype = data_type |
| 415 |
|
| 416 |
mesh = UnitCubeMesh(8, 8, 8) |
| 417 |
mf = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 418 |
mf.rename("cells", "cells") |
| 419 |
for cell in cells(mesh): |
| 420 |
mf[cell] = dtype(cell.index()) |
| 421 |
filename = os.path.join(tempdir, "mf_3D_%s.xdmf" % dtype_str) |
| 422 |
|
| 423 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 424 |
file.write(mf, encoding) |
| 425 |
|
| 426 |
mf_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 427 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 428 |
xdmf.read(mf_in, "cells") |
| 429 |
|
| 430 |
diff = 0 |
| 431 |
for cell in cells(mesh): |
| 432 |
diff += (mf_in[cell] - mf[cell]) |
| 433 |
assert diff == 0 |
| 434 |
|
| 435 |
@pytest.mark.parametrize("encoding", encodings) |
| 436 |
@pytest.mark.parametrize("data_type", data_types) |
| 437 |
def test_save_2D_facet_function(tempdir, encoding, data_type): |
| 438 |
if invalid_config(encoding): |
| 439 |
pytest.skip("XDMF unsupported in current configuration") |
| 440 |
|
| 441 |
dtype_str, dtype = data_type |
| 442 |
|
| 443 |
mesh = UnitSquareMesh(32, 32) |
| 444 |
mf = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 445 |
mf.rename("facets", "facets") |
| 446 |
|
| 447 |
if (MPI.size(mesh.mpi_comm()) == 1): |
| 448 |
for facet in facets(mesh): |
| 449 |
mf[facet] = dtype(facet.index()) |
| 450 |
else: |
| 451 |
for facet in facets(mesh): |
| 452 |
mf[facet] = dtype(facet.global_index()) |
| 453 |
filename = os.path.join(tempdir, "mf_facet_2D_%s.xdmf" % dtype_str) |
| 454 |
|
| 455 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 456 |
xdmf.write(mf, encoding) |
| 457 |
|
| 458 |
mf_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 459 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 460 |
xdmf.read(mf_in, "facets") |
| 461 |
|
| 462 |
diff = 0 |
| 463 |
for facet in facets(mesh): |
| 464 |
diff += (mf_in[facet] - mf[facet]) |
| 465 |
assert diff == 0 |
| 466 |
|
| 467 |
@pytest.mark.parametrize("encoding", encodings) |
| 468 |
@pytest.mark.parametrize("data_type", data_types) |
| 469 |
def test_save_3D_facet_function(tempdir, encoding, data_type): |
| 470 |
if invalid_config(encoding): |
| 471 |
pytest.skip("XDMF unsupported in current configuration") |
| 472 |
|
| 473 |
dtype_str, dtype = data_type |
| 474 |
|
| 475 |
mesh = UnitCubeMesh(8, 8, 8) |
| 476 |
mf = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 477 |
mf.rename("facets", "facets") |
| 478 |
|
| 479 |
if (MPI.size(mesh.mpi_comm()) == 1): |
| 480 |
for facet in facets(mesh): |
| 481 |
mf[facet] = dtype(facet.index()) |
| 482 |
else: |
| 483 |
for facet in facets(mesh): |
| 484 |
mf[facet] = dtype(facet.global_index()) |
| 485 |
filename = os.path.join(tempdir, "mf_facet_3D_%s.xdmf" % dtype_str) |
| 486 |
|
| 487 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 488 |
file.write(mf, encoding) |
| 489 |
|
| 490 |
mf_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 491 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 492 |
file.read(mf_in, "facets") |
| 493 |
|
| 494 |
diff = 0 |
| 495 |
for facet in facets(mesh): |
| 496 |
diff += (mf_in[facet] - mf[facet]) |
| 497 |
assert diff == 0 |
| 498 |
|
| 499 |
@pytest.mark.parametrize("encoding", encodings) |
| 500 |
@pytest.mark.parametrize("data_type", data_types) |
| 501 |
def test_save_3D_edge_function(tempdir, encoding, data_type): |
| 502 |
if invalid_config(encoding): |
| 503 |
pytest.skip("XDMF unsupported in current configuration") |
| 504 |
|
| 505 |
dtype_str, dtype = data_type |
| 506 |
|
| 507 |
mesh = UnitCubeMesh(8, 8, 8) |
| 508 |
mf = MeshFunction(dtype_str, mesh, 1) |
| 509 |
mf.rename("edges", "edges") |
| 510 |
for edge in edges(mesh): |
| 511 |
mf[edge] = dtype(edge.index()) |
| 512 |
|
| 513 |
filename = os.path.join(tempdir, "mf_edge_3D_%s.xdmf" % dtype_str) |
| 514 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 515 |
file.write(mf, encoding) |
| 516 |
|
| 517 |
|
| 518 |
@pytest.mark.parametrize("encoding", encodings) |
| 519 |
@pytest.mark.parametrize("data_type", data_types) |
| 520 |
def test_save_2D_vertex_function(tempdir, encoding, data_type): |
| 521 |
if invalid_config(encoding): |
| 522 |
pytest.skip("XDMF unsupported in current configuration") |
| 523 |
|
| 524 |
dtype_str, dtype = data_type |
| 525 |
|
| 526 |
mesh = UnitSquareMesh(32, 32) |
| 527 |
mf = MeshFunction(dtype_str, mesh, 0) |
| 528 |
mf.rename("vertices", "vertices") |
| 529 |
for vertex in vertices(mesh): |
| 530 |
mf[vertex] = dtype(vertex.global_index()) |
| 531 |
filename = os.path.join(tempdir, "mf_vertex_2D_%s.xdmf" % dtype_str) |
| 532 |
|
| 533 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 534 |
file.write(mf, encoding) |
| 535 |
|
| 536 |
mf_in = MeshFunction(dtype_str, mesh, 0) |
| 537 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 538 |
xdmf.read(mf_in, "vertices") |
| 539 |
|
| 540 |
diff = 0 |
| 541 |
for v in vertices(mesh): |
| 542 |
diff += (mf_in[v] - mf[v]) |
| 543 |
assert diff == 0 |
| 544 |
|
| 545 |
@pytest.mark.parametrize("encoding", encodings) |
| 546 |
@pytest.mark.parametrize("data_type", data_types) |
| 547 |
def test_save_3D_vertex_function(tempdir, encoding, data_type): |
| 548 |
if invalid_config(encoding): |
| 549 |
pytest.skip("XDMF unsupported in current configuration") |
| 550 |
|
| 551 |
dtype_str, dtype = data_type |
| 552 |
|
| 553 |
filename = os.path.join(tempdir, "mf_vertex_3D_%s.xdmf" % dtype_str) |
| 554 |
mesh = UnitCubeMesh(8, 8, 8) |
| 555 |
mf = MeshFunction(dtype_str, mesh, 0) |
| 556 |
for vertex in vertices(mesh): |
| 557 |
mf[vertex] = dtype(vertex.index()) |
| 558 |
|
| 559 |
with XDMFFile(mesh.mpi_comm(), filename) as file: |
| 560 |
file.write(mf, encoding) |
| 561 |
|
| 562 |
@pytest.mark.parametrize("encoding", encodings) |
| 563 |
def test_save_points_2D(tempdir, encoding): |
| 564 |
if invalid_config(encoding): |
| 565 |
pytest.skip("XDMF unsupported in current configuration") |
| 566 |
import numpy |
| 567 |
mesh = UnitSquareMesh(16, 16) |
| 568 |
points, values = [], [] |
| 569 |
for v in vertices(mesh): |
| 570 |
points.append(v.point()) |
| 571 |
values.append(v.point().norm()) |
| 572 |
vals = numpy.array(values) |
| 573 |
|
| 574 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_2D.xdmf")) as file: |
| 575 |
file.write(points, encoding) |
| 576 |
|
| 577 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, |
| 578 |
"points_values_2D.xdmf")) as file: |
| 579 |
file.write(points, vals, encoding) |
| 580 |
|
| 581 |
|
| 582 |
@pytest.mark.parametrize("encoding", encodings) |
| 583 |
def test_save_points_3D(tempdir, encoding): |
| 584 |
if invalid_config(encoding): |
| 585 |
pytest.skip("XDMF unsupported in current configuration") |
| 586 |
import numpy |
| 587 |
mesh = UnitCubeMesh(4, 4, 4) |
| 588 |
points, values = [], [] |
| 589 |
for v in vertices(mesh): |
| 590 |
points.append(v.point()) |
| 591 |
values.append(v.point().norm()) |
| 592 |
vals = numpy.array(values) |
| 593 |
|
| 594 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_3D.xdmf")) as file: |
| 595 |
file.write(points, encoding) |
| 596 |
|
| 597 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "points_values_3D.xdmf")) as file: |
| 598 |
file.write(points, vals, encoding) |
| 599 |
|
| 600 |
|
| 601 |
@pytest.mark.parametrize("encoding", encodings) |
| 602 |
@pytest.mark.parametrize("data_type", data_types) |
| 603 |
def test_save_mesh_value_collection(tempdir, encoding, data_type): |
| 604 |
if invalid_config(encoding): |
| 605 |
pytest.skip("XDMF unsupported in current configuration") |
| 606 |
|
| 607 |
dtype_str, dtype = data_type |
| 608 |
|
| 609 |
mesh = UnitCubeMesh(4, 4, 4) |
| 610 |
tdim = mesh.topology().dim() |
| 611 |
|
| 612 |
meshfn = MeshFunction(dtype_str, mesh, mesh.topology().dim(), False) |
| 613 |
meshfn.rename("volume_marker", "Volume Markers") |
| 614 |
for c in cells(mesh): |
| 615 |
if c.midpoint().y() > 0.1: |
| 616 |
meshfn[c] = dtype(1) |
| 617 |
if c.midpoint().y() > 0.9: |
| 618 |
meshfn[c] = dtype(2) |
| 619 |
|
| 620 |
for mvc_dim in range(0, tdim + 1): |
| 621 |
mvc = MeshValueCollection(dtype_str, mesh, mvc_dim) |
| 622 |
tag = "dim_%d_marker" % mvc_dim |
| 623 |
mvc.rename(tag, "BC") |
| 624 |
mesh.init(mvc_dim, tdim) |
| 625 |
for e in entities(mesh, mvc_dim): |
| 626 |
if (e.midpoint().x() > 0.5): |
| 627 |
mvc.set_value(e.index(), dtype(1)) |
| 628 |
|
| 629 |
filename = os.path.join(tempdir, "mvc_%d.xdmf" % mvc_dim) |
| 630 |
|
| 631 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 632 |
xdmf.write(meshfn, encoding) |
| 633 |
xdmf.write(mvc, encoding) |
| 634 |
|
| 635 |
|
| 636 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 637 |
mvc = MeshValueCollection(dtype_str, mesh) |
| 638 |
xdmf.read(mvc, tag) |
| 639 |
|
| 640 |
|
| 641 |
@skip_in_parallel |
| 642 |
@pytest.mark.parametrize("encoding", encodings) |
| 643 |
def test_quadratic_mesh(tempdir, encoding): |
| 644 |
if invalid_config(encoding): |
| 645 |
pytest.skip("XDMF unsupported in current configuration") |
| 646 |
mesh = UnitDiscMesh.create(MPI.comm_world, 2, 2, 2) |
| 647 |
Q = FunctionSpace(mesh, "CG", 1) |
| 648 |
u = Function(Q) |
| 649 |
u.interpolate(Constant(1.0)) |
| 650 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "quadratic1.xdmf")) as xdmf: |
| 651 |
xdmf.write(u) |
| 652 |
|
| 653 |
Q = FunctionSpace(mesh, "CG", 2) |
| 654 |
u = Function(Q) |
| 655 |
u.interpolate(Constant(1.0)) |
| 656 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "quadratic2.xdmf")) as xdmf: |
| 657 |
xdmf.write(u) |
| 658 |
|
| 659 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "qmesh.xdmf")) as xdmf: |
| 660 |
xdmf.write(mesh) |
| 661 |
c0 = mesh.coordinates() |
| 662 |
|
| 663 |
mesh = Mesh() |
| 664 |
with XDMFFile(mesh.mpi_comm(), os.path.join(tempdir, "qmesh.xdmf")) as xdmf: |
| 665 |
xdmf.read(mesh) |
| 666 |
c1 = mesh.coordinates() |
| 667 |
|
| 668 |
assert (c0 - c1).sum() == 0.0 |
| 669 |
|
| 670 |
|
| 671 |
@pytest.mark.parametrize("encoding", encodings) |
| 672 |
@pytest.mark.parametrize("data_type", data_types) |
| 673 |
def test_append_and_load_mesh_functions(tempdir, encoding, data_type): |
| 674 |
if invalid_config(encoding): |
| 675 |
pytest.skip("XDMF unsupported in current configuration") |
| 676 |
|
| 677 |
dtype_str, dtype = data_type |
| 678 |
|
| 679 |
meshes = [UnitSquareMesh(32, 32), UnitCubeMesh(8, 8, 8)] |
| 680 |
|
| 681 |
for mesh in meshes: |
| 682 |
dim = mesh.topology().dim() |
| 683 |
|
| 684 |
vf = MeshFunction(dtype_str, mesh, 0) |
| 685 |
vf.rename("vertices", "vertices") |
| 686 |
ff = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 687 |
ff.rename("facets", "facets") |
| 688 |
cf = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 689 |
cf.rename("cells", "cells") |
| 690 |
|
| 691 |
if (MPI.size(mesh.mpi_comm()) == 1): |
| 692 |
for vertex in vertices(mesh): |
| 693 |
vf[vertex] = dtype(vertex.index()) |
| 694 |
for facet in facets(mesh): |
| 695 |
ff[facet] = dtype(facet.index()) |
| 696 |
for cell in cells(mesh): |
| 697 |
cf[cell] = dtype(cell.index()) |
| 698 |
else: |
| 699 |
for vertex in vertices(mesh): |
| 700 |
vf[vertex] = dtype(vertex.global_index()) |
| 701 |
for facet in facets(mesh): |
| 702 |
ff[facet] = dtype(facet.global_index()) |
| 703 |
for cell in cells(mesh): |
| 704 |
cf[cell] = dtype(cell.global_index()) |
| 705 |
|
| 706 |
filename = os.path.join(tempdir, "appended_mf_%dD.xdmf" % dim) |
| 707 |
|
| 708 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 709 |
xdmf.write(mesh) |
| 710 |
xdmf.write(vf, encoding) |
| 711 |
xdmf.write(ff, encoding) |
| 712 |
xdmf.write(cf, encoding) |
| 713 |
|
| 714 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 715 |
vf_in = MeshFunction(dtype_str, mesh, 0) |
| 716 |
xdmf.read(vf_in, "vertices") |
| 717 |
ff_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()-1) |
| 718 |
xdmf.read(ff_in, "facets") |
| 719 |
cf_in = MeshFunction(dtype_str, mesh, mesh.topology().dim()) |
| 720 |
xdmf.read(cf_in, "cells") |
| 721 |
|
| 722 |
diff = 0 |
| 723 |
for vertex in vertices(mesh): |
| 724 |
diff += (vf_in[vertex] - vf[vertex]) |
| 725 |
for facet in facets(mesh): |
| 726 |
diff += (ff_in[facet] - ff[facet]) |
| 727 |
for cell in cells(mesh): |
| 728 |
diff += (cf_in[cell] - cf[cell]) |
| 729 |
assert diff == 0 |
| 730 |
|
| 731 |
|
| 732 |
@pytest.mark.parametrize("encoding", encodings) |
| 733 |
@pytest.mark.parametrize("data_type", data_types) |
| 734 |
def test_append_and_load_mesh_value_collections(tempdir, encoding, data_type): |
| 735 |
if invalid_config(encoding): |
| 736 |
pytest.skip("XDMF unsupported in current configuration") |
| 737 |
|
| 738 |
dtype_str, dtype = data_type |
| 739 |
|
| 740 |
mesh = UnitCubeMesh(8, 8, 8) |
| 741 |
mesh.init() |
| 742 |
for d in range(mesh.geometry().dim() + 1): |
| 743 |
mesh.init_global(d) |
| 744 |
|
| 745 |
mvc_v = MeshValueCollection(dtype_str, mesh, 0) |
| 746 |
mvc_v.rename("vertices", "vertices") |
| 747 |
mvc_e = MeshValueCollection(dtype_str, mesh, 1) |
| 748 |
mvc_e.rename("edges", "edges") |
| 749 |
mvc_f = MeshValueCollection(dtype_str, mesh, 2) |
| 750 |
mvc_f.rename("facets", "facets") |
| 751 |
mvc_c = MeshValueCollection(dtype_str, mesh, 3) |
| 752 |
mvc_c.rename("cells", "cells") |
| 753 |
|
| 754 |
mvcs = [mvc_v, mvc_e, mvc_f, mvc_c] |
| 755 |
|
| 756 |
filename = os.path.join(tempdir, "appended_mvcs.xdmf") |
| 757 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 758 |
for mvc in mvcs: |
| 759 |
for ent in entities(mesh, mvc.dim()): |
| 760 |
assert(mvc.set_value(ent.index(), dtype(ent.global_index()))) |
| 761 |
xdmf.write(mvc) |
| 762 |
|
| 763 |
mvc_v_in = MeshValueCollection(dtype_str, mesh, 0) |
| 764 |
mvc_e_in = MeshValueCollection(dtype_str, mesh, 1) |
| 765 |
mvc_f_in = MeshValueCollection(dtype_str, mesh, 2) |
| 766 |
mvc_c_in = MeshValueCollection(dtype_str, mesh, 3) |
| 767 |
|
| 768 |
with XDMFFile(mesh.mpi_comm(), filename) as xdmf: |
| 769 |
xdmf.read(mvc_v_in, "vertices") |
| 770 |
xdmf.read(mvc_e_in, "edges") |
| 771 |
xdmf.read(mvc_f_in, "facets") |
| 772 |
xdmf.read(mvc_c_in, "cells") |
| 773 |
|
| 774 |
mvcs_in = [mvc_v_in, mvc_e_in, mvc_f_in, mvc_c_in] |
| 775 |
|
| 776 |
for (mvc, mvc_in) in zip(mvcs, mvcs_in): |
| 777 |
mf = MeshFunction(dtype_str, mesh, mvc) |
| 778 |
mf_in = MeshFunction(dtype_str, mesh, mvc_in) |
| 779 |
|
| 780 |
diff = 0 |
| 781 |
for ent in entities(mesh, mf.dim()): |
| 782 |
diff += (mf_in[ent] - mf[ent]) |
| 783 |
assert(diff == 0) |