Advanced Multi-Physics (AMP)
On-Line Documentation
Database.h
Go to the documentation of this file.
1#ifndef included_AMP_Database
2#define included_AMP_Database
3
4#include "AMP/AMP_TPLs.h"
5#include "AMP/utils/Array.h"
6#include "AMP/utils/TypeTraits.h"
7#include "AMP/utils/Units.h"
8#include "AMP/utils/typeid.h"
9
10#include "StackTrace/source_location.h"
11
12#include <cstddef>
13#include <iostream>
14#include <memory>
15#include <string>
16#include <string_view>
17#include <vector>
18
19
20// Forward declare SAMRAI's database
21namespace SAMRAI::tbox {
22class Database;
23class DatabaseBox;
24} // namespace SAMRAI::tbox
25
26
27// AMP namespace
28namespace AMP {
29
30
31// Forward declares
32class MathExpr;
33
34
37{
38public:
39 using source_location = StackTrace::source_location;
41 virtual ~KeyData() {}
43 virtual typeID getClassType() const = 0;
45 virtual std::unique_ptr<KeyData> clone() const = 0;
47 virtual void print( std::ostream &os,
48 std::string_view indent = "",
49 bool sort = true,
50 bool printType = false ) const = 0;
52 virtual bool is_floating_point() const = 0;
54 virtual bool is_integral() const = 0;
55 // Check if the entry can be stored as the given type
56 template<class TYPE>
57 bool isType() const;
58 // Get the fundamental type (e.g. double, int, float, ...)
59 virtual typeID getDataType() const = 0;
61 virtual ArraySize arraySize() const = 0;
63 virtual Array<double> convertToDouble() const = 0;
65 virtual Array<int64_t> convertToInt64() const = 0;
67 virtual bool operator==( const KeyData &rhs ) const = 0;
69 inline bool operator!=( const KeyData &rhs ) const { return !operator==( rhs ); }
71 const Units &unit() const { return d_unit; }
73 double convertUnits( const Units &, std::string_view = "" ) const;
75 virtual size_t packSize() const = 0;
77 virtual size_t pack( std::byte * ) const = 0;
79 virtual size_t unpack( const std::byte * ) = 0;
81 virtual void writeHDF5( int64_t fid, const std::string &name ) const = 0;
83 virtual void readHDF5( int64_t fid, const std::string &name ) = 0;
85 template<class TYPE>
86 TYPE getScalar( const Units &unit = {},
87 std::string_view name = "",
88 source_location src = source_location::current() ) const;
90 template<class TYPE>
92 std::string_view name = "",
93 source_location src = source_location::current() ) const;
94
95protected:
97 KeyData( const Units &unit ) : d_unit( unit ) {}
98 KeyData( KeyData && ) = delete;
99 KeyData( const KeyData & ) = delete;
100
101 KeyData &operator=( KeyData && ) = delete;
102 KeyData &operator=( const KeyData & ) = delete;
103
104protected:
106};
107
108
110class Database final : public KeyData
111{
112public:
114 enum class Check : uint8_t {
115 Overwrite,
116 Keep,
118 WarnKeep,
119 Error,
121 };
122
123 template<typename T>
125 typedef T type;
126 };
127 template<typename T>
129
130public:
133
135 explicit Database( std::string name );
136
141 static std::shared_ptr<Database> parseInputFile( const std::string &filename );
142
143 // Read a YAML database
144 static std::unique_ptr<KeyData> readYAML( std::string_view filename,
145 source_location src = source_location::current() );
146
154 template<class... Args>
155 static std::unique_ptr<Database> create( Args... args );
156
164 template<class... Args>
165 static std::unique_ptr<Database> createWithUnits( Args... args );
166
171 static std::unique_ptr<Database> createFromString( std::string_view data );
172
174 Database( const Database & );
175
178
181
184
186 virtual ~Database() = default;
187
193 void readDatabase( const std::string &filename,
194 source_location src = source_location::current() );
195
197 typeID getClassType() const override { return getTypeID<Database>(); }
198
200 inline Check getDefaultAddKeyBehavior() const { return d_check; }
201
203
209 void setDefaultAddKeyBehavior( Check check, bool setChildren );
210
212 std::unique_ptr<KeyData> clone() const override;
213
215 void copy( const Database &rhs );
216
218 std::unique_ptr<Database> cloneDatabase() const;
219
221 inline const std::string &getName() const { return d_name; }
222
224 inline void setName( std::string name ) { d_name = std::move( name ); }
225
231 bool keyExists( std::string_view key ) const;
232
233
238 void deleteData( std::string_view key );
239
240
247 std::vector<std::string> getAllKeys( bool sort = true ) const;
248
249
251 inline size_t empty() const { return d_data.size() == 0; }
252
253
255 inline size_t size() const { return d_data.size(); }
256
257
259 bool operator==( const Database &rhs ) const;
260
261
263 bool operator==( const KeyData &rhs ) const override;
264
265
267 inline bool operator!=( const Database &rhs ) const { return !operator==( rhs ); }
268
269
275 inline std::string getString( std::string_view key,
276 source_location src = source_location::current() ) const
277 {
278 return getScalar<std::string>( key, {}, src );
279 }
280
281
292 template<class TYPE>
293 TYPE getScalar( std::string_view key,
294 const Units &unit = Units(),
295 source_location src = source_location::current() ) const;
296
297
308 template<class TYPE>
309 TYPE getWithDefault( std::string_view key,
311 const Units &unit = Units(),
312 source_location src = source_location::current() ) const;
313
314
325 template<class TYPE>
326 Array<TYPE> getArray( std::string_view key,
327 const Units &unit = Units(),
328 source_location src = source_location::current() ) const;
329
330
341 template<class TYPE>
342 std::vector<TYPE> getVector( std::string_view key,
343 const Units &unit = Units(),
344 source_location src = source_location::current() ) const;
345
346
355 template<class TYPE>
356 void putScalar( std::string_view key,
357 TYPE value,
358 Units unit = Units(),
360 source_location src = source_location::current() );
361
362
375 template<class TYPE>
376 void putArray( std::string_view key,
377 Array<TYPE> data,
378 Units unit = Units(),
380 source_location src = source_location::current() );
381
382
395 template<class TYPE>
396 void putVector( std::string_view key,
397 const std::vector<TYPE> &data,
398 Units unit = Units(),
400 source_location src = source_location::current() );
401
402
409 KeyData *getData( std::string_view key );
410
417 const KeyData *getData( std::string_view key ) const;
418
419
428 void putData( std::string_view key,
429 std::unique_ptr<KeyData> data,
431 source_location src = source_location::current() );
432
433
435 bool isDatabase( std::string_view key, source_location src = source_location::current() ) const;
436
437
439 bool isString( std::string_view key, source_location src = source_location::current() ) const;
440
448 bool isEquation( std::string_view key, source_location src = source_location::current() ) const;
449
450
459 std::unique_ptr<MathExpr> getEquation( std::string_view key,
460 const Units &unit = Units(),
461 source_location src = source_location::current() ) const;
462
463
465 template<class TYPE>
466 bool isType( std::string_view key, source_location src = source_location::current() ) const;
467
469 typeID getDataType( std::string_view key ) const;
470
478 std::shared_ptr<Database> getDatabase( std::string_view key,
479 source_location src = source_location::current() );
480
488 std::shared_ptr<const Database>
489 getDatabase( std::string_view key, source_location src = source_location::current() ) const;
490
491
498 const Database &operator()( std::string_view key,
499 source_location src = source_location::current() ) const;
500
501
509 inline void putDatabase( std::string_view key, std::unique_ptr<Database> db )
510 {
511 putData( key, std::move( db ) );
512 }
513
514
527 inline std::shared_ptr<Database> createAddDatabase( std::string_view key )
528 {
529 putDatabase( key, std::make_unique<Database>( std::string( key ) ) );
530 return getDatabase( key );
531 }
532
533
540 inline std::shared_ptr<Database> putDatabase( std::string_view key )
541 {
542 putData( key, std::make_unique<Database>( std::string( key.data(), key.size() ) ) );
543 return getDatabase( key );
544 }
545
546
554 void erase( std::string_view key, bool check = true );
555
556
560 void clear();
561
562
570 void print( std::ostream &os,
571 std::string_view indent = "",
572 bool sort = true,
573 bool printType = false ) const override;
574
575
577 typeID getDataType() const override { return AMP::getTypeID<Database>(); }
578
579
587 std::string
588 print( std::string_view indent = "", bool sort = true, bool printType = false ) const;
589
590
596 std::vector<std::string> getUnused( bool recursive = true ) const;
597
598
599public: // Pack/unpack data
600 size_t packSize() const override;
601 size_t pack( std::byte * ) const override;
602 size_t unpack( const std::byte * ) override;
603 void writeHDF5( int64_t fid, const std::string &name ) const override;
604 void readHDF5( int64_t fid, const std::string &name ) override;
605
606#ifdef AMP_USE_SAMRAI
607public: // SAMRAI interfaces
609 Database( SAMRAI::tbox::Database & );
610
612 std::shared_ptr<SAMRAI::tbox::Database> cloneToSAMRAI() const;
613
615 static std::shared_ptr<Database> readThroughSAMRAI( const std::string &filename );
616
617#endif
618
619
620protected: // Internal data and functions
622 std::string d_name;
623 std::vector<uint32_t> d_hash;
624 std::vector<std::string> d_keys;
625 std::vector<std::shared_ptr<KeyData>> d_data;
626 mutable std::vector<bool> d_used;
627
628 // Function to add arguments to the database
629 template<class TYPE, class... Args>
630 void addArgs( std::string_view key, TYPE value, Args... args );
631
632 // Function to add arguments to the database
633 template<class TYPE, class... Args>
634 void addArgsWithUnits( std::string_view key, TYPE value, const Units &unit, Args... args );
635
636 // Hash a string
637 static inline uint32_t hashString( std::string_view s )
638 {
639 return std::hash<std::string_view>{}( s );
640 }
641
642 // Find an entry
643 inline int find( uint32_t hash, bool use ) const
644 {
645 for ( size_t i = 0; i < d_hash.size(); i++ ) {
646 if ( hash == d_hash[i] ) {
647 if ( use && !d_used[i] )
648 d_used[i] = true;
649 return i;
650 }
651 }
652 return -1;
653 }
654
655 // Functions inherited from KeyData that really aren't valid
658 ArraySize arraySize() const override { return ArraySize( 1 ); }
659 bool is_floating_point() const override;
660 bool is_integral() const override;
661};
662
663
665class DatabaseBox final
666{
667public:
670
672 DatabaseBox( int dim, const int *lower, const int *upper );
673
675 explicit DatabaseBox( std::string_view str );
676
677#ifdef AMP_USE_SAMRAI
679 DatabaseBox( const SAMRAI::tbox::DatabaseBox & );
680
682 SAMRAI::tbox::DatabaseBox cloneToSAMRAI() const;
683#endif
684
686 uint8_t &dim();
687
689 uint8_t dim() const;
690
694 bool empty() const;
695
697 int &lower( uint8_t i );
698
700 int lower( uint8_t i ) const;
701
703 int &upper( uint8_t i );
704
706 int upper( uint8_t i ) const;
707
708 bool operator==( const DatabaseBox &box ) const;
709
710private:
711 uint8_t d_dim;
712 std::array<int, 5> d_lower, d_upper;
713};
714
715
716/********************************************************************
717 * Register KeyData with the factory *
718 ********************************************************************/
719void registerKeyData( const std::string &name, std::function<std::unique_ptr<KeyData>()> fun );
720
721
722/********************************************************************
723 * Overload ostream operator *
724 ********************************************************************/
725std::ostream &operator<<( std::ostream &out, const DatabaseBox & );
726
727
728/********************************************************************
729 * Create database from arguments *
730 ********************************************************************/
731template<class TYPE, class... Args>
732inline void Database::addArgs( std::string_view key, TYPE value, Args... args )
733{
734 if constexpr ( is_vector_v<TYPE> ) {
735 putVector( key, value );
736 } else if constexpr ( is_Array_v<TYPE> ) {
737 putArray( key, value );
738 } else if constexpr ( std::is_same_v<TYPE, std::string> ||
739 std::is_same_v<TYPE, std::string_view> ) {
740 putScalar( key, value );
741 } else if constexpr ( has_size_v<TYPE> || is_initializer_list_v<TYPE> ) {
742 typedef decltype( *value.begin() ) TYPE2;
743 typedef typename AMP::remove_cvref_t<TYPE2> TYPE3;
744 std::vector<TYPE3> data( value.begin(), value.end() );
745 putVector( key, std::move( data ) );
746 } else {
747 putScalar( key, value );
748 }
749 if constexpr ( sizeof...( args ) > 0 )
750 addArgs( args... );
751}
752template<class TYPE, class... Args>
753inline void
754Database::addArgsWithUnits( std::string_view key, TYPE value, const Units &unit, Args... args )
755{
756 if constexpr ( is_vector_v<TYPE> ) {
757 putVector( key, value, unit );
758 } else if constexpr ( is_Array_v<TYPE> ) {
759 putArray( key, value );
760 } else if constexpr ( std::is_same_v<TYPE, std::string> ||
761 std::is_same_v<TYPE, std::string_view> ) {
762 putScalar( key, value, unit );
763 } else if constexpr ( has_size_v<TYPE> || is_initializer_list_v<TYPE> ) {
764 typedef decltype( *value.begin() ) TYPE2;
765 typedef typename AMP::remove_cvref_t<TYPE2> TYPE3;
766 std::vector<TYPE3> data( value.begin(), value.end() );
767 putVector( key, std::move( data ), unit );
768 } else {
769 putScalar( key, value, unit );
770 }
771 if constexpr ( sizeof...( args ) > 0 )
772 addArgsWithUnits( args... );
773}
774template<class... Args>
775inline std::unique_ptr<Database> Database::create( Args... args )
776{
777 constexpr size_t n = sizeof...( args );
778 static_assert( n % 2 == 0 );
779 auto db = std::make_unique<Database>();
780 if ( n == 0 )
781 return db;
782 db->addArgs( args... );
783 return db;
784}
785template<class... Args>
786inline std::unique_ptr<Database> Database::createWithUnits( Args... args )
787{
788 constexpr size_t n = sizeof...( args );
789 static_assert( n % 3 == 0 );
790 auto db = std::make_unique<Database>();
791 if ( n == 0 )
792 return db;
793 db->addArgsWithUnits( args... );
794 return db;
795}
796
797
798} // namespace AMP
799
800
801#endif
Simple class to store the array dimensions.
Definition ArraySize.h:138
Class to store a box.
Definition Database.h:666
std::array< int, 5 > d_lower
Definition Database.h:712
DatabaseBox(int dim, const int *lower, const int *upper)
Default constructor.
int & lower(uint8_t i)
Return non-const reference to the specified component of the lower index.
int lower(uint8_t i) const
Return the specified component of the lower index.
std::array< int, 5 > d_upper
Definition Database.h:712
bool empty() const
int & upper(uint8_t i)
Return non-const reference to the specified component of the upper index.
uint8_t dim() const
Return the number of dimensions.
uint8_t & dim()
Return a non-const reference to the number of dimensions.
DatabaseBox()
Empty constructor.
bool operator==(const DatabaseBox &box) const
DatabaseBox(std::string_view str)
Construct from a string of the format "[(0,0,0), (7,7,7)]".
int upper(uint8_t i) const
Return the specified component of the upper index.
Class to a database.
Definition Database.h:111
void copy(const Database &rhs)
Copy the data.
bool isEquation(std::string_view key, source_location src=source_location::current()) const
Database(std::string name)
Basic constructor.
bool is_integral() const override
Return true if the type is a integer point type.
std::vector< TYPE > getVector(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
bool operator==(const KeyData &rhs) const override
Return true if the databases are equivalent.
void deleteData(std::string_view key)
std::string getString(std::string_view key, source_location src=source_location::current()) const
Definition Database.h:275
size_t packSize() const override
Return the number of bytes required to pack the data.
TYPE getWithDefault(std::string_view key, IdentityType< const TYPE & > value, const Units &unit=Units(), source_location src=source_location::current()) const
Check getDefaultAddKeyBehavior() const
Get the default behavior when adding keys.
Definition Database.h:200
std::shared_ptr< const Database > getDatabase(std::string_view key, source_location src=source_location::current()) const
static std::unique_ptr< KeyData > readYAML(std::string_view filename, source_location src=source_location::current())
void readDatabase(const std::string &filename, source_location src=source_location::current())
bool operator==(const Database &rhs) const
Return true if the databases are equivalent.
void putScalar(std::string_view key, TYPE value, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
typeID getDataType(std::string_view key) const
Get the fundamental type (e.g. double, int, float, ...)
Database(const Database &)
Copy constructor.
const std::string & getName() const
Get the name of the database.
Definition Database.h:221
size_t unpack(const std::byte *) override
Unpack the data from a buffer.
void putData(std::string_view key, std::unique_ptr< KeyData > data, Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
bool keyExists(std::string_view key) const
Database & operator=(const Database &)
Assignment operator.
std::unique_ptr< Database > cloneDatabase() const
Copy the data.
void erase(std::string_view key, bool check=true)
static std::unique_ptr< Database > create(Args... args)
Create a database from key/value pairs.
Definition Database.h:775
Array< double > convertToDouble() const override
Return the data as a Array<double> (throw error if this is not valid)
std::vector< std::shared_ptr< KeyData > > d_data
Definition Database.h:625
std::shared_ptr< Database > putDatabase(std::string_view key)
Definition Database.h:540
typeID getDataType() const override
Print the type.
Definition Database.h:577
void setDefaultAddKeyBehavior(Check check, bool setChildren)
Set the default behavior when adding keys.
std::vector< std::string > getAllKeys(bool sort=true) const
Return all keys in the database.
void readHDF5(int64_t fid, const std::string &name) override
Read the data from HDF5.
std::unique_ptr< MathExpr > getEquation(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
Array< TYPE > getArray(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
Check d_check
Definition Database.h:621
Database & operator=(Database &&rhs)
Move assignment operator.
bool isString(std::string_view key, source_location src=source_location::current()) const
Check if the named entry is a string.
void putVector(std::string_view key, const std::vector< TYPE > &data, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
Array< int64_t > convertToInt64() const override
Return the data as a Array<int64_t> (throw error if this is not valid)
std::vector< uint32_t > d_hash
Definition Database.h:623
bool operator!=(const Database &rhs) const
Return the number of entries in the database.
Definition Database.h:267
typeID getClassType() const override
Return class type.
Definition Database.h:197
size_t empty() const
Return true if the database is empty.
Definition Database.h:251
void setName(std::string name)
Get the name of the database.
Definition Database.h:224
static std::shared_ptr< Database > parseInputFile(const std::string &filename)
size_t pack(std::byte *) const override
Pack the data to a buffer.
void print(std::ostream &os, std::string_view indent="", bool sort=true, bool printType=false) const override
virtual ~Database()=default
Destructor.
std::string print(std::string_view indent="", bool sort=true, bool printType=false) const
typename IdentityTypeStruct< const T & >::type IdentityType
Definition Database.h:128
std::string d_name
Definition Database.h:622
Database()
Empty constructor.
void putArray(std::string_view key, Array< TYPE > data, Units unit=Units(), Check check=Check::GetDatabaseDefault, source_location src=source_location::current())
std::vector< std::string > d_keys
Definition Database.h:624
void putDatabase(std::string_view key, std::unique_ptr< Database > db)
Definition Database.h:509
std::vector< std::string > getUnused(bool recursive=true) const
bool isType(std::string_view key, source_location src=source_location::current()) const
Check if the entry can be stored as the given type.
int find(uint32_t hash, bool use) const
Definition Database.h:643
KeyData * getData(std::string_view key)
void addArgsWithUnits(std::string_view key, TYPE value, const Units &unit, Args... args)
Definition Database.h:754
void addArgs(std::string_view key, TYPE value, Args... args)
Definition Database.h:732
static uint32_t hashString(std::string_view s)
Definition Database.h:637
bool isDatabase(std::string_view key, source_location src=source_location::current()) const
Check if the key is a database object.
Check
enum to control behavior when trying to add existing keys
Definition Database.h:114
@ Keep
Keep the existing data.
@ WarnKeep
Keep the existing data but print a warning.
@ Error
Throw an error.
@ Overwrite
Overwrite the data.
@ WarnOverwrite
Overwrite the data but print a warning (default)
void writeHDF5(int64_t fid, const std::string &name) const override
Write the data to HDF5.
static std::unique_ptr< Database > createFromString(std::string_view data)
std::unique_ptr< KeyData > clone() const override
Copy the data.
bool is_floating_point() const override
Return true if the type is a floating point type.
Database(Database &&rhs)
Move constructor.
std::shared_ptr< Database > createAddDatabase(std::string_view key)
Definition Database.h:527
ArraySize arraySize() const override
Return the array size.
Definition Database.h:658
static std::unique_ptr< Database > createWithUnits(Args... args)
Create a database from key/value/unit triplets.
Definition Database.h:786
TYPE getScalar(std::string_view key, const Units &unit=Units(), source_location src=source_location::current()) const
const Database & operator()(std::string_view key, source_location src=source_location::current()) const
size_t size() const
Return the number of entries in the database.
Definition Database.h:255
std::shared_ptr< Database > getDatabase(std::string_view key, source_location src=source_location::current())
const KeyData * getData(std::string_view key) const
std::vector< bool > d_used
Definition Database.h:626
Base class to hold data of a given type.
Definition Database.h:37
KeyData(const KeyData &)=delete
KeyData & operator=(const KeyData &)=delete
virtual size_t unpack(const std::byte *)=0
Unpack the data from a buffer.
virtual bool is_floating_point() const =0
Return true if the type is a floating point type.
virtual void readHDF5(int64_t fid, const std::string &name)=0
Read the data from HDF5.
virtual Array< int64_t > convertToInt64() const =0
Return the data as a Array<int64_t> (throw error if this is not valid)
virtual size_t packSize() const =0
Return the number of bytes required to pack the data.
TYPE getScalar(const Units &unit={}, std::string_view name="", source_location src=source_location::current()) const
Convert the data to a scalar of the given type.
double convertUnits(const Units &, std::string_view="") const
Return the conversion factor (if used)
virtual typeID getDataType() const =0
Units d_unit
Definition Database.h:105
bool isType() const
Array< TYPE > getArray(const Units &unit={}, std::string_view name="", source_location src=source_location::current()) const
Convert the data to a scalar of the given type.
virtual ~KeyData()
Destructor.
Definition Database.h:41
KeyData(KeyData &&)=delete
virtual void writeHDF5(int64_t fid, const std::string &name) const =0
Write the data to HDF5.
KeyData & operator=(KeyData &&)=delete
virtual bool is_integral() const =0
Return true if the type is a integer point type.
virtual std::unique_ptr< KeyData > clone() const =0
Copy the data.
StackTrace::source_location source_location
Definition Database.h:39
const Units & unit() const
Return the units.
Definition Database.h:71
virtual ArraySize arraySize() const =0
Return the array size.
KeyData(const Units &unit)
Definition Database.h:97
virtual Array< double > convertToDouble() const =0
Return the data as a Array<double> (throw error if this is not valid)
virtual typeID getClassType() const =0
Return class type.
virtual bool operator==(const KeyData &rhs) const =0
Check if two sets of data are equal.
bool operator!=(const KeyData &rhs) const
Check if two sets of data are not equal.
Definition Database.h:69
virtual size_t pack(std::byte *) const =0
Pack the data to a buffer.
virtual void print(std::ostream &os, std::string_view indent="", bool sort=true, bool printType=false) const =0
Print the data to a stream.
Provides a class for storing units.
Definition Units.h:101
std::ostream & operator<<(std::ostream &out, const AMP::ArraySize &s)
Definition Array.h:861
void registerKeyData(const std::string &name, std::function< std::unique_ptr< KeyData >()> fun)
typename std::remove_cv_t< typename std::remove_reference_t< T > > remove_cvref_t
Definition TypeTraits.h:108
Class to store type info.
Definition typeid.h:210



Advanced Multi-Physics (AMP)
Oak Ridge National Laboratory
Idaho National Laboratory
Los Alamos National Laboratory
This page automatically produced from the
source code by doxygen
Last updated: Tue Mar 10 2026 13:06:41.
Comments on this page