v0.16.3
Loading...
Searching...
No Matches
solution_vector_comparison_atom.cpp
Go to the documentation of this file.
1/** @file solution_vector_comparison_atom.cpp
2 * @brief Compare complete PETSc solution vectors from independent solves.
3 */
4
5#include <MoFEM.hpp>
6using namespace MoFEM;
7
8int main(int argc, char *argv[]) {
9 MoFEM::Core::Initialize(&argc, &argv, nullptr,
10 "Compare saved solution vectors in order.\n");
11 try {
12 char left_file[PETSC_MAX_PATH_LEN] = "";
13 char right_file[PETSC_MAX_PATH_LEN] = "";
14 PetscInt count = 1;
15 PetscReal tolerance = 1.e-9;
16 CHKERR PetscOptionsGetString(nullptr, nullptr, "-compare_left", left_file,
17 sizeof(left_file), nullptr);
18 CHKERR PetscOptionsGetString(nullptr, nullptr, "-compare_right", right_file,
19 sizeof(right_file), nullptr);
20 CHKERR PetscOptionsGetInt(nullptr, nullptr, "-compare_count", &count,
21 nullptr);
22 CHKERR PetscOptionsGetReal(nullptr, nullptr, "-compare_tolerance",
23 &tolerance, nullptr);
24 if (!left_file[0] || !right_file[0] || count < 1 ||
25 !std::isfinite(tolerance) || tolerance <= 0.)
26 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
27 "Specify both vector files, a positive count and tolerance");
28 PetscViewer left_viewer, right_viewer;
29 CHKERR PetscViewerBinaryOpen(PETSC_COMM_WORLD, left_file, FILE_MODE_READ,
30 &left_viewer);
31 CHKERR PetscViewerBinaryOpen(PETSC_COMM_WORLD, right_file, FILE_MODE_READ,
32 &right_viewer);
33 for (PetscInt step = 0; step != count; ++step) {
34 Vec left_raw, right_raw;
35 CHKERR VecCreate(PETSC_COMM_WORLD, &left_raw);
36 CHKERR VecCreate(PETSC_COMM_WORLD, &right_raw);
37 SmartPetscObj<Vec> left(left_raw), right(right_raw);
38 CHKERR VecLoad(left, left_viewer);
39 CHKERR VecLoad(right, right_viewer);
40 PetscInt left_size, right_size;
41 CHKERR VecGetSize(left, &left_size);
42 CHKERR VecGetSize(right, &right_size);
43 if (left_size != right_size || !left_size)
44 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
45 "Solution vectors have different or empty layouts");
46 PetscReal scale, difference;
47 CHKERR VecNorm(left, NORM_INFINITY, &scale);
48 CHKERR VecAXPY(right, -1., left);
49 CHKERR VecNorm(right, NORM_INFINITY, &difference);
50 if (!std::isfinite(scale) || !std::isfinite(difference) ||
51 difference > tolerance * std::max(1., scale))
52 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
53 "Solution vector %" PetscInt_FMT
54 " differs by %.16e (scale %.16e)",
55 step, difference, scale);
56 CHKERR PetscPrintf(PETSC_COMM_WORLD,
57 "Solution vector %" PetscInt_FMT ": %" PetscInt_FMT
58 " coefficients, maximum difference %.16e\n",
59 step, left_size, difference);
60 }
61 for (auto viewer : {left_viewer, right_viewer}) {
62 PetscInt extra, count_read;
63 CHKERR PetscViewerBinaryRead(viewer, &extra, 1, &count_read, PETSC_INT);
64 if (count_read)
65 SETERRQ(PETSC_COMM_WORLD, MOFEM_ATOM_TEST_INVALID,
66 "Vector file contains more than the expected number of states");
67 }
68 CHKERR PetscViewerDestroy(&left_viewer);
69 CHKERR PetscViewerDestroy(&right_viewer);
70 }
73 return 0;
74}
int main()
#define CATCH_ERRORS
Catch errors.
@ MOFEM_ATOM_TEST_INVALID
Definition definitions.h:40
#define CHKERR
Inline error check.
implementation of Data Operators for Forces and Sources
Definition Common.hpp:10
PetscErrorCode PetscOptionsGetInt(PetscOptions *, const char pre[], const char name[], PetscInt *ivalue, PetscBool *set)
PetscErrorCode PetscOptionsGetReal(PetscOptions *, const char pre[], const char name[], PetscReal *dval, PetscBool *set)
PetscErrorCode PetscOptionsGetString(PetscOptions *, const char pre[], const char name[], char str[], size_t size, PetscBool *set)
static MoFEMErrorCode Initialize(int *argc, char ***args, const char file[], const char help[])
Initializes the MoFEM database PETSc, MOAB and MPI.
Definition Core.cpp:68
static MoFEMErrorCode Finalize()
Checks for options to be called at the conclusion of the program.
Definition Core.cpp:123
intrusive_ptr for managing petsc objects
double scale
Definition plastic.cpp:123