PSCF v1.5.0
UnitCell2.cpp
1/*
2* PSCF - Polymer Self-Consistent Field
3*
4* Copyright 2015 - 2026, David Morse (morse012@um.edu) and co-authors
5* Distributed under the terms of the GNU General Public License.
6*/
7
8#include "UnitCell.h"
9#include <util/math/Constants.h>
10
11namespace Pscf {
12
13 using namespace Util;
14
15 // Member functions of UnitCell<2>
16
17 /*
18 * Constructor.
19 */
21 : lattice_(Null)
22 {}
23
24 /*
25 * Read the lattice system and set nParameter.
26 */
28 {
29 UTIL_CHECK(lattice_ != UnitCell<2>::Null);
30 if (lattice_ == UnitCell<2>::Square) {
31 nParameter_ = 1;
32 } else
33 if (lattice_ == UnitCell<2>::Hexagonal) {
34 nParameter_ = 1;
35 } else
36 if (lattice_ == UnitCell<2>::Rectangular) {
37 nParameter_ = 2;
38 } else
39 if (lattice_ == UnitCell<2>::Rhombic) {
40 nParameter_ = 2;
41 } else
42 if (lattice_ == UnitCell<2>::Oblique) {
43 nParameter_ = 3;
44 } else {
45 UTIL_THROW("Invalid lattice system value");
46 }
47 }
48
49 /*
50 * Set the Bravais and reciprocal lattice vectors.
51 */
52 void UnitCell<2>::setBasis()
53 {
54 UTIL_CHECK(lattice_ != UnitCell<2>::Null);
55 UTIL_CHECK(nParameter_ > 0);
56
57 double twoPi = 2.0*Constants::Pi;
58 int i;
59 if (lattice_ == UnitCell<2>::Square) {
60 UTIL_CHECK(nParameter_ == 1);
61 for (i=0; i < 2; ++i) {
62 rBasis_[i][i] = parameters_[0];
63 kBasis_[i][i] = twoPi/parameters_[0];
64 drBasis_[0](i,i) = 1.0;
65 }
66 } else
67 if (lattice_ == UnitCell<2>::Rectangular) {
68 UTIL_CHECK(nParameter_ == 2);
69 for (i=0; i < 2; ++i) {
70 rBasis_[i][i] = parameters_[i];
71 kBasis_[i][i] = twoPi/parameters_[i];
72 drBasis_[i](i,i) = 1.0;
73 }
74 } else
75 if (lattice_ == UnitCell<2>::Hexagonal) {
76 UTIL_CHECK(nParameter_ == 1);
77 double a = parameters_[0];
78 double rt3 = sqrt(3.0);
79
80 rBasis_[0][0] = a;
81 rBasis_[0][1] = 0.0;
82 rBasis_[1][0] = -0.5*a;
83 rBasis_[1][1] = 0.5*rt3*a;
84
85 drBasis_[0](0, 0) = 1.0;
86 drBasis_[0](0, 1) = 0.0;
87 drBasis_[0](1, 0) = -0.5;
88 drBasis_[0](1, 1) = 0.5*rt3;
89
90 kBasis_[0][0] = twoPi/a;
91 kBasis_[0][1] = twoPi/(rt3*a);
92 kBasis_[1][0] = 0.0;
93 kBasis_[1][1] = twoPi/(0.5*rt3*a);
94 } else
95 if (lattice_ == UnitCell<2>::Rhombic) {
96 UTIL_CHECK(nParameter_ == 2);
97 double a = parameters_[0];
98 double gamma = parameters_[1];
99 // gamma is the angle between the two Bravais basis vectors
100
101 double cg = cos(gamma);
102 double sg = sin(gamma);
103
104
105 rBasis_[0][0] = a;
106 rBasis_[0][1] = 0.0;
107 rBasis_[1][0] = cg*a;
108 rBasis_[1][1] = sg*a;
109
110 drBasis_[0](0, 0) = 1.0;
111 drBasis_[0](0, 1) = 0.0;
112 drBasis_[0](1, 0) = cg;
113 drBasis_[0](1, 1) = sg;
114 drBasis_[1](1, 0) = -sg*a;
115 drBasis_[1](1, 1) = cg*a;
116
117 kBasis_[0][0] = twoPi/a;
118 kBasis_[0][1] = -twoPi*cg/(sg*a);
119 kBasis_[1][0] = 0.0;
120 kBasis_[1][1] = twoPi/(a*sg);
121
122 } else
123 if (lattice_ == UnitCell<2>::Oblique) {
124 UTIL_CHECK(nParameter_ == 3);
125 double a = parameters_[0];
126 double b = parameters_[1];
127 double gamma = parameters_[2];
128 // gamma is the angle between the two Bravais basis vectors
129
130 double cg = cos(gamma);
131 double sg = sin(gamma);
132
133 rBasis_[0][0] = a;
134 rBasis_[0][1] = 0.0;
135 rBasis_[1][0] = cg*b;
136 rBasis_[1][1] = sg*b;
137
138 // parameters_[0] => a
139 drBasis_[0](0, 0) = 1.0;
140 drBasis_[0](0, 1) = 0.0;
141 // parameters_[1] => b
142 drBasis_[1](1, 0) = cg;
143 drBasis_[1](1, 1) = sg;
144 // parameters_[2] => gamma
145 drBasis_[2](1, 0) = -sg*b;
146 drBasis_[2](1, 1) = cg*b;
147
148 kBasis_[0][0] = twoPi/a;
149 kBasis_[0][1] = -twoPi*cg/(sg*a);
150 kBasis_[1][0] = 0.0;
151 kBasis_[1][1] = twoPi/(b*sg);
152
153 } else {
154 UTIL_THROW("Unimplemented 2D lattice type");
155 }
156 }
157
158 /*
159 * Assignment operator.
160 */
162 {
163 if (lattice_ != UnitCell<2>::Null) {
164 UTIL_CHECK(other.lattice_ == lattice_);
165 }
166 isInitialized_ = false;
167 lattice_ = other.lattice_;
168 setNParameter();
170 for (int i = 0; i < nParameter_; ++i) {
171 parameters_[i] = other.parameters_[i];
172 }
173 setLattice();
174 return *this;
175 }
176
177 /*
178 * Set state of the unit cell (lattice system and parameters)
179 */
181 {
183 if (lattice_ != UnitCell<2>::Null) {
184 UTIL_CHECK(lattice == lattice_);
185 }
186 isInitialized_ = false;
187 lattice_ = lattice;
188 setNParameter();
189 }
190
191 /*
192 * Set state of the unit cell (lattice system and parameters)
193 */
196 {
197 set(lattice);
199 for (int i = 0; i < nParameter_; ++i) {
200 parameters_[i] = parameters[i];
201 }
202 setLattice();
203 }
204
205 /*
206 * Get the generalized 2D volume (area) of the unit cell.
207 */
208 double UnitCell<2>::volume() const
209 {
210 double a = 0.0;
211 a += rBasis_[0][0]*rBasis_[1][1];
212 a -= rBasis_[0][1]*rBasis_[1][0];
213 return a;
214 }
215
216 // UnitCell<2>::LatticeSystem stream IO operators
217
218 /*
219 * Extract a UnitCell<2>::LatticeSystem from an istream as a string.
220 */
221 std::istream& operator >> (std::istream& in,
223 {
224
225 std::string buffer;
226 in >> buffer;
227 if (buffer == "Square" || buffer == "square") {
228 lattice = UnitCell<2>::Square;
229 } else
230 if (buffer == "Rectangular" || buffer == "rectangular") {
231 lattice = UnitCell<2>::Rectangular;
232 } else
233 if (buffer == "Rhombic" || buffer == "rhombic") {
234 lattice = UnitCell<2>::Rhombic;
235 } else
236 if (buffer == "Hexagonal" || buffer == "hexagonal") {
237 lattice = UnitCell<2>::Hexagonal;
238 } else
239 if (buffer == "Oblique" || buffer == "oblique") {
240 lattice = UnitCell<2>::Oblique;
241 } else {
242 UTIL_THROW("Invalid UnitCell<2>::LatticeSystem value input");
243 }
244 return in;
245 }
246
247 /*
248 * Insert a UnitCell<2>::LatticeSystem to an ostream as a string.
249 */
250 std::ostream& operator << (std::ostream& out,
252 {
253 if (lattice == UnitCell<2>::Square) {
254 out << "square";
255 } else
256 if (lattice == UnitCell<2>::Rectangular) {
257 out << "rectangular";
258 } else
259 if (lattice == UnitCell<2>::Rhombic) {
260 out << "rhombic";
261 } else
262 if (lattice == UnitCell<2>::Hexagonal) {
263 out << "hexagonal";
264 } else
265 if (lattice == UnitCell<2>::Oblique) {
266 out << "oblique";
267 } else
268 if (lattice == UnitCell<2>::Null) {
269 out << "Null";
270 } else {
271 UTIL_THROW("This should never happen");
272 }
273 return out;
274 }
275
276}
int nParameter_
Number of parameters required to specify unit cell.
FArray< double, 6 > parameters_
Parameters used to describe the unit cell.
void setLattice()
Compute all protected data, given latticeSystem and parameters.
bool isInitialized_
Has this unit cell been fully initialized?
FArray< RealVec< D >, D > rBasis_
Array of Bravais lattice basis vectors.
FSArray< double, 6 > parameters() const
Get the parameters of this unit cell.
UnitCell()
Constructor.
Definition UnitCell2.cpp:20
LatticeSystem lattice() const
Return lattice system enumeration value.
Definition UnitCell.h:305
void set(UnitCell< 2 >::LatticeSystem lattice)
Set the lattice system, but not unit cell parameters.
Base template for UnitCell<D> classes, D=1, 2 or 3.
Definition UnitCell.h:55
static const double Pi
Trigonometric constant Pi.
Definition Constants.h:35
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:210
#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
PSCF package top-level namespace.
std::istream & operator>>(std::istream &in, Pair< Data > &pair)
Input a Pair from an istream.
Definition Pair.h:44
std::ostream & operator<<(std::ostream &out, const Pair< Data > &pair)
Output a Pair to an ostream, without line breaks.
Definition Pair.h:57