| 1 |
"""Unit tests for the XML io library for vectors""" |
| 2 |
|
| 3 |
# Copyright (C) 2011-2014 Garth N. Wells |
| 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 |
import pytest |
| 21 |
from dolfin import * |
| 22 |
import os |
| 23 |
from dolfin_utils.test import skip_if_not_PETSc, fixture, cd_tempdir |
| 24 |
|
| 25 |
|
| 26 |
@skip_if_not_PETSc |
| 27 |
def test_save_vector_petsc(cd_tempdir): |
| 28 |
# Create vector and write file |
| 29 |
x = PETScVector(MPI.comm_world, 197) |
| 30 |
x[:] = 1.0 |
| 31 |
f = File("x.xml") |
| 32 |
f << x |
| 33 |
|
| 34 |
@skip_if_not_PETSc |
| 35 |
def test_save_gzipped_vector(cd_tempdir): |
| 36 |
# Create vector and write file |
| 37 |
x = PETScVector(MPI.comm_world, 197) |
| 38 |
x[:] = 1.0 |
| 39 |
f = File("x.xml.gz") |
| 40 |
f << x |
| 41 |
|
| 42 |
@skip_if_not_PETSc |
| 43 |
def test_read_vector_petcs(cd_tempdir): |
| 44 |
# Create vector and write file |
| 45 |
x = PETScVector(MPI.comm_world, 197) |
| 46 |
x[:] = 1.0 |
| 47 |
f = File("x.xml") |
| 48 |
f << x |
| 49 |
|
| 50 |
# Read vector from previous write |
| 51 |
y = PETScVector() |
| 52 |
f >> y |
| 53 |
assert x.size() == y.size() |
| 54 |
assert round(x.norm("l2") - y.norm("l2"), 7) == 0 |
| 55 |
|
| 56 |
|
| 57 |
@skip_if_not_PETSc |
| 58 |
def test_read_gzipped_vector(cd_tempdir): |
| 59 |
# Create vector and write file |
| 60 |
x = PETScVector(MPI.comm_world, 197) |
| 61 |
x[:] = 1.0 |
| 62 |
f = File("x.xml") |
| 63 |
f << x |
| 64 |
|
| 65 |
# Read vector from previous write |
| 66 |
y = PETScVector() |
| 67 |
f >> y |
| 68 |
assert x.size() == y.size() |
| 69 |
assert round(x.norm("l2") - y.norm("l2"), 7) == 0 |
| 70 |
|
| 71 |
def test_save_read_vector(cd_tempdir): |
| 72 |
size = 512 |
| 73 |
x = Vector(MPI.comm_world, size) |
| 74 |
x[:] = 1.0 |
| 75 |
|
| 76 |
out_file = File("test_vector_xml.xml") |
| 77 |
out_file << x |
| 78 |
|
| 79 |
y = Vector() |
| 80 |
out_file >> y |
| 81 |
assert x.size() == y.size() |
| 82 |
assert round((x - y).norm("l2") - 0.0, 7) == 0 |