PSCF v1.4.0
cp/field/Domain.tpp
1#ifndef PRDC_CL_DOMAIN_TPP
2#define PRDC_CL_DOMAIN_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field
6*
7* Copyright 2015 - 2025, The Regents of the University of Minnesota
8* Distributed under the terms of the GNU General Public License.
9*/
10
11#include "Domain.h"
12#include <prdc/field/fieldHeader.h>
13#include <util/signal/Signal.h>
14#include <util/misc/FileMaster.h>
15
16namespace Pscf {
17namespace Cp {
18
19 using namespace Util;
20 using namespace Prdc;
21
22 /*
23 * Constructor.
24 */
25 template <int D, class FFT, class WLT, class FIT>
27 : mesh_(),
28 unitCell_(),
29 lattice_(UnitCell<D>::Null),
30 fftPtr_(nullptr),
31 waveListPtr_(nullptr),
32 fieldIoPtr_(nullptr),
33 signalPtr_(nullptr),
34 fileMasterPtr_(nullptr),
35 isInitialized_(false)
36 {
37 setClassName("Domain");
38
39 // Construct associated objects
40 fftPtr_ = new FFT();
41 bool isRealField = false;
42 waveListPtr_ = new WLT(isRealField);
43 fieldIoPtr_ = new FIT();
44 signalPtr_ = new Signal<void>();
45
46 // Note: Passing the WLT (i.e. WaveList<D>) constructor
47 // an argument isRealField = false configures it to use
48 // a full k-space grid appropriate for a complex field,
49 // rather than the half-space grid used for the DFT of
50 // a real-valued field.
51
52 // Create associations between objects
53 fieldIo().associate(mesh_, fft(), lattice_);
54 unitCell_.setSignal(*signalPtr_);
55 }
56
57 /*
58 * Destructor.
59 */
60 template <int D, class FFT, class WLT, class FIT>
62 {
63 delete fftPtr_;
64 delete waveListPtr_;
65 delete fieldIoPtr_;
66 delete signalPtr_;
67 }
68
69 /*
70 * Create association with a FileMaster.
71 */
72 template <int D, class FFT, class WLT, class FIT>
74 {
75 fileMasterPtr_ = &fileMaster;
76 fieldIo().setFileMaster(fileMaster);
77 }
78
79 /*
80 * Read parameters and initialize.
81 */
82 template <int D, class FFT, class WLT, class FIT>
84 {
85 // Preconditions
86 UTIL_CHECK(!isInitialized_);
87 UTIL_CHECK(fileMasterPtr_);
88
89 // Read computational mesh dimensions (required)
90 read(in, "mesh", mesh_);
91 UTIL_CHECK(mesh_.size() > 0);
92 fft().setup(mesh_.dimensions());
93
94 // Read lattice system enumeration value (required)
95 read(in, "lattice", lattice_);
96 unitCell_.set(lattice_);
97 UTIL_CHECK(unitCell_.lattice() != UnitCell<D>::Null);
98 UTIL_CHECK(unitCell_.nParameter() > 0);
99
100 // Allocate memory for WaveList
101 waveList().allocate(mesh_, unitCell_);
102
103 // Optionally read groupName, and discard information if found
104 std::string groupName;
105 readOptional(in, "groupName", groupName);
106
107 isInitialized_ = true;
108 }
109
110 /*
111 * Read header of r-grid field to initialize the Domain.
112 *
113 * Alternative to parameter file, used only for unit testing.
114 */
115 template <int D, class FFT, class WLT, class FIT>
116 void
118 int& nMonomer)
119 {
120 // Preconditions - confirm that nothing is initialized
121 UTIL_CHECK(!isInitialized_);
122 UTIL_CHECK(lattice_ == UnitCell<D>::Null);
123 UTIL_CHECK(!unitCell_.isInitialized());
124
125 // Read common section of standard field header
126 int ver1, ver2;
127 std::string groupName;
128 Pscf::Prdc::readFieldHeader(in, ver1, ver2,
129 unitCell_, groupName, nMonomer);
130
131 // Set lattice_ (lattice system identifier)
132 lattice_ = unitCell_.lattice();
133 UTIL_CHECK(lattice_ != UnitCell<D>::Null);
134 UTIL_CHECK(unitCell_.isInitialized());
135
136 // Read mesh dimensions
137 std::string label;
138 in >> label;
139 if (label != "mesh" && label != "ngrid") {
140 std::string msg = "\n";
141 msg += "Error reading field file:\n";
142 msg += "Expected mesh or ngrid, but found [";
143 msg += label;
144 msg += "]";
145 UTIL_THROW(msg.c_str());
146 }
147 IntVec<D> nGrid;
148 in >> nGrid;
149
150 // Initialize mesh and fft
151 if (mesh_.size() == 0) {
152 mesh_.setDimensions(nGrid);
153 fft().setup(mesh_.dimensions());
154 }
155
156 // Allocate waveList
157 if (!waveList().isAllocated()) {
158 waveList().allocate(mesh_, unitCell_);
159 }
160
161 isInitialized_ = true;
162 }
163
164} // namespace Cp
165} // namespace Pscf
166#endif
virtual void readParameters(std::istream &in)
Read body of parameter block (without opening and closing lines).
FFT & fft()
Get the FFT by non-const reference.
FIT & fieldIo()
Get the FieldIo by non-const reference.
void setFileMaster(FileMaster &fileMaster)
Create association with a FileMaster, needed by FieldIo.
void readFieldHeader(std::istream &in, int &nMonomer)
Read initialization data from header of an r-grid field file.
WLT & waveList()
Get the WaveList by non-const reference.
Domain()
Constructor.
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
Fourier transform wrapper.
Definition cpu/FFT.h:39
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:56
A FileMaster manages input and output files for a simulation.
Definition FileMaster.h:143
void setClassName(const char *className)
Set class name string.
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
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).
Complex-valued periodic fields (class templates).
Definition cp.mod:6
Periodic fields and crystallography.
Definition complex.cpp:11
PSCF package top-level namespace.