1#ifndef PSCF_FIELD_IO_UTIL_TPP
2#define PSCF_FIELD_IO_UTIL_TPP
11#include <pscf/crystal/Basis.h>
12#include <pscf/crystal/UnitCell.h>
13#include <pscf/crystal/shiftToMinimum.h>
14#include <pscf/crystal/replicateUnitCell.h>
15#include <pscf/fieldIo/fieldHeader.h>
17#include <pscf/mesh/Mesh.h>
18#include <pscf/mesh/MeshIterator.h>
19#include <pscf/mesh/MeshIteratorFortran.h>
20#include <pscf/math/IntVec.h>
21#include <pscf/math/arithmetic.h>
22#include <pscf/backend/complex.h>
24#include <util/containers/DArray.h>
25#include <util/misc/FileMaster.h>
26#include <util/misc/Log.h>
27#include <util/format/Int.h>
28#include <util/format/Dbl.h>
29#include <util/math/Constants.h>
37 template <
int D,
class AT>
51 for (
int k = 0; k < nMonomer; ++k) {
53 assign(fields[k][rank], value);
59 template <
int D,
class AT>
71 assign(field[rank], value);
75 template <
int D,
class AT>
88 for (j = 0; j < nMonomer; ++j) {
89 out <<
" " <<
Dbl(fields[j][rank], 21, 13);
96 template <
int D,
class AT>
105 out <<
" " <<
Dbl(field[rank], 21, 13);
112 template <
int D,
class ACT>
116 IntVec<D>
const& dftDimensions)
118 using T =
typename ACT::BackendIdClass;
119 using RealT =
typename T::Real;
122 MeshIterator<D> iter(dftDimensions);
123 int rank, i, j, idum;
125 for (iter.begin(); !iter.atEnd(); ++iter) {
130 for (j = 0; j < nMonomer; ++j) {
133 assign(fields[j][rank], x, y);
140 template <
int D,
class ACT>
145 using T =
typename ACT::BackendIdClass;
146 using RealT =
typename T::Real;
152 for (iter.begin(); !iter.atEnd(); ++iter) {
160 assign(field[rank], x, y);
165 template <
int D,
class ACT>
174 using T =
typename ACT::BackendIdClass;
175 using RealT =
typename T::Real;
181 for (iter.begin(); !iter.atEnd(); ++iter) {
185 for (
int j = 0; j < nMonomer; ++j) {
186 x =
real(fields[j][rank]);
187 y =
imag(fields[j][rank]);
192 <<
" " <<
Dbl(y, 20, 13);
200 template <
int D,
class ACT>
205 using T =
typename ACT::BackendIdClass;
206 using RealT =
typename T::Real;
212 for (iter.begin(); !iter.atEnd(); ++iter) {
215 x =
real(field[rank]);
216 y =
imag(field[rank]);
220 <<
" " <<
Dbl(y, 20, 13);
246 int fieldCapacity = fields[0].
capacity();
248 for (
int i = 0; i < nMonomer; ++i) {
249 UTIL_CHECK(fields[i].capacity() == fieldCapacity);
250 for (
int j = 0; j < fieldCapacity; ++j) {
256 int nBasis = nBasisIn;
257 if (fieldCapacity < nBasisIn) {
258 nBasis = fieldCapacity;
271 int basisId, basisId2;
274 std::complex<double> coeff, phasor;
276 int nReversedPair = 0;
277 bool waveExists, sizeMatches;
284 for (
int j = 0; j < nMonomer; ++j) {
296 waveMin = shiftToMinimum(waveIn, mesh.
dimensions(), unitCell);
297 waveExists = (waveIn == waveMin);
304 waveId = basis.
waveId(waveDft);
306 starPtr = &basis.
star(starId);
310 if (starPtr->
size == sizeIn) {
314 <<
"Warning: Inconsistent star size (line ignored)\n"
315 <<
"wave from file = " << waveIn <<
"\n"
316 <<
"size from file = " << sizeIn <<
"\n"
317 <<
"size of star = " << starPtr->
size
324 if (waveExists && sizeMatches) {
328 if (starPtr->
waveMin == waveIn) {
331 for (
int j = 0; j < nMonomer; ++j) {
332 fields[j][basisId] = temp[j];
338 <<
"Inconsistent wave of closed star on input\n"
339 <<
"wave from file = " << waveIn <<
"\n"
340 <<
"starId of wave = " << starId <<
"\n"
341 <<
"waveMin of star = " << starPtr->
waveMin
349 for (
int j = 0; j < nMonomer; ++j) {
359 shiftToMinimum(waveIn2, mesh.
dimensions(), unitCell);
365 waveId2 = basis.
waveId(waveDft);
367 starPtr2 = &basis.
star(starId2);
384 for (
int j = 0; j < nMonomer; ++j) {
385 fields[j][basisId] = temp[j];
386 fields[j][basisId2] = temp2[j];
403 shiftToMinimum(nVec, mesh.
dimensions(), unitCell);
452 phasor = phasor/std::abs(phasor);
453 for (
int j = 0; j < nMonomer; ++j) {
454 coeff = std::complex<double>(temp[j],-temp2[j]);
456 fields[j][basisId2] =
real(coeff);
457 fields[j][basisId ] =
imag(coeff);
473 if (nReversedPair > 0) {
475 Log::file() << nReversedPair <<
" reversed pairs of open stars"
476 <<
" detected in readFieldsBasis\n";
517 int fieldCapacity = fields[0].
capacity();
518 for (
int j = 0; j < nMonomer; ++j) {
519 UTIL_CHECK(fieldCapacity == fields[j].capacity());
525 int nStar = basis.
nStar();
526 for (
int i = 0; i < nStar; ++i) {
527 if (ib >= fieldCapacity)
break;
529 for (
int j = 0; j < nMonomer; ++j) {
530 out <<
Dbl(fields[j][ib], 20, 10);
535 for (
int j = 0; j < D; ++j) {
538 out <<
' ' <<
Int(basis.
star(i).
size, 4) << std::endl;
570 template <
int D,
class ACT>
573 Basis<D>
const& basis,
574 IntVec<D>
const& dftDimensions)
579 Mesh<D> dftMesh(dftDimensions);
581 typename Basis<D>::Star
const* starPtr;
582 typename Basis<D>::Wave
const* wavePtr;
583 std::complex<double> component;
584 std::complex<double> coeff;
592 for (rank = 0; rank < dftMesh.size(); ++rank) {
593 assign(out[rank], 0.0, 0.0);
598 while (is < basis.nStar()) {
599 starPtr = &(basis.star(is));
601 if (starPtr->cancel) {
607 ib = starPtr->basisId;
609 if (starPtr->invertFlag == 0) {
612 component = std::complex<double>(in[ib], 0.0);
615 for (iw = starPtr->beginId; iw < starPtr->endId; ++iw) {
616 wavePtr = &basis.wave(iw);
617 if (!wavePtr->implicit) {
618 coeff = component*(wavePtr->coeff);
619 indices = wavePtr->indicesStd;
620 rank = dftMesh.rank(indices);
629 if (starPtr->invertFlag == 1) {
632 component = std::complex<double>(in[ib], -in[ib+1]);
633 component /= sqrt(2.0);
634 starPtr = &(basis.star(is));
635 for (iw = starPtr->beginId; iw < starPtr->endId; ++iw) {
636 wavePtr = &basis.wave(iw);
637 if (!(wavePtr->implicit)) {
638 coeff = component*(wavePtr->coeff);
639 indices = wavePtr->indicesStd;
640 rank = dftMesh.rank(indices);
648 starPtr = &(basis.star(is+1));
650 component = std::complex<double>(in[ib], +in[ib+1]);
651 component /= sqrt(2.0);
652 for (iw = starPtr->beginId; iw < starPtr->endId; ++iw) {
653 wavePtr = &basis.wave(iw);
654 if (!(wavePtr->implicit)) {
655 coeff = component*(wavePtr->coeff);
656 indices = wavePtr->indicesStd;
657 rank = dftMesh.rank(indices);
677 template <
int D,
class ACT>
690 symmetric =
hasSymmetry(in, basis, dftDimensions, epsilon,
true);
693 <<
"WARNING: Conversion of asymmetric field to"
694 <<
"symmetry-adapted basis in convertKgridToBasis."
695 << std::endl << std::endl;
700 Mesh<D> dftMesh(dftDimensions);
704 std::complex<double> component;
711 for (is = 0; is < basis.nBasis(); ++is) {
717 while (is < basis.nStar()) {
718 starPtr = &(basis.star(is));
720 if (starPtr->cancel) {
726 ib = starPtr->basisId;
728 if (starPtr->invertFlag == 0) {
731 int beginId = starPtr->beginId;
732 int endId = starPtr->endId;
734 bool isImplicit =
true;
736 wavePtr = &basis.wave(beginId + iw);
737 if (!wavePtr->implicit) {
741 wavePtr = &basis.wave(endId - 1 - iw);
742 if (!wavePtr->implicit) {
751 rank = dftMesh.rank(wavePtr->indicesStd);
753 assign(component, in[rank]);
754 component /= wavePtr->coeff;
755 out[ib] = component.real();
759 if (starPtr->invertFlag == 1) {
764 wavePtr = &basis.wave(starPtr->beginId);
766 if (wavePtr->implicit) {
767 iw = wavePtr->inverseId;
768 starPtr = &(basis.star(is+1));
770 wavePtr = &basis.wave(iw);
774 rank = dftMesh.rank(wavePtr->indicesStd);
776 assign(component, in[rank]);
777 UTIL_CHECK(std::abs(wavePtr->coeff) > 1.0E-8);
778 component /= wavePtr->coeff;
779 component *= sqrt(2.0);
782 if (starPtr->invertFlag == 1) {
783 out[ib] = component.real();
784 out[ib+1] = -component.imag();
786 out[ib] = component.real();
787 out[ib+1] = component.imag();
803 template <
int D,
class ACT>
814 std::complex<double> waveCoeff;
815 std::complex<double> rootCoeff;
822 double cancelledError(0.0);
823 double uncancelledError(0.0);
827 Mesh<D> dftMesh(dftDimensions);
830 for (is = 0; is < basis.nStar(); ++is) {
831 starPtr = &(basis.star(is));
833 if (starPtr->cancel) {
836 beginId = starPtr->beginId;
837 endId = starPtr->endId;
838 for (iw = beginId; iw < endId; ++iw) {
839 wavePtr = &basis.wave(iw);
840 if (!wavePtr->implicit) {
841 rank = dftMesh.rank(wavePtr->indicesStd);
842 assign(waveCoeff, in[rank]);
843 error = std::abs(waveCoeff);
844 if (error > cancelledError) {
845 cancelledError = error;
846 if (cancelledError > epsilon) {
856 bool hasRoot =
false;
857 beginId = starPtr->beginId;
858 endId = starPtr->endId;
859 for (iw = beginId; iw < endId; ++iw) {
860 wavePtr = &basis.wave(iw);
861 if (!(wavePtr->implicit)) {
862 rank = dftMesh.rank(wavePtr->indicesStd);
863 assign(waveCoeff, in[rank]);
864 waveCoeff /= wavePtr->coeff;
866 error = std::abs(waveCoeff - rootCoeff);
867 if (error > uncancelledError) {
868 uncancelledError = error;
869 if (uncancelledError > epsilon) {
875 rootCoeff = waveCoeff;
882 if (starPtr->invertFlag == 0) {
883 error = std::abs(rootCoeff.imag());
884 if (error > uncancelledError) {
885 uncancelledError = error;
886 if (uncancelledError > epsilon) {
897 if (!result && verbose) {
900 <<
"Maximum coefficient of a cancelled star: "
901 << cancelledError << std::endl
902 <<
"Maximum error of coefficient for an uncancelled star: "
903 << uncancelledError << std::endl;
911 template <
int D,
class AT>
925 for (
int i = 0; i < D; ++i) {
928 repDimensions[i] = replicas[i] * meshDimensions[i];
929 repSize *= repDimensions[i];
935 for (
int i = 0; i < nMonomer; ++i) {
942 Mesh<D> repMesh(repDimensions);
950 for (
int i = 0; i < nMonomer; ++i) {
953 value = fields[i][iter.
rank()];
954 for (cellIter.
begin(); !cellIter.
atEnd(); ++cellIter) {
956 for (
int j=0; j < D; ++j) {
957 repPosition[j] = position[j]
958 + meshDimensions[j]*cellPosition[j];
960 repRank = repMesh.
rank(repPosition);
961 repFields[i][repRank] = value;
974 std::string gName =
"";
982 template <
int D,
class AT>
998 IntVec<1>
const & meshDimensions,
999 UnitCell<1>
const & unitCell,
1016 cellParameters.
append(unitCell.parameter(0));
1019 std::string gName =
"";
1026 IntVec<2> dimensions;
1027 dimensions[0] = meshDimensions[0];
1028 dimensions[1] = newGridDimensions[0];
1032 if (dimensions[0] == dimensions[1]) {
1033 cell.set(UnitCell<2>::Square, cellParameters);
1035 cellParameters.
append((
double)dimensions[1]/dimensions[0]
1036 * cellParameters[0]);
1042 out <<
"mesh " << std::endl
1043 <<
" " << dimensions << std::endl;
1045 }
else if (d == 3) {
1051 dimensions[0] = meshDimensions[0];
1052 dimensions[1] = newGridDimensions[0];
1053 dimensions[2] = newGridDimensions[1];
1057 if (dimensions[2] == dimensions[1]) {
1058 if (dimensions[1] == dimensions[0]) {
1061 cellParameters.
append((
double)dimensions[1]/dimensions[0]
1062 * cellParameters[0]);
1063 cellParameters.append((
double)dimensions[2]/dimensions[0]
1064 * cellParameters[0]);
1068 if (dimensions[1] == dimensions[0]) {
1069 cellParameters.append((
double)dimensions[2]/dimensions[0]
1070 * cellParameters[0]);
1073 cellParameters.append((
double)dimensions[1]/dimensions[0]
1074 * cellParameters[0]);
1075 cellParameters.append((
double)dimensions[2]/dimensions[0]
1076 * cellParameters[0]);
1083 out <<
"mesh " << std::endl
1084 <<
" " << dimensions << std::endl;
1093 int nReplica = newGridDimensions[0];
1095 nReplica *= newGridDimensions[1];
1099 for (
int i = 0; i < nReplica; ++i) {
1100 for (iter.begin(); !iter.atEnd(); ++iter) {
1102 for (
int j = 0; j < nMonomer; ++j) {
1103 out <<
" " <<
Dbl(fields[j][rank], 21, 13);
1131 dimensions[0] = meshDimensions[0];
1132 dimensions[1] = meshDimensions[1];
1133 dimensions[2] = newGridDimensions[0];
1138 cellParameters.
append(unitCell.parameter(0));
1140 if (newGridDimensions[0] == meshDimensions[0]){
1143 cellParameters.
append((
double)dimensions[2]/dimensions[0]
1144 * cellParameters[0]);
1148 cellParameters.
append(unitCell.parameter(1));
1149 cellParameters.
append((
double)dimensions[2]/dimensions[0]
1150 * cellParameters[0]);
1153 cellParameters.append((
double)dimensions[2]/dimensions[0]
1154 * cellParameters[0]);
1157 cellParameters.append(unitCell.parameter(0));
1158 cellParameters.append((
double)dimensions[2]/dimensions[0]
1159 * cellParameters[0]);
1161 cellParameters.append(0.0);
1162 cellParameters.append(unitCell.parameter(1));
1165 cellParameters.append(unitCell.parameter(1));
1166 cellParameters.append((
double)dimensions[2]/dimensions[0]
1167 * cellParameters[0]);
1169 cellParameters.append(0.0);
1170 cellParameters.append(unitCell.parameter(2));
1173 UTIL_THROW(
"Unrecognized 2D lattice system.");
1179 std::string gName =
"";
1181 out <<
"mesh " << std::endl
1182 <<
" " << dimensions << std::endl;
1185 int nReplica = newGridDimensions[0];
1188 for (
int i = 0; i < nReplica; ++i) {
1189 for (iter.begin(); !iter.atEnd(); ++iter) {
1191 for (
int j = 0; j < nMonomer; ++j) {
1192 out <<
" " <<
Dbl(fields[j][rank], 21, 13);
1208 {
UTIL_THROW(
"expandRGridDimension is invalid when D = 3."); }
A list of wavevectors that are related by space-group symmetries.
int size
Number of wavevectors in this star.
int invertFlag
Index for inversion symmetry of star.
int basisId
Index of basis function associated with this star.
int beginId
Wave index of the first wavevector in this star.
bool cancel
Is this star cancelled, i.e., associated with a zero function?
IntVec< D > waveMin
Minimal indices of the root wave of this star.
Wavevector used to construct a basis function.
std::complex< double > coeff
Coefficient of wave within the associated star basis function.
int starId
Index of the star that contains this wavevector.
Symmetry-adapted Fourier basis for pseudo-spectral SCFT.
bool isInitialized() const
Returns true iff this basis is fully initialized.
Star const & star(int id) const
Get a Star, accessed by integer star index.
int waveId(IntVec< D > vector) const
Get the integer index of a wave, as required by wave(int id).
int nStar() const
Total number of stars.
Wave const & wave(int id) const
Get a specific Wave, access by integer index.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Iterator over points in a mesh in "Fortran" order.
int rank() const
Return the scalar array rank of the associated grid point.
void begin()
Initialize the iterator to the first grid point.
bool atEnd() const
Is this the end (i.e., past the last grid point)?
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.
Description of a regular grid of points in a periodic domain.
IntVec< D > dimensions() const
Get an IntVec<D> of the grid dimensions.
int rank(IntVec< D > const &position) const
Get the rank of a grid point with specified position.
int shift(int &coordinate, int i) const
Shift a periodic coordinate into range.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Vec< D, T > & negate(const Vec< D, T > &v)
Return negative of vector v.
int capacity() const
Return allocated size.
bool isAllocated() const
Does this array have associated data?
static const double Pi
Trigonometric constant Pi.
Dynamically allocatable contiguous array template.
void allocate(int capacity)
Allocate the underlying C array.
Dbl()
Default constructor.
A fixed capacity (static) contiguous array with a variable logical size.
void append(Data const &data)
Append data to the end of the array.
Int()
Default constructor.
static std::ostream & file()
Get log ostream by reference.
#define UTIL_CHECK(condition)
Assertion macro suitable for serial or parallel production code.
#define UTIL_THROW(msg)
Macro for throwing an Exception, reporting function, file and line number.
double imag(fftw_complex const &a)
Return the imaginary part of an fftw_complex number.
void assign(fftw_complex &z, double const &a, double const &b)
Create an fftw_complex from real and imaginary parts, z = a + ib.
double real(fftw_complex const &a)
Return the real part of an fftw_complex number.
void replicateUnitCell(IntVec< 1 > const &replicas, UnitCell< 1 > const &cellIn, UnitCell< 1 > &cellOut)
Create a replicated UnitCell<1>.
bool hasSymmetry(AT const &in, Basis< D > const &basis, IntVec< D > const &dftDimensions, double epsilon=1.0e-8, bool verbose=true)
Check if a k-grid field has the declared space group symmetry.
void writeKGridData(std::ostream &out, DArray< AT > const &fields, int nMonomer, IntVec< D > const &dftDimensions)
Write data for array of k-grid fields, with no header section.
void readRGridData(std::istream &in, DArray< AT > &fields, int nMonomer, IntVec< D > const &dimensions)
Read data for array of r-grid fields, with no header section.
void readBasisData(std::istream &in, DArray< DArray< double > > &fields, UnitCell< D > const &unitCell, Mesh< D > const &mesh, Basis< D > const &basis, int nStarIn)
Read an array of fields in basis format, without a header.
void writeMeshDimensions(std::ostream &out, IntVec< D > const &meshDimensions)
Write mesh dimensions to a field file header.
void convertKGridToBasis(AT const &in, DArray< double > &out, Basis< D > const &basis, IntVec< D > const &dftDimensions, bool checkSymmetry=true, double epsilon=1.0e-8)
Convert a real field from Fourier grid to symmetrized basis.
void writeRGridData(std::ostream &out, DArray< AT > const &fields, int nMonomer, IntVec< D > const &dimensions)
Write data for array of r-grid fields, with no header section.
void expandRGridDimension(std::ostream &out, DArray< AT > const &fields, IntVec< D > const &meshDimensions, UnitCell< D > const &unitCell, int d, DArray< int > newGridDimensions)
Expand the dimensionality of space from D to d.
void writeFieldHeader(std::ostream &out, int ver1, int ver2, UnitCell< D > const &cell, std::string const &groupName, int nMonomer)
Write common part of field header (fortran PSCF format).
void writeBasisData(std::ostream &out, DArray< DArray< double > > const &fields, Basis< D > const &basis)
Write data section of an array of fields in basis format.
void convertBasisToKGrid(DArray< double > const &components, AT &dft, Basis< D > const &basis, IntVec< D > const &dftDimensions)
Convert a real field from symmetrized basis to Fourier grid.
void readKGridData(std::istream &in, DArray< AT > &fields, int nMonomer, IntVec< D > const &dftDimensions)
Read data for array of k-grid fields, with no header section.
PSCF package top-level namespace.