NeoN
WIP Prototype of a modern OpenFOAM core
Loading...
Searching...
No Matches
fieldCollection.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 <limits>
6#include <string>
7#include <functional>
8
10#include "NeoN/core/error.hpp"
11#include "NeoN/core/vector.hpp"
12#include "NeoN/fields/field.hpp"
16
18{
27bool validateVectorDoc(const Document& doc);
28
37{
38public:
39
49 template<class VectorType>
51 const VectorType& field,
52 std::int64_t timeIndex,
53 std::int64_t iterationIndex,
54 std::int64_t subCycleIndex
55 )
56 : doc_(
58 {{"name", field.name},
59 {"timeIndex", timeIndex},
60 {"iterationIndex", iterationIndex},
61 {"subCycleIndex", subCycleIndex},
62 {"field", field}}
63 ),
65 )
66 {}
67
74
81
87 const Document& doc() const;
88
89
95 std::string id() const;
96
102 static std::string typeName();
103
110 template<class VectorType>
111 VectorType& field()
112 {
113 return doc_.get<VectorType&>("field");
114 }
115
122 template<class VectorType>
123 const VectorType& field() const
124 {
125 return doc_.get<const VectorType&>("field");
126 }
127
133 std::string name() const;
134
140 std::string& name();
141
147 std::int64_t timeIndex() const;
148
154 std::int64_t& timeIndex();
155
161 std::int64_t iterationIndex() const;
162
168 std::int64_t& iterationIndex();
169
175 std::int64_t subCycleIndex() const;
176
182 std::int64_t& subCycleIndex();
183
184private:
185
186 Document doc_;
187};
188
198using CreateFunction = std::function<VectorDocument(NeoN::Database& db)>;
199
207class VectorCollection : public CollectionMixin<VectorDocument>
208{
209public:
210
218
223
228
233
238
245 bool contains(const std::string& id) const;
246
253 std::string insert(const VectorDocument& fd);
254
261 VectorDocument& fieldDoc(const std::string& id);
262
269 const VectorDocument& fieldDoc(const std::string& id) const;
270
281
282
292 static const VectorCollection& instance(const NeoN::Database& db, std::string name);
293
300 template<class VectorType>
301 static VectorCollection& instance(VectorType& field)
302 {
304 field, "attempting to retrieve VectorCollection from unregistered field"
305 );
306 return instance(field.db(), field.fieldCollectionName);
307 }
308
315 template<class VectorType>
316 static const VectorCollection& instance(const VectorType& field)
317 {
319 field, "attempting to retrieve VectorCollection from unregistered field"
320 );
321 const Database& db = field.db();
322 const Collection& collection = db.at(field.fieldCollectionName);
323 return collection.as<VectorCollection>();
324 // return instance(field.db(), field.fieldCollectionName);
325 }
326
334 template<class VectorType>
335 VectorType& registerVector(CreateFunction createFunc)
336 {
337 VectorDocument doc = createFunc(db());
338 if (!validateVectorDoc(doc.doc()))
339 {
340 throw std::runtime_error {"Document is not valid"};
341 }
342
343 std::string key = insert(doc);
344 VectorDocument& fd = fieldDoc(key);
345 VectorType& field = fd.field<VectorType>();
346 field.key = key;
347 field.fieldCollectionName = name();
348 return field;
349 }
350};
351
352
366template<typename VectorType>
368{
369public:
370
371 std::string name;
372 const VectorType& field;
373 std::int64_t timeIndex = std::numeric_limits<std::int64_t>::max();
374 std::int64_t iterationIndex = std::numeric_limits<std::int64_t>::max();
375 std::int64_t subCycleIndex = std::numeric_limits<std::int64_t>::max();
376
378 {
380 field.mesh().exec(), field.internalVector(), field.boundaryData()
381 );
382
383 VectorType vf(
384 field.exec(), name, field.mesh(), domainVector, field.boundaryConditions(), db, "", ""
385 );
386
387 if (field.registered())
388 {
389 const VectorCollection& fieldCollection = VectorCollection::instance(field);
390 const VectorDocument& fieldDoc = fieldCollection.fieldDoc(field.key);
391 if (timeIndex == std::numeric_limits<std::int64_t>::max())
392 {
393 timeIndex = fieldDoc.timeIndex();
394 }
395 if (iterationIndex == std::numeric_limits<std::int64_t>::max())
396 {
397 iterationIndex = fieldDoc.iterationIndex();
398 }
399 if (subCycleIndex == std::numeric_limits<std::int64_t>::max())
400 {
401 subCycleIndex = fieldDoc.subCycleIndex();
402 }
403 }
404 return NeoN::Document(
405 {{"name", vf.name},
406 {"timeIndex", timeIndex},
407 {"iterationIndex", iterationIndex},
408 {"subCycleIndex", subCycleIndex},
409 {"field", vf}},
411 );
412 }
413};
414
415
416} // 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.
Document & doc(const std::string &id)
Retrieves a document by its ID.
A type-erased interface collection types.
CollectionType & as()
Casts the collection to a specific collection type.
Collection & at(const std::string &name)
Retrieves a collection by its name.
T & get(const std::string &key)
Retrieves the value associated with the given key, casting it to the specified type.
A class representing a document in a database.
Definition document.hpp:51
Represents the domain fields for a computational domain.
Definition field.hpp:34
Creates a VectorDocument from an existing field.
A class representing a collection of field documents in a database.
VectorCollection(const VectorCollection &)=delete
A VectorCollection is not copyable, but moveable.
VectorCollection(NeoN::Database &db, std::string name)
Constructs a VectorCollection with the given database and name.
static const VectorCollection & instance(const NeoN::Database &db, std::string name)
Retrieves the instance of the VectorCollection with the given name (const version).
VectorType & registerVector(CreateFunction createFunc)
Registers a field in the collection.
const VectorDocument & fieldDoc(const std::string &id) const
Retrieves a field document by its ID (const version).
static VectorCollection & instance(VectorType &field)
Retrieves the instance of the VectorCollection from a const registered VectorType.
VectorDocument & fieldDoc(const std::string &id)
Retrieves a field document by its ID.
std::string insert(const VectorDocument &fd)
Inserts a field document into the collection.
VectorCollection(VectorCollection &&)=default
A VectorCollection is move constructable, but not copyable.
static const VectorCollection & instance(const VectorType &field)
Retrieves the instance of the VectorCollection from a const registered VectorType.
VectorCollection & operator=(const VectorCollection &)=delete
A VectorCollection is not copyable, but moveable.
VectorCollection & operator=(VectorCollection &&)=delete
A VectorCollection is not move-assign-able, but move-construct-able.
bool contains(const std::string &id) const
Checks if the collection contains a field with the given 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.
static std::string typeName()
Retrieves the type name of the field.
const VectorType & field() const
Retrieves the field from the document (const version).
std::string id() const
Retrieves the unique identifier of the field collection.
VectorDocument(const VectorType &field, std::int64_t timeIndex, std::int64_t iterationIndex, std::int64_t subCycleIndex)
Constructs a VectorDocument with the given field and metadata.
std::int64_t subCycleIndex() const
Retrieves the sub-cycle index of the field.
const Document & doc() const
Retrieves the underlying Document (const version).
std::int64_t & timeIndex()
Retrieves the time index of the field.
std::int64_t timeIndex() const
Retrieves the time index of the field.
Document & doc()
Retrieves the underlying Document.
std::string name() const
Retrieves the name of the field.
std::int64_t & iterationIndex()
Retrieves the iteration index of the field.
std::int64_t iterationIndex() const
Retrieves the iteration index of the field.
VectorType & field()
Retrieves the field from the document.
std::string & name()
Retrieves the time index of the field.
std::int64_t & subCycleIndex()
Retrieves the sub-cycle index of the field.
VectorDocument(const Document &doc)
Constructs a VectorDocument with the given Document.
bool validateVectorDoc(const Document &doc)
Validates a VectorDocument.
std::function< VectorDocument(NeoN::Database &db)> CreateFunction
A function type for creating a VectorDocument.
void validateRegistration(const Type &field, const std::string &errorMessage)
Validates that a field is registered in the database.
Definition database.hpp:120