Line data Source code
1 : //* This file is part of the MOOSE framework 2 : //* https://mooseframework.inl.gov 3 : //* 4 : //* All rights reserved, see COPYRIGHT for full restrictions 5 : //* https://github.com/idaholab/moose/blob/master/COPYRIGHT 6 : //* 7 : //* Licensed under LGPL 2.1, please see LICENSE for details 8 : //* https://www.gnu.org/licenses/lgpl-2.1.html 9 : 10 : #include "MatrixEqualityCheck.h" 11 : 12 : #include "MooseUtils.h" 13 : #include "libmesh/petsc_matrix.h" 14 : 15 : registerMooseObject("NavierStokesApp", MatrixEqualityCheck); 16 : 17 : InputParameters 18 72 : MatrixEqualityCheck::validParams() 19 : { 20 72 : InputParameters params = GeneralPostprocessor::validParams(); 21 72 : params.addClassDescription("Report whether two matrices are the same or not."); 22 144 : params.addRequiredParam<std::string>("mat1", "The petsc binary mat file containing matrix1"); 23 144 : params.addRequiredParam<std::string>("mat2", "The petsc binary mat file containing matrix2"); 24 144 : params.addParam<Real>( 25 144 : "equivalence_tol", 1e-8, "The relative tolerance for comparing equivalence"); 26 72 : return params; 27 0 : } 28 : 29 36 : MatrixEqualityCheck::MatrixEqualityCheck(const InputParameters & parameters) 30 : : GeneralPostprocessor(parameters), 31 36 : _equiv_tol(getParam<Real>("equivalence_tol")), 32 72 : _mat1_name(getParam<std::string>("mat1")), 33 108 : _mat2_name(getParam<std::string>("mat2")) 34 : { 35 36 : } 36 : 37 : void 38 30 : MatrixEqualityCheck::execute() 39 : { 40 30 : _equiv = true; 41 : 42 30 : auto mat1 = Moose::PetscSupport::createMatrixFromFile(_communicator, _petsc_mat1, _mat1_name); 43 30 : auto mat2 = Moose::PetscSupport::createMatrixFromFile(_communicator, _petsc_mat2, _mat2_name); 44 : 45 90 : if ((mat1->row_start() != mat2->row_start()) || (mat1->row_stop() != mat2->row_stop()) || 46 90 : (mat1->col_start() != mat2->col_start()) || (mat1->col_stop() != mat2->col_stop())) 47 : { 48 0 : _equiv = false; 49 0 : return; 50 : } 51 : 52 13530 : for (const auto i : make_range(mat1->row_start(), mat1->row_stop())) 53 6763500 : for (const auto j : make_range(mat1->col_start(), mat1->col_stop())) 54 : { 55 6750000 : const auto val1 = (*mat1)(i, j); 56 6750000 : const auto val2 = (*mat2)(i, j); 57 6750000 : if (!MooseUtils::relativeFuzzyEqual(val1, val2, _equiv_tol) && 58 : !MooseUtils::absoluteFuzzyEqual(val1, val2, _equiv_tol)) 59 : { 60 0 : _equiv = false; 61 : return; 62 : } 63 : } 64 : } 65 : 66 : void 67 30 : MatrixEqualityCheck::finalize() 68 : { 69 30 : _communicator.min(_equiv); 70 : 71 30 : if (_petsc_mat1) 72 30 : LibmeshPetscCall(MatDestroy(&_petsc_mat1)); 73 30 : if (_petsc_mat2) 74 30 : LibmeshPetscCall(MatDestroy(&_petsc_mat2)); 75 30 : } 76 : 77 : Real 78 30 : MatrixEqualityCheck::getValue() const 79 : { 80 30 : return _equiv; 81 : }