-
Notifications
You must be signed in to change notification settings - Fork 1
/
Box.cpp
269 lines (244 loc) · 8.12 KB
/
Box.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
#include <cmath> // cos, sin, sqrt
#include "Box.h"
#include "Constants.h" // DEGRAD
#include "CpptrajStdio.h"
// CONSTRUCTOR
Box::Box() : btype_(NOBOX) //, debug_(0)
{
box_[0] = 0;
box_[1] = 0;
box_[2] = 0;
box_[3] = 0;
box_[4] = 0;
box_[5] = 0;
}
Box::Box(const double* bIn) //: debug_(0)
{
SetBox( bIn );
}
Box::Box(Matrix_3x3 const& ucell) { SetBox( ucell ); }
// COPY CONSTRUCTOR
Box::Box(const Box& rhs) : btype_(rhs.btype_) //, debug_(rhs.debug_)
{
box_[0] = rhs.box_[0];
box_[1] = rhs.box_[1];
box_[2] = rhs.box_[2];
box_[3] = rhs.box_[3];
box_[4] = rhs.box_[4];
box_[5] = rhs.box_[5];
}
// Assignment
Box &Box::operator=(const Box& rhs) {
if (&rhs == this) return *this;
//debug_ = rhs.debug_;
btype_ = rhs.btype_;
box_[0] = rhs.box_[0];
box_[1] = rhs.box_[1];
box_[2] = rhs.box_[2];
box_[3] = rhs.box_[3];
box_[4] = rhs.box_[4];
box_[5] = rhs.box_[5];
return *this;
}
const double Box::TRUNCOCTBETA_ = 2.0*acos(1.0/sqrt(3.0))*Constants::RADDEG;
/** This value is chosen so that TRUNCOCTBETA_ - TruncOctDelta_ is just
* less than 109.47, about 109.4699, since 109.47 is probably the lowest
* precision angle we can accept. +0.000000703 to avoid FP round-down.
*/
const double Box::TruncOctDelta_ = 0.001220703;
const double Box::TruncOctMin_ = Box::TRUNCOCTBETA_ - Box::TruncOctDelta_;
const double Box::TruncOctMax_ = Box::TRUNCOCTBETA_ + Box::TruncOctDelta_;
/** Used to detect low precision trunc. oct. angles (e.g. 109.47). */
const double Box::TruncOctEps_ = 0.001;
const char* Box::BoxNames_[] = {
"None", "Orthogonal", "Trunc. Oct.", "Rhombic Dodec.", "Non-orthogonal"
};
// Box::SetBetaLengths()
void Box::SetBetaLengths(double beta, double xin, double yin, double zin) {
box_[0] = xin;
box_[1] = yin;
box_[2] = zin;
box_[3] = 0;
box_[4] = beta;
box_[5] = 0;
SetBoxType();
}
/** Set box from double[6] array */
void Box::SetBox(const double* xyzabg) {
if (xyzabg == 0) {
mprinterr("Error: SetBox: Input array is null\n");
return;
}
box_[0] = xyzabg[0];
box_[1] = xyzabg[1];
box_[2] = xyzabg[2];
box_[3] = xyzabg[3];
box_[4] = xyzabg[4];
box_[5] = xyzabg[5];
SetBoxType();
}
/** Set box from unit cell matrix. */
void Box::SetBox(Matrix_3x3 const& ucell) {
Vec3 x_axis = ucell.Row1();
Vec3 y_axis = ucell.Row2();
Vec3 z_axis = ucell.Row3();
box_[0] = x_axis.Normalize(); // A
box_[1] = y_axis.Normalize(); // B
box_[2] = z_axis.Normalize(); // C
box_[3] = y_axis.Angle( z_axis ) * Constants::RADDEG; // alpha
box_[4] = x_axis.Angle( z_axis ) * Constants::RADDEG; // beta
box_[5] = x_axis.Angle( y_axis ) * Constants::RADDEG; // gamma
SetBoxType();
}
// Box::SetTruncOct()
/** Set as truncated octahedron with all lengths set to whatever X is. */
void Box::SetTruncOct() {
box_[1] = box_[0];
box_[2] = box_[0];
box_[3] = TRUNCOCTBETA_;
box_[4] = TRUNCOCTBETA_;
box_[5] = TRUNCOCTBETA_;
btype_ = TRUNCOCT;
mprintf("Info: Setting box to be perfect truncated octahedron (a=b=g=%g)\n", box_[3]);
}
// Box::SetNoBox()
void Box::SetNoBox() {
box_[0] = 0;
box_[1] = 0;
box_[2] = 0;
box_[3] = 0;
box_[4] = 0;
box_[5] = 0;
btype_ = NOBOX;
}
// Box::SetMissingInfo()
/** Set this box info from rhs if <= 0. */
void Box::SetMissingInfo(const Box& rhs) {
if (box_[0] <= 0) box_[0] = rhs.box_[0];
if (box_[1] <= 0) box_[1] = rhs.box_[1];
if (box_[2] <= 0) box_[2] = rhs.box_[2];
if (box_[3] <= 0) box_[3] = rhs.box_[3];
if (box_[4] <= 0) box_[4] = rhs.box_[4];
if (box_[5] <= 0) box_[5] = rhs.box_[5];
SetBoxType();
}
bool Box::IsTruncOct(double angle) {
return (angle > TruncOctMin_ && angle < TruncOctMax_);
}
bool Box::BadTruncOctAngle(double angle) {
return (fabs( TRUNCOCTBETA_ - angle ) > TruncOctEps_);
}
// Box::SetBoxType()
/** Determine box type (none/ortho/nonortho) based on box angles. */
void Box::SetBoxType() {
btype_ = NONORTHO;
bool noLengths = (box_[0] < Constants::SMALL &&
box_[1] < Constants::SMALL &&
box_[2] < Constants::SMALL);
bool noAngles = ( box_[3] <= 0 && box_[4] <= 0 && box_[5] <= 0);
if ( noLengths ) {
// No lengths, no box
btype_ = NOBOX;
if (!noAngles)
mprintf("Warning: Box length(s) <= 0.0; setting box to NONE.\n");
} else if ( noAngles ) {
// No angles, no box
mprintf("Warning: Box angle(s) <= 0.0; setting box to NONE.\n");
btype_ = NOBOX;
} else if (box_[3] == 90.0 && box_[4] == 90.0 && box_[5] == 90.0)
// All 90, orthogonal
btype_ = ORTHO;
else if ( IsTruncOct( box_[3] ) && IsTruncOct( box_[4] ) && IsTruncOct( box_[5] ) )
// All 109.47, truncated octahedron
btype_ = TRUNCOCT;
else if (box_[3] == 0 && box_[4] != 0 && box_[5] == 0) {
// Only beta angle is set (e.g. from Amber topology).
if (box_[4] == 90.0) {
btype_ = ORTHO;
box_[3] = 90.0;
box_[5] = 90.0;
//if (debug_>0) mprintf("\tSetting box to be orthogonal\n");
} else if ( IsTruncOct( box_[4] ) ) {
btype_ = TRUNCOCT;
box_[3] = box_[4];
box_[5] = box_[4];
//if (debug_>0) mprintf("\tSetting box to be a truncated octahedron, angle is %lf\n",box_[3]);
} else if (box_[4] == 60.0) {
btype_ = RHOMBIC;
box_[3]=60.0;
box_[4]=90.0;
box_[5]=60.0;
//if (debug_>0) mprintf("\tSetting box to be a rhombic dodecahedron, alpha=gamma=60.0, beta=90.0\n");
} else {
mprintf("Warning: Box: Unrecognized beta (%g); setting all angles to beta.\n",box_[4]);
box_[3] = box_[4];
box_[5] = box_[4];
}
}
//if (debug_>0) mprintf("\tBox type is %s (beta=%lf)\n",TypeName(), box_[4]);
// Check for low-precision truncated octahedron angles.
if (btype_ == TRUNCOCT) {
if ( BadTruncOctAngle(box_[3]) || BadTruncOctAngle(box_[4]) || BadTruncOctAngle(box_[5]) )
mprintf("Warning: Low precision truncated octahedron angles detected (%g vs %g).\n"
"Warning: If desired, the 'box' command can be used during processing\n"
"Warning: to set higher-precision angles.\n", box_[4], TRUNCOCTBETA_);
}
}
// Box::ToRecip()
/** Use box coordinates to calculate unit cell and fractional matrix for use
* with imaging routines. Return cell volume.
*/
double Box::ToRecip(Matrix_3x3& ucell, Matrix_3x3& recip) const {
double u12x,u12y,u12z;
double u23x,u23y,u23z;
double u31x,u31y,u31z;
double volume,onevolume;
// If box lengths are zero no imaging possible
if (box_[0]==0.0 || box_[1]==0.0 || box_[2]==0.0) {
ucell.Zero();
recip.Zero();
return -1.0;
}
ucell[0] = box_[0]; // u(1,1)
ucell[1] = 0.0; // u(2,1)
ucell[2] = 0.0; // u(3,1)
ucell[3] = box_[1]*cos(Constants::DEGRAD*box_[5]); // u(1,2)
ucell[4] = box_[1]*sin(Constants::DEGRAD*box_[5]); // u(2,2)
ucell[5] = 0.0; // u(3,2)
ucell[6] = box_[2]*cos(Constants::DEGRAD*box_[4]);
ucell[7] = (box_[1]*box_[2]*cos(Constants::DEGRAD*box_[3]) - ucell[6]*ucell[3]) / ucell[4];
ucell[8] = sqrt(box_[2]*box_[2] - ucell[6]*ucell[6] - ucell[7]*ucell[7]);
// Get reciprocal vectors
u23x = ucell[4]*ucell[8] - ucell[5]*ucell[7];
u23y = ucell[5]*ucell[6] - ucell[3]*ucell[8];
u23z = ucell[3]*ucell[7] - ucell[4]*ucell[6];
u31x = ucell[7]*ucell[2] - ucell[8]*ucell[1];
u31y = ucell[8]*ucell[0] - ucell[6]*ucell[2];
u31z = ucell[6]*ucell[1] - ucell[7]*ucell[0];
u12x = ucell[1]*ucell[5] - ucell[2]*ucell[4];
u12y = ucell[2]*ucell[3] - ucell[0]*ucell[5];
u12z = ucell[0]*ucell[4] - ucell[1]*ucell[3];
volume=ucell[0]*u23x + ucell[1]*u23y + ucell[2]*u23z;
onevolume = 1.0 / volume;
recip[0] = u23x*onevolume;
recip[1] = u23y*onevolume;
recip[2] = u23z*onevolume;
recip[3] = u31x*onevolume;
recip[4] = u31y*onevolume;
recip[5] = u31z*onevolume;
recip[6] = u12x*onevolume;
recip[7] = u12y*onevolume;
recip[8] = u12z*onevolume;
return volume;
}
void Box::PrintInfo() const {
mprintf("\tBox: '%s' XYZ= { %8.3f %8.3f %8.3f } ABG= { %6.2f %6.2f %6.2f }\n",
BoxNames_[btype_], box_[0], box_[1], box_[2], box_[3], box_[4], box_[5]);
}
#ifdef MPI
int Box::SyncBox(Parallel::Comm const& commIn) {
commIn.MasterBcast( &btype_, 1, MPI_INT );
commIn.MasterBcast( box_, 6, MPI_DOUBLE );
return 0;
}
#endif