-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmolecule.cpp
More file actions
209 lines (195 loc) · 5.39 KB
/
molecule.cpp
File metadata and controls
209 lines (195 loc) · 5.39 KB
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
//molecule.cpp
#include "molecule.h"
#include <cstring>
#include <iostream>
#include <vector>
#include <iomanip>
#include <fstream>
#include <cstdlib>
void Molecule::Initiate(std::string filename, std::string filetype)
{
if (filetype == "xyz")
{
std::string strLine;
std::string stringtok; //needed to read tokLine
char * tokLine;
char * charLine;
int counterLine=0;
int counterTok=0;
int strlength;
std::ifstream inputFile(filename.c_str());
if (!inputFile){std::cout << "File did not open\n"; exit(1);}
while (getline(inputFile, strLine))
{
if (counterLine == 0) {atomCount = atoi(strLine.c_str());}
else if (counterLine > 1 && counterLine < atomCount+2) //YOU ARE HERE
{
strlength = strLine.length();
charLine = new char[strlength+1]();
strcpy(charLine, strLine.c_str());
atomVec.push_back(atom());
tokLine = strtok(charLine," ");
while (tokLine != NULL) // used to be while (tokLine != NULL)
{
stringtok = tokLine;
if (counterTok == 0){atomVec[counterLine-2].atomSym = stringtok;}
if (counterTok == 1){atomVec[counterLine-2].coord[0] = atof(tokLine);}
if (counterTok == 2){atomVec[counterLine-2].coord[1] = atof(tokLine);}
if (counterTok == 3){atomVec[counterLine-2].coord[2] = atof(tokLine);}
tokLine = strtok (NULL, " ");
counterTok++;
}
counterTok = 0;
delete charLine;
}
counterLine++;
}
inputFile.close();
}
return;
}
double Molecule::GetAtomNum(std::string sym)
{
if (sym == "H"){return 1.0;}
if (sym == "C"){return 6.0;}
if (sym == "N"){return 7.0;}
if (sym == "O"){return 8.0;}
if (sym == "F"){return 9.0;}
if (sym == "Al" || sym == "AL"){return 13.0;}
if (sym == "Si" || sym == "SI"){return 14.0;}
if (sym == "S"){return 16.0;}
if (sym == "Cl" || sym == "CL"){return 17.0;}
if (sym == "Au" || sym == "AU"){return 79.0;}
return 0;
}
void Molecule::PrintInfo(std::string filename, std::string filetype)
{
if (filetype == "xyz")
{
std::ofstream outputFile;
outputFile.open(filename.c_str());
if (outputFile)
{
outputFile << atomCount << "\n\n";
for (int i = 0; i < atomCount; i++)
{
outputFile << std::fixed;//keep in loop?
outputFile << atomVec[i].atomSym << " ";
outputFile << std::setprecision(9) << atomVec[i].coord[0]
<< " " << atomVec[i].coord[1] << " "
<< atomVec[i].coord[2] << '\n';
}
}
}
if (filetype == "gamess")
{
std::ofstream outputFile;
outputFile.open(filename.c_str());
if (outputFile)
{
for (int i = 0; i < atomCount; i++)
{
outputFile << std::fixed;//keep in loop?
outputFile << atomVec[i].atomSym << " ";
outputFile << std::setprecision(1) << GetAtomNum(atomVec[i].atomSym) << " ";
outputFile << std::setprecision(9) << atomVec[i].coord[0]
<< " " << atomVec[i].coord[1] << " "
<< atomVec[i].coord[2] << '\n';
}
}
}
if (filetype == "maple")//probably not needed
{
std::ofstream outputFile;
outputFile.open(filename.c_str());
if (outputFile)
{
outputFile << "mol.atom=\"";
for (int i = 0; i < atomCount; i++)
{
outputFile << std::fixed;
outputFile << atomVec[i].atomSym << " ";
outputFile << std::setprecision(9) << atomVec[i].coord[0]
<< " " << atomVec[i].coord[1]
<< " " << atomVec[i].coord[2] << ";";
}
outputFile << "\"";
}
}
return;
}
void Molecule::SetAtomCoord(int ithAtom,char toEdit, double coordValue)
{
if(toEdit == 'x'){atomVec[ithAtom].coord[0] = coordValue;}
else if(toEdit == 'y'){atomVec[ithAtom].coord[1] = coordValue;}
else if(toEdit == 'z'){atomVec[ithAtom].coord[2] = coordValue;}
return;
}
void Molecule::SetAtomSym(int ithAtom, std::string newAtom){atomVec[ithAtom].atomSym = newAtom;}
double Molecule::GetAtomCoord(int ithAtom, char toGet)
{
if (toGet == 'x'){return atomVec[ithAtom].coord[0];}
else if (toGet == 'y'){return atomVec[ithAtom].coord[1];}
else if (toGet == 'z'){return atomVec[ithAtom].coord[2];}
return 0.0;
}
std::string Molecule::GetAtomSym(int ithAtom){return atomVec[ithAtom].atomSym;}
void Molecule::AddAtom(std::string AtSym, double coordX, double coordY, double coordZ)
{
const int CURRSIZE = atomVec.size();
atomVec.push_back(atom());
atomVec[CURRSIZE].atomSym = AtSym;
atomVec[CURRSIZE].coord[0] = coordX;
atomVec[CURRSIZE].coord[1] = coordY;
atomVec[CURRSIZE].coord[2] = coordZ;
atomCount += 1;
return;
}
void Molecule::RemoveAtom(int ithAtom)
{
atomVec.erase(atomVec.begin()+ithAtom);
atomCount -= 1;
return;
}
int Molecule::GetAtomCount() {return atomCount;}
void Molecule::SwapStruct(atom& atom1, atom& atom2)
{
atom tmpatom;
tmpatom.atomSym = atom1.atomSym;
tmpatom.coord[0] = atom1.coord[0];
tmpatom.coord[1] = atom1.coord[1];
tmpatom.coord[2] = atom1.coord[2];
atom1.atomSym = atom2.atomSym;
atom1.coord[0] = atom2.coord[0];
atom1.coord[1] = atom2.coord[1];
atom1.coord[2] = atom2.coord[2];
atom2.atomSym = tmpatom.atomSym;
atom2.coord[0] = tmpatom.coord[0];
atom2.coord[1] = tmpatom.coord[1];
atom2.coord[2] = tmpatom.coord[2];
}
void Molecule::SortByCoord(char axis)
{
int i, j, a;
double tmpval;
switch (axis)
{
case 'x': a=0; break;
case 'X': a=0; break;
case 'y': a=1; break;
case 'Y': a=1; break;
case 'z': a=2; break;
case 'Z': a=2; break;
default: break;
}
for (i=1;i<atomVec.size();i++)
{
j=i;
tmpval=atomVec[i].coord[a];
while (j>0 && tmpval<atomVec[j-1].coord[a])
{
SwapStruct(atomVec[j],atomVec[j-1]);
j--;
}
}
}