source: src/analysis_correlation.cpp@ 1fa107

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 1fa107 was 1614174, checked in by Frederik Heber <heber@…>, 15 years ago

LEAKFIX: ReturnFullMatrixForSymmetric() and InverseMatrix() have Malloc()s, in code was delete(), not Free()

  • valgrind showed the "mismatch free/delete" and is fixed. (mostly concerned functions in molecule_geometry.cpp and analysis_correlation.cpp)
  • LEAKFIX: logger and errorLogger instances were not free'd, this is done.

Signed-off-by: Frederik Heber <heber@…>

  • Property mode set to 100644
File size: 18.0 KB
Line 
1/*
2 * analysis.cpp
3 *
4 * Created on: Oct 13, 2009
5 * Author: heber
6 */
7
8#include <iostream>
9
10#include "analysis_correlation.hpp"
11#include "element.hpp"
12#include "log.hpp"
13#include "molecule.hpp"
14#include "tesselation.hpp"
15#include "tesselationhelpers.hpp"
16#include "vector.hpp"
17#include "verbose.hpp"
18
19
20/** Calculates the pair correlation between given elements.
21 * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si))
22 * \param *out output stream for debugging
23 * \param *molecules list of molecules structure
24 * \param *type1 first element or NULL (if any element)
25 * \param *type2 second element or NULL (if any element)
26 * \return Map of doubles with values the pair of the two atoms.
27 */
28PairCorrelationMap *PairCorrelation(MoleculeListClass * const &molecules, const element * const type1, const element * const type2 )
29{
30 PairCorrelationMap *outmap = NULL;
31 double distance = 0.;
32
33 if (molecules->ListOfMolecules.empty()) {
34 eLog() << Verbose(1) <<"No molecule given." << endl;
35 return outmap;
36 }
37 outmap = new PairCorrelationMap;
38 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
39 if ((*MolWalker)->ActiveFlag) {
40 eLog() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
41 atom *Walker = (*MolWalker)->start;
42 while (Walker->next != (*MolWalker)->end) {
43 Walker = Walker->next;
44 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
45 if ((type1 == NULL) || (Walker->type == type1)) {
46 for (MoleculeList::const_iterator MolOtherWalker = MolWalker; MolOtherWalker != molecules->ListOfMolecules.end(); MolOtherWalker++)
47 if ((*MolOtherWalker)->ActiveFlag) {
48 Log() << Verbose(2) << "Current other molecule is " << *MolOtherWalker << "." << endl;
49 atom *OtherWalker = (*MolOtherWalker)->start;
50 while (OtherWalker->next != (*MolOtherWalker)->end) { // only go up to Walker
51 OtherWalker = OtherWalker->next;
52 Log() << Verbose(3) << "Current otheratom is " << *OtherWalker << "." << endl;
53 if (Walker->nr < OtherWalker->nr)
54 if ((type2 == NULL) || (OtherWalker->type == type2)) {
55 distance = Walker->node->PeriodicDistance(OtherWalker->node, (*MolWalker)->cell_size);
56 //Log() << Verbose(1) <<"Inserting " << *Walker << " and " << *OtherWalker << endl;
57 outmap->insert ( pair<double, pair <atom *, atom*> > (distance, pair<atom *, atom*> (Walker, OtherWalker) ) );
58 }
59 }
60 }
61 }
62 }
63 }
64
65 return outmap;
66};
67
68/** Calculates the pair correlation between given elements.
69 * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si))
70 * \param *out output stream for debugging
71 * \param *molecules list of molecules structure
72 * \param *type1 first element or NULL (if any element)
73 * \param *type2 second element or NULL (if any element)
74 * \param ranges[NDIM] interval boundaries for the periodic images to scan also
75 * \return Map of doubles with values the pair of the two atoms.
76 */
77PairCorrelationMap *PeriodicPairCorrelation(MoleculeListClass * const &molecules, const element * const type1, const element * const type2, const int ranges[NDIM] )
78{
79 PairCorrelationMap *outmap = NULL;
80 double distance = 0.;
81 int n[NDIM];
82 Vector checkX;
83 Vector periodicX;
84 int Othern[NDIM];
85 Vector checkOtherX;
86 Vector periodicOtherX;
87
88 if (molecules->ListOfMolecules.empty()) {
89 eLog() << Verbose(1) <<"No molecule given." << endl;
90 return outmap;
91 }
92 outmap = new PairCorrelationMap;
93 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
94 if ((*MolWalker)->ActiveFlag) {
95 double * FullMatrix = ReturnFullMatrixforSymmetric((*MolWalker)->cell_size);
96 double * FullInverseMatrix = InverseMatrix(FullMatrix);
97 eLog() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
98 atom *Walker = (*MolWalker)->start;
99 while (Walker->next != (*MolWalker)->end) {
100 Walker = Walker->next;
101 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
102 if ((type1 == NULL) || (Walker->type == type1)) {
103 periodicX.CopyVector(Walker->node);
104 periodicX.MatrixMultiplication(FullInverseMatrix); // x now in [0,1)^3
105 // go through every range in xyz and get distance
106 for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
107 for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
108 for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
109 checkX.Init(n[0], n[1], n[2]);
110 checkX.AddVector(&periodicX);
111 checkX.MatrixMultiplication(FullMatrix);
112 for (MoleculeList::const_iterator MolOtherWalker = MolWalker; MolOtherWalker != molecules->ListOfMolecules.end(); MolOtherWalker++)
113 if ((*MolOtherWalker)->ActiveFlag) {
114 Log() << Verbose(2) << "Current other molecule is " << *MolOtherWalker << "." << endl;
115 atom *OtherWalker = (*MolOtherWalker)->start;
116 while (OtherWalker->next != (*MolOtherWalker)->end) { // only go up to Walker
117 OtherWalker = OtherWalker->next;
118 Log() << Verbose(3) << "Current otheratom is " << *OtherWalker << "." << endl;
119 if (Walker->nr < OtherWalker->nr)
120 if ((type2 == NULL) || (OtherWalker->type == type2)) {
121 periodicOtherX.CopyVector(OtherWalker->node);
122 periodicOtherX.MatrixMultiplication(FullInverseMatrix); // x now in [0,1)^3
123 // go through every range in xyz and get distance
124 for (Othern[0]=-ranges[0]; Othern[0] <= ranges[0]; Othern[0]++)
125 for (Othern[1]=-ranges[1]; Othern[1] <= ranges[1]; Othern[1]++)
126 for (Othern[2]=-ranges[2]; Othern[2] <= ranges[2]; Othern[2]++) {
127 checkOtherX.Init(Othern[0], Othern[1], Othern[2]);
128 checkOtherX.AddVector(&periodicOtherX);
129 checkOtherX.MatrixMultiplication(FullMatrix);
130 distance = checkX.Distance(&checkOtherX);
131 //Log() << Verbose(1) <<"Inserting " << *Walker << " and " << *OtherWalker << endl;
132 outmap->insert ( pair<double, pair <atom *, atom*> > (distance, pair<atom *, atom*> (Walker, OtherWalker) ) );
133 }
134 }
135 }
136 }
137 }
138 }
139 }
140 Free(&FullMatrix);
141 Free(&FullInverseMatrix);
142 }
143
144 return outmap;
145};
146
147/** Calculates the distance (pair) correlation between a given element and a point.
148 * \param *out output stream for debugging
149 * \param *molecules list of molecules structure
150 * \param *type element or NULL (if any element)
151 * \param *point vector to the correlation point
152 * \return Map of dobules with values as pairs of atom and the vector
153 */
154CorrelationToPointMap *CorrelationToPoint(MoleculeListClass * const &molecules, const element * const type, const Vector *point )
155{
156 CorrelationToPointMap *outmap = NULL;
157 double distance = 0.;
158
159 if (molecules->ListOfMolecules.empty()) {
160 Log() << Verbose(1) <<"No molecule given." << endl;
161 return outmap;
162 }
163 outmap = new CorrelationToPointMap;
164 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
165 if ((*MolWalker)->ActiveFlag) {
166 Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
167 atom *Walker = (*MolWalker)->start;
168 while (Walker->next != (*MolWalker)->end) {
169 Walker = Walker->next;
170 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
171 if ((type == NULL) || (Walker->type == type)) {
172 distance = Walker->node->PeriodicDistance(point, (*MolWalker)->cell_size);
173 Log() << Verbose(4) << "Current distance is " << distance << "." << endl;
174 outmap->insert ( pair<double, pair<atom *, const Vector*> >(distance, pair<atom *, const Vector*> (Walker, point) ) );
175 }
176 }
177 }
178
179 return outmap;
180};
181
182/** Calculates the distance (pair) correlation between a given element, all its periodic images and a point.
183 * \param *out output stream for debugging
184 * \param *molecules list of molecules structure
185 * \param *type element or NULL (if any element)
186 * \param *point vector to the correlation point
187 * \param ranges[NDIM] interval boundaries for the periodic images to scan also
188 * \return Map of dobules with values as pairs of atom and the vector
189 */
190CorrelationToPointMap *PeriodicCorrelationToPoint(MoleculeListClass * const &molecules, const element * const type, const Vector *point, const int ranges[NDIM] )
191{
192 CorrelationToPointMap *outmap = NULL;
193 double distance = 0.;
194 int n[NDIM];
195 Vector periodicX;
196 Vector checkX;
197
198 if (molecules->ListOfMolecules.empty()) {
199 Log() << Verbose(1) <<"No molecule given." << endl;
200 return outmap;
201 }
202 outmap = new CorrelationToPointMap;
203 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
204 if ((*MolWalker)->ActiveFlag) {
205 double * FullMatrix = ReturnFullMatrixforSymmetric((*MolWalker)->cell_size);
206 double * FullInverseMatrix = InverseMatrix(FullMatrix);
207 Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
208 atom *Walker = (*MolWalker)->start;
209 while (Walker->next != (*MolWalker)->end) {
210 Walker = Walker->next;
211 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
212 if ((type == NULL) || (Walker->type == type)) {
213 periodicX.CopyVector(Walker->node);
214 periodicX.MatrixMultiplication(FullInverseMatrix); // x now in [0,1)^3
215 // go through every range in xyz and get distance
216 for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
217 for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
218 for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
219 checkX.Init(n[0], n[1], n[2]);
220 checkX.AddVector(&periodicX);
221 checkX.MatrixMultiplication(FullMatrix);
222 distance = checkX.Distance(point);
223 Log() << Verbose(4) << "Current distance is " << distance << "." << endl;
224 outmap->insert ( pair<double, pair<atom *, const Vector*> >(distance, pair<atom *, const Vector*> (Walker, point) ) );
225 }
226 }
227 }
228 Free(&FullMatrix);
229 Free(&FullInverseMatrix);
230 }
231
232 return outmap;
233};
234
235/** Calculates the distance (pair) correlation between a given element and a surface.
236 * \param *out output stream for debugging
237 * \param *molecules list of molecules structure
238 * \param *type element or NULL (if any element)
239 * \param *Surface pointer to Tesselation class surface
240 * \param *LC LinkedCell structure to quickly find neighbouring atoms
241 * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest
242 */
243CorrelationToSurfaceMap *CorrelationToSurface(MoleculeListClass * const &molecules, const element * const type, const Tesselation * const Surface, const LinkedCell *LC )
244{
245 CorrelationToSurfaceMap *outmap = NULL;
246 double distance = 0;
247 class BoundaryTriangleSet *triangle = NULL;
248 Vector centroid;
249
250 if ((Surface == NULL) || (LC == NULL) || (molecules->ListOfMolecules.empty())) {
251 Log() << Verbose(1) <<"No Tesselation, no LinkedCell or no molecule given." << endl;
252 return outmap;
253 }
254 outmap = new CorrelationToSurfaceMap;
255 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
256 if ((*MolWalker)->ActiveFlag) {
257 Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
258 atom *Walker = (*MolWalker)->start;
259 while (Walker->next != (*MolWalker)->end) {
260 Walker = Walker->next;
261 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
262 if ((type == NULL) || (Walker->type == type)) {
263 triangle = Surface->FindClosestTriangleToPoint(Walker->node, LC );
264 if (triangle != NULL) {
265 distance = DistanceToTrianglePlane(Walker->node, triangle);
266 outmap->insert ( pair<double, pair<atom *, BoundaryTriangleSet*> >(distance, pair<atom *, BoundaryTriangleSet*> (Walker, triangle) ) );
267 }
268 }
269 }
270 }
271
272 return outmap;
273};
274
275/** Calculates the distance (pair) correlation between a given element, all its periodic images and and a surface.
276 * Note that we also put all periodic images found in the cells given by [ -ranges[i], ranges[i] ] and i=0,...,NDIM-1.
277 * I.e. We multiply the atom::node with the inverse of the domain matrix, i.e. transform it to \f$[0,0^3\f$, then add per
278 * axis an integer from [ -ranges[i], ranges[i] ] onto it and multiply with the domain matrix to bring it back into
279 * the real space. Then, we Tesselation::FindClosestTriangleToPoint() and DistanceToTrianglePlane().
280 * \param *out output stream for debugging
281 * \param *molecules list of molecules structure
282 * \param *type element or NULL (if any element)
283 * \param *Surface pointer to Tesselation class surface
284 * \param *LC LinkedCell structure to quickly find neighbouring atoms
285 * \param ranges[NDIM] interval boundaries for the periodic images to scan also
286 * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest
287 */
288CorrelationToSurfaceMap *PeriodicCorrelationToSurface(MoleculeListClass * const &molecules, const element * const type, const Tesselation * const Surface, const LinkedCell *LC, const int ranges[NDIM] )
289{
290 CorrelationToSurfaceMap *outmap = NULL;
291 double distance = 0;
292 class BoundaryTriangleSet *triangle = NULL;
293 Vector centroid;
294 int n[NDIM];
295 Vector periodicX;
296 Vector checkX;
297
298 if ((Surface == NULL) || (LC == NULL) || (molecules->ListOfMolecules.empty())) {
299 Log() << Verbose(1) <<"No Tesselation, no LinkedCell or no molecule given." << endl;
300 return outmap;
301 }
302 outmap = new CorrelationToSurfaceMap;
303 for (MoleculeList::const_iterator MolWalker = molecules->ListOfMolecules.begin(); MolWalker != molecules->ListOfMolecules.end(); MolWalker++)
304 if ((*MolWalker)->ActiveFlag) {
305 double * FullMatrix = ReturnFullMatrixforSymmetric((*MolWalker)->cell_size);
306 double * FullInverseMatrix = InverseMatrix(FullMatrix);
307 Log() << Verbose(2) << "Current molecule is " << *MolWalker << "." << endl;
308 atom *Walker = (*MolWalker)->start;
309 while (Walker->next != (*MolWalker)->end) {
310 Walker = Walker->next;
311 Log() << Verbose(3) << "Current atom is " << *Walker << "." << endl;
312 if ((type == NULL) || (Walker->type == type)) {
313 periodicX.CopyVector(Walker->node);
314 periodicX.MatrixMultiplication(FullInverseMatrix); // x now in [0,1)^3
315 // go through every range in xyz and get distance
316 for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
317 for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
318 for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
319 checkX.Init(n[0], n[1], n[2]);
320 checkX.AddVector(&periodicX);
321 checkX.MatrixMultiplication(FullMatrix);
322 triangle = Surface->FindClosestTriangleToPoint(&checkX, LC );
323 if (triangle != NULL) {
324 distance = DistanceToTrianglePlane(&checkX, triangle);
325 outmap->insert ( pair<double, pair<atom *, BoundaryTriangleSet*> >(distance, pair<atom *, BoundaryTriangleSet*> (Walker, triangle) ) );
326 }
327 }
328 }
329 }
330 Free(&FullMatrix);
331 Free(&FullInverseMatrix);
332 }
333
334 return outmap;
335};
336
337/** Returns the start of the bin for a given value.
338 * \param value value whose bin to look for
339 * \param BinWidth width of bin
340 * \param BinStart first bin
341 */
342double GetBin ( const double value, const double BinWidth, const double BinStart )
343{
344 double bin =(double) (floor((value - BinStart)/BinWidth));
345 return (bin*BinWidth+BinStart);
346};
347
348
349/** Prints correlation (double, int) pairs to file.
350 * \param *file file to write to
351 * \param *map map to write
352 */
353void OutputCorrelation( ofstream * const file, const BinPairMap * const map )
354{
355 *file << "# BinStart\tCount" << endl;
356 for (BinPairMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
357 *file << runner->first << "\t" << runner->second << endl;
358 }
359};
360
361/** Prints correlation (double, (atom*,atom*) ) pairs to file.
362 * \param *file file to write to
363 * \param *map map to write
364 */
365void OutputPairCorrelation( ofstream * const file, const PairCorrelationMap * const map )
366{
367 *file << "# BinStart\tAtom1\tAtom2" << endl;
368 for (PairCorrelationMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
369 *file << runner->first << "\t" << *(runner->second.first) << "\t" << *(runner->second.second) << endl;
370 }
371};
372
373/** Prints correlation (double, int) pairs to file.
374 * \param *file file to write to
375 * \param *map map to write
376 */
377void OutputCorrelationToPoint( ofstream * const file, const CorrelationToPointMap * const map )
378{
379 *file << "# BinStart\tAtom::x[i]-point.x[i]" << endl;
380 for (CorrelationToPointMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
381 *file << runner->first;
382 for (int i=0;i<NDIM;i++)
383 *file << "\t" << (runner->second.first->node->x[i] - runner->second.second->x[i]);
384 *file << endl;
385 }
386};
387
388/** Prints correlation (double, int) pairs to file.
389 * \param *file file to write to
390 * \param *map map to write
391 */
392void OutputCorrelationToSurface( ofstream * const file, const CorrelationToSurfaceMap * const map )
393{
394 *file << "# BinStart\tTriangle" << endl;
395 for (CorrelationToSurfaceMap::const_iterator runner = map->begin(); runner != map->end(); ++runner) {
396 *file << runner->first << "\t" << *(runner->second.second) << endl;
397 }
398};
399
Note: See TracBrowser for help on using the repository browser.