diff --git a/applications/test/BinSum/Make/files b/applications/test/BinSum/Make/files new file mode 100644 index 0000000000000000000000000000000000000000..34d265d30c766c4e89c21a38a6efb5b701b7835d --- /dev/null +++ b/applications/test/BinSum/Make/files @@ -0,0 +1,3 @@ +Test-BinSum.C + +EXE = $(FOAM_USER_APPBIN)/Test-BinSum diff --git a/applications/test/BinSum/Make/options b/applications/test/BinSum/Make/options new file mode 100644 index 0000000000000000000000000000000000000000..6a9e9810b3d5ce6684bdaf03143933480ff45e42 --- /dev/null +++ b/applications/test/BinSum/Make/options @@ -0,0 +1,2 @@ +/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */ +/* EXE_LIBS = -lfiniteVolume */ diff --git a/applications/test/BinSum/Test-BinSum.C b/applications/test/BinSum/Test-BinSum.C new file mode 100644 index 0000000000000000000000000000000000000000..fb768f80c6769c1c33850f5f1dea0af987f2ac66 --- /dev/null +++ b/applications/test/BinSum/Test-BinSum.C @@ -0,0 +1,74 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2012 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 <http://www.gnu.org/licenses/>. + +Application + Test-BinSum + +Description + Test BinSum container + +\*---------------------------------------------------------------------------*/ + +#include "BinSum.H" +#include "IOstreams.H" +#include "Random.H" +#include "scalarField.H" + +using namespace Foam; + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +int main(int argc, char *argv[]) +{ + Random rndGen(0); + + scalarField samples(10000000); + forAll(samples, i) + { + samples[i] = rndGen.scalar01(); + } + + const scalar min = 0; + const scalar max = 1; + const scalar delta = 0.1; + + BinSum<scalar, scalarField> count(min, max, delta); + BinSum<scalar, scalarField> sum(min, max, delta); + + forAll(samples, i) + { + count.add(samples[i], 1); + sum.add(samples[i], samples[i]); + } + + Info<< "sum : " << sum << endl; + Info<< "count : " << count << endl; + Info<< "average: " << sum/count << endl; + + Info<< "End\n" << endl; + + return 0; +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/BinSum/BinSum.C b/src/OpenFOAM/containers/Lists/BinSum/BinSum.C new file mode 100644 index 0000000000000000000000000000000000000000..6854597644a9a06d5fe8e86ea9812463b0eaa4d4 --- /dev/null +++ b/src/OpenFOAM/containers/Lists/BinSum/BinSum.C @@ -0,0 +1,73 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2012 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 <http://www.gnu.org/licenses/>. + +\*---------------------------------------------------------------------------*/ + +#include "BinSum.H" + +// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * // + +template<class IndexType, class List, class CombineOp> +Foam::BinSum<IndexType, List, CombineOp>::BinSum +( + const IndexType min, + const IndexType max, + const IndexType delta +) +: + List(ceil((max-min)/delta), pTraits<typename List::value_type>::zero), + min_(min), + max_(max), + delta_(delta), + lowSum_(pTraits<typename List::value_type>::zero), + highSum_(pTraits<typename List::value_type>::zero) +{} + + +// * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * * // + +template<class IndexType, class List, class CombineOp> +void Foam::BinSum<IndexType, List, CombineOp>::add +( + const IndexType& indexVal, + const typename List::const_reference val, + const CombineOp& cop +) +{ + if (indexVal < min_) + { + cop(lowSum_, val); + } + else if (indexVal >= max_) + { + cop(highSum_, val); + } + else + { + label index = (indexVal-min_)/delta_; + cop(this->operator[](index), val); + } +} + + +// ************************************************************************* // diff --git a/src/OpenFOAM/containers/Lists/BinSum/BinSum.H b/src/OpenFOAM/containers/Lists/BinSum/BinSum.H new file mode 100644 index 0000000000000000000000000000000000000000..6e0195a780fc850840ef881f7690d63706fea000 --- /dev/null +++ b/src/OpenFOAM/containers/Lists/BinSum/BinSum.H @@ -0,0 +1,131 @@ +/*---------------------------------------------------------------------------*\ + ========= | + \\ / F ield | OpenFOAM: The Open Source CFD Toolbox + \\ / O peration | + \\ / A nd | Copyright (C) 2012 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 <http://www.gnu.org/licenses/>. + +Class + Foam::BinSum + +Description + Sums into bins + +SourceFiles + BinSum.C + +\*---------------------------------------------------------------------------*/ + +#ifndef BinSum_H +#define BinSum_H + +#include "ops.H" + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +namespace Foam +{ + + +/*---------------------------------------------------------------------------*\ + Class BinSum Declaration +\*---------------------------------------------------------------------------*/ + +template +< + class IndexType, + class List, + class CombineOp = plusEqOp<typename List::value_type> +> +class BinSum +: + public List +{ + // Private data + + const IndexType min_; + + const IndexType max_; + + const IndexType delta_; + + + //- Sum < lowest bin + typename List::value_type lowSum_; + + //- Sum of >= highest bin + typename List::value_type highSum_; + +public: + + // Constructors + + //- Construct given min, max, delta + BinSum + ( + const IndexType min, + const IndexType max, + const IndexType delta + ); + + + // Access + + //- Return the delta + inline IndexType delta() const + { + return delta_; + } + + //- Return the sum of all added elements < min + inline const IndexType& lowSum() const + { + return lowSum_; + } + + //- Return the sum of all added elements >= max + inline const IndexType& highSum() const + { + return highSum_; + } + + void add + ( + const IndexType& indexVal, + const typename List::const_reference val, + const CombineOp& cop = plusEqOp<typename List::value_type>() + ); +}; + + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +} // End namespace Foam + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#ifdef NoRepository +# include "BinSum.C" +#endif + +// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * // + +#endif + +// ************************************************************************* //