ABLATE Source Documentation  0.12.34
particleSolver.hpp
1 #ifndef ABLATELIBRARY_PARTICLESOLVER_HPP
2 #define ABLATELIBRARY_PARTICLESOLVER_HPP
3 
4 #include "field.hpp"
5 #include "fieldDescription.hpp"
6 #include "initializers/initializer.hpp"
7 #include "processes/process.hpp"
8 #include "solver/solver.hpp"
9 
10 namespace ablate::particles {
11 
13  public:
15  inline static const char ParticleVelocity[] = "ParticleVelocity";
16  inline static const char ParticleDiameter[] = "ParticleDiameter";
17  inline static const char ParticleDensity[] = "ParticleDensity";
18  inline static const char PackedSolution[] = "PackedSolution";
19  inline static const char ParticleInitialLocation[] = "InitialLocation";
20 
22  inline static const char ParticleCoordinates[] = "coordinates";
23 
24  protected:
26  DM swarmDm = nullptr;
27 
29  TS particleTs = nullptr;
30 
32  PetscReal timeInitial = 0.0;
33 
35  PetscReal timeFinal = 0.0;
36 
38  PetscInt ndims = 0;
39 
41  bool dmChanged = false;
42 
44  std::vector<FieldDescription> fieldsDescriptions;
45 
47  std::vector<Field> fields;
48 
50  std::map<std::string, Field> fieldsMap;
51 
53  std::vector<std::shared_ptr<processes::Process>> processes;
54 
56  std::shared_ptr<particles::initializers::Initializer> initializer = nullptr;
57 
59  const std::vector<std::shared_ptr<mathFunctions::FieldFunction>> fieldInitialization;
60 
62  const std::vector<std::shared_ptr<mathFunctions::FieldFunction>> exactSolutions;
63 
64  public:
75  ParticleSolver(std::string solverId, std::shared_ptr<domain::Region>, std::shared_ptr<parameters::Parameters> options, std::vector<FieldDescription> fields,
76  std::vector<std::shared_ptr<processes::Process>> processes, std::shared_ptr<initializers::Initializer> initializer,
77  std::vector<std::shared_ptr<mathFunctions::FieldFunction>> fieldInitialization, std::vector<std::shared_ptr<mathFunctions::FieldFunction>> exactSolutions = {});
78 
89  ParticleSolver(std::string solverId, std::shared_ptr<domain::Region>, std::shared_ptr<parameters::Parameters> options, const std::vector<std::shared_ptr<FieldDescription>>& fields,
90  std::vector<std::shared_ptr<processes::Process>> processes, std::shared_ptr<initializers::Initializer> initializer,
91  std::vector<std::shared_ptr<mathFunctions::FieldFunction>> fieldInitialization, std::vector<std::shared_ptr<mathFunctions::FieldFunction>> exactSolutions = {});
92 
93  ~ParticleSolver() override;
94 
96  void Setup() override;
97 
98  /*** Set up mesh dependent initialization, this may be called multiple times if the mesh changes **/
99  void Initialize() override;
100 
105  inline DM GetParticleDM() { return swarmDm; }
106 
111  inline TS GetParticleTS() { return particleTs; }
112 
118  static PetscErrorCode ComputeParticleExactSolution(TS particleTS, Vec);
119 
124  [[nodiscard]] const std::string& GetId() const override { return GetSolverId(); }
125 
133  PetscErrorCode Save(PetscViewer viewer, PetscInt steps, PetscReal time) override;
134 
142  PetscErrorCode Restore(PetscViewer viewer, PetscInt steps, PetscReal time) override;
143 
144  protected:
148  virtual void MacroStepParticles(TS macroTS, bool swarmMigrate);
149 
154  void RegisterParticleField(const FieldDescription& fieldDescriptor);
155 
160 
168  static PetscErrorCode ComputeParticleError(TS particleTS, Vec u, Vec e);
169 
173  void ProjectFunction(const std::shared_ptr<mathFunctions::FieldFunction>& fieldFunction, PetscReal time = 0.0);
174 
178  void SwarmMigrate();
179 
184 
189 
190  protected:
194  template <class T>
195  void GetField(const Field& field, T** values) {
196  if (field.location == domain::FieldLocation::SOL) {
197  // Get the solution vector
198  DMSwarmGetField(swarmDm, PackedSolution, nullptr, nullptr, (void**)values) >> utilities::PetscUtilities::checkError;
199  } else {
200  // get the raw field
201  DMSwarmGetField(swarmDm, field.name.c_str(), nullptr, nullptr, (void**)values) >> utilities::PetscUtilities::checkError;
202  }
203  }
204 
208  template <class T>
209  void RestoreField(const Field& field, T** values) {
210  if (field.location == domain::FieldLocation::SOL) {
211  // Get the solution vector
212  DMSwarmRestoreField(swarmDm, PackedSolution, nullptr, nullptr, (void**)values) >> utilities::PetscUtilities::checkError;
213  } else {
214  // get the raw field
215  DMSwarmRestoreField(swarmDm, field.name.c_str(), nullptr, nullptr, (void**)values) >> utilities::PetscUtilities::checkError;
216  }
217  }
218 
222  template <class T>
223  const Field& GetField(const std::string& fieldName, T** values) {
224  const auto& field = GetField(fieldName);
225  GetField(field, values);
226  return field;
227  }
228 
232  template <class T>
233  void RestoreField(const std::string& fieldName, T** values) {
234  const auto& field = GetField(fieldName);
235  RestoreField(field, values);
236  }
237 
241  [[nodiscard]] const Field& GetField(const std::string& fieldName) const { return fieldsMap.at(fieldName); }
242 
252  static PetscErrorCode ComputeParticleRHS(TS ts, PetscReal t, Vec X, Vec F, void* ctx);
253 };
254 
255 } // namespace ablate::particles
256 #endif // ABLATELIBRARY_PARTICLESOLVER_HPP
Definition: serializable.hpp:13
Definition: particleSolver.hpp:12
std::vector< std::shared_ptr< processes::Process > > processes
the processes that add source terms to the particle and domain ts
Definition: particleSolver.hpp:53
const Field & GetField(const std::string &fieldName) const
Definition: particleSolver.hpp:241
DM swarmDm
particle dm, this is a swarm
Definition: particleSolver.hpp:26
void Setup() override
Definition: particleSolver.cpp:39
void RestoreField(const std::string &fieldName, T **values)
Definition: particleSolver.hpp:233
bool dmChanged
store a boolean to state if a dmChanged (number of particles local/global changed)
Definition: particleSolver.hpp:41
std::map< std::string, Field > fieldsMap
a map of fields for easy field lookup
Definition: particleSolver.hpp:50
void StoreInitialParticleLocations()
Definition: particleSolver.cpp:205
static const char ParticleVelocity[]
Definition: particleSolver.hpp:15
void SwarmMigrate()
Definition: particleSolver.cpp:361
std::vector< FieldDescription > fieldsDescriptions
the fields specific to be created to create in the particle solver
Definition: particleSolver.hpp:44
PetscErrorCode Save(PetscViewer viewer, PetscInt steps, PetscReal time) override
Definition: particleSolver.cpp:606
void RestoreField(const Field &field, T **values)
Definition: particleSolver.hpp:209
static PetscErrorCode ComputeParticleExactSolution(TS particleTS, Vec)
Definition: particleSolver.cpp:523
std::vector< Field > fields
all fields in the particle solver
Definition: particleSolver.hpp:47
void GetField(const Field &field, T **values)
Definition: particleSolver.hpp:195
void RegisterParticleField(const FieldDescription &fieldDescriptor)
Definition: particleSolver.cpp:167
static PetscErrorCode ComputeParticleRHS(TS ts, PetscReal t, Vec X, Vec F, void *ctx)
Definition: particleSolver.cpp:495
PetscReal timeFinal
The time for uf, at the end of the advection solve.
Definition: particleSolver.hpp:35
TS particleTs
time integration data
Definition: particleSolver.hpp:29
PetscInt ndims
the dims from the subdomain
Definition: particleSolver.hpp:38
static PetscErrorCode ComputeParticleError(TS particleTS, Vec u, Vec e)
Definition: particleSolver.cpp:222
ParticleSolver(std::string solverId, std::shared_ptr< domain::Region >, std::shared_ptr< parameters::Parameters > options, std::vector< FieldDescription > fields, std::vector< std::shared_ptr< processes::Process >> processes, std::shared_ptr< initializers::Initializer > initializer, std::vector< std::shared_ptr< mathFunctions::FieldFunction >> fieldInitialization, std::vector< std::shared_ptr< mathFunctions::FieldFunction >> exactSolutions={})
Definition: particleSolver.cpp:11
TS GetParticleTS()
Definition: particleSolver.hpp:111
void ProjectFunction(const std::shared_ptr< mathFunctions::FieldFunction > &fieldFunction, PetscReal time=0.0)
Definition: particleSolver.cpp:325
void CoordinatesToSolutionVector()
Definition: particleSolver.cpp:437
const std::vector< std::shared_ptr< mathFunctions::FieldFunction > > exactSolutions
store the exact solution if provided
Definition: particleSolver.hpp:62
const std::string & GetId() const override
Definition: particleSolver.hpp:124
const std::vector< std::shared_ptr< mathFunctions::FieldFunction > > fieldInitialization
initialize other particle variables
Definition: particleSolver.hpp:59
static const char ParticleCoordinates[]
These coordinates are part of the solution vector.
Definition: particleSolver.hpp:22
const Field & GetField(const std::string &fieldName, T **values)
Definition: particleSolver.hpp:223
DM GetParticleDM()
Definition: particleSolver.hpp:105
std::shared_ptr< particles::initializers::Initializer > initializer
Store the particle location and field initialization.
Definition: particleSolver.hpp:56
void CoordinatesFromSolutionVector()
Definition: particleSolver.cpp:466
PetscErrorCode Restore(PetscViewer viewer, PetscInt steps, PetscReal time) override
Definition: particleSolver.cpp:674
virtual void MacroStepParticles(TS macroTS, bool swarmMigrate)
Definition: particleSolver.cpp:390
PetscReal timeInitial
The time for ui, at the beginning of the advection solve.
Definition: particleSolver.hpp:32
Definition: solver.hpp:17
const std::string & GetSolverId() const
Definition: solver.hpp:58
static utilities::PetscUtilities::ErrorChecker checkError
Definition: petscUtilities.hpp:46
Definition: fieldDescription.hpp:11
Definition: field.hpp:11
enum domain::FieldLocation location
The field location (sol or aux)
Definition: field.hpp:22
const std::string name
The unique name of the particle field.
Definition: field.hpp:13