38 List<List<scalar>>(
pTraits<Type>::nComponents),
40 listStarts_(
pTraits<Type>::nComponents, 0)
49 listStarts_(
pTraits<Type>::nComponents, 0)
56 List<List<scalar>>(static_cast<const List<List<scalar>>&>(d)),
57 binWidth_(d.binWidth()),
76 scalar sumOfWeights = 0.0;
78 forAll(cmptDistribution, i)
80 sumOfWeights += cmptDistribution[i];
90 return identity((*
this)[cmpt].
size(), listStarts_[cmpt]);
103 if (cmptDistribution.
empty())
107 cmptDistribution.
setSize(2, 0.0);
109 listStarts_[cmpt] =
n;
114 label listIndex = -1;
116 label& listStart = listStarts_[cmpt];
118 label testIndex =
n - listStart;
125 List<scalar> newCmptDistribution(2*cmptDistribution.
size(),
Zero);
127 label sOld = cmptDistribution.
size();
129 forAll(cmptDistribution, i)
131 newCmptDistribution[i + sOld] = cmptDistribution[i];
134 cmptDistribution = newCmptDistribution;
139 listIndex = index(cmpt,
n);
141 else if (testIndex > cmptDistribution.
size() - 1)
145 cmptDistribution.
setSize(2*cmptDistribution.
size(), 0.0);
149 listIndex = index(cmpt,
n);
153 listIndex =
n - listStart;
166 const List<scalar>& cmptDistribution = (*this)[cmpt];
170 Pair<label>
limits(-1, -1);
172 forAll(cmptDistribution, i)
174 if (cmptDistribution[i] > 0.0)
195 Type meanValue(
Zero);
197 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
199 const List<scalar>& cmptDistribution = (*this)[cmpt];
201 scalar totalCmptWeight = totalWeight(cmpt);
203 List<label> theKeys = keys(cmpt);
207 label key = theKeys[
k];
224 Type medianValue(
Zero);
226 List<List<Pair<scalar>>> normDistribution =
normalised();
228 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
230 List<Pair<scalar>>& normDist = normDistribution[cmpt];
234 if (normDist.size() == 1)
241 && normDist[0].second()*
component(binWidth_, cmpt) > 0.5
252 scalar Sk = (normDist[0].second())*
component(binWidth_, cmpt);
254 setComponent(medianValue, cmpt) = 0.5*(xk - xkm1)/(Sk) + xkm1;
258 label previousNonZeroIndex = 0;
260 scalar cumulative = 0.0;
267 + (normDist[nD].second()*
component(binWidth_, cmpt))
276 normDist[previousNonZeroIndex].first()
281 + (normDist[nD].second()*
component(binWidth_, cmpt));
283 scalar Skm1 = cumulative;
286 (0.5 - Skm1)*(xk - xkm1)/(Sk - Skm1) + xkm1;
290 else if (
mag(normDist[nD].second()) > VSMALL)
293 normDist[nD].second()*
component(binWidth_, cmpt);
295 previousNonZeroIndex = nD;
310 const Type& valueToAdd,
314 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
316 List<scalar>& cmptDistribution = (*this)[cmpt];
322 label listIndex = index(cmpt,
n);
333 List<List<Pair<scalar>>> normDistribution(pTraits<Type>::nComponents);
335 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
337 const List<scalar>& cmptDistribution = (*this)[cmpt];
339 if (cmptDistribution.empty())
344 scalar totalCmptWeight = totalWeight(cmpt);
356 label
k =
limits.first(), i = 0;
361 label
key = cmptKeys[
k];
363 normDist[i].first() =
364 (0.5 + scalar(key))*
component(binWidth_, cmpt);
366 normDist[i].second() =
373 return normDistribution;
383 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
387 if (cmptDistribution.
empty())
402 label
k =
limits.first(), i = 0;
407 label key = cmptKeys[
k];
409 rawDist[i].
first() = (0.5 + scalar(key))*
component(binWidth_, cmpt);
411 rawDist[i].second() = cmptDistribution[
k];
415 return rawDistribution;
426 normalisedDistribution;
428 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
431 normalisedDistribution[cmpt];
434 cumulativeNormalisedDistribution[cmpt];
440 cumNormalisedCmpt[i].
first() =
441 normalisedCmpt[i].
first()
444 cumNormalisedCmpt[i].second() =
445 normalisedCmpt[i].second()*
component(binWidth_, cmpt) +
sum;
447 sum = cumNormalisedCmpt[i].second();
451 return cumulativeNormalisedDistribution;
463 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
473 cumRawCmpt[i].
first() =
477 cumRawCmpt[i].second() = rawCmpt[i].second() +
sum;
479 sum = cumRawCmpt[i].second();
483 return cumulativeRawDistribution;
490 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
492 (*this)[cmpt].clear();
494 listStarts_[cmpt] = 0;
502 List<List<Pair<scalar>>> rawDistribution = raw();
504 List<List<Pair<scalar>>> normDistribution =
normalised();
506 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
508 const List<Pair<scalar>>& rawPairs = rawDistribution[cmpt];
510 const List<Pair<scalar>>& normPairs = normDistribution[cmpt];
512 OFstream
os(filePrefix +
'_' + pTraits<Type>::componentNames[cmpt]);
514 os <<
"# key normalised raw" <<
endl;
518 os << normPairs[i].first()
519 <<
' ' << normPairs[i].second()
520 <<
' ' << rawPairs[i].second()
529 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
540 os <<
"# key normalised raw" <<
endl;
544 os << normPairs[i].first()
545 <<
' ' << normPairs[i].second()
546 <<
' ' << rawPairs[i].second()
558 const Distribution<Type>&
rhs
568 binWidth_ =
rhs.binWidth();
570 listStarts_ =
rhs.listStarts();
577Foam::Istream& Foam::operator>>
593Foam::Ostream& Foam::operator<<
611Foam::Distribution<Type> Foam::operator+
622 rawDists[0] = d1.
raw();
623 rawDists[1] = d2.raw();
627 for (
direction cmpt = 0; cmpt < pTraits<Type>::nComponents; cmpt++)
635 scalar valueToAdd = cmptRaw[rI].
first();
636 scalar cmptWeight = cmptRaw[rI].second();
650 label listIndex = d.index(cmpt,
n);
652 cmptDistribution[listIndex] += cmptWeight;
Various functions to operate on Lists.
Accumulating histogram of component values. Specified bin resolution, automatic generation of bins.
List< List< Pair< scalar > > > cumulativeNormalised() const
Return the cumulative normalised distribution and.
List< label > keys(direction cmpt) const
void add(const Type &valueToAdd, const Type &weight=pTraits< Type >::one)
Add a value to the distribution, optionally specifying a weight.
~Distribution()
Destructor.
const List< label > & listStarts() const
Return the List start bin indices.
void write(const fileName &filePrefix) const
Write the distribution to file: key normalised raw.
Distribution()
Construct null.
scalar totalWeight(direction cmpt) const
Sum the total weight added to the component in the.
void clear()
Resets the Distribution by clearing the stored lists.
Pair< label > validLimits(direction cmpt) const
Returns the indices of the first and last non-zero entries.
const Type & binWidth() const
Return the bin width.
List< List< Pair< scalar > > > cumulativeRaw() const
Return the cumulative total bin weights and integration.
List< List< Pair< scalar > > > raw() const
Return the distribution of the total bin weights.
label index(direction cmpt, label n)
Return the appropriate List index for the given bin index.
List< List< Pair< scalar > > > normalised() const
Return the normalised distribution (probability density).
virtual bool check(const char *operation) const
Check IOstream status for given operation.
An Istream is an abstract base class for all input systems (streams, files, token lists etc)....
A 1D array of objects of type <T>, where the size of the vector is known and used for subscript bound...
void setSize(label n)
Alias for resize().
Output to file stream as an OSstream, normally using std::ofstream for the actual output.
An Ostream is an abstract base class for all output systems (streams, files, token lists,...
An ordered pair of two objects of type <T> with first() and second() elements.
T & first()
Access first element of the list, position [0].
bool empty() const noexcept
True if List is empty (ie, size() is zero).
void size(const label n)
Older name for setAddressableSize.
A class for handling file names.
A class representing the concept of 1 (one) that can be used to avoid manipulating objects known to b...
A traits class, which is primarily used for primitives and vector-space.
OBJstream os(runTime.globalPath()/outputName)
constexpr auto key(const Type &t) noexcept
Helper function to return the enum value.
label max(const labelHashSet &set, label maxValue=labelMin)
Find the max value in labelHashSet, optionally limited by second argument.
quaternion normalised(const quaternion &q)
Return the normalised (unit) quaternion of the given quaternion.
void component(FieldField< Field, typename FieldField< Field, Type >::cmptType > &sf, const FieldField< Field, Type > &f, const direction d)
label & setComponent(label &val, const direction) noexcept
Non-const access to integer-type (has no components).
Ostream & endl(Ostream &os)
Add newline and flush stream.
dimensioned< typename typeOfMag< Type >::type > mag(const dimensioned< Type > &dt)
labelList identity(const label len, label start=0)
Return an identity map of the given length with (map[i] == i), works like std::iota() but returning a...
dimensionedScalar neg(const dimensionedScalar &ds)
void rhs(fvMatrix< typename Expr::value_type > &m, const Expr &expression)
dimensioned< Type > sum(const DimensionedField< Type, GeoMesh > &f1, const label comm)
static constexpr const zero Zero
Global zero (0).
constexpr char nl
The newline '\n' character (0x0a).
#define forAll(list, i)
Loop across all elements in list.