mirror of
https://develop.openfoam.com/Development/openfoam.git
synced 2025-11-28 03:28:01 +00:00
ENH: BinSum: new summing container. Replacement for uniform-binned Histograms
This commit is contained in:
3
applications/test/BinSum/Make/files
Normal file
3
applications/test/BinSum/Make/files
Normal file
@ -0,0 +1,3 @@
|
||||
Test-BinSum.C
|
||||
|
||||
EXE = $(FOAM_USER_APPBIN)/Test-BinSum
|
||||
2
applications/test/BinSum/Make/options
Normal file
2
applications/test/BinSum/Make/options
Normal file
@ -0,0 +1,2 @@
|
||||
/* EXE_INC = -I$(LIB_SRC)/cfdTools/include */
|
||||
/* EXE_LIBS = -lfiniteVolume */
|
||||
74
applications/test/BinSum/Test-BinSum.C
Normal file
74
applications/test/BinSum/Test-BinSum.C
Normal file
@ -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;
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
73
src/OpenFOAM/containers/Lists/BinSum/BinSum.C
Normal file
73
src/OpenFOAM/containers/Lists/BinSum/BinSum.C
Normal file
@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// ************************************************************************* //
|
||||
131
src/OpenFOAM/containers/Lists/BinSum/BinSum.H
Normal file
131
src/OpenFOAM/containers/Lists/BinSum/BinSum.H
Normal file
@ -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
|
||||
|
||||
// ************************************************************************* //
|
||||
Reference in New Issue
Block a user