-
Notifications
You must be signed in to change notification settings - Fork 0
/
basinhopping.cc
225 lines (178 loc) · 5.69 KB
/
basinhopping.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
#include <vector>
#include <random>
#include <iostream>
#include <iomanip>
#include "basinhopping.h"
#include "geometry.h"
#include "potential.h"
#include "iop.h"
#include "parameter.h"
#include "lina.h"
#include "storage.h"
using namespace std;
template <typename T>
int BasinHopping<T>::run ()
{
vector<double> p;
parameter<double> opt; //vector of algo settings, 0 == accuracy, 1 == dforce, 2 == stepsize, 3 == nsteps
parameter<int> switches; //vector of switches, 0 == potential, 1 == algo, 2 == scaling
double scalingFactor(1.0);
int status = readsettings(opt, p, switches, scalingFactor);
std::unique_ptr< pairPotential > potential;
switch (status)
{
case 0:
cerr << "Failed reading settings file" << endl;
return 1;
case 1:
potential.reset( LJ::readPotential() );
break;
case 2:
potential.reset( ELJ::readPotential() );
break;
case 3:
potential.reset( RangeLJ::readPotential() );
break;
default:
cerr << "readsettings returned an unknown status" << endl;
return 1;
}
ofstream dummy;
//dummy.open("out");
float progress;
for (int i = 0; i < _nsteps; i++)
{
_accepted = false;
_iteration++;
_previousStep.setEnergy(potential->calcEnergy(_previousStep));
_currentStep.setEnergy(potential->calcEnergy(_currentStep));
_currentStep = potential->optimize(dummy, _currentStep);
if (!_currentStep.isConverged())
{
_currentStep = _previousStep; //rejected because optimisation not converged
}
else
{
vector< vector<double> >hessian = potential->calcHessian(_currentStep);
vector<double> eigenValues = diag(hessian);
_currentStep.setHessian(eigenValues);
if (_currentStep.isMinimum()) //check for true minimum
{
if (this->checkConf()) //check spherical container
{
double oldE, newE = _currentStep.getEnergy();
if (_iteration == 1) {oldE = newE;}
else {oldE = _previousStep.getEnergy();}
if (this->acceptStep(oldE, newE)) //accept the step
{
_accepted = true;
_uniqueStructures.addCluster(_currentStep);
}
else {_currentStep = _previousStep;} //rejected
}
else{_currentStep = _previousStep;} //rejected because spherical container
}
else {_currentStep = _previousStep;} //rejected because of hessian
}
//cout.precision(5);
//cout << fixed << left << setprecision(10);
//cout << setfill(' ') << left <<
// _iteration << left <<
// " oldE = " << left << _previousStep.getEnergy() << left <<
// " newE = " << left << _currentStep.getEnergy() << left <<
// " accepted = " << boolalpha << accepted <<
// endl;
int barWidth = 70;
progress = i / static_cast<double>(_nsteps);
cout << "[";
int pos = barWidth * progress;
for (int j = 0; j < barWidth; j++)
{
if (j < pos) cout << "=";
else if (j == pos) cout << ">";
else cout << " ";
}
cout << "] " << static_cast<int>(progress * 100.0) << " % N: " << this->nStructures() << " step: " << _stepScale << " T: " << _accept->getT() << "\r";
cout.flush();
this->updateStep();
this->propagate();
}
cout << endl;
//dummy.close();
_uniqueStructures.printStructures();
return 0;
}
template <typename T>
void BasinHopping<T>::propagate()
{
_previousStep = _currentStep;
vector<coord3d> coordinates = _currentStep.getCoordinates();
random_device r;
default_random_engine generator{r()};
for (auto& i : coordinates)
{
for (int j = 0; j < 3; j++)
{
uniform_real_distribution<double> distribution(-1.0,1.0);
i[j] += _stepScale * distribution(generator);
}
}
_currentStep.setCoordinates(coordinates);
}
template <typename T>
void BasinHopping<T>::updateStep()
{
if (_iteration == 1) return;
_nattempts++;
if (_accepted) _naccept++;
if ( fabs(_currentStep.getEnergy() - _previousStep.getEnergy()) < 1e-4) _nsame++;
if (_nattempts % _interval == 0)
{
adjustStep();
adjustTemp();
resetUpdateStep();
}
}
template <typename T>
void BasinHopping<T>::adjustStep ()
{
double f = 1 - (static_cast<double>(_nsame) / _nattempts);
if (f < 0.71) _stepScale /= 0.95;
else _stepScale *= 0.95;
}
template <typename T>
void BasinHopping<T>::adjustTemp ()
{
double temp = _accept->getT();
int ndiff = _nattempts - _nsame;
int ndiff_accept = _naccept - _nsame;
double f(0);
if (ndiff == 0) f = 1;
else f = static_cast<double>(ndiff_accept) / ndiff;
if (f > 0.71) _accept->setT(temp * 0.95);
else _accept->setT(temp / 0.95);
}
template <typename T>
void BasinHopping<T>::resetUpdateStep ()
{
_nattempts = 0;
_naccept = 0;
_nsame = 0;
}
template <typename T>
bool BasinHopping<T>::acceptStep (double oldE, double newE)
{
return (*_accept)(oldE, newE);
}
template <typename T>
bool BasinHopping<T>::checkConf()
{
vector<coord3d> coordinates = _currentStep.getCoordinates();
for (auto& i : coordinates)
{
if (i.norm() > 4) return false;
}
return true;
}
template class BasinHopping<StorageByEnergy>;
template class BasinHopping<StorageByInterPartDist>;