PSCF v1.5.0
Basis.tpp
1#ifndef PSCF_BASIS_TPP
2#define PSCF_BASIS_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Basis.h"
12#include "BWave.h"
13#include "groupFile.h"
14#include <pscf/crystal/UnitCell.h>
15#include <pscf/crystal/SpaceGroup.h>
16#include <pscf/crystal/shiftToMinimum.h>
17#include <pscf/mesh/Mesh.h>
18#include <pscf/mesh/MeshIterator.h>
19#include <util/signal/Signal.h>
20#include <util/format/Dbl.h>
21#include <util/math/Constants.h>
22
23#include <algorithm>
24#include <vector>
25#include <set>
26#include <fstream>
27
28namespace Pscf {
29
30 /*
31 * Constructor.
32 */
33 template <int D>
35 : waves_(),
36 stars_(),
37 waveIds_(),
38 starIds_(),
39 nWave_(0),
40 nBasisWave_(0),
41 nStar_(0),
42 nBasis_(0),
43 signalPtr_(nullptr),
44 unitCell_(),
45 meshPtr_(0),
46 isInitialized_(false)
47 {
48 signalPtr_ = new Signal<void>();
49 }
50
51 /*
52 * Destructor.
53 */
54 template <int D>
56 {
57 delete signalPtr_;
58 }
59
60 /*
61 * Construct basis for pseudo-spectral scft.
62 */
63 template <int D>
64 void Basis<D>::makeBasis(Mesh<D> const & mesh,
65 UnitCell<D> const & unitCell,
66 std::string groupName)
67 {
68 SpaceGroup<D> group;
69 readGroup(groupName, group);
70 makeBasis(mesh, unitCell, group);
71 }
72
73 /*
74 * Construct a symmetry-adapted basis for pseudo-spectral scft.
75 */
76 template <int D>
77 void Basis<D>::makeBasis(Mesh<D> const & mesh,
78 UnitCell<D> const & unitCell,
79 SpaceGroup<D> const & group)
80 {
81 // Precondition: Check compatibility of mesh with space group
82 group.checkMeshDimensions(mesh.dimensions());
83
84 // Save a pointer to the mesh and a copy of the unit cell (the
85 // unit cell passed by the caller may be a temporary object)
86 meshPtr_ = &mesh;
87 unitCell_ = unitCell;
88
89 // Allocate arrays
90 nWave_ = mesh.size();
91 waves_.allocate(nWave_);
92 waveIds_.allocate(nWave_);
93
94 // Make sorted array of waves
95 makeWaves();
96
97 // Identify stars of waves that are related by symmetry
98 makeStars(group);
99
100 // Apply validity test suite
101 bool valid = isValid();
102 if (!valid) {
103 UTIL_THROW("Basis failed validity test suite");
104 }
105
106 // Mark as initialized
107 isInitialized_ = true;
108
109 // Notify any observers of successful basis initialization
110 signal().notify();
111 }
112
113 /*
114 * Construct ordered list of waves.
115 *
116 * On exit:
117 * - Member array waves_ contains list of waves ordered by sqNorm.
118 * - Each wave has indicesStd, indicesMin and sqNorm set.
119 * - Member array stars_ is still empty.
120 */
121 template <int D>
122 void Basis<D>::makeWaves()
123 {
124 IntVec<D> meshDimensions = mesh().dimensions();
125 std::vector< BWave<D> > twaves;
126 twaves.reserve(nWave_);
127
128 // Loop over k-grid mesh to generate all waves, add to twaves
129 BWave<D> w;
130 IntVec<D> v;
131 MeshIterator<D> itr(mesh().dimensions());
132 for (itr.begin(); !itr.atEnd(); ++itr) {
133 w.indicesStd = itr.position();
134 v = shiftToMinimum(w.indicesStd, meshDimensions, unitCell_);
135 w.indicesMin = v;
136 w.sqNorm = unitCell().ksq(v);
137 twaves.push_back(w);
138 }
139
140 // Sort temporary array twaves
141 BWaveNormComp<D> comp;
142 std::sort(twaves.begin(), twaves.end(), comp);
143
144 // Copy temporary array twaves into member variable waves_
145 for (int i = 0; i < nWave_; ++i) {
146 waves_[i].sqNorm = twaves[i].sqNorm;
147 waves_[i].indicesStd = twaves[i].indicesStd;
148 waves_[i].indicesMin = twaves[i].indicesMin;
149 }
150
151 }
152
153 /*
154 * Complete construction of a basis by grouping presorted waves into
155 * stars and completing initialization of all Wave and Star objects.
156 */
157 template <int D>
158 void Basis<D>::makeStars(SpaceGroup<D> const & group)
159 {
160
161 /*
162 * Conceptual definitions:
163 *
164 * - A "bunch" is a set of wavevectors of equal magnitude.
165 * - A "star" is a set of wavevectors that are related by symmetry.
166 *
167 * Each bunch contains one or more complete stars. Bunches are
168 * identified as an intermediate step in identification of stars.
169 *
170 * During initial processing, wavevectors are temporarily stored in
171 * BWave<D> objects. The following local containers of BWave<D>
172 * objects are used:
173 *
174 * bunch - a set of waves of equal norm
175 * star - a set of symmetry-related waves
176 * starList - a sorted star, sorted by descending indicesMin
177 * bunchList - a sorted bunch, with contiguous sorted stars
178 *
179 * The bunch and star containers are std::set< BWave<D> > objects.
180 * The starList container is a std::vector< BWave<D> > objects.
181 * The use of std::set simplifies addition of waves, by allowing
182 * easy identification of duplicates, as well as later removal.
183 * The use of std::vector for the starList allows the use of the
184 * C++ std::sort algorithm to sort stars by standard wavevector
185 * indices.
186 */
187
188 // Local BWave<D> containers and associated iterators
189 std::set< BWave<D>, BWaveStdComp<D> > bunch;
190 std::set< BWave<D>, BWaveStdComp<D> > star;
191 std::vector< BWave<D> > starList;
192 GArray< BWave<D> > bunchList;
193 typename std::set< BWave<D>, BWaveStdComp<D> >::iterator rootItr;
194 typename std::set< BWave<D>, BWaveStdComp<D> >::iterator setItr;
195
196 // Local variables
197 BWave<D> wave;
198 Basis<D>::Star newStar;
199 std::complex<double> coeff;
200 double Gsq;
201 double Gsq_max;
202 double phase_diff;
203 const double twoPi = 2.0*Constants::Pi;
204 const double epsilon = 1.0E-8;
205 IntVec<D> meshDimensions = mesh().dimensions();
206 IntVec<D> rootVecMin; // Min indices for root of this star
207 IntVec<D> rootVecStd; // Std indices for root of this star
208 IntVec<D> vec; // Indices of temporary wavevector
209 IntVec<D> nVec; // Indices of inverse of a wavevector
210 int bunchBegin = 0; // id of first wave in this bunch
211 int bunchEnd = 0; // (id of last wave in this bunch) + 1
212 int bunchSize; // bunchEnd - bunchBegin
213 int starBegin = 0; // id of first wave in this star
214 int i, j, k;
215 bool cancel;
216
217 /*
218 * Overview of algorithm:
219 *
220 * Precondition: Wavevectors in the array waves_ are sorted in
221 * nondecreasing order by wavevector norm.
222 *
223 * Loop over index i of array waves_ {
224 *
225 * Search for end of a "bunch" (i.e., contiguous block of waves
226 * of equal magnitude) by identifying changes in magnitude. The
227 * resulting bunch has indices bunchBegin, ... , bunchEnd-1 .
228 * Set newList true.
229 *
230 * // Each bunch may contain one or more stars.
231 * // Process the newly identified bunch to identify stars.
232 *
233 * If (newList) {
234 *
235 * Copy all waves in the range into std::set bunch
236 *
237 * Set rootItr to the first wave in the bunch
238 *
239 * // Loop over stars within the bunch
240 * while (bunch.size() > 0) {
241 *
242 * // To generate a star from a root wave rootItr,
243 * // loop over symmetry operations of space group.
244 *
245 * For each symmetry operation group[j] {
246 * Compute vec = (rootItr->indicesMin)*group[j]
247 * Set phase = rootItr->indicesMin .dot. (group[j].t)
248 * Check for cancellation of the star, set "cancel" flag
249 * Add wave to std::set<BWave> star if not added before
250 * // Here, use of a std::set simplifies test of uniqueness
251 * }
252 *
253 * Copy all waves from star to std::vector<BWave> starList
254 * Sort starList by indicesMin, in descending order
255 * // Here, use of a std::vector for starList allows sorting
256 *
257 * // Add waves in star to bunchList and remove from bunch
258 * For each wave in starList {
259 * Append the wave to GArray<BWave<D> > bunchList
260 * Erase the wave from std::set<BWave<D> > bunch
261 * // Here, use of a std::set for bunch simplifies erasure
262 * }
263 *
264 * Initialize a Star object named newStar
265 * Assign values to members beginId, endId, size, cancel
266 *
267 * // Assign values of newStar.invertFlag, nextInvert, rootItr
268 * if (nextInvert == -1) {
269 * // This is the second star in pair
270 * newStar.invertFlag = -1;
271 * nextInvert = 1;
272 * Set rootItr to the first wave in remaining bunch
273 * } else {
274 * Search for inverse of rootItr in this star
275 * if inverse is in this star {
276 * // This is a closed star
277 * newStar.invertFlag = 0
278 * nextInvert = 1;
279 * Set rootItr to the first wave in remaining bunch
280 * } else
281 * Search for inverse of rootItr in this remaining bunch
282 * if the inverse is in the remaining bunch {
283 * // This is the first open star in a pair
284 * newStar.invertFlag = 1
285 * nextInvert = -1;
286 * set rootItr to inverse of current root
287 * }
288 * }
289 *
290 * Append newStar object to GArray<Star> stars_
291 *
292 * } // end loop over stars in a single bunch
293 *
294 * // At this point, bunchList contains the contents of the
295 * // waves_ array occupying the range [beginId, endId-1],
296 * // grouped by stars, with waves within each star sorted
297 * // by minimal indices.
298 *
299 * // Overwrite the block of array waves_ with indices in the
300 * // range [beginId, endId-1] with the contents of bunchList.
301 * For each wave in bunchList {
302 * Copy a BWave in bunchList to a Basis:Wave in waves_
303 * Assign a complex coefficient of unit norm to the Wave
304 * }
305 *
306 * // At this point, coefficients of waves have unit magnitude
307 * // and correct relative phases within each star, but not the
308 * // final absolute phases or magnitude.
309 *
310 * } // finish processing of one bunch (waves of equal norm)
311 *
312 * } // End initial processing of all waves and stars
313 *
314 * // Set phases of wave coefficients
315 * For each star in array stars_ {
316 * if star is closed under inversion (star.invertFlag == 0) {
317 * Find the inverse of every wave in this star
318 * if star is cancelled {
319 * Set coefficients of all waves to zero
320 * } else {
321 * Set the root to the first wave in the star
322 * For each wave in star:
323 * Divide coeff by the root coefficient
324 * }
325 * if (coeffs of root & inverse are not complex conjugates){
326 * Divide all coeffs by a common phasor chosen to obtain
327 * complex conjugate coefficients for root and inverse
328 * }
329 * }
330 * } else
331 * if (star.invertFlag == 1) {
332 * Find the inverse of every wave in this star and next star
333 * Set root of this star to the 1st wave in this star
334 * Set partner to the inverse of the root of this star
335 * If this star is cancelled {
336 * Set coefficients in this star and next to zero
337 * } else {
338 * For each wave in this star:
339 * Divide coeff by the root coefficient
340 * }
341 * For each wave in the next star:
342 * Divide coeff by the partner coefficient
343 * }
344 * }
345 * }
346 * // Note: If star.invertFlag = -1, do nothing because properties
347 * // of this star were all set when processing its partner.
348 * }
349 *
350 * // For all waves, normalize coefficients and set starId
351 * For each star in array stars_ {
352 * For each wave in this star {
353 * Set Wave.starId
354 * Divide coefficient by sqrt(double(star.size))
355 * }
356 * }
357 *
358 * // For all waves, set implicit member and add to look up table
359 * For each wave in array waves_ {
360 * Set Wave::implicit attribute
361 * Set waveIds_[rank] = i
362 * }
363 */
364
365 // Loop over all waves (initial processing of waves)
366 nBasis_ = 0;
367 nBasisWave_ = 0;
368 Gsq_max = waves_[0].sqNorm;
369 for (i = 1; i <= nWave_; ++i) {
370
371 // Determine if this wave begins a new bunch
372 bool newList = false;
373 if (i == nWave_) {
374 bunchEnd = i;
375 bunchSize = bunchEnd - bunchBegin;
376 newList = true;
377 } else {
378 Gsq = waves_[i].sqNorm;
379 if (Gsq > Gsq_max + epsilon) {
380 Gsq_max = Gsq;
381 bunchEnd = i;
382 bunchSize = bunchEnd - bunchBegin;
383 newList = true;
384 }
385 }
386
387 // Process completed bunch of wavectors of equal norm
388 if (newList) {
389
390 // Copy waves of equal norm into std::set "bunch"
391 bunch.clear();
392 bunchList.clear();
393 for (j = bunchBegin; j < bunchEnd; ++j) {
394 wave.indicesStd = waves_[j].indicesStd;
395 wave.indicesMin = waves_[j].indicesMin;
396 wave.sqNorm = waves_[j].sqNorm;
397 if (j > bunchBegin) {
398 UTIL_CHECK( std::abs(wave.sqNorm-waves_[j].sqNorm)
399 < 2.0*epsilon );
400 }
401 bunch.insert(wave);
402 }
403
404 // On entry to each iteration of the loop over stars,
405 // rootItr and nextInvert are known. The iterator rootItr
406 // points to the wave in the remaining bunch that will be
407 // used as the root of the next star. The flag nextInvert
408 // is equal to -1 iff the previous star was the first of
409 // a pair that are open under inversion, and is equal
410 // to + 1 otherwise.
411
412 // Initial values for first star in this bunch
413 rootItr = bunch.begin();
414 int nextInvert = 1;
415
416 // Loop over stars with a bunch of waves of equal norm,
417 // removing each star from set bunch as it is identified.
418 // The root of the next star must have been chosen on
419 // entry to each iteration of this loop.
420
421 while (bunch.size() > 0) {
422
423 rootVecMin = rootItr->indicesMin;
424 rootVecStd = rootItr->indicesStd;
425 Gsq = rootItr->sqNorm;
426 cancel = false;
427 star.clear();
428
429 // Construct a star from root vector, by applying every
430 // symmetry operation in the group to the root wavevector.
431 // The root vector will always be added, with zero phase,
432 // because a group always includes the identity operation
433
434 for (j = 0; j < group.size(); ++j) {
435
436 // Apply symmetry (i.e., multiply by rotation matrix)
437 // vec = rotated wavevector.
438 vec = rootVecMin*group[j];
439
440 // Check that rotated vector has the same norm as root
441 UTIL_CHECK(std::abs(Gsq - unitCell().ksq(vec)) < epsilon);
442
443 // Initialize BWave object associated with rotated wave
444 wave.sqNorm = Gsq;
445 wave.indicesMin = shiftToMinimum(vec, meshDimensions,
446 unitCell_);
447 wave.indicesStd = vec;
448 mesh().shift(wave.indicesStd);
449
450 // Compute phase for coeff. of wave in basis function.
451 // Convention -pi < phase <= pi.
452 wave.phase = 0.0;
453 for (k = 0; k < D; ++k) {
454 wave.phase += rootVecMin[k]*(group[j].t(k));
455 }
456 while (wave.phase > 0.5) {
457 wave.phase -= 1.0;
458 }
459 while (wave.phase <= -0.5) {
460 wave.phase += 1.0;
461 }
462
463 // Convert phase to radians, in range [-Pi, Pi]
464 wave.phase *= twoPi;
465
466 // Check for cancellation of star: The star is cancelled
467 // if application of any symmetry operation in the group
468 // to the root vector yields a rotated wavevector that
469 // is either:
470 // - Equivalent to the root vector, but with a nonzero
471 // relative phase
472 // - Equivalent to another wavevector that has already
473 // been added to the star, with an inequivalent phase
474
475 // Check if the rotated wavevector is equal to the root
476 if (wave.indicesStd == rootVecStd) {
477 if (std::abs(wave.phase) > 1.0E-6) {
478 cancel = true;
479 }
480 }
481
482 // Search for an equivalent wave already in the star.
483 // Equivalent waves have equal standard indices.
484 setItr = star.find(wave);
485
486 if (setItr == star.end()) {
487
488 // If no equivalent wave was found in the star,
489 // then add this wave to the star
490
491 star.insert(wave);
492
493 } else {
494
495 // If an equivalent wave was found, check if the
496 // phases are equivalent. If the phases are not
497 // equivalent, the star is cancelled.
498
499 phase_diff = setItr->phase - wave.phase;
500 while (phase_diff > Constants::Pi) {
501 phase_diff -= twoPi;
502 }
503 while (phase_diff <= - Constants::Pi) {
504 phase_diff += twoPi;
505 }
506 if (std::abs(phase_diff) > 1.0E-6) {
507 cancel = true;
508 }
509
510 }
511
512 // Note: Construction of the star is completed whether
513 // or not the star is cancelled
514
515 }
516
517 // Copy waves from std::set star to std::vector starList
518 starList.clear();
519 setItr = star.begin();
520 for ( ; setItr != star.end(); ++setItr) {
521 starList.push_back(*setItr);
522 }
523
524 // Sort starList, in descending order by indicesMin.
525 BWaveMinComp<D> waveMinComp;
526 std::sort(starList.begin(), starList.end(), waveMinComp);
527
528 // Append starList to bunchList, erase from set bunch
529 int starListSize = starList.size();
530 for (j = 0; j < starListSize; ++j) {
531 bunch.erase(starList[j]);
532 bunchList.append(starList[j]);
533 }
534 UTIL_CHECK(int(bunchList.size()+bunch.size())==bunchSize);
535
536 // If this star is not cancelled, increment the number of
537 // basis functions (nBasis_) & waves in basis (nBasisWave_)
538 if (!cancel) {
539 ++nBasis_;
540 nBasisWave_ += star.size();
541 }
542
543 // Initialize a Star object
544 // newStar.eigen = Gsq;
545 newStar.beginId = starBegin;
546 newStar.endId = newStar.beginId + star.size();
547 newStar.size = star.size();
548 newStar.cancel = cancel;
549 // Note: newStar.starInvert is not yet known
550
551 // Determine invertFlag, rootItr and nextInvert
552 if (nextInvert == -1) {
553
554 // If this star is 2nd of a pair related by inversion,
555 // set root for next star to 1st wave of remaining
556 // bunch.
557
558 newStar.invertFlag = -1;
559 rootItr = bunch.begin();
560 nextInvert = 1;
561
562 } else {
563
564 // If this star is not the 2nd of a pair of partners,
565 // then determine if it is closed under inversion.
566
567 // Compute negation nVec of root vector
568 nVec.negate(rootVecMin);
569
570 // Shift nVec to the standard mesh
571 (*meshPtr_).shift(nVec);
572
573 // Search for inverse of root vector within this star
574 bool inverseFound = false;
575 setItr = star.begin();
576 for ( ; setItr != star.end(); ++setItr) {
577 if (nVec == setItr->indicesStd) {
578 inverseFound = true;
579 break;
580 }
581 }
582
583 if (inverseFound) {
584
585 // If this star is closed under inversion, the root
586 // of the next star is the 1st vector of remaining
587 // bunch.
588
589 newStar.invertFlag = 0;
590 rootItr = bunch.begin();
591 nextInvert = 1;
592
593 } else {
594
595 // This star is open under inversion, and is the
596 // first star of a pair related by inversion
597
598 newStar.invertFlag = 1;
599 nextInvert = -1;
600
601 // Find inverse of the root of this star in the
602 // remaining bunch, and use this inverse as the
603 // root of the next star.
604
605 setItr = bunch.begin();
606 for ( ; setItr != bunch.end(); ++setItr) {
607 if (nVec == setItr->indicesStd) {
608 inverseFound = true;
609 rootItr = setItr;
610 break;
611 }
612 }
613 // If inverseFound, then rootVecStd = nVec
614
615 // Failure to find the inverse here is an error:
616 // It must be either in this star or remaining bunch
617
618 if (!inverseFound) {
619 std::cout << "Inverse not found for: " << "\n";
620 std::cout << " vec (std):"
621 << rootVecStd <<"\n";
622 std::cout << " vec (min):"
623 << rootVecMin <<"\n";
624 std::cout << "-vec (std):" << nVec << "\n";
625 UTIL_CHECK(inverseFound);
626 }
627
628 }
629
630 }
631
632 stars_.append(newStar);
633 starBegin = newStar.endId;
634
635 }
636 // End loop over stars within a bunch.
637
638 UTIL_CHECK(bunch.size() == 0);
639 UTIL_CHECK(bunchList.size() == bunchEnd - bunchBegin);
640
641 // Copy bunchList into corresponding section of waves_,
642 // overwriting the section of waves_ used to create the bunch.
643 // Compute a complex coefficient of unit norm for each wave.
644 for (j = 0; j < bunchList.size(); ++j) {
645 k = j + bunchBegin;
646 waves_[k].indicesStd = bunchList[j].indicesStd;
647 waves_[k].indicesMin = bunchList[j].indicesMin;
648 waves_[k].sqNorm = bunchList[j].sqNorm;
649 coeff = std::complex<double>(0.0, bunchList[j].phase);
650 coeff = exp(coeff);
651 if (std::abs(imag(coeff)) < 1.0E-6) {
652 coeff = std::complex<double>(real(coeff), 0.0);
653 }
654 if (std::abs(real(coeff)) < 1.0E-6) {
655 coeff = std::complex<double>(0.0, imag(coeff));
656 }
657 waves_[k].coeff = coeff;
658 }
659
660 // Processing of this bunch is now complete.
661 // Here, waves_[k].coeff has unit absolute magnitude, and
662 // correct relative phases for waves within a star, but
663 // the coeff may not be unity for the first or last wave
664 // in the star.
665
666 bunchBegin = bunchEnd;
667 }
668 // Finished processing a bunch of waves of equal norm
669
670 } // End loop over all waves
671 nStar_ = stars_.size();
672 // Complete initial processing of all bunches and stars
673
674 /*
675 * Conventions for phases of wave coefficients (imposed below):
676 * - Coefficients of the root of each star and its inverse must
677 * be complex conjugates.
678 * - In a closed star (starInvert = 0), the coefficient of the
679 * root must have a non-negative real part. If the root
680 * coefficient is pure imaginary, the imaginary part must
681 * be negative.
682 * - In a pair of open stars that are related by inversion
683 * symmetry, the coefficients of the first wave of the first
684 * star and its inverse must have real coefficients.
685 */
686
687 // Final processing of phases of of waves in stars:
688 std::complex<double> rootCoeff; // Coefficient of root wave
689 std::complex<double> partCoeff; // Coefficient of partner of root
690 std::complex<double> d;
691 int rootId, partId;
692 for (i = 0; i < nStar_; ++i) {
693
694 // Treat open and closed stars differently
695
696 if (stars_[i].invertFlag == 0) {
697
698 // First, assign inverseId for each wave in star
699 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
700 if (waves_[j].inverseId < 0) { // if inverseId is unassigned
701
702 // Compute nVec = inverse of root, shifted to std mesh
703 nVec.negate(waves_[j].indicesMin);
704 (*meshPtr_).shift(nVec);
705
706 // Find inverse
707
708 // Check in the position that the inverse is
709 // expected to be located for a typical star
710 k = stars_[i].endId - 1 - (j - stars_[i].beginId);
711 if (nVec == waves_[k].indicesStd) {
712 waves_[j].inverseId = k;
713 waves_[k].inverseId = j;
714 } else {
715 // Inverse not in expected position, search full star
716 // (this may occur for stars on the edge of the
717 // Brillouin zone)
718 for (k = j; k < stars_[i].endId; ++k) {
719 if (nVec == waves_[k].indicesStd) {
720 waves_[j].inverseId = k;
721 waves_[k].inverseId = j;
722 break;
723 }
724 }
725 }
726
727 // For invertFlag == 0, failure to find nVec in this
728 // star is a fatal error
729 if (waves_[j].inverseId < 0) {
730 std::cout << "\n";
731 std::cout << "Inverse not found in closed star"
732 << std::endl;
733 std::cout << "G = " << waves_[j].indicesMin
734 << ", coeff = " << waves_[j].coeff
735 << std::endl;
736 std::cout << "All waves in star " << i
737 << std::endl;
738 for (k=stars_[i].beginId; k < stars_[i].endId; ++k) {
739 std::cout << waves_[k].indicesMin << " "
740 << waves_[k].coeff << std::endl;
741 }
742 UTIL_CHECK(waves_[j].inverseId >= 0);
743 }
744
745 }
746 }
747
748 // Identify root of this star (star i)
749 // Set the root to be the first wave in the star
750
751 rootId = stars_[i].beginId;
752 stars_[i].rootId = rootId;
753 stars_[i].waveMin = waves_[rootId].indicesMin;
754
755 if (stars_[i].cancel) {
756
757 // If the star is cancelled, set all coefficients to zero
758 std::complex<double> czero(0.0, 0.0);
759 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
760 waves_[j].coeff = czero;
761 }
762
763 } else { // if not cancelled
764
765 // Set partId to index of the inverse of the root
766 partId = waves_[rootId].inverseId;
767
768 // Divide all coefficients by the root coefficient
769 rootCoeff = waves_[rootId].coeff;
770 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
771 waves_[j].coeff /= rootCoeff;
772 }
773 rootCoeff = waves_[rootId].coeff;
774 UTIL_CHECK(std::abs(real(rootCoeff) - 1.0) < 1.0E-9);
775 UTIL_CHECK(std::abs(imag(rootCoeff)) < 1.0E-9);
776
777 // Require coefficients of root and inverse are conjugates
778 if (partId != rootId) {
779
780 partCoeff = waves_[partId].coeff;
781 UTIL_CHECK(std::abs(std::abs(partCoeff) - 1.0) < 1.0E-9);
782 if (std::abs(partCoeff - rootCoeff) > 1.0E-6) {
783 d = sqrt(partCoeff);
784 if (real(d) < -1.0E-4) {
785 d = -d;
786 } else
787 if (std::abs(real(d)) <= 1.0E-4) {
788 if (imag(d) < 0.0) {
789 d = -d;
790 }
791 }
792 for (j=stars_[i].beginId; j < stars_[i].endId; ++j){
793 waves_[j].coeff /= d;
794 }
795 }
796
797 }
798
799 } // end if (cancel) ... else ...
800
801 } // end if (stars_[i].invertFlag == 0)
802 else
803 if (stars_[i].invertFlag == 1) {
804
805 // Process a pair of open stars related by inversion.
806
807 // Preconditions:
808 UTIL_CHECK(stars_[i].size == stars_[i+1].size);
809 UTIL_CHECK(stars_[i].cancel == stars_[i+1].cancel);
810 // UTIL_CHECK(stars_[i+1].invertFlag == -1);
811
812 // First, assign inverseId for each wave in pair of stars
813 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
814 if (waves_[j].inverseId < 0) { // if inverseId is unassigned
815
816 // Compute nVec = inverse of root, shifted to std mesh
817 nVec.negate(waves_[j].indicesMin);
818 (*meshPtr_).shift(nVec);
819
820 // Find inverse
821
822 // Check in the position that the inverse is expected
823 // to be located for a typical pair of stars
824 k = stars_[i+1].endId - 1 - (j - stars_[i].beginId);
825 if (nVec == waves_[k].indicesStd) {
826 waves_[j].inverseId = k;
827 waves_[k].inverseId = j;
828 } else {
829 // Inverse not in expected position, search full star
830 // (this may occur for stars on the edge of the
831 // Brillouin zone)
832 k = stars_[i+1].beginId;
833 for ( ; k < stars_[i+1].endId; ++k) {
834 if (nVec == waves_[k].indicesStd) {
835 waves_[j].inverseId = k;
836 waves_[k].inverseId = j;
837 break;
838 }
839 }
840 }
841
842 // For invertFlag = 1, failure to find nVec in the
843 // next star is a fatal error
844 UTIL_CHECK(waves_[j].inverseId >= 0);
845
846 }
847 }
848
849 // Check that inverseId was assigned for all waves in the
850 // next star
851 for (j = stars_[i+1].beginId; j < stars_[i+1].endId; ++j) {
852 UTIL_CHECK(waves_[j].inverseId >= 0);
853 }
854
855 // Identify root of this star (star i)
856 // Set the root to be the first wave in the star
857 rootId = stars_[i].beginId;
858 stars_[i].rootId = rootId;
859 stars_[i].waveMin = waves_[rootId].indicesMin;
860
861 // Identify root of the next star (star i+1)
862 // Set the root to be the inverse of the root of star i
863 partId = waves_[rootId].inverseId;
864 stars_[i+1].rootId = partId;
865 stars_[i+1].waveMin = waves_[partId].indicesMin;
866
867 if (stars_[i].cancel) {
868
869 std::complex<double> czero(0.0, 0.0);
870 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
871 waves_[j].coeff = czero;
872 }
873 for (j = stars_[i+1].beginId; j < stars_[i+1].endId; ++j) {
874 waves_[j].coeff = czero;
875 }
876
877 } else { // if star is not cancelled
878
879 // Divide all coefficients in this star by root coeff
880 rootCoeff = waves_[rootId].coeff;
881 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
882 waves_[j].coeff /= rootCoeff;
883 }
884
885 // Divide coefficients in next star by a partner coeff.
886 partCoeff = waves_[partId].coeff;
887 for (j = stars_[i+1].beginId; j < stars_[i+1].endId; ++j) {
888 waves_[j].coeff /= partCoeff;
889 }
890
891 } // end if (cancel) ... else ...
892
893 } // end if (invertFlag==0) ... else if (invertFlag==1) ...
894
895 // Note: If invertFlag == -1, do nothing and continue
896 // Related stars with invertFlag == 1 and -1 are treated together
897
898 } // end loop over stars
899
900 // For all waves, normalize coefficients and set starId
901 for (i = 0; i < nStar_; ++i) {
902 double snorm = 1.0/sqrt(double(stars_[i].size));
903 for (j = stars_[i].beginId; j < stars_[i].endId; ++j) {
904 waves_[j].coeff *= snorm;
905 waves_[j].starId = i;
906 }
907 }
908
909 // Set tiny real and imaginary parts to zero (due to round-off)
910 for (i = 0; i < nWave_; ++i) {
911 if (std::abs(real(waves_[i].coeff)) < 1.0E-8) {
912 waves_[i].coeff
913 = std::complex<double>(0.0, imag(waves_[i].coeff));
914 }
915 if (std::abs(imag(waves_[i].coeff)) < 1.0E-8) {
916 waves_[i].coeff
917 = std::complex<double>(real(waves_[i].coeff), 0.0);
918 }
919 }
920
921 // For each wave, set implicit attribute and add to look-up table
922 for (i = 0; i < nWave_; ++i) {
923 vec = waves_[i].indicesStd;
924
925 // Validity check - check that vec is in standard k-grid mesh
926 for (j = 0; j < D; ++j) {
927 UTIL_CHECK(vec[j] >= 0);
928 UTIL_CHECK(vec[j] < meshDimensions[j]);
929 }
930
931 // Set implicit attribute
932 if ((vec[D-1] + 1) > (meshDimensions[D-1]/2 + 1)) {
933 waves_[i].implicit = true;
934 } else {
935 waves_[i].implicit = false;
936 }
937
938 // Look up table for waves
939 waveIds_[mesh().rank(vec)] = i;
940 }
941
942 // Set Star::starId and Star::basisId, add to look-up table starIds_.
943 starIds_.allocate(nBasis_);
944 j = 0;
945 for (i = 0; i < nStar_; ++i) {
946 stars_[i].starId = i;
947 if (stars_[i].cancel) {
948 stars_[i].basisId = -1;
949 } else {
950 stars_[i].basisId = j;
951 UTIL_CHECK(j < nBasis_);
952 starIds_[j] = i;
953 ++j;
954 }
955 }
956 UTIL_CHECK(j == nBasis_);
957
958 }
959
960 // Return value of nBasis
961 template <int D>
963 { return nBasis_; }
964
965
966 template <int D>
967 void Basis<D>::outputWaves(std::ostream& out, bool outputAll) const
968 {
969 out << "N_wave" << std::endl;
970 if (outputAll) {
971 out << " " << nWave_ << std::endl;
972 } else {
973 out << " " << nBasisWave_ << std::endl;
974 }
975 // Each integer column is preceded by a space, so that columns
976 // stay separated when a value fills its field width
977 int i, j, k, starId;
978 k = 0;
979 for (i = 0; i < nWave_; ++i) {
980 starId = waves_[i].starId;
981 if (outputAll || (!stars_[starId].cancel)) {
982 out << ' ' << Int(k, 7);
983 out << ' ' << Int(i, 7);
984 for (j = 0; j < D; ++j) {
985 out << ' ' << Int(waves_[i].indicesMin[j], 4);
986 }
987 out << ' ' << Int(waves_[i].starId, 5);
988 out << " " << Dbl(waves_[i].coeff.real(), 15);
989 out << " " << Dbl(waves_[i].coeff.imag(), 15);
990 out << std::endl;
991 k++;
992 }
993 }
994 }
995
996
997 template <int D>
998 void Basis<D>::outputStars(std::ostream& out, bool outputAll) const
999 {
1000 // Output number of stars in appropriate format
1001 if (outputAll) {
1002 out << "N_star" << std::endl
1003 << " " << nStar_ << std::endl;
1004 } else {
1005 out << "N_basis" << std::endl
1006 << " " << nBasis_ << std::endl;
1007 }
1008
1009 // Loop over stars. Each integer column is preceded by a space,
1010 // so that columns stay separated when a value fills its width.
1011 int i, j;
1012 for (i = 0; i < nStar_; ++i) {
1013 if (outputAll || (!stars_[i].cancel)) {
1014 out << ' ' << Int(stars_[i].basisId, 5); // basisId
1015 out << ' ' << Int(i, 5); // starId
1016 out << ' ' << Int(stars_[i].size, 4)
1017 << ' ' << Int(stars_[i].beginId, 7)
1018 << ' ' << Int(stars_[i].endId, 7)
1019 << ' ' << Int(stars_[i].invertFlag, 3);
1020 if (outputAll) {
1021 out << ' ' << Int(stars_[i].cancel, 3);
1022 }
1023 for (j = 0; j < D; ++j) {
1024 out << ' ' << Int(stars_[i].waveMin[j], 5);
1025 }
1026 out << std::endl;
1027 }
1028 }
1029 }
1030
1031 template <int D>
1033 {
1034 double Gsq;
1035 IntVec<D> v;
1036 int is, ib, iw, iwp, j;
1037
1038 // Check total number of waves == # of grid points
1039 if (nWave_ != mesh().size()) {
1040 std::cout << "nWave != size of mesh" << std::endl;
1041 return false;
1042 }
1043
1044 // Loop over k-grid mesh to check consistency of waveIds_ and waves_
1045 MeshIterator<D> itr(mesh().dimensions());
1046 for (itr.begin(); !itr.atEnd(); ++itr) {
1047 v = itr.position();
1048 iw = waveId(v);
1049 if (wave(iw).indicesStd != v) {
1050 std::cout << "Inconsistent waveId and Wave::indicesStd"
1051 << std::endl;
1052 return false;
1053 }
1054 }
1055
1056 // Loop over elements of waves_, check consistency of wave data.
1057 for (iw = 0; iw < nWave_; ++iw) {
1058
1059 // Check sqNorm
1060 v = waves_[iw].indicesMin;
1061 Gsq = unitCell().ksq(v);
1062 if (std::abs(Gsq - waves_[iw].sqNorm) > 1.0E-8) {
1063 std::cout << "\n";
1064 std::cout << "Incorrect sqNorm:" << "\n"
1065 << "wave.indicesMin = " << "\n"
1066 << "wave.sqNorm = " << waves_[iw].sqNorm << "\n"
1067 << "|v|^{2} = " << Gsq << "\n";
1068 return false;
1069 }
1070
1071 // Check that wave indicesMin is an image of indicesStd
1072 mesh().shift(v);
1073 if (v != waves_[iw].indicesStd) {
1074 std::cout << "\n";
1075 std::cout << "shift(indicesMin) != indicesStd" << std::endl;
1076 return false;
1077 }
1078
1079 // Compare Wave::starId to Star::beginId and Star::endId
1080 is = waves_[iw].starId;
1081 if (iw < stars_[is].beginId) {
1082 std::cout << "\n";
1083 std::cout << "Wave::starId < Star::beginId" << std::endl;
1084 return false;
1085 }
1086 if (iw >= stars_[is].endId) {
1087 std::cout << "\n";
1088 std::cout << "Wave::starId >= Star::endId" << std::endl;
1089 return false;
1090 }
1091
1092 // Check that inverseId has been assigned
1093 if (waves_[iw].inverseId < 0) {
1094 std::cout << "\n";
1095 std::cout << "Wave::inverseId not assigned\n";
1096 std::cout << "G = " << waves_[iw].indicesMin << std::endl;
1097 return false;
1098 }
1099
1100 // Check that inverseId points to the correct wave
1101 v.negate(waves_[iw].indicesMin);
1102 mesh().shift(v);
1103 iwp = waves_[iw].inverseId;
1104 if (waves_[iwp].indicesStd != v) {
1105 std::cout << "\n";
1106 std::cout << "Wave::inverseId is not inverse" << std::endl;
1107 std::cout << "G = " << waves_[iw].indicesMin << std::endl;
1108 std::cout << "-G (from inverseId) = "
1109 << waves_[iwp].indicesMin << std::endl;
1110 return false;
1111 }
1112
1113 // Check that inverseId of the inverse wave is correct
1114 if (waves_[iwp].inverseId != iw) {
1115 std::cout << "\n";
1116 std::cout << "Wave::inverseId values do not agree\n";
1117 std::cout << "+G = " << waves_[iw].indicesMin << std::endl;
1118 std::cout << "-G = " << waves_[iwp].indicesMin << std::endl;
1119 return false;
1120 }
1121
1122 // Check that either this wave or its inverse is explicit
1123 if (waves_[iw].implicit == true && waves_[iwp].implicit == true)
1124 {
1125 std::cout << "\n";
1126 std::cout << "Wave and its inverse are both implicit";
1127 std::cout << "+G = " << waves_[iw].indicesMin << std::endl;
1128 std::cout << "-G = " << waves_[iwp].indicesMin << std::endl;
1129 return false;
1130 }
1131 }
1132
1133 // Loop over all stars (elements of stars_ array)
1134 int nWave = 0;
1135 for (is = 0; is < nStar_; ++is) {
1136
1137 // Check star size
1138 nWave += stars_[is].size;
1139 if (stars_[is].size != stars_[is].endId - stars_[is].beginId) {
1140 std::cout << "\n";
1141 std::cout << "Inconsistent Star::size:" << std::endl;
1142 std::cout << "Star id " << is << std::endl;
1143 std::cout << "star size " << stars_[is].size << std::endl;
1144 std::cout << "Star begin " << stars_[is].beginId << std::endl;
1145 std::cout << "Star end " << stars_[is].endId << std::endl;
1146 return false;
1147 }
1148 if (is > 0) {
1149 if (stars_[is].beginId != stars_[is-1].endId) {
1150 std::cout << "\n";
1151 std::cout << "Star ranges not consecutive:" << std::endl;
1152 std::cout << "Star id " << is << std::endl;
1153 std::cout << "stars_[" << is << "]" << ".beginId = "
1154 << stars_[is].beginId << std::endl;
1155 std::cout << "stars_[" << is-1 << "]" << ".endId = "
1156 << stars_[is-1].endId << std::endl;
1157 return false;
1158 }
1159 }
1160
1161 // Check waveMin indices of star
1162 if (stars_[is].invertFlag == -1) {
1163 v.negate(stars_[is-1].waveMin);
1164 v = shiftToMinimum(v, mesh().dimensions(), unitCell_);
1165 if (stars_[is].waveMin != v) {
1166 std::cout << "\n";
1167 std::cout << "waveMin of star is not inverse of waveMin "
1168 << "of previous star" << std::endl;
1169 std::cout << "star id " << is << std::endl;
1170 std::cout << "waveMin " << stars_[is].waveMin << std::endl;
1171 std::cout << "waveMin (previous star) "
1172 << stars_[is-1].waveMin << std::endl;
1173 return false;
1174 }
1175 } else {
1176 v = waves_[stars_[is].beginId].indicesMin;
1177 if (stars_[is].waveMin != v) {
1178 std::cout << "\n";
1179 std::cout << "waveMin of star != first wave of star"
1180 << std::endl;
1181 std::cout << "star id " << is << std::endl;
1182 std::cout << "waveMin " << stars_[is].waveMin
1183 << std::endl;
1184 std::cout << "first wave " << v << std::endl;
1185 return false;
1186 }
1187 }
1188
1189 // Check star ids of waves in star
1190 for (iw = stars_[is].beginId; iw < stars_[is].endId; ++iw) {
1191 if (waves_[iw].starId != is) {
1192 std::cout << "\n";
1193 std::cout << "Inconsistent Wave::starId :" << std::endl;
1194 std::cout << "star id " << is << std::endl;
1195 std::cout << "star beginId " << stars_[is].beginId << "\n";
1196 std::cout << "star endId " << stars_[is].endId << "\n";
1197 std::cout << "wave id " << iw << "\n";
1198 std::cout << "wave starId " << waves_[iw].starId << "\n";
1199 return false;
1200 }
1201 }
1202
1203 // Check Star::starId is equal to array index
1204 if (stars_[is].starId != is) {
1205 std::cout << "\n";
1206 std::cout << "stars_[is].starId != is for "
1207 << "is = " << is << "\n";
1208 return false;
1209 }
1210
1211 // Check Star::basisId and starIds_ look up table
1212 ib = stars_[is].basisId;
1213 if (stars_[is].cancel) {
1214 if (ib != -1) {
1215 std::cout << "\n";
1216 std::cout << "basisId != -1 for cancelled star\n";
1217 std::cout << "star id = " << is << "\n";
1218 return false;
1219 }
1220 } else {
1221 if (starIds_[ib] != is) {
1222 std::cout << "\n";
1223 std::cout << "starIds_[stars_[is].basisId] != is for: \n";
1224 std::cout << "is = " << is << "\n";
1225 std::cout << "ib = stars_[is].basisId = " << ib << "\n";
1226 std::cout << "starIds_[ib] = " << starIds_[ib]
1227 << "\n";
1228 return false;
1229 }
1230 }
1231
1232 // Check ordering of waves in star
1233 for (iw = stars_[is].beginId + 1; iw < stars_[is].endId; ++iw) {
1234 if (waves_[iw].indicesMin > waves_[iw-1].indicesMin) {
1235 std::cout << "\n";
1236 std::cout << "Failure of ordering by indicesB within star"
1237 << std::endl;
1238 return false;
1239 }
1240 if (waves_[iw].indicesMin == waves_[iw-1].indicesMin) {
1241 std::cout << "\n";
1242 std::cout << "Equal values of indicesMin within star"
1243 << std::endl;
1244 return false;
1245 }
1246 }
1247
1248 // Check that all coefficients are zero if star is cancelled
1249 if (stars_[is].cancel) {
1250 for (iw = stars_[is].beginId + 1; iw < stars_[is].endId; ++iw)
1251 {
1252 if (std::abs(waves_[iw].coeff) > 1.0E-8) {
1253 std::cout << "\n";
1254 std::cout << "Nonzero coefficient in a cancelled star"
1255 << "\n";
1256 std::cout << "G = " << waves_[iw].indicesMin
1257 << " coeff = " << waves_[iw].coeff
1258 << "\n";
1259 return false;
1260 }
1261 }
1262 }
1263
1264 } // End do loop over all stars
1265
1266 // Check that all waves in mesh are accounted for in stars
1267 if (stars_[nStar_-1].endId != mesh().size()) {
1268 std::cout << "\n";
1269 std::cout << "Star endId of last star != mesh size" << std::endl;
1270 return false;
1271 }
1272 if (nWave != mesh().size()) {
1273 std::cout << "\n";
1274 std::cout << "Sum of star sizes != mesh size" << std::endl;
1275 return false;
1276 }
1277
1278 // Loop over closed stars and related pairs of stars.
1279 // Test closure under inversion and conjugacy of coefficients.
1280 std::complex<double> cdel;
1281 bool cancel;
1282 is = 0;
1283 while (is < nStar_) {
1284 cancel = stars_[is].cancel;
1285
1286 if (stars_[is].invertFlag == 0) {
1287
1288 // Test that star is closed under inversion and real
1289 int begin = stars_[is].beginId;
1290 int end = stars_[is].endId;
1291 for (iw = begin; iw < end; ++iw) {
1292 iwp = waves_[iw].inverseId;
1293 if (waves_[iwp].starId != is) {
1294 std::cout << "\n";
1295 std::cout << "Inverse not found in closed star"
1296 << std::endl;
1297 std::cout << "G = " << waves_[iw].indicesMin
1298 << "coeff = " << waves_[iw].coeff
1299 << std::endl;
1300 std::cout << "All waves in star " << is << "\n";
1301 for (j=begin; j < end; ++j) {
1302 std::cout << waves_[j].indicesMin << " "
1303 << waves_[j].coeff << "\n";
1304 }
1305 return false;
1306 }
1307 if (!cancel) {
1308 cdel = std::conj(waves_[iwp].coeff);
1309 cdel -= waves_[iw].coeff;
1310 if (std::abs(cdel) > 1.0E-8) {
1311 std::cout << "\n";
1312 std::cout << "Function for closed star is not real:"
1313 << "\n";
1314 std::cout << "+G = " << waves_[iw].indicesMin
1315 << " coeff = " << waves_[iw].coeff
1316 << "\n";
1317 std::cout << "-G = " << waves_[iwp].indicesMin
1318 << " coeff = " << waves_[iwp].coeff
1319 << "\n";
1320 std::cout << "Coefficients are not conjugates."
1321 << "\n";
1322 std::cout << "All waves in star " << is << "\n";
1323 for (j=begin; j < end; ++j) {
1324 std::cout << waves_[j].indicesMin << " "
1325 << waves_[j].coeff << "\n";
1326 }
1327 return false;
1328 }
1329 }
1330 }
1331
1332 // Finished processing a closed star, increment counter is
1333 ++is;
1334
1335 } else {
1336
1337 // Test pairs of open stars
1338
1339 if (stars_[is].invertFlag != 1) {
1340 std::cout << "\n";
1341 std::cout << "Expected invertFlag == 1" << std::endl;
1342 return false;
1343 }
1344 if (stars_[is+1].invertFlag != -1) {
1345 std::cout << "\n";
1346 std::cout << "Expected invertFlag == -1" << std::endl;
1347 return false;
1348 }
1349 if (stars_[is+1].size != stars_[is].size) {
1350 std::cout << "\n";
1351 std::cout << "Partner stars of different size" << std::endl;
1352 return false;
1353 }
1354 if (stars_[is+1].cancel != stars_[is].cancel) {
1355 std::cout << "\n";
1356 std::cout << "Partners stars with different cancel flags"
1357 << std::endl;
1358 return false;
1359 }
1360
1361 // Begin and end wave ids for the first and second stars
1362 int begin1 = stars_[is].beginId;
1363 int end1 = stars_[is].endId;
1364 int begin2 = stars_[is+1].beginId;
1365 int end2 = stars_[is+1].endId;
1366
1367 // Check that inverse is in next star and check for
1368 // conjugate coefficients
1369
1370 // Loop over waves in first star
1371 for (iw = begin1; iw < end1; ++iw) {
1372 iwp = waves_[iw].inverseId;
1373 if (waves_[iwp].starId != is + 1) {
1374 std::cout << "\n";
1375 std::cout << "Inverse not found for G in open star"
1376 << std::endl;
1377 std::cout << "First star id = " << is << std::endl;
1378 std::cout << "+G = " << waves_[iw].indicesMin
1379 << "coeff = " << waves_[iw].coeff
1380 << std::endl;
1381 std::cout << "Waves in star " << is
1382 << " (starInvert ==1):" << "\n";
1383 for (j = begin1; j < end1; ++j) {
1384 std::cout << waves_[j].indicesMin << " "
1385 << waves_[j].coeff << "\n";
1386 }
1387 std::cout << "Waves in star " << is+1
1388 << " (starInvert == -1):" << "\n";
1389 for (j=begin2; j < end2; ++j) {
1390 std::cout << waves_[j].indicesMin << " "
1391 << waves_[j].coeff << "\n";
1392 }
1393 return false;
1394 }
1395 if (!cancel) {
1396 cdel = std::conj(waves_[iwp].coeff);
1397 cdel -= waves_[iw].coeff;
1398 if (std::abs(cdel) > 1.0E-8) {
1399 std::cout << "\n";
1400 std::cout << "Error of coefficients in open stars:"
1401 << "\n";
1402 std::cout << "First star id = " << is << std::endl;
1403 std::cout << "+G = " << waves_[iw].indicesMin
1404 << " coeff = " << waves_[iw].coeff
1405 << "\n";
1406 std::cout << "-G = " << waves_[iwp].indicesMin
1407 << " coeff = " << waves_[iwp].coeff
1408 << "\n";
1409 std::cout << "Coefficients are not conjugates."
1410 << "\n";
1411 std::cout << "Waves in star " << is
1412 << " (starInvert ==1):" << "\n";
1413 for (j = begin1; j < end1; ++j) {
1414 std::cout << waves_[j].indicesMin << " "
1415 << waves_[j].coeff << "\n";
1416 }
1417 std::cout << "Waves in star " << is+1
1418 << " (starInvert == -1):" << "\n";
1419 for (j=begin2; j < end2; ++j) {
1420 std::cout << waves_[j].indicesMin << " "
1421 << waves_[j].coeff << "\n";
1422 }
1423 return false;
1424 }
1425 }
1426 }
1427
1428 // Finished processing a pair, increment star counter by 2
1429 is += 2;
1430
1431 } // end if (stars_[is].invertFlag == 0) ... else ...
1432
1433 } // end while (is < nStar_) loop over stars
1434
1435 // Loop over basis functions
1436 for (ib = 0; ib < nBasis_; ++ib) {
1437 is = starIds_[ib];
1438 if (stars_[is].cancel) {
1439 std::cout << "\n";
1440 std::cout << "Star referred to by starIds_ is cancelled\n";
1441 return false;
1442 }
1443 if (stars_[is].basisId != ib) {
1444 std::cout << "\n";
1445 std::cout << "Error: stars_[starIds_[ib]].basisId != ib\n";
1446 std::cout << "Basis function index ib = " << ib << "\n";
1447 std::cout << "is = starIds_[ib] = " << is << "\n";
1448 std::cout << "stars_[is].basisId = "
1449 << stars_[is].basisId << "\n";
1450 return false;
1451 }
1452 }
1453
1454 // The end of this function is reached iff all tests passed.
1455 return true;
1456 }
1457
1458 /*
1459 * Get associated signal (notifies observers of basis initialization).
1460 */
1461 template <int D>
1463 {
1464 UTIL_CHECK(signalPtr_);
1465 return *signalPtr_;
1466 }
1467
1468}
1469#endif
A list of wavevectors that are related by space-group symmetries.
Definition Basis.h:495
~Basis()
Destructor.
Definition Basis.tpp:55
int nBasis() const
Total number of nonzero symmetry-adapted basis functions.
Definition Basis.tpp:962
bool isValid() const
Returns true if this basis is valid, false otherwise.
Definition Basis.tpp:1032
int nWave() const
Total number of wavevectors.
Definition Basis.h:890
void outputStars(std::ostream &out, bool outputAll=false) const
Print a list of all stars to an output stream.
Definition Basis.tpp:998
void makeBasis(Mesh< D > const &mesh, UnitCell< D > const &unitCell, SpaceGroup< D > const &group)
Construct basis for a specific mesh and space group.
Definition Basis.tpp:77
UnitCell< D > const & unitCell() const
Get the unit cell from which this basis was constructed.
Definition Basis.h:922
int waveId(IntVec< D > vector) const
Get the integer index of a wave, as required by wave(int id).
Definition Basis.h:914
Signal< void > & signal()
Get a Signal that is triggered by basis initialization.
Definition Basis.tpp:1462
Basis()
Constructor.
Definition Basis.tpp:34
Wave const & wave(int id) const
Get a specific Wave, access by integer index.
Definition Basis.h:902
void outputWaves(std::ostream &out, bool outputAll=false) const
Print a list of all waves to an output stream.
Definition Basis.tpp:967
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>.
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.
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
Crystallographic space group.
Definition SpaceGroup.h:31
void checkMeshDimensions(IntVec< D > const &dimensions) const
Check if input mesh dimensions are compatible with space group.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
Vec< D, T > & negate(const Vec< D, T > &v)
Return negative of vector v.
Definition Vec.h:520
static const double Pi
Trigonometric constant Pi.
Definition Constants.h:35
Dbl()
Default constructor.
Definition Dbl.cpp:17
An automatically growable array, analogous to a std::vector.
Definition GArray.h:34
int size() const
Return the logical size of this array (number of elements).
Definition GArray.h:450
void clear()
Reset to empty state.
Definition GArray.h:296
void append(Data const &data)
Append an element to the end of the sequence.
Definition GArray.h:303
Int()
Default constructor.
Definition Int.cpp:19
Notifier (or subject) in the Observer design pattern.
Definition Signal.h:39
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
Definition global.h:68
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
Definition global.h:49
double imag(fftw_complex const &a)
Return the imaginary part of an fftw_complex number.
Definition cpp/complex.h:49
double real(fftw_complex const &a)
Return the real part of an fftw_complex number.
Definition cpp/complex.h:38
PSCF package top-level namespace.
Comparator for BWave objects, based on BWave::indicesMin.
Definition BWave.h:75
Comparator for BWave objects, based on BWave::indicesStd.
Definition BWave.h:57
Wave struct designed for use within Basis construction.
Definition BWave.h:24