NeoN
A framework for CFD software
Loading...
Searching...
No Matches
fieldCollection.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 <limits>
8#include <string>
9#include <functional>
10
12#include "NeoN/core/error.hpp"
14#include "NeoN/core/logging.hpp"
15#include "NeoN/fields/field.hpp"
19
21{
30bool validateVectorDoc(const Document& doc);
31
40{
41public:
42
52 template<class VectorType>
54 const VectorType& field,
55 std::int64_t timeIndex,
56 std::int64_t iterationIndex,
57 std::int64_t subCycleIndex
58 )
59 : doc_(
61 {{"name", field.name},
62 {"timeIndex", timeIndex},
63 {"iterationIndex", iterationIndex},
64 {"subCycleIndex", subCycleIndex},
65 {"field", field}}
66 ),
68 )
69 {}
70
77
84
90 const Document& doc() const;
91
92
98 std::string id() const;
99
105 static std::string typeName();
106
113 template<class VectorType>
114 VectorType& field()
115 {
116 return doc_.get<VectorType&>("field");
117 }
118
125 template<class VectorType>
126 const VectorType& field() const
127 {
128 return doc_.get<const VectorType&>("field");
129 }
130
136 std::string name() const;
137
143 std::string& name();
144
150 std::int64_t timeIndex() const;
151
157 std::int64_t& timeIndex();
158
164 std::int64_t iterationIndex() const;
165
171 std::int64_t& iterationIndex();
172
178 std::int64_t subCycleIndex() const;
179
185 std::int64_t& subCycleIndex();
186
187private:
188
189 Document doc_;
190};
191
201using CreateFunction = std::function<VectorDocument(NeoN::Database& db)>;
202
210class VectorCollection : public CollectionMixin<VectorDocument>
211{
212
213public:
214
222
227
232
237
242
249 bool contains(const std::string& id) const;
250
257 std::string insert(const VectorDocument& fd);
258
265 VectorDocument& fieldDoc(const std::string& id);
266
273 const VectorDocument& fieldDoc(const std::string& id) const;
274
285
286
296 static const VectorCollection& instance(const NeoN::Database& db, std::string name);
297
304 template<class VectorType>
305 static VectorCollection& instance(VectorType& field)
306 {
308 field, "attempting to retrieve VectorCollection from unregistered field"
309 );
310 return instance(field.db(), field.fieldCollectionName);
311 }
312
319 template<class VectorType>
320 static const VectorCollection& instance(const VectorType& field)
321 {
323 field, "attempting to retrieve VectorCollection from unregistered field"
324 );
325 const Database& db = field.db();
326 const Collection& collection = db.at(field.fieldCollectionName);
327 return collection.as<VectorCollection>();
328 // return instance(field.db(), field.fieldCollectionName);
329 }
330
338 template<class VectorType>
339 VectorType& registerVector(CreateFunction createFunc)
340 {
341 VectorDocument doc = createFunc(db());
342 if (!validateVectorDoc(doc.doc()))
343 {
344 throw std::runtime_error {"Document is not valid"};
345 }
346
347 std::string key = insert(doc);
348 VectorDocument& fd = fieldDoc(key);
349 VectorType& field = fd.field<VectorType>();
350 field.key = key;
351 field.fieldCollectionName = name();
352
353 return field;
354 }
355};
356
357
371template<typename VectorType>
373{
374public:
375
376 std::string name;
377 const VectorType& field;
378 std::int64_t timeIndex = std::numeric_limits<std::int64_t>::max();
379 std::int64_t iterationIndex = std::numeric_limits<std::int64_t>::max();
380 std::int64_t subCycleIndex = std::numeric_limits<std::int64_t>::max();
381
383 {
385 field.mesh().exec(), field.internalVector(), field.boundaryData()
386 );
387
388 VectorType vf(
389 field.exec(), name, field.mesh(), domainVector, field.boundaryConditions(), db, "", ""
390 );
391
392 if (field.registered())
393 {
394 const VectorCollection& fieldCollection = VectorCollection::instance(field);
395 const VectorDocument& fieldDoc = fieldCollection.fieldDoc(field.key);
396 if (timeIndex == std::numeric_limits<std::int64_t>::max())
397 {
398 timeIndex = fieldDoc.timeIndex();
399 }
400 if (iterationIndex == std::numeric_limits<std::int64_t>::max())
401 {
402 iterationIndex = fieldDoc.iterationIndex();
403 }
404 if (subCycleIndex == std::numeric_limits<std::int64_t>::max())
405 {
406 subCycleIndex = fieldDoc.subCycleIndex();
407 }
408 }
409 return NeoN::Document(
410 {{"name", vf.name},
411 {"timeIndex", timeIndex},
412 {"iterationIndex", iterationIndex},
413 {"subCycleIndex", subCycleIndex},
414 {"field", vf}},
416 );
417 }
418};
419
420
421} // 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:52
Represents the domain fields for a computational domain.
Definition field.hpp:36
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:121