PSCF v1.4.0
sortWaves.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2025, The Regents of the University of Minnesota
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "sortWaves.h"
9#include <prdc/cpu/FFT.h>
10#include <prdc/crystal/shiftToMinimum.h>
11#include <prdc/crystal/UnitCell.h>
12#include <pscf/mesh/MeshIterator.h>
13#include <pscf/math/IntVec.h>
14
15namespace Pscf {
16namespace Prdc {
17
18 using namespace Util;
19 using namespace Prdc;
20
26 template <int D>
27 void sortWaves(UnitCell<D> const & cell,
28 IntVec<D> const & meshDimensions,
29 std::vector< Sort::Item<double> >& items,
30 GArray< Sort::Bunch >& bunches,
31 double epsilon,
32 bool isRealField)
33 {
34 // Compute dimensions of Fourier space (DFT) k-grid
35 IntVec<D> kMeshDimensions;
36 int kMeshSize;
37 if (isRealField) {
38 Cpu::FFT<D>::computeKMesh(meshDimensions,
39 kMeshDimensions, kMeshSize);
40 } else {
41 kMeshSize = 1;
42 for (int i = 0; i < D; ++i) {
43 kMeshDimensions[i] = meshDimensions[i];
44 kMeshSize *= meshDimensions[i];
45 }
46 }
47
48 // Generated unsorted array of items, one per wavevector
49 items.clear();
50 items.reserve(kMeshSize);
52 IntVec<D> v;
53 MeshIterator<D> itr(kMeshDimensions);
54 for (itr.begin(); !itr.atEnd(); ++itr) {
55 item.id = itr.rank();
56 v = itr.position();
57 v = shiftToMinimum(v, meshDimensions, cell);
58 item.value = cell.ksq(v);
59 items.push_back(item);
60 }
61
62 // Sort items
63 Sort::sort(items);
64
65 // Identify bunches (array slices) of waves of equal magnitude
66 bunches.clear();
67 Sort::findBunches(items, bunches, epsilon);
68 }
69
70 // Explicit instantiation definitions
71 template
72 void sortWaves<1>(UnitCell<1> const &, IntVec<1> const &,
73 std::vector< Sort::Item<double> >&,
74 GArray< Sort::Bunch >&, double, bool);
75 template
76 void sortWaves<2>(UnitCell<2> const &, IntVec<2> const &,
77 std::vector< Sort::Item<double> >&,
78 GArray< Sort::Bunch >&, double, bool);
79 template
80 void sortWaves<3>(UnitCell<3> const &, IntVec<3> const &,
81 std::vector< Sort::Item<double> >&,
82 GArray< Sort::Bunch >&, double, bool);
83
84}
85}
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.
static void computeKMesh(IntVec< D > const &rMeshDimensions, IntVec< D > &kMeshDimensions, int &kSize)
Compute dimensions and size of k-space mesh for DFT of real data.
Definition cpu/FFT.tpp:264
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:56
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:27
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
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.
Struct with value and index, to keep track of permutation.
Definition Sort.h:37