Skip to content

Commit

Permalink
Speedup FFT buffer indexing (idaholab#401)
Browse files Browse the repository at this point in the history
  • Loading branch information
dschwen authored and recuero committed Apr 29, 2020
1 parent 8c7a1da commit a97a514
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
3 changes: 3 additions & 0 deletions include/postprocessors/FourierLengthScale.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@ class FourierLengthScale : public GeneralPostprocessor

/// averaging weight
Real _weight_sum;

/// index variable for recursive buffer traversal
std::size_t _index;
};

#endif // FFTW3_ENABLED
25 changes: 12 additions & 13 deletions src/postprocessors/FourierLengthScale.C
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,19 @@ FourierLengthScale::FourierLengthScale(const InputParameters & parameters)
void
FourierLengthScale::computeLengthScale(std::vector<int> & c, std::vector<Real> & F, std::size_t i)
{
if (i == 0)
if (i == _dim - 1)
{
Real sum_high = 0.0;
for (unsigned int j = 1; j < _dim; ++j)
sum_high += F[j];

for (c[0] = 0; c[0] < _grid[0]; ++c[0])
auto & cc = c[_dim - 1];
for (cc = 0; cc < _grid[_dim - 1]; ++cc)
{
// do stuff at lowest dimension
Real sum =
sum_high + Utility::pow<2>((c[0] * 2 > _grid[0] ? _grid[0] - c[0] : c[0]) / _box_size(0));
// do stuff at highest dimension
F[_dim - 1] =
Utility::pow<2>((cc * 2 > _grid[_dim - 1] ? _grid[_dim - 1] - cc : cc) / _box_size(0));
Real sum = sum_high + F[_dim - 1];

// find bin and add to spectrum
if (sum > 0)
Expand All @@ -64,26 +66,22 @@ FourierLengthScale::computeLengthScale(std::vector<int> & c, std::vector<Real> &
for (unsigned int j = 1; j < _dim; ++j)
normalization *= frequency;

// recalculate index (row major)
int index = 0;
for (unsigned int j = 0; j < _dim; ++j)
index = index * _grid[j] + c[j];

// weight is the normalized intensity at this frequency
const Real weight = Utility::pow<2>(_buffer[index]) / normalization;
const Real weight = Utility::pow<2>(_buffer[_index]) / normalization;

// compute weighted average of frequencies
_length_scale += frequency * weight;
_weight_sum += weight;
}
_index++;
}
}
else
// iterate over lower dimensions
for (c[i] = 0; c[i] < _grid[i]; ++c[i])
{
F[i] = Utility::pow<2>((c[i] * 2 > _grid[i] ? _grid[i] - c[i] : c[i]) / _box_size(i));
computeLengthScale(c, F, i - 1);
computeLengthScale(c, F, i + 1);
}
}

Expand All @@ -100,7 +98,8 @@ FourierLengthScale::execute()
// compute the length scale
std::vector<int> c(_dim, 0);
std::vector<Real> F(_dim);
computeLengthScale(c, F, _dim - 1);
_index = 0;
computeLengthScale(c, F, 0);

// divide by weight and take reciprocal (frequency -> length)
_length_scale = _weight_sum / _length_scale;
Expand Down

0 comments on commit a97a514

Please sign in to comment.