PSCF v1.5.0
FieldIo.tpp
1#ifndef RP_FIELD_IO_TPP
2#define RP_FIELD_IO_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 "FieldIo.h"
12
13#include <pscf/fieldIo/fieldCheck.h>
14#include <pscf/fieldIo/rFieldIo.h>
15#include <pscf/crystal/shiftToMinimum.h>
16#include <pscf/crystal/UnitCell.h>
17#include <pscf/crystal/Basis.h>
18#include <pscf/crystal/SpaceGroup.h>
19#include <pscf/crystal/UnitCell.h>
20#include <pscf/crystal/BFieldComparison.h>
21#include <pscf/field/FFT.h>
22#include <pscf/field/RFieldComparison.h>
23#include <pscf/field/RField.h>
24#include <pscf/fieldIo/rFieldIo.h>
25#include <pscf/fieldIo/fieldCheck.h>
26
27#include <pscf/backend/HostRecvArray.h>
28#include <pscf/backend/HostSendArray.h>
29#include <pscf/backend/VecOp.h>
30#include <pscf/backend/complex.h>
31#include <pscf/mesh/Mesh.h>
32#include <pscf/mesh/MeshIterator.h>
33#include <pscf/mesh/MeshIteratorFortran.h>
34#include <pscf/math/IntVec.h>
35
36#include <util/containers/DArray.h>
37#include <util/containers/DMatrix.h>
38#include <util/misc/FileMaster.h>
39#include <util/misc/Log.h>
40#include <util/format/Str.h>
41#include <util/format/Int.h>
42#include <util/format/Dbl.h>
43
44#include <iomanip>
45#include <string>
46
47namespace Pscf {
48namespace Rp {
49
50 using namespace Util;
51
52 /*
53 * Constructor.
54 */
55 template <int D, class T>
57 : meshPtr_(nullptr),
58 fftPtr_(nullptr),
59 hasGroupPtr_(nullptr),
60 groupNamePtr_(nullptr),
61 groupPtr_(nullptr),
62 basisPtr_(nullptr),
63 fileMasterPtr_(nullptr),
64 nMonomer_(0),
65 isAllocatedBasis_(false),
66 isAllocatedRGrid_(false),
67 isAllocatedKGrid_(false)
68 {}
69
70 // Default destructor
71
72 // Initialization functions
73
74 /*
75 * Create associations with other members of a parent Domain<D> object.
76 */
77 template <int D, class T>
79 Mesh<D> const & mesh,
80 FFT<D,T> const & fft,
81 typename UnitCell<D>::LatticeSystem const & lattice,
82 bool const & hasGroup,
83 std::string const & groupName,
84 SpaceGroup<D> const & group,
86 {
87 meshPtr_ = &mesh;
88 fftPtr_ = &fft;
89 latticePtr_ = &lattice;
90 hasGroupPtr_ = &hasGroup;
91 groupNamePtr_ = &groupName;
92 groupPtr_ = &group;
93 basisPtr_ = &basis;
94 }
95
96 /*
97 * Create an association with a FileMaster.
98 */
99 template <int D, class T>
101 { fileMasterPtr_ = &fileMaster; }
102
103 /*
104 * Set nMonomer, the number of monomer types.
105 */
106 template <int D, class T>
107 void FieldIo<D,T>::setNMonomer(int nMonomer)
108 {
109 // Preconditions - require that function is only called once
110 UTIL_CHECK(nMonomer_ == 0);
111 UTIL_CHECK(nMonomer > 0);
112
113 nMonomer_ = nMonomer;
114 }
115
116 // Field File IO - Symmetry-Adapted Basis Format
118 /*
119 * Read an array of fields in basis format from an input stream.
120 */
121 template <int D, class T>
123 std::istream& in,
124 DArray< DArray<double> >& fields,
125 UnitCell<D>& unitCell) const
126 {
127 // Precondition
128 UTIL_CHECK(hasGroup());
130 // Read header (checks compatibility with space group)
131 int nMonomer;
132 bool isSymmetric;
133 readFieldHeader(in, nMonomer, unitCell, isSymmetric);
134 UTIL_CHECK(isSymmetric);
135 UTIL_CHECK(basis().isInitialized());
136 // Note: readFieldHeader can initialize basis if not done previously
137 int nBasisIn = readNBasis(in);
138
139 // Check allocation of fields container
140 if (fields.isAllocated()) {
141 int nMonomerFields, fieldCapacity;
142 inspectArrays(fields, nMonomerFields, fieldCapacity);
143 UTIL_CHECK(nMonomerFields == nMonomer);
144 UTIL_CHECK(fieldCapacity == basis().nBasis());
145 } else {
146 fields.allocate(nMonomer);
147 for (int i = 0; i < nMonomer; ++i) {
148 fields[i].allocate(basis().nBasis());
149 }
150 }
151
152 // Read field data (components in basis)
153 Pscf::readBasisData(in, fields, unitCell, mesh(), basis(), nBasisIn);
154 }
155
156 /*
157 * Open-close a file and read a set of fields in basis format.
158 */
159 template <int D, class T>
161 std::string filename,
162 DArray<DArray<double> >& fields,
163 UnitCell<D>& unitCell) const
164 {
165
166 std::ifstream file;
167 fileMaster().openInputFile(filename, file);
168 readFieldsBasis(file, fields, unitCell);
169 file.close();
170 }
171
172 /*
173 * Read a single field in basis format from an input stream
174 */
175 template <int D, class T>
177 std::istream& in,
178 DArray<double>& field,
179 UnitCell<D>& unitCell) const
180 {
181 // Local array of fields, of type required by readFieldsBasis
182 DArray< DArray<double> > fields;
183
184 // If single field is allocated, allocate local array fields
185 if (field.isAllocated()) {
186 int capacity = field.capacity();
187 UTIL_CHECK(basis().nBasis() == capacity);
188 fields.allocate(1);
189 fields[0].allocate(capacity);
190 }
191 // Otherwise, pass unallocated fields array to readFieldsBasis,
192 // which will allocate it as necessary.
193
194 // Read file containing a single field
195 readFieldsBasis(in, fields, unitCell);
196
197 // Check that only one field was read from file
198 UTIL_CHECK(fields.capacity() == 1);
200 // Copy data from local array fields to function parameter field.
201 // The assignment operator will allocate the field if necessary.
202 field = fields[0];
203 }
204
205 /*
206 * Open-close a file and read a single field in basis format.
207 */
208 template <int D, class T>
210 std::string filename,
211 DArray<double>& field,
212 UnitCell<D>& unitCell) const
213 {
214 std::ifstream file;
215 fileMaster().openInputFile(filename, file);
216 readFieldBasis(file, field, unitCell);
217 file.close();
218 }
219
220 /*
221 * Write an array of fields in basis format to an output stream.
222 */
223 template <int D, class T>
225 std::ostream &out,
226 DArray< DArray<double> > const & fields,
227 UnitCell<D> const & unitCell) const
228 {
229 // Inspect fields to obtain nMonomer and fieldCapacity
230 int nMonomer;
231 int fieldCapacity;
232 inspectArrays(fields, nMonomer, fieldCapacity);
233
234 // Preconditions
235 UTIL_CHECK(basis().isInitialized());
236 UTIL_CHECK(fieldCapacity <= basis().nBasis());
237 int nBasis = fieldCapacity;
238
239 // Write header
240 bool isSymmetric = true;
241 writeFieldHeader(out, nMonomer, unitCell, isSymmetric);
242 writeNBasis(out, nBasis);
243
244 // Write data (field components)
245 Pscf::writeBasisData(out, fields, basis());
246 }
247
248 /*
249 * Open-close a file, and write an array of fields in basis format.
250 */
251 template <int D, class T>
253 std::string filename,
254 DArray<DArray<double> > const & fields,
255 UnitCell<D> const & unitCell) const
256 {
257 std::ofstream file;
258 fileMaster().openOutputFile(filename, file);
259 writeFieldsBasis(file, fields, unitCell);
260 file.close();
261 }
262
263 /*
264 * Write a single field in basis format to an output stream.
265 */
266 template <int D, class T>
268 std::ostream& out,
269 DArray<double> const & field,
270 UnitCell<D> const & unitCell) const
271 {
272 // Create local array of type required by writeFieldsBasis
273 DArray<DArray<double> > fields;
274 fields.allocate(1);
275 fields[0].allocate(field.capacity());
276
277 // Copy data from input parameter to local array
278 fields[0] = field;
279
280 writeFieldsBasis(out, fields, unitCell);
281 }
282
283 /*
284 * Open-close a file, and write a single field in basis format.
285 */
286 template <int D, class T>
288 std::string filename,
289 DArray<double> const & field,
290 UnitCell<D> const & unitCell) const
291 {
292 std::ofstream file;
293 fileMaster().openOutputFile(filename, file);
294 writeFieldBasis(file, field, unitCell);
295 file.close();
296 }
297
298 // Field IO in r-grid format
299
300 /*
301 * Read an array of fields in r-grid format.
302 */
303 template <int D, class T>
305 std::istream &in,
306 DArray< RField<D,T> >& fields,
307 UnitCell<D>& unitCell) const
308 {
309 // Read header and check fields dimensions
310 int nMonomer;
311 bool isSymmetric;
312 readFieldHeader(in, nMonomer, unitCell, isSymmetric);
313 readMeshDimensions(in, mesh().dimensions());
314 checkAllocateFields(fields, nMonomer, mesh().dimensions());
315
316 // Setup local host arrays
318 associateArrays(hostFields, fields);
319
320 // Read data
321 Pscf::readRGridData(in, hostFields, nMonomer, mesh().dimensions());
322
323 // Copy host -> device
324 copyArrays(fields, hostFields);
325 dissociateArrays(hostFields);
326
327 // Return true iff the header contains a space group declaration
328 return isSymmetric;
329 }
330
331 /*
332 * Open-close a file, and write an array of fields in r-grid format
333 */
334 template <int D, class T>
336 std::string filename,
337 DArray< RField<D,T> >& fields,
338 UnitCell<D>& unitCell) const
339 {
340 std::ifstream file;
341 fileMaster().openInputFile(filename, file);
342 bool isSymmetric;
343 isSymmetric = readFieldsRGrid(file, fields, unitCell);
344 file.close();
345 return isSymmetric;
346 }
347
348 /*
349 * Read the data section of an array of fields in r-grid format.
350 */
351 template <int D, class T>
353 std::istream& in,
354 DArray< RField<D,T> >& fields,
355 int nMonomer) const
356 {
357 // Precondition: Check dimensions of fields
358 checkAllocateFields(fields, nMonomer, mesh().dimensions());
359
360 // Setup local host arrays
362 associateArrays(hostFields, fields);
363
364 // Read data section of file
365 Pscf::readRGridData(in, hostFields, nMonomer, mesh().dimensions());
366
367 // Copy host -> device
368 copyArrays(fields, hostFields);
369 dissociateArrays(hostFields);
370 }
371
372 /*
373 * Read a single field in r-grid format.
374 */
375 template <int D, class T>
377 std::istream &in,
378 RField<D,T> & field,
379 UnitCell<D>& unitCell) const
380 {
381
382 // Read header and check field dimensions
383 int nMonomer;
384 bool isSymmetric;
385 readFieldHeader(in, nMonomer, unitCell, isSymmetric);
386 UTIL_CHECK(nMonomer == 1);
387 readMeshDimensions(in, mesh().dimensions());
388 checkAllocateField(field, mesh().dimensions());
389
390 // Setup local host array
391 HostSendArray<RealT,T> hostField;
392 hostField.associate(field);
393
394 // Read data section with one field
395 Pscf::readRGridData(in, hostField, mesh().dimensions());
396
397 // Copy from host to device
398 field = hostField;
399 hostField.dissociate();
400
401 // Return true iff the header contains a space group declaration
402 return isSymmetric;
403 }
404
405 /*
406 * Open-close a file, and read a single field in r-grid format
407 */
408 template <int D, class T>
410 std::string filename,
411 RField<D,T> & field,
412 UnitCell<D>& unitCell) const
413 {
414 std::ifstream file;
415 fileMaster().openInputFile(filename, file);
416 bool isSymmetric;
417 isSymmetric = readFieldRGrid(file, field, unitCell);
418 file.close();
419 return isSymmetric;
420 }
421
422 /*
423 * Write an array of fields in r-grid format.
424 */
425 template <int D, class T>
427 std::ostream &out,
428 DArray< RField<D,T> > const & fields,
429 UnitCell<D> const & unitCell,
430 bool writeHeader,
431 bool isSymmetric,
432 bool writeMeshSize) const
433 {
434 // Inspect fields array, check field dimensions
435 int nMonomer;
436 IntVec<D> meshDimensions;
437 inspectFields(fields, nMonomer, meshDimensions);
438 UTIL_CHECK(meshDimensions == mesh().dimensions());
439 int meshSize = mesh().size();
440 UTIL_CHECK(fields[0].capacity() == meshSize);
441
442 // Write header
443 if (writeHeader){
444 writeFieldHeader(out, nMonomer, unitCell, isSymmetric);
445 }
446 if (writeMeshSize){
447 writeMeshDimensions(out, meshDimensions);
448 }
449
450 // Copy field data to host container
452 copyArrays(hostFields, fields);
453
454 // Write data section
455 Pscf::writeRGridData(out, hostFields, nMonomer, meshDimensions);
456
457 dissociateArrays(hostFields);
458 }
459
460 /*
461 * Open-close a file, and write an array of fields in r-grid format
462 */
463 template <int D, class T>
465 std::string filename,
466 DArray< RField<D,T> > const & fields,
467 UnitCell<D> const & unitCell,
468 bool isSymmetric) const
469 {
470 std::ofstream file;
471 fileMaster().openOutputFile(filename, file);
472 bool writeHeader = true;
473 bool writeMeshSize = true;
474 writeFieldsRGrid(file, fields, unitCell,
475 writeHeader, isSymmetric, writeMeshSize);
476 file.close();
477 }
478
479 /*
480 * Write a single field in r-grid format.
481 */
482 template <int D, class T>
484 std::ostream &out,
485 RField<D,T> const & field,
486 UnitCell<D> const & unitCell,
487 bool writeHeader,
488 bool isSymmetric) const
489 {
490 IntVec<D> meshDimensions = field.meshDimensions();
491 int meshSize = field.capacity();
492 UTIL_CHECK(meshDimensions == mesh().dimensions());
493 UTIL_CHECK(meshSize == mesh().size());
494
495 // Write header
496 if (writeHeader) {
497 writeFieldHeader(out, 1, unitCell, isSymmetric);
498 writeMeshDimensions(out, meshDimensions);
499 }
500
501 // Copy field data to host container
502 HostRecvArray<RealT,T> hostField;
503 hostField = field;
504
505 // Write data from hostField
506 Pscf::writeRGridData(out, hostField, meshDimensions);
507
508 hostField.dissociate();
509 }
510
511 /*
512 * Open-close a file, and write a single field in r-grid format
513 */
514 template <int D, class T>
516 std::string filename,
517 RField<D,T> const & field,
518 UnitCell<D> const & unitCell,
519 bool isSymmetric) const
520 {
521 std::ofstream file;
522 fileMaster().openOutputFile(filename, file);
523 bool writeHeader = true;
524 writeFieldRGrid(file, field, unitCell, writeHeader, isSymmetric);
525 file.close();
526 }
527
528 // Field IO in k-grid format
529
530 /*
531 * Read an array of fields in k-grid format
532 */
533 template <int D, class T>
535 std::istream &in,
536 DArray< RFieldDft<D,T> >& fields,
537 UnitCell<D>& unitCell) const
539 // Read header and validate field mesh dimensions
540 int nMonomer;
541 bool isSymmetric;
542 readFieldHeader(in, nMonomer, unitCell, isSymmetric);
543 readMeshDimensions(in, mesh().dimensions());
544 checkAllocateFields(fields, nMonomer, mesh().dimensions());
545 IntVec<D> dftDimensions = fields[0].dftDimensions();
546
547 // Allocate hostFields
549 associateArrays(hostFields, fields);
550
551 // Read data into hostFields
552 Pscf::readKGridData(in, hostFields, nMonomer, dftDimensions);
553
554 // Copy host to device
555 copyArrays(fields, hostFields);
556
557 dissociateArrays(hostFields);
558
559 // Return true iff the header contains a space group declaration
560 return isSymmetric;
561 }
562
563 /*
564 * Open-close a file, and read an array of fields in k-grid format
565 */
566 template <int D, class T>
568 std::string filename,
569 DArray< RFieldDft<D,T> >& fields,
570 UnitCell<D>& unitCell) const
571 {
572 std::ifstream file;
573 fileMaster().openInputFile(filename, file);
574 bool isSymmetric;
575 isSymmetric = readFieldsKGrid(file, fields, unitCell);
576 file.close();
577 return isSymmetric;
578 }
579
580 /*
581 * Write an array of fields in k-grid format
582 */
583 template <int D, class T>
585 std::ostream &out,
586 DArray< RFieldDft<D,T> > const & fields,
587 UnitCell<D> const & unitCell,
588 bool isSymmetric) const
589 {
590 // Read header and validate field mesh dimensions
591 int nMonomer;
592 IntVec<D> meshDimensions;
593 inspectFields(fields, nMonomer, meshDimensions);
594 UTIL_CHECK(mesh().dimensions() == meshDimensions);
595 IntVec<D> dftDimensions = fields[0].dftDimensions();
596
597 // Write header
598 writeFieldHeader(out, nMonomer, unitCell, isSymmetric);
599 writeMeshDimensions(out, meshDimensions);
600
601 // Copy data from device to host container
603 copyArrays(hostFields, fields);
605 // Write data from host container
606 Pscf::writeKGridData(out, hostFields, nMonomer, dftDimensions);
607
608 dissociateArrays(hostFields);
609 }
610
611 /*
612 * Open-close a file, and write an array of fields in k-grid format
613 */
614 template <int D, class T>
616 std::string filename,
617 DArray< RFieldDft<D,T> > const & fields,
618 UnitCell<D> const & unitCell,
619 bool isSymmetric) const
620 {
621 std::ofstream file;
622 fileMaster().openOutputFile(filename, file);
623 writeFieldsKGrid(file, fields, unitCell, isSymmetric);
624 file.close();
625 }
626
627 // Field Format Conversion Functions - Basis <-> KGrid
628
629 /*
630 * Convert a single field from basis to k-grid format.
631 */
632 template <int D, class T>
634 DArray<double> const & in,
635 RFieldDft<D,T>& out) const
636 {
638 UTIL_CHECK(out.isAllocated());
639 UTIL_CHECK(in.capacity() > 0);
640 UTIL_CHECK(out.meshDimensions() == mesh().dimensions());
641
642 // Setup host container for k-grid data
644 hostField.associate(out);
645
646 // Convert basis to k-grid on host
647 Pscf::convertBasisToKGrid(in, hostField, basis(),
648 out.dftDimensions());
649
650 // Copy from host to device
651 out = hostField;
652
653 hostField.dissociate();
654 }
655
656 /*
657 * Convert array of fields from basis to k-grid format.
658 */
659 template <int D, class T>
661 DArray< DArray <double> > const & in,
662 DArray< RFieldDft<D,T> >& out) const
664 // Inspect input and output field containers
665 int nMonomer, nMonomerOut, capacity;
666 inspectArrays(in, nMonomer, capacity);
667 IntVec<D> dimensions;
668 inspectFields(out, nMonomerOut, dimensions);
669 UTIL_CHECK(nMonomer == nMonomerOut);
670 UTIL_CHECK(capacity == basis().nBasis());
671 UTIL_CHECK(dimensions == mesh().dimensions());
672
673 // Convert fields for all monomer types
674 for (int i = 0; i < nMonomer; ++i) {
675 convertBasisToKGrid(in[i], out[i]);
676 }
677 }
678
679 /*
680 * Convert a field file from basis to k-grid format.
681 */
682 template <int D, class T>
684 std::string const & inFileName,
685 std::string const & outFileName) const
686 {
687 checkAllocateKGrid();
688 checkAllocateBasis(inFileName);
689 UnitCell<D> tmpUnitCell;
690 readFieldsBasis(inFileName, tmpFieldsBasis_, tmpUnitCell);
691 convertBasisToKGrid(tmpFieldsBasis_, tmpFieldsKGrid_);
692 writeFieldsKGrid(outFileName, tmpFieldsKGrid_, tmpUnitCell);
693 }
694
695 /*
696 * Write a single field from k-grid to basis format.
697 */
698 template <int D, class T>
700 RFieldDft<D,T> const & in,
701 DArray<double>& out,
702 bool checkSymmetry,
703 double epsilon) const
704 {
705 UTIL_CHECK(in.isAllocated());
706 UTIL_CHECK(out.isAllocated());
707 UTIL_CHECK(in.meshDimensions() == mesh().dimensions());
708 UTIL_CHECK(out.capacity() > 0);
709
710 // Copy k-grid data from device to const host container
712 hostField = in;
713
714 // Convert from k-grid to basis format on host
715 Pscf::convertKGridToBasis(hostField, out, basis(),
716 in.dftDimensions(),
717 checkSymmetry, epsilon);
718
719 hostField.dissociate();
720 }
721
722 /*
723 * Convert array of fields from k-grid format to basis format.
724 */
725 template <int D, class T>
727 DArray< RFieldDft<D,T> > const & in,
728 DArray< DArray <double> > & out,
729 bool checkSymmetry,
730 double epsilon) const
731 {
732 // Inspect input and output containers
733 int nMonomer, nMonomerOut, capacity;
734 IntVec<D> dimensions;
735 inspectFields(in, nMonomer, dimensions);
736 inspectArrays(out, nMonomerOut, capacity);
737 UTIL_CHECK(nMonomer == nMonomerOut);
738
739 // Convert fields for all monomer types
740 for (int i = 0; i < nMonomer; ++i) {
741 convertKGridToBasis(in[i], out[i], checkSymmetry, epsilon);
742 }
743 }
744
745 /*
746 * Convert a field file from k-grid to basis format.
747 */
748 template <int D, class T>
750 std::string const & inFileName,
751 std::string const & outFileName) const
752 {
753 checkAllocateKGrid();
754 checkAllocateBasis(inFileName);
755 UnitCell<D> tmpUnitCell;
756 readFieldsKGrid(inFileName, tmpFieldsKGrid_, tmpUnitCell);
757 convertKGridToBasis(tmpFieldsKGrid_, tmpFieldsBasis_);
758 writeFieldsBasis(outFileName, tmpFieldsBasis_, tmpUnitCell);
759 }
760
761 // Field Format Conversion Functions - Basis <-> RGrid
762
763 /*
764 * Convert a single field from basis to r-grid format.
765 */
766 template <int D, class T>
768 DArray<double> const & in,
769 RField<D,T>& out) const
770 {
772 UTIL_CHECK(out.isAllocated());
773 checkAllocateField(workDft_, mesh().dimensions());
774 convertBasisToKGrid(in, workDft_);
775 fft().inverseTransformUnsafe(workDft_, out);
776 }
777
778 /*
779 * Convert an array of fields from basis to r-grid format.
780 */
781 template <int D, class T>
783 DArray< DArray <double> > const & in,
784 DArray< RField<D,T> >& out) const
785 {
786 UTIL_CHECK(in.isAllocated());
787 UTIL_CHECK(out.isAllocated());
788 UTIL_CHECK(in.capacity() == out.capacity());
789 checkAllocateField(workDft_, mesh().dimensions());
790
791 int n = in.capacity();
792 for (int i = 0; i < n; ++i) {
793 convertBasisToKGrid(in[i], workDft_);
794 fft().inverseTransformUnsafe(workDft_, out[i]);
795 }
796 }
797
798 /*
799 * Convert a field file from basis to r-grid format.
800 */
801 template <int D, class T>
803 std::string const & inFileName,
804 std::string const & outFileName) const
805 {
806 checkAllocateRGrid();
807 checkAllocateBasis(inFileName);
808 UnitCell<D> tmpUnitCell;
809
810 readFieldsBasis(inFileName, tmpFieldsBasis_, tmpUnitCell);
811 convertBasisToRGrid(tmpFieldsBasis_, tmpFieldsRGrid_);
812 writeFieldsRGrid(outFileName, tmpFieldsRGrid_, tmpUnitCell);
813 }
814
815 /*
816 * Convert a single field from r-grid to basis format.
817 */
818 template <int D, class T>
820 RField<D,T> const & in,
821 DArray<double> & out,
822 bool checkSymmetry,
823 double epsilon) const
824 {
825 checkAllocateField(workDft_, mesh().dimensions());
826 fft().forwardTransform(in, workDft_);
827 convertKGridToBasis(workDft_, out, checkSymmetry, epsilon);
828 }
829
830 /*
831 * Convert an array of fields from r-grid to basis format.
832 */
833 template <int D, class T>
835 DArray< RField<D,T> > const & in,
836 DArray< DArray <double> > & out,
837 bool checkSymmetry,
838 double epsilon) const
839 {
840 // Inspect input and output containers
841 int nMonomer, nMonomerOut, capacity;
842 IntVec<D> dimensions;
843 inspectFields(in, nMonomer, dimensions);
844 UTIL_CHECK(mesh().dimensions() == dimensions);
845 inspectArrays(out, nMonomerOut, capacity);
846 UTIL_CHECK(nMonomer == nMonomerOut);
847 checkAllocateField(workDft_, dimensions);
848
849 // Convert RGrid -> KGrid -> Basis for each field
850 for (int i = 0; i < nMonomer; ++i) {
851 fft().forwardTransform(in[i], workDft_);
852 convertKGridToBasis(workDft_, out[i], checkSymmetry, epsilon);
853 }
854 }
855
856 /*
857 * Convert a field file from r-grid to basis format.
858 */
859 template <int D, class T>
861 std::string const & inFileName,
862 std::string const & outFileName) const
863 {
864 checkAllocateRGrid();
865 checkAllocateBasis(inFileName);
866 UnitCell<D> tmpUnitCell;
867 readFieldsRGrid(inFileName, tmpFieldsRGrid_, tmpUnitCell);
868 convertRGridToBasis(tmpFieldsRGrid_, tmpFieldsBasis_);
869 writeFieldsBasis(outFileName, tmpFieldsBasis_, tmpUnitCell);
870 }
871
872 // FFT conversion functions (KGrid <-> RGrid) [Same in Rpc and Rpg]
873
874 /*
875 * Apply inverse FFT to an array of k-grid fields, converting to r-grid.
876 */
877 template <int D, class T>
879 DArray< RFieldDft<D,T> > const & in,
880 DArray< RField<D,T> >& out) const
881 {
882 UTIL_ASSERT(in.capacity() == out.capacity());
883 int n = in.capacity();
884 for (int i = 0; i < n; ++i) {
885 fft().inverseTransformSafe(in[i], out[i]);
886 }
887 }
888
889 /*
890 * Apply inverse FFT to a single k-grid field, converting to r-grid.
891 */
892 template <int D, class T>
894 RFieldDft<D,T> const & in,
895 RField<D,T>& out)
896 const
897 {
898 fft().inverseTransformSafe(in, out);
899 }
900
901 /*
902 * Apply forward FFT to an array of r-grid fields, converting to k-grid.
903 */
904 template <int D, class T>
906 DArray< RField<D,T> > const & in,
907 DArray< RFieldDft<D,T> >& out) const
908 {
909 UTIL_ASSERT(in.capacity() == out.capacity());
910 int n = in.capacity();
911 for (int i = 0; i < n; ++i) {
912 fft().forwardTransform(in[i], out[i]);
913 }
914 }
915
916 /*
917 * Apply forward FFT to a single r-grid field, converting to k-grid.
918 */
919 template <int D, class T>
921 RField<D,T> const & in,
922 RFieldDft<D,T>& out) const
923 { fft().forwardTransform(in, out); }
924
925 /*
926 * Convert a field file from k-grid to r-grid format.
927 */
928 template <int D, class T>
930 std::string const & inFileName,
931 std::string const & outFileName) const
932 {
933 checkAllocateKGrid();
934 checkAllocateRGrid();
935 UnitCell<D> tmpUnitCell;
936 bool isSymmetric;
937 isSymmetric = readFieldsKGrid(inFileName, tmpFieldsKGrid_,
938 tmpUnitCell);
939 for (int i = 0; i < nMonomer_; ++i) {
940 fft().inverseTransformUnsafe(tmpFieldsKGrid_[i],
941 tmpFieldsRGrid_[i]);
942 }
943 // Declare the group only if the input file and the system have one
944 writeFieldsRGrid(outFileName, tmpFieldsRGrid_, tmpUnitCell,
945 isSymmetric && hasGroup());
946 }
947
948 /*
949 * Convert a field file from r-grid to k-grid format.
950 */
951 template <int D, class T>
953 std::string const & inFileName,
954 std::string const & outFileName) const
955 {
956 checkAllocateRGrid();
957 checkAllocateKGrid();
958 UnitCell<D> tmpUnitCell;
959 bool isSymmetric;
960 isSymmetric = readFieldsRGrid(inFileName, tmpFieldsRGrid_,
961 tmpUnitCell);
962 convertRGridToKGrid(tmpFieldsRGrid_, tmpFieldsKGrid_);
963 // Declare the group only if the input file and the system have one
964 writeFieldsKGrid(outFileName, tmpFieldsKGrid_, tmpUnitCell,
965 isSymmetric && hasGroup());
966 }
967
968 // Field Inspection
970 /*
971 * Test if an real field DFT has the declared space group symmetry.
972 */
973 template <int D, class T>
975 RFieldDft<D,T> const & in,
976 double epsilon,
977 bool verbose) const
978 {
979 UTIL_CHECK(in.isAllocated());
980 UTIL_CHECK(in.meshDimensions() == mesh().dimensions());
981
982 // Copy k-grid data from device to const host container
984 hostField = in;
985
986 // Check symmetry of k-grid data on host, return result
987 bool result;
988 result = Pscf::hasSymmetry(hostField,
989 basis(), in.dftDimensions(),
990 epsilon, verbose);
991 hostField.dissociate();
992
993 return result;
994 }
995
996 /*
997 * Test if a single r-grid field has declared space group symmetry.
998 * Return true if symmetric, false otherwise. Print error values
999 * if verbose == true and hasSymmetry == false.
1000 */
1001 template <int D, class T>
1003 RField<D,T> const & in,
1004 double epsilon,
1005 bool verbose) const
1007 checkAllocateField(workDft_, mesh().dimensions());
1008 fft().forwardTransform(in, workDft_);
1009 return hasSymmetry(workDft_, epsilon, verbose);
1010 }
1011
1012 /*
1013 * Check if r-grid fields have declared space group symmetry.
1014 */
1015 template <int D, class T>
1017 std::string const & inFileName,
1018 double epsilon) const
1019 {
1020 checkAllocateRGrid();
1021
1022 // Read fields
1023 UnitCell<D> tmpUnitCell;
1024 readFieldsRGrid(inFileName, tmpFieldsRGrid_, tmpUnitCell);
1025
1026 // Check symmetry for all fields
1027 for (int i = 0; i < nMonomer_; ++i) {
1028 bool symmetric;
1029 symmetric = hasSymmetry(tmpFieldsRGrid_[i], epsilon);
1030 if (!symmetric) {
1031 return false;
1032 }
1033 }
1034 return true;
1035 }
1036
1037 /*
1038 * Compare two fields in basis format, write report to Log file.
1039 */
1040 template <int D, class T>
1042 DArray< DArray<double> > const & field1,
1043 DArray< DArray<double> > const & field2) const
1044 {
1045 BFieldComparison comparison(1);
1046 comparison.compare(field1, field2);
1047
1048 Log::file() << "\n Basis expansion field comparison results"
1049 << std::endl;
1050 Log::file() << " Maximum Absolute Difference: "
1051 << comparison.maxDiff() << std::endl;
1052 Log::file() << " Root-Mean-Square Difference: "
1053 << comparison.rmsDiff() << "\n" << std::endl;
1054 }
1055
1056 template <int D, class T>
1058 std::string const & filename1,
1059 std::string const & filename2) const
1060 {
1061 DArray< DArray<double> > fields1, fields2;
1062 UnitCell<D> tmpUnitCell;
1063 // Unallocated arrays will be allocated in readFieldsBasis
1064 readFieldsBasis(filename1, fields1, tmpUnitCell);
1065 readFieldsBasis(filename2, fields2, tmpUnitCell);
1066 compareFieldsBasis(fields1, fields2);
1067 }
1068
1069 /*
1070 * Compare arrays of fields in r-grid format, output report to Log file.
1071 */
1072 template <int D, class T>
1074 DArray< RField<D,T> > const & field1,
1075 DArray< RField<D,T> > const & field2) const
1076 {
1077 RFieldComparison<D,T> comparison;
1078 comparison.compare(field1, field2);
1079
1080 Log::file() << "\n Real-space field comparison results"
1081 << std::endl;
1082 Log::file() << " Maximum Absolute Difference: "
1083 << comparison.maxDiff() << std::endl;
1084 Log::file() << " Root-Mean-Square Difference: "
1085 << comparison.rmsDiff() << "\n" << std::endl;
1086 }
1087
1088 template <int D, class T>
1090 std::string const & filename1,
1091 std::string const & filename2) const
1092 {
1093 DArray< RField<D,T> > fields1, fields2;
1094 UnitCell<D> tmpUnitCell;
1095 // Unallocated arrays will be allocated in readFieldsRGrid
1096 readFieldsRGrid(filename1, fields1, tmpUnitCell);
1097 readFieldsRGrid(filename2, fields2, tmpUnitCell);
1098 compareFieldsRGrid(fields1, fields2);
1099 }
1100
1101 // Field Scaling
1102
1103 /*
1104 * Multiply a single field in basis format by a constant factor.
1105 */
1106 template <int D, class T>
1108 DArray<double> & field,
1109 double factor) const
1110 {
1111 UTIL_CHECK(field.isAllocated());
1112 int n = field.capacity();
1113 for (int i = 0; i < n; ++i) {
1114 field[i] *= factor;
1115 }
1116 }
1117
1118 /*
1119 * Rescale an array of basis format fields by a constant factor.
1120 */
1121 template <int D, class T>
1123 DArray< DArray<double> >& fields,
1124 double factor) const
1125 {
1126 int n = fields.capacity();
1127 UTIL_CHECK(n > 0);
1128 int m = fields[0].capacity();
1129 for (int i = 0; i < n; ++i) {
1130 UTIL_CHECK(fields[i].capacity() == m);
1131 scaleFieldBasis(fields[i], factor);
1132 }
1133 }
1134
1135 /*
1136 * Rescale basis format fields in files by a constant factor.
1137 */
1138 template <int D, class T>
1140 std::string const & inFileName,
1141 std::string const & outFileName,
1142 double factor) const
1143 {
1144 checkAllocateBasis(inFileName);
1145 UnitCell<D> tmpUnitCell;
1146 readFieldsBasis(inFileName, tmpFieldsBasis_, tmpUnitCell);
1147 scaleFieldsBasis(tmpFieldsBasis_, factor);
1148 writeFieldsBasis(outFileName, tmpFieldsBasis_, tmpUnitCell);
1149 }
1150
1151 /*
1152 * Rescale a single r-grid field by a constant factor.
1153 */
1154 template <int D, class T>
1156 RField<D,T> & field,
1157 double factor) const
1158 {
1159 UTIL_CHECK(field.isAllocated());
1160 VecOp::mulEqS(field, factor);
1161 }
1162
1163 /*
1164 * Rescale r-grid fields by a constant factor.
1165 */
1166 template <int D, class T>
1168 DArray< RField<D,T> > & fields,
1169 double factor) const
1170 {
1171 int n = fields.capacity();
1172 for (int i = 0; i < n; ++i) {
1173 scaleFieldRGrid(fields[i], factor);
1174 }
1175 }
1176
1177 /*
1178 * Rescale r-grid fields in files by a constant factor.
1179 */
1180 template <int D, class T>
1182 std::string const & inFileName,
1183 std::string const & outFileName,
1184 double factor) const
1185 {
1186 checkAllocateRGrid();
1187 UnitCell<D> tmpUnitCell;
1188 bool isSymmetric;
1189 isSymmetric = readFieldsRGrid(inFileName, tmpFieldsRGrid_,
1190 tmpUnitCell);
1191 scaleFieldsRGrid(tmpFieldsRGrid_, factor);
1192 // Declare the group only if the input file and the system have one
1193 writeFieldsRGrid(outFileName, tmpFieldsRGrid_, tmpUnitCell,
1194 isSymmetric && hasGroup());
1195 }
1196
1197 // Computation of Estimated W Fields
1198
1199 /*
1200 * Convert an array of c fields to estimated w fields, in basis form.
1201 */
1202 template <int D, class T>
1204 DMatrix<double> const & chi,
1205 DArray< DArray<double> > & fields) const
1206 {
1207 // Check dimensions
1208 UTIL_CHECK(fields.capacity() == nMonomer_);
1209 UTIL_CHECK(chi.capacity1() == nMonomer_);
1210 UTIL_CHECK(chi.capacity2() == nMonomer_);
1211 const int nb = fields[0].capacity();
1212 for (int i = 0; i < nMonomer_; ++i) {
1213 UTIL_CHECK(fields[i].capacity() == nb);
1214 }
1215
1216 // Allocate small work array (one element per monomer type)
1217 DArray<double> wtmp;
1218 wtmp.allocate(nMonomer_);
1219
1220 // Compute estimated w fields from c fields - convert in place
1221 int i, j, k;
1222 for (i = 0; i < nb; ++i) {
1223 for (j = 0; j < nMonomer_; ++j) {
1224 wtmp[j] = 0.0;
1225 for (k = 0; k < nMonomer_; ++k) {
1226 wtmp[j] += chi(j,k)*fields[k][i];
1227 }
1228 }
1229 for (j = 0; j < nMonomer_; ++j) {
1230 fields[j][i] = wtmp[j];
1231 }
1232 }
1233
1234 }
1235
1236 /*
1237 * Convert a file of c fields to estimated w fields, in basis format.
1238 */
1239 template <int D, class T>
1241 std::string const & inFileName,
1242 std::string const & outFileName,
1243 DMatrix<double> const & chi) const
1244 {
1245 // Preconditions
1246 UTIL_CHECK(chi.capacity1() == nMonomer_);;
1247 UTIL_CHECK(chi.capacity2() == nMonomer_);;
1248
1249 // Set and check nb = number of basis functions
1250 checkAllocateBasis(inFileName);
1251 UTIL_CHECK(basis().isInitialized());
1252 const int nb = basis().nBasis();
1253 UTIL_CHECK(nb > 0);
1254
1255 // Open input file
1256 std::ifstream in;
1257 fileMaster().openInputFile(inFileName, in);
1258
1259 // Read input file header
1260 int nMonomerIn;
1261 bool isSymmetric;
1262 UnitCell<D> tmpUnitCell;
1263 readFieldHeader(in, nMonomerIn, tmpUnitCell, isSymmetric);
1264 int nBasisIn = readNBasis(in);
1265 UTIL_CHECK(nMonomer_ == nMonomerIn);
1266 UTIL_CHECK(isSymmetric);
1267
1268 // Read c field data into tmpFieldsBasis_
1269 Pscf::readBasisData(in, tmpFieldsBasis_,
1270 tmpUnitCell, mesh(), basis(), nBasisIn);
1271 in.close();
1272
1273 // Compute estimated w, stored in tmpFilesBasis_
1274 estimateWBasis(chi, tmpFieldsBasis_);
1275
1276 // Open and write output file
1277 std::ofstream out;
1278 fileMaster().openOutputFile(outFileName, out);
1279 writeFieldsBasis(out, tmpFieldsBasis_, tmpUnitCell);
1280 }
1281
1282
1283 // Grid manipulation utilities
1284
1285 /*
1286 * Replicate the unit cell for an array of r-grid fields.
1287 */
1288 template <int D, class T>
1290 std::ostream &out,
1291 DArray< RField<D,T> > const & fields,
1292 UnitCell<D> const & unitCell,
1293 IntVec<D> const & replicas) const
1294
1295 {
1296 // Inspect fields to obtain nMonomer and meshDimensions
1297 int nMonomer;
1298 IntVec<D> meshDimensions;
1299 inspectFields(fields, nMonomer, meshDimensions);
1300 UTIL_CHECK(meshDimensions == mesh().dimensions());
1301
1302 // Copy r-grid input from device to host
1303 DArray< HostRecvArray<RealT,T> > hostFields;
1304 copyArrays(hostFields, fields);
1305
1306 // Compute replicated fields and write to a file
1307 Pscf::replicateUnitCell(out, hostFields, meshDimensions,
1308 unitCell, replicas);
1309
1310 dissociateArrays(hostFields);
1311 }
1312
1313 /*
1314 * Expand spatial dimension of an array of r-grid fields.
1315 */
1316 template <int D, class T>
1318 std::ostream &out,
1319 DArray< RField<D,T> > const & fields,
1320 UnitCell<D> const & unitCell,
1321 int d,
1322 DArray<int> const& newGridDimensions) const
1323 {
1324 // Inspect fields to obtain nMonomer and meshDimensions
1325 int nMonomer;
1326 IntVec<D> meshDimensions;
1327 inspectFields(fields, nMonomer, meshDimensions);
1328 UTIL_CHECK(meshDimensions == mesh().dimensions());
1329
1330 // Copy k-grid data from device to const host container
1331 DArray< HostRecvArray<RealT,T> > hostFields;
1332 copyArrays(hostFields, fields);
1333
1334 Pscf::expandRGridDimension(out, hostFields, meshDimensions,
1335 unitCell, d, newGridDimensions);
1336
1337 dissociateArrays(hostFields);
1338 }
1339
1340 /*
1341 * Replicate unit cell a specified number of times in each direction.
1342 */
1343 template <int D, class T>
1345 std::string filename,
1346 DArray<RField<D,T> > const & fields,
1347 UnitCell<D> const & unitCell,
1348 IntVec<D> const & replicas) const
1349 {
1350 std::ofstream file;
1351 fileMaster().openOutputFile(filename, file);
1352 replicateUnitCell(file, fields, unitCell, replicas);
1353 file.close();
1354 }
1355
1356 /*
1357 * Replicate unit cell a specified number of times in each direction.
1358 */
1359 template <int D, class T>
1361 std::string const & inFileName,
1362 std::string const & outFileName,
1363 IntVec<D> const & replicas) const
1364 {
1365 checkAllocateRGrid();
1366 UnitCell<D> tmpUnitCell;
1367 readFieldsRGrid(inFileName, tmpFieldsRGrid_, tmpUnitCell);
1368 replicateUnitCell(outFileName, tmpFieldsRGrid_, tmpUnitCell,
1369 replicas);
1370 }
1371
1372 /*
1373 * Expand the number of spatial dimensions of an RField.
1374 */
1375 template <int D, class T>
1377 std::string filename,
1378 DArray<RField<D,T> > const & fields,
1379 UnitCell<D> const & unitCell, int d,
1380 DArray<int> newGridDimensions) const
1381 {
1382 std::ofstream file;
1383 fileMaster().openOutputFile(filename, file);
1384 expandRGridDimension(file, fields, unitCell, d, newGridDimensions);
1385 file.close();
1386 }
1387
1388 /*
1389 * Expand the number of spatial dimensions of an RField.
1390 */
1391 template <int D, class T>
1393 std::string const & inFileName,
1394 std::string const & outFileName,
1395 int d,
1396 DArray<int> newGridDimensions) const
1397 {
1398 checkAllocateRGrid();
1399 UnitCell<D> tmpUnitCell;
1400 readFieldsRGrid(inFileName, tmpFieldsRGrid_, tmpUnitCell);
1401 expandRGridDimension(outFileName, tmpFieldsRGrid_, tmpUnitCell,
1402 d, newGridDimensions);
1403 }
1404
1405 // File Header IO Utilities
1406
1407 /*
1408 * Read common part of field header.
1409 *
1410 * Extracts number of monomers (i.e., number of fields) and unitCell
1411 * data (lattice type and parameters) from the file, and returns these
1412 * via non-const reference parameters nMonomer and unitCell.
1413 *
1414 * Also validates lattice type and groupName (if any), and constructs
1415 * a symmetry-adapted basis if there is a group but no basis.
1416 */
1417 template <int D, class T>
1419 std::istream& in,
1420 int& nMonomer,
1421 UnitCell<D>& unitCell,
1422 bool & isSymmetric) const
1423 {
1424 // Preconditions
1425 UTIL_CHECK(latticePtr_);
1426 if (unitCell.lattice() == UnitCell<D>::Null) {
1427 UTIL_CHECK(unitCell.nParameter() == 0);
1428 } else {
1429 UTIL_CHECK(unitCell.nParameter() > 0);
1430 UTIL_CHECK(unitCell.lattice() == lattice());
1431 }
1432
1433 // Read field header to set unitCell, groupNameIn, nMonomer
1434 int ver1, ver2;
1435 std::string groupNameIn;
1436
1437 Pscf::readFieldHeader(in, ver1, ver2, unitCell,
1438 groupNameIn, nMonomer);
1439 // Note: Function definition in pscf/fieldIo/fieldHeader.tpp
1440
1441 // Checks of data from header
1442 UTIL_CHECK(ver1 == 1);
1443 //UTIL_CHECK(ver2 == 0);
1444 UTIL_CHECK(unitCell.isInitialized());
1445 UTIL_CHECK(unitCell.lattice() != UnitCell<D>::Null);
1446 UTIL_CHECK(unitCell.nParameter() > 0);
1447
1448 // Validate or initialize lattice type
1449 if (lattice() != unitCell.lattice()) {
1450 Log::file() << std::endl
1451 << "Error - Mismatched lattice types "
1452 << "in FieldIo function readFieldHeader:\n"
1453 << " FieldIo::lattice :" << lattice() << "\n"
1454 << " Unit cell lattice :" << unitCell.lattice()
1455 << "\n";
1456 UTIL_THROW("Mismatched lattice types");
1457 }
1458
1459 // Check for presence of group name in the field file header
1460 isSymmetric = false;
1461 if (groupNameIn != "") {
1462 isSymmetric = true;
1463 }
1464
1465 // Process group and basis (if any)
1466 if (hasGroup()) {
1467
1468 // Check consistency of groupName values
1469 if (isSymmetric) {
1470 UTIL_CHECK(groupNamePtr_);
1471 if (groupNameIn != groupName()) {
1472 Log::file() << std::endl
1473 << "Error - Mismatched group names in "
1474 << "FieldIo member function readFieldHeader:\n"
1475 << " FieldIo::groupName :" << groupName() << "\n"
1476 << " Field file header :" << groupNameIn << "\n";
1477 UTIL_THROW("Mismatched group names");
1478 }
1479 }
1480
1481 // If there is a group but no basis, construct a basis
1482 UTIL_CHECK(basisPtr_);
1483 if (!basis().isInitialized()) {
1484 basisPtr_->makeBasis(mesh(), unitCell, group());
1485 }
1486 UTIL_CHECK(basis().isInitialized());
1487
1488 } else {
1489
1490 if (isSymmetric) {
1491 Log::file() << std::endl
1492 << "Warning: Group name found in a field file header"
1493 << "but no group declared in the parameter file.\n";
1494 }
1495
1496 }
1497
1498 }
1499
1500 /*
1501 * Write a field file header.
1502 */
1503 template <int D, class T>
1505 std::ostream &out,
1506 int nMonomer,
1507 UnitCell<D> const & unitCell,
1508 bool isSymmetric) const
1509 {
1510 int v1 = 1;
1511 int v2 = 0;
1512 std::string gName = "";
1513 if (isSymmetric) {
1514 UTIL_CHECK(hasGroup());
1515 gName = groupName();
1516 }
1517 Pscf::writeFieldHeader(out, v1, v2, unitCell,
1518 gName, nMonomer);
1519 // Note: This function is defined in pscf/fieldIo/fieldHeader.tpp
1520 }
1521
1522 // Protected functions to check and allocate private workspace arrays
1523
1524 /*
1525 * If necessary, allocate r-grid workspace.
1526 */
1527 template <int D, class T>
1528 void FieldIo<D,T>::checkAllocateRGrid() const
1529 {
1530 if (isAllocatedRGrid_) return;
1531
1532 UTIL_CHECK(nMonomer_ > 0);
1533 UTIL_CHECK(mesh().size() > 0);
1534 IntVec<D> const & meshDimensions = mesh().dimensions();
1535 tmpFieldsRGrid_.allocate(nMonomer_);
1536 for (int i = 0; i < nMonomer_; ++i) {
1537 tmpFieldsRGrid_[i].allocate(meshDimensions);
1538 }
1539 isAllocatedRGrid_ = true;
1540 }
1541
1542 /*
1543 * If necessary, allocate k-grid field workspace.
1544 */
1545 template <int D, class T>
1546 void FieldIo<D,T>::checkAllocateKGrid() const
1547 {
1548 if (isAllocatedKGrid_) return;
1549
1550 UTIL_CHECK(nMonomer_ > 0);
1551 UTIL_CHECK(mesh().size() > 0);
1552 IntVec<D> const & meshDimensions = mesh().dimensions();
1553 tmpFieldsKGrid_.allocate(nMonomer_);
1554 for (int i = 0; i < nMonomer_; ++i) {
1555 tmpFieldsKGrid_[i].allocate(meshDimensions);
1556 }
1557 isAllocatedKGrid_ = true;
1558 }
1559
1560 /*
1561 * If necessary, allocate basis field workspace.
1562 */
1563 template <int D, class T>
1564 void FieldIo<D,T>::checkAllocateBasis( std::string const & inFileName)
1565 const
1566 {
1567 if (isAllocatedBasis_) return;
1568
1569 UTIL_CHECK(nMonomer_ > 0);
1570 UTIL_CHECK(hasGroup());
1571 if (!basis().isInitialized()) {
1572 // Peek at field header to initialize basis
1573 std::ifstream file;
1574 fileMaster().openInputFile(inFileName, file);
1575 int nMonomerIn;
1576 bool isSymmetricIn;
1577 UnitCell<D> tmpUnitCell;
1578 readFieldHeader(file, nMonomerIn, tmpUnitCell, isSymmetricIn);
1579 // Note: readFieldHeader can initialize basis if needed
1580 file.close();
1581 }
1582 UTIL_CHECK(basis().isInitialized());
1583 int nBasis = basis().nBasis();
1584 UTIL_CHECK(nBasis > 0);
1585 tmpFieldsBasis_.allocate(nMonomer_);
1586 for (int i = 0; i < nMonomer_; ++i) {
1587 DArray<double>& field = tmpFieldsBasis_[i];
1588 field.allocate(nBasis);
1589 for (int j = 0; j < nBasis; ++j) {
1590 field[j] = 0.0;
1591 }
1592 }
1593 isAllocatedBasis_ = true;
1594 }
1595
1596} // namespace Rp
1597} // namespace Pscf
1598#endif
Comparator for fields in symmetry-adapted basis format.
Symmetry-adapted Fourier basis for pseudo-spectral SCFT.
Definition Basis.h:384
double rmsDiff() const
Return the precomputed root-mean-squared difference.
double compare(FT const &a, FT const &b)
Compare individual fields.
double maxDiff() const
Return the precomputed maximum element-by-element difference.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Description of a regular grid of points in a periodic domain.
Definition Mesh.h:61
FieldIo()
Constructor.
Definition FieldIo.tpp:56
void compareFieldsBasis(DArray< DArray< double > > const &field1, DArray< DArray< double > > const &field2) const
Compare array of fields in basis form, write a report to Log file.
Definition FieldIo.tpp:1041
virtual void writeFieldsRGrid(std::ostream &out, DArray< RField< D, T > > const &fields, UnitCell< D > const &unitCell, bool writeHeader=true, bool isSymmetric=true, bool writeMeshSize=true) const
Write an array of r-grid fields to an output stream.
Definition FieldIo.tpp:426
void associate(Mesh< D > const &mesh, FFT< D, T > const &fft, typename UnitCell< D >::LatticeSystem const &lattice, bool const &hasGroup, std::string const &groupName, SpaceGroup< D > const &group, Basis< D > &basis)
Create associations with other members of the parent Domain.
Definition FieldIo.tpp:78
void readFieldsBasis(std::istream &in, DArray< DArray< double > > &fields, UnitCell< D > &unitCell) const
Read an array of fields in basis format from an input stream.
Definition FieldIo.tpp:122
void scaleFieldsRGrid(DArray< RField< D, T > > &fields, double factor) const
Scale an array of r-grid fields by a scalar.
Definition FieldIo.tpp:1167
Mesh< D > const & mesh() const
Get the associated spatial discretization mesh by const reference.
virtual bool hasSymmetry(RFieldDft< D, T > const &in, double epsilon=1.0e-8, bool verbose=true) const
Check if a k-grid field has the declared space group symmetry.
Definition FieldIo.tpp:974
void writeFieldHeader(std::ostream &out, int nMonomer, UnitCell< D > const &unitCell, bool isSymmetric=true) const
Definition FieldIo.tpp:1504
virtual void writeFieldRGrid(std::ostream &out, RField< D, T > const &field, UnitCell< D > const &unitCell, bool writeHeader=true, bool isSymmetric=true) const
Write a single r-grid field to an an output stream.
Definition FieldIo.tpp:483
void convertRGridToBasis(RField< D, T > const &in, DArray< double > &out, bool checkSymmetry=true, double epsilon=1.0e-8) const
Convert a single field from r-grid to basis form.
Definition FieldIo.tpp:819
void scaleFieldsBasis(DArray< DArray< double > > &fields, double factor) const
Multiply an array of fields in basis format by a real scalar.
Definition FieldIo.tpp:1122
virtual bool readFieldRGrid(std::istream &in, RField< D, T > &field, UnitCell< D > &unitCell) const
Read a single r-grid field from an input stream.
Definition FieldIo.tpp:376
void readFieldHeader(std::istream &in, int &nMonomer, UnitCell< D > &unitCell, bool &isSymmetric) const
Definition FieldIo.tpp:1418
FileMaster const & fileMaster() const
Get the associated FileMaster by const reference.
void setNMonomer(int nMonomer)
Set the number of monomer types.
Definition FieldIo.tpp:107
virtual void scaleFieldBasis(DArray< double > &field, double factor) const
Multiply a single field in basis form by a real scalar.
Definition FieldIo.tpp:1107
virtual void readFieldsRGridData(std::istream &in, DArray< RField< D, T > > &fields, int nMonomer) const
Read data for array of r-grid fields, with no header section.
Definition FieldIo.tpp:352
virtual void convertBasisToKGrid(DArray< double > const &components, RFieldDft< D, T > &dft) const
Convert a single field from basis to Fourier (k-grid) form.
Definition FieldIo.tpp:633
Basis< D > const & basis() const
Get the associated Basis by const reference.
void setFileMaster(FileMaster const &fileMaster)
Create an association with a FileMaster.
Definition FieldIo.tpp:100
virtual void expandRGridDimension(std::ostream &out, DArray< RField< D, T > > const &fields, UnitCell< D > const &unitCell, int d, DArray< int > const &newGridDimensions) const
Increase D for an array of r-grid fields, write to ostream.
Definition FieldIo.tpp:1317
void convertBasisToRGrid(DArray< double > const &in, RField< D, T > &out) const
Convert a single field from basis to r-grid format.
Definition FieldIo.tpp:767
void convertRGridToKGrid(DArray< RField< D, T > > const &in, DArray< RFieldDft< D, T > > &out) const
Convert an array of fields from r-grid to k-grid (Fourier) format.
Definition FieldIo.tpp:905
void estimateWBasis(DMatrix< double > const &chi, DArray< DArray< double > > &fields) const
Convert c fields to estimated w fields, in basis format.
Definition FieldIo.tpp:1203
void convertKGridToRGrid(DArray< RFieldDft< D, T > > const &in, DArray< RField< D, T > > &out) const
Convert an array of field from k-grid to r-grid format.
Definition FieldIo.tpp:878
virtual bool readFieldsRGrid(std::istream &in, DArray< RField< D, T > > &fields, UnitCell< D > &unitCell) const
Read array of r-grid fields from an input stream.
Definition FieldIo.tpp:304
void readFieldBasis(std::istream &in, DArray< double > &field, UnitCell< D > &unitCell) const
Read a single field in basis format from an input stream.
Definition FieldIo.tpp:176
void writeFieldsBasis(std::ostream &out, DArray< DArray< double > > const &fields, UnitCell< D > const &unitCell) const
Write an array of fields in basis format to an output stream.
Definition FieldIo.tpp:224
virtual void convertKGridToBasis(RFieldDft< D, T > const &in, DArray< double > &out, bool checkSymmetry=true, double epsilon=1.0e-8) const
Convert a single field from Fourier (k-grid) to basis form.
Definition FieldIo.tpp:699
virtual void replicateUnitCell(std::ostream &out, DArray< RField< D, T > > const &fields, UnitCell< D > const &unitCell, IntVec< D > const &replicas) const
Write r-grid fields in a replicated unit cell to std::ostream.
Definition FieldIo.tpp:1289
virtual bool readFieldsKGrid(std::istream &in, DArray< RFieldDft< D, T > > &fields, UnitCell< D > &unitCell) const
Read an array of k-grid fields from an input stream.
Definition FieldIo.tpp:534
void writeFieldBasis(std::ostream &out, DArray< double > const &field, UnitCell< D > const &unitCell) const
Write a single field in basis format to an output stream.
Definition FieldIo.tpp:267
virtual void writeFieldsKGrid(std::ostream &out, DArray< RFieldDft< D, T > > const &fields, UnitCell< D > const &unitCell, bool isSymmetric=true) const
Write an array of k-grid fields to a output stream.
Definition FieldIo.tpp:584
virtual void compareFieldsRGrid(DArray< RField< D, T > > const &field1, DArray< RField< D, T > > const &field2) const
Compare two fields in r-grid form, write a report to Log file.
Definition FieldIo.tpp:1073
void scaleFieldRGrid(RField< D, T > &field, double factor) const
Multiply a single field in r-grid format by a scalar.
Definition FieldIo.tpp:1155
Crystallographic space group.
Definition SpaceGroup.h:31
int nParameter() const
Get the number of parameters in the unit cell.
bool isInitialized() const
Has this unit cell been initialized?
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
int capacity() const
Return allocated size.
Definition Array.h:250
bool isAllocated() const
Does this array have associated data?
Definition Array.h:236
Dynamically allocatable contiguous array template.
Definition DArray.h:32
void allocate(int capacity)
Allocate the underlying C array.
Definition DArray.h:262
Dynamically allocated Matrix.
Definition DMatrix.h:25
A FileMaster manages input and output files for a simulation.
Definition FileMaster.h:143
static std::ostream & file()
Get log ostream by reference.
Definition Log.cpp:59
int capacity2() const
Get number of columns (range of the second array index).
Definition Matrix.h:139
int capacity1() const
Get number of rows (range of the first array index).
Definition Matrix.h:132
#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
#define UTIL_ASSERT(condition)
Assertion macro suitable for debugging serial or parallel code.
Definition global.h:75
void replicateUnitCell(IntVec< 1 > const &replicas, UnitCell< 1 > const &cellIn, UnitCell< 1 > &cellOut)
Create a replicated UnitCell<1>.
void readFieldHeader(std::istream &in, int &ver1, int &ver2, UnitCell< D > &cell, std::string &groupName, int &nMonomer)
Read common part of field header (fortran PSCF format).
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 inspectArrays(DArray< AT > const &arrays, int &nMonomer, int &capacity)
Inspect dimensions of a DArray of 1D arrays, each of type AT.
void checkAllocateField(FT &field, IntVec< D > const &dimensions)
Check allocation of a single field, allocate if necessary.
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 readMeshDimensions(std::istream &in, IntVec< D > const &meshDimensions)
Read mesh dimensions from a field file header.
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.
Definition rFieldIo.tpp:38
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.
Definition rFieldIo.tpp:232
void checkAllocateFields(DArray< FT > &fields, int nMonomer, IntVec< D > const &dimensions)
Check allocation of an array of fields, allocate if necessary.
void copyArrays(DArray< OAT > &out, DArray< IAT > const &in)
Copy a DArray of 1D arrays.
void associateArrays(DArray< HAT > &host, DArray< DAT > &device)
Associate host arrays with backend arrays.
void writeNBasis(std::ostream &out, int nBasis)
Write the number of basis functions to a basis field file header.
Definition rFieldIo.cpp:43
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.
Definition rFieldIo.tpp:76
void dissociateArrays(DArray< HAT > &host)
Dissociate an array of host arrays.
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.
Definition rFieldIo.tpp:984
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.
Definition rFieldIo.tpp:510
void inspectFields(DArray< FT > const &fields, int &nMonomer, IntVec< D > &dimensions)
Inspect dimensions of a DArray of fields, each of type FT.
int readNBasis(std::istream &in)
Read the number of basis functions from a basis field file header.
Definition rFieldIo.cpp:15
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.
void mulEqS(Array< double > &a, double b)
Vector-scalar in-place multiplication, a[i] *= b (real).
Definition VecOp.cpp:270
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.