diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 6b271cfc1d..47c8dee788 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -209,6 +209,7 @@ $(dll)/codedBase/codedBase.C db/functionObjects/functionObject/functionObject.C db/functionObjects/functionObjectList/functionObjectList.C db/functionObjects/functionObjectFile/functionObjectFile.C +db/functionObjects/functionObjectState/functionObjectState.C db/functionObjects/outputFilterOutputControl/outputFilterOutputControl.C diff --git a/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.C b/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.C index 847747964d..e44f41f6ac 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -39,6 +39,7 @@ Foam::label Foam::functionObjectFile::addChars = 7; void Foam::functionObjectFile::initStream(Ostream& os) const { os.setf(ios_base::scientific, ios_base::floatfield); + os.precision(writePrecision_); os.width(charWidth()); } @@ -78,78 +79,44 @@ Foam::fileName Foam::functionObjectFile::baseTimeDir() const } -void Foam::functionObjectFile::createFiles() +Foam::autoPtr Foam::functionObjectFile::createFile +( + const word& name +) const { - if (Pstream::master()) + autoPtr osPtr; + + if (Pstream::master() && writeToFile_) { const word startTimeName = obr_.time().timeName(obr_.time().startTime().value()); - forAll(names_, i) + fileName outputDir(baseFileDir()/prefix_/startTimeName); + + mkDir(outputDir); + + word fName(name); + + // Check if file already exists + IFstream is(outputDir/(fName + ".dat")); + if (is.good()) { - if (!filePtrs_.set(i)) - { - fileName outputDir(baseFileDir()/prefix_/startTimeName); - mkDir(outputDir); - - word fName(names_[i]); - - // Check if file already exists - IFstream is(outputDir/(fName + ".dat")); - if (is.good()) - { - fName = fName + "_" + obr_.time().timeName(); - } - - filePtrs_.set(i, new OFstream(outputDir/(fName + ".dat"))); - - initStream(filePtrs_[i]); - - writeFileHeader(i); - - } + fName = fName + "_" + obr_.time().timeName(); } + + osPtr.set(new OFstream(outputDir/(fName + ".dat"))); + + initStream(osPtr()); } + + return osPtr; } -void Foam::functionObjectFile::writeFileHeader(const label i) -{} - - -void Foam::functionObjectFile::write() +void Foam::functionObjectFile::resetFile(const word& fileName) { - createFiles(); -} - - -void Foam::functionObjectFile::resetNames(const wordList& names) -{ - names_.clear(); - names_.append(names); - - if (Pstream::master()) - { - filePtrs_.clear(); - filePtrs_.setSize(names_.size()); - - createFiles(); - } -} - - -void Foam::functionObjectFile::resetName(const word& name) -{ - names_.clear(); - names_.append(name); - - if (Pstream::master()) - { - filePtrs_.clear(); - filePtrs_.setSize(1); - - createFiles(); - } + fileName_ = fileName; + filePtr_ = createFile(fileName_); } @@ -169,8 +136,10 @@ Foam::functionObjectFile::functionObjectFile : obr_(obr), prefix_(prefix), - names_(), - filePtrs_() + fileName_("undefined"), + filePtr_(), + writePrecision_(IOstream::defaultPrecision()), + writeToFile_(true) {} @@ -178,46 +147,22 @@ Foam::functionObjectFile::functionObjectFile ( const objectRegistry& obr, const word& prefix, - const word& name + const word& fileName, + const dictionary& dict ) : obr_(obr), prefix_(prefix), - names_(), - filePtrs_() + fileName_(fileName), + filePtr_(), + writePrecision_(IOstream::defaultPrecision()), + writeToFile_(true) { - names_.clear(); - names_.append(name); - if (Pstream::master()) + read(dict); + + if (writeToFile_) { - filePtrs_.clear(); - filePtrs_.setSize(1); - - // Cannot create files - need to access virtual function - } -} - - -Foam::functionObjectFile::functionObjectFile -( - const objectRegistry& obr, - const word& prefix, - const wordList& names -) -: - obr_(obr), - prefix_(prefix), - names_(names), - filePtrs_() -{ - names_.clear(); - names_.append(names); - if (Pstream::master()) - { - filePtrs_.clear(); - filePtrs_.setSize(names_.size()); - - // Cannot create files - need to access virtual function + filePtr_ = createFile(fileName_); } } @@ -230,72 +175,38 @@ Foam::functionObjectFile::~functionObjectFile() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // -const Foam::wordList& Foam::functionObjectFile::names() const +void Foam::functionObjectFile::read(const dictionary& dict) { - return names_; + writePrecision_ = + dict.lookupOrDefault("writePrecision", IOstream::defaultPrecision()); + + // Only write on master process + writeToFile_ = dict.lookupOrDefault("writeToFile", true); + writeToFile_ = writeToFile_ && Pstream::master(); } Foam::OFstream& Foam::functionObjectFile::file() { - if (!Pstream::master()) + if (!writeToFile_) + { + return Snull; + } + + if (!filePtr_.valid()) { FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()") - << "Request for file() can only be done by the master process" + << "File pointer not allocated" << abort(FatalError); } - if (filePtrs_.size() != 1) - { - WarningIn("Foam::Ostream& Foam::functionObjectFile::file()") - << "Requested single file, but multiple files are present" - << endl; - } - - if (!filePtrs_.set(0)) - { - FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()") - << "File pointer at index " << 0 << " not allocated" - << abort(FatalError); - } - - return filePtrs_[0]; + return filePtr_(); } -Foam::PtrList& Foam::functionObjectFile::files() +bool Foam::functionObjectFile::writeToFile() const { - if (!Pstream::master()) - { - FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::files()") - << "Request for files() can only be done by the master process" - << abort(FatalError); - } - - return filePtrs_; -} - - -Foam::OFstream& Foam::functionObjectFile::file(const label i) -{ - if (!Pstream::master()) - { - FatalErrorIn - ( - "Foam::OFstream& Foam::functionObjectFile::file(const label)" - ) - << "Request for file(i) can only be done by the master process" - << abort(FatalError); - } - - if (!filePtrs_.set(i)) - { - FatalErrorIn("Foam::OFstream& Foam::functionObjectFile::file()") - << "File pointer at index " << i << " not allocated" - << abort(FatalError); - } - - return filePtrs_[i]; + return writeToFile_; } diff --git a/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.H b/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.H index 5b5e3597a1..2dfd5c7c11 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.H +++ b/src/OpenFOAM/db/functionObjects/functionObjectFile/functionObjectFile.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -57,6 +57,8 @@ namespace Foam class functionObjectFile { +private: + // Private data //- Reference to the database @@ -65,15 +67,24 @@ class functionObjectFile //- Prefix const word prefix_; - //- File names - wordList names_; + //- Name of file + word fileName_; //- File pointer - PtrList filePtrs_; + autoPtr filePtr_; + + //- Write precision + label writePrecision_; protected: + // Protected Data + + //- Flag to enable/disable writing to file + bool writeToFile_; + + // Protected Member Functions //- Initialise the output stream for writing @@ -85,20 +96,11 @@ protected: //- Return the base directory for the current time value virtual fileName baseTimeDir() const; - //- Create the output file - virtual void createFiles(); + //- Return an autoPtr to a new file + virtual autoPtr createFile(const word& name) const; - //- File header information - virtual void writeFileHeader(const label i = 0); - - //- Write function - virtual void write(); - - //- Reset the list of names from a wordList - virtual void resetNames(const wordList& names); - - //- Reset the list of names to a single name entry - virtual void resetName(const word& name); + //- Reset internal file pointer to new file with new name + virtual void resetFile(const word& name); //- Return the value width when writing to stream with optional offset virtual Omanip valueWidth(const label offset = 0) const; @@ -118,26 +120,18 @@ public: //- Additional characters for writing static label addChars; - // Constructors //- Construct null functionObjectFile(const objectRegistry& obr, const word& prefix); - //- Construct from components + //- Construct from components and read options from dictionary functionObjectFile ( const objectRegistry& obr, const word& prefix, - const word& name - ); - - //- Construct from components - functionObjectFile - ( - const objectRegistry& obr, - const word& prefix, - const wordList& names + const word& fileName, + const dictionary& dict ); @@ -147,17 +141,14 @@ public: // Member Functions - //- Return const access to the names - const wordList& names() const; + //- Read + void read(const dictionary& dict); //- Return access to the file (if only 1) OFstream& file(); - //- Return access to the files - PtrList& files(); - - //- Return file 'i' - OFstream& file(const label i); + //- Return true if can write to file + bool writeToFile() const; //- Write a commented string to stream void writeCommented diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C index 0c44dbbfbd..0c93b738d2 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.C @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation - \\/ M anipulation | + \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -29,6 +29,28 @@ License // * * * * * * * * * * * * Private Member Functions * * * * * * * * * * * * // +void Foam::functionObjectList::createStateDict() const +{ + // Cannot set the state dictionary on construction since Time has not + // been fully initialised + stateDictPtr_.reset + ( + new IOdictionary + ( + IOobject + ( + "functionObjectProperties", + time_.timeName(), + "uniform"/word("functionObjects"), + time_, + IOobject::READ_IF_PRESENT, + IOobject::NO_WRITE + ) + ) + ); +} + + Foam::functionObject* Foam::functionObjectList::remove ( const word& key, @@ -70,6 +92,7 @@ Foam::functionObjectList::functionObjectList indices_(), time_(t), parentDict_(t.controlDict()), + stateDictPtr_(), execution_(execution), updated_(false) {} @@ -87,6 +110,7 @@ Foam::functionObjectList::functionObjectList indices_(), time_(t), parentDict_(parentDict), + stateDictPtr_(), execution_(execution), updated_(false) {} @@ -100,6 +124,28 @@ Foam::functionObjectList::~functionObjectList() // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // +Foam::IOdictionary& Foam::functionObjectList::stateDict() +{ + if (!stateDictPtr_.valid()) + { + createStateDict(); + } + + return stateDictPtr_(); +} + + +const Foam::IOdictionary& Foam::functionObjectList::stateDict() const +{ + if (!stateDictPtr_.valid()) + { + createStateDict(); + } + + return stateDictPtr_(); +} + + void Foam::functionObjectList::clear() { PtrList::clear(); @@ -165,6 +211,22 @@ bool Foam::functionObjectList::execute(const bool forceWrite) } } + // Force writing of state dictionary after function object execution + if (time_.outputTime()) + { + label oldPrecision = IOstream::precision_; + IOstream::precision_ = 16; + + stateDictPtr_->writeObject + ( + IOstream::ASCII, + IOstream::currentVersion, + time_.writeCompression() + ); + + IOstream::precision_ = oldPrecision; + } + return ok; } @@ -234,6 +296,11 @@ bool Foam::functionObjectList::adjustTimeStep() bool Foam::functionObjectList::read() { + if (!stateDictPtr_.valid()) + { + createStateDict(); + } + bool ok = true; updated_ = execution_; diff --git a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H index 126a1f18bb..681e135d0f 100644 --- a/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H +++ b/src/OpenFOAM/db/functionObjects/functionObjectList/functionObjectList.H @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation - \\/ M anipulation | + \\ / A nd | Copyright (C) 2011-2014 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,6 +43,7 @@ SourceFiles #include "functionObject.H" #include "SHA1Digest.H" #include "HashTable.H" +#include "IOdictionary.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -74,6 +75,9 @@ class functionObjectList // functionObject specifications. const dictionary& parentDict_; + //- Function object properties - stores state information + mutable autoPtr stateDictPtr_; + //- Switch for the execution of the functionObjects bool execution_; @@ -83,6 +87,9 @@ class functionObjectList // Private Member Functions + //- Create state dictionary + void createStateDict() const; + //- Remove and return the function object pointer by name, // and returns the old index via the parameter. // Returns a NULL pointer (and index -1) if it didn't exist. @@ -136,6 +143,12 @@ public: //- Access to the functionObjects using PtrList::operator[]; + //- Return the state dictionary + IOdictionary& stateDict(); + + //- Return const access to the state dictionary + const IOdictionary& stateDict() const; + //- Clear the list of function objects virtual void clear(); diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C new file mode 100644 index 0000000000..37d57d06f1 --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.C @@ -0,0 +1,173 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +#include "functionObjectState.H" +#include "Time.H" + +const Foam::word Foam::functionObjectState::resultsName_ = "results"; + + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +Foam::functionObjectState::functionObjectState +( + const objectRegistry& obr, + const word& name +) +: + obr_(obr), + name_(name), + active_(true), + stateDict_ + ( + const_cast(obr.time().functionObjects().stateDict()) + ) +{} + + +// * * * * * * * * * * * * * * * * Destructor * * * * * * * * * * * * * * * // + +Foam::functionObjectState::~functionObjectState() +{} + + +// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // + +const Foam::word& Foam::functionObjectState::name() const +{ + return name_; +} + + +bool Foam::functionObjectState::active() const +{ + return active_; +} + + +const Foam::IOdictionary& Foam::functionObjectState::stateDict() const +{ + return stateDict_; +} + + +Foam::dictionary& Foam::functionObjectState::propertyDict() +{ + if (!stateDict_.found(name_)) + { + stateDict_.add(name_, dictionary()); + } + + return stateDict_.subDict(name_); +} + + +bool Foam::functionObjectState::foundProperty(const word& entryName) const +{ + if (stateDict_.found(name_)) + { + const dictionary& baseDict = stateDict_.subDict(name_); + return baseDict.found(entryName); + } + + return false; +} + + +Foam::word Foam::functionObjectState::resultType(const word& entryName) const +{ + return objectResultType(name_, entryName); +} + + +Foam::word Foam::functionObjectState::objectResultType +( + const word& objectName, + const word& entryName +) const +{ + word result = word::null; + + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + forAllConstIter(dictionary, objectDict, iter) + { + const dictionary& dict = iter().dict(); + + if (dict.found(entryName)) + { + return dict.dictName(); + } + } + } + } + + return result; +} + + +Foam::List Foam::functionObjectState::objectResultEntries() const +{ + return objectResultEntries(name_); +} + + +Foam::List Foam::functionObjectState::objectResultEntries +( + const word& objectName +) const +{ + DynamicList result(2); + + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + forAllConstIter(dictionary, objectDict, iter) + { + const dictionary& dict = iter().dict(); + result.append(dict.toc()); + } + } + } + + wordList entries; + entries.transfer(result); + + return entries; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H new file mode 100644 index 0000000000..c3c8b94c7a --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectState.H @@ -0,0 +1,255 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +Class + Foam::functionObjectState + +Description + Base class for function objects, adding functionality to read/write state + information (data required for smooth restart behaviour) and results + to/from the state dictionary + +See Also + Foam::functionObject + +SourceFiles + functionObjectState.C + +\*---------------------------------------------------------------------------*/ + +#ifndef functionObjectState_H +#define functionObjectState_H + +#include "objectRegistry.H" +#include "IOdictionary.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + +/*---------------------------------------------------------------------------*\ + Class functionObjectState Declaration +\*---------------------------------------------------------------------------*/ + +class functionObjectState +{ +private: + + // Private data + + //- Name of the results dictionary + static const word resultsName_; + + //- Reference to the database + const objectRegistry& obr_; + + +protected: + + // Protected data + + //- Name of model + const word name_; + + //- Flag to indicate whether the object is active + bool active_; + + //- Reference to the state dictionary + IOdictionary& stateDict_; + + +protected: + + // Protected Member Functions + + //- Disallow default bitwise copy construct + functionObjectState(const functionObjectState&); + + //- Disallow default bitwise assignment + void operator=(const functionObjectState&); + + +public: + + // Constructors + + //- Construct from components + functionObjectState + ( + const objectRegistry& obr, + const word& name + ); + + + //- Destructor + virtual ~functionObjectState(); + + + // Member Functions + + //- Return the name + const word& name() const; + + //- Return the active flag + bool active() const; + + //- Return access to the state dictionary + const IOdictionary& stateDict() const; + + //- Return access to the property dictionary + dictionary& propertyDict(); + + //- Set the active status by querying objectRegistry type + // returns new active status + template + bool setActive(); + + + // Properties + + //- Return true if the property exists + bool foundProperty(const word& entryName) const; + + //- Retrieve generic property + template + Type getProperty + ( + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve generic property + template + void getProperty(const word& entryName, Type& value) const; + + //- Add generic property + template + void setProperty(const word& entryName, const Type& value); + + //- Retrieve generic property from named object + template + Type getObjectProperty + ( + const word& objectName, + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve generic property from named object + template + void getObjectProperty + ( + const word& objectName, + const word& entryName, + Type& value + ) const; + + //- Add generic property from named object + template + void setObjectProperty + ( + const word& objectName, + const word& entryName, + const Type& value + ); + + + // Results + + //- Add result + template + void setResult + ( + const word& entryName, + const Type& value + ); + + //- Add result from named object + template + void setObjectResult + ( + const word& objectName, + const word& entryName, + const Type& value + ); + + //- Retrieve result + template + Type getResult + ( + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve result from named object + template + Type getObjectResult + ( + const word& objectName, + const word& entryName, + const Type& defaultValue = pTraits::zero + ) const; + + //- Retrieve result from named object + template + void getObjectResult + ( + const word& objectName, + const word& entryName, + Type& value + ) const; + + //- Retrieve the result type + word resultType(const word& entryName) const; + + //- Return the type of result + word objectResultType + ( + const word& objectName, + const word& entryName + ) const; + + //- Retrieve the result entries + List objectResultEntries() const; + + //- Return result entries for named object + List objectResultEntries(const word& objectName) const; +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository + #include "functionObjectStateTemplates.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C new file mode 100644 index 0000000000..fb27b2668c --- /dev/null +++ b/src/OpenFOAM/db/functionObjects/functionObjectState/functionObjectStateTemplates.C @@ -0,0 +1,242 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd +------------------------------------------------------------------------------- +License + This file is part of OpenFOAM. + + OpenFOAM is free software: you can redistribute it and/or modify it + under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + OpenFOAM is distributed in the hope that it will be useful, but WITHOUT + ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + for more details. + + You should have received a copy of the GNU General Public License + along with OpenFOAM. If not, see . + +\*---------------------------------------------------------------------------*/ + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +template +bool Foam::functionObjectState::setActive() +{ + active_ = true; + + if (!isA(obr_)) + { + WarningIn + ( + "void Foam::functionObjectState::setActive()" + ) << "No " << Type::typeName << " available, deactivating " << name_ + << endl; + + active_ = false; + } + + return active_; +} + + +template +Type Foam::functionObjectState::getProperty +( + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getProperty(entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getProperty +( + const word& entryName, + Type& value +) const +{ + getObjectProperty(name_, entryName, value); +} + + +template +void Foam::functionObjectState::setProperty +( + const word& entryName, + const Type& value +) +{ + setObjectProperty(name_, entryName, value); +} + + +template +Type Foam::functionObjectState::getObjectProperty +( + const word& objectName, + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getObjectProperty(objectName, entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getObjectProperty +( + const word& objectName, + const word& entryName, + Type& value +) const +{ + if (stateDict_.found(objectName)) + { + const dictionary& baseDict = stateDict_.subDict(objectName); + if (baseDict.found(entryName)) + { + if (baseDict.isDict(entryName)) + { + value = baseDict.subDict(entryName); + } + else + { + baseDict.lookup(entryName) >> value; + } + } + } +} + + +template +void Foam::functionObjectState::setObjectProperty +( + const word& objectName, + const word& entryName, + const Type& value +) +{ + if (!stateDict_.found(objectName)) + { + stateDict_.add(objectName, dictionary()); + } + + dictionary& baseDict = stateDict_.subDict(objectName); + baseDict.add(entryName, value, true); +} + + +template +void Foam::functionObjectState::setResult +( + const word& entryName, + const Type& value +) +{ + setObjectResult(name_, entryName, value); +} + + +template +void Foam::functionObjectState::setObjectResult +( + const word& objectName, + const word& entryName, + const Type& value +) +{ + if (!stateDict_.found(resultsName_)) + { + stateDict_.add(resultsName_, dictionary()); + } + + dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (!resultsDict.found(objectName)) + { + resultsDict.add(name_, dictionary()); + } + + dictionary& objectDict = resultsDict.subDict(objectName); + + const word& dictTypeName = pTraits::typeName; + + if (!objectDict.found(dictTypeName)) + { + objectDict.add(dictTypeName, dictionary()); + } + + dictionary& resultTypeDict = objectDict.subDict(dictTypeName); + + resultTypeDict.add(entryName, value, true); +} + + +template +Type Foam::functionObjectState::getResult +( + const word& entryName, + const Type& defaultValue +) const +{ + return getObjectResult(name_, entryName, defaultValue); +} + + +template +Type Foam::functionObjectState::getObjectResult +( + const word& objectName, + const word& entryName, + const Type& defaultValue +) const +{ + Type result = defaultValue; + getObjectResult(objectName, entryName, result); + return result; +} + + +template +void Foam::functionObjectState::getObjectResult +( + const word& objectName, + const word& entryName, + Type& value +) const +{ + if (stateDict_.found(resultsName_)) + { + const dictionary& resultsDict = stateDict_.subDict(resultsName_); + + if (resultsDict.found(objectName)) + { + const dictionary& objectDict = resultsDict.subDict(objectName); + + const word& dictTypeName = pTraits::typeName; + + if (objectDict.found(dictTypeName)) + { + const dictionary& resultTypeDict = + objectDict.subDict(dictTypeName); + + resultTypeDict.readIfPresent(entryName, value); + } + } + } +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.C b/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.C index e4f60195a8..87a78b64cb 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.C +++ b/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -55,6 +55,20 @@ Foam::Constant::Constant(const word& entryName, const dictionary& dict) } +template +Foam::Constant::Constant +( + const word& entryName, + const Type& value, + const dimensionSet& dimensions +) +: + DataEntry(entryName), + value_(value), + dimensions_(dimensions) +{} + + template Foam::Constant::Constant(const Constant& cnst) : diff --git a/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.H b/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.H index 0c251fec03..024eca6725 100644 --- a/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.H +++ b/src/OpenFOAM/primitives/functions/DataEntry/Constant/Constant.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -86,7 +86,15 @@ public: // Constructors - //- Construct from entry name and Istream + //- Construct from components + Constant + ( + const word& entryName, + const Type& value, + const dimensionSet& dimensions = dimless + ); + + //- Construct from entry name and dictionary Constant(const word& entryName, const dictionary& dict); //- Copy constructor diff --git a/src/postProcessing/functionObjects/IO/IOFunctionObjectsDoc.H b/src/postProcessing/functionObjects/IO/doc/IOFunctionObjectsDoc.H similarity index 100% rename from src/postProcessing/functionObjects/IO/IOFunctionObjectsDoc.H rename to src/postProcessing/functionObjects/IO/doc/IOFunctionObjectsDoc.H diff --git a/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.C b/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.C index 480d5fa8be..fd58741bd7 100644 --- a/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.C +++ b/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -37,13 +37,16 @@ namespace Foam // * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * * // -void Foam::cloudInfo::writeFileHeader(const label i) +void Foam::cloudInfo::writeFileHeader(Ostream& os) const { - writeHeader(file(), "Cloud information"); - writeCommented(file(), "Time"); - writeTabbed(file(), "nParcels"); - writeTabbed(file(), "mass"); - file() << endl; + writeHeader(os, "Cloud information"); + writeCommented(os, "Time"); + writeTabbed(os, "nParcels"); + writeTabbed(os, "mass"); + writeTabbed(os, "Dmax"); + writeTabbed(os, "D10"); + writeTabbed(os, "D32"); + os << endl; } @@ -60,7 +63,10 @@ Foam::cloudInfo::cloudInfo functionObjectFile(obr, name), name_(name), obr_(obr), - active_(true) + active_(true), + log_(true), + cloudNames_(), + filePtrs_() { read(dict); } @@ -78,47 +84,70 @@ void Foam::cloudInfo::read(const dictionary& dict) { if (active_) { - functionObjectFile::resetNames(dict.lookup("clouds")); + functionObjectFile::read(dict); - Info<< type() << " " << name_ << ": "; - if (names().size()) + log_ = dict.lookupOrDefault("log", true); + dict.lookup("clouds") >> cloudNames_; + + if (log_) { - Info<< "applying to clouds:" << nl; - forAll(names(), i) + Info<< type() << " " << name_ << ": "; + + if (cloudNames_.size()) { - Info<< " " << names()[i] << nl; + Info<< "applying to clouds:" << nl; + forAll(cloudNames_, i) + { + Info<< " " << cloudNames_[i] << nl; + } + Info<< endl; + } + else + { + Info<< "no clouds to be processed" << nl << endl; } - Info<< endl; } - else + + if (writeToFile()) { - Info<< "no clouds to be processed" << nl << endl; + filePtrs_.setSize(cloudNames_.size()); + filePtrs_.clear(); + forAll(filePtrs_, fileI) + { + const word& cloudName = cloudNames_[fileI]; + filePtrs_.set(fileI, createFile(cloudName)); + writeFileHeader(filePtrs_[fileI]); + } } } } void Foam::cloudInfo::execute() -{} +{ + // Do nothing +} void Foam::cloudInfo::end() -{} +{ + // Do nothing +} void Foam::cloudInfo::timeSet() -{} +{ + // Do nothing +} void Foam::cloudInfo::write() { if (active_) { - functionObjectFile::write(); - - forAll(names(), i) + forAll(cloudNames_, cloudI) { - const word& cloudName = names()[i]; + const word& cloudName = cloudNames_[cloudI]; const kinematicCloud& cloud = obr_.lookupObject(cloudName); @@ -127,12 +156,31 @@ void Foam::cloudInfo::write() scalar massInSystem = returnReduce(cloud.massInSystem(), sumOp()); + scalar Dmax = cloud.Dmax(); + scalar D10 = cloud.Dij(1, 0); + scalar D32 = cloud.Dij(3, 2); + if (Pstream::master()) { - file(i) + filePtrs_[cloudI] << obr_.time().value() << token::TAB << nParcels << token::TAB - << massInSystem << endl; + << massInSystem << token::TAB + << Dmax << token::TAB + << D10 << token::TAB + << D32 << token::TAB + << endl; + } + + if (log_) + { + Info<< type() << " " << name_ << " output:" << nl + << " number of parcels : " << nParcels << nl + << " mass in system : " << massInSystem << nl + << " maximum diameter : " << Dmax << nl + << " D10 diameter : " << D10 << nl + << " D32 diameter : " << D32 << nl + << endl; } } } diff --git a/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.H b/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.H index 9bd1761061..9e160bbab4 100644 --- a/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.H +++ b/src/postProcessing/functionObjects/cloud/cloudInfo/cloudInfo.H @@ -2,8 +2,8 @@ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | - \\ / A nd | Copyright (C) 2012-2013 OpenFOAM Foundation - \\/ M anipulation | + \\ / A nd | Copyright (C) 2012-2015 OpenFOAM Foundation + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -109,11 +109,20 @@ protected: //- on/off switch bool active_; + //- Switch to send output to Info as well + Switch log_; + + //- List of cloud names + wordList cloudNames_; + + //- Output file per cloud + PtrList filePtrs_; + // Protected Member Functions //- File header information - virtual void writeFileHeader(const label i); + virtual void writeFileHeader(Ostream& os) const; //- Disallow default bitwise copy construct cloudInfo(const cloudInfo&); diff --git a/src/postProcessing/functionObjects/cloud/cloudFunctionObjectsDoc.H b/src/postProcessing/functionObjects/cloud/doc/cloudFunctionObjectsDoc.H similarity index 100% rename from src/postProcessing/functionObjects/cloud/cloudFunctionObjectsDoc.H rename to src/postProcessing/functionObjects/cloud/doc/cloudFunctionObjectsDoc.H diff --git a/src/postProcessing/functionObjects/doc/functionObjects.dox b/src/postProcessing/functionObjects/doc/functionObjects.dox index 4fa79c730c..dc0b395fe8 100644 --- a/src/postProcessing/functionObjects/doc/functionObjects.dox +++ b/src/postProcessing/functionObjects/doc/functionObjects.dox @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2012-2014 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -63,6 +63,8 @@ functions enabled yes; timeStart 0; timeEnd 10; + evaluateControl timeStep; + evaluateInterval 1; outputControl outputTime; outputInterval 1; ... @@ -78,7 +80,9 @@ Where: region | name of region for multi-region cases | no | enabled | on/off switch | no | yes timeStart| start time | no | - timeEnd | end time | no | + timeEnd | end time | no | + evaluateControl | when to evaluate: either 'outputTime' or 'timeStep'| no | timeStep + evaluateInterval| steps between evaluation when evaluateControl=timeStep | no | 1 outputControl | when to output: either 'outputTime' or 'timeStep'| no | timeStep outputInterval| steps between output when outputControl=timeStep | no | 1 \endtable @@ -88,7 +92,7 @@ typically used as the name of the output directory for any derived data. The \c type entry defines the type of function object properties that follow. Since the function objects are packaged into separate libraries, the user must tell the code where to find the function object implementation, identified -using the \c libs entry. +using the \c functionObjectLibs entry. \*---------------------------------------------------------------------------*/ diff --git a/src/postProcessing/functionObjects/field/fieldFunctionObjectsDoc.H b/src/postProcessing/functionObjects/field/doc/fieldFunctionObjectsDoc.H similarity index 100% rename from src/postProcessing/functionObjects/field/fieldFunctionObjectsDoc.H rename to src/postProcessing/functionObjects/field/doc/fieldFunctionObjectsDoc.H diff --git a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.C b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.C index 62407d8316..5652904ffa 100644 --- a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.C +++ b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -66,7 +66,8 @@ void Foam::fieldAverage::initialize() { resetFields(); - Info<< type() << " " << name_ << ":" << nl; + if (log_) Info << type() << " " << name_ << ":" << nl; + // Add mean fields to the field lists forAll(faItems_, fieldI) @@ -98,7 +99,7 @@ void Foam::fieldAverage::initialize() // ensure first averaging works unconditionally prevTimeIndex_ = -1; - Info<< endl; + if (log_) Info << endl; initialised_ = true; } @@ -123,9 +124,11 @@ void Foam::fieldAverage::calcAverages() prevTimeIndex_ = currentTimeIndex; } - Info<< type() << " " << name_ << " output:" << nl; - - Info<< " Calculating averages" << nl; + if (log_) + { + Info<< type() << " " << name_ << " output:" << nl + << " Calculating averages" << nl; + } addMeanSqrToPrime2Mean(); addMeanSqrToPrime2Mean(); @@ -149,7 +152,7 @@ void Foam::fieldAverage::calcAverages() void Foam::fieldAverage::writeAverages() const { - Info<< " Writing average fields" << endl; + if (log_) Info << " Writing average fields" << endl; writeFields(); writeFields(); @@ -159,31 +162,17 @@ void Foam::fieldAverage::writeAverages() const } -void Foam::fieldAverage::writeAveragingProperties() const +void Foam::fieldAverage::writeAveragingProperties() { - IOdictionary propsDict - ( - IOobject - ( - "fieldAveragingProperties", - obr_.time().timeName(), - "uniform", - obr_, - IOobject::NO_READ, - IOobject::NO_WRITE, - false - ) - ); - forAll(faItems_, fieldI) { const word& fieldName = faItems_[fieldI].fieldName(); - propsDict.add(fieldName, dictionary()); - propsDict.subDict(fieldName).add("totalIter", totalIter_[fieldI]); - propsDict.subDict(fieldName).add("totalTime", totalTime_[fieldI]); - } - propsDict.regIOobject::write(); + dictionary propsDict; + propsDict.add("totalIter", totalIter_[fieldI]); + propsDict.add("totalTime", totalTime_[fieldI]); + setProperty(fieldName, propsDict); + } } @@ -195,46 +184,41 @@ void Foam::fieldAverage::readAveragingProperties() totalTime_.clear(); totalTime_.setSize(faItems_.size(), obr_.time().deltaTValue()); - if (resetOnRestart_ || resetOnOutput_) + if (log_ && (resetOnRestart_ || resetOnOutput_)) { Info<< " Starting averaging at time " << obr_.time().timeName() << nl; } else { - IOobject propsDictHeader - ( - "fieldAveragingProperties", - obr_.time().timeName(obr_.time().startTime().value()), - "uniform", - obr_, - IOobject::MUST_READ_IF_MODIFIED, - IOobject::NO_WRITE, - false - ); + if (log_) Info << " Restarting averaging for fields:" << nl; - if (!propsDictHeader.headerOk()) - { - Info<< " Starting averaging at time " << obr_.time().timeName() - << nl; - return; - } - - IOdictionary propsDict(propsDictHeader); - - Info<< " Restarting averaging for fields:" << nl; forAll(faItems_, fieldI) { const word& fieldName = faItems_[fieldI].fieldName(); - if (propsDict.found(fieldName)) + if (foundProperty(fieldName)) { - dictionary fieldDict(propsDict.subDict(fieldName)); + dictionary fieldDict; + getProperty(fieldName, fieldDict); totalIter_[fieldI] = readLabel(fieldDict.lookup("totalIter")); totalTime_[fieldI] = readScalar(fieldDict.lookup("totalTime")); - Info<< " " << fieldName - << " iters = " << totalIter_[fieldI] - << " time = " << totalTime_[fieldI] << nl; + + if (log_) + { + Info<< " " << fieldName + << " iters = " << totalIter_[fieldI] + << " time = " << totalTime_[fieldI] << nl; + } + } + else + { + if (log_) + { + Info<< " " << fieldName + << ": starting averaging at time " + << obr_.time().timeName() << endl; + } } } } @@ -251,37 +235,22 @@ Foam::fieldAverage::fieldAverage const bool loadFromFiles ) : - name_(name), + functionObjectState(obr, name), obr_(obr), - active_(true), prevTimeIndex_(-1), resetOnRestart_(false), resetOnOutput_(false), + log_(true), initialised_(false), faItems_(), totalIter_(), totalTime_() { // Only active if a fvMesh is available - if (isA(obr_)) + if (setActive()) { read(dict); } - else - { - active_ = false; - WarningIn - ( - "fieldAverage::fieldAverage" - "(" - "const word&, " - "const objectRegistry&, " - "const dictionary&, " - "const bool " - ")" - ) << "No fvMesh available, deactivating " << name_ << nl - << endl; - } } @@ -299,7 +268,9 @@ void Foam::fieldAverage::read(const dictionary& dict) { initialised_ = false; - Info<< type() << " " << name_ << ":" << nl; + log_.readIfPresent("log", dict); + + if (log_) Info << type() << " " << name_ << ":" << nl; dict.readIfPresent("resetOnRestart", resetOnRestart_); dict.readIfPresent("resetOnOutput", resetOnOutput_); @@ -307,7 +278,7 @@ void Foam::fieldAverage::read(const dictionary& dict) readAveragingProperties(); - Info<< endl; + if (log_) Info << endl; } } @@ -317,7 +288,7 @@ void Foam::fieldAverage::execute() if (active_) { calcAverages(); - Info<< endl; + if (log_) Info << endl; } } @@ -326,8 +297,7 @@ void Foam::fieldAverage::end() { if (active_) { - calcAverages(); - Info<< endl; + execute(); } } @@ -345,8 +315,11 @@ void Foam::fieldAverage::write() if (resetOnOutput_) { - Info<< " Restarting averaging at time " << obr_.time().timeName() - << nl << endl; + if (log_) + { + Info<< " Restarting averaging at time " << obr_.time().timeName() + << nl << endl; + } totalIter_.clear(); totalIter_.setSize(faItems_.size(), 1); @@ -357,7 +330,7 @@ void Foam::fieldAverage::write() initialize(); } - Info<< endl; + if (log_) Info << endl; } } diff --git a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.H b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.H index 01b2558010..748b4ee168 100644 --- a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.H +++ b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverage.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2013 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -94,9 +94,10 @@ Description \table Property | Description | Required | Default value type | type name: fieldAverage | yes | - resetOnRestart | flag to reset the averaging on restart | yes | + resetOnRestart | flag to reset the averaging on restart | yes | resetOnOutput| flag to reset the averaging on output | yes | fields | list of fields and averaging options | yes | + log | Log to standard output | no | yes \endtable @@ -117,6 +118,7 @@ SourceFiles #ifndef fieldAverage_H #define fieldAverage_H +#include "functionObjectState.H" #include "volFieldsFwd.H" #include "Switch.H" @@ -139,20 +141,16 @@ class mapPolyMesh; \*---------------------------------------------------------------------------*/ class fieldAverage +: + public functionObjectState { protected: // Protected data - //- Name of this set of field averages. - word name_; - - //- Database this class is registered to + //- Reference to the database const objectRegistry& obr_; - //- On/off switch - bool active_; - //- Time at last call, prevents repeated averaging label prevTimeIndex_; @@ -162,6 +160,9 @@ protected: //- Reset the averaging process on output flag Switch resetOnOutput_; + //- Switch to send output to Info as well as to file + Switch log_; + //- Initialised flag bool initialised_; @@ -251,7 +252,7 @@ protected: void writeFields() const; //- Write averaging properties - steps and time - void writeAveragingProperties() const; + void writeAveragingProperties(); //- Read averaging properties - steps and time void readAveragingProperties(); diff --git a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverageTemplates.C b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverageTemplates.C index 62e733481d..4f3a2d7cab 100644 --- a/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverageTemplates.C +++ b/src/postProcessing/functionObjects/field/fieldAverage/fieldAverage/fieldAverageTemplates.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -38,7 +38,7 @@ void Foam::fieldAverage::addMeanFieldType(const label fieldI) const word& fieldName = faItems_[fieldI].fieldName(); const word& meanFieldName = faItems_[fieldI].meanFieldName(); - Info<< " Reading/initialising field " << meanFieldName << endl; + if (log_) Info << " Reading/initialising field " << meanFieldName << endl; if (obr_.foundObject(meanFieldName)) { @@ -46,9 +46,12 @@ void Foam::fieldAverage::addMeanFieldType(const label fieldI) } else if (obr_.found(meanFieldName)) { - Info<< " Cannot allocate average field " << meanFieldName - << " since an object with that name already exists." - << " Disabling averaging for field." << endl; + if (log_) + { + Info<< " Cannot allocate average field " << meanFieldName + << " since an object with that name already exists." + << " Disabling averaging for field." << endl; + } faItems_[fieldI].mean() = false; } @@ -107,7 +110,10 @@ void Foam::fieldAverage::addPrime2MeanFieldType(const label fieldI) const word& meanFieldName = faItems_[fieldI].meanFieldName(); const word& prime2MeanFieldName = faItems_[fieldI].prime2MeanFieldName(); - Info<< " Reading/initialising field " << prime2MeanFieldName << nl; + if (log_) + { + Info << " Reading/initialising field " << prime2MeanFieldName << nl; + } if (obr_.foundObject(prime2MeanFieldName)) { @@ -115,9 +121,12 @@ void Foam::fieldAverage::addPrime2MeanFieldType(const label fieldI) } else if (obr_.found(prime2MeanFieldName)) { - Info<< " Cannot allocate average field " << prime2MeanFieldName - << " since an object with that name already exists." - << " Disabling averaging for field." << nl; + if (log_) + { + Info<< " Cannot allocate average field " << prime2MeanFieldName + << " since an object with that name already exists." + << " Disabling averaging for field." << nl; + } faItems_[fieldI].prime2Mean() = false; } diff --git a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.C b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.C index dac7d8a71e..56ce3cde08 100644 --- a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.C +++ b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -49,6 +49,45 @@ const Foam::NamedEnum Foam::fieldMinMax::modeTypeNames_; +// * * * * * * * * * * * * * Protected Member Functions * * * * * * * * * * // + +void Foam::fieldMinMax::writeFileHeader(Ostream& os) const +{ + writeHeader(os, "Field minima and maxima"); + writeCommented(os, "Time"); + + if (writeLocation_) + { + writeTabbed(os, "field"); + writeTabbed(os, "min"); + writeTabbed(os, "position(min)"); + + if (Pstream::parRun()) + { + writeTabbed(os, "processor"); + } + + writeTabbed(os, "max"); + writeTabbed(os, "position(max)"); + + if (Pstream::parRun()) + { + writeTabbed(os, "processor"); + } + } + else + { + forAll(fieldSet_, fieldI) + { + writeTabbed(os, "min(" + fieldSet_[fieldI] + ')'); + writeTabbed(os, "max(" + fieldSet_[fieldI] + ')'); + } + } + + os << endl; +} + + // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // Foam::fieldMinMax::fieldMinMax @@ -59,33 +98,20 @@ Foam::fieldMinMax::fieldMinMax const bool loadFromFiles ) : - functionObjectFile(obr, name, typeName), - name_(name), + functionObjectState(obr, name), + functionObjectFile(obr, name, typeName, dict), obr_(obr), - active_(true), log_(true), - location_(true), + writeLocation_(true), mode_(mdMag), fieldSet_() { // Check if the available mesh is an fvMesh otherise deactivate - if (!isA(obr_)) + if (setActive()) { - active_ = false; - WarningIn - ( - "fieldMinMax::fieldMinMax" - "(" - "const word&, " - "const objectRegistry&, " - "const dictionary&, " - "const bool" - ")" - ) << "No fvMesh available, deactivating " << name_ - << endl; + read(dict); + writeFileHeader(file()); } - - read(dict); } @@ -101,8 +127,10 @@ void Foam::fieldMinMax::read(const dictionary& dict) { if (active_) { + functionObjectFile::read(dict); + log_ = dict.lookupOrDefault("log", true); - location_ = dict.lookupOrDefault("location", true); + writeLocation_ = dict.lookupOrDefault("writeLocation", true); mode_ = modeTypeNames_[dict.lookupOrDefault("mode", "magnitude")]; dict.lookup("fields") >> fieldSet_; @@ -110,46 +138,6 @@ void Foam::fieldMinMax::read(const dictionary& dict) } -void Foam::fieldMinMax::writeFileHeader(const label i) -{ - OFstream& file = this->file(); - - writeHeader(file, "Field minima and maxima"); - writeCommented(file, "Time"); - - if (location_) - { - writeTabbed(file, "field"); - - writeTabbed(file, "min"); - writeTabbed(file, "location(min)"); - - if (Pstream::parRun()) - { - writeTabbed(file, "processor"); - } - - writeTabbed(file, "max"); - writeTabbed(file, "location(max)"); - - if (Pstream::parRun()) - { - writeTabbed(file, "processor"); - } - } - else - { - forAll(fieldSet_, fieldI) - { - writeTabbed(file, "min(" + fieldSet_[fieldI] + ')'); - writeTabbed(file, "max(" + fieldSet_[fieldI] + ')'); - } - } - - file<< endl; -} - - void Foam::fieldMinMax::execute() { // Do nothing - only valid on write @@ -172,9 +160,7 @@ void Foam::fieldMinMax::write() { if (active_) { - functionObjectFile::write(); - - if (!location_) file()<< obr_.time().value(); + if (!writeLocation_) file()<< obr_.time().value(); if (log_) Info<< type() << " " << name_ << " output:" << nl; forAll(fieldSet_, fieldI) @@ -186,7 +172,7 @@ void Foam::fieldMinMax::write() calcMinMaxFields(fieldSet_[fieldI], mode_); } - if (!location_) file()<< endl; + if (!writeLocation_) file()<< endl; if (log_) Info<< endl; } } diff --git a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.H b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.H index b2246eb9b1..2c6f45e923 100644 --- a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.H +++ b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMax.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -28,7 +28,7 @@ Group grpFieldFunctionObjects Description - This function object calculates the value and location of scalar minimim + This function object calculates the value and position of scalar minimim and maximum for a list of user-specified fields. For variables with a rank greater than zero, either the min/max of a component value or the magnitude is reported. When operating in parallel, the processor owning the value @@ -41,15 +41,11 @@ Description type fieldMinMax; functionObjectLibs ("libfieldFunctionObjects.so"); ... - write yes; + writeToFile yes; log yes; - location yes; + writeLocation yes; mode magnitude; - fields - ( - U - p - ); + fields (U p); } \endverbatim @@ -57,20 +53,24 @@ Description \table Property | Description | Required | Default value type | type name: fieldMinMax | yes | - write | write min/max data to file | no | yes - log | write min/max data to standard output | no | no - location | write location of the min/max value | no | yes + writeToFile | write min/max data to file | no | yes + log | write min/max data to standard output | no | yes + writeLocation | write location of the min/max value | no | yes mode | calculation mode: magnitude or component | no | magnitude + fields | list of fields to process | yes | \endtable Output data is written to the file \/fieldMinMax.dat SeeAlso Foam::functionObject + Foam::functionObjectFile + Foam::functionObjectState Foam::OutputFilterFunctionObject SourceFiles fieldMinMax.C + fieldMinMaxTemplates.C IOfieldMinMax.H \*---------------------------------------------------------------------------*/ @@ -78,8 +78,10 @@ SourceFiles #ifndef fieldMinMax_H #define fieldMinMax_H +#include "functionObjectState.H" #include "functionObjectFile.H" #include "Switch.H" +#include "NamedEnum.H" #include "vector.H" // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // @@ -94,20 +96,24 @@ class polyMesh; class mapPolyMesh; /*---------------------------------------------------------------------------*\ - Class fieldMinMax Declaration + Class fieldMinMax Declaration \*---------------------------------------------------------------------------*/ class fieldMinMax : + public functionObjectState, public functionObjectFile { public: - enum modeType - { - mdMag, - mdCmpt - }; + // Public enumerations + + enum modeType + { + mdMag, // magnitude + mdCmpt // component + }; + protected: @@ -116,20 +122,14 @@ protected: //- Mode type names static const NamedEnum modeTypeNames_; - //- Name of this set of field min/max - // Also used as the name of the output directory - word name_; - + //- Reference to the database const objectRegistry& obr_; - //- on/off switch - bool active_; - //- Switch to send output to Info as well Switch log_; //- Switch to write location of min/max values - Switch location_; + Switch writeLocation_; //- Mode for min/max - only applicable for ranks > 0 modeType mode_; @@ -154,15 +154,16 @@ protected: const Type& maxValue ); + + //- Output file header information + virtual void writeFileHeader(Ostream& os) const; + //- Disallow default bitwise copy construct fieldMinMax(const fieldMinMax&); //- Disallow default bitwise assignment void operator=(const fieldMinMax&); - //- Output file header information - virtual void writeFileHeader(const label i); - public: @@ -189,12 +190,6 @@ public: // Member Functions - //- Return name of the set of field min/max - virtual const word& name() const - { - return name_; - } - //- Read the field min/max data virtual void read(const dictionary&); diff --git a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMaxTemplates.C b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMaxTemplates.C index 729500caef..3680134b89 100644 --- a/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMaxTemplates.C +++ b/src/postProcessing/functionObjects/field/fieldMinMax/fieldMinMaxTemplates.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -43,7 +43,7 @@ void Foam::fieldMinMax::output { OFstream& file = this->file(); - if (location_) + if (writeLocation_) { file<< obr_.time().value(); @@ -95,6 +95,15 @@ void Foam::fieldMinMax::output } if (log_) Info<< endl; + + // Write state/results information + word nameStr('(' + outputName + ')'); + this->setResult("min" + nameStr, minValue); + this->setResult("min" + nameStr + "_position", minC); + this->setResult("min" + nameStr + "_processor", minProcI); + this->setResult("max" + nameStr, maxValue); + this->setResult("max" + nameStr + "_position", maxC); + this->setResult("max" + nameStr + "_processor", maxProcI); } @@ -163,33 +172,34 @@ void Foam::fieldMinMax::calcMinMaxFields } Pstream::gatherList(minVs); + Pstream::scatterList(minVs); Pstream::gatherList(minCs); + Pstream::scatterList(minCs); Pstream::gatherList(maxVs); + Pstream::scatterList(maxVs); Pstream::gatherList(maxCs); + Pstream::scatterList(maxCs); - if (Pstream::master()) - { - label minI = findMin(minVs); - scalar minValue = minVs[minI]; - const vector& minC = minCs[minI]; + label minI = findMin(minVs); + scalar minValue = minVs[minI]; + const vector& minC = minCs[minI]; - label maxI = findMax(maxVs); - scalar maxValue = maxVs[maxI]; - const vector& maxC = maxCs[maxI]; + label maxI = findMax(maxVs); + scalar maxValue = maxVs[maxI]; + const vector& maxC = maxCs[maxI]; - output - ( - fieldName, - word("mag(" + fieldName + ")"), - minC, - maxC, - minI, - maxI, - minValue, - maxValue - ); - } + output + ( + fieldName, + word("mag(" + fieldName + ")"), + minC, + maxC, + minI, + maxI, + minValue, + maxValue + ); break; } case mdCmpt: @@ -236,33 +246,34 @@ void Foam::fieldMinMax::calcMinMaxFields } Pstream::gatherList(minVs); + Pstream::scatterList(minVs); Pstream::gatherList(minCs); + Pstream::scatterList(minCs); Pstream::gatherList(maxVs); + Pstream::scatterList(maxVs); Pstream::gatherList(maxCs); + Pstream::scatterList(maxCs); - if (Pstream::master()) - { - label minI = findMin(minVs); - Type minValue = minVs[minI]; - const vector& minC = minCs[minI]; + label minI = findMin(minVs); + Type minValue = minVs[minI]; + const vector& minC = minCs[minI]; - label maxI = findMax(maxVs); - Type maxValue = maxVs[maxI]; - const vector& maxC = maxCs[maxI]; + label maxI = findMax(maxVs); + Type maxValue = maxVs[maxI]; + const vector& maxC = maxCs[maxI]; - output - ( - fieldName, - fieldName, - minC, - maxC, - minI, - maxI, - minValue, - maxValue - ); - } + output + ( + fieldName, + fieldName, + minC, + maxC, + minI, + maxI, + minValue, + maxValue + ); break; } default: @@ -274,7 +285,8 @@ void Foam::fieldMinMax::calcMinMaxFields "const word&, " "const modeType&" ")" - ) << "Unknown min/max mode: " << modeTypeNames_[mode_] + ) + << "Unknown min/max mode: " << modeTypeNames_[mode_] << exit(FatalError); } } diff --git a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.C b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.C index 9d4b8400c8..40d3ff5bf6 100644 --- a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.C +++ b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.C @@ -79,8 +79,6 @@ void Foam::fieldValues::cellSource::setCellZoneCells() { case stCellZone: { - dict().lookup("sourceName") >> sourceName_; - label zoneId = mesh().cellZones().findZoneID(sourceName_); if (zoneId < 0) @@ -135,7 +133,8 @@ void Foam::fieldValues::cellSource::initialise(const dictionary& dict) WarningIn ( "Foam::fieldValues::cellSource::initialise(const dictionary&)" - ) << type() << " " << name_ << ": " + ) + << type() << " " << name_ << ": " << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl << " Source has no cells - deactivating" << endl; @@ -145,44 +144,46 @@ void Foam::fieldValues::cellSource::initialise(const dictionary& dict) volume_ = volume(); - Info<< type() << " " << name_ << ":" - << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl - << " total cells = " << nCells_ << nl - << " total volume = " << volume_ - << nl << endl; + if (log_) + { + Info<< type() << " " << name_ << ":" + << sourceTypeNames_[source_] << "(" << sourceName_ << "):" << nl + << " total cells = " << nCells_ << nl + << " total volume = " << volume_ + << nl << endl; + } if (dict.readIfPresent("weightField", weightFieldName_)) { - Info<< " weight field = " << weightFieldName_; + if (log_) Info << " weight field = " << weightFieldName_; } - Info<< nl << endl; + if (log_) Info << nl << endl; } -void Foam::fieldValues::cellSource::writeFileHeader(const label i) +void Foam::fieldValues::cellSource::writeFileHeader(Ostream& os) const { - writeCommented(file(), "Source : "); - file() << sourceTypeNames_[source_] << " " << sourceName_ << endl; - writeCommented(file(), "Cells : "); - file() << nCells_ << endl; - writeCommented(file(), "Volume : "); - file() << volume_ << endl; + writeHeaderValue(os, "Source", sourceTypeNames_[source_]); + writeHeaderValue(os, "Name", sourceName_); + writeHeaderValue(os, "Cells", nCells_); + writeHeaderValue(os, "Volume", volume_); + writeHeaderValue(os, "Scale factor", scaleFactor_); - writeCommented(file(), "Time"); + + writeCommented(os, "Time"); if (writeVolume_) { - file() << tab << "Volume"; + os << tab << "Volume"; } forAll(fields_, i) { - file() - << tab << operationTypeNames_[operation_] + os << tab << operationTypeNames_[operation_] << "(" << fields_[i] << ")"; } - file() << endl; + os << endl; } @@ -204,7 +205,11 @@ Foam::fieldValues::cellSource::cellSource weightFieldName_("none"), writeVolume_(dict.lookupOrDefault("writeVolume", false)) { - read(dict); + if (active_) + { + read(dict); + writeFileHeader(file()); + } } @@ -218,11 +223,11 @@ Foam::fieldValues::cellSource::~cellSource() void Foam::fieldValues::cellSource::read(const dictionary& dict) { - fieldValue::read(dict); - if (active_) { - // no additional info to read + fieldValue::read(dict); + + // No additional info to read initialise(dict); } } @@ -234,33 +239,34 @@ void Foam::fieldValues::cellSource::write() if (active_) { - if (Pstream::master()) - { - file() << obr_.time().value(); - } + file() << obr_.time().value(); + // Construct weight field. Note: zero size indicates unweighted + scalarField weightField; + if (weightFieldName_ != "none") + { + weightField = setFieldValues(weightFieldName_, true); + } + if (writeVolume_) { volume_ = volume(); - if (Pstream::master()) - { - file() << tab << volume_; - } + file() << tab << volume_; if (log_) Info<< " total volume = " << volume_ << endl; } forAll(fields_, i) { const word& fieldName = fields_[i]; - bool processed = false; + bool ok = false; - processed = processed || writeValues(fieldName); - processed = processed || writeValues(fieldName); - processed = processed || writeValues(fieldName); - processed = processed || writeValues(fieldName); - processed = processed || writeValues(fieldName); + ok = ok || writeValues(fieldName, weightField); + ok = ok || writeValues(fieldName, weightField); + ok = ok || writeValues(fieldName, weightField); + ok = ok || writeValues(fieldName, weightField); + ok = ok || writeValues(fieldName, weightField); - if (!processed) + if (!ok) { WarningIn("void Foam::fieldValues::cellSource::write()") << "Requested field " << fieldName @@ -269,10 +275,7 @@ void Foam::fieldValues::cellSource::write() } } - if (Pstream::master()) - { - file()<< endl; - } + file()<< endl; if (log_) Info<< endl; } diff --git a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.H b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.H index 42f788d884..8c59c8450d 100644 --- a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.H +++ b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSource.H @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -62,11 +62,11 @@ Description log | Write data to standard output | no | no valueOutput | Write the raw output values | yes | writeVolume | Write the volume of the cellSource | no | - source | cell source: see below | yes | - sourceName | name of cell source if required | no | - operation | operation to perform | yes | - weightField | name of field to apply weighting | no | - fields | list of fields to operate on | yes | + source | Cell source: see below | yes | + sourceName | Name of cell source if required | no | + operation | Operation to perform | yes | + weightField | Name of field to apply weighting | no | + fields | List of fields to operate on | yes | \endtable \linebreak @@ -228,7 +228,7 @@ protected: ) const; //- Output file header information - virtual void writeFileHeader(const label i); + virtual void writeFileHeader(Ostream& os) const; public: @@ -272,7 +272,11 @@ public: //- Templated helper function to output field values template - bool writeValues(const word& fieldName); + bool writeValues + ( + const word& fieldName, + const scalarField& weightField + ); //- Filter a field according to cellIds template diff --git a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSourceTemplates.C b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSourceTemplates.C index 15030ea689..f6f67d55e6 100644 --- a/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSourceTemplates.C +++ b/src/postProcessing/functionObjects/field/fieldValues/cellSource/cellSourceTemplates.C @@ -3,7 +3,7 @@ \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | \\ / A nd | Copyright (C) 2011-2015 OpenFOAM Foundation - \\/ M anipulation | + \\/ M anipulation | Copyright (C) 2015 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -87,52 +87,66 @@ Type Foam::fieldValues::cellSource::processValues { case opSum: { - result = sum(values); + result = gSum(values); break; } case opSumMag: { - result = sum(cmptMag(values)); + result = gSum(cmptMag(values)); break; } case opAverage: { - result = sum(values)/values.size(); + label n = returnReduce(values.size(), sumOp