https://mooseframework.inl.gov
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 
17 using libMesh::Number;
21 
28 {
29 public:
32 
35 
39 
41  void restore();
42 
44  PetscScalar operator()(const numeric_index_type i) const;
45 
46 private:
48  bool readable() const { return _raw_value != nullptr; }
49 
52 
54  const PetscScalar * _raw_value;
55 };
56 
57 inline PetscScalar
59 {
60  mooseAssert(readable(), "Not readable");
61  const numeric_index_type local_index = _vec.map_global_to_local_index(i);
62  return _raw_value[local_index];
63 }
const PetscScalar * _raw_value
The raw values in the vector.
PetscVector< Number > & _vec
Reference to the petsc vector whose values shall be read.
dof_id_type numeric_index_type
A class which helps with repeated reading from a petsc vector.
numeric_index_type map_global_to_local_index(const numeric_index_type i) const
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. ...
PetscVectorReader(PetscVector< Number > &vec)
Construct using a pets vector.
PetscScalar operator()(const numeric_index_type i) const
Access a value in the petsc vector.
Real Number
void restore()
Restore the array, usually upon going out of scope.