diff --git a/applications/test/exprValue1/Test-exprValue1.cxx b/applications/test/exprValue1/Test-exprValue1.cxx index df77fd9956..2a1f41564c 100644 --- a/applications/test/exprValue1/Test-exprValue1.cxx +++ b/applications/test/exprValue1/Test-exprValue1.cxx @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021-2023 OpenCFD Ltd. + Copyright (C) 2021-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM, distributed under GPL-3.0-or-later. @@ -21,6 +21,8 @@ Description #include "argList.H" #include "IOstreams.H" #include "ITstream.H" +#include "OTstream.H" +#include "SpanStream.H" #include "exprValue.H" #include "Pstream.H" @@ -34,6 +36,41 @@ void printInfo(const expressions::exprValue& val) } +void write_read(const expressions::exprValue& val) +{ + OCharStream os; + os << val; + + ISpanStream is(os.view()); + expressions::exprValue val2; + is >> val2; + + Pout<< "wrote " << os.count() << " chars: " << os.str() << nl; + + printInfo(val); + printInfo(val2); + Pout<< "====" << nl; +} + + +tokenList tokens_of(const expressions::exprValue& val) +{ + OTstream toks; + toks << val; + + Pout<< "val with tokens: "; + toks.writeList(Pout, 0) << nl; + + for (const auto& t : toks) + { + Pout<< " " << t.info() << nl; + } + Pout<< nl; + + return toks; +} + + expressions::exprValue tryParse(const std::string& str) { expressions::exprValue val, val2; @@ -90,6 +127,7 @@ int main(int argc, char *argv[]) { expressions::exprValue value; + tokenList toks; Info<< "exprValue" << " sizeof:" << value.size_bytes() @@ -99,21 +137,31 @@ int main(int argc, char *argv[]) // Nothing printInfo(value); + toks = tokens_of(value); + write_read(value); value.set(scalar(100)); - printInfo(value); + printInfo(value); write_read(value); toks = tokens_of(value); + + value.set(scalar(100.01)); + printInfo(value); write_read(value); toks = tokens_of(value); value.set(vector(1,2,3)); - printInfo(value); + printInfo(value); write_read(value); toks = tokens_of(value); value = vector(4,5,6); - printInfo(value); + printInfo(value); write_read(value); toks = tokens_of(value); value = Zero; - printInfo(value); + printInfo(value); write_read(value); toks = tokens_of(value); value.clear(); + printInfo(value); write_read(value); toks = tokens_of(value); + + value.set(true); + printInfo(value); + printInfo(value); write_read(value); toks = tokens_of(value); if (UPstream::parRun()) { diff --git a/applications/test/exprValue2/Make/files b/applications/test/exprValue2/Make/files index c49a9ce705..42c8232f68 100644 --- a/applications/test/exprValue2/Make/files +++ b/applications/test/exprValue2/Make/files @@ -1,5 +1,3 @@ -exprValueFieldTag.cxx - Test-exprValue2.cxx EXE = $(FOAM_USER_APPBIN)/Test-exprValue2 diff --git a/applications/test/exprValue2/Test-exprValue2.cxx b/applications/test/exprValue2/Test-exprValue2.cxx index 93897ae321..49f75f24b4 100644 --- a/applications/test/exprValue2/Test-exprValue2.cxx +++ b/applications/test/exprValue2/Test-exprValue2.cxx @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2023 OpenCFD Ltd. + Copyright (C) 2023-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -36,7 +36,6 @@ Description #include "vectorField.H" #include "DynamicList.H" #include "Random.H" -#include "exprValue.H" #include "exprValueFieldTag.H" using namespace Foam; @@ -61,27 +60,81 @@ int main(int argc, char *argv[]) #include "setRootCase.H" + DynamicList allTags; + { scalarField fld1(20); scalarField fld2a(20, Zero); scalarField fld2b(10, 3.10); scalarField fld3; - forAll(fld1, i) + for (auto& val : fld1) { - fld1[i] = rnd.position(0, 20); + val = rnd.position(0, 20); + } + + if (!UPstream::master()) + { + fld2b.resize(5); + fld2b *= 2; } fieldTag tag1(fld1.begin(), fld1.end()); fieldTag tag2a(fld2a.begin(), fld2a.end()); fieldTag tag2b(fld2b.begin(), fld2b.end()); fieldTag tag3(fld3.begin(), fld3.end()); + fieldTag tag4(fld3.begin(), fld3.end()); printInfo(tag1) << nl; printInfo(tag2a) << nl; printInfo(tag2b) << nl; printInfo(tag3) << nl; + { + Pout<< "Test reduce" << nl; + + fieldTag work(fld2b.begin(), fld2b.end()); + + Pout<< "Before" << nl; + printInfo(work) << nl; + + work.reduce(); + + Pout<< "After" << nl; + printInfo(work) << nl; + Pout<< "====" << nl; + } + + allTags.clear(); + allTags.push_back(tag1); + allTags.push_back(tag2a); + allTags.push_back(tag2b); + allTags.push_back(tag3); + allTags.push_back(tag4); + allTags.push_back(fieldTag::make_empty()); + + + // Add some other types + { + vectorField vfld2a(20, vector::uniform(1.23)); + + allTags.emplace_back + ( + vfld2a.begin(), + vfld2a.end() + ); + allTags.emplace_back(vector(1.01, 2.02, 3.03)); + allTags.emplace_back(12.4); + + allTags.emplace_back().set_value(vector::uniform(2.0)); + allTags.back().set_empty(); + } + Info<< "all tags: " << allTags << nl; + + Foam::sort(allTags); + + Info<< "sorted: " << allTags << nl; + fieldTag result; result = fieldTag::combineOp{}(tag1, tag2a); diff --git a/applications/test/exprValue2/exprValueFieldTag.cxx b/applications/test/exprValue2/exprValueFieldTag.cxx deleted file mode 100644 index e30226107b..0000000000 --- a/applications/test/exprValue2/exprValueFieldTag.cxx +++ /dev/null @@ -1,160 +0,0 @@ -/*---------------------------------------------------------------------------*\ - ========= | - \\ / F ield | OpenFOAM: The Open Source CFD Toolbox - \\ / O peration | - \\ / A nd | www.openfoam.com - \\/ M anipulation | -------------------------------------------------------------------------------- - Copyright (C) 2023 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 "exprValueFieldTag.H" - -// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // - -bool Foam::expressions::exprValueFieldTag::empty() const noexcept -{ - return - ( - uniformity_ == Foam::Detail::ListPolicy::uniformity::EMPTY - ); -} - - -bool Foam::expressions::exprValueFieldTag::is_uniform() const noexcept -{ - return - ( - uniformity_ == Foam::Detail::ListPolicy::uniformity::UNIFORM - ); -} - - -bool Foam::expressions::exprValueFieldTag::is_nonuniform() const noexcept -{ - return - ( - uniformity_ == Foam::Detail::ListPolicy::uniformity::NONUNIFORM - ); -} - - -bool Foam::expressions::exprValueFieldTag::equal -( - const exprValueFieldTag& rhs -) const -{ - return (value_ == rhs.value_); -} - - -void Foam::expressions::exprValueFieldTag::set_nouniform() noexcept -{ - uniformity_ = Foam::Detail::ListPolicy::uniformity::NONUNIFORM; - value_ = Foam::zero{}; -} - - -void Foam::expressions::exprValueFieldTag::combine -( - const exprValueFieldTag& b -) -{ - if (b.empty()) - { - // no-op - return; - } - - exprValueFieldTag& a = *this; - - if (a.empty()) - { - a = b; - } - else if (a.is_nonuniform()) - { - // Already non-uniform/mixed - // a.uniformity_ |= b.uniformity_; - - a.value_ = Foam::zero{}; - } - else if (a.is_uniform() && b.is_uniform()) - { - // Both are uniform, but are they the same value? - if (!a.equal(b)) - { - a.set_nouniform(); - } - } -} - - -// * * * * * * * * * * * * * * * IOstream Operators * * * * * * * * * * * * // - -void Foam::expressions::exprValueFieldTag::read(Istream& is) -{ - label uniformTag; - - is >> uniformTag; - uniformity_ = int(uniformTag); - value_.read(is); -} - - -void Foam::expressions::exprValueFieldTag::write(Ostream& os) const -{ - os << label(uniformity_); - value_.write(os, false); // No pruning -} - - -void Foam::expressions::exprValueFieldTag::print(Ostream& os) const -{ - os << "{ uniform:" - << label(uniformity_) - << " type:" << label(value_.typeCode()) - << " value: " << value_ << " }"; -} - - -Foam::Istream& Foam::operator>> -( - Istream& is, - expressions::exprValueFieldTag& tag -) -{ - tag.read(is); - return is; -} - - -Foam::Ostream& Foam::operator<< -( - Ostream& os, - const expressions::exprValueFieldTag& tag -) -{ - tag.write(os); - return os; -} - - -// ************************************************************************* // diff --git a/src/OpenFOAM/Make/files b/src/OpenFOAM/Make/files index 3c99495537..f8df675d93 100644 --- a/src/OpenFOAM/Make/files +++ b/src/OpenFOAM/Make/files @@ -185,6 +185,7 @@ $(expr)/scanToken/exprScanToken.C $(expr)/traits/exprTraits.C $(expr)/value/exprValue.C +$(expr)/value/exprValueFieldTag.C $(expr)/exprDriver/exprDriver.C $(expr)/exprDriver/exprDriverFields.C diff --git a/src/OpenFOAM/expressions/exprResult/exprResult.H b/src/OpenFOAM/expressions/exprResult/exprResult.H index cd7f3d6177..e712e1c440 100644 --- a/src/OpenFOAM/expressions/exprResult/exprResult.H +++ b/src/OpenFOAM/expressions/exprResult/exprResult.H @@ -146,11 +146,11 @@ class exprResult //- Type-checked determination of centre value (min/max) // \return True if the type check was satisfied template - bool setAverageValueChecked(const bool parRun = Pstream::parRun()); + bool setAverageValueChecked(const bool parRun = UPstream::parRun()); //- Type-checked determination of average bool value // \return True if the type check was satisfied - bool setAverageValueCheckedBool(const bool parRun = Pstream::parRun()); + bool setAverageValueCheckedBool(const bool parRun = UPstream::parRun()); //- Type-checked copy of field // \return True if the type check was satisfied @@ -385,7 +385,7 @@ public: //- Test if field corresponds to a single-value and thus uniform. // Uses field min/max to establish uniformity. // Test afterwards with isUniform() - void testIfSingleValue(const bool parRun = Pstream::parRun()); + void testIfSingleValue(const bool parRun = UPstream::parRun()); // Set results @@ -437,7 +437,7 @@ public: ( const label size, const bool noWarn, - const bool parRun = Pstream::parRun() + const bool parRun = UPstream::parRun() ) const; //- Get a reduced result diff --git a/src/OpenFOAM/expressions/value/exprValue.C b/src/OpenFOAM/expressions/value/exprValue.C index bfddba1137..7272979d2c 100644 --- a/src/OpenFOAM/expressions/value/exprValue.C +++ b/src/OpenFOAM/expressions/value/exprValue.C @@ -5,7 +5,7 @@ \\ / A nd | www.openfoam.com \\/ M anipulation | ------------------------------------------------------------------------------- - Copyright (C) 2021-2023 OpenCFD Ltd. + Copyright (C) 2021-2024 OpenCFD Ltd. ------------------------------------------------------------------------------- License This file is part of OpenFOAM. @@ -26,24 +26,42 @@ License \*---------------------------------------------------------------------------*/ #include "exprValue.H" +#include "error.H" #include "ITstream.H" #include "Switch.H" #include // For memcpy, memset +// * * * * * * * * * * * * * * * * Details * * * * * * * * * * * * * * * * // + +void Foam::expressions::Detail::exprValueUnion::notSpecialized +( + const std::string& msg +) noexcept +{ + FatalErrorInFunction + << "non-specialized: " << msg.c_str() << endl + << abort(FatalError); +} + + // * * * * * * * * * * * * * * * Local Functions * * * * * * * * * * * * * * // namespace Foam { +#if 0 +// General way to add tokens for VectorSpace types +// (caller excludes none/invalid) template -static void fillTokens(const Type& val, tokenList& toks) +static void addTokens(tokenList& toks, const Type& val) { const direction nCmpt = pTraits::nComponents; const direction nParen = 2*(pTraits::rank || (nCmpt > 1) ? 1 : 0); - toks.resize_nocopy(nCmpt + nParen); + const label nOld = toks.size(); + toks.resize(nOld + label(nCmpt + nParen)); - auto iter = toks.begin(); + auto iter = toks.begin(nOld); if (nParen) { @@ -67,28 +85,57 @@ static void fillTokens(const Type& val, tokenList& toks) //- Specialized for bool template<> -void fillTokens(const bool& val, tokenList& toks) +void addTokens(tokenList& toks, const bool& val) { - toks.resize_nocopy(1); - toks.front() = token::boolean(val); + toks.emplace_back() = token::boolean(val); } //- Specialized for label template<> -void fillTokens