File: TestCellGridReadWrite.cxx

package info (click to toggle)
paraview 5.13.2%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 544,220 kB
  • sloc: cpp: 3,374,605; ansic: 1,332,409; python: 150,381; xml: 122,166; sql: 65,887; sh: 7,317; javascript: 5,262; yacc: 4,417; java: 3,977; perl: 2,363; lex: 1,929; f90: 1,397; makefile: 170; objc: 153; tcl: 59; pascal: 50; fortran: 29
file content (183 lines) | stat: -rw-r--r-- 5,824 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
// SPDX-FileCopyrightText: Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
// SPDX-License-Identifier: BSD-3-Clause
#include "vtkCellGrid.h"
#include "vtkCellGridReader.h"
#include "vtkCellGridWriter.h"
#include "vtkDGHex.h"
#include "vtkDGTet.h"
#include "vtkDGTri.h"
#include "vtkDataSetAttributes.h"
#include "vtkDoubleArray.h"
#include "vtkIdTypeArray.h"
#include "vtkIntArray.h"
#include "vtkNew.h"
#include "vtkSmartPointer.h"
#include "vtkTable.h"
#include "vtkTestUtilities.h"
#include "vtkVector.h"

namespace
{

struct AttInfo
{
  vtkStringToken Name;
  vtkStringToken CellType;
  vtkStringToken DOFSharing;
  vtkStringToken FunctionSpace;
  vtkStringToken Basis;
  int Order;
};

bool RoundTrip(const char* filename, const std::string& tempDir, vtkIdType numCells,
  const std::vector<AttInfo>& expectedAttributes)
{
  if (!filename)
  {
    return false;
  }
  bool ok = true;

  std::cout << "=== Start of round trip " << filename << " ===\n";
  vtkNew<vtkCellGridReader> reader;
  reader->SetFileName(filename);
  reader->Update();
  auto og = vtkCellGrid::SafeDownCast(reader->GetOutputDataObject(0));
  if (!og)
  {
    std::cerr << "ERROR: Could not read source cell-grid.\n";
    ok = false;
    return ok;
  }

  std::cout << "  === Write step ===\n";
  vtkNew<vtkCellGridWriter> writer;
  std::string tempFile = tempDir.empty() ? "test.dg" : tempDir + "/test.dg";
  writer->SetFileName(tempFile.c_str());
  writer->SetInputConnection(reader->GetOutputPort());
  writer->Write();

  std::cout << "  === Read step ===\n";
  vtkNew<vtkCellGridReader> reader2;
  reader2->SetFileName(tempFile.c_str()); // "test.dg");
  reader2->Update();

  std::cout << "  === Validation ===\n";
  auto cg = vtkCellGrid::SafeDownCast(reader2->GetOutputDataObject(0));
  if (!cg)
  {
    std::cerr << "ERROR: Could not round trip.\n";
    ok = false;
    return ok;
  }

  if (cg->GetNumberOfCells() != numCells)
  {
    std::cerr << "ERROR: Expected to have " << numCells << " cells, got " << cg->GetNumberOfCells()
              << ".\n";
    ok = false;
  }

  for (const auto& attData : expectedAttributes)
  {
    auto* att = cg->GetCellAttributeByName(attData.Name.Data());
    if (!att)
    {
      ok = false;
      std::cerr << "ERROR: Failed to find cell-attribute \"" << attData.Name.Data() << "\".\n";
      continue;
    }
    auto cellTypeInfo = att->GetCellTypeInfo(attData.CellType);
    if (attData.DOFSharing != cellTypeInfo.DOFSharing)
    {
      ok = false;
      std::cerr << "ERROR: Attribute " << attData.Name.Data() << " had DOF sharing "
                << cellTypeInfo.DOFSharing.Data() << " " << std::hex
                << cellTypeInfo.DOFSharing.GetId() << " vs " << attData.DOFSharing.Data() << " "
                << attData.DOFSharing.GetId() << ".\n";
    }
    if (attData.FunctionSpace != cellTypeInfo.FunctionSpace)
    {
      ok = false;
      std::cerr << "ERROR: Attribute " << attData.Name.Data() << " had function space "
                << cellTypeInfo.FunctionSpace.Data() << " " << std::hex
                << cellTypeInfo.FunctionSpace.GetId() << " vs " << attData.FunctionSpace.GetId()
                << ".\n";
    }
    if (attData.Basis != cellTypeInfo.Basis)
    {
      ok = false;
      std::cerr << "ERROR: Attribute " << attData.Name.Data() << " had basis "
                << cellTypeInfo.Basis.Data() << " " << std::hex << cellTypeInfo.Basis.GetId()
                << " vs " << attData.Basis.GetId() << ".\n";
    }
    if (attData.Order != cellTypeInfo.Order)
    {
      ok = false;
      std::cerr << "ERROR: Attribute " << attData.Name.Data() << " had order " << cellTypeInfo.Order
                << " vs " << attData.Order << ".\n";
    }
  }
  if (expectedAttributes.size() != cg->GetCellAttributeIds().size())
  {
    ok = false;
    std::cerr << "ERROR: Expected " << expectedAttributes.size() << " attributes, got "
              << cg->GetCellAttributeIds().size() << ".\n";
  }

  if (og->GetSchemaName() != cg->GetSchemaName() ||
    og->GetSchemaVersion() != cg->GetSchemaVersion())
  {
    ok = false;
    std::cerr << "ERROR: Schema name/version information not preserved.\n";
  }

  if (og->GetContentVersion() != cg->GetContentVersion())
  {
    ok = false;
    std::cerr << "ERROR: Content version information not preserved.\n";
  }

  delete[] filename;

  return ok;
}

} // anonymous namespace

int TestCellGridReadWrite(int argc, char* argv[])
{
  vtkStringToken invalid;
  std::string tempDir =
    vtkTestUtilities::GetArgOrEnvOrDefault("-T", argc, argv, "VTK_TEMP_DIR", "Testing/Temporary/");

  // clang-format off
  if (!RoundTrip(vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/dgHexahedra.dg", 0),
        tempDir,
        /* numCells */ 2, {
        { "shape",     "vtkDGHex", "coordinates", "HGRAD", "C", 1 },
        { "scalar0",   "vtkDGHex",  invalid,      "HGRAD", "C", 1 },
        { "scalar1",   "vtkDGHex",  invalid,      "HGRAD", "C", 1 },
        { "scalar2",   "vtkDGHex",  invalid,      "HGRAD", "C", 1 },
        { "scalar3",   "vtkDGHex", "point-data",  "HGRAD", "C", 1 },
        { "curl1",     "vtkDGHex",  invalid,      "HCURL", "I", 1 },
        { "quadratic", "vtkDGHex",  invalid,      "HGRAD", "I", 2 } }))
  {
    return EXIT_FAILURE;
  }

  if (!RoundTrip(vtkTestUtilities::ExpandDataFileName(argc, argv, "Data/dgTetrahedra.dg", 0),
        tempDir,
        /* numCells */ 2, {
        { "shape",   "vtkDGTet", "coordinates", "HGRAD", "C", 1 },
        { "scalar0", "vtkDGTet",  invalid,      "HGRAD", "C", 1 },
        { "scalar1", "vtkDGTet",  invalid,      "HGRAD", "C", 1 },
        { "scalar2", "vtkDGTet",  invalid,      "HGRAD", "C", 1 },
        { "scalar3", "vtkDGTet", "point-data",  "HGRAD", "C", 1 } }))
  {
    return EXIT_FAILURE;
  }
  // clang-format on

  return EXIT_SUCCESS;
}