-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathStructure.cpp
214 lines (182 loc) · 6.54 KB
/
Structure.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
#include <cstring>
#include <cmath>
#include <cfloat>
#include <fstream>
#include <climits>
#include "Structure.h"
using namespace cfp_internal;
Structure::Structure()
{
// Per structure description
mStepId = -1;
mNumAtoms = 0;
mUnitCell[15] = 0.0F; // means no unit cell
mEnergyPerAtom = 0.0F;
mHasEnergy = false;
// Fingerprint
mFingerprintSectionLen = 0;
mFingerprintNumSections = 0;
}
Structure::Structure(int aStepId, int aNumAtoms, const float* aCoords, const unsigned int* aZ, const float* aUnitCell, float aEnergyPerAtom, bool aHasEnergy)
{
// Load the base data
mStepId = aStepId;
mNumAtoms = aNumAtoms;
mEnergyPerAtom = aEnergyPerAtom;
mHasEnergy = aHasEnergy;
// If no atoms or no unit cell do not load it
if(aUnitCell == 0)
{
mUnitCell[15] = 0.0F; // means no unit cell
}
else
{
memcpy(mUnitCell, aUnitCell, 16*sizeof(float));
}
mCoordinates.assign(aCoords, aCoords+3*aNumAtoms);
mAtomZ.assign(aZ, aZ+aNumAtoms);
// Fingerprint
mFingerprintSectionLen = 0;
mFingerprintNumSections = 0;
}
Structure::~Structure()
{
mCoordinates.clear();
mAtomZ.clear();
mFingerprint.clear();
mWeights.clear();
mInteratomicDistances.clear();
}
// Copy constructor and assignment
Structure::Structure(const Structure& p)
{
// Copy per structure data
mStepId = p.mStepId;
mNumAtoms = p.mNumAtoms;
mEnergyPerAtom = p.mEnergyPerAtom;
mHasEnergy = p.mHasEnergy;
mCoordinates = p.mCoordinates;
mAtomZ = p.mAtomZ;
memcpy(mUnitCell, p.mUnitCell, 16*sizeof(float));
// Copy the fingerprint data
mFingerprint = p.mFingerprint;
mFingerprintSectionLen = p.mFingerprintSectionLen;
mFingerprintNumSections = p.mFingerprintNumSections;
mWeights = p.mWeights;
// Copy the interatomic distances
mInteratomicDistances = p.mInteratomicDistances;
}
Structure& Structure::operator=(const Structure& p)
{
// Make sure not same object
if(this != &p)
{
// Copy per structure data
mStepId = p.mStepId;
mNumAtoms = p.mNumAtoms;
mEnergyPerAtom = p.mEnergyPerAtom;
mHasEnergy = p.mHasEnergy;
mCoordinates = p.mCoordinates;
mAtomZ = p.mAtomZ;
memcpy(mUnitCell, p.mUnitCell, 16*sizeof(float));
// Copy the fingerprint data
mFingerprint = p.mFingerprint;
mFingerprintSectionLen = p.mFingerprintSectionLen;
mFingerprintNumSections = p.mFingerprintNumSections;
mWeights = p.mWeights;
// Copy the interatomic distances
mInteratomicDistances = p.mInteratomicDistances;
}
// Return ref for multiple assignment
return *this;
}
float Structure::getMaxDiagonalLength(void) const
{
// First diagonal: a+b+c
float dx = mUnitCell[0] + mUnitCell[4] + mUnitCell[8];
float dy = mUnitCell[1] + mUnitCell[5] + mUnitCell[9];
float dz = mUnitCell[2] + mUnitCell[6] + mUnitCell[10];
float len = dx*dx+dy*dy+dz*dz;
float max_basis_len = len;
// Second diagonal: a-b+c
dx = mUnitCell[0] - mUnitCell[4] + mUnitCell[8];
dy = mUnitCell[1] - mUnitCell[5] + mUnitCell[9];
dz = mUnitCell[2] - mUnitCell[6] + mUnitCell[10];
len = dx*dx+dy*dy+dz*dz;
if(len > max_basis_len) max_basis_len = len;
// Third diagonal: a-b-c
dx = mUnitCell[0] - mUnitCell[4] - mUnitCell[8];
dy = mUnitCell[1] - mUnitCell[5] - mUnitCell[9];
dz = mUnitCell[2] - mUnitCell[6] - mUnitCell[10];
len = dx*dx+dy*dy+dz*dz;
if(len > max_basis_len) max_basis_len = len;
// Forth diagonal: a+b-c
dx = mUnitCell[0] + mUnitCell[4] - mUnitCell[8];
dy = mUnitCell[1] + mUnitCell[5] - mUnitCell[9];
dz = mUnitCell[2] + mUnitCell[6] - mUnitCell[10];
len = dx*dx+dy*dy+dz*dz;
if(len > max_basis_len) max_basis_len = len;
// Return the maximum value
return sqrt(max_basis_len);
}
#include <iostream>
void Structure::serialize(std::ofstream& aStream) const
{
aStream.write((char *)&mStepId, sizeof(int));
aStream.write((char *)&mNumAtoms, sizeof(unsigned int));
if(mNumAtoms) aStream.write((char *)&mCoordinates[0], sizeof(float)*3*mNumAtoms);
if(mNumAtoms) aStream.write((char *)&mAtomZ[0], sizeof(unsigned int)*mNumAtoms);
aStream.write((char *)mUnitCell, sizeof(float)*16);
aStream.write((char *)&mEnergyPerAtom, sizeof(float));
unsigned int x = (mHasEnergy) ? 1 : 0;
aStream.write((char *)&x, sizeof(unsigned int));
aStream.write((char *)&mFingerprintSectionLen, sizeof(unsigned int));
aStream.write((char *)&mFingerprintNumSections, sizeof(unsigned int));
if(mFingerprintSectionLen > 0 && mFingerprintNumSections > 0)
aStream.write((char *)&mFingerprint[0], sizeof(float)*mFingerprintSectionLen*mFingerprintNumSections);
x = static_cast<unsigned int>(mWeights.size());
aStream.write((char *)&x, sizeof(unsigned int));
if(x) aStream.write((char *)&mWeights[0], sizeof(float)*x);
x = static_cast<unsigned int>(mInteratomicDistances.size());
aStream.write((char *)&x, sizeof(unsigned int));
unsigned int i;
for(i=0; i < x; ++i)
{
unsigned int y = static_cast<unsigned int>(mInteratomicDistances[i].size());
aStream.write((char *)&y, sizeof(unsigned int));
if(y) aStream.write((char *)&mInteratomicDistances[i][0], sizeof(float)*y);
}
}
void Structure::unserialize(std::ifstream& aStream)
{
aStream.read((char *)&mStepId, sizeof(int));
aStream.read((char *)&mNumAtoms, sizeof(unsigned int));
mCoordinates.resize(3*mNumAtoms);
if(mNumAtoms) aStream.read((char *)&mCoordinates[0], sizeof(float)*3*mNumAtoms);
mAtomZ.resize(mNumAtoms);
if(mNumAtoms) aStream.read((char *)&mAtomZ[0], sizeof(unsigned int)*mNumAtoms);
aStream.read((char *)mUnitCell, sizeof(float)*16);
aStream.read((char *)&mEnergyPerAtom, sizeof(float));
unsigned int x;
aStream.read((char *)&x, sizeof(unsigned int));
mHasEnergy = (x != 0);
aStream.read((char *)&mFingerprintSectionLen, sizeof(unsigned int));
aStream.read((char *)&mFingerprintNumSections, sizeof(unsigned int));
mFingerprint.resize(mFingerprintSectionLen*mFingerprintNumSections);
if(mFingerprintSectionLen > 0 && mFingerprintNumSections > 0)
aStream.read((char *)&mFingerprint[0], sizeof(float)*mFingerprintSectionLen*mFingerprintNumSections);
aStream.read((char *)&x, sizeof(unsigned int));
mWeights.resize(x);
if(x) aStream.read((char *)&mWeights[0], sizeof(float)*x);
aStream.read((char *)&x, sizeof(unsigned int));
mInteratomicDistances.reserve(x);
for(unsigned int i=0; i < x; ++i)
{
std::vector<float> v;
mInteratomicDistances.push_back(v);
unsigned int y;
aStream.read((char *)&y, sizeof(unsigned int));
mInteratomicDistances[i].resize(y);
if(y) aStream.read((char *)&mInteratomicDistances[i][0], sizeof(float)*y);
}
}