/*---------------------------------------------------------------------------*\ ========= | \\ / F ield | OpenFOAM: The Open Source CFD Toolbox \\ / O peration | Website: https://openfoam.org \\ / A nd | Copyright (C) 2011-2026 OpenFOAM Foundation \\/ M anipulation | ------------------------------------------------------------------------------- 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 "error.H" #include "products.H" #include "VectorSpaceOp.H" #include // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // namespace Foam { // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // template inline VectorSpace::VectorSpace() {} template inline VectorSpace::VectorSpace(const Foam::zero) { VectorSpaceOp::op([](Cmpt& x) { x = Zero; }, *this); } template template inline VectorSpace::VectorSpace ( const VectorSpace& vs ) { VectorSpaceOp::op ( [](Cmpt& a, const Cmpt2& b) { a = b; }, *this, vs ); } template template inline VectorSpace::ConstBlock::ConstBlock ( const vsType& vs ) : vs_(vs) { static_assert ( vsType::nComponents >= BStart + nComponents, "Requested block size > VectorSpace size" ); } // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * // template inline direction VectorSpace::size() { return Ncmpts; } template inline const Cmpt& VectorSpace::component ( const direction d ) const { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif return v_[d]; } template inline Cmpt& VectorSpace::component ( const direction d ) { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif return v_[d]; } template inline void VectorSpace::component ( Cmpt& c, const direction d ) const { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif c = v_[d]; } template inline void VectorSpace::replace ( const direction d, const Cmpt& c ) { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif v_[d] = c; } template inline Form VectorSpace::uniform(const Cmpt& s) { Form v; VectorSpaceOp::op([&s](Cmpt& x) { x = s; }, v); return v; } template template inline const typename VectorSpace::template ConstBlock VectorSpace::block() const { return *this; } // * * * * * * * * * * * * * * * Member Operators * * * * * * * * * * * * * // template inline const Cmpt& VectorSpace::operator[] ( const direction d ) const { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif return v_[d]; } template inline Cmpt& VectorSpace::operator[] ( const direction d ) { #ifdef FULLDEBUG if (d >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif return v_[d]; } template template inline const Cmpt& VectorSpace:: ConstBlock::operator[] ( const direction i ) const { #ifdef FULLDEBUG if (i >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } #endif return vs_[BStart + i]; } template template inline const Cmpt& VectorSpace:: ConstBlock::operator() ( const direction i, const direction j ) const { #ifdef FULLDEBUG if (i >= Ncmpts) { FatalErrorInFunction << "index out of range" << abort(FatalError); } if (j != 0) { FatalErrorInFunction << "j != 0" << abort(FatalError); } #endif return vs_[BStart + i]; } template inline void VectorSpace::operator+= ( const VectorSpace& vs ) { VectorSpaceOp::op ( [](Cmpt& x, const Cmpt& y) { x += y; }, *this, vs ); } template inline void VectorSpace::operator-= ( const VectorSpace& vs ) { VectorSpaceOp::op ( [](Cmpt& x, const Cmpt& y) { x -= y; }, *this, vs ); } template inline void VectorSpace::operator=(const Foam::zero) { VectorSpaceOp::op([](Cmpt& x) { x = Zero; }, *this); } template template inline void VectorSpace::operator*= ( const Type s ) { VectorSpaceOp::op([s](Cmpt& x) { x *= s; }, *this); } template template inline void VectorSpace::operator/= ( const Type s ) { VectorSpaceOp::op([&s](Cmpt& x) { x /= s; }, *this); } // * * * * * * * * * * * * * * * Global Functions * * * * * * * * * * * * * // template inline Cmpt& setComponent ( VectorSpace& vs, const direction d ) { return vs.component(d); } template inline const Cmpt& component ( const VectorSpace& vs, const direction d ) { return vs.component(d); } // Powers of a Form // Equivalent to outer-products between the Form and itself // Form^0 = 1.0 // template // inline typename powProduct::type pow // ( // const VectorSpace&, // typename powProduct::type = // pTraits::type>::zero // ) // { // return 1.0; // } // // Form^1 = Form // template // inline typename powProduct::type pow // ( // const VectorSpace& v, // typename powProduct::type = // pTraits::type>::zero // ) // { // return static_cast(v); // } // // Form^2 = sqr(Form) // template // inline typename powProduct::type pow // ( // const VectorSpace& v, // typename powProduct::type = // pTraits::type>::zero // ) // { // return sqr(static_cast(v)); // } template inline auto magSqr ( const VectorSpace& vs ) { auto ms = magSqr(vs.v_[0]); VectorSpaceOp::op ( [&ms](const Cmpt& x) { ms += magSqr(x); }, vs ); return ms; } template inline auto mag ( const VectorSpace& vs ) { return sqrt(magSqr(static_cast(vs))); } template inline Form normalised ( const VectorSpace& vs ) { const auto magVs = mag(vs); return magVs > 0 ? vs/magVs : Zero; } template inline Form cmptMultiply ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = cmptMultiply(b, c); }, v, vs1, vs2 ); return v; } template inline Form cmptPow ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = cmptPow(b, c); }, v, vs1, vs2 ); return v; } template inline Form cmptDivide ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = cmptDivide(b, c); }, v, vs1, vs2 ); return v; } template inline Form stabilise ( const VectorSpace& vs, const Cmpt& small ) { Form v; VectorSpaceOp::op ( [&small](Cmpt& a, const Cmpt& b) { a = stabilise(b, small); }, v, vs ); return v; } template inline Cmpt cmptMax ( const VectorSpace& vs ) { Cmpt cMax = vs.v_[0]; VectorSpaceOp::op ( [&cMax](const Cmpt& c) { cMax = max(c, cMax); }, vs ); return cMax; } template inline Cmpt cmptMin ( const VectorSpace& vs ) { Cmpt cMin = vs.v_[0]; VectorSpaceOp::op ( [&cMin](const Cmpt& c) { cMin = min(c, cMin); }, vs ); return cMin; } template inline Cmpt cmptSum ( const VectorSpace& vs ) { Cmpt sum = vs.v_[0]; VectorSpaceOp::op ( [&sum](const Cmpt& c) { sum += c; }, vs ); return sum; } template inline Cmpt cmptAv ( const VectorSpace& vs ) { return cmptSum(vs)/Ncmpts; } template inline Cmpt cmptProduct ( const VectorSpace& vs ) { Cmpt product = vs.v_[0]; VectorSpaceOp::op ( [&product](const Cmpt& c) { product *= c; }, vs ); return product; } template inline Form cmptSqr ( const VectorSpace& vs ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b) { a = sqr(b); }, v, vs ); return v; } template inline Form cmptMag ( const VectorSpace& vs ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b) { a = mag(b); }, v, vs ); return v; } template inline Form max ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = max(b, c); }, v, vs1, vs2 ); return v; } template inline Form min ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = min(b, c); }, v, vs1, vs2 ); return v; } template inline Form minMod ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = minMod(b, c); }, v, vs1, vs2 ); return v; } template < class Form1, class Cmpt1, direction Ncmpts1, class Form2, class Cmpt2, direction Ncmpts2 > inline typename innerProduct::type dot ( const VectorSpace& t1, const VectorSpace& t2 ) { return static_cast(t1) & static_cast(t2); } template inline direction findMax(const VectorSpace& vs) { direction index = 0; for (direction i=1; i vs[i] ? index : i; } return index; } template inline direction findMin(const VectorSpace& vs) { direction index = 0; for (direction i=1; i inline Form operator- ( const VectorSpace& vs ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b) { a = -b; }, v, vs ); return v; } template inline Form operator+ ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = b + c; }, v, vs1, vs2 ); return v; } template inline Form operator- ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Form v; VectorSpaceOp::op ( [](Cmpt& a, const Cmpt& b, const Cmpt& c) { a = b - c; }, v, vs1, vs2 ); return v; } template < class Form, class Cmpt, direction Ncmpts, class Type, class Enable = EnableRank0 > inline Form operator* ( Type s, const VectorSpace& vs ) { Form v; VectorSpaceOp::op ( [&s](Cmpt& a, const Cmpt& b) { a = s*b; }, v, vs ); return v; } template < class Form, class Cmpt, direction Ncmpts, class Type, class Enable = EnableRank0 > inline Form operator* ( const VectorSpace& vs, Type s ) { Form v; VectorSpaceOp::op ( [&s](Cmpt& a, const Cmpt& b) { a = b*s; }, v, vs ); return v; } template < class Form, class Cmpt, direction Ncmpts, class Type, class Enable = EnableRank0 > inline Form operator/ ( const VectorSpace& vs, Type s ) { Form v; VectorSpaceOp::op ( [&s](Cmpt& a, const Cmpt& b) { a = b/s; }, v, vs ); return v; } template inline Cmpt operator&& ( const VectorSpace& vs1, const VectorSpace& vs2 ) { Cmpt ddProd = vs1.v_[0]*vs2.v_[0]; VectorSpaceOp::op ( [&ddProd](const Cmpt& a, const Cmpt& b) { ddProd += a*b; }, vs1, vs2 ); return ddProd; } template inline bool operator== ( const VectorSpace& vs1, const VectorSpace& vs2 ) { bool eq = true; for (direction i=0; i inline bool operator!= ( const VectorSpace& vs1, const VectorSpace& vs2 ) { return !(vs1 == vs2); } template inline bool operator> ( const VectorSpace& vs1, const VectorSpace& vs2 ) { bool gt = true; for (direction i=0; i vs2.v_[i])) break; } return gt; } template inline bool operator< ( const VectorSpace& vs1, const VectorSpace& vs2 ) { bool lt = true; for (direction i=0; i inline bool operator>= ( const VectorSpace& vs1, const VectorSpace& vs2 ) { return !(vs1 < vs2); } template inline bool operator<= ( const VectorSpace& vs1, const VectorSpace& vs2 ) { return !(vs1 > vs2); } // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // } // End namespace Foam // ************************************************************************* //