ABLATE Source Documentation  0.12.34
fieldAccessor.hpp
1 #ifndef ABLATELIBRARY_FIELDACCESSOR_HPP
2 #define ABLATELIBRARY_FIELDACCESSOR_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  PetscScalar* dataArray{};
28 
32  DM dataDM;
33 
34  public:
35  FieldAccessor(Vec dataVectorIn, const Field& dataFieldIn, DM dm = nullptr) : dataVector(dataVectorIn), dataField(dataFieldIn), dataDM(dm) {
36  // Get read/write access to the vector
37  VecGetArray(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 
45  ~FieldAccessor() {
46  // Put back the vector
47  VecRestoreArray(dataVector, &dataArray) >> ablate::utilities::PetscUtilities::checkError;
48  };
49 
51  [[nodiscard]] inline const Field& GetField() const { return dataField; }
52 
54  template <class IndexType>
55  inline DataType* operator[](IndexType point) {
56  DataType* field;
57  if constexpr (isLocal) {
58  DMPlexPointLocalFieldRef(dataDM, point, dataField.id, dataArray, &field) >> ablate::utilities::PetscUtilities::checkError;
59  } else {
60  DMPlexPointGlobalFieldRef(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, DataType** field) {
68  PetscFunctionBeginHot;
69  if constexpr (isLocal) {
70  PetscCall(DMPlexPointLocalFieldRef(dataDM, point, dataField.id, dataArray, field));
71  } else {
72  PetscCall(DMPlexPointGlobalFieldRef(dataDM, point, dataField.id, dataArray, field));
73  }
74  PetscFunctionReturn(PETSC_SUCCESS);
75  }
76 
80  FieldAccessor(const FieldAccessor&) = delete;
81 };
82 } // namespace ablate::domain
83 #endif // ABLATELIBRARY_FIELDACCESSOR_HPP
Definition: fieldAccessor.hpp:12
FieldAccessor(const FieldAccessor &)=delete
PetscErrorCode operator()(IndexType point, DataType **field)
Inline function to compute the memory address at this point using PETSC style return error.
Definition: fieldAccessor.hpp:67
const Field & GetField() const
provide easy access to the field information
Definition: fieldAccessor.hpp:51
DataType * operator[](IndexType point)
Inline function to compute the memory address at this point.
Definition: fieldAccessor.hpp:55
static utilities::PetscUtilities::ErrorChecker checkError
Definition: petscUtilities.hpp:46
Definition: field.hpp:17