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 : #pragma once 11 : 12 : #include "libmesh/petsc_vector.h" 13 : #include "libmesh/id_types.h" 14 : #include "libmesh/libmesh_common.h" 15 : #include "MooseError.h" 16 : 17 : using libMesh::Number; 18 : using libMesh::numeric_index_type; 19 : using libMesh::NumericVector; 20 : using libMesh::PetscVector; 21 : 22 : /** 23 : * A class which helps with repeated reading from a petsc vector. 24 : * Its main purpose is to avoid unnecessary calls to the get_array() function 25 : * in the wrapper. 26 : */ 27 : class PetscVectorReader 28 : { 29 : public: 30 : /// Construct using a pets vector 31 : PetscVectorReader(PetscVector<Number> & vec); 32 : 33 : /// Construct using a numeric vector 34 : PetscVectorReader(NumericVector<Number> & vec); 35 : 36 : /// Destructor to make sure the vector is restored every time this 37 : /// goes out of scope 38 : ~PetscVectorReader(); 39 : 40 : /// Restore the array, usually upon going out of scope 41 : void restore(); 42 : 43 : /// Access a value in the petsc vector 44 : PetscScalar operator()(const numeric_index_type i) const; 45 : 46 : private: 47 : /// Check if this vector is readable 48 : bool readable() const { return _raw_value != nullptr; } 49 : 50 : /// Reference to the petsc vector whose values shall be read 51 : PetscVector<Number> & _vec; 52 : 53 : /// The raw values in the vector 54 : const PetscScalar * _raw_value; 55 : }; 56 : 57 : inline PetscScalar 58 13372408 : PetscVectorReader::operator()(const numeric_index_type i) const 59 : { 60 : mooseAssert(readable(), "Not readable"); 61 13372408 : const numeric_index_type local_index = _vec.map_global_to_local_index(i); 62 13372408 : return _raw_value[local_index]; 63 : }