-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint.cpp
176 lines (139 loc) · 4.65 KB
/
point.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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <assert.h>
#include <pthread.h>
#include <unistd.h>
#include <gsl/gsl_multimin.h>
#include <vector>
#include <boost/thread.hpp>
#include <sstream>
#include <limits>
#include "foreach.hpp"
#include "model.hpp"
#include "data.hpp"
#include "vis.hpp"
#include "minimiser.hpp"
using namespace std;
using namespace boost;
/*********************************************************************************
* Global State
*********************************************************************************/
//Nuclear Positions
Nuclei nuclei;
//Experimental Vals
Vals expVals;
//Calculated values to display
Vals calcVals;
PointModel lastModel;
/********************************************************************************
* Logging
********************************************************************************/
ofstream fout;
/********************************************************************************
* Visuliser
*********************************************************************************/
struct VisualThread {
VisualThread(){}
void operator()() {
fw.mainLoop();
}
FittingWindow fw;
private:
VisualThread(const VisualThread&);
};
//When set to true, created threads should exit
bool threadExit = false;
double minf(PointModel thisModel) {
//Now reduce the results
double total = 0;
Vals results;
results.resize(nuclei.size());
for(unsigned long i = 0;i<expVals.size();i++) {
results[i] = thisModel.eval(nuclei[i].x,nuclei[i].y,nuclei[i].z);
double diff = expVals[i] - results[i];
total += diff*diff;
}
//Push the results into
calcVals = results;
//Record the results
fout << thisModel.ax << " "
<< thisModel.rh << " "
<< thisModel.metal.x << " "
<< thisModel.metal.y << " "
<< thisModel.metal.z << " "
<< thisModel.angle_x << " "
<< thisModel.angle_y << " "
<< thisModel.angle_z << " " << endl;
return total;
}
void onIterate(VisualThread& visualThread,const PointModel& m) {
visualThread.fw.setCalcVals(calcVals,m.metal,m.angle_x,m.angle_y,m.angle_z);
}
int main() {
//Load the data
pairNucVals data = loadData("dataset_one.inp");
nuclei = data.first;
expVals = data.second;
calcVals.resize(expVals.size());
//Set up the model
PointModel m;
m.ax = 100.0;
m.rh = 0.0;
m.metal = Vector3((nuclei.xmin+nuclei.xmax)/2,
(nuclei.ymin+nuclei.ymax)/2,
(nuclei.zmin+nuclei.zmax)/2);
m.setEulerAngles(0,0,0);
//Start the visualisation thread
VisualThread visualThread;
visualThread.fw.setNuclei(nuclei);
visualThread.fw.setExpVals(expVals);
boost::thread boostVisualThread(boost::ref(visualThread));
//Initalise the minimizer
Minimiser<PointModel> minimiser(&unpackPointModel,
&packPointModel,
&minf,
bind(onIterate,boost::ref(visualThread),_1));
//Open the log file
fout.open("params.log");
if(!fout.is_open()) {
cerr << "Could not open params.log for writing" << endl;
return 1;
}
//Clear the results directory
system("rm results/*");
double best = numeric_limits<double>::infinity();
PointModel bestModel;
//Main loop
for(long i = 0;i<10;i++) {
for(long j = 0;j<10;j++) {
for(long k = 0;k<10;k++) {
double fx = i/10.0;
double fy = j/10.0;
double fz = k/10.0;
m.metal.x = nuclei.xmin*(1-fx) + nuclei.xmax*fx;
m.metal.y = nuclei.xmin*(1-fy) + nuclei.xmax*fy;
m.metal.z = nuclei.xmin*(1-fz) + nuclei.xmax*fz;
cout << "Starting: (" << m.metal.x << "," << m.metal.y << "," << m.metal.z << ")" << endl;
std::pair<double,PointModel> thePair = minimiser.minimise(m);
double found_min = thePair.first;
if(best > found_min) {
bestModel = thePair.second;
best = found_min;
cout << "New Best Model:" << endl;
cout << "ax = " << bestModel.ax
<< "rh = " << bestModel.rh
<< "x = " << bestModel.metal.x
<< "y = " << bestModel.metal.y
<< "z = " << bestModel.metal.z
<< "angle_x = " << bestModel.angle_x
<< "angle_y = " << bestModel.angle_y
<< "angle_z = " << bestModel.angle_z << endl << endl;
}
cout << "Finished, minimum = " << found_min << " best so far " << best <<endl;
}
}
}
return 0;
}