PSCF v1.1
pspg/field/Domain.tpp
1#ifndef PSPG_DOMAIN_TPP
2#define PSPG_DOMAIN_TPP
3
4/*
5* PSCF - Polymer Self-Consistent Field Theory
6*
7* Copyright 2016 - 2022, 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
13namespace Pscf {
14namespace Pspg
15{
16
17 using namespace Util;
18
19 /*
20 * Constructor.
21 */
22 template <int D>
24 : unitCell_(),
25 mesh_(),
26 basis_(),
27 fft_(),
28 fieldIo_(),
29 lattice_(UnitCell<D>::Null),
30 groupName_(),
31 hasFileMaster_(false),
32 isInitialized_(false)
33 { setClassName("Domain"); }
34
35 /*
36 * Destructor.
37 */
38 template <int D>
40 {}
41
42 template <int D>
44 {
45 fieldIo_.associate(mesh_, fft_,
46 lattice_, groupName_, group_, basis_,
47 fileMaster);
48 hasFileMaster_ = true;
49 }
50
51 /*
52 * Read parameters and initialize.
53 */
54 template <int D>
55 void Domain<D>::readParameters(std::istream& in)
56 {
57 UTIL_CHECK(hasFileMaster_);
58
59 bool hasUnitCell = false;
60 // Uncomment for backwards compatibility for old format (<v1.0)
61 #if 0
62 // Optionally read unit cell
63 readOptional(in, "unitCell", unitCell_);
64 if (unitCell_.lattice() != UnitCell<D>::Null) {
65 lattice_ = unitCell_.lattice();
66 hasUnitCell = true;
67 }
68 #endif
69
70 read(in, "mesh", mesh_);
71 UTIL_CHECK(mesh().size() > 0);
72 fft_.setup(mesh_.dimensions());
73
74 // If no unit cell was read, read lattice system
75 if (!hasUnitCell) {
76 read(in, "lattice", lattice_);
77 unitCell_.set(lattice_);
78 }
79 UTIL_CHECK(unitCell().lattice() != UnitCell<D>::Null);
80 UTIL_CHECK(unitCell().nParameter() > 0);
81
82 // Allocate memory for WaveList
83 waveList().allocate(mesh(), unitCell());
84
85 // Read group name and initialize space group
86 read(in, "groupName", groupName_);
87 readGroup(groupName_, group_);
88
89 // Make symmetry-adapted basis
90 if (unitCell().isInitialized()) {
91 basis().makeBasis(mesh(), unitCell(), group_);
92 }
93
94 isInitialized_ = true;
95 }
96
97
98 template <int D>
99 void Domain<D>::readRGridFieldHeader(std::istream& in, int& nMonomer)
100 {
101 // Read common section of standard field header
102 int ver1, ver2;
103 Pscf::readFieldHeader(in, ver1, ver2,
104 unitCell_, groupName_, nMonomer);
105
106 // Read grid dimensions
107 std::string label;
108 in >> label;
109 if (label != "mesh" && label != "ngrid") {
110 std::string msg = "\n";
111 msg += "Error reading field file:\n";
112 msg += "Expected mesh or ngrid, but found [";
113 msg += label;
114 msg += "]";
115 UTIL_THROW(msg.c_str());
116 }
117 IntVec<D> nGrid;
118 in >> nGrid;
119
120 // Initialize mesh, fft and basis
121 mesh_.setDimensions(nGrid);
122 fft_.setup(mesh_.dimensions());
123
124 // Initialize group and basis
125 readGroup(groupName_, group_);
126 basis_.makeBasis(mesh_, unitCell_, group_);
127
128 isInitialized_ = true;
129 }
130
131 /*
132 * Set the unit cell, make basis if needed.
133 */
134 template <int D>
135 void Domain<D>::setUnitCell(UnitCell<D> const & unitCell)
136 {
137 if (lattice_ == UnitCell<D>::Null) {
138 lattice_ = unitCell.lattice();
139 } else {
140 UTIL_CHECK(lattice_ == unitCell.lattice());
141 }
142 unitCell_ = unitCell;
143 if (!basis_.isInitialized()) {
144 makeBasis();
145 }
146 waveList_.computeKSq(unitCell_);
147 waveList_.computedKSq(unitCell_);
148 }
149
150 /*
151 * Set the unit cell, make basis if needed.
152 */
153 template <int D>
155 FSArray<double, 6> const & parameters)
156 {
157 if (lattice_ == UnitCell<D>::Null) {
158 lattice_ = lattice;
159 } else {
160 UTIL_CHECK(lattice_ == lattice);
161 }
162 unitCell_.set(lattice, parameters);
163 if (!basis_.isInitialized()) {
164 makeBasis();
165 }
166 waveList_.computeKSq(unitCell_);
167 waveList_.computedKSq(unitCell_);
168 }
169
170 /*
171 * Set parameters of the associated unit cell, make basis if needed.
172 */
173 template <int D>
175 {
176 UTIL_CHECK(unitCell_.lattice() != UnitCell<D>::Null);
177 UTIL_CHECK(unitCell_.nParameter() == parameters.size());
178 unitCell_.setParameters(parameters);
179 if (!basis_.isInitialized()) {
180 makeBasis();
181 }
182 waveList_.computeKSq(unitCell_);
183 waveList_.computedKSq(unitCell_);
184 }
185
186 template <int D>
188 {
189 UTIL_CHECK(mesh_.size() > 0);
190 UTIL_CHECK(unitCell_.lattice() != UnitCell<D>::Null);
191 UTIL_CHECK(unitCell_.nParameter() > 0);
192 UTIL_CHECK(unitCell_.isInitialized());
193
194 #if 0
195 // Check group, read from file if necessary
196 if (group_.size() == 1) {
197 UTIL_CHECK(groupName_ != "");
198 readGroup(groupName_, group_);
199 }
200 #endif
201
202 // Check basis, construct if not initialized
203 if (!basis().isInitialized()) {
204 basis_.makeBasis(mesh_, unitCell_, group_);
205 }
206 UTIL_CHECK(basis().isInitialized());
207
208 // Compute minimum images in WaveList
209 UTIL_CHECK(waveList().isAllocated());
210 if (!waveList().hasMinimumImages()) {
211 waveList().computeMinimumImages(mesh(), unitCell());
212 }
213
214 }
215
216} // namespace Pspg
217} // namespace Pscf
218#endif
An IntVec<D, T> is a D-component vector of elements of integer type T.
Definition: IntVec.h:27
void setUnitCell(UnitCell< D > const &unitCell)
Set the unit cell, given a UnitCell<D> object.
void readRGridFieldHeader(std::istream &in, int &nMonomer)
Read initialization data from header of an r-grid field file.
void setFileMaster(FileMaster &fileMaster)
Create association with a FileMaster, needed by FieldIo.
void makeBasis()
Construct basis if not done already.
virtual void readParameters(std::istream &in)
Read body of parameter block (without opening and closing lines).
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition: UnitCell.h:44
A fixed capacity (static) contiguous array with a variable logical size.
Definition: FSArray.h:38
int size() const
Return logical size of this array (i.e., number of elements).
Definition: FSArray.h:207
A FileMaster manages input and output files for a simulation.
Definition: FileMaster.h:143
void setClassName(const char *className)
Set class name string.
#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:51
C++ namespace for polymer self-consistent field theory (PSCF).
Utility classes for scientific computation.
Definition: accumulators.mod:1