diff --git a/src/OpenFOAM/expressions/exprResult/exprResult.C b/src/OpenFOAM/expressions/exprResult/exprResult.C index b1f93df85594bd1adc283792585f66af59e55f12..2b43da245fc434a891b4f46536dfd282c148ecf4 100644 --- a/src/OpenFOAM/expressions/exprResult/exprResult.C +++ b/src/OpenFOAM/expressions/exprResult/exprResult.C @@ -624,21 +624,7 @@ void Foam::expressions::exprResult::writeDict os.writeEntryIfDifferent<Switch>("isPointValue", false, isPointData_); os.writeEntry<Switch>("isSingleValue", isUniform_); - const bool ok = - ( - writeValueFieldChecked<scalar>(os) - || writeValueFieldChecked<vector>(os) - || writeValueFieldChecked<tensor>(os) - || writeValueFieldChecked<symmTensor>(os) - || writeValueFieldChecked<sphericalTensor>(os) - || writeValueFieldChecked<bool>(os) - ); - - if (!ok) - { - WarningInFunction - << "Unknown data type " << valType_ << endl; - } + this->writeField(os, "value"); } if (subDict) @@ -650,6 +636,38 @@ void Foam::expressions::exprResult::writeDict } +void Foam::expressions::exprResult::writeField +( + Ostream& os, + const word& keyword +) const +{ + // const auto oldFmt = os.format(IOstream::ASCII); + + DebugInFunction + << Foam::name(this) << nl + << "Format: " + << IOstreamOption::formatNames[os.format()] << nl; + + const bool ok = + ( + writeFieldChecked<scalar>(keyword, os) + || writeFieldChecked<vector>(keyword, os) + || writeFieldChecked<tensor>(keyword, os) + || writeFieldChecked<symmTensor>(keyword, os) + || writeFieldChecked<sphericalTensor>(keyword, os) + || writeFieldChecked<label>(keyword, os) + || writeFieldChecked<bool>(keyword, os) + ); + + if (!ok) + { + WarningInFunction + << "Unknown data type " << valType_ << endl; + } +} + + void Foam::expressions::exprResult::writeValue ( Ostream& os diff --git a/src/OpenFOAM/expressions/exprResult/exprResult.H b/src/OpenFOAM/expressions/exprResult/exprResult.H index 3c6f9299dbf263dcf1777f1103deba5b4725bd22..7e66ceaef6a22c5ee8da3d5d90d38592f0dc855b 100644 --- a/src/OpenFOAM/expressions/exprResult/exprResult.H +++ b/src/OpenFOAM/expressions/exprResult/exprResult.H @@ -214,10 +214,11 @@ class exprResult template<class Type> bool writeSingleValueChecked(Ostream& os) const; - //- Type-checked writing of "value" field entry + //- Type-checked writing field as entry (if keyword is non-empty) + //- or as plain field (if keyword is empty) // \return True if the type check was satisfied template<class Type> - bool writeValueFieldChecked(Ostream& os) const; + bool writeFieldChecked(const word& keyword, Ostream& os) const; //- Type-checked forwarding to Field::writeEntry // \return True if the type check was satisfied @@ -516,6 +517,9 @@ public: //- Write entry as dictionary contents void writeDict(Ostream& os, const bool subDict=true) const; + //- Write the field, optionally as an entry + void writeField(Ostream& os, const word& keyword = "") const; + //- Write the single value, or the first value from field void writeValue(Ostream& os) const; diff --git a/src/OpenFOAM/expressions/exprResult/exprResultI.H b/src/OpenFOAM/expressions/exprResult/exprResultI.H index 27fb817e82ecf007f105ab545dbb3ae1bf6f940f..a8246355d4a5220a7db7d87b3562cf31ea530d82 100644 --- a/src/OpenFOAM/expressions/exprResult/exprResultI.H +++ b/src/OpenFOAM/expressions/exprResult/exprResultI.H @@ -5,8 +5,8 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2012-2018 Bernhard Gschaider <bgschaid@hfd-research.com> - Copyright (C) 2019-2020 OpenCFD Ltd. + Copyright (C) 2012-2018 Bernhard Gschaider + Copyright (C) 2019-2021 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -496,7 +496,11 @@ bool Foam::expressions::exprResult::writeSingleValueChecked(Ostream& os) const template<class Type> -bool Foam::expressions::exprResult::writeValueFieldChecked(Ostream& os) const +bool Foam::expressions::exprResult::writeFieldChecked +( + const word& keyword, + Ostream& os +) const { if (!isType<Type>()) { @@ -508,26 +512,46 @@ bool Foam::expressions::exprResult::writeValueFieldChecked(Ostream& os) const if (isUniform_) { const Type& val = single_.get<Type>(); - os.writeEntry("value", val); + if (keyword.empty()) + { + os << val; + } + else + { + os.writeEntry(keyword, val); + } } else { - // Zero-sized - const Field<Type> fld; - fld.writeEntry("value", os); + // Zero-sized - could write nothing, or a zero value + if (keyword.empty()) + { + os << pTraits<Type>::zero; + } + else + { + Field<Type>().writeEntry(keyword, os); + } } } else { const Field<Type>& fld = *static_cast<const Field<Type>*>(fieldPtr_); - if (isUniform_) + if (keyword.empty()) { - os.writeEntry("value", fld.first()); + os << fld; } else { - fld.writeEntry("value", os); + if (isUniform_) + { + os.writeEntry(keyword, fld.first()); + } + else + { + fld.writeEntry(keyword, os); + } } }