-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathvtkdiff.cpp
643 lines (555 loc) · 20.4 KB
/
vtkdiff.cpp
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
/**
* \copyright
* Copyright (c) 2015-2025, OpenGeoSys Community (http://www.opengeosys.org)
* Distributed under a Modified BSD License.
* See accompanying file LICENSE.txt or
* http://www.opengeosys.org/project/license
*/
#include <tclap/CmdLine.h>
#include <vtkCellArray.h>
#include <vtkCellData.h>
#include <vtkCommand.h>
#include <vtkDataArray.h>
#include <vtkDoubleArray.h>
#include <vtkPointData.h>
#include <vtkSmartPointer.h>
#include <vtkUnstructuredGrid.h>
#include <vtkVersion.h>
#include <vtkXMLUnstructuredGridReader.h>
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <iomanip>
#include <ios>
#include <iterator>
#include <optional>
#include <sstream>
#include <tuple>
#include <type_traits>
template <typename T>
auto float_to_string(T const& v) -> std::string
{
static_assert(std::is_floating_point<T>::value,
"float_to_string requires a floating point input type.");
std::stringstream double_eps_sstream;
double_eps_sstream << std::scientific << std::setprecision(16) << v;
return double_eps_sstream.str();
}
bool stringEndsWith(std::string const& str, std::string const& ending)
{
if (str.length() < ending.length())
return false;
// now the difference is non-negative, no underflow possible.
auto const string_end_length = str.length() - ending.length();
return str.compare(string_end_length, ending.length(), ending) == 0;
}
template <typename T>
std::ostream& operator<<(std::ostream& os, std::vector<T> const& vector)
{
if (vector.empty())
{
return os << "[]";
}
// print first n-1 elements
os << "[";
std::size_t const size = vector.size();
for (std::size_t i = 0; i < size - 1; ++i)
{
os << vector[i] << ", ";
}
return os << vector.back() << "]";
}
struct Args
{
bool const quiet;
bool const verbose;
bool const meshcheck;
double const abs_err_thr;
double const rel_err_thr;
std::string const vtk_input_a;
std::string const vtk_input_b;
std::string const data_array_a;
std::string const data_array_b;
};
auto parseCommandLine(int argc, char* argv[]) -> Args
{
TCLAP::CmdLine cmd(
"VtkDiff software.\n"
"Copyright (c) 2015-2025, OpenGeoSys Community "
"(http://www.opengeosys.org) "
"Distributed under a Modified BSD License. "
"See accompanying file LICENSE.txt or "
"http://www.opengeosys.org/project/license",
' ',
"0.1");
TCLAP::UnlabeledValueArg<std::string> vtk_input_a_arg(
"input-file-a",
"Path to the VTK unstructured grid input file.",
true,
"",
"VTK FILE");
cmd.add(vtk_input_a_arg);
TCLAP::UnlabeledValueArg<std::string> vtk_input_b_arg(
"input-file-b",
"Path to the second VTK unstructured grid input file.",
false,
"",
"VTK FILE");
cmd.add(vtk_input_b_arg);
TCLAP::ValueArg<std::string> data_array_a_arg(
"a", "first_data_array", "First data array name for comparison", false,
"", "NAME");
TCLAP::ValueArg<std::string> data_array_b_arg(
"b",
"second_data_array",
"Second data array name for comparison",
false,
"",
"NAME");
cmd.add(data_array_b_arg);
TCLAP::SwitchArg meshcheck_arg(
"m", "mesh_check", "Compare mesh geometries using absolute tolerance.");
cmd.xorAdd(data_array_a_arg, meshcheck_arg);
TCLAP::SwitchArg quiet_arg("q", "quiet", "Suppress all but error output.");
cmd.add(quiet_arg);
TCLAP::SwitchArg verbose_arg("v", "verbose",
"Also print which values differ.");
cmd.add(verbose_arg);
auto const double_eps_string =
float_to_string(std::numeric_limits<double>::epsilon());
TCLAP::ValueArg<double> abs_err_thr_arg(
"",
"abs",
"Tolerance for the absolute error in the maximum norm (" +
double_eps_string + ")",
false,
std::numeric_limits<double>::epsilon(),
"FLOAT");
cmd.add(abs_err_thr_arg);
TCLAP::ValueArg<double> rel_err_thr_arg(
"",
"rel",
"Tolerance for the componentwise relative error (" + double_eps_string +
")",
false,
std::numeric_limits<double>::epsilon(),
"FLOAT");
cmd.add(rel_err_thr_arg);
cmd.parse(argc, argv);
return Args{quiet_arg.getValue(), verbose_arg.getValue(),
meshcheck_arg.getValue(), abs_err_thr_arg.getValue(),
rel_err_thr_arg.getValue(), vtk_input_a_arg.getValue(),
vtk_input_b_arg.getValue(), data_array_a_arg.getValue(),
data_array_b_arg.getValue()};
}
template <typename T>
class ErrorCallback : public vtkCommand
{
public:
vtkTypeMacro(ErrorCallback, vtkCommand);
static ErrorCallback<T>* New() { return new ErrorCallback<T>; }
void Execute(vtkObject* caller, unsigned long vtkNotUsed(eventId),
void* callData) override
{
auto* reader = static_cast<T*>(caller);
std::cerr << "Error reading file `" << reader->GetFileName() << "'\n"
<< static_cast<char*>(callData) << "\nAborting." << std::endl;
std::exit(2);
}
};
enum class Domain
{
Point,
Cell,
Field
};
vtkSmartPointer<vtkUnstructuredGrid> readMesh(std::string const& filename)
{
if (filename.empty())
{
return nullptr;
}
if (!stringEndsWith(filename, ".vtu"))
{
std::cerr << "Error: Expected a file with .vtu extension."
<< "File '" << filename << "' not read.";
return nullptr;
}
vtkSmartPointer<ErrorCallback<vtkXMLUnstructuredGridReader>> errorCallback =
vtkSmartPointer<ErrorCallback<vtkXMLUnstructuredGridReader>>::New();
vtkSmartPointer<vtkXMLUnstructuredGridReader> reader =
vtkSmartPointer<vtkXMLUnstructuredGridReader>::New();
reader->AddObserver(vtkCommand::ErrorEvent, errorCallback);
reader->SetFileName(filename.c_str());
reader->Update();
return reader->GetOutput();
}
std::tuple<vtkSmartPointer<vtkUnstructuredGrid>,
vtkSmartPointer<vtkUnstructuredGrid>>
readMeshes(std::string const& file_a_name, std::string const& file_b_name)
{
return {readMesh(file_a_name), readMesh(file_b_name)};
}
std::optional<Domain> determineDomain(vtkUnstructuredGrid& mesh,
std::string const& data_array_name)
{
if (mesh.GetPointData()->HasArray(data_array_name.c_str()))
{
return Domain::Point;
}
else if (mesh.GetCellData()->HasArray(data_array_name.c_str()))
{
return Domain::Cell;
}
else if (mesh.GetFieldData()->HasArray(data_array_name.c_str()))
{
return Domain::Field;
}
else
{
std::cerr << "Error: Scalars data array "
<< "\'" << data_array_name.c_str() << "\'"
<< " neither found in point data nor in cell data nor in "
"field data.\n";
return std::nullopt;
}
}
vtkSmartPointer<vtkDataArray> getDataArray(vtkUnstructuredGrid& mesh,
std::string const& data_array_name,
Domain domain)
{
vtkSmartPointer<vtkDataArray> data_array;
switch (domain)
{
case Domain::Point:
data_array =
mesh.GetPointData()->GetScalars(data_array_name.c_str());
break;
case Domain::Cell:
data_array =
mesh.GetCellData()->GetScalars(data_array_name.c_str());
break;
case Domain::Field:
// GetArray() is not recommended for use, but according to the VTK
// API docs it is OK here, because we want to get a vtkDataArray.
data_array = mesh.GetFieldData()->GetArray(data_array_name.c_str());
break;
}
// Check arrays' validity
if (!data_array)
{
std::cerr << "Error: Scalars data array "
<< "\'" << data_array_name.c_str() << "\'"
<< " could not be read.\n";
}
return data_array;
}
std::tuple<bool, vtkSmartPointer<vtkDataArray>, vtkSmartPointer<vtkDataArray>>
readDataArraysFromMeshes(
std::tuple<vtkSmartPointer<vtkUnstructuredGrid>,
vtkSmartPointer<vtkUnstructuredGrid>> const& meshes,
std::string const& data_array_a_name,
std::string const& data_array_b_name)
{
auto const& [mesh_a_ptr, mesh_b_ptr] = meshes;
if (mesh_a_ptr == nullptr)
{
std::cerr << "First mesh was not read correctly and is a nullptr.\n";
return {false, nullptr, nullptr};
}
auto const opt_domain = determineDomain(*mesh_a_ptr, data_array_a_name);
if (!opt_domain)
{
return {false, nullptr, nullptr};
}
auto const domain = *opt_domain;
// Get arrays
vtkSmartPointer<vtkDataArray> a =
getDataArray(*mesh_a_ptr, data_array_a_name, domain);
if (!a)
{
return {false, nullptr, nullptr};
}
vtkSmartPointer<vtkDataArray> b;
if (mesh_b_ptr == nullptr)
{
if (data_array_a_name == data_array_b_name)
{
std::cerr << "Error: You are trying to compare data array `"
<< data_array_a_name
<< "' from first file to itself. Aborting.\n";
std::exit(3);
}
b = getDataArray(*mesh_a_ptr, data_array_b_name, domain);
}
else
{
b = getDataArray(*mesh_b_ptr, data_array_b_name, domain);
}
if (!b)
{
return {false, nullptr, nullptr};
}
return {true, a, b};
}
bool compareCellTopology(vtkCellArray* const cells_a,
vtkCellArray* const cells_b)
{
vtkIdType const n_cells_a{cells_a->GetNumberOfCells()};
vtkIdType const n_cells_b{cells_b->GetNumberOfCells()};
if (n_cells_a != n_cells_b)
{
std::cerr << "Number of cells in the first mesh is " << n_cells_a
<< " and differs from the number of cells in the second "
"mesh, which is "
<< n_cells_b << "\n";
return false;
}
vtkIdType n_cell_points_a, n_cell_points_b;
#if (VTK_MAJOR_VERSION > 8 || VTK_MINOR_VERSION == 90)
const vtkIdType *cell_points_a, *cell_points_b;
#else
vtkIdType *cell_points_a, *cell_points_b;
#endif
cells_a->InitTraversal();
cells_b->InitTraversal();
int get_next_cell_a = cells_a->GetNextCell(n_cell_points_a, cell_points_a);
int get_next_cell_b = cells_b->GetNextCell(n_cell_points_b, cell_points_b);
int cell_number = 0;
while (get_next_cell_a == 1 && get_next_cell_b == 1)
{
if (n_cell_points_a != n_cell_points_b)
{
std::cerr << "Cell " << cell_number << " in first input has "
<< n_cell_points_a << " points but in the second input "
<< n_cell_points_b << " points.\n";
}
for (vtkIdType i = 0; i < n_cell_points_a; ++i)
{
if (cell_points_a[i] != cell_points_b[i])
{
std::cerr << "Point " << i << " of cell " << cell_number
<< " has id " << cell_points_a[i]
<< " in the first input but id " << cell_points_b[i]
<< " in the second input.\n";
return false;
}
}
get_next_cell_a = cells_a->GetNextCell(n_cell_points_a, cell_points_a);
get_next_cell_b = cells_b->GetNextCell(n_cell_points_b, cell_points_b);
cell_number++;
}
if (get_next_cell_a != 0)
{
std::cerr << "Unexpected return value (" << get_next_cell_a
<< ") for cells_a->GetNextCell() call. Expected 0 for "
"end-of-list or 1 for no error.\n";
return false;
}
if (get_next_cell_b != 0)
{
std::cerr << "Unexpected return value (" << get_next_cell_b
<< ") for cells_b->GetNextCell() call. Expected 0 for "
"end-of-list or 1 for no error.\n";
return false;
}
return true;
}
bool comparePoints(vtkPoints* const points_a, vtkPoints* const points_b,
double const eps_squared)
{
vtkIdType const n_points_a{points_a->GetNumberOfPoints()};
vtkIdType const n_points_b{points_b->GetNumberOfPoints()};
if (n_points_a != n_points_b)
{
std::cerr << "Number of points in the first mesh is " << n_points_a
<< " and differst from the number of point in the second "
"mesh, which is "
<< n_points_b << "\n";
return false;
}
for (vtkIdType p = 0; p < n_points_a; ++p)
{
auto const a = points_a->GetPoint(p);
auto const b = points_b->GetPoint(p);
double const distance2 = vtkMath::Distance2BetweenPoints(a, b);
if (distance2 >= eps_squared)
{
std::cerr << "Point " << p << " with coordinates (" << a[0] << ", "
<< a[1] << ", " << a[2]
<< ") from the first mesh is significantly different "
"from the same point in the second mesh, which "
"has coordinates ("
<< b[0] << ", " << b[1] << ", " << b[2]
<< ") with distance between them " << std::sqrt(distance2)
<< "\n";
return false;
}
}
return true;
}
int main(int argc, char* argv[])
{
auto const digits10 = std::numeric_limits<double>::digits10;
auto const args = parseCommandLine(argc, argv);
// Setup the standard output and error stream numerical formats.
std::cout << std::scientific << std::setprecision(digits10);
std::cerr << std::scientific << std::setprecision(digits10);
auto meshes = readMeshes(args.vtk_input_a, args.vtk_input_b);
if (args.meshcheck)
{
if (args.vtk_input_a == args.vtk_input_b)
{
std::cout << "Will not compare meshes from same input file.\n";
return EXIT_SUCCESS;
}
if (!comparePoints(std::get<0>(meshes)->GetPoints(),
std::get<1>(meshes)->GetPoints(),
args.abs_err_thr * args.abs_err_thr))
{
std::cerr << "Error in mesh points' comparison occured.\n";
return EXIT_FAILURE;
}
if (!compareCellTopology(std::get<0>(meshes)->GetCells(),
std::get<1>(meshes)->GetCells()))
{
std::cerr << "Error in cells' topology comparison occured.\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
// Read arrays from input file.
bool read_successful;
vtkSmartPointer<vtkDataArray> a;
vtkSmartPointer<vtkDataArray> b;
std::tie(read_successful, a, b) =
readDataArraysFromMeshes(meshes, args.data_array_a, args.data_array_b);
if (!read_successful)
return EXIT_FAILURE;
if (!args.quiet)
std::cout << "Comparing data array `" << args.data_array_a
<< "' from file `" << args.vtk_input_a << "' to data array `"
<< args.data_array_b << "' from file `" << args.vtk_input_b
<< "'.\n";
// Check similarity of the data arrays.
// Is numeric
if (!a->IsNumeric())
{
std::cerr << "Data in data array a is not numeric:\n"
<< "data type is " << a->GetDataTypeAsString() << "\n";
return EXIT_FAILURE;
}
if (!b->IsNumeric())
{
std::cerr << "Data in data array b is not numeric.\n"
<< "data type is " << b->GetDataTypeAsString() << "\n";
return EXIT_FAILURE;
}
auto const num_tuples = a->GetNumberOfTuples();
// Number of components
if (num_tuples != b->GetNumberOfTuples())
{
std::cerr << "Number of tuples differ:\n"
<< num_tuples << " in data array a and "
<< b->GetNumberOfTuples() << " in data array b\n";
return EXIT_FAILURE;
}
auto const num_components = a->GetNumberOfComponents();
// Number of components
if (num_components != b->GetNumberOfComponents())
{
std::cerr << "Number of components differ:\n"
<< num_components << " in data array a and "
<< b->GetNumberOfComponents() << " in data array b\n";
return EXIT_FAILURE;
}
// Calculate difference of the data arrays.
// Absolute error and norms.
std::vector<double> abs_err_norm_l1(num_components);
std::vector<double> abs_err_norm_2_2(num_components);
std::vector<double> abs_err_norm_max(num_components);
// Relative error and norms.
std::vector<double> rel_err_norm_l1(num_components);
std::vector<double> rel_err_norm_2_2(num_components);
std::vector<double> rel_err_norm_max(num_components);
for (auto tuple_idx = 0; tuple_idx < num_tuples; ++tuple_idx)
{
for (auto component_idx = 0; component_idx < num_components;
++component_idx)
{
auto const a_comp = a->GetComponent(tuple_idx, component_idx);
auto const b_comp = b->GetComponent(tuple_idx, component_idx);
auto const abs_err = std::abs(a_comp - b_comp);
abs_err_norm_l1[component_idx] += abs_err;
abs_err_norm_2_2[component_idx] += abs_err * abs_err;
abs_err_norm_max[component_idx] =
std::max(abs_err_norm_max[component_idx], abs_err);
// relative error and its norms:
double rel_err;
if (abs_err == 0.0)
{
rel_err = 0.0;
}
else if (a_comp == 0.0 || b_comp == 0.0)
{
rel_err = std::numeric_limits<double>::infinity();
}
else
{
rel_err =
abs_err / std::min(std::abs(a_comp), std::abs(b_comp));
}
rel_err_norm_l1[component_idx] += rel_err;
rel_err_norm_2_2[component_idx] += rel_err * rel_err;
rel_err_norm_max[component_idx] =
std::max(rel_err_norm_max[component_idx], rel_err);
if (abs_err > args.abs_err_thr && rel_err > args.rel_err_thr &&
args.verbose)
{
std::cout << "tuple: " << std::setw(4) << tuple_idx
<< "component: " << std::setw(2) << component_idx
<< ": abs err = " << std::setw(digits10 + 7)
<< abs_err
<< ", rel err = " << std::setw(digits10 + 7)
<< rel_err << "\n";
}
}
}
// Error information
if (!args.quiet)
{
std::cout << "Computed difference between data arrays:\n";
std::cout << "abs l1 norm = " << abs_err_norm_l1 << "\n";
std::cout << "abs l2-norm^2 = " << abs_err_norm_2_2 << "\n";
// temporary squared norm vector for output.
std::vector<double> abs_err_norm_2;
std::transform(std::begin(abs_err_norm_2_2), std::end(abs_err_norm_2_2),
std::back_inserter(abs_err_norm_2),
[](double x) { return std::sqrt(x); });
std::cout << "abs l2-norm = " << abs_err_norm_2 << "\n";
std::cout << "abs maximum norm = " << abs_err_norm_max << "\n";
std::cout << "\n";
std::cout << "rel l1 norm = " << rel_err_norm_l1 << "\n";
std::cout << "rel l2-norm^2 = " << rel_err_norm_2_2 << "\n";
// temporary squared norm vector for output.
std::vector<double> rel_err_norm_2;
std::transform(std::begin(rel_err_norm_2_2), std::end(rel_err_norm_2_2),
std::back_inserter(rel_err_norm_2),
[](double x) { return std::sqrt(x); });
std::cout << "rel l2-norm = " << rel_err_norm_2 << "\n";
std::cout << "rel maximum norm = " << rel_err_norm_max << "\n";
}
if (*std::max_element(abs_err_norm_max.begin(), abs_err_norm_max.end()) >
args.abs_err_thr &&
*std::max_element(rel_err_norm_max.begin(), rel_err_norm_max.end()) >
args.rel_err_thr)
{
if (!args.quiet)
std::cout << "Absolute and relative error (maximum norm) are larger"
" than the corresponding thresholds "
<< args.abs_err_thr << " and " << args.rel_err_thr
<< ".\n";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}