Peano
Loading...
Searching...
No Matches
test_unknown_names.py
Go to the documentation of this file.
1import vtk
2
3from tarch.plotter.api.input.PatchFileParser import unknown_names_from_description
5 VTUUnstructuredGrid,
6 group_unknown_names,
7)
8
9from test_point_probe import patch_file
10
11
12# ---------------------------------------------------------------------------
13# Reading the names from the description
14# ---------------------------------------------------------------------------
15
16
18 assert unknown_names_from_description(" h, hu, hv, b", 4) == ["h", "hu", "hv", "b"]
19
20
22 assert unknown_names_from_description("p, v[0], v[1]", 3) == ["p", "v[0]", "v[1]"]
23
24
26 assert unknown_names_from_description("h, hu, hv", 4) is None
27
28
30 assert unknown_names_from_description("SWEQ", 4) is None
31 assert unknown_names_from_description("test data", 1) is None
32 assert unknown_names_from_description("", 1) is None
33
34
36 assert group_unknown_names(["p", "v[0]", "v[1]", "b"]) == [
37 ("p", [0]),
38 ("v", [1, 2]),
39 ("b", [3]),
40 ]
41
42
43# ---------------------------------------------------------------------------
44# VTU output
45# ---------------------------------------------------------------------------
46
47
48def convert(tmp_path, monkeypatch, description, split_unknowns):
49 """Convert one patch with 2x2 cells and three unknowns into a vtu file"""
50 monkeypatch.chdir(tmp_path)
51 (tmp_path / "data.peano-patch-file").write_text(
52 patch_file(
53 2, "cell", "Q", 2, 3, [(1, (0.0, 0.0), (1.0, 1.0))], description=description
54 )
55 )
56 visualiser = VTUUnstructuredGrid(
57 "data.peano-patch-file", output_directory=".", split_unknowns=split_unknowns
58 )
59 file_name = visualiser.display_single_file()["Q"]
60 reader = vtk.vtkXMLUnstructuredGridReader()
61 reader.SetFileName(file_name)
62 reader.Update()
63 return reader.GetOutput().GetCellData()
64
65
66def test_components_carry_names(tmp_path, monkeypatch):
67 cell_data = convert(tmp_path, monkeypatch, "p, v[0], v[1]", False)
68 assert cell_data.GetNumberOfArrays() == 1
69 data = cell_data.GetArray(0)
70 assert [data.GetComponentName(i) for i in range(4)] == [
71 "p",
72 "v[0]",
73 "v[1]",
74 "subdomain",
75 ]
76
77
79 cell_data = convert(tmp_path, monkeypatch, "p, v[0], v[1]", True)
80 p = cell_data.GetArray("p")
81 v = cell_data.GetArray("v")
82 assert p.GetNumberOfComponents() == 1
83 assert v.GetNumberOfComponents() == 3
84 # Values encode 100*patch + 10*dof + unknown, cf. test_point_probe
85 assert p.GetTuple(3) == (130.0,)
86 assert v.GetTuple(3) == (131.0, 132.0, 0.0)
87
88
89def test_no_names_no_labels(tmp_path, monkeypatch):
90 cell_data = convert(tmp_path, monkeypatch, "test data", True)
91 assert cell_data.GetNumberOfArrays() == 1
92 assert cell_data.GetArray(0).GetComponentName(0) is None
The visualiser is first and foremost a persistency layer around datasets.
test_components_carry_names(tmp_path, monkeypatch)
test_no_names_no_labels(tmp_path, monkeypatch)
test_split_unknowns_writes_scalar_and_padded_vector(tmp_path, monkeypatch)