NeoFOAM
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 NeoFOAM authors
3#pragma once
4
5#include <limits>
6#include <string>
7#include <functional>
8
14
16{
25bool validateFieldDoc(const Document& doc);
26
35{
36public:
37
47 template<class FieldType>
49 const FieldType& field,
50 std::int64_t timeIndex,
51 std::int64_t iterationIndex,
52 std::int64_t subCycleIndex
53 )
54 : doc_(
56 {{"name", field.name},
57 {"timeIndex", timeIndex},
58 {"iterationIndex", iterationIndex},
59 {"subCycleIndex", subCycleIndex},
60 {"field", field}}
61 ),
63 )
64 {}
65
72
79
85 const Document& doc() const;
86
87
93 std::string id() const;
94
100 static std::string typeName();
101
108 template<class FieldType>
109 FieldType& field()
110 {
111 return doc_.get<FieldType&>("field");
112 }
113
120 template<class FieldType>
121 const FieldType& field() const
122 {
123 return doc_.get<const FieldType&>("field");
124 }
125
131 std::string name() const;
132
138 std::string& name();
139
145 std::int64_t timeIndex() const;
146
152 std::int64_t& timeIndex();
153
159 std::int64_t iterationIndex() const;
160
166 std::int64_t& iterationIndex();
167
173 std::int64_t subCycleIndex() const;
174
180 std::int64_t& subCycleIndex();
181
182private:
183
184 Document doc_;
185};
186
197
205class FieldCollection : public CollectionMixin<FieldDocument>
206{
207public:
208
216
221
226
231
236
243 bool contains(const std::string& id) const;
244
251 std::string insert(const FieldDocument& fd);
252
259 FieldDocument& fieldDoc(const std::string& id);
260
267 const FieldDocument& fieldDoc(const std::string& id) const;
268
279
280
290 static const FieldCollection& instance(const NeoFOAM::Database& db, std::string name);
291
298 template<class FieldType>
299 static FieldCollection& instance(FieldType& field)
300 {
302 field, "attempting to retrieve FieldCollection from unregistered field"
303 );
304 return instance(field.db(), field.fieldCollectionName);
305 }
306
313 template<class FieldType>
314 static const FieldCollection& instance(const FieldType& field)
315 {
317 field, "attempting to retrieve FieldCollection from unregistered field"
318 );
319 const Database& db = field.db();
320 const Collection& collection = db.at(field.fieldCollectionName);
321 return collection.as<FieldCollection>();
322 // return instance(field.db(), field.fieldCollectionName);
323 }
324
332 template<class FieldType>
333 FieldType& registerField(CreateFunction createFunc)
334 {
335 FieldDocument doc = createFunc(db());
336 if (!validateFieldDoc(doc.doc()))
337 {
338 throw std::runtime_error("Document is not valid");
339 }
340
341 std::string key = insert(doc);
342 FieldDocument& fd = fieldDoc(key);
343 FieldType& field = fd.field<FieldType>();
344 field.key = key;
345 field.fieldCollectionName = name();
346 return field;
347 }
348};
349
350
364template<typename FieldType>
366{
367public:
368
369 std::string name;
370 const FieldType& field;
371 std::int64_t timeIndex = std::numeric_limits<std::int64_t>::max();
372 std::int64_t iterationIndex = std::numeric_limits<std::int64_t>::max();
373 std::int64_t subCycleIndex = std::numeric_limits<std::int64_t>::max();
374
376 {
377 FieldType vf(
378 field.exec(),
379 name,
380 field.mesh(),
381 field.internalField(),
382 field.boundaryConditions(),
383 db,
384 "",
385 ""
386 );
387
388 if (field.registered())
389 {
390 const FieldCollection& fieldCollection = FieldCollection::instance(field);
391 const FieldDocument& fieldDoc = fieldCollection.fieldDoc(field.key);
392 if (timeIndex == std::numeric_limits<std::int64_t>::max())
393 {
394 timeIndex = fieldDoc.timeIndex();
395 }
396 if (iterationIndex == std::numeric_limits<std::int64_t>::max())
397 {
398 iterationIndex = fieldDoc.iterationIndex();
399 }
400 if (subCycleIndex == std::numeric_limits<std::int64_t>::max())
401 {
402 subCycleIndex = fieldDoc.subCycleIndex();
403 }
404 }
405 return NeoFOAM::Document(
406 {{"name", vf.name},
407 {"timeIndex", timeIndex},
408 {"iterationIndex", iterationIndex},
409 {"subCycleIndex", subCycleIndex},
410 {"field", vf}},
412 );
413 }
414};
415
416
417} // namespace NeoFOAM
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.
Document & doc(const std::string &id)
Retrieves a document by its ID.
const NeoFOAM::Database & db() const
Gets a const reference to the database.
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
Creates a FieldDocument from an existing field.
A class representing a collection of field documents in a database.
bool contains(const std::string &id) const
Checks if the collection contains a field with the given ID.
FieldType & registerField(CreateFunction createFunc)
Registers a field in the collection.
static const FieldCollection & instance(const NeoFOAM::Database &db, std::string name)
Retrieves the instance of the FieldCollection with the given name (const version).
FieldCollection & operator=(FieldCollection &&)=delete
A FieldCollection is not move-assign-able, but move-construct-able.
static FieldCollection & instance(NeoFOAM::Database &db, std::string name)
Retrieves the instance of the FieldCollection with the given name.
static const FieldCollection & instance(const FieldType &field)
Retrieves the instance of the FieldCollection from a const registered FieldType.
static FieldCollection & instance(FieldType &field)
Retrieves the instance of the FieldCollection from a const registered FieldType.
const FieldDocument & fieldDoc(const std::string &id) const
Retrieves a field document by its ID (const version).
std::string insert(const FieldDocument &fd)
Inserts a field document into the collection.
FieldDocument & fieldDoc(const std::string &id)
Retrieves a field document by its ID.
FieldCollection(FieldCollection &&)=default
A FieldCollection is move constructable, but not copyable.
FieldCollection & operator=(const FieldCollection &)=delete
A FieldCollection is not copyable, but moveable.
FieldCollection(const FieldCollection &)=delete
A FieldCollection is not copyable, but moveable.
FieldCollection(NeoFOAM::Database &db, std::string name)
Constructs a FieldCollection with the given database and name.
A class representing a field document in a database.
FieldType & field()
Retrieves the field from the document.
FieldDocument(const FieldType &field, std::int64_t timeIndex, std::int64_t iterationIndex, std::int64_t subCycleIndex)
Constructs a FieldDocument with the given field and metadata.
const FieldType & field() const
Retrieves the field from the document (const version).
FieldDocument(const Document &doc)
Constructs a FieldDocument with the given Document.
std::string name() const
Retrieves the name of the field.
Document & doc()
Retrieves the underlying Document.
std::int64_t timeIndex() const
Retrieves the time index of the field.
std::string id() const
Retrieves the unique identifier of the field collection.
std::int64_t & timeIndex()
Retrieves the time index of the field.
const Document & doc() const
Retrieves the underlying Document (const version).
static std::string typeName()
Retrieves the type name of the field.
std::int64_t subCycleIndex() const
Retrieves the sub-cycle index of the field.
std::int64_t & iterationIndex()
Retrieves the iteration index of the field.
std::int64_t & subCycleIndex()
Retrieves the sub-cycle index of the field.
std::int64_t iterationIndex() const
Retrieves the iteration index of the field.
std::string & name()
Retrieves the time index of the field.
bool validateFieldDoc(const Document &doc)
Validates a FieldDocument.
std::function< FieldDocument(NeoFOAM::Database &db)> CreateFunction
A function type for creating a FieldDocument.
void validateRegistration(const Type &obj, const std::string errorMessage)
Validates that a field is registered in the database.
Definition database.hpp:119