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