ABLATE Source Documentation  0.12.33
field.hpp
1 #ifndef ABLATELIBRARY_FIELD_HPP
2 #define ABLATELIBRARY_FIELD_HPP
3 
4 #include <petsc.h>
5 #include <algorithm>
6 #include <set>
7 #include <string>
8 #include <vector>
9 
10 namespace ablate::domain {
11 
12 enum class FieldLocation { SOL, AUX };
13 enum class FieldType { FVM, FEM };
14 
15 struct FieldDescription;
16 
17 struct Field {
18  const std::string name;
19  const PetscInt numberComponents;
20  const std::vector<std::string> components;
21 
22  // The global id in the dm for this field
23  const PetscInt id;
24 
25  // The specific id in this ds/subdomain
26  const PetscInt subId = PETSC_DEFAULT;
27 
28  // The specific offset for this field in this ds/subdomain
29  const PetscInt offset = PETSC_DEFAULT;
30 
31  const enum FieldLocation location = FieldLocation::SOL;
32 
33  // Keep track of the field type
34  const enum FieldType type;
35 
36  // store any optional tags, there are strings that can be used to describe the field
37  const std::set<std::string> tags;
38 
39  static Field FromFieldDescription(const FieldDescription& fieldDescription, PetscInt id, PetscInt subId = PETSC_DEFAULT, PetscInt offset = PETSC_DEFAULT);
40 
41  [[nodiscard]] Field CreateSubField(PetscInt subId, PetscInt offset) const;
42 
43  // helper function to check if the field contains a certain tag
44  [[nodiscard]] inline bool Tagged(std::string_view tag) const {
45  return std::any_of(tags.begin(), tags.end(), [tag](const auto& tagItem) { return tagItem == tag; });
46  }
47 
53  [[nodiscard]] Field Rename(std::string newName) const;
54 
60  [[nodiscard]] inline std::size_t ComponentIndex(std::string_view search) const {
61  auto it = std::find_if(components.begin(), components.end(), [search](const auto& component) { return component == search; });
62 
63  // If element was found
64  if (it != components.end()) {
65  return std::distance(components.begin(), it);
66  } else {
67  throw std::invalid_argument(std::string("Cannot locate component ") + std::string(search) + " in field " + name);
68  }
69  }
70 
76  [[nodiscard]] inline std::size_t ComponentOffset(std::string_view search) const { return ComponentIndex(search) + offset; }
77 };
78 
79 std::istream& operator>>(std::istream& is, FieldLocation& v);
80 std::istream& operator>>(std::istream& is, FieldType& v);
81 
82 } // namespace ablate::domain
83 #endif // ABLATELIBRARY_FIELD_HPP
Definition: fieldDescription.hpp:20
Definition: field.hpp:17
Field Rename(std::string newName) const
Definition: field.cpp:53
std::size_t ComponentIndex(std::string_view search) const
Definition: field.hpp:60
std::size_t ComponentOffset(std::string_view search) const
Definition: field.hpp:76