Debian Codegraph @

python/test/unit/io/test_XML_mesh_value_collection.py

raw
1 "Unit tests for XML input/output of MeshValueCollection"
2
3 # Copyright (C) 2011 Anders Logg
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 fixture, skip_in_parallel, cd_tempdir, filedir
24
25 @skip_in_parallel
26 def test_insertion_extraction_io(cd_tempdir):
27 "Test input/output via << and >>."
28 filename = "xml_mesh_value_collection_test_io.xml"
29
30 # Create mesh
31 mesh = UnitCubeMesh(5, 5, 5)
32
33 # Create mesh value collection and add some data
34 mesh = UnitCubeMesh(5, 5, 5)
35 output_values = MeshValueCollection("size_t", mesh, 2)
36 output_values.set_value(1, 1, 1);
37 output_values.set_value(2, 1, 3);
38 output_values.set_value(5, 1, 8);
39 output_values.set_value(13, 1, 21);
40 output_values.set_value(7, 2, 13);
41 output_values.set_value(4, 2, 2);
42
43 name = "test_mesh_value_collection"
44 output_values.rename(name, "a MeshValueCollection")
45
46 # Write to file
47 output_file = File(filename)
48 output_file << output_values
49
50 # Read from file
51 input_file = File(filename)
52 input_values = MeshValueCollection("size_t", mesh)
53 input_file >> input_values
54
55 # Get some data and check that it matches
56 assert input_values.size() == output_values.size()
57 assert input_values.dim() == output_values.dim()
58 assert input_values.name() == name
59
60 def test_constructor_input(filedir):
61 "Test input via constructor."
62 filename = os.path.join(filedir, "xml_value_collection_ref.xml")
63
64 # Create mesh
65 mesh = UnitCubeMesh(5, 5, 5)
66
67 # Read from file
68 input_values = MeshValueCollection("size_t", mesh, filename)
69
70 # Check that size is correct
71 assert MPI.sum(mesh.mpi_comm(), input_values.size()) == 6