source: src/Analysis/analysis_correlation.cpp@ a58c16

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 a58c16 was a58c16, checked in by Frederik Heber <heber@…>, 10 years ago

Replaced World::getAllAtoms() by const version where possible.

  • Property mode set to 100644
File size: 29.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
22 */
23
24/*
25 * analysis.cpp
26 *
27 * Created on: Oct 13, 2009
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include "CodePatterns/MemDebug.hpp"
37
38#include <algorithm>
39#include <iostream>
40#include <iomanip>
41#include <limits>
42
43#include "Atom/atom.hpp"
44#include "Bond/bond.hpp"
45#include "Tesselation/BoundaryTriangleSet.hpp"
46#include "Box.hpp"
47#include "Element/element.hpp"
48#include "CodePatterns/Info.hpp"
49#include "CodePatterns/Log.hpp"
50#include "CodePatterns/Verbose.hpp"
51#include "Descriptors/AtomOfMoleculeSelectionDescriptor.hpp"
52#include "Descriptors/MoleculeFormulaDescriptor.hpp"
53#include "Descriptors/MoleculeOfAtomSelectionDescriptor.hpp"
54#include "Formula.hpp"
55#include "LinearAlgebra/Vector.hpp"
56#include "LinearAlgebra/RealSpaceMatrix.hpp"
57#include "LinkedCell/LinkedCell_View.hpp"
58#include "molecule.hpp"
59#include "Tesselation/tesselation.hpp"
60#include "Tesselation/tesselationhelpers.hpp"
61#include "Tesselation/triangleintersectionlist.hpp"
62#include "World.hpp"
63#include "WorldTime.hpp"
64
65#include "analysis_correlation.hpp"
66
67/** Calculates the dipole vector of a given atomSet.
68 *
69 * Note that we use the following procedure as rule of thumb:
70 * -# go through every bond of the atom
71 * -# calculate the difference of electronegativities \f$\Delta\mathrm{EN}\f$
72 * -# if \f$\Delta\mathrm{EN} > 0.5\f$, we align the bond vector in direction of the more negative element
73 * -# sum up all vectors
74 * -# finally, divide by the number of summed vectors
75 *
76 * @param atomsbegin begin iterator of atomSet
77 * @param atomsend end iterator of atomset
78 * @return dipole vector
79 */
80Vector getDipole(molecule::const_iterator atomsbegin, molecule::const_iterator atomsend)
81{
82 Vector DipoleVector;
83 size_t SumOfVectors = 0;
84 Box &domain = World::getInstance().getDomain();
85
86 // go through all atoms
87 for (molecule::const_iterator atomiter = atomsbegin;
88 atomiter != atomsend;
89 ++atomiter) {
90 // go through all bonds
91 const BondList& ListOfBonds = (*atomiter)->getListOfBonds();
92 ASSERT(ListOfBonds.begin() != ListOfBonds.end(),
93 "getDipole() - no bonds in molecule!");
94 for (BondList::const_iterator bonditer = ListOfBonds.begin();
95 bonditer != ListOfBonds.end();
96 ++bonditer) {
97 const atom * Otheratom = (*bonditer)->GetOtherAtom(*atomiter);
98 if (Otheratom->getId() > (*atomiter)->getId()) {
99 const double DeltaEN = (*atomiter)->getType()->getElectronegativity()
100 -Otheratom->getType()->getElectronegativity();
101 // get distance and correct for boundary conditions
102 Vector BondDipoleVector = domain.periodicDistanceVector(
103 (*atomiter)->getPosition(),
104 Otheratom->getPosition());
105 // DeltaEN is always positive, gives correct orientation of vector
106 BondDipoleVector.Normalize();
107 BondDipoleVector *= DeltaEN;
108 LOG(3,"INFO: Dipole vector from bond " << **bonditer << " is " << BondDipoleVector);
109 DipoleVector += BondDipoleVector;
110 SumOfVectors++;
111 }
112 }
113 }
114 LOG(3,"INFO: Sum over all bond dipole vectors is "
115 << DipoleVector << " with " << SumOfVectors << " in total.");
116 if (SumOfVectors != 0)
117 DipoleVector *= 1./(double)SumOfVectors;
118 LOG(2, "INFO: Resulting dipole vector is " << DipoleVector);
119
120 return DipoleVector;
121};
122
123/** Calculate minimum and maximum amount of trajectory steps by going through given atomic trajectories.
124 * \param vector of atoms whose trajectories to check for [min,max]
125 * \return range with [min, max]
126 */
127range<size_t> getMaximumTrajectoryBounds(const std::vector<atom *> &atoms)
128{
129 // get highest trajectory size
130 LOG(0,"STATUS: Retrieving maximum amount of time steps ...");
131 if (atoms.size() == 0)
132 return range<size_t>(0,0);
133 size_t max_timesteps = std::numeric_limits<size_t>::min();
134 size_t min_timesteps = std::numeric_limits<size_t>::max();
135 BOOST_FOREACH(atom *_atom, atoms) {
136 if (_atom->getTrajectorySize() > max_timesteps)
137 max_timesteps = _atom->getTrajectorySize();
138 if (_atom->getTrajectorySize() < min_timesteps)
139 min_timesteps = _atom->getTrajectorySize();
140 }
141 LOG(1,"INFO: Minimum number of time steps found is " << min_timesteps);
142 LOG(1,"INFO: Maximum number of time steps found is " << max_timesteps);
143
144 return range<size_t>(min_timesteps, max_timesteps);
145}
146
147/** Calculates the angular dipole zero orientation from current time step.
148 * \param molecules vector of molecules to calculate dipoles of
149 * \return map with orientation vector for each atomic id given in \a atoms.
150 */
151std::map<atomId_t, Vector> CalculateZeroAngularDipole(const std::vector<molecule *> &molecules)
152{
153 // get zero orientation for each molecule.
154 LOG(0,"STATUS: Calculating dipoles for current time step ...");
155 std::map<atomId_t, Vector> ZeroVector;
156 BOOST_FOREACH(molecule *_mol, molecules) {
157 const Vector Dipole =
158 getDipole(
159 const_cast<const molecule *>(_mol)->begin(),
160 const_cast<const molecule *>(_mol)->end());
161 for(molecule::const_iterator iter = const_cast<const molecule *>(_mol)->begin();
162 iter != const_cast<const molecule *>(_mol)->end();
163 ++iter)
164 ZeroVector[(*iter)->getId()] = Dipole;
165 LOG(2,"INFO: Zero alignment for molecule " << _mol->getId() << " is " << Dipole);
166 }
167 LOG(1,"INFO: We calculated zero orientation for a total of " << molecules.size() << " molecule(s).");
168
169 return ZeroVector;
170}
171
172/** Calculates the dipole angular correlation for given molecule type.
173 * Calculate the change of the dipole orientation angle over time.
174 * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si))
175 * Angles are given in degrees.
176 * \param &atoms list of atoms of the molecules taking part (Note: molecules may
177 * change over time as bond structure is recalculated, hence we need the atoms)
178 * \param timestep time step to calculate angular correlation for (relative to
179 * \a ZeroVector)
180 * \param ZeroVector map with Zero orientation vector for each atom in \a atoms.
181 * \param DontResetTime don't reset time to old value (triggers re-creation of bond system)
182 * \return Map of doubles with values the pair of the two atoms.
183 */
184DipoleAngularCorrelationMap *DipoleAngularCorrelation(
185 const Formula &DipoleFormula,
186 const size_t timestep,
187 const std::map<atomId_t, Vector> &ZeroVector,
188 const enum ResetWorldTime DoTimeReset
189 )
190{
191 Info FunctionInfo(__func__);
192 DipoleAngularCorrelationMap *outmap = new DipoleAngularCorrelationMap;
193
194 unsigned int oldtime = 0;
195 if (DoTimeReset == DoResetTime) {
196 // store original time step
197 oldtime = WorldTime::getTime();
198 }
199
200 // set time step
201 LOG(0,"STATUS: Stepping onto to time step " << timestep << ".");
202 World::getInstance().setTime(timestep);
203
204 // get all molecules for this time step
205 World::getInstance().clearMoleculeSelection();
206 World::getInstance().selectAllMolecules(MoleculeByFormula(DipoleFormula));
207 std::vector<molecule *> molecules = World::getInstance().getSelectedMolecules();
208 LOG(1,"INFO: There are " << molecules.size() << " molecules for time step " << timestep << ".");
209
210 // calculate dipoles for each
211 LOG(0,"STATUS: Calculating dipoles for time step " << timestep << " ...");
212 size_t i=0;
213 size_t Counter_rejections = 0;
214 BOOST_FOREACH(molecule *_mol, molecules) {
215 const Vector Dipole =
216 getDipole(
217 const_cast<const molecule *>(_mol)->begin(),
218 const_cast<const molecule *>(_mol)->end());
219 LOG(3,"INFO: Dipole vector at time step " << timestep << " for for molecule "
220 << _mol->getId() << " is " << Dipole);
221 // check that all atoms are valid (zeroVector known)
222 molecule::const_iterator iter = const_cast<const molecule *>(_mol)->begin();
223 for(; iter != const_cast<const molecule *>(_mol)->end(); ++iter) {
224 if (!ZeroVector.count((*iter)->getId()))
225 break;
226 }
227 if (iter != const_cast<const molecule *>(_mol)->end()) {
228 ELOG(2, "Skipping molecule " << _mol->getName() << " as not all atoms have a valid zeroVector.");
229 ++Counter_rejections;
230 continue;
231 } else
232 iter = const_cast<const molecule *>(_mol)->begin();
233 std::map<atomId_t, Vector>::const_iterator zeroValue = ZeroVector.find((*iter)->getId()); //due to iter is const
234 double angle = 0.;
235 LOG(2, "INFO: ZeroVector of first atom " << **iter << " is "
236 << zeroValue->second << ".");
237 LOG(4, "INFO: Squared norm of difference vector is "
238 << (zeroValue->second - Dipole).NormSquared() << ".");
239 if ((zeroValue->second - Dipole).NormSquared() > MYEPSILON)
240 angle = Dipole.Angle(zeroValue->second) * (180./M_PI);
241 else
242 LOG(2, "INFO: Both vectors (almost) coincide, numerically unstable, angle set to zero.");
243 // we print six digits, hence round up to six digit precision
244 const double precision = 1e-6;
245 angle = precision*floor(angle/precision);
246 LOG(1,"INFO: Resulting relative angle for molecule " << _mol->getName()
247 << " is " << angle << ".");
248 outmap->insert ( std::make_pair (angle, *iter ) );
249 ++i;
250 }
251 ASSERT(Counter_rejections <= molecules.size(),
252 "DipoleAngularCorrelation() - more rejections ("+toString(Counter_rejections)
253 +") than there are molecules ("+toString(molecules.size())+").");
254 LOG(1,"INFO: " << Counter_rejections << " molecules have been rejected in time step " << timestep << ".");
255
256 LOG(0,"STATUS: Done with calculating dipoles.");
257
258 if (DoTimeReset == DoResetTime) {
259 // re-set to original time step again
260 World::getInstance().setTime(oldtime);
261 }
262
263 // and return results
264 return outmap;
265};
266
267/** Calculates the dipole correlation for given molecule type.
268 * I.e. we calculate how the angle between any two given dipoles in the
269 * systems behaves. Sort of pair correlation but distance is replaced by
270 * the orientation distance, i.e. an angle.
271 * Note given element order is unimportant (i.e. g(Si, O) === g(O, Si))
272 * Angles are given in degrees.
273 * \param *molecules vector of molecules
274 * \return Map of doubles with values the pair of the two atoms.
275 */
276DipoleCorrelationMap *DipoleCorrelation(std::vector<molecule *> &molecules)
277{
278 Info FunctionInfo(__func__);
279 DipoleCorrelationMap *outmap = new DipoleCorrelationMap;
280// double distance = 0.;
281// Box &domain = World::getInstance().getDomain();
282//
283 if (molecules.empty()) {
284 ELOG(1, "No molecule given.");
285 return outmap;
286 }
287
288 for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin();
289 MolWalker != molecules.end(); ++MolWalker) {
290 LOG(2, "INFO: Current molecule is " << (*MolWalker)->getId() << ".");
291 const Vector Dipole =
292 getDipole(
293 const_cast<const molecule *>(*MolWalker)->begin(),
294 const_cast<const molecule *>(*MolWalker)->end());
295 std::vector<molecule *>::const_iterator MolOtherWalker = MolWalker;
296 for (++MolOtherWalker;
297 MolOtherWalker != molecules.end();
298 ++MolOtherWalker) {
299 LOG(2, "INFO: Current other molecule is " << (*MolOtherWalker)->getId() << ".");
300 const Vector OtherDipole = getDipole(
301 const_cast<const molecule *>(*MolOtherWalker)->begin(),
302 const_cast<const molecule *>(*MolOtherWalker)->end());
303 const double angle = Dipole.Angle(OtherDipole) * (180./M_PI);
304 LOG(1, "Angle is " << angle << ".");
305 outmap->insert ( make_pair (angle, make_pair ((*MolWalker), (*MolOtherWalker)) ) );
306 }
307 }
308 return outmap;
309};
310
311/** Calculates the pair correlation between given atom sets.
312 *
313 * Note we correlate each of the \a &atomsfirst with each of the second set
314 * \a &atoms_second. However, we are aware of double counting. If an atom is
315 * in either set, the pair is counted only once.
316 *
317 * \param &atoms_first vector of atoms
318 * \param &atoms_second vector of atoms
319 * \param max_distance maximum distance for the correlation
320 * \return Map of doubles with values the pair of the two atoms.
321 */
322PairCorrelationMap *PairCorrelation(
323 const World::ConstAtomComposite &atoms_first,
324 const World::ConstAtomComposite &atoms_second,
325 const double max_distance)
326{
327 Info FunctionInfo(__func__);
328 PairCorrelationMap *outmap = new PairCorrelationMap;
329 //double distance = 0.;
330 Box &domain = World::getInstance().getDomain();
331
332 if (atoms_first.empty() || atoms_second.empty()) {
333 ELOG(1, "No atoms given.");
334 return outmap;
335 }
336
337 //!> typedef for an unsorted container, (output) compatible with STL algorithms
338 typedef std::vector<const TesselPoint *> LinkedVector;
339
340 // create intersection (to know when to check for double-counting)
341 LinkedVector intersected_atoms(atoms_second.size(), NULL);
342 LinkedVector::iterator intersected_atoms_end =
343 std::set_intersection(
344 atoms_first.begin(),atoms_first.end(),
345 atoms_second.begin(), atoms_second.end(),
346 intersected_atoms.begin());
347 const LinkedCell::LinkedList intersected_atoms_set(intersected_atoms.begin(), intersected_atoms_end);
348
349 // get linked cell view
350 LinkedCell::LinkedCell_View LC = World::getInstance().getLinkedCell(max_distance);
351
352 // convert second to _sorted_ set
353 LinkedCell::LinkedList atoms_second_set(atoms_second.begin(), atoms_second.end());
354 LOG(2, "INFO: first set has " << atoms_first.size()
355 << " and second set has " << atoms_second_set.size() << " atoms.");
356
357 // fill map
358 for (World::ConstAtomComposite::const_iterator iter = atoms_first.begin();
359 iter != atoms_first.end();
360 ++iter) {
361 const TesselPoint * const Walker = *iter;
362 LOG(3, "INFO: Current point is " << Walker->getName() << ".");
363 // obtain all possible neighbors (that is a sorted set)
364 LinkedCell::LinkedList ListOfNeighbors = LC.getPointsInsideSphere(
365 max_distance,
366 Walker->getPosition());
367 LOG(2, "INFO: There are " << ListOfNeighbors.size() << " neighbors.");
368
369 // create intersection with second set
370 // NOTE: STL algorithms do mostly not work on sorted container because reassignment
371 // of a value may also require changing its position.
372 LinkedVector intersected_set(atoms_second.size(), NULL);
373 LinkedVector::iterator intersected_end =
374 std::set_intersection(
375 ListOfNeighbors.begin(),ListOfNeighbors.end(),
376 atoms_second_set.begin(), atoms_second_set.end(),
377 intersected_set.begin());
378 // count remaining elements
379 LOG(2, "INFO: Intersection with second set has " << int(intersected_end - intersected_set.begin()) << " elements.");
380 // we have some possible candidates, go through each
381 for (LinkedVector::const_iterator neighboriter = intersected_set.begin();
382 neighboriter != intersected_end;
383 ++neighboriter) {
384 const TesselPoint * const OtherWalker = (*neighboriter);
385 LinkedCell::LinkedList::const_iterator equaliter = intersected_atoms_set.find(OtherWalker);
386 if ((equaliter != intersected_atoms_set.end()) && (OtherWalker <= Walker)) {
387 // present in both sets, assure that we are larger
388 continue;
389 }
390 LOG(3, "INFO: Current other point is " << *OtherWalker << ".");
391 const double distance = domain.periodicDistance(OtherWalker->getPosition(),Walker->getPosition());
392 LOG(3, "INFO: Resulting distance is " << distance << ".");
393 outmap->insert (
394 std::pair<double, std::pair <const TesselPoint *, const TesselPoint*> > (
395 distance,
396 std::make_pair (Walker, OtherWalker)
397 )
398 );
399 }
400 }
401 // and return
402 return outmap;
403};
404
405/** Calculates the distance (pair) correlation between a given element and a point.
406 * \param *molecules list of molecules structure
407 * \param &elements vector of elements to correlate with point
408 * \param *point vector to the correlation point
409 * \return Map of dobules with values as pairs of atom and the vector
410 */
411CorrelationToPointMap *CorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point )
412{
413 Info FunctionInfo(__func__);
414 CorrelationToPointMap *outmap = new CorrelationToPointMap;
415 double distance = 0.;
416 Box &domain = World::getInstance().getDomain();
417
418 if (molecules.empty()) {
419 LOG(1, "No molecule given.");
420 return outmap;
421 }
422
423 for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) {
424 LOG(2, "Current molecule is " << *MolWalker << ".");
425 for (molecule::const_iterator iter = const_cast<const molecule *>(*MolWalker)->begin();
426 iter != const_cast<const molecule *>(*MolWalker)->end();
427 ++iter) {
428 LOG(3, "Current atom is " << **iter << ".");
429 for (vector<const element *>::const_iterator type = elements.begin(); type != elements.end(); ++type)
430 if ((*type == NULL) || ((*iter)->getType() == *type)) {
431 distance = domain.periodicDistance((*iter)->getPosition(),*point);
432 LOG(4, "Current distance is " << distance << ".");
433 outmap->insert (
434 std::pair<double, std::pair<const atom *, const Vector*> >(
435 distance,
436 std::pair<const atom *, const Vector*> (
437 (*iter),
438 point)
439 )
440 );
441 }
442 }
443 }
444
445 return outmap;
446};
447
448/** Calculates the distance (pair) correlation between a given element, all its periodic images and a point.
449 * \param *molecules list of molecules structure
450 * \param &elements vector of elements to correlate to point
451 * \param *point vector to the correlation point
452 * \param ranges[NDIM] interval boundaries for the periodic images to scan also
453 * \return Map of dobules with values as pairs of atom and the vector
454 */
455CorrelationToPointMap *PeriodicCorrelationToPoint(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Vector *point, const int ranges[NDIM] )
456{
457 Info FunctionInfo(__func__);
458 CorrelationToPointMap *outmap = new CorrelationToPointMap;
459 double distance = 0.;
460 int n[NDIM];
461 Vector periodicX;
462 Vector checkX;
463
464 if (molecules.empty()) {
465 LOG(1, "No molecule given.");
466 return outmap;
467 }
468
469 for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) {
470 RealSpaceMatrix FullMatrix = World::getInstance().getDomain().getM();
471 RealSpaceMatrix FullInverseMatrix = World::getInstance().getDomain().getMinv();
472 LOG(2, "Current molecule is " << *MolWalker << ".");
473 for (molecule::const_iterator iter = const_cast<const molecule *>(*MolWalker)->begin();
474 iter != const_cast<const molecule *>(*MolWalker)->end();
475 ++iter) {
476 LOG(3, "Current atom is " << **iter << ".");
477 for (vector<const element *>::const_iterator type = elements.begin(); type != elements.end(); ++type)
478 if ((*type == NULL) || ((*iter)->getType() == *type)) {
479 periodicX = FullInverseMatrix * ((*iter)->getPosition()); // x now in [0,1)^3
480 // go through every range in xyz and get distance
481 for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
482 for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
483 for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
484 checkX = FullMatrix * (Vector(n[0], n[1], n[2]) + periodicX);
485 distance = checkX.distance(*point);
486 LOG(4, "Current distance is " << distance << ".");
487 outmap->insert (
488 std::pair<double,
489 std::pair<const atom *, const Vector*> >(
490 distance,
491 std::pair<const atom *, const Vector*> (
492 *iter,
493 point)
494 )
495 );
496 }
497 }
498 }
499 }
500
501 return outmap;
502};
503
504/** Calculates the distance (pair) correlation between a given element and a surface.
505 * \param *molecules list of molecules structure
506 * \param &elements vector of elements to correlate to surface
507 * \param *Surface pointer to Tesselation class surface
508 * \param *LC LinkedCell_deprecated structure to quickly find neighbouring atoms
509 * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest
510 */
511CorrelationToSurfaceMap *CorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell_deprecated *LC )
512{
513 Info FunctionInfo(__func__);
514 CorrelationToSurfaceMap *outmap = new CorrelationToSurfaceMap;
515 double distance = 0;
516 class BoundaryTriangleSet *triangle = NULL;
517 Vector centroid;
518
519 if ((Surface == NULL) || (LC == NULL) || (molecules.empty())) {
520 ELOG(1, "No Tesselation, no LinkedCell or no molecule given.");
521 return outmap;
522 }
523
524 for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) {
525 LOG(2, "Current molecule is " << (*MolWalker)->name << ".");
526 if ((*MolWalker)->empty())
527 LOG(2, "\t is empty.");
528 for (molecule::const_iterator iter = const_cast<const molecule *>(*MolWalker)->begin();
529 iter != const_cast<const molecule *>(*MolWalker)->end();
530 ++iter) {
531 LOG(3, "\tCurrent atom is " << *(*iter) << ".");
532 for (vector<const element *>::const_iterator type = elements.begin(); type != elements.end(); ++type)
533 if ((*type == NULL) || ((*iter)->getType() == *type)) {
534 TriangleIntersectionList Intersections((*iter)->getPosition(),Surface,LC);
535 distance = Intersections.GetSmallestDistance();
536 triangle = Intersections.GetClosestTriangle();
537 outmap->insert (
538 std::pair<double,
539 std::pair<const atom *, BoundaryTriangleSet*> >(
540 distance,
541 std::pair<const atom *, BoundaryTriangleSet*> (
542 (*iter),
543 triangle)
544 )
545 );
546 }
547 }
548 }
549
550 return outmap;
551};
552
553/** Calculates the distance (pair) correlation between a given element, all its periodic images and and a surface.
554 * Note that we also put all periodic images found in the cells given by [ -ranges[i], ranges[i] ] and i=0,...,NDIM-1.
555 * 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
556 * axis an integer from [ -ranges[i], ranges[i] ] onto it and multiply with the domain matrix to bring it back into
557 * the real space. Then, we Tesselation::FindClosestTriangleToPoint() and DistanceToTrianglePlane().
558 * \param *molecules list of molecules structure
559 * \param &elements vector of elements to correlate to surface
560 * \param *Surface pointer to Tesselation class surface
561 * \param *LC LinkedCell_deprecated structure to quickly find neighbouring atoms
562 * \param ranges[NDIM] interval boundaries for the periodic images to scan also
563 * \return Map of doubles with values as pairs of atom and the BoundaryTriangleSet that's closest
564 */
565CorrelationToSurfaceMap *PeriodicCorrelationToSurface(std::vector<molecule *> &molecules, const std::vector<const element *> &elements, const Tesselation * const Surface, const LinkedCell_deprecated *LC, const int ranges[NDIM] )
566{
567 Info FunctionInfo(__func__);
568 CorrelationToSurfaceMap *outmap = new CorrelationToSurfaceMap;
569 double distance = 0;
570 class BoundaryTriangleSet *triangle = NULL;
571 Vector centroid;
572 int n[NDIM];
573 Vector periodicX;
574 Vector checkX;
575
576 if ((Surface == NULL) || (LC == NULL) || (molecules.empty())) {
577 LOG(1, "No Tesselation, no LinkedCell or no molecule given.");
578 return outmap;
579 }
580
581 double ShortestDistance = 0.;
582 BoundaryTriangleSet *ShortestTriangle = NULL;
583 for (std::vector<molecule *>::const_iterator MolWalker = molecules.begin(); MolWalker != molecules.end(); MolWalker++) {
584 RealSpaceMatrix FullMatrix = World::getInstance().getDomain().getM();
585 RealSpaceMatrix FullInverseMatrix = World::getInstance().getDomain().getMinv();
586 LOG(2, "Current molecule is " << *MolWalker << ".");
587 for (molecule::const_iterator iter = const_cast<const molecule *>(*MolWalker)->begin();
588 iter != const_cast<const molecule *>(*MolWalker)->end();
589 ++iter) {
590 LOG(3, "Current atom is " << **iter << ".");
591 for (vector<const element *>::const_iterator type = elements.begin(); type != elements.end(); ++type)
592 if ((*type == NULL) || ((*iter)->getType() == *type)) {
593 periodicX = FullInverseMatrix * ((*iter)->getPosition()); // x now in [0,1)^3
594 // go through every range in xyz and get distance
595 ShortestDistance = -1.;
596 for (n[0]=-ranges[0]; n[0] <= ranges[0]; n[0]++)
597 for (n[1]=-ranges[1]; n[1] <= ranges[1]; n[1]++)
598 for (n[2]=-ranges[2]; n[2] <= ranges[2]; n[2]++) {
599 checkX = FullMatrix * (Vector(n[0], n[1], n[2]) + periodicX);
600 TriangleIntersectionList Intersections(checkX,Surface,LC);
601 distance = Intersections.GetSmallestDistance();
602 triangle = Intersections.GetClosestTriangle();
603 if ((ShortestDistance == -1.) || (distance < ShortestDistance)) {
604 ShortestDistance = distance;
605 ShortestTriangle = triangle;
606 }
607 }
608 // insert
609 outmap->insert (
610 std::pair<double,
611 std::pair<const atom *, BoundaryTriangleSet*> >(
612 ShortestDistance,
613 std::pair<const atom *, BoundaryTriangleSet*> (
614 *iter,
615 ShortestTriangle)
616 )
617 );
618 //LOG(1, "INFO: Inserting " << Walker << " with distance " << ShortestDistance << " to " << *ShortestTriangle << ".");
619 }
620 }
621 }
622
623 return outmap;
624};
625
626/** Returns the index of the bin for a given value.
627 * \param value value whose bin to look for
628 * \param BinWidth width of bin
629 * \param BinStart first bin
630 */
631int GetBin ( const double value, const double BinWidth, const double BinStart )
632{
633 //Info FunctionInfo(__func__);
634 int bin =(int) (floor((value - BinStart)/BinWidth));
635 return (bin);
636};
637
638
639/** Adds header part that is unique to BinPairMap.
640 *
641 * @param file stream to print to
642 */
643void OutputCorrelation_Header( ofstream * const file )
644{
645 *file << "\tCount";
646};
647
648/** Prints values stored in BinPairMap iterator.
649 *
650 * @param file stream to print to
651 * @param runner iterator pointing at values to print
652 */
653void OutputCorrelation_Value( ofstream * const file, BinPairMap::const_iterator &runner )
654{
655 *file << runner->second;
656};
657
658
659/** Adds header part that is unique to DipoleAngularCorrelationMap.
660 *
661 * @param file stream to print to
662 */
663void OutputDipoleAngularCorrelation_Header( ofstream * const file )
664{
665 *file << "\tFirstAtomOfMolecule";
666};
667
668/** Prints values stored in DipoleCorrelationMap iterator.
669 *
670 * @param file stream to print to
671 * @param runner iterator pointing at values to print
672 */
673void OutputDipoleAngularCorrelation_Value( ofstream * const file, DipoleAngularCorrelationMap::const_iterator &runner )
674{
675 *file << *(runner->second);
676};
677
678
679/** Adds header part that is unique to DipoleAngularCorrelationMap.
680 *
681 * @param file stream to print to
682 */
683void OutputDipoleCorrelation_Header( ofstream * const file )
684{
685 *file << "\tMolecule";
686};
687
688/** Prints values stored in DipoleCorrelationMap iterator.
689 *
690 * @param file stream to print to
691 * @param runner iterator pointing at values to print
692 */
693void OutputDipoleCorrelation_Value( ofstream * const file, DipoleCorrelationMap::const_iterator &runner )
694{
695 *file << runner->second.first->getId() << "\t" << runner->second.second->getId();
696};
697
698
699/** Adds header part that is unique to PairCorrelationMap.
700 *
701 * @param file stream to print to
702 */
703void OutputPairCorrelation_Header( ofstream * const file )
704{
705 *file << "\tAtom1\tAtom2";
706};
707
708/** Prints values stored in PairCorrelationMap iterator.
709 *
710 * @param file stream to print to
711 * @param runner iterator pointing at values to print
712 */
713void OutputPairCorrelation_Value( ofstream * const file, PairCorrelationMap::const_iterator &runner )
714{
715 *file << *(runner->second.first) << "\t" << *(runner->second.second);
716};
717
718
719/** Adds header part that is unique to CorrelationToPointMap.
720 *
721 * @param file stream to print to
722 */
723void OutputCorrelationToPoint_Header( ofstream * const file )
724{
725 *file << "\tAtom::x[i]-point.x[i]";
726};
727
728/** Prints values stored in CorrelationToPointMap iterator.
729 *
730 * @param file stream to print to
731 * @param runner iterator pointing at values to print
732 */
733void OutputCorrelationToPoint_Value( ofstream * const file, CorrelationToPointMap::const_iterator &runner )
734{
735 for (int i=0;i<NDIM;i++)
736 *file << "\t" << setprecision(8) << (runner->second.first->at(i) - runner->second.second->at(i));
737};
738
739
740/** Adds header part that is unique to CorrelationToSurfaceMap.
741 *
742 * @param file stream to print to
743 */
744void OutputCorrelationToSurface_Header( ofstream * const file )
745{
746 *file << "\tTriangle";
747};
748
749/** Prints values stored in CorrelationToSurfaceMap iterator.
750 *
751 * @param file stream to print to
752 * @param runner iterator pointing at values to print
753 */
754void OutputCorrelationToSurface_Value( ofstream * const file, CorrelationToSurfaceMap::const_iterator &runner )
755{
756 *file << *(runner->second.first) << "\t" << *(runner->second.second);
757};
Note: See TracBrowser for help on using the repository browser.