PSCF v1.5.0
sortWaves.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "sortWaves.h"
9#include <pscf/field/cpp/FFT.h>
10#include <pscf/crystal/shiftToMinimum.h>
11#include <pscf/crystal/UnitCell.h>
12#include <pscf/mesh/MeshIterator.h>
13#include <pscf/math/IntVec.h>
14
15namespace Pscf {
16
17 using namespace Util;
18
24 template <int D>
25 void sortWaves(UnitCell<D> const & cell,
26 IntVec<D> const & meshDimensions,
27 std::vector< Sort::Item<double> >& items,
28 GArray< Sort::Bunch >& bunches,
29 double epsilon,
30 bool isRealField)
31 {
32 // Compute dimensions of Fourier space (DFT) k-grid
33 IntVec<D> kMeshDimensions;
34 int kMeshSize;
35 if (isRealField) {
36 FFT<D,CPT>::computeKMesh(meshDimensions,
37 kMeshDimensions, kMeshSize);
38 } else {
39 kMeshSize = 1;
40 for (int i = 0; i < D; ++i) {
41 kMeshDimensions[i] = meshDimensions[i];
42 kMeshSize *= meshDimensions[i];
43 }
44 }
45
46 // Generated unsorted array of items, one per wavevector
47 items.clear();
48 items.reserve(kMeshSize);
50 IntVec<D> v;
51 MeshIterator<D> itr(kMeshDimensions);
52 for (itr.begin(); !itr.atEnd(); ++itr) {
53 item.id = itr.rank();
54 v = itr.position();
55 v = shiftToMinimum(v, meshDimensions, cell);
56 item.value = cell.ksq(v);
57 items.push_back(item);
58 }
59
60 // Sort items
61 Sort::sort(items);
62
63 // Identify bunches (array slices) of waves of equal magnitude
64 bunches.clear();
65 Sort::findBunches(items, bunches, epsilon);
66 }
67
68 // Explicit instantiation definitions
69 template
70 void sortWaves<1>(UnitCell<1> const &, IntVec<1> const &,
71 std::vector< Sort::Item<double> >&,
72 GArray< Sort::Bunch >&, double, bool);
73 template
74 void sortWaves<2>(UnitCell<2> const &, IntVec<2> const &,
75 std::vector< Sort::Item<double> >&,
76 GArray< Sort::Bunch >&, double, bool);
77 template
78 void sortWaves<3>(UnitCell<3> const &, IntVec<3> const &,
79 std::vector< Sort::Item<double> >&,
80 GArray< Sort::Bunch >&, double, bool);
81
82}
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Iterator over points in a Mesh<D>.
int rank() const
Get the rank of current element.
void begin()
Set iterator to the first point in the mesh.
bool atEnd() const
Is this the end (i.e., one past the last point)?
IntVec< D > position() const
Get current position in the grid, as integer vector.
virtual double ksq(IntVec< D > const &k) const
Compute square magnitude of reciprocal lattice vector.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
void clear()
Reset to empty state.
Definition GArray.h:296
void sortWaves(UnitCell< D > const &cell, IntVec< D > const &meshDimensions, std::vector< Sort::Item< double > > &items, GArray< Sort::Bunch > &bunches, double epsilon, bool isRealField)
Sorted waves and identify bunches of equal magnitude.
Definition sortWaves.cpp:25
void findBunches(std::vector< Item< T > > const &items, GArray< Bunch > &bunches, T epsilon)
Identify "bunches" of equal values within a sorted vector.
Definition Sort.cpp:29
void sort(std::vector< Item< T > > &items)
Sort a std::vector< Item<T> > by ascending item value.
Definition Sort.cpp:22
PSCF package top-level namespace.
Struct with value and index, to keep track of permutation.
Definition Sort.h:37