NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
oldTimeCollection.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// SPDX-FileCopyrightText: 2024 NeoN authors
3#pragma once
4
5#include <string>
6
11
12
14{
15
17{
18public:
19
21
23 std::string nextTime, std::string previousTime, std::string currentTime, int32_t level
24 );
25
26 std::string& nextTime();
27
28 const std::string& nextTime() const;
29
30 std::string& previousTime();
31
32 const std::string& previousTime() const;
33
34 std::string& currentTime();
35
36 const std::string& currentTime() const;
37
38 int32_t& level();
39
40 const int32_t& level() const;
41
43
44 const Document& doc() const;
45
46 std::string id() const;
47
48 static std::string typeName();
49
50private:
51
52 Document doc_;
53};
54
55
56class OldTimeCollection : public CollectionMixin<OldTimeDocument>
57{
58public:
59
60 OldTimeCollection(Database& db, std::string name, std::string fieldCollectionName);
61
62 bool contains(const std::string& id) const;
63
64 bool insert(const OldTimeDocument& cc);
65
66 std::string findNextTime(std::string id) const;
67
68 std::string findPreviousTime(std::string id) const;
69
70 OldTimeDocument& oldTimeDoc(const std::string& id);
71
72 const OldTimeDocument& oldTimeDoc(const std::string& id) const;
73
74 template<typename VectorType>
75 VectorType& getOrInsert(std::string idOfNextVector)
76 {
77 std::string nextId = findNextTime(idOfNextVector);
78 VectorCollection& fieldCollection = VectorCollection::instance(db(), fieldCollectionName_);
79
80 if (nextId != "") // oldVector is already registered
81 {
82 OldTimeDocument& oldTimeDocument = oldTimeDoc(nextId);
83 return fieldCollection.fieldDoc(oldTimeDocument.previousTime()).field<VectorType>();
84 }
85 VectorDocument& fieldDoc = fieldCollection.fieldDoc(idOfNextVector);
86
87 std::string oldTimeName = fieldDoc.field<VectorType>().name + "_0";
88 VectorType& oldVector =
89 fieldCollection.registerVector<VectorType>(CreateFromExistingVector<VectorType> {
90 .name = oldTimeName,
91 .field = fieldDoc.field<VectorType>(),
92 .timeIndex = fieldDoc.timeIndex() - 1,
93 .iterationIndex = fieldDoc.iterationIndex(),
94 .subCycleIndex = fieldDoc.subCycleIndex()
95 });
96 OldTimeDocument oldTimeDocument(fieldDoc.field<VectorType>().key, oldVector.key, "", -1);
97 setCurrentVectorAndLevel(oldTimeDocument);
98 insert(oldTimeDocument);
99 return oldVector;
100 }
101
102 template<typename VectorType>
103 const VectorType& get(std::string idOfNextVector) const
104 {
105 std::string nextId = findNextTime(idOfNextVector);
106 const VectorCollection& fieldCollection =
107 VectorCollection::instance(db(), fieldCollectionName_);
108
109 if (nextId != "") // oldVector has to be registered
110 {
111 const OldTimeDocument& oldTimeDocument = oldTimeDoc(nextId);
112 return fieldCollection.fieldDoc(oldTimeDocument.previousTime()).field<VectorType>();
113 }
114 else
115 {
116 // TODO replace with NF_THROW
117 NF_ERROR_EXIT("Old field not found");
118 }
119 }
120
121 static OldTimeCollection&
122 instance(Database& db, std::string name, std::string fieldCollectionName);
123
124 static const OldTimeCollection& instance(const Database& db, std::string name);
125
126 static OldTimeCollection& instance(VectorCollection& fieldCollection);
127
128 static const OldTimeCollection& instance(const VectorCollection& fieldCollection);
129
130private:
131
133 void setCurrentVectorAndLevel(OldTimeDocument& oldTimeDoc);
134
135 std::string fieldCollectionName_;
136};
137
146template<typename VectorType>
147VectorType& oldTime(VectorType& field)
148{
149 VectorCollection& fieldCollection = VectorCollection::instance(field);
150 OldTimeCollection& oldTimeCollection = OldTimeCollection::instance(fieldCollection);
151 return oldTimeCollection.getOrInsert<VectorType>(field.key);
152}
153
162template<typename VectorType>
163const VectorType& oldTime(const VectorType& field)
164{
165 const VectorCollection& fieldCollection = VectorCollection::instance(field);
166 const OldTimeCollection& oldTimeCollection = OldTimeCollection::instance(fieldCollection);
167 return oldTimeCollection.get<VectorType>(field.key);
168}
169
170} // namespace NeoN
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:51
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:108
VectorType & oldTime(VectorType &field)
Retrieves the old time field of a given field.