https://mooseframework.inl.gov
Loading...
Searching...
No Matches
PetscVectorReader.h
Go to the documentation of this file.
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
23{
24public:
26 PetscVectorReader(PetscVector<Number> & vec);
27
29 PetscVectorReader(NumericVector<Number> & vec);
30
34
36 void restore();
37
39 PetscScalar operator()(const numeric_index_type i) const;
40
41private:
43 bool readable() const { return _raw_value != nullptr; }
44
46 PetscVector<Number> & _vec;
47
49 const PetscScalar * _raw_value;
50};
51
52inline PetscScalar
53PetscVectorReader::operator()(const numeric_index_type i) const
54{
55 mooseAssert(readable(), "Not readable");
56 const numeric_index_type local_index = _vec.map_global_to_local_index(i);
57 return _raw_value[local_index];
58}
A class which helps with repeated reading from a petsc vector.
PetscScalar operator()(const numeric_index_type i) const
Access a value in the petsc vector.
void restore()
Restore the array, usually upon going out of scope.
const PetscScalar * _raw_value
The raw values in the vector.
bool readable() const
Check if this vector is readable.
~PetscVectorReader()
Destructor to make sure the vector is restored every time this goes out of scope.
PetscVector< Number > & _vec
Reference to the petsc vector whose values shall be read.