diff --git a/applications/test/dictionary/testDictEval5 b/applications/test/dictionary/testDictEval5 new file mode 100644 index 0000000000..535a100bc6 --- /dev/null +++ b/applications/test/dictionary/testDictEval5 @@ -0,0 +1,39 @@ +/*--------------------------------*- C++ -*----------------------------------*\ +| ========= | | +| \\ / F ield | OpenFOAM: The Open Source CFD Toolbox | +| \\ / O peration | Version: v2306 | +| \\ / A nd | Website: www.openfoam.com | +| \\/ M anipulation | | +\*---------------------------------------------------------------------------*/ +FoamFile +{ + version 2.0; + format ascii; + class dictionary; + object dictionary; +} +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +// Test evaluation with small values + +dp #eval{ sqrt(3./2.) *1e-3}; +a #eval{ 2/sqrt(2.)*$dp }; +A_square #eval{ $a*$a }; +A_inlet #eval{ $A_square-2*degToRad(180)*$dp*$dp*0.25 }; +Q $A_inlet; +Qerror #eval{$A_inlet}; +e #eval{0.2526944494428081}; +Uin #eval{ $Q/($A_square*$e) }; + +// Bypass + +alt_dp #eval{ sqrt(3./2.) *1e-3}; +alt_a #eval{ 2/sqrt(2.)*$[alt_dp] }; +alt_A_square #eval{ $[alt_a]*$[alt_a] }; +alt_A_inlet #eval{ $[alt_A_square]-2*degToRad(180)*$[alt_dp]*$[alt_dp]*0.25 }; +alt_Q $alt_A_inlet; +alt_Qerror #eval{ $[alt_A_inlet] }; +alt_e #eval{0.2526944494428081}; +alt_Uin #eval{ $[alt_Q]/($[alt_A_square]*$[alt_e]) }; + +// ************************************************************************* // diff --git a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C index c779dba4de..884cffe2fd 100644 --- a/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C +++ b/src/OpenFOAM/db/IOstreams/Tstreams/ITstream.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2015 OpenFOAM Foundation - Copyright (C) 2017-2022 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -287,11 +287,11 @@ void Foam::ITstream::print(Ostream& os) const } else { - os << toks.first().lineNumber(); + os << toks.front().lineNumber(); - if (toks.first().lineNumber() < toks.last().lineNumber()) + if (toks.front().lineNumber() < toks.back().lineNumber()) { - os << '-' << toks.last().lineNumber(); + os << '-' << toks.back().lineNumber(); } } os << ", "; @@ -302,17 +302,22 @@ void Foam::ITstream::print(Ostream& os) const std::string Foam::ITstream::toString() const { - // NOTE: may wish to have special handling if there is a single token - // and it is already a string or word + const tokenList& toks = *this; + const label nToks = toks.size(); + + if (nToks == 1 && toks.front().isStringType()) + { + // Already a string-type (WORD, STRING, ...). Just copy. + return toks.front().stringToken(); + } OStringStream buf; - unsigned i = 0; - for (const token& tok : *this) + buf.precision(16); // Some reasonably high precision + bool addSpace = false; // Separate from previous token with a space + for (const token& tok : toks) { - if (i++) - { - buf << ' '; - } + if (addSpace) buf << token::SPACE; + addSpace = true; buf << tok; } @@ -345,7 +350,7 @@ void Foam::ITstream::seek(label pos) if (nToks) { - lineNumber_ = toks.first().lineNumber(); + lineNumber_ = toks.front().lineNumber(); } setOpened(); @@ -358,7 +363,7 @@ void Foam::ITstream::seek(label pos) if (nToks) { - lineNumber_ = toks.last().lineNumber(); + lineNumber_ = toks.back().lineNumber(); } setEof(); @@ -461,7 +466,7 @@ Foam::Istream& Foam::ITstream::read(token& tok) if (nToks) { - tok.lineNumber(toks.last().lineNumber()); + tok.lineNumber(toks.back().lineNumber()); } else { diff --git a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C index cf3b0734de..35e854bde7 100644 --- a/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C +++ b/src/OpenFOAM/db/dictionary/functionEntries/calcEntry/calcEntry.C @@ -87,13 +87,14 @@ Foam::string Foam::functionEntries::calcEntry::evaluate dictionary codeDict(parentDict, codeSubDict); // Use function to write stream - OStringStream os(is.format()); + OStringStream buf(is.format()); + buf.precision(16); // Some reasonably high precision streamingFunctionType function = getFunction(parentDict, codeDict); - (*function)(os, parentDict); + (*function)(buf, parentDict); // Return evaluated content as string - return os.str(); + return buf.str(); } diff --git a/src/OpenFOAM/expressions/exprEntry/expressionEntry.C b/src/OpenFOAM/expressions/exprEntry/expressionEntry.C index 7a2158e7d7..17921cd418 100644 --- a/src/OpenFOAM/expressions/exprEntry/expressionEntry.C +++ b/src/OpenFOAM/expressions/exprEntry/expressionEntry.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2014-2018 Bernhard Gschaider - Copyright (C) 2019-2021 OpenCFD Ltd. + Copyright (C) 2019-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -238,18 +238,9 @@ void Foam::exprTools::expressionEntry::inplaceExpand if (castTo.empty()) { - // Serialized with spaces + // Serialized with spaces - fails for non-primitiveEntry ITstream& its = eptr->stream(); - - if (its.size() == 1 && its[0].isStringType()) - { - // Already a string-type (WORD, STRING, ...). Just copy. - varValue = its[0].stringToken(); - } - else - { - varValue = its.toString(); - } + varValue = its.toString(); } else { diff --git a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C index b6187ab323..5413326a10 100644 --- a/src/OpenFOAM/primitives/strings/stringOps/stringOps.C +++ b/src/OpenFOAM/primitives/strings/stringOps/stringOps.C @@ -6,7 +6,7 @@ \\/ M anipulation | ------------------------------------------------------------------------------- Copyright (C) 2011-2016 OpenFOAM Foundation - Copyright (C) 2017-2021 OpenCFD Ltd. + Copyright (C) 2017-2023 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -231,33 +231,32 @@ static inline std::string entryToString if (eptr) { - OStringStream buf; - // Force floating point numbers to be printed with at least - // some decimal digits. - buf << fixed; - buf.precision(IOstream::defaultPrecision()); + // Note, for OpenFOAM-v2212 and earlier: used 'fixed' notation + // to force floating point numbers to be printed with at least + // some decimal digits. However, in the meantime we are more + // flexible with handling float/int input so remove this constraint. - if (allowSubDict && eptr->isDict()) + if (eptr->isDict()) { - eptr->dict().write(buf, false); - str = buf.str(); - } - else - { - // Fail for non-primitiveEntry - const auto& pe = dynamicCast(*eptr); - - if (pe.size() == 1 && pe[0].isStringType()) + if (allowSubDict) { - // Already a string-type (WORD, STRING, ...). Just copy. - str = pe[0].stringToken(); + OStringStream buf; + buf.precision(16); // Some reasonably high precision + + eptr->dict().write(buf, false); + str = buf.str(); } else { - pe.write(buf, true); - str = buf.str(); + // Ignore silently... } } + else + { + // Serialized with spaces (primitiveEntry) + ITstream& its = eptr->stream(); + return its.toString(); + } } return str;