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