NeoN
A framework for CFD software
Loading...
Searching...
No Matches
fieldCollection.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 <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{
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
91
97 const Document& doc() const;
98
99
105 std::string id() const;
106
112 static std::string typeName();
113
120 template<class VectorType>
121 VectorType& field()
122 {
123 return doc_.get<VectorType&>("field");
124 }
125
132 template<class VectorType>
133 const VectorType& field() const
134 {
135 return doc_.get<const VectorType&>("field");
136 }
137
143 std::string name() const;
144
150 std::string& name();
151
157 std::int64_t timeIndex() const;
158
164 std::int64_t& timeIndex();
165
171 std::int64_t iterationIndex() const;
172
178 std::int64_t& iterationIndex();
179
185 std::int64_t subCycleIndex() const;
186
192 std::int64_t& subCycleIndex();
193
194private:
195
196 Document doc_;
197};
198
208using CreateFunction = std::function<VectorDocument(NeoN::Database& db)>;
209
217class VectorCollection : public CollectionMixin<VectorDocument>
218{
219
220public:
221
229
234
239
244
249
256 bool contains(const std::string& id) const;
257
264 std::string insert(const VectorDocument& fd);
265
267 std::string insert(VectorDocument&& fd);
268
275 VectorDocument& fieldDoc(const std::string& id);
276
283 const VectorDocument& fieldDoc(const std::string& id) const;
284
295
296
306 static const VectorCollection& instance(const NeoN::Database& db, std::string name);
307
314 template<class VectorType>
315 static VectorCollection& instance(VectorType& field)
316 {
318 field, "attempting to retrieve VectorCollection from unregistered field"
319 );
320 return instance(field.db(), field.fieldCollectionName);
321 }
322
329 template<class VectorType>
330 static const VectorCollection& instance(const VectorType& field)
331 {
333 field, "attempting to retrieve VectorCollection from unregistered field"
334 );
335 const Database& db = field.db();
336 const Collection& collection = db.at(field.fieldCollectionName);
337 return collection.as<VectorCollection>();
338 // return instance(field.db(), field.fieldCollectionName);
339 }
340
348 template<class VectorType>
349 VectorType& registerVector(CreateFunction createFunc)
350 {
351 VectorDocument doc = createFunc(db());
352 if (!validateVectorDoc(doc.doc()))
353 {
354 throw std::runtime_error {"Document is not valid"};
355 }
356
357 std::string key = insert(std::move(doc));
358 VectorDocument& fd = fieldDoc(key);
359 VectorType& field = fd.field<VectorType>();
360 field.key = key;
361 field.fieldCollectionName = name();
362
363 return field;
364 }
365};
366
367
381template<typename VectorType>
383{
384public:
385
386 std::string name;
387 const VectorType& field;
388 std::int64_t timeIndex = std::numeric_limits<std::int64_t>::max();
389 std::int64_t iterationIndex = std::numeric_limits<std::int64_t>::max();
390 std::int64_t subCycleIndex = std::numeric_limits<std::int64_t>::max();
391
393 {
395 field.mesh().exec(), field.internalVector(), field.boundaryData()
396 );
397
398 VectorType vf(
399 field.exec(),
400 name,
401 field.mesh(),
402 std::move(domainVector),
403 field.boundaryConditions(),
404 db,
405 "",
406 ""
407 );
408
409 if (field.registered())
410 {
411 const VectorCollection& fieldCollection = VectorCollection::instance(field);
412 const VectorDocument& fieldDoc = fieldCollection.fieldDoc(field.key);
413 if (timeIndex == std::numeric_limits<std::int64_t>::max())
414 {
415 timeIndex = fieldDoc.timeIndex();
416 }
417 if (iterationIndex == std::numeric_limits<std::int64_t>::max())
418 {
419 iterationIndex = fieldDoc.iterationIndex();
420 }
421 if (subCycleIndex == std::numeric_limits<std::int64_t>::max())
422 {
423 subCycleIndex = fieldDoc.subCycleIndex();
424 }
425 }
426 // The field is moved in: an initializer list would copy it twice.
427 Dictionary dict(
428 {{"name", vf.name},
429 {"timeIndex", timeIndex},
430 {"iterationIndex", iterationIndex},
431 {"subCycleIndex", subCycleIndex}}
432 );
433 dict.insert("field", std::any(std::move(vf)));
434 return NeoN::Document(std::move(dict), validateVectorDoc);
435 }
436};
437
438
439} // namespace NeoN
const std::string & name() const
CollectionMixin(NeoN::Database &db, std::string name)
const NeoN::Database & db() const
Document & doc(const std::string &id)
A type-erased interface collection types.
CollectionType & as()
Casts the collection to a specific collection type.
A class representing a dictionary that stores key-value pairs.
void insert(const std::string &key, const std::any &value)
Inserts a key-value pair into the dictionary.
A class representing a document in a database.
Definition document.hpp:52
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.
std::string insert(VectorDocument &&fd)
Inserts a field document into the collection.
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.
VectorDocument(Document &&doc)
Constructs a VectorDocument from the given Document, moving its entries.
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 Dictionary &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