NeoN
A framework for CFD software
Loading...
Searching...
No Matches
oldTimeCollection.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2024 - 2026 NeoN authors
2//
3// SPDX-License-Identifier: MIT
4
5#pragma once
6
7#include <string>
8
15
17{
18
19// dont do dangling rereference warning here
20#if defined(__GNUC__) or defined(__llvm__)
21#pragma GCC diagnostic push
22#pragma GCC diagnostic ignored "-Wdangling-reference"
23#endif
24
26{
27public:
28
30
32 std::string nextTime, std::string previousTime, std::string currentTime, int32_t level
33 );
34
35 std::string& nextTime();
36
37 const std::string& nextTime() const;
38
39 std::string& previousTime();
40
41 const std::string& previousTime() const;
42
43 std::string& currentTime();
44
45 const std::string& currentTime() const;
46
47 int32_t& level();
48
49 const int32_t& level() const;
50
52
53 const Document& doc() const;
54
55 std::string id() const;
56
57 static std::string typeName();
58
59private:
60
61 Document doc_;
62};
63
64
65class OldTimeCollection : public CollectionMixin<OldTimeDocument>
66{
67public:
68
69 OldTimeCollection(Database& db, std::string name, std::string fieldCollectionName);
70
71 bool contains(const std::string& id) const;
72
73 bool insert(const OldTimeDocument& cc);
74
75 std::string findNextTime(std::string id) const;
76
77 std::string findPreviousTime(std::string id) const;
78
79 OldTimeDocument& oldTimeDoc(const std::string& id);
80
81 const OldTimeDocument& oldTimeDoc(const std::string& id) const;
82
83 template<typename VectorType>
84 VectorType& getOrInsert(std::string idOfNextVector)
85 {
86 std::string nextId = findNextTime(idOfNextVector);
87 VectorCollection& fieldCollection = VectorCollection::instance(db(), fieldCollectionName_);
88
89 if (nextId != "") // oldVector is already registered
90 {
91 OldTimeDocument& oldTimeDocument = oldTimeDoc(nextId);
92 return fieldCollection.fieldDoc(oldTimeDocument.previousTime()).field<VectorType>();
93 }
94 VectorDocument& fieldDoc = fieldCollection.fieldDoc(idOfNextVector);
95
96 std::string oldTimeName = fieldDoc.field<VectorType>().name + "_0";
97 VectorType& oldVector =
98 fieldCollection.registerVector<VectorType>(CreateFromExistingVector<VectorType> {
99 .name = oldTimeName,
100 .field = fieldDoc.field<VectorType>(),
101 .timeIndex = fieldDoc.timeIndex() - 1,
102 .iterationIndex = fieldDoc.iterationIndex(),
103 .subCycleIndex = fieldDoc.subCycleIndex()
104 });
105 OldTimeDocument oldTimeDocument(fieldDoc.field<VectorType>().key, oldVector.key, "", 0);
106 setCurrentVectorAndLevel(oldTimeDocument);
107 insert(oldTimeDocument);
108 return oldVector;
109 }
110
111 template<typename VectorType>
112 const VectorType& get(std::string idOfNextVector) const
113 {
114 std::string nextId = findNextTime(idOfNextVector);
115
116 // NOTE this triggers a false positive dangling reference warning
117 // on gcc <= 15
118 const VectorCollection& fieldCollection =
119 VectorCollection::instance(db(), fieldCollectionName_);
120
121 if (nextId != "") // oldVector has to be registered
122 {
123 const OldTimeDocument& oldTimeDocument = oldTimeDoc(nextId);
124 return fieldCollection.fieldDoc(oldTimeDocument.previousTime()).field<VectorType>();
125 }
126 else
127 {
128 // TODO replace with NF_THROW
129 NF_ERROR_EXIT("Old field not found");
130 }
131 }
132
133 static OldTimeCollection&
134 instance(Database& db, std::string name, std::string fieldCollectionName);
135
136 static const OldTimeCollection& instance(const Database& db, std::string name);
137
138 static OldTimeCollection& instance(VectorCollection& fieldCollection);
139
140 static const OldTimeCollection& instance(const VectorCollection& fieldCollection);
141
142private:
143
145 void setCurrentVectorAndLevel(OldTimeDocument& oldTimeDoc);
146
147 std::string fieldCollectionName_;
148};
149
158template<typename VectorType>
159VectorType& oldTime(VectorType& field)
160{
161 VectorCollection& fieldCollection = VectorCollection::instance(field);
162 OldTimeCollection& oldTimeCollection = OldTimeCollection::instance(fieldCollection);
163 return oldTimeCollection.getOrInsert<VectorType>(field.key);
164}
165
174template<typename VectorType>
175const VectorType& oldTime(const VectorType& field)
176{
177 const VectorCollection& fieldCollection = VectorCollection::instance(field);
178 const OldTimeCollection& oldTimeCollection = OldTimeCollection::instance(fieldCollection);
179 return oldTimeCollection.get<VectorType>(field.key);
180}
181
188template<typename VectorType>
189inline int oldTimeLevel(const VectorType& field)
190{
191 const auto& fieldCollection = VectorCollection::instance(field);
192 const auto& oldTimeCollection = OldTimeCollection::instance(fieldCollection);
193
194 int level = 0;
195 std::string currentId = field.key;
196
197 while (true)
198 {
199 std::string nextId = oldTimeCollection.findNextTime(currentId);
200 if (nextId.empty())
201 {
202 return level;
203 }
204
205 ++level;
206 currentId = oldTimeCollection.oldTimeDoc(nextId).previousTime();
207 }
208}
209
244template<typename VectorType>
245void rotateOldTimes(VectorType& field);
246
247} // namespace NeoN
248
249#if defined(__GNUC__) or defined(__llvm__)
250#pragma GCC diagnostic pop
251#endif
A mixin class for collection of documents in a database to simplify the implementation of common oper...
const std::string & name() const
Gets the name of the collection.
const NeoN::Database & db() const
Gets a const reference to the database.
A class representing a document in a database.
Definition document.hpp:52
Creates a VectorDocument from an existing field.
static const OldTimeCollection & instance(const VectorCollection &fieldCollection)
static const OldTimeCollection & instance(const Database &db, std::string name)
static OldTimeCollection & instance(Database &db, std::string name, std::string fieldCollectionName)
std::string findNextTime(std::string id) const
VectorType & getOrInsert(std::string idOfNextVector)
std::string findPreviousTime(std::string id) const
const VectorType & get(std::string idOfNextVector) const
OldTimeCollection(Database &db, std::string name, std::string fieldCollectionName)
OldTimeDocument & oldTimeDoc(const std::string &id)
static OldTimeCollection & instance(VectorCollection &fieldCollection)
bool contains(const std::string &id) const
const OldTimeDocument & oldTimeDoc(const std::string &id) const
OldTimeDocument(std::string nextTime, std::string previousTime, std::string currentTime, int32_t level)
A class representing a collection of field documents in a database.
VectorType & registerVector(CreateFunction createFunc)
Registers a field in the collection.
VectorDocument & fieldDoc(const std::string &id)
Retrieves a field document by its ID.
static VectorCollection & instance(NeoN::Database &db, std::string name)
Retrieves the instance of the VectorCollection with the given name.
A class representing a field document in a database.
std::int64_t subCycleIndex() const
Retrieves the sub-cycle index of the field.
std::int64_t timeIndex() const
Retrieves the time index of the field.
std::int64_t iterationIndex() const
Retrieves the iteration index of the field.
VectorType & field()
Retrieves the field from the document.
#define NF_ERROR_EXIT(message)
Macro for printing an error message and aborting the program.
Definition error.hpp:90
int oldTimeLevel(const VectorType &field)
Helper function to retrieve the history depth of a field in oldTimeCollection.
void rotateOldTimes(VectorType &field)
Rotate time levels for a field. Meaning of level (computed via oldTimeLevel(field)): level == 0 : no ...
VectorType & oldTime(VectorType &field)
Retrieves the old time field of a given field.