# PhasicFlow: src/phasicFlow/dictionary/dictionary.hpp Source File
PhasicFlow is an open-source parallel DEM (discrete element method) package for simulating granular flow. It is developed in C++ and can be exectued on both GPU (like CUDA) and CPU.

dictionary.hpp
Go to the documentation of this file.
/*------------------------------- phasicFlow ---------------------------------
O C enter of
O O E ngineering and
O O M ultiscale modeling of
OOOOOOO F luid flow
------------------------------------------------------------------------------
Copyright (C): www.cemf.ir
email: hamid.r.norouzi AT gmail.com
------------------------------------------------------------------------------
Licence:
This file is part of phasicFlow code. It is a free software for simulating
granular and multiphase flows. You can redistribute it and/or modify it under
the terms of GNU General Public License v3 or any other later versions.
phasicFlow is distributed to help others in their research in the field of
granular and multiphase flows, but WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
-----------------------------------------------------------------------------*/
// based on OpenFOAM dictionary, with some modifications/simplifications
// to be tailored to our needs
#ifndef __dictionary_hpp__
#define __dictionary_hpp__
#include "types.hpp"
#include "iEntry.hpp"
#include "dataEntry.hpp"
#include "MapPtr.hpp"
#include "List.hpp"
#include "fileSystem.hpp"
namespace pFlow
{
class dictionary
:
public iEntry
{
protected:
word name_;
wordOrderedMapPtr<iEntry> entries_;
List<iEntry*> orderedEntries_;
const dictionary& parDict_;
bool isGlobal_ = false;
iEntry* findEntry(const word& keyword);
iEntry* findEntry(const word& keyword)const;
template<typename T>
bool readDataEntry( const word& keyword, T& val)const;
bool readDictionary(iIstream & is);
bool writeDictionary(iOstream& os, bool withBlock = true)const;
public:
// null dictionary object, to be used for references
static dictionary nullDict;
TypeInfo("dictionary");
dictionary(const word& keyword, bool global);
dictionary(const word& keyword, const fileSystem& file);
dictionary();
dictionary(const word& keyword);
dictionary(const word& keyword, const dictionary& parDict);
dictionary( const word& keyword, const dictionary& parDict, iIstream& is);
dictionary(const word& keyword, const dictionary& parDict, const dictionary& dict);
dictionary(const dictionary& );
dictionary(const dictionary& src, bool global);
dictionary& operator=(const dictionary& rhs);
virtual dictionary* dictPtr();
virtual const dictionary* dictPtr() const;
virtual bool isDictionary() const;
virtual word globalName()const;
virtual const dictionary& parrentDict() const;
virtual dictionary& dict();
virtual const dictionary& dict() const;
virtual bool isFileDict()const;
bool addPtr(const word& keyword, uniquePtr<iEntry>& etry, bool warning = true );
bool add(const word& keyword, const float& v);
bool add(const word& keyword, const double& v);
bool add(const word& keyword, const word& v);
bool add(const word& keyword, const int64& v);
bool add(const word& keyword, const int32& v);
bool add(const word& keyword, const int8& v);
bool add(const word& keyword, const uint64& v);
bool add(const word& keyword, const uint32& v);
bool add(const word& keyword, const uint8& v);
bool addDict(const word& keyword, const dictionary& dict);
template<typename T>
bool add(const word& keyword, const T& v );
template<typename T>
bool addOrKeep(const word& keyword, const T& v);
template<typename T>
bool addOrReplace(const word& keyword, const T& v);
void clear();
dictionary* subDictPtr(const word& keyword);
dictionary& subDict(const word& keyword);
const dictionary& subDict(const word& keyword) const;
dataEntry* dataEntryPtr(const word& keyword);
dataEntry& dataEntryRef(const word& keyword);
const dataEntry& dataEntryRef(const word& keyword)const;
dictionary& subDictOrCreate(const word& keyword);
template<typename T>
T getVal(const word& keyword) const;
template<typename T>
T getValMax(const word& keyword, const T& maxVal)const;
template<typename T>
T getValMin(const word& keyword, const T& minVal)const;
template<typename T>
T getValOrSet(const word& keyword, const T& setVal)const;
template<typename T>
T getValOrSetMax(const word& keyword, const T& setMaxVal)const;
template<typename T>
T getValOrSetMin(const word& keyword, const T& setMinVal)const;
size_t numEntries()const;
size_t numDataEntries()const;
size_t numDictionaries()const;
wordList allKeywords()const;
wordList dataEntryKeywords()const;
wordList dictionaryKeywords()const;
bool containsDictionay(const word& name)const;
bool containsDataEntry(const word& name)const;
virtual uniquePtr<iEntry> clone() const;
virtual iEntry* clonePtr() const;
virtual uniquePtr<iEntry> clone(const dictionary& parDict)const;
virtual iEntry* clonePtr(const dictionary& parDict) const;
bool read(iIstream& is) override;
bool write(iOstream& os) const override;
};
template<typename T>
bool dictionary::add(const word& keyword, const T& v )
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
return addPtr(keyword, ptr);
}
template<typename T>
bool dictionary::addOrKeep(const word& keyword, const T& v )
{
if(!containsDataEntry(keyword))
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
return addPtr(keyword, ptr);
}
return true;
}
template<typename T>
bool dictionary::readDataEntry
(
const word& keyword,
T& val
)const
{
if( auto entry = findEntry(keyword); entry!= nullptr && !entry->isDictionary() )
{
iTstream& is = dynamic_cast< dataEntry&>(*entry).stream();
is >> val;
return true;
}
else
{
return false;
}
}
template<typename T>
T dictionary::getVal
(
const word& keyword
)const
{
T val{};
if(!readDataEntry(keyword, val))
{
fatalErrorInFunction <<
"cannot find dataEntry "<< keyword <<" in dictionary "<< globalName()<<endl;
fatalExit;
}
return val;
}
template<typename T>
T dictionary::getValMax
(
const word& keyword,
const T& maxVal
)const
{
return max(getVal<T>(keyword), maxVal);
}
template<typename T>
T dictionary::getValMin
(
const word& keyword,
const T& minVal
)const
{
return min(getVal<T>(keyword), minVal);
}
template<typename T>
T dictionary::getValOrSet
(
const word& keyword,
const T& setVal
)const
{
T val{};
if( readDataEntry(keyword, val) )
{
return val;
}
else
{
return setVal;
}
}
template<typename T>
T dictionary::getValOrSetMax(const word& keyword, const T& setMaxVal)const
{
return max(getValOrSet(keyword, setMaxVal), setMaxVal);
}
template<typename T>
T dictionary::getValOrSetMin(const word& keyword, const T& setMinVal)const
{
return min(getValOrSet(keyword, setMinVal), setMinVal);
}
template <typename T>
inline bool dictionary::addOrReplace
(
const word &keyword,
const T &v
)
{
uniquePtr<iEntry> ptr = makeUnique<dataEntry>(keyword, *this ,v);
return addPtr(keyword, ptr, false);
}
} // pFlow
#endif // __dictionary_hpp__
pFlow::dictionary::orderedEntries_
List< iEntry * > orderedEntries_
entries in order of insertion
Definition: dictionary.hpp:82
pFlow::List
Definition: List.hpp:39
pFlow::dictionary::getValOrSet
T getValOrSet(const word &keyword, const T &setVal) const
get the value of data entry or if not found, set the value to setVal
Definition: dictionary.hpp:415
fatalExit
#define fatalExit
Fatal exit.
Definition: error.hpp:98
pFlow::dictionary::name_
word name_
global name of dictionary, separated with dots
Definition: dictionary.hpp:76
pFlow::dataEntry::stream
virtual iTstream & stream()
access to token stream
Definition: dataEntry.cpp:265
pFlow::dictionary::read
bool read(iIstream &is) override
read from stream
Definition: dictionary.cpp:772
types.hpp
pFlow::dictionary::allKeywords
wordList allKeywords() const
return all keywords (non-nullptr) in this dictionary
Definition: dictionary.cpp:708
pFlow::dictionary::findEntry
iEntry * findEntry(const word &keyword)
find an entry based on keyword return nullptr if not found
Definition: dictionary.cpp:106
List.hpp
pFlow::dictionary::parDict_
const dictionary & parDict_
ref to parrent dictionary
Definition: dictionary.hpp:85
pFlow::uint32
unsigned int uint32
Definition: builtinTypes.hpp:56
pFlow::dictionary::isGlobal_
bool isGlobal_
Definition: dictionary.hpp:87
pFlow::dictionary::operator=
dictionary & operator=(const dictionary &rhs)
assignment preserve name of this dictionary only entries are transfered with ownership
Definition: dictionary.cpp:310
pFlow::word
std::string word
Definition: builtinTypes.hpp:64
pFlow::max
T max(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:79
pFlow::dictionary::addOrKeep
bool addOrKeep(const word &keyword, const T &v)
Definition: dictionary.hpp:343
pFlow::int64
long long int int64
Definition: builtinTypes.hpp:52
pFlow::dictionary::globalName
virtual word globalName() const
global name of entry, separated with dots
Definition: dictionary.cpp:356
pFlow::dictionary::subDictOrCreate
dictionary & subDictOrCreate(const word &keyword)
search for a sub-dict with keyword create a new sub-dict if not found and return a ref to it fatalExi...
Definition: dictionary.cpp:647
pFlow::dictionary::add
bool add(const word &keyword, const float &v)
add a float dataEntry
Definition: dictionary.cpp:435
pFlow::iEntry::keyword
virtual const word & keyword() const
return keyword
Definition: iEntry.hpp:88
pFlow::dictionary::isDictionary
virtual bool isDictionary() const
if this is a dictionary
Definition: dictionary.cpp:351
pFlow::endl
iOstream & endl(iOstream &os)
Add newline and flush stream.
Definition: iOstream.hpp:341
pFlow::dictionary::nullDict
static dictionary nullDict
Definition: dictionary.hpp:116
fileSystem.hpp
pFlow::dictionary::clonePtr
virtual iEntry * clonePtr() const
clone the object
Definition: dictionary.cpp:810
pFlow
Definition: demGeometry.hpp:27
pFlow::dictionary::containsDataEntry
bool containsDataEntry(const word &name) const
check if a data entry exist
Definition: dictionary.cpp:761
pFlow::dictionary::dictionaryKeywords
wordList dictionaryKeywords() const
return a list of all dictionary (non-null) keywords
Definition: dictionary.cpp:734
pFlow::dictionary::dictPtr
virtual dictionary * dictPtr()
pointer to this dictionary
Definition: dictionary.cpp:339
pFlow::dictionary::entries_
wordOrderedMapPtr< iEntry > entries_
all the entries (data and dictionary) of the current dictionary
Definition: dictionary.hpp:79
pFlow::dictionary::TypeInfo
TypeInfo("dictionary")
pFlow::fileSystem
Manages file pathes, manupulate and combines them.
Definition: fileSystem.hpp:71
pFlow::dictionary::containsDictionay
bool containsDictionay(const word &name) const
check if a sub-dictionary exists
Definition: dictionary.cpp:749
pFlow::dictionary::getValMin
T getValMin(const word &keyword, const T &minVal) const
get the value of data entry and return min(value, minVal)
Definition: dictionary.hpp:405
pFlow::MapPtr
Definition: MapPtr.hpp:39
pFlow::dataEntry
Data entry to be used in dictionries.
Definition: dataEntry.hpp:48
pFlow::dictionary::getValMax
T getValMax(const word &keyword, const T &maxVal) const
get the value of data entry and return max(value, maxVal)
Definition: dictionary.hpp:395
pFlow::dictionary::numEntries
size_t numEntries() const
return number of entris in this dictionary
Definition: dictionary.cpp:677
pFlow::iIstream
Interface class for any input stream
Definition: iIstream.hpp:37
pFlow::dictionary::clone
virtual uniquePtr< iEntry > clone() const
clone polymorphic object (here dictionary)
Definition: dictionary.cpp:804
pFlow::dictionary::dataEntryPtr
dataEntry * dataEntryPtr(const word &keyword)
pointer to a dataEntry fatalExit if not found/not a dataEntry
Definition: dictionary.cpp:598
fatalErrorInFunction
#define fatalErrorInFunction
Report a fatal error and function name and exit the application.
Definition: error.hpp:77
pFlow::int32
int int32
Definition: builtinTypes.hpp:50
pFlow::dictionary::isFileDict
virtual bool isFileDict() const
if dictionary is file dictionary, return false
Definition: dictionary.cpp:380
pFlow::dictionary::dict
virtual dictionary & dict()
ref to this dictionary, if it is a dictionary
Definition: dictionary.cpp:369
pFlow::dictionary::readDictionary
bool readDictionary(iIstream &is)
read dictionary from stream - without keyword
Definition: dictionary.cpp:37
pFlow::dictionary::writeDictionary
bool writeDictionary(iOstream &os, bool withBlock=true) const
write dictionary to stream - with keyword
Definition: dictionary.cpp:87
pFlow::dictionary::numDataEntries
size_t numDataEntries() const
return number of non-nullptr dataEntries
Definition: dictionary.cpp:682
pFlow::dictionary::dataEntryRef
dataEntry & dataEntryRef(const word &keyword)
ref to a subdictioanry fatalExit if not found/not a dataEntry
Definition: dictionary.cpp:613
pFlow::dictionary::write
bool write(iOstream &os) const override
write to stream
Definition: dictionary.cpp:793
pFlow::min
T min(const internalField< T, MemorySpace > &iField)
Definition: internalFieldAlgorithms.hpp:28
pFlow::dictionary::subDictPtr
dictionary * subDictPtr(const word &keyword)
pointer to a subdictionary fatalExit if not found
Definition: dictionary.cpp:542
pFlow::dictionary::readDataEntry
bool readDataEntry(const word &keyword, T &val) const
reads a dataEntry with keyword from dictionary
Definition: dictionary.hpp:357
pFlow::dictionary::subDict
dictionary & subDict(const word &keyword)
ref to a subdictioanry fatalExit if not found
Definition: dictionary.cpp:560
iEntry.hpp
pFlow::iEntry::name
virtual word name() const
name/keyword of entry
Definition: iEntry.hpp:100
pFlow::dictionary::getVal
T getVal(const word &keyword) const
get the value of data entry
Definition: dictionary.hpp:379
pFlow::dictionary::addPtr
bool addPtr(const word &keyword, uniquePtr< iEntry > &etry, bool warning=true)
add a pointer entry (dictionary/dataEntry) replaces this entry with existing one and issue a warning
Definition: dictionary.cpp:386
pFlow::iTstream
Input token stream.
Definition: iTstream.hpp:27
pFlow::dictionary::addOrReplace
bool addOrReplace(const word &keyword, const T &v)
Definition: dictionary.hpp:445
pFlow::uniquePtr
Definition: uniquePtr.hpp:42
pFlow::dictionary::getValOrSetMin
T getValOrSetMin(const word &keyword, const T &setMinVal) const
get the value of data entry anf return max(setMinVal, value) if not found, set the value to setMinVal
Definition: dictionary.hpp:438
dataEntry.hpp
pFlow::uint64
unsigned long long int uint64
Definition: builtinTypes.hpp:58
pFlow::iEntry
Interface calss for data entry and dictionary
Definition: iEntry.hpp:42
pFlow::dictionary::dictionary
dictionary()
cunstructs a null dictionary
Definition: dictionary.cpp:135
pFlow::dictionary::clear
void clear()
Definition: dictionary.cpp:535
pFlow::int8
signed char int8
Definition: builtinTypes.hpp:48
pFlow::dictionary::addDict
bool addDict(const word &keyword, const dictionary &dict)
add a dictionary with the specifiedd keyword, if it exists, replace it.
Definition: dictionary.cpp:526
pFlow::dictionary::numDictionaries
size_t numDictionaries() const
return number of non-nullptr dictionaries
Definition: dictionary.cpp:695
pFlow::dictionary::getValOrSetMax
T getValOrSetMax(const word &keyword, const T &setMaxVal) const
get the value of data entry anf return max(setMaxVal, value) if not found, set the value to setMaxVal
Definition: dictionary.hpp:432
pFlow::uint8
unsigned char uint8
Definition: builtinTypes.hpp:54
pFlow::dictionary::parrentDict
virtual const dictionary & parrentDict() const
const ref to parrent dictionary
Definition: dictionary.cpp:362
pFlow::iOstream
Interface class for any output stream.
Definition: iOstream.hpp:59
pFlow::dictionary::dataEntryKeywords
wordList dataEntryKeywords() const
return a list of all dataEntries (non-nullptr) keywords
Definition: dictionary.cpp:719
pFlow::dictionary
Dictionary holds a set of data entries or sub-dictionaries that are enclosed in a curely braces or ar...
Definition: dictionary.hpp:67
MapPtr.hpp