ABLATE Source Documentation  0.12.34
boundarySolver.hpp
1 #ifndef ABLATELIBRARY_BOUNDARYSOLVER_HPP
2 #define ABLATELIBRARY_BOUNDARYSOLVER_HPP
3 
4 #include <memory>
5 #include "solver/cellSolver.hpp"
6 #include "solver/timeStepper.hpp"
7 
8 namespace ablate::boundarySolver {
9 
10 // forward declare the boundaryProcess
11 class BoundaryProcess;
12 
13 class BoundarySolver : public solver::CellSolver, public solver::RHSFunction, private utilities::Loggable<BoundarySolver>, public io::Serializable {
14  public:
18  typedef struct {
19  PetscInt faceId; /* local id for this face. For merged faces this is the first face merged **/
20  PetscReal normal[3]; /* normals (pointing into the boundary from the other region) */
21  PetscReal areas[3]; /* Area-scaled normals */
22  PetscReal centroid[3]; /* Location of centroid (quadrature point) */
24 
25  using BoundarySourceFunction = PetscErrorCode (*)(PetscInt dim, const BoundaryFVFaceGeom* fg, const PetscFVCellGeom* boundaryCell, const PetscInt uOff[], const PetscScalar* boundaryValues,
26  const PetscScalar* stencilValues[], const PetscInt aOff[], const PetscScalar* auxValues, const PetscScalar* stencilAuxValues[],
27  PetscInt stencilSize, const PetscInt stencil[], const PetscScalar stencilWeights[], const PetscInt sOff[], PetscScalar source[], void* ctx);
28 
32  using BoundaryUpdateFunction = PetscErrorCode (*)(PetscInt dim, const BoundaryFVFaceGeom* fg, const PetscFVCellGeom* boundaryCell, const PetscInt uOff[], PetscScalar* boundaryValues,
33  const PetscScalar* stencilValues, const PetscInt aOff[], PetscScalar* auxValues, const PetscScalar* stencilAuxValues, void* ctx);
34 
38  using BoundaryPreRHSFunctionDefinition = PetscErrorCode (*)(BoundarySolver&, TS ts, PetscReal time, bool initialStage, Vec locX, void* ctx);
39 
43  enum class BoundarySourceType {
44  Point,
45  Distributed,
46  Flux,
47  Face
48  };
49 
58  static void ComputeGradient(PetscInt dim, PetscScalar boundaryValue, PetscInt stencilSize, const PetscScalar* stencilValues, const PetscScalar* stencilWeights, PetscScalar* grad);
59 
68  static void ComputeGradientAlongNormal(PetscInt dim, const BoundaryFVFaceGeom* fg, PetscScalar boundaryValue, PetscInt stencilSize, const PetscScalar* stencilValues,
69  const PetscScalar* stencilWeights, PetscScalar& dPhiDNorm);
70 
74  using BoundaryPreRHSPointFunction = PetscErrorCode (*)(PetscReal time, PetscReal dt, PetscInt dim, const BoundaryFVFaceGeom* fg, const PetscFVCellGeom* boundaryCell, const PetscInt uOff[],
75  PetscScalar* boundaryValues, const PetscScalar* stencilValues[], const PetscInt aOff[], PetscScalar* auxValues,
76  const PetscScalar* stencilAuxValues[], PetscInt stencilSize, const PetscInt stencil[], const PetscScalar stencilWeights[], void* ctx);
77 
83  void* context;
84 
85  std::vector<PetscInt> inputFieldsOffset;
86  std::vector<PetscInt> auxFieldsOffset;
87  };
88 
97  PetscErrorCode ComputeBoundaryPreRHSPointFunction(PetscReal time, PetscReal dt, Vec locXVec, const BoundaryPreRHSPointFunctionDefinition& boundaryPreRhsPointFunction);
98 
99  private:
103  struct GradientStencil {
105  PetscInt cellId;
107  BoundaryFVFaceGeom geometry;
109  std::vector<PetscInt> stencil;
111  PetscInt stencilSize;
113  std::vector<PetscScalar> gradientWeights;
115  std::vector<PetscScalar> distributionWeights;
117  std::vector<PetscScalar> volumes;
118  };
119 
123  struct BoundarySourceFunctionDescription {
124  BoundarySourceFunction function;
125  void* context;
126  BoundarySourceType type;
127 
128  std::vector<PetscInt> sourceFieldsOffset;
129  std::vector<PetscInt> inputFieldsOffset;
130  std::vector<PetscInt> auxFieldsOffset;
131  };
132 
136  struct BoundaryUpdateFunctionDescription {
137  BoundaryUpdateFunction function;
138  void* context;
139 
140  std::vector<PetscInt> inputFields;
141  std::vector<PetscInt> auxFields;
142  };
143 
144  // Hold the region used to define the boundary faces
145  const std::shared_ptr<domain::Region> fieldBoundary;
146 
147  // hold the update functions for flux and point sources
148  std::vector<BoundarySourceFunctionDescription> boundarySourceFunctions;
149 
150  // boundary output functions that can be used for
151  std::vector<BoundarySourceFunctionDescription> boundaryOutputFunctions;
152 
153  // keep track of the output field components
154  std::vector<std::string> outputComponents;
155 
156  // hold the update functions for flux and point sources
157  std::vector<BoundaryUpdateFunctionDescription> boundaryUpdateFunctions;
158 
159  // Hold a list of boundaryProcesses that contribute to this solver
160  std::vector<std::shared_ptr<BoundaryProcess>> boundaryProcesses;
161 
162  // allow the use of any arbitrary pre rhs functions
163  std::vector<std::pair<BoundaryPreRHSFunctionDefinition, void*>> preRhsFunctions;
164 
165  protected:
166  // Hold a list of GradientStencils, this order corresponds to the face order
167  std::vector<GradientStencil> gradientStencils;
168 
169  private:
170  // keep track of maximumStencilSize
171  PetscInt maximumStencilSize = 0;
172 
173  // The PetscFV (usually the least squares method) is used to compute the gradient weights
174  PetscFV gradientCalculator = nullptr;
175 
176  // Determine if multiple faces should be merged for a single cell
177  const bool mergeFaces;
178 
187  void CreateGradientStencil(PetscInt cellId, const BoundaryFVFaceGeom& geometry, const std::vector<PetscInt>& stencil, DM cellDM, const PetscScalar* cellGeomArray);
188 
192  void UpdateVariablesPreStep(TS ts, ablate::solver::Solver&);
193 
194  public:
203  BoundarySolver(std::string solverId, std::shared_ptr<domain::Region> region, std::shared_ptr<domain::Region> fieldBoundary, std::vector<std::shared_ptr<BoundaryProcess>> boundaryProcesses,
204  std::shared_ptr<parameters::Parameters> options, bool mergeFaces = false);
205  ~BoundarySolver() override;
206 
208  void Setup() override;
209  void Initialize() override;
210 
216  void RegisterFunction(BoundarySourceFunction function, void* context, const std::vector<std::string>& sourceFields, const std::vector<std::string>& inputFields,
217  const std::vector<std::string>& auxFields, BoundarySourceType type = BoundarySourceType::Point);
218 
224  void RegisterFunction(BoundaryUpdateFunction function, void* context, const std::vector<std::string>& inputFields, const std::vector<std::string>& auxFields);
225 
231  void RegisterPreRHSFunction(BoundaryPreRHSFunctionDefinition function, void* context);
232 
240  PetscErrorCode ComputeRHSFunction(PetscReal time, Vec locXVec, Vec locFVec) override;
241 
250  PetscErrorCode ComputeRHSFunction(PetscReal time, Vec locXVec, Vec locFVec, const std::vector<BoundarySourceFunctionDescription>& boundarySourceFunctions);
251 
255  void InsertFieldFunctions(const std::vector<std::shared_ptr<mathFunctions::FieldFunction>>& initialization, PetscReal time = 0.0);
256 
260  [[nodiscard]] std::vector<GradientStencil> GetBoundaryGeometry(PetscInt cell) const;
261 
265  const std::vector<GradientStencil>& GetBoundaryGeometry() const { return gradientStencils; }
266 
270  inline const std::vector<std::string>& GetOutputComponents() { return outputComponents; }
271 
275  inline const std::vector<BoundarySourceFunctionDescription>& GetOutputFunctions() { return boundaryOutputFunctions; }
276 
283  PetscErrorCode PreRHSFunction(TS ts, PetscReal time, bool initialStage, Vec locX) override;
284 
289  [[nodiscard]] SerializerType Serialize() const override;
290 
295  [[nodiscard]] const std::string& GetId() const override { return GetSolverId(); }
296 
303  PetscErrorCode Save(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) override;
304 
311  PetscErrorCode Restore(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) override;
312 };
313 
320 std::istream& operator>>(std::istream& is, BoundarySolver::BoundarySourceType& v);
321 
322 } // namespace ablate::boundarySolver
323 #endif // ABLATELIBRARY_BOUNDARYSOLVER_HPP
Definition: boundarySolver.hpp:13
static void ComputeGradientAlongNormal(PetscInt dim, const BoundaryFVFaceGeom *fg, PetscScalar boundaryValue, PetscInt stencilSize, const PetscScalar *stencilValues, const PetscScalar *stencilWeights, PetscScalar &dPhiDNorm)
Definition: boundarySolver.cpp:665
PetscErrorCode Save(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) override
Definition: boundarySolver.cpp:953
const std::vector< BoundarySourceFunctionDescription > & GetOutputFunctions()
Definition: boundarySolver.hpp:275
void RegisterPreRHSFunction(BoundaryPreRHSFunctionDefinition function, void *context)
Definition: boundarySolver.cpp:381
SerializerType Serialize() const override
Definition: boundarySolver.cpp:951
PetscErrorCode(*)(BoundarySolver &, TS ts, PetscReal time, bool initialStage, Vec locX, void *ctx) BoundaryPreRHSFunctionDefinition
Definition: boundarySolver.hpp:38
void RegisterFunction(BoundarySourceFunction function, void *context, const std::vector< std::string > &sourceFields, const std::vector< std::string > &inputFields, const std::vector< std::string > &auxFields, BoundarySourceType type=BoundarySourceType::Point)
Definition: boundarySolver.cpp:318
const std::string & GetId() const override
Definition: boundarySolver.hpp:295
void Setup() override
Definition: boundarySolver.cpp:63
BoundarySourceType
Definition: boundarySolver.hpp:43
BoundarySolver(std::string solverId, std::shared_ptr< domain::Region > region, std::shared_ptr< domain::Region > fieldBoundary, std::vector< std::shared_ptr< BoundaryProcess >> boundaryProcesses, std::shared_ptr< parameters::Parameters > options, bool mergeFaces=false)
Definition: boundarySolver.cpp:8
void InsertFieldFunctions(const std::vector< std::shared_ptr< mathFunctions::FieldFunction >> &initialization, PetscReal time=0.0)
Definition: boundarySolver.cpp:604
const std::vector< std::string > & GetOutputComponents()
Definition: boundarySolver.hpp:270
PetscErrorCode Restore(PetscViewer viewer, PetscInt sequenceNumber, PetscReal time) override
Definition: boundarySolver.cpp:965
PetscErrorCode(*)(PetscInt dim, const BoundaryFVFaceGeom *fg, const PetscFVCellGeom *boundaryCell, const PetscInt uOff[], PetscScalar *boundaryValues, const PetscScalar *stencilValues, const PetscInt aOff[], PetscScalar *auxValues, const PetscScalar *stencilAuxValues, void *ctx) BoundaryUpdateFunction
Definition: boundarySolver.hpp:33
PetscErrorCode ComputeRHSFunction(PetscReal time, Vec locXVec, Vec locFVec) override
Definition: boundarySolver.cpp:383
PetscErrorCode(*)(PetscReal time, PetscReal dt, PetscInt dim, const BoundaryFVFaceGeom *fg, const PetscFVCellGeom *boundaryCell, const PetscInt uOff[], PetscScalar *boundaryValues, const PetscScalar *stencilValues[], const PetscInt aOff[], PetscScalar *auxValues, const PetscScalar *stencilAuxValues[], PetscInt stencilSize, const PetscInt stencil[], const PetscScalar stencilWeights[], void *ctx) BoundaryPreRHSPointFunction
Definition: boundarySolver.hpp:76
const std::vector< GradientStencil > & GetBoundaryGeometry() const
Definition: boundarySolver.hpp:265
PetscErrorCode PreRHSFunction(TS ts, PetscReal time, bool initialStage, Vec locX) override
Definition: boundarySolver.cpp:835
PetscErrorCode ComputeBoundaryPreRHSPointFunction(PetscReal time, PetscReal dt, Vec locXVec, const BoundaryPreRHSPointFunctionDefinition &boundaryPreRhsPointFunction)
Definition: boundarySolver.cpp:853
static void ComputeGradient(PetscInt dim, PetscScalar boundaryValue, PetscInt stencilSize, const PetscScalar *stencilValues, const PetscScalar *stencilWeights, PetscScalar *grad)
Definition: boundarySolver.cpp:652
Definition: serializable.hpp:13
SerializerType
Definition: serializable.hpp:18
Definition: cellSolver.hpp:11
Definition: rhsFunction.hpp:7
Definition: solver.hpp:17
const std::string & GetSolverId() const
Definition: solver.hpp:58
Definition: loggable.hpp:9