From 56cb41bfaab4d1fa1939d07daf7c248abd8439b0 Mon Sep 17 00:00:00 2001 From: Cody Permann Date: Wed, 12 Oct 2016 10:01:59 -0600 Subject: [PATCH] Update SphericalAverage VPP to avoid temporaries --- .../vectorpostprocessors/SphericalAverage.h | 5 +-- .../vectorpostprocessors/SphericalAverage.C | 42 +++++++++---------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/framework/include/vectorpostprocessors/SphericalAverage.h b/framework/include/vectorpostprocessors/SphericalAverage.h index b068c19a61ac..92537388342a 100644 --- a/framework/include/vectorpostprocessors/SphericalAverage.h +++ b/framework/include/vectorpostprocessors/SphericalAverage.h @@ -64,11 +64,8 @@ class SphericalAverage : public ElementVectorPostprocessor /// value mid point of the bin VectorPostprocessorValue & _bin_center; - /// local thread copy of the sum vectors - std::vector _sum_tmp; - /// sample count per bin - VectorPostprocessorValue _count_tmp; + std::vector _counts; /// aggregated global average vectors std::vector _average; diff --git a/framework/src/vectorpostprocessors/SphericalAverage.C b/framework/src/vectorpostprocessors/SphericalAverage.C index 60148ed14109..11287a7d305f 100644 --- a/framework/src/vectorpostprocessors/SphericalAverage.C +++ b/framework/src/vectorpostprocessors/SphericalAverage.C @@ -37,15 +37,16 @@ SphericalAverage::SphericalAverage(const InputParameters & parameters) : _values(_nvals), _empty_bin_value(getParam("empty_bin_value")), _bin_center(declareVector("radius")), - _sum_tmp(_nvals), - _count_tmp(_nbins), + _counts(_nbins), _average(_nvals) { if (coupledComponents("variable") != 1) mooseError("SphericalAverage works on exactly one coupled variable"); + // Note: We associate the local variable "i" with nbins and "j" with nvals throughout. + // couple variables initialize vectors - for (unsigned int j = 0; j < _nvals; ++j) + for (auto j = beginIndex(_average); j < _nvals; ++j) { _values[j] = &coupledValue("variable", j); _average[j] = &declareVector(getVar("variable", j)->name()); @@ -53,7 +54,7 @@ SphericalAverage::SphericalAverage(const InputParameters & parameters) : // initialize the bin center value vector _bin_center.resize(_nbins); - for (unsigned i = 0; i < _nbins; ++i) + for (auto i = beginIndex(_counts); i < _nbins; ++i) _bin_center[i] = (i + 0.5) * _deltaR; } @@ -61,11 +62,11 @@ void SphericalAverage::initialize() { // reset the histogram - for (unsigned int j = 0; j < _nvals; ++j) - _sum_tmp[j].assign(_nbins, 0.0); + for (auto vec_ptr : _average) + vec_ptr->assign(_nbins, 0.0); // reset bin counts - _count_tmp.assign(_nbins, 0.0); + _counts.assign(_nbins, 0.0); } void @@ -75,15 +76,15 @@ SphericalAverage::execute() for (_qp = 0; _qp < _qrule->n_points(); ++_qp) { // compute target bin - int bin = computeDistance() / _deltaR; + auto bin = computeDistance() / _deltaR; // add the volume contributed by the current quadrature point if (bin >= 0 && bin < static_cast(_nbins)) { - for (unsigned int j = 0; j < _nvals; ++j) - _sum_tmp[j][bin] += (*_values[j])[_qp]; + for (auto j = decltype(_nvals)(0); j < _nvals; ++j) + (*_average[j])[bin] += (*_values[j])[_qp]; - _count_tmp[bin]++; + _counts[bin]++; } } } @@ -91,15 +92,14 @@ SphericalAverage::execute() void SphericalAverage::finalize() { - gatherSum(_count_tmp); + gatherSum(_counts); - for (unsigned int j = 0; j < _nvals; ++j) + for (auto j = beginIndex(_average); j < _nvals; ++j) { - gatherSum(_sum_tmp[j]); - (*_average[j]).resize(_nbins); + gatherSum(*_average[j]); - for (unsigned int i = 0; i < _nbins; ++i) - (*_average[j])[i] = _count_tmp[i] > 0 ? _sum_tmp[j][i] / _count_tmp[i] : _empty_bin_value; + for (auto i = beginIndex(_counts); i < _nbins; ++i) + (*_average[j])[i] = _counts[i] > 0 ? (*_average[j])[i] / static_cast(_counts[i]) : _empty_bin_value; } } @@ -108,12 +108,12 @@ SphericalAverage::threadJoin(const UserObject & y) { const SphericalAverage & uo = static_cast(y); - for (unsigned int i = 0; i < _nbins; ++i) + for (auto i = beginIndex(_counts); i < _nbins; ++i) { - _count_tmp[i] += uo._count_tmp[i]; + _counts[i] += uo._counts[i]; - for (unsigned int j = 0; j < _nvals; ++j) - _sum_tmp[j][i] += uo._sum_tmp[j][i]; + for (auto j = beginIndex(_average); j < _nvals; ++j) + (*_average[j])[i] += (*uo._average[j])[i]; } }