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 144 : MatrixEqualityCheck::validParams() 19 : { 20 144 : InputParameters params = GeneralPostprocessor::validParams(); 21 144 : params.addClassDescription("Report whether two matrices are the same or not."); 22 288 : params.addRequiredParam<std::string>("mat1", "The petsc binary mat file containing matrix1"); 23 288 : params.addRequiredParam<std::string>("mat2", "The petsc binary mat file containing matrix2"); 24 288 : params.addParam<Real>( 25 288 : "equivalence_tol", 1e-8, "The relative tolerance for comparing equivalence"); 26 144 : return params; 27 0 : } 28 : 29 72 : MatrixEqualityCheck::MatrixEqualityCheck(const InputParameters & parameters) 30 : : GeneralPostprocessor(parameters), 31 72 : _equiv_tol(getParam<Real>("equivalence_tol")), 32 144 : _mat1_name(getParam<std::string>("mat1")), 33 216 : _mat2_name(getParam<std::string>("mat2")) 34 : { 35 72 : } 36 : 37 : void 38 54 : MatrixEqualityCheck::execute() 39 : { 40 54 : _equiv = true; 41 : 42 54 : auto mat1 = Moose::PetscSupport::createMatrixFromFile(_communicator, _petsc_mat1, _mat1_name); 43 54 : auto mat2 = Moose::PetscSupport::createMatrixFromFile(_communicator, _petsc_mat2, _mat2_name); 44 : 45 162 : if ((mat1->row_start() != mat2->row_start()) || (mat1->row_stop() != mat2->row_stop()) || 46 162 : (mat1->col_start() != mat2->col_start()) || (mat1->col_stop() != mat2->col_stop())) 47 : { 48 0 : _equiv = false; 49 0 : return; 50 : } 51 : 52 24354 : for (const auto i : make_range(mat1->row_start(), mat1->row_stop())) 53 12174300 : for (const auto j : make_range(mat1->col_start(), mat1->col_stop())) 54 : { 55 12150000 : const auto val1 = (*mat1)(i, j); 56 12150000 : const auto val2 = (*mat2)(i, j); 57 12150000 : 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 54 : MatrixEqualityCheck::finalize() 68 : { 69 54 : _communicator.min(_equiv); 70 : 71 54 : if (_petsc_mat1) 72 54 : LibmeshPetscCall(MatDestroy(&_petsc_mat1)); 73 54 : if (_petsc_mat2) 74 54 : LibmeshPetscCall(MatDestroy(&_petsc_mat2)); 75 54 : } 76 : 77 : Real 78 54 : MatrixEqualityCheck::getValue() const 79 : { 80 54 : return _equiv; 81 : }