-
Notifications
You must be signed in to change notification settings - Fork 0
/
pso_serial_B5.cpp
367 lines (316 loc) · 10.1 KB
/
pso_serial_B5.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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#include <bits/stdc++.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <iostream>
#include <ctime>
#include <cstdlib>
#include <fstream>
#include <string>
#define atomCount 5
#define structCount 14
using namespace std;
typedef struct atom{
char symbol[3];
double coX;
double coY;
double coZ;
double velX;
double velY;
double velZ;
}atom;
typedef struct particle{
struct atom atoms[atomCount];
struct atom pBest[atomCount];
double cost;
double pBestCost;
}particle;
std::string to_string(int i)
{
std::stringstream ss;
ss << i;
return ss.str();
}
void gengjf(struct particle particles[], int particleNum)
{
string str1 = "str";
string str2 = to_string(particleNum);
string str3 = ".gjf";
string str4 = str1 + str2 + str3;
fstream gjfFile;
gjfFile.open(str4.c_str(), ios::out | ios::trunc);
char procShared[] = "%nprocshared=2";
char memUsed[] = "%mem=2GB";
char noSave[] = "%nosave";
char titleCard[] = "B5 struct";
char method[] = "# b3lyp/6-311+g(d,p)";
char chargeSpin[] = "0 2";
gjfFile << procShared << endl;
gjfFile << memUsed << endl;
gjfFile << noSave << endl;
gjfFile << method << endl;
gjfFile << endl << titleCard << endl << endl;
gjfFile << chargeSpin << endl;
/*
double dbl = 1278.65626487, dbl2 = 123.31541458;
char buffer [100];
int n, a=5, b=3;
n=snprintf (buffer, 100, "%lf plus %lf is %lf", dbl, dbl2, dbl+dbl2);
printf ("[%s] is a string %d chars long\n\n",buffer,n);*/
cout << "Generating gjf file: " << str4 << endl;
for(int i = 0; i < atomCount; i++)
{
char buff[100];
int n;
const char* zeroFloat = "0.00000000";
gjfFile << " " << particles[particleNum-1].atoms[i].symbol << "\t\t\t\t\t";
if(particles[particleNum-1].atoms[i].coX == 0.0)
gjfFile << zeroFloat << "\t";
else
{
n = sprintf(buff, "%lf\t", particles[particleNum - 1].atoms[i].coX);
gjfFile << buff;
}
if(particles[particleNum-1].atoms[i].coY == 0.0)
gjfFile << zeroFloat << "\t";
else
{
n = sprintf(buff, "%lf\t", particles[particleNum - 1].atoms[i].coY);
gjfFile << buff;
}
if(particles[particleNum-1].atoms[i].coZ == 0.0)
gjfFile << zeroFloat << "\n";
else
{
n = sprintf(buff, "%lf", particles[particleNum - 1].atoms[i].coZ);
gjfFile << buff << "\n";
}
}
gjfFile << "\n\n\n\n";
gjfFile.close();
}
string readEnergy(string logFile)
{
string search = "E(UB3LYP)";
ifstream inFile;
string line;
inFile.open(logFile.c_str());
string zeroRet = "0.00";
if(!inFile)
{
cout << "Unable to open file" << endl;
//return zeroRet.c_str();
return zeroRet;
}
string energyLine;
int count = 0, lastFound = 0;
size_t pos;
while(inFile.good())
{
count++;
getline(inFile,line);
pos=line.find(search);
if(pos!=string::npos)
{
lastFound = count;
energyLine = line;
}
}
std::string delimiter = " ";
size_t posi = 0;
string token;
string energy;
int tokenCount = 0;
while ((posi = energyLine.find(delimiter)) != std::string::npos)
{
tokenCount++;
token = energyLine.substr(0, posi);
if(tokenCount == 8)
{
energy = token;
break;
}
energyLine.erase(0, posi + delimiter.length());
}
//return energy.c_str();
return energy;
}
void initializePos(struct particle particles[], int structureCount)
{
for(int i = 1;i < structureCount+1;i++)
{
string str1 = "str";
string str2 = to_string(i);
string str3 = ".txt";
string str5 = ".gjf";
string str4 = str1 + str2 + str3;
cout << "FILE to be opened: " << str4 << "\n";
fstream valFile;
valFile.open(str4.c_str(), ios::in);
char data[20];
for(int j=0; j <atomCount; j++)
{
valFile >> particles[i-1].atoms[j].symbol;
strcpy(particles[i-1].pBest[j].symbol, particles[i-1].atoms[j].symbol);
valFile >> data;
string temp1(data);
particles[i-1].atoms[j].coX = strtod(temp1.c_str(), NULL);
particles[i-1].pBest[j].coX = particles[i-1].atoms[j].coX;
valFile >> data;
string temp2(data);
particles[i-1].atoms[j].coY = strtod(temp2.c_str(), NULL);
particles[i-1].pBest[j].coY = particles[i-1].atoms[j].coY;
valFile >> data;
string temp3(data);
particles[i-1].atoms[j].coZ = strtod(temp3.c_str(), NULL);
particles[i-1].pBest[j].coZ = particles[i-1].atoms[j].coZ;
particles[i-1].atoms[j].velX = 0;
particles[i-1].atoms[j].velY = 0;
particles[i-1].atoms[j].velZ = 0;
}
particles[i-1].pBestCost = 100000;
particles[i-1].cost = 100000;
gengjf(particles, i);
string command = "g09 " + str1 + str2 + str5;// + " &"; // Uncomment the '&' to execute the program in the background.
cout << "command: " << command <<"\n";
system(command.c_str());
string str6 = ".log";
double energyCost = atof(readEnergy(str1+str2+str6).c_str());
particles[i-1].cost = particles[i-1].pBestCost = energyCost;
printf("Energy Read: %lf\n", particles[i-1].pBestCost);
valFile.close();
}
}
int relDiff(double prev, double curr)
{
if(prev-curr <= 0)
return 0;
else
{
double temp = (prev-curr)*100/prev;
//if(temp >= 0.01) Commented by SSL
if(temp >= 0.0000001)
return 1;
else
return 0;
}
}
int main(int argc, char const *argv[])
{
struct particle particles[structCount];
initializePos(particles, structCount);
struct particle globalBest;
globalBest.cost = 1000000;
float wMax = 0.9;
float wMin = 0.4;
float w;
int iterMax = 1000, iter = 0;
//int iterMax = 3, iter = 0; //Commented by SSL
int c1 = 2, c2 = 2;
double r1, r2;
for(int i = 1; i < structCount+1 ;i++)
{
if(globalBest.cost > particles[i-1].cost)
{
globalBest.cost = particles[i-1].cost;
for(int j = 0; j < atomCount; j++)
{
strcpy(globalBest.atoms[j].symbol, particles[i-1].atoms[j].symbol);
globalBest.atoms[j].coX = particles[i-1].atoms[j].coX;
globalBest.atoms[j].coY = particles[i-1].atoms[j].coY;
globalBest.atoms[j].coZ = particles[i-1].atoms[j].coZ;
}
}
}
srand (static_cast <unsigned> (time(0)));
double prevBest = 10000;
while(iter < iterMax) // && relDiff(prevBest, globalBest.cost) == 1) //Commentd the last part by SSL
//while(iter < iterMax && relDiff(prevBest, globalBest.cost) == 1) //Commented by SSL
{
prevBest = globalBest.cost;
w = wMax - ((wMax - wMin) * iter / iterMax);
cout << "\n\nITERATION: " << iter << " \n\n";
for(int i = 1; i < structCount+1; i++)
{
printf("Particle %d: \n", i);
for(int j = 0; j < atomCount; j++)
{
float r;
r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
r1 = r;
r = static_cast <float> (rand()) / static_cast <float> (RAND_MAX);
r2 = r;
//r1 = r2 = 1;
particles[i-1].atoms[j].velX = w * (particles[i-1].atoms[j].velX) + c1 * r1 * (particles[i-1].pBest[j].coX - particles[i-1].atoms[j].coX) + c2 * r2 * (globalBest.atoms[j].coX - particles[i-1].atoms[j].coX);
particles[i-1].atoms[j].velY = w * (particles[i-1].atoms[j].velY) + c1 * r1 * (particles[i-1].pBest[j].coY - particles[i-1].atoms[j].coY) + c2 * r2 * (globalBest.atoms[j].coY - particles[i-1].atoms[j].coY);
particles[i-1].atoms[j].velZ = w * (particles[i-1].atoms[j].velZ) + c1 * r1 * (particles[i-1].pBest[j].coZ - particles[i-1].atoms[j].coZ) + c2 * r2 * (globalBest.atoms[j].coZ - particles[i-1].atoms[j].coZ);
particles[i-1].atoms[j].coX = particles[i-1].atoms[j].coX + particles[i-1].atoms[j].velX;
particles[i-1].atoms[j].coY = particles[i-1].atoms[j].coY + particles[i-1].atoms[j].velY;
particles[i-1].atoms[j].coZ = particles[i-1].atoms[j].coZ + particles[i-1].atoms[j].velZ;
}
printf("Position\n");
for(int j = 0; j < atomCount; j++)
{
printf("%lf \t %lf \t %lf\n", particles[i-1].atoms[j].coX, particles[i-1].atoms[j].coY, particles[i-1].atoms[j].coZ);
}
printf("Velocities\n");
for(int j = 0; j < atomCount; j++)
{
printf("%lf \t %lf \t %lf\n", particles[i-1].atoms[j].velX, particles[i-1].atoms[j].velY, particles[i-1].atoms[j].velZ);
}
gengjf(particles, i);
string str1 = "str";
string str2 = to_string(i);
string str3 = ".txt";
string str5 = ".gjf";
string command = "g09 " + str1 + str2 + str5;// + " &"; // Uncomment the '&' to execute the program in the background.
cout << command <<"\n";
system(command.c_str());
string str6 = ".log";
double energyCost = atof(readEnergy(str1+str2+str6).c_str());
particles[i-1].cost = energyCost;
printf("Energy read: %lf\n", energyCost);
if(particles[i-1].cost < particles[i-1].pBestCost)
{
printf("INSIDE1\n");
for(int j = 0; j < atomCount; j++)
{
particles[i-1].pBest[j].coX = particles[i-1].atoms[j].coX;
particles[i-1].pBest[j].coY = particles[i-1].atoms[j].coY;
particles[i-1].pBest[j].coZ = particles[i-1].atoms[j].coZ;
}
particles[i-1].pBestCost = particles[i-1].cost;
}
if(particles[i-1].cost < globalBest.cost)
{
printf("INSIDE2\n");
for(int j = 0; j < atomCount; j++)
{
globalBest.atoms[j].coX = particles[i-1].atoms[j].coX;
globalBest.atoms[j].coY = particles[i-1].atoms[j].coY;
globalBest.atoms[j].coZ = particles[i-1].atoms[j].coZ;
}
globalBest.cost = particles[i-1].cost;
}
}
//Added by SSL on 29/11/2017
printf ("==================================");
printf("\\Global Best So far at the end of Iteration: %d\n", iter);
for(int j = 0; j < atomCount; j++)
{
printf("%lf \t %lf \t %lf\n", globalBest.atoms[j].coX, globalBest.atoms[j].coY, globalBest.atoms[j].coZ);
}
printf("Global Best Cost: %lf\n", globalBest.cost);
printf ("==================================");
//End of Added by SSL on 29/11/2017
iter++;
}
printf("Final Position\n");
for(int j = 0; j < atomCount; j++)
{
printf("%lf \t %lf \t %lf\n", globalBest.atoms[j].coX, globalBest.atoms[j].coY, globalBest.atoms[j].coZ);
}
printf("Global Best Cost: %lf\n", globalBest.cost);
return 0;
}