Debian Codegraph @

python/test/unit/io/test_XYZ.py

raw
1 # Copyright (C) 2017 Michal Habera
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 numpy as np
20
21 import os
22
23 from dolfin import *
24 from dolfin_utils.test import skip_in_parallel, fixture, tempdir
25
26
27 @skip_in_parallel
28 def test_save_2d_scalar(tempdir):
29
30 mesh = UnitSquareMesh(16, 16)
31 filename = os.path.join(tempdir, "scalar2D.xyz")
32 file = File(filename)
33 V = FunctionSpace(mesh, "Lagrange", 1)
34 u = Function(V)
35 u.interpolate(Constant(1.0))
36
37 # Save results multiple times for append test case
38 N = 10
39 for i in range(N):
40 file << u
41
42 # Load name of datafile, stored in main file (usually scalar2D******.xyz)
43 with open(filename, 'r') as fl:
44 data_filenames = fl.readlines()
45
46 # Check if there are really N lines in main file
47 assert len(data_filenames) == N
48
49 for data_filename in data_filenames:
50
51 # Load saved file as np array
52 data_filename = data_filename.replace('\n', '')
53 loaded_file = np.loadtxt(os.path.join(tempdir, data_filename))
54
55 # Check if the loaded function is everywhere == 1.
56 assert (loaded_file[:, 2] == 1.).all()