ABLATE Source Documentation  0.12.34
constFieldAccessor.hpp
1 #ifndef ABLATELIBRARY_CONSTFIELDACCESSOR_HPP
2 #define ABLATELIBRARY_CONSTFIELDACCESSOR_HPP
3 
4 #include <petsc.h>
5 #include "field.hpp"
6 
7 namespace ablate::domain {
11 template <class DataType, bool isLocal = true>
13  private:
17  Vec dataVector;
18 
22  const Field& dataField;
23 
27  const PetscScalar* dataArray{};
28 
32  DM dataDM;
33 
34  public:
35  ConstFieldAccessor(Vec dataVectorIn, const Field& dataFieldIn, DM dm = nullptr) : dataVector(dataVectorIn), dataField(dataFieldIn), dataDM(dm) {
36  // Get read/write access to the vector
37  VecGetArrayRead(dataVector, &dataArray) >> ablate::utilities::PetscUtilities::checkError;
38 
39  // Get the dm from the vector if not specified
40  if (!dataDM) {
41  VecGetDM(dataVector, &dataDM) >> ablate::utilities::PetscUtilities::checkError;
42  }
43  }
44 
46  // Put back the vector
47  VecRestoreArrayRead(dataVector, &dataArray) >> ablate::utilities::PetscUtilities::checkError;
48  };
49 
51  [[nodiscard]] inline const Field& GetField() const { return dataField; }
52 
54  template <class IndexType>
55  inline const DataType* operator[](IndexType point) const {
56  const DataType* field;
57  if constexpr (isLocal) {
58  DMPlexPointLocalFieldRead(dataDM, point, dataField.id, dataArray, &field) >> ablate::utilities::PetscUtilities::checkError;
59  } else {
60  DMPlexPointGlobalFieldRead(dataDM, point, dataField.id, dataArray, &field) >> ablate::utilities::PetscUtilities::checkError;
61  }
62  return field;
63  }
64 
66  template <class IndexType>
67  inline PetscErrorCode operator()(IndexType point, const DataType** field) const {
68  PetscFunctionBeginHot;
69  if constexpr (isLocal) {
70  PetscCall(DMPlexPointLocalFieldRead(dataDM, point, dataField.id, dataArray, field));
71  } else {
72  PetscCall(DMPlexPointGlobalFieldRead(dataDM, point, dataField.id, dataArray, field));
73  }
74  PetscFunctionReturn(PETSC_SUCCESS);
75  }
76 
81 };
82 } // namespace ablate::domain
83 #endif // ABLATELIBRARY_CONSTFIELDACCESSOR_HPP
Definition: constFieldAccessor.hpp:12
ConstFieldAccessor(const ConstFieldAccessor &)=delete
const DataType * operator[](IndexType point) const
Inline function to compute the memory address at this point.
Definition: constFieldAccessor.hpp:55
const Field & GetField() const
provide easy access to the field information
Definition: constFieldAccessor.hpp:51
PetscErrorCode operator()(IndexType point, const DataType **field) const
Inline function to compute the memory address at this point using PETSC style return error.
Definition: constFieldAccessor.hpp:67
static utilities::PetscUtilities::ErrorChecker checkError
Definition: petscUtilities.hpp:46
Definition: field.hpp:17