source: src/analysis_correlation.cpp@ 7326b2

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 7326b2 was 7ea9e6, checked in by Frederik Heber <heber@…>, 16 years ago

Periodic variants of AnalysisPair...() implemented.

  • BUGFIX: Vector::DistanceToPlane() - we did not check whether sign is 0.
  • NOTE: Due to changes in Vector::DistanceToPlane() with sign, Unit test AnalysisCorrelationToSurfaceUnitTest had to be changed:
    • we now have bin -0.5 filled with 1 (instead of 0.) and -0.288 instead of 0.288
    • find() replaced by lower_bound
  • new functions: PeriodicPairCorrelation(), PeriodicCorrelationToPoint(), PeriodicCorrelationToSurface()
    • each has a ranges[NDIM] argument with specifies the neighbours to scan: [ -ranges[i], ranges[i] ]
    • the atom::node is periodically translated to each periodic cell and the distance calculated.
  • NOTE: make check was broken before due to implementation of Periodic variants as normal ones (yielding more points that expected in unit test).

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