source: src/ellipsoid.cpp@ 70ff32

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 70ff32 was f66195, checked in by Frederik Heber <heber@…>, 15 years ago

forward declarations used to untangle interdependet classes.

  • basically, everywhere in header files we removed '#include' lines were only pointer to the respective classes were used and the include line was moved to the implementation file.
  • as a sidenote, lots of funny errors happened because headers were included via a nesting over three other includes. Now, all should be declared directly as needed, as only very little include lines remain in header files.
  • Property mode set to 100644
File size: 16.5 KB
Line 
1/*
2 * ellipsoid.cpp
3 *
4 * Created on: Jan 20, 2009
5 * Author: heber
6 */
7
8#include <gsl/gsl_multimin.h>
9#include <gsl/gsl_vector.h>
10
11#include <iomanip>
12
13#include <set>
14
15#include "boundary.hpp"
16#include "ellipsoid.hpp"
17#include "linkedcell.hpp"
18#include "tesselation.hpp"
19#include "vector.hpp"
20#include "verbose.hpp"
21
22/** Determines squared distance for a given point \a x to surface of ellipsoid.
23 * \param x given point
24 * \param EllipsoidCenter center of ellipsoid
25 * \param EllipsoidLength[3] three lengths of half axis of ellipsoid
26 * \param EllipsoidAngle[3] three rotation angles of ellipsoid
27 * \return squared distance from point to surface
28 */
29double SquaredDistanceToEllipsoid(Vector &x, Vector &EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
30{
31 Vector helper, RefPoint;
32 double distance = -1.;
33 double Matrix[NDIM*NDIM];
34 double InverseLength[3];
35 double psi,theta,phi; // euler angles in ZX'Z'' convention
36
37 //cout << Verbose(3) << "Begin of SquaredDistanceToEllipsoid" << endl;
38
39 for(int i=0;i<3;i++)
40 InverseLength[i] = 1./EllipsoidLength[i];
41
42 // 1. translate coordinate system so that ellipsoid center is in origin
43 helper.CopyVector(&x);
44 helper.SubtractVector(&EllipsoidCenter);
45 RefPoint.CopyVector(&helper);
46 //cout << Verbose(4) << "Translated given point is at " << RefPoint << "." << endl;
47
48 // 2. transform coordinate system by inverse of rotation matrix and of diagonal matrix
49 psi = EllipsoidAngle[0];
50 theta = EllipsoidAngle[1];
51 phi = EllipsoidAngle[2];
52 Matrix[0] = cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi);
53 Matrix[1] = -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi);
54 Matrix[2] = sin(psi)*sin(theta);
55 Matrix[3] = sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi);
56 Matrix[4] = cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi);
57 Matrix[5] = -cos(psi)*sin(theta);
58 Matrix[6] = sin(theta)*sin(phi);
59 Matrix[7] = sin(theta)*cos(phi);
60 Matrix[8] = cos(theta);
61 helper.MatrixMultiplication(Matrix);
62 helper.Scale(InverseLength);
63 //cout << Verbose(4) << "Transformed RefPoint is at " << helper << "." << endl;
64
65 // 3. construct intersection point with unit sphere and ray between origin and x
66 helper.Normalize(); // is simply normalizes vector in distance direction
67 //cout << Verbose(4) << "Transformed intersection is at " << helper << "." << endl;
68
69 // 4. transform back the constructed intersection point
70 psi = -EllipsoidAngle[0];
71 theta = -EllipsoidAngle[1];
72 phi = -EllipsoidAngle[2];
73 helper.Scale(EllipsoidLength);
74 Matrix[0] = cos(psi)*cos(phi) - sin(psi)*cos(theta)*sin(phi);
75 Matrix[1] = -cos(psi)*sin(phi) - sin(psi)*cos(theta)*cos(phi);
76 Matrix[2] = sin(psi)*sin(theta);
77 Matrix[3] = sin(psi)*cos(phi) + cos(psi)*cos(theta)*sin(phi);
78 Matrix[4] = cos(psi)*cos(theta)*cos(phi) - sin(psi)*sin(phi);
79 Matrix[5] = -cos(psi)*sin(theta);
80 Matrix[6] = sin(theta)*sin(phi);
81 Matrix[7] = sin(theta)*cos(phi);
82 Matrix[8] = cos(theta);
83 helper.MatrixMultiplication(Matrix);
84 //cout << Verbose(4) << "Intersection is at " << helper << "." << endl;
85
86 // 5. determine distance between backtransformed point and x
87 distance = RefPoint.DistanceSquared(&helper);
88 //cout << Verbose(4) << "Squared distance between intersection and RefPoint is " << distance << "." << endl;
89
90 return distance;
91 //cout << Verbose(3) << "End of SquaredDistanceToEllipsoid" << endl;
92};
93
94/** structure for ellipsoid minimisation containing points to fit to.
95 */
96struct EllipsoidMinimisation {
97 int N; //!< dimension of vector set
98 Vector *x; //!< array of vectors
99};
100
101/** Sum of squared distance to ellipsoid to be minimised.
102 * \param *x parameters for the ellipsoid
103 * \param *params EllipsoidMinimisation with set of data points to minimise distance to and dimension
104 * \return sum of squared distance, \sa SquaredDistanceToEllipsoid()
105 */
106double SumSquaredDistance (const gsl_vector * x, void * params)
107{
108 Vector *set= ((struct EllipsoidMinimisation *)params)->x;
109 int N = ((struct EllipsoidMinimisation *)params)->N;
110 double SumDistance = 0.;
111 double distance;
112 Vector Center;
113 double EllipsoidLength[3], EllipsoidAngle[3];
114
115 // put parameters into suitable ellipsoid form
116 for (int i=0;i<3;i++) {
117 Center.x[i] = gsl_vector_get(x, i+0);
118 EllipsoidLength[i] = gsl_vector_get(x, i+3);
119 EllipsoidAngle[i] = gsl_vector_get(x, i+6);
120 }
121
122 // go through all points and sum distance
123 for (int i=0;i<N;i++) {
124 distance = SquaredDistanceToEllipsoid(set[i], Center, EllipsoidLength, EllipsoidAngle);
125 if (!isnan(distance)) {
126 SumDistance += distance;
127 } else {
128 SumDistance = GSL_NAN;
129 break;
130 }
131 }
132
133 //cout << "Current summed distance is " << SumDistance << "." << endl;
134 return SumDistance;
135};
136
137/** Finds best fitting ellipsoid parameter set in Least square sense for a given point set.
138 * \param *out output stream for debugging
139 * \param *set given point set
140 * \param N number of points in set
141 * \param EllipsoidParamter[3] three parameters in ellipsoid equation
142 * \return true - fit successful, false - fit impossible
143 */
144bool FitPointSetToEllipsoid(ofstream *out, Vector *set, int N, Vector *EllipsoidCenter, double *EllipsoidLength, double *EllipsoidAngle)
145{
146 int status = GSL_SUCCESS;
147 *out << Verbose(2) << "Begin of FitPointSetToEllipsoid " << endl;
148 if (N >= 3) { // check that enough points are given (9 d.o.f.)
149 struct EllipsoidMinimisation par;
150 const gsl_multimin_fminimizer_type *T = gsl_multimin_fminimizer_nmsimplex;
151 gsl_multimin_fminimizer *s = NULL;
152 gsl_vector *ss, *x;
153 gsl_multimin_function minex_func;
154
155 size_t iter = 0;
156 double size;
157
158 /* Starting point */
159 x = gsl_vector_alloc (9);
160 for (int i=0;i<3;i++) {
161 gsl_vector_set (x, i+0, EllipsoidCenter->x[i]);
162 gsl_vector_set (x, i+3, EllipsoidLength[i]);
163 gsl_vector_set (x, i+6, EllipsoidAngle[i]);
164 }
165 par.x = set;
166 par.N = N;
167
168 /* Set initial step sizes */
169 ss = gsl_vector_alloc (9);
170 for (int i=0;i<3;i++) {
171 gsl_vector_set (ss, i+0, 0.1);
172 gsl_vector_set (ss, i+3, 1.0);
173 gsl_vector_set (ss, i+6, M_PI/20.);
174 }
175
176 /* Initialize method and iterate */
177 minex_func.n = 9;
178 minex_func.f = &SumSquaredDistance;
179 minex_func.params = (void *)&par;
180
181 s = gsl_multimin_fminimizer_alloc (T, 9);
182 gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
183
184 do {
185 iter++;
186 status = gsl_multimin_fminimizer_iterate(s);
187
188 if (status)
189 break;
190
191 size = gsl_multimin_fminimizer_size (s);
192 status = gsl_multimin_test_size (size, 1e-2);
193
194 if (status == GSL_SUCCESS) {
195 for (int i=0;i<3;i++) {
196 EllipsoidCenter->x[i] = gsl_vector_get (s->x,i+0);
197 EllipsoidLength[i] = gsl_vector_get (s->x, i+3);
198 EllipsoidAngle[i] = gsl_vector_get (s->x, i+6);
199 }
200 *out << setprecision(3) << Verbose(4) << "Converged fit at: " << *EllipsoidCenter << ", lengths " << EllipsoidLength[0] << ", " << EllipsoidLength[1] << ", " << EllipsoidLength[2] << ", angles " << EllipsoidAngle[0] << ", " << EllipsoidAngle[1] << ", " << EllipsoidAngle[2] << " with summed distance " << s->fval << "." << endl;
201 }
202
203 } while (status == GSL_CONTINUE && iter < 1000);
204
205 gsl_vector_free(x);
206 gsl_vector_free(ss);
207 gsl_multimin_fminimizer_free (s);
208
209 } else {
210 *out << Verbose(3) << "Not enough points provided for fit to ellipsoid." << endl;
211 return false;
212 }
213 *out << Verbose(2) << "End of FitPointSetToEllipsoid" << endl;
214 if (status == GSL_SUCCESS)
215 return true;
216 else
217 return false;
218};
219
220/** Picks a number of random points from a LC neighbourhood as a fitting set.
221 * \param *out output stream for debugging
222 * \param *T Tesselation containing boundary points
223 * \param *LC linked cell list of all atoms
224 * \param *&x random point set on return (not allocated!)
225 * \param PointsToPick number of points in set to pick
226 */
227void PickRandomNeighbouredPointSet(ofstream *out, class Tesselation *T, class LinkedCell *LC, Vector *&x, size_t PointsToPick)
228{
229 size_t PointsLeft = 0;
230 size_t PointsPicked = 0;
231 int Nlower[NDIM], Nupper[NDIM];
232 set<int> PickedAtomNrs; // ordered list of picked atoms
233 set<int>::iterator current;
234 int index;
235 TesselPoint *Candidate = NULL;
236 LinkedNodes *List = NULL;
237 *out << Verbose(2) << "Begin of PickRandomPointSet" << endl;
238
239 // allocate array
240 if (x == NULL) {
241 x = new Vector[PointsToPick];
242 } else {
243 *out << "WARNING: Given pointer to vector array seems already allocated." << endl;
244 }
245
246 do {
247 for(int i=0;i<NDIM;i++) // pick three random indices
248 LC->n[i] = (rand() % LC->N[i]);
249 *out << Verbose(2) << "INFO: Center cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " ... ";
250 // get random cell
251 List = LC->GetCurrentCell();
252 if (List == NULL) { // set index to it
253 continue;
254 }
255 *out << "with No. " << LC->index << "." << endl;
256
257 *out << Verbose(2) << "LC Intervals:";
258 for (int i=0;i<NDIM;i++) {
259 Nlower[i] = ((LC->n[i]-1) >= 0) ? LC->n[i]-1 : 0;
260 Nupper[i] = ((LC->n[i]+1) < LC->N[i]) ? LC->n[i]+1 : LC->N[i]-1;
261 *out << " [" << Nlower[i] << "," << Nupper[i] << "] ";
262 }
263 *out << endl;
264
265 // count whether there are sufficient atoms in this cell+neighbors
266 PointsLeft=0;
267 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
268 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
269 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
270 List = LC->GetCurrentCell();
271 PointsLeft += List->size();
272 }
273 *out << Verbose(2) << "There are " << PointsLeft << " atoms in this neighbourhood." << endl;
274 if (PointsLeft < PointsToPick) { // ensure that we can pick enough points in its neighbourhood at all.
275 continue;
276 }
277
278 // pre-pick a fixed number of atoms
279 PickedAtomNrs.clear();
280 do {
281 index = (rand() % PointsLeft);
282 current = PickedAtomNrs.find(index); // not present?
283 if (current == PickedAtomNrs.end()) {
284 //*out << Verbose(2) << "Picking atom nr. " << index << "." << endl;
285 PickedAtomNrs.insert(index);
286 }
287 } while (PickedAtomNrs.size() < PointsToPick);
288
289 index = 0; // now go through all and pick those whose from PickedAtomsNr
290 PointsPicked=0;
291 current = PickedAtomNrs.begin();
292 for (LC->n[0] = Nlower[0]; LC->n[0] <= Nupper[0]; LC->n[0]++)
293 for (LC->n[1] = Nlower[1]; LC->n[1] <= Nupper[1]; LC->n[1]++)
294 for (LC->n[2] = Nlower[2]; LC->n[2] <= Nupper[2]; LC->n[2]++) {
295 List = LC->GetCurrentCell();
296// *out << Verbose(2) << "Current cell is " << LC->n[0] << ", " << LC->n[1] << ", " << LC->n[2] << " with No. " << LC->index << " containing " << List->size() << " points." << endl;
297 if (List != NULL) {
298// if (List->begin() != List->end())
299// *out << Verbose(2) << "Going through candidates ... " << endl;
300// else
301// *out << Verbose(2) << "Cell is empty ... " << endl;
302 for (LinkedNodes::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
303 if ((current != PickedAtomNrs.end()) && (*current == index)) {
304 Candidate = (*Runner);
305 *out << Verbose(2) << "Current picked node is " << **Runner << " with index " << index << "." << endl;
306 x[PointsPicked++].CopyVector(Candidate->node); // we have one more atom picked
307 current++; // next pre-picked atom
308 }
309 index++; // next atom nr.
310 }
311// } else {
312// *out << Verbose(2) << "List for this index not allocated!" << endl;
313 }
314 }
315 *out << Verbose(2) << "The following points were picked: " << endl;
316 for (size_t i=0;i<PointsPicked;i++)
317 *out << Verbose(2) << x[i] << endl;
318 if (PointsPicked == PointsToPick) // break out of loop if we have all
319 break;
320 } while(1);
321
322 *out << Verbose(2) << "End of PickRandomPointSet" << endl;
323};
324
325/** Picks a number of random points from a set of boundary points as a fitting set.
326 * \param *out output stream for debugging
327 * \param *T Tesselation containing boundary points
328 * \param *&x random point set on return (not allocated!)
329 * \param PointsToPick number of points in set to pick
330 */
331void PickRandomPointSet(ofstream *out, class Tesselation *T, Vector *&x, size_t PointsToPick)
332{
333 size_t PointsLeft = (size_t) T->PointsOnBoundaryCount;
334 size_t PointsPicked = 0;
335 double value, threshold;
336 PointMap *List = &T->PointsOnBoundary;
337 *out << Verbose(2) << "Begin of PickRandomPointSet" << endl;
338
339 // allocate array
340 if (x == NULL) {
341 x = new Vector[PointsToPick];
342 } else {
343 *out << "WARNING: Given pointer to vector array seems already allocated." << endl;
344 }
345
346 if (List != NULL)
347 for (PointMap::iterator Runner = List->begin(); Runner != List->end(); Runner++) {
348 threshold = 1. - (double)(PointsToPick - PointsPicked)/(double)PointsLeft;
349 value = (double)rand()/(double)RAND_MAX;
350 //*out << Verbose(3) << "Current node is " << *Runner->second->node << " with " << value << " ... " << threshold << ": ";
351 if (value > threshold) {
352 x[PointsPicked].CopyVector(Runner->second->node->node);
353 PointsPicked++;
354 //*out << "IN." << endl;
355 } else {
356 //*out << "OUT." << endl;
357 }
358 PointsLeft--;
359 }
360 *out << Verbose(2) << "The following points were picked: " << endl;
361 for (size_t i=0;i<PointsPicked;i++)
362 *out << Verbose(3) << x[i] << endl;
363
364 *out << Verbose(2) << "End of PickRandomPointSet" << endl;
365};
366
367/** Finds best fitting ellipsoid parameter set in least square sense for a given point set.
368 * \param *out output stream for debugging
369 * \param *T Tesselation containing boundary points
370 * \param *LCList linked cell list of all atoms
371 * \param N number of unique points in ellipsoid fit, must be greater equal 6
372 * \param number of fits (i.e. parameter sets in output file)
373 * \param *filename name for output file
374 */
375void FindDistributionOfEllipsoids(ofstream *out, class Tesselation *T, class LinkedCell *LCList, int N, int number, const char *filename)
376{
377 ofstream output;
378 Vector *x = NULL;
379 Vector Center;
380 Vector EllipsoidCenter;
381 double EllipsoidLength[3];
382 double EllipsoidAngle[3];
383 double distance, MaxDistance, MinDistance;
384 *out << Verbose(0) << "Begin of FindDistributionOfEllipsoids" << endl;
385
386 // construct center of gravity of boundary point set for initial ellipsoid center
387 Center.Zero();
388 for (PointMap::iterator Runner = T->PointsOnBoundary.begin(); Runner != T->PointsOnBoundary.end(); Runner++)
389 Center.AddVector(Runner->second->node->node);
390 Center.Scale(1./T->PointsOnBoundaryCount);
391 *out << Verbose(1) << "Center is at " << Center << "." << endl;
392
393 // Output header
394 output.open(filename, ios::trunc);
395 output << "# Nr.\tCenterX\tCenterY\tCenterZ\ta\tb\tc\tpsi\ttheta\tphi" << endl;
396
397 // loop over desired number of parameter sets
398 for (;number >0;number--) {
399 *out << Verbose(1) << "Determining data set " << number << " ... " << endl;
400 // pick the point set
401 x = NULL;
402 //PickRandomPointSet(out, T, LCList, x, N);
403 PickRandomNeighbouredPointSet(out, T, LCList, x, N);
404
405 // calculate some sensible starting values for parameter fit
406 MaxDistance = 0.;
407 MinDistance = x[0].ScalarProduct(&x[0]);
408 for (int i=0;i<N;i++) {
409 distance = x[i].ScalarProduct(&x[i]);
410 if (distance > MaxDistance)
411 MaxDistance = distance;
412 if (distance < MinDistance)
413 MinDistance = distance;
414 }
415 //*out << Verbose(2) << "MinDistance " << MinDistance << ", MaxDistance " << MaxDistance << "." << endl;
416 EllipsoidCenter.CopyVector(&Center); // use Center of Gravity as initial center of ellipsoid
417 for (int i=0;i<3;i++)
418 EllipsoidAngle[i] = 0.;
419 EllipsoidLength[0] = sqrt(MaxDistance);
420 EllipsoidLength[1] = sqrt((MaxDistance+MinDistance)/2.);
421 EllipsoidLength[2] = sqrt(MinDistance);
422
423 // fit the parameters
424 if (FitPointSetToEllipsoid(out, x, N, &EllipsoidCenter, &EllipsoidLength[0], &EllipsoidAngle[0])) {
425 *out << Verbose(1) << "Picking succeeded!" << endl;
426 // output obtained parameter set
427 output << number << "\t";
428 for (int i=0;i<3;i++)
429 output << setprecision(9) << EllipsoidCenter.x[i] << "\t";
430 for (int i=0;i<3;i++)
431 output << setprecision(9) << EllipsoidLength[i] << "\t";
432 for (int i=0;i<3;i++)
433 output << setprecision(9) << EllipsoidAngle[i] << "\t";
434 output << endl;
435 } else { // increase N to pick one more
436 *out << Verbose(1) << "Picking failed!" << endl;
437 number++;
438 }
439 delete[](x); // free allocated memory for point set
440 }
441 // close output and finish
442 output.close();
443
444 *out << Verbose(0) << "End of FindDistributionOfEllipsoids" << endl;
445};
Note: See TracBrowser for help on using the repository browser.