PSCF v1.5.0
Domain.tpp
1#ifndef RP_DOMAIN_TPP
2#define RP_DOMAIN_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 "Domain.h"
12#include <rp/field/FieldIo.h>
13#include <pscf/field/FFT.h>
14#include <pscf/field/WaveList.h>
15#include <pscf/crystal/SpaceGroup.h>
16#include <pscf/crystal/Basis.h>
17#include <pscf/fieldIo/fieldHeader.h>
18#include <util/signal/Signal.h>
19#include <util/misc/FileMaster.h>
20
21namespace Pscf {
22namespace Rp {
23
24 using namespace Util;
25
26 /*
27 * Constructor.
28 */
29 template <int D, class T>
31 : mesh_(),
32 unitCell_(),
33 lattice_(UnitCell<D>::Null),
34 groupName_(""),
35 groupPtr_(nullptr),
36 basisPtr_(nullptr),
37 fftPtr_(nullptr),
38 waveListPtr_(nullptr),
39 fieldIoPtr_(nullptr),
40 signalPtr_(nullptr),
41 fileMasterPtr_(nullptr),
42 hasGroup_(false),
43 isInitialized_(false)
44 {
46
47 // Construct associated objects
48 groupPtr_ = new SpaceGroup<D>();
49 basisPtr_ = new Basis<D>();
50 fftPtr_ = new FFT<D,T>();
51 waveListPtr_ = new WaveList<D,T>();
52 fieldIoPtr_ = new FieldIo<D,T>();
53 signalPtr_ = new Signal<void>();
54
55 // Create associations between objects
56 fieldIo().associate(mesh_, fft(), lattice_,
57 hasGroup_, groupName_, group(), basis());
58 unitCell_.setSignal(*signalPtr_);
59 }
60
61 /*
62 * Destructor.
63 */
64 template <int D, class T>
66 {
67 delete groupPtr_;
68 delete basisPtr_;
69 delete fftPtr_;
70 delete waveListPtr_;
71 delete fieldIoPtr_;
72 delete signalPtr_;
73 }
75 /*
76 * Create association with a FileMaster.
77 */
78 template <int D, class T>
80 {
81 fileMasterPtr_ = &fileMaster;
82 fieldIo().setFileMaster(fileMaster);
83 }
84
85 /*
86 * Read parameters and initialize.
87 */
88 template <int D, class T>
89 void Domain<D,T>::readParameters(std::istream& in)
90 {
91 // Preconditions
92 UTIL_CHECK(!isInitialized_);
93 UTIL_CHECK(fileMasterPtr_);
94
95 // Read computational mesh dimensions (required)
96 ParamComposite::read(in, "mesh", mesh_);
97 UTIL_CHECK(mesh_.size() > 0);
98 fft().setup(mesh_.dimensions());
99
100 // Read lattice system enumeration value (required)
101 ParamComposite::read(in, "lattice", lattice_);
102 unitCell_.set(lattice_);
103 UTIL_CHECK(unitCell_.lattice() != UnitCell<D>::Null);
104 UTIL_CHECK(unitCell_.nParameter() > 0);
105
106 // Allocate memory for WaveList
107 waveList().allocate(mesh_, unitCell_);
108
109 // Optionally read groupName_ (string identifier for space group)
110 hasGroup_ = false;
111 bool hasGroupName = false;
113 in, "groupName", groupName_).isActive();
114
115 // If groupName_ exists, read and construct group (space group)
116 if (hasGroupName) {
117 // Read group symmetry operations from file
118 // An Exception is thrown if groupName_ string is not recognized
119 readGroup(groupName_, *groupPtr_);
120 hasGroup_ = true;
121 }
122
123 isInitialized_ = true;
124 }
125
126 /*
127 * Read header of r-grid field to initialize the Domain.
128 *
129 * Alternative to parameter file, used only for unit testing.
130 */
131 template <int D, class T>
132 void Domain<D,T>::readRGridFieldHeader(std::istream& in, int& nMonomer)
133 {
134 // Preconditions - confirm that nothing is initialized
135 UTIL_CHECK(!isInitialized_);
136 UTIL_CHECK(lattice_ == UnitCell<D>::Null);
137 UTIL_CHECK(!unitCell_.isInitialized());
138 UTIL_CHECK(!hasGroup_);
139 UTIL_CHECK(groupName_ == "");
140
141 // Read common section of standard field header
142 int ver1, ver2;
143 Pscf::readFieldHeader(in, ver1, ver2,
144 unitCell_, groupName_, nMonomer);
145
146 // Set lattice_ (lattice system identifier)
147 lattice_ = unitCell_.lattice();
148 UTIL_CHECK(lattice_ != UnitCell<D>::Null);
149 UTIL_CHECK(unitCell_.isInitialized());
150
151 // Read grid dimensions
152 std::string label;
153 in >> label;
154 if (label != "mesh" && label != "ngrid") {
155 std::string msg = "\n";
156 msg += "Error reading field file:\n";
157 msg += "Expected mesh or ngrid, but found [";
158 msg += label;
159 msg += "]";
160 UTIL_THROW(msg.c_str());
161 }
162 IntVec<D> nGrid;
163 in >> nGrid;
164
165 // Initialize mesh and fft
166 if (mesh_.size() == 0) {
167 mesh_.setDimensions(nGrid);
168 fft().setup(mesh_.dimensions());
169 }
170
171 // Allocate waveList
172 if (!waveList().isAllocated()) {
173 waveList().allocate(mesh_, unitCell_);
174 }
175
176 // If groupName is present, construct group and basis
177 if (groupName_ != "") {
178 readGroup(groupName_, *groupPtr_);
179 hasGroup_ = true;
180 basis().makeBasis(mesh_, unitCell_, group());
181 }
182
183 isInitialized_ = true;
184 }
185
186 /*
187 * Make basis if needed.
188 */
189 template <int D, class T>
191 {
192 UTIL_CHECK(mesh_.size() > 0);
193 UTIL_CHECK(unitCell_.lattice() != UnitCell<D>::Null);
194 UTIL_CHECK(unitCell_.isInitialized());
195 UTIL_CHECK(hasGroup_);
196
197 // Check basis, construct if not initialized
198 if (!basis().isInitialized()) {
199 basis().makeBasis(mesh_, unitCell_, group());
200 }
201 UTIL_CHECK(basis().isInitialized());
202 }
203
204 // Crystallographic Data Output
206 /*
207 * Write description of symmetry-adapted stars and basis to file.
208 */
209 template <int D, class T>
210 void Domain<D,T>::writeStars(std::string const & filename) const
211 {
213 UTIL_CHECK(basis().isInitialized());
214 std::ofstream file;
215 fileMaster().openOutputFile(filename, file);
216 bool isSymmetric = true;
217 int nMonomer = 0;
218 // Header gives the unit cell from which the basis was constructed
219 UnitCell<D> const & cell = basis().unitCell();
220 fieldIo().writeFieldHeader(file, nMonomer, cell, isSymmetric);
221 basis().outputStars(file);
222 file.close();
223 }
224
225 /*
226 * Write a list of waves and associated stars to file.
227 */
228 template <int D, class T>
229 void Domain<D,T>::writeWaves(std::string const & filename) const
230 {
232 UTIL_CHECK(basis().isInitialized());
233 std::ofstream file;
234 fileMaster().openOutputFile(filename, file);
235 bool isSymmetric = true;
236 int nMonomer = 0;
237 // Header gives the unit cell from which the basis was constructed
238 UnitCell<D> const & cell = basis().unitCell();
239 fieldIo().writeFieldHeader(file, nMonomer, cell, isSymmetric);
240 basis().outputWaves(file);
241 file.close();
242 }
243
244 /*
245 * Write all elements of the space group to a file.
246 */
247 template <int D, class T>
248 void Domain<D,T>::writeGroup(std::string const & filename) const
249 {
251 std::ofstream file;
252 fileMaster().openOutputFile(filename, file);
253 file << group();
254 file.close();
255 }
256
257 /*
258 * Has a symmetry-adapted Fourier basis been initialized ?
259 */
260 template <int D, class T>
262 { return basis().isInitialized(); }
263
264} // namespace Rp
265} // namespace Pscf
266#endif
Symmetry-adapted Fourier basis for pseudo-spectral SCFT.
Definition Basis.h:384
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition IntVec.h:27
void makeBasis()
Construct basis if not done already.
Definition Domain.tpp:190
void setFileMaster(FileMaster &fileMaster)
Create association with a FileMaster, needed by FieldIo.
Definition Domain.tpp:79
FieldIo< D, T > & fieldIo()
Get the FieldIo by non-const reference.
WaveList< D, CPT > & waveList()
SpaceGroup< D > const & group() const
Get the SpaceGroup by const reference.
void readRGridFieldHeader(std::istream &in, int &nMonomer)
Read initialization data from header of an r-grid field file.
Definition Domain.tpp:132
void writeWaves(std::string const &filename) const
Output information about waves.
Definition Domain.tpp:229
Basis< D > & basis()
Get the Basis object by non-const reference.
void writeGroup(std::string const &filename) const
Output all elements of the space group.
Definition Domain.tpp:248
bool hasGroup() const
Has a space group been declared?
~Domain()
Destructor.
Definition Domain.tpp:65
bool hasBasis() const
Has a symmetry-adapted Fourier basis been initialized?
Definition Domain.tpp:261
FFT< D, T > & fft()
Get the FFT by non-const reference.
virtual void readParameters(std::istream &in)
Read body of parameter block (without opening and closing lines).
Definition Domain.tpp:89
Domain()
Constructor.
Definition Domain.tpp:30
void writeStars(std::string const &filename) const
Output information about stars and symmetrized basis functions.
Definition Domain.tpp:210
FieldIo()
Constructor.
Definition FieldIo.tpp:56
Crystallographic space group.
Definition SpaceGroup.h:31
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
A FileMaster manages input and output files for a simulation.
Definition FileMaster.h:143
ScalarParam< Type > & read(std::istream &in, const char *label, Type &value)
Add and read a new required ScalarParam < Type > object.
void setClassName(const char *className)
Set class name string.
ScalarParam< Type > & readOptional(std::istream &in, const char *label, Type &value)
Add and read a new optional ScalarParam < Type > object.
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).
Code for programs that simulate real-valued periodic fields.
Definition param_pc.dox:2
PSCF package top-level namespace.