-
Notifications
You must be signed in to change notification settings - Fork 0
/
iop.cc
295 lines (254 loc) · 8.95 KB
/
iop.cc
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
#include <iomanip>
#include <iostream>
#include <fstream>
#include <sstream>
#include <libconfig.h++>
#include "structure.h"
#include "iop.h"
#include "parameter.h"
#include "globals.h"
using namespace std;
using namespace libconfig;
void xyzout (structure &outputStructure, const string &name)
{
vector<coord3d> coordinates = outputStructure.getCoordinates();
ofstream xyz;
xyz.open ("output/" + name);
xyz << outputStructure.nAtoms() << endl;
xyz << "Energy: " << outputStructure.getEnergy() << ", moment of inertia: " << outputStructure.getMomentOfInertia() << endl;
for (int i = 0; i < outputStructure.nAtoms(); i++) {
xyz << setw(5) << left << "X" << setw(15) << setprecision(6) <<
setiosflags(ios::fixed) << right << coordinates[i][0] << setw(15)
<< coordinates[i][1] << setw(15) << coordinates[i][2] << endl;
}
xyz.close();
}
void xyzoutall (vector<structure> &outputStructures, const string &name)
{
ofstream xyz;
xyz.open("output/" + name);
for (vector<structure>::size_type i = 0; i < outputStructures.size(); i++)
{
vector<coord3d> coordinates = outputStructures[i].getCoordinates();
xyz << outputStructures[i].nAtoms() << endl;
xyz << "Energy: " << outputStructures[i].getEnergy()
<< ", moment of inertia: " << outputStructures[i].getMomentOfInertia() << endl;
for (int j = 0; j < outputStructures[i].nAtoms(); j++)
{
xyz << setw(5) << left << "X" << setw(15) << setprecision(6) <<
setiosflags(ios::fixed) << right << coordinates[j][0] <<
setw(15) << coordinates[j][1] << setw(15) << coordinates[j][2]
<< endl;
}
}
xyz.close();
}
void simpleout (vector<structure> &outputStructures, stringstream &output) {
output.precision(14);
for (vector<structure>::size_type i=0; i < outputStructures.size(); i++) {
for (int j=0; j < outputStructures[i].nAtoms(); j++) {
output << outputStructures[i][j][0] << " " << outputStructures[i][j][1] << " " << outputStructures[i][j][2] << endl;
}
output << endl;
}
}
//function for determining if a line in the input is emtpy
//used for breaking the read loop
bool justempty(string str) {
if (str.empty()) {
return true;
}
return false;
}
//function for testing if input file exists
//returns true for existing file
bool fexists (const std::string &fileName) {
ifstream infile(fileName.c_str());
bool exist = infile.good();
infile.close();
return exist;
}
//function for reading all strucutures of input file
//structures must be separated by a blank line, last line should be a blank line as well
vector<structure> readallstruct (const std::string& fileName) {
ifstream infile(fileName.c_str());
coord3d sphere; //sphere refers to 1 'atom'
string line;
int number = 1;
vector<structure> allKissingSpheres; //this vector stores all read in structures
//double while loop, inner loop goes over all coordinates of one structure, outer loop goes over all
//structures till eof
while (true) {
vector<coord3d> coordinates;
while (true) {
getline(infile,line);
if(justempty(line) || infile.eof()) break;
else {
stringstream lineStream(line); //string can't be read into coord3d object, convert to
//stringstream first
lineStream >> sphere;
coordinates.push_back(sphere);
}
}
structure kissingSphere(number, coordinates); //kissingSphere refers to 1 cluster of kissing Spheres
if (infile.eof()) break; //does last line has to be blank? break works for files with blank line at
//the end
else {
allKissingSpheres.push_back(kissingSphere);
number++;
}
}
infile.close();
//cout << "\t" << "Number of structures: " << allKissingSpheres.size() << endl;
return allKissingSpheres;
}
/*-------------------------------------------------------------------------------------*/
// function to read settings file
// return value determins potential
// 0 = fail
// 1 = LJ
// 2 = ELJ
/*-------------------------------------------------------------------------------------*/
int readsettings (parameter<double> &opt, vector<double> &p, parameter<int> &switches, double &scalingFactor) {
libconfig::Config cfg;
try {
cfg.readFile("settings");
}
catch(const FileIOException &fioex) {
cerr << "\tI/O error while reading file." << endl;
return 0;
}
catch(const ParseException &pex) {
cerr << "\tParse error at " << pex.getFile() << ":" << pex.getLine() << " - " << pex.getError() << endl;
return 0;
}
const Setting &root = cfg.getRoot();
string potential, algo;
double convergence(1e-3);
int potential_switch(0), algo_switch, scaling_switch;
int nsteps(100);
cout << endl;
if (cfg.lookupValue("scaling.factor", scalingFactor)) {
cout << "\tScaling: " << scalingFactor << endl;
scaling_switch = 1;
}
else {
cout << "\tNo scaling" << endl;
}
if (cfg.lookupValue("potential.name", potential)) {
if (potential == "LJ")
{
potential_switch = 1;
}
else if (potential == "ELJ")
{
potential_switch = 2;
}
else if (potential == "RangeLJ")
{
potential_switch = 3;
}
else
{
cerr << potential << " is not a valid name for a potential." << endl;
return 0;
}
cout << "\tPotential: " << potential << endl;
}
else {
cout << "\tNo 'potential' setting in configuration file." << endl;
return 0;
}
if (potential == "LJ") {
double exp1(12), exp2(6), rm, epsilon;
if (cfg.lookupValue("potential.epsilon", epsilon)) {
p.push_back(epsilon);
cout << "\t\tEpsilon: " << epsilon << endl;
}
else {
cout << "\tNo 'epsilon' in configuration file." << endl;
return 0;
}
if (cfg.lookupValue("potential.rm", rm)) {
p.push_back(rm);
cout << "\t\tRm: " << rm << endl;
}
else {
cout << "\tNo 'rm' in configuration file." << endl;
return 0;
}
if (cfg.lookupValue("potential.exp1", exp1)) {
p.push_back(exp1);
cout << "\t\tExp1: " << exp1 << endl;
}
else {
p.push_back(exp1);
cout << "\t\tExp1: " << exp1 << endl;
}
if (cfg.lookupValue("potential.exp2", exp2)) {
p.push_back(exp2);
cout << "\t\tExp2: " << exp2 << endl;
}
else {
p.push_back(exp2);
cout << "\t\tExp2: " << exp2 << endl;
}
}
cout << endl;
if (cfg.lookupValue("opt.name", algo)) {
if (algo == "BFGS") {
algo_switch = 1;
}
if (algo == "CG") {
algo_switch = 2;
}
cout << "\tAlgo: " << algo << endl;
}
else {
cout << "\tNo 'opt' setting in configuration file." << endl;
return 0;
}
if (cfg.lookupValue("opt.convergence", convergence)) {
cout << "\t\tConvergence: " << convergence << endl;
}
else {
cout << "\t\tConvergence: " << convergence << endl;
}
if (cfg.lookupValue("opt.nsteps", nsteps)) {
cout << "\t\tNsteps: " << nsteps << endl;
}
else {
cout << "\t\tNsteps: " << nsteps << endl;
}
opt.set("convergence", convergence); //opt[1]
opt.set("nsteps", nsteps); //opt[3]
switches.set("potential", potential_switch);
switches.set("algo", algo_switch);
switches.set("scaling", scaling_switch);
cout << endl;
return potential_switch;
}
template <typename T> void matrixout (vector< vector<T> > &matrix, ostream &out)
{
//ios::fmtflags f(out.flags());
ios oldState(nullptr);
oldState.copyfmt(out);
out << fixed;
for (typename vector< vector<T> >::size_type i = 0; i < matrix.size(); i++)
{
for (typename vector<T>::size_type j = 0; j < matrix[i].size(); j++)
{
if (j == matrix[i].size() - 1)
{
out << setw(3) << ( (matrix[i][j] == 0) ? setprecision(0) :
setprecision(4) ) << matrix[i][j] << "\\\\";
continue;
}
out << setw(3) << ( (matrix[i][j] == 0) ? setprecision(0) :
setprecision(4) ) << matrix[i][j] << " &"; }
out << endl;
}
out.copyfmt(oldState);
}
template void matrixout<int> (vector< vector<int> > &matrix, ostream &out);
template void matrixout<double> (vector< vector<double> > &matrix, ostream &out);