[bcf653] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
[0aa122] | 4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
[bcf653] | 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
[f66195] | 8 | /** \file boundary.cpp
|
---|
[edb93c] | 9 | *
|
---|
| 10 | * Implementations and super-function for envelopes
|
---|
[2319ed] | 11 | */
|
---|
| 12 |
|
---|
[bf3817] | 13 | // include config.h
|
---|
| 14 | #ifdef HAVE_CONFIG_H
|
---|
| 15 | #include <config.h>
|
---|
| 16 | #endif
|
---|
| 17 |
|
---|
[ad011c] | 18 | #include "CodePatterns/MemDebug.hpp"
|
---|
[112b09] | 19 |
|
---|
[6f0841] | 20 | #include "Atom/atom.hpp"
|
---|
[129204] | 21 | #include "Bond/bond.hpp"
|
---|
[8eb17a] | 22 | #include "boundary.hpp"
|
---|
[34c43a] | 23 | #include "BoundaryLineSet.hpp"
|
---|
| 24 | #include "BoundaryPointSet.hpp"
|
---|
| 25 | #include "BoundaryTriangleSet.hpp"
|
---|
| 26 | #include "Box.hpp"
|
---|
| 27 | #include "CandidateForTesselation.hpp"
|
---|
| 28 | #include "CodePatterns/Info.hpp"
|
---|
| 29 | #include "CodePatterns/Log.hpp"
|
---|
| 30 | #include "CodePatterns/Verbose.hpp"
|
---|
[f66195] | 31 | #include "config.hpp"
|
---|
[3bdb6d] | 32 | #include "Element/element.hpp"
|
---|
[34c43a] | 33 | #include "LinearAlgebra/Plane.hpp"
|
---|
| 34 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
|
---|
[53c7fc] | 35 | #include "LinkedCell/linkedcell.hpp"
|
---|
| 36 | #include "LinkedCell/PointCloudAdaptor.hpp"
|
---|
[f66195] | 37 | #include "molecule.hpp"
|
---|
[42127c] | 38 | #include "MoleculeListClass.hpp"
|
---|
[34c43a] | 39 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
| 40 | #include "RandomNumbers/RandomNumberGenerator.hpp"
|
---|
[f66195] | 41 | #include "tesselation.hpp"
|
---|
| 42 | #include "tesselationhelpers.hpp"
|
---|
[b34306] | 43 | #include "World.hpp"
|
---|
[2319ed] | 44 |
|
---|
[986ed3] | 45 | #include <iostream>
|
---|
[36166d] | 46 | #include <iomanip>
|
---|
| 47 |
|
---|
[357fba] | 48 | #include<gsl/gsl_poly.h>
|
---|
[d6eb80] | 49 | #include<time.h>
|
---|
[2319ed] | 50 |
|
---|
[357fba] | 51 | // ========================================== F U N C T I O N S =================================
|
---|
[2319ed] | 52 |
|
---|
| 53 |
|
---|
[357fba] | 54 | /** Determines greatest diameters of a cluster defined by its convex envelope.
|
---|
| 55 | * Looks at lines parallel to one axis and where they intersect on the projected planes
|
---|
[2319ed] | 56 | * \param *out output stream for debugging
|
---|
[357fba] | 57 | * \param *BoundaryPoints NDIM set of boundary points defining the convex envelope on each projected plane
|
---|
| 58 | * \param *mol molecule structure representing the cluster
|
---|
[776b64] | 59 | * \param *&TesselStruct Tesselation structure with triangles
|
---|
[357fba] | 60 | * \param IsAngstroem whether we have angstroem or atomic units
|
---|
| 61 | * \return NDIM array of the diameters
|
---|
[e4ea46] | 62 | */
|
---|
[e138de] | 63 | double *GetDiametersOfCluster(const Boundaries *BoundaryPtr, const molecule *mol, Tesselation *&TesselStruct, const bool IsAngstroem)
|
---|
[caf5d6] | 64 | {
|
---|
[f67b6e] | 65 | Info FunctionInfo(__func__);
|
---|
[357fba] | 66 | // get points on boundary of NULL was given as parameter
|
---|
| 67 | bool BoundaryFreeFlag = false;
|
---|
[ad37ab] | 68 | double OldComponent = 0.;
|
---|
| 69 | double tmp = 0.;
|
---|
| 70 | double w1 = 0.;
|
---|
| 71 | double w2 = 0.;
|
---|
| 72 | Vector DistanceVector;
|
---|
| 73 | Vector OtherVector;
|
---|
| 74 | int component = 0;
|
---|
| 75 | int Othercomponent = 0;
|
---|
[776b64] | 76 | Boundaries::const_iterator Neighbour;
|
---|
| 77 | Boundaries::const_iterator OtherNeighbour;
|
---|
[ad37ab] | 78 | double *GreatestDiameter = new double[NDIM];
|
---|
| 79 |
|
---|
[776b64] | 80 | const Boundaries *BoundaryPoints;
|
---|
| 81 | if (BoundaryPtr == NULL) {
|
---|
[357fba] | 82 | BoundaryFreeFlag = true;
|
---|
[e138de] | 83 | BoundaryPoints = GetBoundaryPoints(mol, TesselStruct);
|
---|
[86234b] | 84 | } else {
|
---|
[776b64] | 85 | BoundaryPoints = BoundaryPtr;
|
---|
[47d041] | 86 | LOG(0, "Using given boundary points set.");
|
---|
[86234b] | 87 | }
|
---|
[357fba] | 88 | // determine biggest "diameter" of cluster for each axis
|
---|
| 89 | for (int i = 0; i < NDIM; i++)
|
---|
| 90 | GreatestDiameter[i] = 0.;
|
---|
| 91 | for (int axis = 0; axis < NDIM; axis++)
|
---|
| 92 | { // regard each projected plane
|
---|
[47d041] | 93 | //LOG(1, "Current axis is " << axis << ".");
|
---|
[357fba] | 94 | for (int j = 0; j < 2; j++)
|
---|
| 95 | { // and for both axis on the current plane
|
---|
| 96 | component = (axis + j + 1) % NDIM;
|
---|
| 97 | Othercomponent = (axis + 1 + ((j + 1) & 1)) % NDIM;
|
---|
[47d041] | 98 | //LOG(1, "Current component is " << component << ", Othercomponent is " << Othercomponent << ".");
|
---|
[776b64] | 99 | for (Boundaries::const_iterator runner = BoundaryPoints[axis].begin(); runner != BoundaryPoints[axis].end(); runner++) {
|
---|
[47d041] | 100 | //LOG(1, "Current runner is " << *(runner->second.second) << ".");
|
---|
[357fba] | 101 | // seek for the neighbours pair where the Othercomponent sign flips
|
---|
| 102 | Neighbour = runner;
|
---|
| 103 | Neighbour++;
|
---|
| 104 | if (Neighbour == BoundaryPoints[axis].end()) // make it wrap around
|
---|
| 105 | Neighbour = BoundaryPoints[axis].begin();
|
---|
[d74077] | 106 | DistanceVector = (runner->second.second->getPosition()) - (Neighbour->second.second->getPosition());
|
---|
[776b64] | 107 | do { // seek for neighbour pair where it flips
|
---|
[0a4f7f] | 108 | OldComponent = DistanceVector[Othercomponent];
|
---|
[357fba] | 109 | Neighbour++;
|
---|
| 110 | if (Neighbour == BoundaryPoints[axis].end()) // make it wrap around
|
---|
| 111 | Neighbour = BoundaryPoints[axis].begin();
|
---|
[d74077] | 112 | DistanceVector = (runner->second.second->getPosition()) - (Neighbour->second.second->getPosition());
|
---|
[47d041] | 113 | //LOG(2, "OldComponent is " << OldComponent << ", new one is " << DistanceVector.x[Othercomponent] << ".");
|
---|
[776b64] | 114 | } while ((runner != Neighbour) && (fabs(OldComponent / fabs(
|
---|
[0a4f7f] | 115 | OldComponent) - DistanceVector[Othercomponent] / fabs(
|
---|
| 116 | DistanceVector[Othercomponent])) < MYEPSILON)); // as long as sign does not flip
|
---|
[776b64] | 117 | if (runner != Neighbour) {
|
---|
[357fba] | 118 | OtherNeighbour = Neighbour;
|
---|
| 119 | if (OtherNeighbour == BoundaryPoints[axis].begin()) // make it wrap around
|
---|
| 120 | OtherNeighbour = BoundaryPoints[axis].end();
|
---|
| 121 | OtherNeighbour--;
|
---|
[47d041] | 122 | //LOG(1, "The pair, where the sign of OtherComponent flips, is: " << *(Neighbour->second.second) << " and " << *(OtherNeighbour->second.second) << ".");
|
---|
[357fba] | 123 | // now we have found the pair: Neighbour and OtherNeighbour
|
---|
[d74077] | 124 | OtherVector = (runner->second.second->getPosition()) - (OtherNeighbour->second.second->getPosition());
|
---|
[47d041] | 125 | //LOG(1, "Distances to Neighbour and OtherNeighbour are " << DistanceVector.x[component] << " and " << OtherVector.x[component] << ".");
|
---|
| 126 | //LOG(1, "OtherComponents to Neighbour and OtherNeighbour are " << DistanceVector.x[Othercomponent] << " and " << OtherVector.x[Othercomponent] << ".");
|
---|
[357fba] | 127 | // do linear interpolation between points (is exact) to extract exact intersection between Neighbour and OtherNeighbour
|
---|
[0a4f7f] | 128 | w1 = fabs(OtherVector[Othercomponent]);
|
---|
| 129 | w2 = fabs(DistanceVector[Othercomponent]);
|
---|
| 130 | tmp = fabs((w1 * DistanceVector[component] + w2
|
---|
| 131 | * OtherVector[component]) / (w1 + w2));
|
---|
[357fba] | 132 | // mark if it has greater diameter
|
---|
[47d041] | 133 | //LOG(1, "Comparing current greatest " << GreatestDiameter[component] << " to new " << tmp << ".");
|
---|
[357fba] | 134 | GreatestDiameter[component] = (GreatestDiameter[component]
|
---|
| 135 | > tmp) ? GreatestDiameter[component] : tmp;
|
---|
| 136 | } //else
|
---|
[47d041] | 137 | //LOG(1, "Saw no sign flip, probably top or bottom node.");
|
---|
[3d919e] | 138 | }
|
---|
| 139 | }
|
---|
| 140 | }
|
---|
[47d041] | 141 | LOG(0, "RESULT: The biggest diameters are "
|
---|
[357fba] | 142 | << GreatestDiameter[0] << " and " << GreatestDiameter[1] << " and "
|
---|
| 143 | << GreatestDiameter[2] << " " << (IsAngstroem ? "angstrom"
|
---|
[47d041] | 144 | : "atomiclength") << ".");
|
---|
[03648b] | 145 |
|
---|
[357fba] | 146 | // free reference lists
|
---|
| 147 | if (BoundaryFreeFlag)
|
---|
| 148 | delete[] (BoundaryPoints);
|
---|
[e4ea46] | 149 |
|
---|
[357fba] | 150 | return GreatestDiameter;
|
---|
[e4ea46] | 151 | }
|
---|
| 152 | ;
|
---|
[03648b] | 153 |
|
---|
[042f82] | 154 |
|
---|
[357fba] | 155 | /** Determines the boundary points of a cluster.
|
---|
| 156 | * Does a projection per axis onto the orthogonal plane, transforms into spherical coordinates, sorts them by the angle
|
---|
| 157 | * and looks at triples: if the middle has less a distance than the allowed maximum height of the triangle formed by the plane's
|
---|
| 158 | * center and first and last point in the triple, it is thrown out.
|
---|
| 159 | * \param *out output stream for debugging
|
---|
| 160 | * \param *mol molecule structure representing the cluster
|
---|
[776b64] | 161 | * \param *&TesselStruct pointer to Tesselation structure
|
---|
[e4ea46] | 162 | */
|
---|
[e138de] | 163 | Boundaries *GetBoundaryPoints(const molecule *mol, Tesselation *&TesselStruct)
|
---|
[caf5d6] | 164 | {
|
---|
[f67b6e] | 165 | Info FunctionInfo(__func__);
|
---|
[357fba] | 166 | PointMap PointsOnBoundary;
|
---|
| 167 | LineMap LinesOnBoundary;
|
---|
| 168 | TriangleMap TrianglesOnBoundary;
|
---|
[e138de] | 169 | Vector *MolCenter = mol->DetermineCenterOfAll();
|
---|
[357fba] | 170 | Vector helper;
|
---|
[ad37ab] | 171 | BoundariesTestPair BoundaryTestPair;
|
---|
| 172 | Vector AxisVector;
|
---|
| 173 | Vector AngleReferenceVector;
|
---|
| 174 | Vector AngleReferenceNormalVector;
|
---|
| 175 | Vector ProjectedVector;
|
---|
[5309ba] | 176 | Boundaries *BoundaryPoints = new Boundaries[NDIM]; // first is alpha, second is (r, Nr)
|
---|
[ad37ab] | 177 | double angle = 0.;
|
---|
[042f82] | 178 |
|
---|
[357fba] | 179 | // 3a. Go through every axis
|
---|
| 180 | for (int axis = 0; axis < NDIM; axis++) {
|
---|
| 181 | AxisVector.Zero();
|
---|
| 182 | AngleReferenceVector.Zero();
|
---|
| 183 | AngleReferenceNormalVector.Zero();
|
---|
[0a4f7f] | 184 | AxisVector[axis] = 1.;
|
---|
| 185 | AngleReferenceVector[(axis + 1) % NDIM] = 1.;
|
---|
| 186 | AngleReferenceNormalVector[(axis + 2) % NDIM] = 1.;
|
---|
[042f82] | 187 |
|
---|
[47d041] | 188 | LOG(1, "Axisvector is " << AxisVector << " and AngleReferenceVector is " << AngleReferenceVector << ", and AngleReferenceNormalVector is " << AngleReferenceNormalVector << ".");
|
---|
[042f82] | 189 |
|
---|
[357fba] | 190 | // 3b. construct set of all points, transformed into cylindrical system and with left and right neighbours
|
---|
[59fff1] | 191 | // Boundaries stores non-const TesselPoint ref, hence we need iterator here
|
---|
| 192 | for (molecule::iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[d74077] | 193 | ProjectedVector = (*iter)->getPosition() - (*MolCenter);
|
---|
[273382] | 194 | ProjectedVector.ProjectOntoPlane(AxisVector);
|
---|
[042f82] | 195 |
|
---|
[357fba] | 196 | // correct for negative side
|
---|
[776b64] | 197 | const double radius = ProjectedVector.NormSquared();
|
---|
[357fba] | 198 | if (fabs(radius) > MYEPSILON)
|
---|
[273382] | 199 | angle = ProjectedVector.Angle(AngleReferenceVector);
|
---|
[357fba] | 200 | else
|
---|
| 201 | angle = 0.; // otherwise it's a vector in Axis Direction and unimportant for boundary issues
|
---|
[042f82] | 202 |
|
---|
[47d041] | 203 | //LOG(1, "Checking sign in quadrant : " << ProjectedVector.Projection(&AngleReferenceNormalVector) << ".");
|
---|
[273382] | 204 | if (ProjectedVector.ScalarProduct(AngleReferenceNormalVector) > 0) {
|
---|
[357fba] | 205 | angle = 2. * M_PI - angle;
|
---|
| 206 | }
|
---|
[47d041] | 207 | LOG(1, "Inserting " << **iter << ": (r, alpha) = (" << radius << "," << angle << "): " << ProjectedVector);
|
---|
[6b5657] | 208 | BoundaryTestPair = BoundaryPoints[axis].insert(BoundariesPair(angle, TesselPointDistancePair (radius, (*iter))));
|
---|
[357fba] | 209 | if (!BoundaryTestPair.second) { // same point exists, check first r, then distance of original vectors to center of gravity
|
---|
[47d041] | 210 | LOG(2, "Encountered two vectors whose projection onto axis " << axis << " is equal: ");
|
---|
| 211 | LOG(2, "Present vector: " << *BoundaryTestPair.first->second.second);
|
---|
| 212 | LOG(2, "New vector: " << **iter);
|
---|
[776b64] | 213 | const double ProjectedVectorNorm = ProjectedVector.NormSquared();
|
---|
| 214 | if ((ProjectedVectorNorm - BoundaryTestPair.first->second.first) > MYEPSILON) {
|
---|
| 215 | BoundaryTestPair.first->second.first = ProjectedVectorNorm;
|
---|
[9879f6] | 216 | BoundaryTestPair.first->second.second = (*iter);
|
---|
[47d041] | 217 | LOG(2, "Keeping new vector due to larger projected distance " << ProjectedVectorNorm << ".");
|
---|
[776b64] | 218 | } else if (fabs(ProjectedVectorNorm - BoundaryTestPair.first->second.first) < MYEPSILON) {
|
---|
[d74077] | 219 | helper = (*iter)->getPosition() - (*MolCenter);
|
---|
[776b64] | 220 | const double oldhelperNorm = helper.NormSquared();
|
---|
[d74077] | 221 | helper = BoundaryTestPair.first->second.second->getPosition() - (*MolCenter);
|
---|
[776b64] | 222 | if (helper.NormSquared() < oldhelperNorm) {
|
---|
[9879f6] | 223 | BoundaryTestPair.first->second.second = (*iter);
|
---|
[47d041] | 224 | LOG(2, "Keeping new vector due to larger distance to molecule center " << helper.NormSquared() << ".");
|
---|
[357fba] | 225 | } else {
|
---|
[47d041] | 226 | LOG(2, "Keeping present vector due to larger distance to molecule center " << oldhelperNorm << ".");
|
---|
[357fba] | 227 | }
|
---|
| 228 | } else {
|
---|
[47d041] | 229 | LOG(2, "Keeping present vector due to larger projected distance " << ProjectedVectorNorm << ".");
|
---|
[357fba] | 230 | }
|
---|
[018741] | 231 | }
|
---|
[3d919e] | 232 | }
|
---|
[357fba] | 233 | // printing all inserted for debugging
|
---|
| 234 | // {
|
---|
[47d041] | 235 | // std::stringstream output;
|
---|
| 236 | // output << "Printing list of candidates for axis " << axis << " which we have inserted so far: ";
|
---|
[357fba] | 237 | // int i=0;
|
---|
| 238 | // for(Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner != BoundaryPoints[axis].end(); runner++) {
|
---|
| 239 | // if (runner != BoundaryPoints[axis].begin())
|
---|
[47d041] | 240 | // output << ", " << i << ": " << *runner->second.second;
|
---|
[357fba] | 241 | // else
|
---|
[47d041] | 242 | // output << i << ": " << *runner->second.second;
|
---|
[357fba] | 243 | // i++;
|
---|
| 244 | // }
|
---|
[47d041] | 245 | // LOG(1, output.str());
|
---|
[357fba] | 246 | // }
|
---|
| 247 | // 3c. throw out points whose distance is less than the mean of left and right neighbours
|
---|
| 248 | bool flag = false;
|
---|
[47d041] | 249 | LOG(1, "Looking for candidates to kick out by convex condition ... ");
|
---|
[357fba] | 250 | do { // do as long as we still throw one out per round
|
---|
| 251 | flag = false;
|
---|
[accebe] | 252 | Boundaries::iterator left = BoundaryPoints[axis].begin();
|
---|
| 253 | Boundaries::iterator right = BoundaryPoints[axis].begin();
|
---|
| 254 | Boundaries::iterator runner = BoundaryPoints[axis].begin();
|
---|
| 255 | bool LoopOnceDone = false;
|
---|
| 256 | while (!LoopOnceDone) {
|
---|
| 257 | runner = right;
|
---|
| 258 | right++;
|
---|
[357fba] | 259 | // set neighbours correctly
|
---|
| 260 | if (runner == BoundaryPoints[axis].begin()) {
|
---|
| 261 | left = BoundaryPoints[axis].end();
|
---|
| 262 | } else {
|
---|
| 263 | left = runner;
|
---|
| 264 | }
|
---|
| 265 | left--;
|
---|
| 266 | if (right == BoundaryPoints[axis].end()) {
|
---|
| 267 | right = BoundaryPoints[axis].begin();
|
---|
[accebe] | 268 | LoopOnceDone = true;
|
---|
[357fba] | 269 | }
|
---|
| 270 | // check distance
|
---|
[3d919e] | 271 |
|
---|
[357fba] | 272 | // construct the vector of each side of the triangle on the projected plane (defined by normal vector AxisVector)
|
---|
| 273 | {
|
---|
| 274 | Vector SideA, SideB, SideC, SideH;
|
---|
[d74077] | 275 | SideA = left->second.second->getPosition() - (*MolCenter);
|
---|
[273382] | 276 | SideA.ProjectOntoPlane(AxisVector);
|
---|
[47d041] | 277 | // LOG(1, "SideA: " << SideA);
|
---|
[3d919e] | 278 |
|
---|
[d74077] | 279 | SideB = right->second.second->getPosition() -(*MolCenter);
|
---|
[273382] | 280 | SideB.ProjectOntoPlane(AxisVector);
|
---|
[47d041] | 281 | // LOG(1, "SideB: " << SideB);
|
---|
[3d919e] | 282 |
|
---|
[d74077] | 283 | SideC = left->second.second->getPosition() - right->second.second->getPosition();
|
---|
[273382] | 284 | SideC.ProjectOntoPlane(AxisVector);
|
---|
[47d041] | 285 | // LOG(1, "SideC: " << SideC);
|
---|
[3d919e] | 286 |
|
---|
[d74077] | 287 | SideH = runner->second.second->getPosition() -(*MolCenter);
|
---|
[273382] | 288 | SideH.ProjectOntoPlane(AxisVector);
|
---|
[47d041] | 289 | // LOG(1, "SideH: " << SideH);
|
---|
[3d919e] | 290 |
|
---|
[357fba] | 291 | // calculate each length
|
---|
[ad37ab] | 292 | const double a = SideA.Norm();
|
---|
| 293 | //const double b = SideB.Norm();
|
---|
| 294 | //const double c = SideC.Norm();
|
---|
| 295 | const double h = SideH.Norm();
|
---|
[357fba] | 296 | // calculate the angles
|
---|
[273382] | 297 | const double alpha = SideA.Angle(SideH);
|
---|
| 298 | const double beta = SideA.Angle(SideC);
|
---|
| 299 | const double gamma = SideB.Angle(SideH);
|
---|
| 300 | const double delta = SideC.Angle(SideH);
|
---|
[ad37ab] | 301 | const double MinDistance = a * sin(beta) / (sin(delta)) * (((alpha < M_PI / 2.) || (gamma < M_PI / 2.)) ? 1. : -1.);
|
---|
[47d041] | 302 | //LOG(1, " I calculated: a = " << a << ", h = " << h << ", beta(" << left->second.second->Name << "," << left->second.second->Name << "-" << right->second.second->Name << ") = " << beta << ", delta(" << left->second.second->Name << "," << runner->second.second->Name << ") = " << delta << ", Min = " << MinDistance << ".");
|
---|
| 303 | LOG(1, "Checking CoG distance of runner " << *runner->second.second << " " << h << " against triangle's side length spanned by (" << *left->second.second << "," << *right->second.second << ") of " << MinDistance << ".");
|
---|
[357fba] | 304 | if ((fabs(h / fabs(h) - MinDistance / fabs(MinDistance)) < MYEPSILON) && ((h - MinDistance)) < -MYEPSILON) {
|
---|
| 305 | // throw out point
|
---|
[47d041] | 306 | LOG(1, "Throwing out " << *runner->second.second << ".");
|
---|
[357fba] | 307 | BoundaryPoints[axis].erase(runner);
|
---|
[accebe] | 308 | runner = right;
|
---|
[357fba] | 309 | flag = true;
|
---|
[3d919e] | 310 | }
|
---|
| 311 | }
|
---|
| 312 | }
|
---|
[357fba] | 313 | } while (flag);
|
---|
[3d919e] | 314 | }
|
---|
[357fba] | 315 | delete(MolCenter);
|
---|
| 316 | return BoundaryPoints;
|
---|
[6ac7ee] | 317 | };
|
---|
| 318 |
|
---|
[357fba] | 319 | /** Tesselates the convex boundary by finding all boundary points.
|
---|
| 320 | * \param *out output stream for debugging
|
---|
[776b64] | 321 | * \param *mol molecule structure with Atom's and Bond's.
|
---|
[bdc91e] | 322 | * \param *BoundaryPts set of boundary points to use or NULL
|
---|
[357fba] | 323 | * \param *TesselStruct Tesselation filled with points, lines and triangles on boundary on return
|
---|
[6bd7e0] | 324 | * \param *LCList atoms in LinkedCell_deprecated list
|
---|
[357fba] | 325 | * \param *filename filename prefix for output of vertex data
|
---|
| 326 | * \return *TesselStruct is filled with convex boundary and tesselation is stored under \a *filename.
|
---|
[6ac7ee] | 327 | */
|
---|
[6bd7e0] | 328 | void FindConvexBorder(const molecule* mol, Boundaries *BoundaryPts, Tesselation *&TesselStruct, const LinkedCell_deprecated *LCList, const char *filename)
|
---|
[6ac7ee] | 329 | {
|
---|
[f67b6e] | 330 | Info FunctionInfo(__func__);
|
---|
[357fba] | 331 | bool BoundaryFreeFlag = false;
|
---|
| 332 | Boundaries *BoundaryPoints = NULL;
|
---|
[3d919e] | 333 |
|
---|
[776b64] | 334 | if (TesselStruct != NULL) // free if allocated
|
---|
| 335 | delete(TesselStruct);
|
---|
| 336 | TesselStruct = new class Tesselation;
|
---|
[3d919e] | 337 |
|
---|
[357fba] | 338 | // 1. Find all points on the boundary
|
---|
[bdc91e] | 339 | if (BoundaryPts == NULL) {
|
---|
| 340 | BoundaryFreeFlag = true;
|
---|
| 341 | BoundaryPoints = GetBoundaryPoints(mol, TesselStruct);
|
---|
[357fba] | 342 | } else {
|
---|
[bdc91e] | 343 | BoundaryPoints = BoundaryPts;
|
---|
[47d041] | 344 | LOG(0, "Using given boundary points set.");
|
---|
[3d919e] | 345 | }
|
---|
| 346 |
|
---|
[357fba] | 347 | // printing all inserted for debugging
|
---|
[47d041] | 348 | if (DoLog(1)) {
|
---|
| 349 | for (int axis=0; axis < NDIM; axis++) {
|
---|
| 350 | std::stringstream output;
|
---|
| 351 | output << "Printing list of candidates for axis " << axis << " which we have inserted so far: ";
|
---|
| 352 | int i=0;
|
---|
| 353 | for(Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner != BoundaryPoints[axis].end(); runner++) {
|
---|
| 354 | if (runner != BoundaryPoints[axis].begin())
|
---|
| 355 | output << ", " << i << ": " << *runner->second.second;
|
---|
| 356 | else
|
---|
| 357 | output << i << ": " << *runner->second.second;
|
---|
| 358 | i++;
|
---|
| 359 | }
|
---|
| 360 | LOG(1, output.str());
|
---|
[a37350] | 361 | }
|
---|
[bdc91e] | 362 | }
|
---|
[3d919e] | 363 |
|
---|
[357fba] | 364 | // 2. fill the boundary point list
|
---|
| 365 | for (int axis = 0; axis < NDIM; axis++)
|
---|
| 366 | for (Boundaries::iterator runner = BoundaryPoints[axis].begin(); runner != BoundaryPoints[axis].end(); runner++)
|
---|
[776b64] | 367 | if (!TesselStruct->AddBoundaryPoint(runner->second.second, 0))
|
---|
[47d041] | 368 | LOG(2, "Point " << *(runner->second.second) << " is already present.");
|
---|
[e4ea46] | 369 |
|
---|
[47d041] | 370 | LOG(0, "I found " << TesselStruct->PointsOnBoundaryCount << " points on the convex boundary.");
|
---|
[357fba] | 371 | // now we have the whole set of edge points in the BoundaryList
|
---|
[018741] | 372 |
|
---|
[357fba] | 373 | // listing for debugging
|
---|
[47d041] | 374 | //if (DoLog(1)) {
|
---|
| 375 | // std::stringstream output;
|
---|
| 376 | // output << "Listing PointsOnBoundary:";
|
---|
[357fba] | 377 | // for(PointMap::iterator runner = PointsOnBoundary.begin(); runner != PointsOnBoundary.end(); runner++) {
|
---|
[47d041] | 378 | // output << " " << *runner->second;
|
---|
[357fba] | 379 | // }
|
---|
[47d041] | 380 | // LOG(1, output.str());
|
---|
| 381 | //}
|
---|
[018741] | 382 |
|
---|
[357fba] | 383 | // 3a. guess starting triangle
|
---|
[e138de] | 384 | TesselStruct->GuessStartingTriangle();
|
---|
[018741] | 385 |
|
---|
[357fba] | 386 | // 3b. go through all lines, that are not yet part of two triangles (only of one so far)
|
---|
[caa06ef] | 387 | PointCloudAdaptor< molecule > cloud(const_cast<molecule *>(mol), mol->name);
|
---|
[34c43a] | 388 | TesselStruct->TesselateOnBoundary(cloud);
|
---|
[3d919e] | 389 |
|
---|
[357fba] | 390 | // 3c. check whether all atoms lay inside the boundary, if not, add to boundary points, segment triangle into three with the new point
|
---|
[34c43a] | 391 | if (!TesselStruct->InsertStraddlingPoints(cloud, LCList))
|
---|
[47d041] | 392 | ELOG(1, "Insertion of straddling points failed!");
|
---|
[3d919e] | 393 |
|
---|
[47d041] | 394 | LOG(0, "I created " << TesselStruct->TrianglesOnBoundary.size() << " intermediate triangles with " << TesselStruct->LinesOnBoundary.size() << " lines and " << TesselStruct->PointsOnBoundary.size() << " points.");
|
---|
[ef0e6d] | 395 |
|
---|
| 396 | // 4. Store triangles in tecplot file
|
---|
[d51975] | 397 | StoreTrianglesinFile(mol, TesselStruct, filename, "_intermed");
|
---|
[ef0e6d] | 398 |
|
---|
[357fba] | 399 | // 3d. check all baselines whether the peaks of the two adjacent triangles with respect to center of baseline are convex, if not, make the baseline between the two peaks and baseline endpoints become the new peaks
|
---|
[ad37ab] | 400 | bool AllConvex = true;
|
---|
[093645] | 401 | class BoundaryLineSet *line = NULL;
|
---|
| 402 | do {
|
---|
| 403 | AllConvex = true;
|
---|
[776b64] | 404 | for (LineMap::iterator LineRunner = TesselStruct->LinesOnBoundary.begin(); LineRunner != TesselStruct->LinesOnBoundary.end(); LineRunner++) {
|
---|
[093645] | 405 | line = LineRunner->second;
|
---|
[47d041] | 406 | LOG(1, "INFO: Current line is " << *line << ".");
|
---|
[e138de] | 407 | if (!line->CheckConvexityCriterion()) {
|
---|
[47d041] | 408 | LOG(1, "... line " << *line << " is concave, flipping it.");
|
---|
[093645] | 409 |
|
---|
| 410 | // flip the line
|
---|
[e138de] | 411 | if (TesselStruct->PickFarthestofTwoBaselines(line) == 0.)
|
---|
[47d041] | 412 | ELOG(1, "Correction of concave baselines failed!");
|
---|
[57066a] | 413 | else {
|
---|
[e138de] | 414 | TesselStruct->FlipBaseline(line);
|
---|
[47d041] | 415 | LOG(1, "INFO: Correction of concave baselines worked.");
|
---|
[accebe] | 416 | LineRunner = TesselStruct->LinesOnBoundary.begin(); // LineRunner may have been erase if line was deleted from LinesOnBoundary
|
---|
[57066a] | 417 | }
|
---|
[093645] | 418 | }
|
---|
| 419 | }
|
---|
| 420 | } while (!AllConvex);
|
---|
[3d919e] | 421 |
|
---|
[ef0e6d] | 422 | // 3e. we need another correction here, for TesselPoints that are below the surface (i.e. have an odd number of concave triangles surrounding it)
|
---|
[776b64] | 423 | // if (!TesselStruct->CorrectConcaveTesselPoints(out))
|
---|
[47d041] | 424 | // ELOG(1, "Correction of concave tesselpoints failed!");
|
---|
[ef0e6d] | 425 |
|
---|
[47d041] | 426 | LOG(0, "I created " << TesselStruct->TrianglesOnBoundary.size() << " triangles with " << TesselStruct->LinesOnBoundary.size() << " lines and " << TesselStruct->PointsOnBoundary.size() << " points.");
|
---|
[3d919e] | 427 |
|
---|
[357fba] | 428 | // 4. Store triangles in tecplot file
|
---|
[d51975] | 429 | StoreTrianglesinFile(mol, TesselStruct, filename, "");
|
---|
[ef0e6d] | 430 |
|
---|
[357fba] | 431 | // free reference lists
|
---|
| 432 | if (BoundaryFreeFlag)
|
---|
| 433 | delete[] (BoundaryPoints);
|
---|
[3d919e] | 434 | };
|
---|
[6ac7ee] | 435 |
|
---|
[d4fa23] | 436 | /** For testing removes one boundary point after another to check for leaks.
|
---|
| 437 | * \param *out output stream for debugging
|
---|
| 438 | * \param *TesselStruct Tesselation containing envelope with boundary points
|
---|
| 439 | * \param *mol molecule
|
---|
| 440 | * \param *filename name of file
|
---|
| 441 | * \return true - all removed, false - something went wrong
|
---|
| 442 | */
|
---|
[e138de] | 443 | bool RemoveAllBoundaryPoints(class Tesselation *&TesselStruct, const molecule * const mol, const char * const filename)
|
---|
[d4fa23] | 444 | {
|
---|
[f67b6e] | 445 | Info FunctionInfo(__func__);
|
---|
[d4fa23] | 446 | int i=0;
|
---|
| 447 | char number[MAXSTRINGSIZE];
|
---|
| 448 |
|
---|
| 449 | if ((TesselStruct == NULL) || (TesselStruct->PointsOnBoundary.empty())) {
|
---|
[47d041] | 450 | ELOG(1, "TesselStruct is empty.");
|
---|
[d4fa23] | 451 | return false;
|
---|
| 452 | }
|
---|
| 453 |
|
---|
| 454 | PointMap::iterator PointRunner;
|
---|
| 455 | while (!TesselStruct->PointsOnBoundary.empty()) {
|
---|
[47d041] | 456 | if (DoLog(1)) {
|
---|
| 457 | std::stringstream output;
|
---|
| 458 | output << "Remaining points are: ";
|
---|
| 459 | for (PointMap::iterator PointSprinter = TesselStruct->PointsOnBoundary.begin(); PointSprinter != TesselStruct->PointsOnBoundary.end(); PointSprinter++)
|
---|
| 460 | output << *(PointSprinter->second) << "\t";
|
---|
| 461 | LOG(1, output.str());
|
---|
| 462 | }
|
---|
[d4fa23] | 463 |
|
---|
| 464 | PointRunner = TesselStruct->PointsOnBoundary.begin();
|
---|
| 465 | // remove point
|
---|
[e138de] | 466 | TesselStruct->RemovePointFromTesselatedSurface(PointRunner->second);
|
---|
[d4fa23] | 467 |
|
---|
| 468 | // store envelope
|
---|
| 469 | sprintf(number, "-%04d", i++);
|
---|
[e138de] | 470 | StoreTrianglesinFile(mol, (const Tesselation *&)TesselStruct, filename, number);
|
---|
[d4fa23] | 471 | }
|
---|
| 472 |
|
---|
| 473 | return true;
|
---|
| 474 | };
|
---|
| 475 |
|
---|
[08ef35] | 476 | /** Creates a convex envelope from a given non-convex one.
|
---|
[093645] | 477 | * -# First step, remove concave spots, i.e. singular "dents"
|
---|
| 478 | * -# We go through all PointsOnBoundary.
|
---|
| 479 | * -# We CheckConvexityCriterion() for all its lines.
|
---|
| 480 | * -# If all its lines are concave, it cannot be on the convex envelope.
|
---|
| 481 | * -# Hence, we remove it and re-create all its triangles from its getCircleOfConnectedPoints()
|
---|
| 482 | * -# We calculate the additional volume.
|
---|
| 483 | * -# We go over all lines until none yields a concavity anymore.
|
---|
| 484 | * -# Second step, remove concave lines, i.e. line-shape "dents"
|
---|
| 485 | * -# We go through all LinesOnBoundary
|
---|
| 486 | * -# We CheckConvexityCriterion()
|
---|
| 487 | * -# If it returns concave, we flip the line in this quadrupel of points (abusing the degeneracy of the tesselation)
|
---|
| 488 | * -# We CheckConvexityCriterion(),
|
---|
| 489 | * -# if it's concave, we continue
|
---|
| 490 | * -# if not, we mark an error and stop
|
---|
[08ef35] | 491 | * Note: This routine - for free - calculates the difference in volume between convex and
|
---|
| 492 | * non-convex envelope, as the former is easy to calculate - VolumeOfConvexEnvelope() - it
|
---|
| 493 | * can be used to compute volumes of arbitrary shapes.
|
---|
| 494 | * \param *out output stream for debugging
|
---|
| 495 | * \param *TesselStruct non-convex envelope, is changed in return!
|
---|
[093645] | 496 | * \param *mol molecule
|
---|
| 497 | * \param *filename name of file
|
---|
[08ef35] | 498 | * \return volume difference between the non- and the created convex envelope
|
---|
| 499 | */
|
---|
[e138de] | 500 | double ConvexizeNonconvexEnvelope(class Tesselation *&TesselStruct, const molecule * const mol, const char * const filename)
|
---|
[08ef35] | 501 | {
|
---|
[f67b6e] | 502 | Info FunctionInfo(__func__);
|
---|
[08ef35] | 503 | double volume = 0;
|
---|
| 504 | class BoundaryPointSet *point = NULL;
|
---|
| 505 | class BoundaryLineSet *line = NULL;
|
---|
[ad37ab] | 506 | bool Concavity = false;
|
---|
[57066a] | 507 | char dummy[MAXSTRINGSIZE];
|
---|
[ad37ab] | 508 | PointMap::iterator PointRunner;
|
---|
| 509 | PointMap::iterator PointAdvance;
|
---|
| 510 | LineMap::iterator LineRunner;
|
---|
| 511 | LineMap::iterator LineAdvance;
|
---|
| 512 | TriangleMap::iterator TriangleRunner;
|
---|
| 513 | TriangleMap::iterator TriangleAdvance;
|
---|
| 514 | int run = 0;
|
---|
[093645] | 515 |
|
---|
| 516 | // check whether there is something to work on
|
---|
[08ef35] | 517 | if (TesselStruct == NULL) {
|
---|
[47d041] | 518 | ELOG(1, "TesselStruct is empty!");
|
---|
[08ef35] | 519 | return volume;
|
---|
| 520 | }
|
---|
| 521 |
|
---|
[093645] | 522 | // First step: RemovePointFromTesselatedSurface
|
---|
[1d9b7aa] | 523 | do {
|
---|
| 524 | Concavity = false;
|
---|
[57066a] | 525 | sprintf(dummy, "-first-%d", run);
|
---|
[e138de] | 526 | //CalculateConcavityPerBoundaryPoint(TesselStruct);
|
---|
| 527 | StoreTrianglesinFile(mol, (const Tesselation *&)TesselStruct, filename, dummy);
|
---|
[57066a] | 528 |
|
---|
[1d9b7aa] | 529 | PointRunner = TesselStruct->PointsOnBoundary.begin();
|
---|
| 530 | PointAdvance = PointRunner; // we need an advanced point, as the PointRunner might get removed
|
---|
| 531 | while (PointRunner != TesselStruct->PointsOnBoundary.end()) {
|
---|
| 532 | PointAdvance++;
|
---|
| 533 | point = PointRunner->second;
|
---|
[47d041] | 534 | LOG(1, "INFO: Current point is " << *point << ".");
|
---|
[1d9b7aa] | 535 | for (LineMap::iterator LineRunner = point->lines.begin(); LineRunner != point->lines.end(); LineRunner++) {
|
---|
| 536 | line = LineRunner->second;
|
---|
[47d041] | 537 | LOG(1, "INFO: Current line of point " << *point << " is " << *line << ".");
|
---|
[e138de] | 538 | if (!line->CheckConvexityCriterion()) {
|
---|
[57066a] | 539 | // remove the point if needed
|
---|
[47d041] | 540 | LOG(1, "... point " << *point << " cannot be on convex envelope.");
|
---|
[e138de] | 541 | volume += TesselStruct->RemovePointFromTesselatedSurface(point);
|
---|
[57066a] | 542 | sprintf(dummy, "-first-%d", ++run);
|
---|
[e138de] | 543 | StoreTrianglesinFile(mol, (const Tesselation *&)TesselStruct, filename, dummy);
|
---|
[57066a] | 544 | Concavity = true;
|
---|
| 545 | break;
|
---|
| 546 | }
|
---|
[1d9b7aa] | 547 | }
|
---|
| 548 | PointRunner = PointAdvance;
|
---|
[093645] | 549 | }
|
---|
| 550 |
|
---|
[57066a] | 551 | sprintf(dummy, "-second-%d", run);
|
---|
[e138de] | 552 | //CalculateConcavityPerBoundaryPoint(TesselStruct);
|
---|
| 553 | StoreTrianglesinFile(mol, (const Tesselation *&)TesselStruct, filename, dummy);
|
---|
[093645] | 554 |
|
---|
[1d9b7aa] | 555 | // second step: PickFarthestofTwoBaselines
|
---|
| 556 | LineRunner = TesselStruct->LinesOnBoundary.begin();
|
---|
| 557 | LineAdvance = LineRunner; // we need an advanced line, as the LineRunner might get removed
|
---|
| 558 | while (LineRunner != TesselStruct->LinesOnBoundary.end()) {
|
---|
| 559 | LineAdvance++;
|
---|
| 560 | line = LineRunner->second;
|
---|
[47d041] | 561 | LOG(1, "INFO: Picking farthest baseline for line is " << *line << ".");
|
---|
[1d9b7aa] | 562 | // take highest of both lines
|
---|
[e138de] | 563 | if (TesselStruct->IsConvexRectangle(line) == NULL) {
|
---|
| 564 | const double tmp = TesselStruct->PickFarthestofTwoBaselines(line);
|
---|
[57066a] | 565 | volume += tmp;
|
---|
[ad37ab] | 566 | if (tmp != 0.) {
|
---|
[e138de] | 567 | TesselStruct->FlipBaseline(line);
|
---|
[57066a] | 568 | Concavity = true;
|
---|
| 569 | }
|
---|
[1d9b7aa] | 570 | }
|
---|
| 571 | LineRunner = LineAdvance;
|
---|
| 572 | }
|
---|
[57066a] | 573 | run++;
|
---|
[1d9b7aa] | 574 | } while (Concavity);
|
---|
[e138de] | 575 | //CalculateConcavityPerBoundaryPoint(TesselStruct);
|
---|
| 576 | //StoreTrianglesinFile(mol, filename, "-third");
|
---|
[093645] | 577 |
|
---|
| 578 | // third step: IsConvexRectangle
|
---|
[7dea7c] | 579 | // LineRunner = TesselStruct->LinesOnBoundary.begin();
|
---|
| 580 | // LineAdvance = LineRunner; // we need an advanced line, as the LineRunner might get removed
|
---|
| 581 | // while (LineRunner != TesselStruct->LinesOnBoundary.end()) {
|
---|
| 582 | // LineAdvance++;
|
---|
| 583 | // line = LineRunner->second;
|
---|
[47d041] | 584 | // LOG(1, "INFO: Current line is " << *line << ".");
|
---|
[7dea7c] | 585 | // //if (LineAdvance != TesselStruct->LinesOnBoundary.end())
|
---|
[47d041] | 586 | // //LOG(1, "INFO: Next line will be " << *(LineAdvance->second) << ".");
|
---|
[7dea7c] | 587 | // if (!line->CheckConvexityCriterion(out)) {
|
---|
[47d041] | 588 | // LOG(1, "INFO: ... line " << *line << " is concave, flipping it.");
|
---|
[7dea7c] | 589 | //
|
---|
| 590 | // // take highest of both lines
|
---|
[e138de] | 591 | // point = TesselStruct->IsConvexRectangle(line);
|
---|
[7dea7c] | 592 | // if (point != NULL)
|
---|
[e138de] | 593 | // volume += TesselStruct->RemovePointFromTesselatedSurface(point);
|
---|
[7dea7c] | 594 | // }
|
---|
| 595 | // LineRunner = LineAdvance;
|
---|
| 596 | // }
|
---|
[093645] | 597 |
|
---|
[e138de] | 598 | CalculateConcavityPerBoundaryPoint(TesselStruct);
|
---|
| 599 | StoreTrianglesinFile(mol, (const Tesselation *&)TesselStruct, filename, "");
|
---|
[0077b5] | 600 |
|
---|
| 601 | // end
|
---|
[47d041] | 602 | LOG(0, "Volume is " << volume << ".");
|
---|
[0077b5] | 603 | return volume;
|
---|
| 604 | };
|
---|
| 605 |
|
---|
[6ac7ee] | 606 |
|
---|
[357fba] | 607 | /** Determines the volume of a cluster.
|
---|
| 608 | * Determines first the convex envelope, then tesselates it and calculates its volume.
|
---|
[6ac7ee] | 609 | * \param *out output stream for debugging
|
---|
[357fba] | 610 | * \param *TesselStruct Tesselation filled with points, lines and triangles on boundary on return
|
---|
| 611 | * \param *configuration needed for path to store convex envelope file
|
---|
| 612 | * \return determined volume of the cluster in cubed config:GetIsAngstroem()
|
---|
[3d919e] | 613 | */
|
---|
[e138de] | 614 | double VolumeOfConvexEnvelope(class Tesselation *TesselStruct, class config *configuration)
|
---|
[357fba] | 615 | {
|
---|
[f67b6e] | 616 | Info FunctionInfo(__func__);
|
---|
[357fba] | 617 | bool IsAngstroem = configuration->GetIsAngstroem();
|
---|
| 618 | double volume = 0.;
|
---|
[ad37ab] | 619 | Vector x;
|
---|
| 620 | Vector y;
|
---|
[6ac7ee] | 621 |
|
---|
[357fba] | 622 | // 6a. Every triangle forms a pyramid with the center of gravity as its peak, sum up the volumes
|
---|
| 623 | for (TriangleMap::iterator runner = TesselStruct->TrianglesOnBoundary.begin(); runner != TesselStruct->TrianglesOnBoundary.end(); runner++)
|
---|
| 624 | { // go through every triangle, calculate volume of its pyramid with CoG as peak
|
---|
[8f215d] | 625 | x = runner->second->getEndpoint(0) - runner->second->getEndpoint(1);
|
---|
| 626 | y = runner->second->getEndpoint(0) - runner->second->getEndpoint(2);
|
---|
| 627 | const double a = x.Norm();
|
---|
| 628 | const double b = y.Norm();
|
---|
| 629 | const double c = runner->second->getEndpoint(2).distance(runner->second->getEndpoint(1));
|
---|
[ad37ab] | 630 | const double G = sqrt(((a + b + c) * (a + b + c) - 2 * (a * a + b * b + c * c)) / 16.); // area of tesselated triangle
|
---|
[8f215d] | 631 | x = runner->second->getPlane().getNormal();
|
---|
| 632 | x.Scale(runner->second->getEndpoint(1).ScalarProduct(x));
|
---|
[ad37ab] | 633 | const double h = x.Norm(); // distance of CoG to triangle
|
---|
| 634 | const double PyramidVolume = (1. / 3.) * G * h; // this formula holds for _all_ pyramids (independent of n-edge base or (not) centered peak)
|
---|
[47d041] | 635 | LOG(1, "INFO: Area of triangle is " << setprecision(10) << G << " "
|
---|
[357fba] | 636 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^2, height is "
|
---|
| 637 | << h << " and the volume is " << PyramidVolume << " "
|
---|
[47d041] | 638 | << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[357fba] | 639 | volume += PyramidVolume;
|
---|
[3d919e] | 640 | }
|
---|
[47d041] | 641 | LOG(0, "RESULT: The summed volume is " << setprecision(6)
|
---|
| 642 | << volume << " " << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[6ac7ee] | 643 |
|
---|
[357fba] | 644 | return volume;
|
---|
[7dea7c] | 645 | };
|
---|
| 646 |
|
---|
| 647 | /** Stores triangles to file.
|
---|
| 648 | * \param *out output stream for debugging
|
---|
| 649 | * \param *mol molecule with atoms and bonds
|
---|
[71b20e] | 650 | * \param *TesselStruct Tesselation with boundary triangles
|
---|
[7dea7c] | 651 | * \param *filename prefix of filename
|
---|
| 652 | * \param *extraSuffix intermediate suffix
|
---|
| 653 | */
|
---|
[71b20e] | 654 | void StoreTrianglesinFile(const molecule * const mol, const Tesselation * const TesselStruct, const char *filename, const char *extraSuffix)
|
---|
[7dea7c] | 655 | {
|
---|
[f67b6e] | 656 | Info FunctionInfo(__func__);
|
---|
[caa06ef] | 657 | PointCloudAdaptor< molecule > cloud(const_cast<molecule *>(mol), mol->name);
|
---|
[7dea7c] | 658 | // 4. Store triangles in tecplot file
|
---|
| 659 | if (filename != NULL) {
|
---|
| 660 | if (DoTecplotOutput) {
|
---|
| 661 | string OutputName(filename);
|
---|
| 662 | OutputName.append(extraSuffix);
|
---|
| 663 | OutputName.append(TecplotSuffix);
|
---|
| 664 | ofstream *tecplot = new ofstream(OutputName.c_str());
|
---|
[34c43a] | 665 | WriteTecplotFile(tecplot, TesselStruct, cloud, -1);
|
---|
[7dea7c] | 666 | tecplot->close();
|
---|
| 667 | delete(tecplot);
|
---|
| 668 | }
|
---|
| 669 | if (DoRaster3DOutput) {
|
---|
| 670 | string OutputName(filename);
|
---|
| 671 | OutputName.append(extraSuffix);
|
---|
| 672 | OutputName.append(Raster3DSuffix);
|
---|
| 673 | ofstream *rasterplot = new ofstream(OutputName.c_str());
|
---|
[34c43a] | 674 | WriteRaster3dFile(rasterplot, TesselStruct, cloud);
|
---|
[7dea7c] | 675 | rasterplot->close();
|
---|
| 676 | delete(rasterplot);
|
---|
| 677 | }
|
---|
| 678 | }
|
---|
| 679 | };
|
---|
[03648b] | 680 |
|
---|
[357fba] | 681 | /** Creates multiples of the by \a *mol given cluster and suspends them in water with a given final density.
|
---|
| 682 | * We get cluster volume by VolumeOfConvexEnvelope() and its diameters by GetDiametersOfCluster()
|
---|
[accebe] | 683 | * TODO: Here, we need a VolumeOfGeneralEnvelope (i.e. non-convex one)
|
---|
[357fba] | 684 | * \param *out output stream for debugging
|
---|
| 685 | * \param *configuration needed for path to store convex envelope file
|
---|
| 686 | * \param *mol molecule structure representing the cluster
|
---|
[776b64] | 687 | * \param *&TesselStruct Tesselation structure with triangles on return
|
---|
[357fba] | 688 | * \param ClusterVolume guesstimated cluster volume, if equal 0 we used VolumeOfConvexEnvelope() instead.
|
---|
| 689 | * \param celldensity desired average density in final cell
|
---|
[8c54a3] | 690 | */
|
---|
[e138de] | 691 | void PrepareClustersinWater(config *configuration, molecule *mol, double ClusterVolume, double celldensity)
|
---|
[357fba] | 692 | {
|
---|
[f67b6e] | 693 | Info FunctionInfo(__func__);
|
---|
[fa649a] | 694 | bool IsAngstroem = true;
|
---|
[ad37ab] | 695 | double *GreatestDiameter = NULL;
|
---|
| 696 | Boundaries *BoundaryPoints = NULL;
|
---|
| 697 | class Tesselation *TesselStruct = NULL;
|
---|
| 698 | Vector BoxLengths;
|
---|
| 699 | int repetition[NDIM] = { 1, 1, 1 };
|
---|
| 700 | int TotalNoClusters = 1;
|
---|
| 701 | double totalmass = 0.;
|
---|
| 702 | double clustervolume = 0.;
|
---|
| 703 | double cellvolume = 0.;
|
---|
| 704 |
|
---|
[6e5084] | 705 | // transform to PAS by Action
|
---|
| 706 | Vector MainAxis(0.,0.,1.);
|
---|
[1f91f4] | 707 | mol->RotateToPrincipalAxisSystem(MainAxis);
|
---|
[3d919e] | 708 |
|
---|
[ad37ab] | 709 | IsAngstroem = configuration->GetIsAngstroem();
|
---|
[e138de] | 710 | BoundaryPoints = GetBoundaryPoints(mol, TesselStruct);
|
---|
[bdc91e] | 711 | GreatestDiameter = GetDiametersOfCluster(BoundaryPoints, mol, TesselStruct, IsAngstroem);
|
---|
[caa06ef] | 712 | PointCloudAdaptor< molecule > cloud(mol, mol->name);
|
---|
[6bd7e0] | 713 | LinkedCell_deprecated *LCList = new LinkedCell_deprecated(cloud, 10.);
|
---|
| 714 | FindConvexBorder(mol, BoundaryPoints, TesselStruct, (const LinkedCell_deprecated *&)LCList, NULL);
|
---|
[accebe] | 715 | delete (LCList);
|
---|
[bdc91e] | 716 | delete[] BoundaryPoints;
|
---|
[accebe] | 717 |
|
---|
[ad37ab] | 718 |
|
---|
| 719 | // some preparations beforehand
|
---|
[357fba] | 720 | if (ClusterVolume == 0)
|
---|
[e138de] | 721 | clustervolume = VolumeOfConvexEnvelope(TesselStruct, configuration);
|
---|
[357fba] | 722 | else
|
---|
| 723 | clustervolume = ClusterVolume;
|
---|
[ad37ab] | 724 |
|
---|
[bdc91e] | 725 | delete TesselStruct;
|
---|
| 726 |
|
---|
[357fba] | 727 | for (int i = 0; i < NDIM; i++)
|
---|
| 728 | TotalNoClusters *= repetition[i];
|
---|
[8c54a3] | 729 |
|
---|
[357fba] | 730 | // sum up the atomic masses
|
---|
[9879f6] | 731 | for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[83f176] | 732 | totalmass += (*iter)->getType()->getMass();
|
---|
[ad37ab] | 733 | }
|
---|
[47d041] | 734 | LOG(0, "RESULT: The summed mass is " << setprecision(10) << totalmass << " atomicmassunit.");
|
---|
| 735 | LOG(0, "RESULT: The average density is " << setprecision(10) << totalmass / clustervolume << " atomicmassunit/" << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[8c54a3] | 736 |
|
---|
[357fba] | 737 | // solve cubic polynomial
|
---|
[47d041] | 738 | LOG(1, "Solving equidistant suspension in water problem ...");
|
---|
[357fba] | 739 | if (IsAngstroem)
|
---|
[ad37ab] | 740 | cellvolume = (TotalNoClusters * totalmass / SOLVENTDENSITY_A - (totalmass / clustervolume)) / (celldensity - 1);
|
---|
[357fba] | 741 | else
|
---|
[ad37ab] | 742 | cellvolume = (TotalNoClusters * totalmass / SOLVENTDENSITY_a0 - (totalmass / clustervolume)) / (celldensity - 1);
|
---|
[47d041] | 743 | LOG(1, "Cellvolume needed for a density of " << celldensity << " g/cm^3 is " << cellvolume << " " << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[ad37ab] | 744 |
|
---|
| 745 | double minimumvolume = TotalNoClusters * (GreatestDiameter[0] * GreatestDiameter[1] * GreatestDiameter[2]);
|
---|
[47d041] | 746 | LOG(1, "Minimum volume of the convex envelope contained in a rectangular box is " << minimumvolume << " atomicmassunit/" << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[ad37ab] | 747 | if (minimumvolume > cellvolume) {
|
---|
[47d041] | 748 | ELOG(1, "the containing box already has a greater volume than the envisaged cell volume!");
|
---|
| 749 | LOG(0, "Setting Box dimensions to minimum possible, the greatest diameters.");
|
---|
[ad37ab] | 750 | for (int i = 0; i < NDIM; i++)
|
---|
[0a4f7f] | 751 | BoxLengths[i] = GreatestDiameter[i];
|
---|
[e138de] | 752 | mol->CenterEdge(&BoxLengths);
|
---|
[ad37ab] | 753 | } else {
|
---|
[0a4f7f] | 754 | BoxLengths[0] = (repetition[0] * GreatestDiameter[0] + repetition[1] * GreatestDiameter[1] + repetition[2] * GreatestDiameter[2]);
|
---|
| 755 | BoxLengths[1] = (repetition[0] * repetition[1] * GreatestDiameter[0] * GreatestDiameter[1] + repetition[0] * repetition[2] * GreatestDiameter[0] * GreatestDiameter[2] + repetition[1] * repetition[2] * GreatestDiameter[1] * GreatestDiameter[2]);
|
---|
| 756 | BoxLengths[2] = minimumvolume - cellvolume;
|
---|
[ad37ab] | 757 | double x0 = 0.;
|
---|
| 758 | double x1 = 0.;
|
---|
| 759 | double x2 = 0.;
|
---|
[0a4f7f] | 760 | if (gsl_poly_solve_cubic(BoxLengths[0], BoxLengths[1], BoxLengths[2], &x0, &x1, &x2) == 1) // either 1 or 3 on return
|
---|
[47d041] | 761 | LOG(0, "RESULT: The resulting spacing is: " << x0 << " .");
|
---|
[ad37ab] | 762 | else {
|
---|
[47d041] | 763 | LOG(0, "RESULT: The resulting spacings are: " << x0 << " and " << x1 << " and " << x2 << " .");
|
---|
[ad37ab] | 764 | x0 = x2; // sorted in ascending order
|
---|
[357fba] | 765 | }
|
---|
[8c54a3] | 766 |
|
---|
[ad37ab] | 767 | cellvolume = 1.;
|
---|
| 768 | for (int i = 0; i < NDIM; i++) {
|
---|
[0a4f7f] | 769 | BoxLengths[i] = repetition[i] * (x0 + GreatestDiameter[i]);
|
---|
| 770 | cellvolume *= BoxLengths[i];
|
---|
[8c54a3] | 771 | }
|
---|
[ad37ab] | 772 |
|
---|
| 773 | // set new box dimensions
|
---|
[47d041] | 774 | LOG(0, "Translating to box with these boundaries.");
|
---|
[ad37ab] | 775 | mol->SetBoxDimension(&BoxLengths);
|
---|
[e138de] | 776 | mol->CenterInBox();
|
---|
[ad37ab] | 777 | }
|
---|
[bdc91e] | 778 | delete GreatestDiameter;
|
---|
[357fba] | 779 | // update Box of atoms by boundary
|
---|
| 780 | mol->SetBoxDimension(&BoxLengths);
|
---|
[47d041] | 781 | LOG(0, "RESULT: The resulting cell dimensions are: " << BoxLengths[0] << " and " << BoxLengths[1] << " and " << BoxLengths[2] << " with total volume of " << cellvolume << " " << (IsAngstroem ? "angstrom" : "atomiclength") << "^3.");
|
---|
[ad37ab] | 782 | };
|
---|
[8c54a3] | 783 |
|
---|
| 784 |
|
---|
[eee966] | 785 | /** Fills the empty space around other molecules' surface of the simulation box with a filler.
|
---|
[357fba] | 786 | * \param *out output stream for debugging
|
---|
| 787 | * \param *List list of molecules already present in box
|
---|
| 788 | * \param *TesselStruct contains tesselated surface
|
---|
| 789 | * \param *filler molecule which the box is to be filled with
|
---|
| 790 | * \param configuration contains box dimensions
|
---|
[775d133] | 791 | * \param MaxDistance fills in molecules only up to this distance (set to -1 if whole of the domain)
|
---|
[357fba] | 792 | * \param distance[NDIM] distance between filling molecules in each direction
|
---|
[9473f6] | 793 | * \param boundary length of boundary zone between molecule and filling mollecules
|
---|
[71b20e] | 794 | * \param epsilon distance to surface which is not filled
|
---|
[357fba] | 795 | * \param RandAtomDisplacement maximum distance for random displacement per atom
|
---|
| 796 | * \param RandMolDisplacement maximum distance for random displacement per filler molecule
|
---|
| 797 | * \param DoRandomRotation true - do random rotiations, false - don't
|
---|
[6ac7ee] | 798 | */
|
---|
[7a51be] | 799 | void FillBoxWithMolecule(MoleculeListClass *List, molecule *filler, config &configuration, const double MaxDistance, const double distance[NDIM], const double boundary, const double RandomAtomDisplacement, const double RandomMolDisplacement, const bool DoRandomRotation)
|
---|
[6ac7ee] | 800 | {
|
---|
[f67b6e] | 801 | Info FunctionInfo(__func__);
|
---|
[23b547] | 802 | molecule *Filling = World::getInstance().createMolecule();
|
---|
[357fba] | 803 | Vector CurrentPosition;
|
---|
| 804 | int N[NDIM];
|
---|
| 805 | int n[NDIM];
|
---|
[cca9ef] | 806 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
| 807 | RealSpaceMatrix Rotations;
|
---|
| 808 | const RealSpaceMatrix &MInverse = World::getInstance().getDomain().getMinv();
|
---|
[357fba] | 809 | Vector AtomTranslations;
|
---|
| 810 | Vector FillerTranslations;
|
---|
| 811 | Vector FillerDistance;
|
---|
[30d9e7] | 812 | Vector Inserter;
|
---|
[357fba] | 813 | double FillIt = false;
|
---|
| 814 | bond *Binder = NULL;
|
---|
[ad37ab] | 815 | double phi[NDIM];
|
---|
[30d9e7] | 816 | map<molecule *, Tesselation *> TesselStruct;
|
---|
[6bd7e0] | 817 | map<molecule *, LinkedCell_deprecated *> LCList;
|
---|
[ef0e6d] | 818 |
|
---|
[c5805a] | 819 | for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++)
|
---|
[a7b761b] | 820 | if ((*ListRunner)->getAtomCount() > 0) {
|
---|
[47d041] | 821 | LOG(1, "Pre-creating linked cell lists for molecule " << *ListRunner << ".");
|
---|
[caa06ef] | 822 | PointCloudAdaptor< molecule > cloud(*ListRunner, (*ListRunner)->name);
|
---|
[6bd7e0] | 823 | LCList[(*ListRunner)] = new LinkedCell_deprecated(cloud, 10.); // get linked cell list
|
---|
[47d041] | 824 | LOG(1, "Pre-creating tesselation for molecule " << *ListRunner << ".");
|
---|
[c5805a] | 825 | TesselStruct[(*ListRunner)] = NULL;
|
---|
[6bd7e0] | 826 | FindNonConvexBorder((*ListRunner), TesselStruct[(*ListRunner)], (const LinkedCell_deprecated *&)LCList[(*ListRunner)], 5., NULL);
|
---|
[c5805a] | 827 | }
|
---|
[8c54a3] | 828 |
|
---|
[357fba] | 829 | // Center filler at origin
|
---|
[c5805a] | 830 | filler->CenterEdge(&Inserter);
|
---|
[e0aee2b] | 831 | const int FillerCount = filler->getAtomCount();
|
---|
[47d041] | 832 | LOG(2, "INFO: Filler molecule has the following bonds:");
|
---|
[9d83b6] | 833 | for(molecule::iterator AtomRunner = filler->begin(); AtomRunner != filler->end(); ++AtomRunner) {
|
---|
| 834 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
| 835 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 836 | BondRunner != ListOfBonds.end();
|
---|
| 837 | ++BondRunner) {
|
---|
[e08c46] | 838 | if ((*BondRunner)->leftatom == *AtomRunner)
|
---|
[47d041] | 839 | LOG(2, " " << *(*BondRunner));
|
---|
[9d83b6] | 840 | }
|
---|
| 841 | }
|
---|
[8c54a3] | 842 |
|
---|
[e0aee2b] | 843 | atom * CopyAtoms[FillerCount];
|
---|
[ef0e6d] | 844 |
|
---|
[357fba] | 845 | // calculate filler grid in [0,1]^3
|
---|
[5108e1] | 846 | FillerDistance = MInverse * Vector(distance[0], distance[1], distance[2]);
|
---|
[71b20e] | 847 | for(int i=0;i<NDIM;i++)
|
---|
[8cbb97] | 848 | N[i] = (int) ceil(1./FillerDistance[i]);
|
---|
[47d041] | 849 | LOG(1, "INFO: Grid steps are " << N[0] << ", " << N[1] << ", " << N[2] << ".");
|
---|
[8c54a3] | 850 |
|
---|
[d6eb80] | 851 | // initialize seed of random number generator to current time
|
---|
[a5028f] | 852 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 853 | const double rng_min = random.min();
|
---|
| 854 | const double rng_max = random.max();
|
---|
| 855 | //srand ( time(NULL) );
|
---|
[d6eb80] | 856 |
|
---|
[357fba] | 857 | // go over [0,1]^3 filler grid
|
---|
| 858 | for (n[0] = 0; n[0] < N[0]; n[0]++)
|
---|
| 859 | for (n[1] = 0; n[1] < N[1]; n[1]++)
|
---|
| 860 | for (n[2] = 0; n[2] < N[2]; n[2]++) {
|
---|
| 861 | // calculate position of current grid vector in untransformed box
|
---|
[5108e1] | 862 | CurrentPosition = M * Vector((double)n[0]/(double)N[0], (double)n[1]/(double)N[1], (double)n[2]/(double)N[2]);
|
---|
[30d9e7] | 863 | // create molecule random translation vector ...
|
---|
| 864 | for (int i=0;i<NDIM;i++)
|
---|
[a5028f] | 865 | FillerTranslations[i] = RandomMolDisplacement*(random()/((rng_max-rng_min)/2.) - 1.);
|
---|
[47d041] | 866 | LOG(2, "INFO: Current Position is " << CurrentPosition << "+" << FillerTranslations << ".");
|
---|
[8c54a3] | 867 |
|
---|
[30d9e7] | 868 | // go through all atoms
|
---|
[e0aee2b] | 869 | for (int i=0;i<FillerCount;i++)
|
---|
[c5805a] | 870 | CopyAtoms[i] = NULL;
|
---|
[0b15bb] | 871 |
|
---|
| 872 | // have same rotation angles for all molecule's atoms
|
---|
| 873 | if (DoRandomRotation)
|
---|
| 874 | for (int i=0;i<NDIM;i++)
|
---|
[a5028f] | 875 | phi[i] = (random()/(rng_max-rng_min))*(2.*M_PI);
|
---|
[0b15bb] | 876 |
|
---|
[59fff1] | 877 | // atom::clone is not const member function, hence we need iterator here
|
---|
| 878 | for(molecule::iterator iter = filler->begin(); iter !=filler->end();++iter){
|
---|
[8c54a3] | 879 |
|
---|
[30d9e7] | 880 | // create atomic random translation vector ...
|
---|
[357fba] | 881 | for (int i=0;i<NDIM;i++)
|
---|
[a5028f] | 882 | AtomTranslations[i] = RandomAtomDisplacement*(random()/((rng_max-rng_min)/2.) - 1.);
|
---|
[8c54a3] | 883 |
|
---|
[30d9e7] | 884 | // ... and rotation matrix
|
---|
| 885 | if (DoRandomRotation) {
|
---|
[a679d1] | 886 | Rotations.set(0,0, cos(phi[0]) *cos(phi[2]) + (sin(phi[0])*sin(phi[1])*sin(phi[2])));
|
---|
| 887 | Rotations.set(0,1, sin(phi[0]) *cos(phi[2]) - (cos(phi[0])*sin(phi[1])*sin(phi[2])));
|
---|
| 888 | Rotations.set(0,2, cos(phi[1])*sin(phi[2]) );
|
---|
| 889 | Rotations.set(1,0, -sin(phi[0])*cos(phi[1]) );
|
---|
| 890 | Rotations.set(1,1, cos(phi[0])*cos(phi[1]) );
|
---|
| 891 | Rotations.set(1,2, sin(phi[1]) );
|
---|
| 892 | Rotations.set(2,0, -cos(phi[0]) *sin(phi[2]) + (sin(phi[0])*sin(phi[1])*cos(phi[2])));
|
---|
| 893 | Rotations.set(2,1, -sin(phi[0]) *sin(phi[2]) - (cos(phi[0])*sin(phi[1])*cos(phi[2])));
|
---|
| 894 | Rotations.set(2,2, cos(phi[1])*cos(phi[2]) );
|
---|
[30d9e7] | 895 | }
|
---|
[8c54a3] | 896 |
|
---|
[30d9e7] | 897 | // ... and put at new position
|
---|
[d74077] | 898 | Inserter = (*iter)->getPosition();
|
---|
[30d9e7] | 899 | if (DoRandomRotation)
|
---|
[5108e1] | 900 | Inserter *= Rotations;
|
---|
[8cbb97] | 901 | Inserter += AtomTranslations + FillerTranslations + CurrentPosition;
|
---|
[30d9e7] | 902 |
|
---|
| 903 | // check whether inserter is inside box
|
---|
[5108e1] | 904 | Inserter *= MInverse;
|
---|
[30d9e7] | 905 | FillIt = true;
|
---|
[357fba] | 906 | for (int i=0;i<NDIM;i++)
|
---|
[8cbb97] | 907 | FillIt = FillIt && (Inserter[i] >= -MYEPSILON) && ((Inserter[i]-1.) <= MYEPSILON);
|
---|
[5108e1] | 908 | Inserter *= M;
|
---|
[30d9e7] | 909 |
|
---|
| 910 | // Check whether point is in- or outside
|
---|
| 911 | for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++) {
|
---|
| 912 | // get linked cell list
|
---|
[c5805a] | 913 | if (TesselStruct[(*ListRunner)] != NULL) {
|
---|
[8db598] | 914 | const double distance = (TesselStruct[(*ListRunner)]->GetDistanceToSurface(Inserter, LCList[(*ListRunner)]));
|
---|
| 915 | FillIt = FillIt && (distance > boundary) && ((MaxDistance < 0) || (MaxDistance > distance));
|
---|
[30d9e7] | 916 | }
|
---|
| 917 | }
|
---|
| 918 | // insert into Filling
|
---|
| 919 | if (FillIt) {
|
---|
[47d041] | 920 | LOG(1, "INFO: Position at " << Inserter << " is outer point.");
|
---|
[357fba] | 921 | // copy atom ...
|
---|
[735b1c] | 922 | CopyAtoms[(*iter)->getNr()] = (*iter)->clone();
|
---|
| 923 | (*CopyAtoms[(*iter)->getNr()]).setPosition(Inserter);
|
---|
| 924 | Filling->AddAtom(CopyAtoms[(*iter)->getNr()]);
|
---|
[47d041] | 925 | LOG(1, "Filling atom " << **iter << ", translated to " << AtomTranslations << ", at final position is " << (CopyAtoms[(*iter)->getNr()]->getPosition()) << ".");
|
---|
[30d9e7] | 926 | } else {
|
---|
[47d041] | 927 | LOG(1, "INFO: Position at " << Inserter << " is inner point, within boundary or outside of MaxDistance.");
|
---|
[735b1c] | 928 | CopyAtoms[(*iter)->getNr()] = NULL;
|
---|
[c5805a] | 929 | continue;
|
---|
[357fba] | 930 | }
|
---|
[a837d0] | 931 | }
|
---|
| 932 | // go through all bonds and add as well
|
---|
[9d83b6] | 933 | for(molecule::iterator AtomRunner = filler->begin(); AtomRunner != filler->end(); ++AtomRunner) {
|
---|
| 934 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
| 935 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 936 | BondRunner != ListOfBonds.end();
|
---|
| 937 | ++BondRunner)
|
---|
[e08c46] | 938 | if ((*BondRunner)->leftatom == *AtomRunner) {
|
---|
| 939 | Binder = (*BondRunner);
|
---|
[735b1c] | 940 | if ((CopyAtoms[Binder->leftatom->getNr()] != NULL) && (CopyAtoms[Binder->rightatom->getNr()] != NULL)) {
|
---|
[47d041] | 941 | LOG(3, "Adding Bond between " << *CopyAtoms[Binder->leftatom->getNr()] << " and " << *CopyAtoms[Binder->rightatom->getNr()]<< ".");
|
---|
[735b1c] | 942 | Filling->AddBond(CopyAtoms[Binder->leftatom->getNr()], CopyAtoms[Binder->rightatom->getNr()], Binder->BondDegree);
|
---|
[e08c46] | 943 | }
|
---|
| 944 | }
|
---|
[9d83b6] | 945 | }
|
---|
[8c54a3] | 946 | }
|
---|
[bdc91e] | 947 | for (MoleculeList::iterator ListRunner = List->ListOfMolecules.begin(); ListRunner != List->ListOfMolecules.end(); ListRunner++) {
|
---|
| 948 | delete LCList[*ListRunner];
|
---|
| 949 | delete TesselStruct[(*ListRunner)];
|
---|
| 950 | }
|
---|
[3d919e] | 951 | };
|
---|
[8c54a3] | 952 |
|
---|
[66fd49] | 953 | /** Rotates given molecule \a Filling and moves its atoms according to given
|
---|
| 954 | * \a RandomAtomDisplacement.
|
---|
| 955 | *
|
---|
| 956 | * Note that for rotation to be sensible, the molecule should be centered at
|
---|
| 957 | * the origin. This is not done here!
|
---|
| 958 | *
|
---|
| 959 | * \param &Filling molecule whose atoms to displace
|
---|
| 960 | * \param RandomAtomDisplacement magnitude of random displacement
|
---|
| 961 | * \param &Rotations 3D rotation matrix (or unity if no rotation desired)
|
---|
| 962 | */
|
---|
| 963 | void RandomizeMoleculePositions(
|
---|
| 964 | molecule *&Filling,
|
---|
| 965 | double RandomAtomDisplacement,
|
---|
[a5028f] | 966 | RealSpaceMatrix &Rotations,
|
---|
| 967 | RandomNumberGenerator &random
|
---|
[66fd49] | 968 | )
|
---|
| 969 | {
|
---|
[a5028f] | 970 | const double rng_min = random.min();
|
---|
| 971 | const double rng_max = random.max();
|
---|
| 972 |
|
---|
[66fd49] | 973 | Vector AtomTranslations;
|
---|
| 974 | for(molecule::iterator miter = Filling->begin(); miter != Filling->end(); ++miter) {
|
---|
| 975 | Vector temp = (*miter)->getPosition();
|
---|
| 976 | temp *= Rotations;
|
---|
| 977 | (*miter)->setPosition(temp);
|
---|
| 978 | // create atomic random translation vector ...
|
---|
| 979 | for (int i=0;i<NDIM;i++)
|
---|
[a5028f] | 980 | AtomTranslations[i] = RandomAtomDisplacement*(random()/((rng_max-rng_min)/2.) - 1.);
|
---|
[66fd49] | 981 | (*miter)->setPosition((*miter)->getPosition() + AtomTranslations);
|
---|
| 982 | }
|
---|
| 983 | }
|
---|
| 984 |
|
---|
| 985 | /** Removes all atoms of a molecule outside.
|
---|
| 986 | *
|
---|
| 987 | * If the molecule is empty, it is removed as well.
|
---|
| 988 | *
|
---|
| 989 | * @param Filling molecule whose atoms to check, removed if eventually left
|
---|
| 990 | * empty.
|
---|
[9df680] | 991 | * @return true - atoms had to be removed, false - no atoms have been removed
|
---|
[66fd49] | 992 | */
|
---|
[9df680] | 993 | bool RemoveAtomsOutsideDomain(molecule *&Filling)
|
---|
[66fd49] | 994 | {
|
---|
[9df680] | 995 | bool status = false;
|
---|
[66fd49] | 996 | Box &Domain = World::getInstance().getDomain();
|
---|
| 997 | // check if all is still inside domain
|
---|
| 998 | for(molecule::iterator miter = Filling->begin(); miter != Filling->end(); ) {
|
---|
| 999 | // check whether each atom is inside box
|
---|
| 1000 | if (!Domain.isInside((*miter)->getPosition())) {
|
---|
[9df680] | 1001 | status = true;
|
---|
[66fd49] | 1002 | atom *Walker = *miter;
|
---|
| 1003 | ++miter;
|
---|
| 1004 | World::getInstance().destroyAtom(Walker);
|
---|
| 1005 | } else {
|
---|
| 1006 | ++miter;
|
---|
| 1007 | }
|
---|
| 1008 | }
|
---|
| 1009 | if (Filling->empty()) {
|
---|
[47d041] | 1010 | LOG(0, "Removing molecule " << Filling->getName() << ", all atoms have been removed.");
|
---|
[66fd49] | 1011 | World::getInstance().destroyMolecule(Filling);
|
---|
[2e352f] | 1012 | Filling = NULL;
|
---|
[66fd49] | 1013 | }
|
---|
[9df680] | 1014 | return status;
|
---|
[66fd49] | 1015 | }
|
---|
| 1016 |
|
---|
| 1017 | /** Checks whether there are no atoms inside a sphere around \a CurrentPosition
|
---|
| 1018 | * except those atoms present in \a *filler.
|
---|
[6bd7e0] | 1019 | * If filler is NULL, then we just call LinkedCell_deprecated::GetPointsInsideSphere() and
|
---|
[66fd49] | 1020 | * check whether the return list is empty.
|
---|
| 1021 | * @param *filler
|
---|
| 1022 | * @param boundary
|
---|
| 1023 | * @param CurrentPosition
|
---|
| 1024 | */
|
---|
| 1025 | bool isSpaceAroundPointVoid(
|
---|
[6bd7e0] | 1026 | LinkedCell_deprecated *LC,
|
---|
[66fd49] | 1027 | molecule *filler,
|
---|
| 1028 | const double boundary,
|
---|
| 1029 | Vector &CurrentPosition)
|
---|
| 1030 | {
|
---|
| 1031 | size_t compareTo = 0;
|
---|
[34c43a] | 1032 | TesselPointSTLList* liste = LC->GetPointsInsideSphere(boundary == 0. ? MYEPSILON : boundary, &CurrentPosition);
|
---|
[66fd49] | 1033 | if (filler != NULL) {
|
---|
[34c43a] | 1034 | for (TesselPointSTLList::const_iterator iter = liste->begin();
|
---|
[66fd49] | 1035 | iter != liste->end();
|
---|
| 1036 | ++iter) {
|
---|
| 1037 | for (molecule::iterator miter = filler->begin();
|
---|
| 1038 | miter != filler->end();
|
---|
| 1039 | ++miter) {
|
---|
| 1040 | if (*iter == *miter)
|
---|
| 1041 | ++compareTo;
|
---|
| 1042 | }
|
---|
| 1043 | }
|
---|
| 1044 | }
|
---|
| 1045 | const bool result = (liste->size() == compareTo);
|
---|
| 1046 | if (!result) {
|
---|
[47d041] | 1047 | LOG(0, "Skipping because of the following atoms:");
|
---|
[34c43a] | 1048 | for (TesselPointSTLList::const_iterator iter = liste->begin();
|
---|
[66fd49] | 1049 | iter != liste->end();
|
---|
| 1050 | ++iter) {
|
---|
[47d041] | 1051 | LOG(0, **iter);
|
---|
[66fd49] | 1052 | }
|
---|
| 1053 | }
|
---|
| 1054 | delete(liste);
|
---|
| 1055 | return result;
|
---|
| 1056 | }
|
---|
| 1057 |
|
---|
[b81f1c] | 1058 | /** Sets given 3x3 matrix to a random rotation matrix.
|
---|
| 1059 | *
|
---|
| 1060 | * @param a matrix to set
|
---|
| 1061 | */
|
---|
| 1062 | inline void setRandomRotation(RealSpaceMatrix &a)
|
---|
| 1063 | {
|
---|
| 1064 | double phi[NDIM];
|
---|
| 1065 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 1066 | const double rng_min = random.min();
|
---|
| 1067 | const double rng_max = random.max();
|
---|
| 1068 |
|
---|
| 1069 | for (int i=0;i<NDIM;i++) {
|
---|
| 1070 | phi[i] = (random()/(rng_max-rng_min))*(2.*M_PI);
|
---|
| 1071 | std::cout << "Random angle is " << phi[i] << std::endl;
|
---|
| 1072 | }
|
---|
| 1073 |
|
---|
[8766e72] | 1074 | a.setRotation(phi);
|
---|
[b81f1c] | 1075 | }
|
---|
| 1076 |
|
---|
[66fd49] | 1077 | /** Fills the empty space of the simulation box with water.
|
---|
[eee966] | 1078 | * \param *filler molecule which the box is to be filled with
|
---|
| 1079 | * \param configuration contains box dimensions
|
---|
| 1080 | * \param distance[NDIM] distance between filling molecules in each direction
|
---|
[66fd49] | 1081 | * \param boundary length of boundary zone between molecule and filling molecules
|
---|
[eee966] | 1082 | * \param RandAtomDisplacement maximum distance for random displacement per atom
|
---|
| 1083 | * \param RandMolDisplacement maximum distance for random displacement per filler molecule
|
---|
[66fd49] | 1084 | * \param MinDistance minimum distance to boundary of domain and present molecules
|
---|
| 1085 | * \param DoRandomRotation true - do random rotations, false - don't
|
---|
[eee966] | 1086 | */
|
---|
[66fd49] | 1087 | void FillVoidWithMolecule(
|
---|
| 1088 | molecule *&filler,
|
---|
| 1089 | config &configuration,
|
---|
| 1090 | const double distance[NDIM],
|
---|
| 1091 | const double boundary,
|
---|
| 1092 | const double RandomAtomDisplacement,
|
---|
| 1093 | const double RandomMolDisplacement,
|
---|
| 1094 | const double MinDistance,
|
---|
| 1095 | const bool DoRandomRotation
|
---|
| 1096 | )
|
---|
[eee966] | 1097 | {
|
---|
| 1098 | Info FunctionInfo(__func__);
|
---|
| 1099 | molecule *Filling = NULL;
|
---|
| 1100 | Vector CurrentPosition;
|
---|
| 1101 | int N[NDIM];
|
---|
| 1102 | int n[NDIM];
|
---|
[cca9ef] | 1103 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
| 1104 | RealSpaceMatrix Rotations;
|
---|
| 1105 | const RealSpaceMatrix &MInverse = World::getInstance().getDomain().getMinv();
|
---|
[eee966] | 1106 | Vector FillerTranslations;
|
---|
| 1107 | Vector FillerDistance;
|
---|
| 1108 | Vector Inserter;
|
---|
| 1109 | double FillIt = false;
|
---|
[66fd49] | 1110 | Vector firstInserter;
|
---|
| 1111 | bool firstInsertion = true;
|
---|
| 1112 | const Box &Domain = World::getInstance().getDomain();
|
---|
[6bd7e0] | 1113 | map<molecule *, LinkedCell_deprecated *> LCList;
|
---|
[eee966] | 1114 | std::vector<molecule *> List = World::getInstance().getAllMolecules();
|
---|
| 1115 | MoleculeListClass *MolList = World::getInstance().getMolecules();
|
---|
| 1116 |
|
---|
| 1117 | for (std::vector<molecule *>::iterator ListRunner = List.begin(); ListRunner != List.end(); ListRunner++)
|
---|
| 1118 | if ((*ListRunner)->getAtomCount() > 0) {
|
---|
[47d041] | 1119 | LOG(1, "Pre-creating linked cell lists for molecule " << *ListRunner << ".");
|
---|
[caa06ef] | 1120 | PointCloudAdaptor< molecule > cloud(*ListRunner, (*ListRunner)->name);
|
---|
[6bd7e0] | 1121 | LCList[(*ListRunner)] = new LinkedCell_deprecated(cloud, 10.); // get linked cell list
|
---|
[eee966] | 1122 | }
|
---|
| 1123 |
|
---|
[9df680] | 1124 | // Center filler at its center of gravity
|
---|
| 1125 | Vector *gravity = filler->DetermineCenterOfGravity();
|
---|
| 1126 | filler->CenterAtVector(gravity);
|
---|
| 1127 | delete gravity;
|
---|
[66fd49] | 1128 | //const int FillerCount = filler->getAtomCount();
|
---|
[47d041] | 1129 | LOG(2, "INFO: Filler molecule has the following bonds:");
|
---|
[9d83b6] | 1130 | for(molecule::iterator AtomRunner = filler->begin(); AtomRunner != filler->end(); ++AtomRunner) {
|
---|
| 1131 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
| 1132 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 1133 | BondRunner != ListOfBonds.end();
|
---|
| 1134 | ++BondRunner)
|
---|
[eee966] | 1135 | if ((*BondRunner)->leftatom == *AtomRunner)
|
---|
[47d041] | 1136 | LOG(2, " " << *(*BondRunner));
|
---|
[9d83b6] | 1137 | }
|
---|
[eee966] | 1138 |
|
---|
| 1139 | // calculate filler grid in [0,1]^3
|
---|
| 1140 | FillerDistance = MInverse * Vector(distance[0], distance[1], distance[2]);
|
---|
| 1141 | for(int i=0;i<NDIM;i++)
|
---|
| 1142 | N[i] = (int) ceil(1./FillerDistance[i]);
|
---|
[47d041] | 1143 | LOG(1, "INFO: Grid steps are " << N[0] << ", " << N[1] << ", " << N[2] << ".");
|
---|
[eee966] | 1144 |
|
---|
| 1145 | // initialize seed of random number generator to current time
|
---|
[a5028f] | 1146 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 1147 | const double rng_min = random.min();
|
---|
| 1148 | const double rng_max = random.max();
|
---|
| 1149 | //srand ( time(NULL) );
|
---|
[eee966] | 1150 |
|
---|
| 1151 | // go over [0,1]^3 filler grid
|
---|
| 1152 | for (n[0] = 0; n[0] < N[0]; n[0]++)
|
---|
| 1153 | for (n[1] = 0; n[1] < N[1]; n[1]++)
|
---|
| 1154 | for (n[2] = 0; n[2] < N[2]; n[2]++) {
|
---|
| 1155 | // calculate position of current grid vector in untransformed box
|
---|
| 1156 | CurrentPosition = M * Vector((double)n[0]/(double)N[0], (double)n[1]/(double)N[1], (double)n[2]/(double)N[2]);
|
---|
| 1157 | // create molecule random translation vector ...
|
---|
[a5028f] | 1158 | for (int i=0;i<NDIM;i++) // have the random values [-1,1]*RandomMolDisplacement
|
---|
| 1159 | FillerTranslations[i] = RandomMolDisplacement*(random()/((rng_max-rng_min)/2.) - 1.);
|
---|
[47d041] | 1160 | LOG(2, "INFO: Current Position is " << CurrentPosition << "+" << FillerTranslations << ".");
|
---|
[eee966] | 1161 |
|
---|
| 1162 | // ... and rotation matrix
|
---|
[66fd49] | 1163 | if (DoRandomRotation)
|
---|
[b81f1c] | 1164 | setRandomRotation(Rotations);
|
---|
[66fd49] | 1165 | else
|
---|
| 1166 | Rotations.setIdentity();
|
---|
[eee966] | 1167 |
|
---|
| 1168 |
|
---|
[66fd49] | 1169 | // Check whether there is anything too close by and whether atom is outside of domain
|
---|
[eee966] | 1170 | FillIt = true;
|
---|
[6bd7e0] | 1171 | for (std::map<molecule *, LinkedCell_deprecated *>::iterator ListRunner = LCList.begin(); ListRunner != LCList.end(); ++ListRunner) {
|
---|
[66fd49] | 1172 | FillIt = FillIt && isSpaceAroundPointVoid(
|
---|
| 1173 | ListRunner->second,
|
---|
| 1174 | (firstInsertion ? filler : NULL),
|
---|
| 1175 | boundary,
|
---|
| 1176 | CurrentPosition);
|
---|
[2a0271] | 1177 | FillIt = FillIt && (Domain.isValid(CurrentPosition))
|
---|
[66fd49] | 1178 | && ((Domain.DistanceToBoundary(CurrentPosition) - MinDistance) > -MYEPSILON);
|
---|
| 1179 | if (!FillIt)
|
---|
| 1180 | break;
|
---|
[eee966] | 1181 | }
|
---|
[66fd49] | 1182 |
|
---|
[eee966] | 1183 | // insert into Filling
|
---|
| 1184 | if (FillIt) {
|
---|
| 1185 | Inserter = CurrentPosition + FillerTranslations;
|
---|
[47d041] | 1186 | LOG(1, "INFO: Position at " << Inserter << " is void point.");
|
---|
[66fd49] | 1187 | // fill!
|
---|
[9df680] | 1188 | Filling = filler->CopyMolecule();
|
---|
[a5028f] | 1189 | RandomizeMoleculePositions(Filling, RandomAtomDisplacement, Rotations, random);
|
---|
[9df680] | 1190 | // translation
|
---|
| 1191 | Filling->Translate(&Inserter);
|
---|
| 1192 | // remove out-of-bounds atoms
|
---|
| 1193 | const bool status = RemoveAtomsOutsideDomain(Filling);
|
---|
| 1194 | if ((firstInsertion) && (!status)) { // no atom has been removed
|
---|
| 1195 | // remove copied atoms and molecule again
|
---|
| 1196 | Filling->removeAtomsinMolecule();
|
---|
| 1197 | World::getInstance().destroyMolecule(Filling);
|
---|
| 1198 | // and mark is final filler position
|
---|
[66fd49] | 1199 | Filling = filler;
|
---|
| 1200 | firstInsertion = false;
|
---|
| 1201 | firstInserter = Inserter;
|
---|
[9df680] | 1202 | } else {
|
---|
[66fd49] | 1203 | // TODO: Remove when World has no MoleculeListClass anymore
|
---|
[69948e] | 1204 | if (Filling)
|
---|
| 1205 | MolList->insert(Filling);
|
---|
[eee966] | 1206 | }
|
---|
| 1207 | } else {
|
---|
[47d041] | 1208 | LOG(1, "INFO: Position at " << Inserter << " is non-void point, within boundary or outside of MaxDistance.");
|
---|
[2a0271] | 1209 | continue;
|
---|
| 1210 | }
|
---|
| 1211 | }
|
---|
| 1212 |
|
---|
| 1213 | // have we inserted any molecules?
|
---|
| 1214 | if (firstInsertion) {
|
---|
| 1215 | // If not remove filler
|
---|
| 1216 | for(molecule::iterator miter = filler->begin(); !filler->empty(); miter = filler->begin()) {
|
---|
| 1217 | atom *Walker = *miter;
|
---|
| 1218 | World::getInstance().destroyAtom(Walker);
|
---|
| 1219 | }
|
---|
| 1220 | World::getInstance().destroyMolecule(filler);
|
---|
| 1221 | } else {
|
---|
| 1222 | // otherwise translate and randomize to final position
|
---|
| 1223 | if (DoRandomRotation)
|
---|
| 1224 | setRandomRotation(Rotations);
|
---|
| 1225 | else
|
---|
| 1226 | Rotations.setIdentity();
|
---|
| 1227 | RandomizeMoleculePositions(filler, RandomAtomDisplacement, Rotations, random);
|
---|
| 1228 | // translation
|
---|
| 1229 | filler->Translate(&firstInserter);
|
---|
| 1230 | // remove out-of-bounds atoms
|
---|
| 1231 | RemoveAtomsOutsideDomain(filler);
|
---|
| 1232 | }
|
---|
| 1233 |
|
---|
| 1234 | LOG(0, MolList->ListOfMolecules.size() << " molecules have been inserted.");
|
---|
| 1235 |
|
---|
| 1236 | for (std::map<molecule *, LinkedCell_deprecated *>::iterator ListRunner = LCList.begin(); !LCList.empty(); ListRunner = LCList.begin()) {
|
---|
| 1237 | delete ListRunner->second;
|
---|
| 1238 | LCList.erase(ListRunner);
|
---|
| 1239 | }
|
---|
| 1240 | };
|
---|
| 1241 |
|
---|
| 1242 |
|
---|
| 1243 | /** Fills the empty space around other molecules' surface of the simulation box with a filler.
|
---|
| 1244 | *
|
---|
| 1245 | * Note that we use \a FindNonConvexBorder to determine the surface of the found molecules.
|
---|
| 1246 | * There, we use a radius of twice the given \a boundary.
|
---|
| 1247 | *
|
---|
| 1248 | * \param *out output stream for debugging
|
---|
| 1249 | * \param *List list of molecules already present in box
|
---|
| 1250 | * \param *TesselStruct contains tesselated surface
|
---|
| 1251 | * \param *filler molecule which the box is to be filled with
|
---|
| 1252 | * \param configuration contains box dimensions
|
---|
| 1253 | * \param MaxSurfaceDistance fills in molecules only up to this distance (set to -1 if whole of the domain)
|
---|
| 1254 | * \param distance[NDIM] distance between filling molecules in each direction
|
---|
| 1255 | * \param boundary length of boundary zone between molecule and filling molecules
|
---|
| 1256 | * \param MinDistance distance to boundary of domain which is not filled
|
---|
| 1257 | * \param RandAtomDisplacement maximum distance for random displacement per atom
|
---|
| 1258 | * \param RandMolDisplacement maximum distance for random displacement per filler molecule
|
---|
| 1259 | * \param DoRandomRotation true - do random rotations, false - don't
|
---|
| 1260 | */
|
---|
| 1261 | void FillBoxWithMolecule(
|
---|
| 1262 | MoleculeListClass *MolList,
|
---|
| 1263 | molecule *filler,
|
---|
| 1264 | config &configuration,
|
---|
| 1265 | const double MaxSurfaceDistance,
|
---|
| 1266 | const double distance[NDIM],
|
---|
| 1267 | const double boundary,
|
---|
| 1268 | const double MinDistance,
|
---|
| 1269 | const double RandomAtomDisplacement,
|
---|
| 1270 | const double RandomMolDisplacement,
|
---|
| 1271 | const bool DoRandomRotation)
|
---|
| 1272 | {
|
---|
| 1273 | Info FunctionInfo(__func__);
|
---|
| 1274 | molecule *Filling = NULL;
|
---|
| 1275 | Vector CurrentPosition;
|
---|
| 1276 | int N[NDIM];
|
---|
| 1277 | int n[NDIM];
|
---|
| 1278 | const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
|
---|
| 1279 | RealSpaceMatrix Rotations;
|
---|
| 1280 | const RealSpaceMatrix &MInverse = World::getInstance().getDomain().getMinv();
|
---|
| 1281 | Vector FillerTranslations;
|
---|
| 1282 | Vector FillerDistance;
|
---|
| 1283 | Vector Inserter;
|
---|
| 1284 | double FillIt = false;
|
---|
| 1285 | Vector firstInserter;
|
---|
| 1286 | bool firstInsertion = true;
|
---|
| 1287 | const Box &Domain = World::getInstance().getDomain();
|
---|
| 1288 | map<molecule *, LinkedCell_deprecated *> LCList;
|
---|
| 1289 | std::vector<molecule *> List = World::getInstance().getAllMolecules();
|
---|
| 1290 | map<molecule *, Tesselation *> TesselStruct;
|
---|
| 1291 |
|
---|
| 1292 | for (MoleculeList::iterator ListRunner = MolList->ListOfMolecules.begin(); ListRunner != MolList->ListOfMolecules.end(); ListRunner++)
|
---|
| 1293 | if ((*ListRunner)->getAtomCount() > 0) {
|
---|
| 1294 | LOG(1, "Pre-creating linked cell lists for molecule " << *ListRunner << ".");
|
---|
| 1295 | PointCloudAdaptor< molecule > cloud(*ListRunner, (*ListRunner)->name);
|
---|
| 1296 | LCList[(*ListRunner)] = new LinkedCell_deprecated(cloud, 4.*boundary); // get linked cell list
|
---|
| 1297 | LOG(1, "Pre-creating tesselation for molecule " << *ListRunner << ".");
|
---|
| 1298 | TesselStruct[(*ListRunner)] = NULL;
|
---|
| 1299 | FindNonConvexBorder((*ListRunner), TesselStruct[(*ListRunner)], (const LinkedCell_deprecated *&)LCList[(*ListRunner)], 2.*boundary, NULL);
|
---|
| 1300 | }
|
---|
| 1301 |
|
---|
| 1302 | // Center filler at origin
|
---|
| 1303 | filler->CenterEdge(&Inserter);
|
---|
| 1304 | const int FillerCount = filler->getAtomCount();
|
---|
| 1305 | LOG(2, "INFO: Filler molecule has the following bonds:");
|
---|
| 1306 | for(molecule::iterator AtomRunner = filler->begin(); AtomRunner != filler->end(); ++AtomRunner) {
|
---|
| 1307 | const BondList& ListOfBonds = (*AtomRunner)->getListOfBonds();
|
---|
| 1308 | for(BondList::const_iterator BondRunner = ListOfBonds.begin();
|
---|
| 1309 | BondRunner != ListOfBonds.end();
|
---|
| 1310 | ++BondRunner) {
|
---|
| 1311 | if ((*BondRunner)->leftatom == *AtomRunner)
|
---|
| 1312 | LOG(2, " " << *(*BondRunner));
|
---|
| 1313 | }
|
---|
| 1314 | }
|
---|
| 1315 |
|
---|
| 1316 | atom * CopyAtoms[FillerCount];
|
---|
| 1317 |
|
---|
| 1318 | setVerbosity(4);
|
---|
| 1319 |
|
---|
| 1320 | // calculate filler grid in [0,1]^3
|
---|
| 1321 | FillerDistance = MInverse * Vector(distance[0], distance[1], distance[2]);
|
---|
| 1322 | for(int i=0;i<NDIM;i++)
|
---|
| 1323 | N[i] = (int) ceil(1./FillerDistance[i]);
|
---|
| 1324 | LOG(1, "INFO: Grid steps are " << N[0] << ", " << N[1] << ", " << N[2] << ".");
|
---|
| 1325 |
|
---|
| 1326 | // initialize seed of random number generator to current time
|
---|
| 1327 | RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
|
---|
| 1328 | const double rng_min = random.min();
|
---|
| 1329 | const double rng_max = random.max();
|
---|
| 1330 | //srand ( time(NULL) );
|
---|
| 1331 |
|
---|
| 1332 | // go over [0,1]^3 filler grid
|
---|
| 1333 | for (n[0] = 0; n[0] < N[0]; n[0]++)
|
---|
| 1334 | for (n[1] = 0; n[1] < N[1]; n[1]++)
|
---|
| 1335 | for (n[2] = 0; n[2] < N[2]; n[2]++) {
|
---|
| 1336 | // calculate position of current grid vector in untransformed box
|
---|
| 1337 | CurrentPosition = M * Vector((double)n[0]/(double)N[0], (double)n[1]/(double)N[1], (double)n[2]/(double)N[2]);
|
---|
| 1338 | // create molecule random translation vector ...
|
---|
| 1339 | for (int i=0;i<NDIM;i++)
|
---|
| 1340 | FillerTranslations[i] = RandomMolDisplacement*(random()/((rng_max-rng_min)/2.) - 1.);
|
---|
| 1341 | LOG(1, "INFO: Current Position is " << CurrentPosition << "+" << FillerTranslations << ".");
|
---|
| 1342 |
|
---|
| 1343 | // ... and rotation matrix
|
---|
| 1344 | if (DoRandomRotation)
|
---|
| 1345 | setRandomRotation(Rotations);
|
---|
| 1346 | else
|
---|
| 1347 | Rotations.setIdentity();
|
---|
| 1348 |
|
---|
| 1349 |
|
---|
| 1350 | // Check whether there is anything too close by and whether atom is outside of domain
|
---|
| 1351 | FillIt = true;
|
---|
| 1352 | for (std::map<molecule *, LinkedCell_deprecated *>::iterator ListRunner = LCList.begin(); ListRunner != LCList.end(); ++ListRunner) {
|
---|
| 1353 | // check whether its void
|
---|
| 1354 | FillIt = FillIt && isSpaceAroundPointVoid(
|
---|
| 1355 | ListRunner->second,
|
---|
| 1356 | (firstInsertion ? filler : NULL),
|
---|
| 1357 | boundary,
|
---|
| 1358 | CurrentPosition);
|
---|
| 1359 | if (!FillIt) {
|
---|
| 1360 | LOG(2, "REJECT: Position at " << Inserter << " is non-void.");
|
---|
| 1361 | break;
|
---|
| 1362 | }
|
---|
| 1363 | // check whether inside domain
|
---|
| 1364 | FillIt = FillIt && (Domain.isValid(CurrentPosition));
|
---|
| 1365 | if (!FillIt) {
|
---|
| 1366 | LOG(2, "REJECT: Position at " << CurrentPosition << " is "
|
---|
| 1367 | << distance << ", hence outside of domain.");
|
---|
| 1368 | break;
|
---|
| 1369 | }
|
---|
| 1370 | // check minimum distance to boundary
|
---|
| 1371 | const double distance = (Domain.DistanceToBoundary(CurrentPosition) - MinDistance);
|
---|
| 1372 | FillIt = FillIt && (distance > -MYEPSILON);
|
---|
| 1373 | if (!FillIt) {
|
---|
| 1374 | LOG(2, "REJECT: Position at " << CurrentPosition << " is " << distance << ", less than "
|
---|
| 1375 | << MinDistance << " hence, too close to boundary.");
|
---|
| 1376 | break;
|
---|
| 1377 | }
|
---|
| 1378 | }
|
---|
| 1379 | // Check whether point is in- or outside of tesselations
|
---|
| 1380 | if (FillIt) {
|
---|
| 1381 | for (MoleculeList::iterator ListRunner = MolList->ListOfMolecules.begin(); ListRunner != MolList->ListOfMolecules.end(); ListRunner++) {
|
---|
| 1382 | // get linked cell list
|
---|
| 1383 | if (TesselStruct[(*ListRunner)] != NULL) {
|
---|
| 1384 | const double distance = (TesselStruct[(*ListRunner)]->GetDistanceToSurface(Inserter, LCList[(*ListRunner)]));
|
---|
| 1385 | LOG(2, "INFO: Distance to surface is " << distance << ".");
|
---|
| 1386 | FillIt = FillIt && ((distance == -1.) || (distance > boundary)) && ((MaxSurfaceDistance < 0) || (MaxSurfaceDistance > distance));
|
---|
| 1387 | if (!FillIt) {
|
---|
| 1388 | LOG(2, "REJECT: Position at " << CurrentPosition << " is in distance of " << distance
|
---|
| 1389 | << " to a surface, less than " << MaxSurfaceDistance << " hence, too close.");
|
---|
| 1390 | break;
|
---|
| 1391 | }
|
---|
| 1392 | }
|
---|
| 1393 | }
|
---|
| 1394 | }
|
---|
| 1395 |
|
---|
| 1396 | // insert into Filling
|
---|
| 1397 | if (FillIt) {
|
---|
| 1398 | Inserter = CurrentPosition + FillerTranslations;
|
---|
| 1399 | LOG(2, "ACCEPT: Position at " << CurrentPosition << " is void point.");
|
---|
| 1400 | // fill!
|
---|
| 1401 | Filling = filler->CopyMolecule();
|
---|
| 1402 | RandomizeMoleculePositions(Filling, RandomAtomDisplacement, Rotations, random);
|
---|
| 1403 | // translation
|
---|
| 1404 | Filling->Translate(&Inserter);
|
---|
| 1405 | // remove out-of-bounds atoms
|
---|
| 1406 | const bool status = RemoveAtomsOutsideDomain(Filling);
|
---|
| 1407 | if ((firstInsertion) && (!status)) { // no atom has been removed
|
---|
| 1408 | // remove copied atoms and molecule again
|
---|
| 1409 | Filling->removeAtomsinMolecule();
|
---|
| 1410 | World::getInstance().destroyMolecule(Filling);
|
---|
| 1411 | // and mark is final filler position
|
---|
| 1412 | Filling = filler;
|
---|
| 1413 | firstInsertion = false;
|
---|
| 1414 | firstInserter = Inserter;
|
---|
| 1415 | } else {
|
---|
| 1416 | // TODO: Remove when World has no MoleculeListClass anymore
|
---|
| 1417 | if (Filling)
|
---|
| 1418 | MolList->insert(Filling);
|
---|
| 1419 | }
|
---|
| 1420 | } else {
|
---|
| 1421 | LOG(2, "REJECT: Position at " << CurrentPosition << " is non-void point, within boundary or outside of MaxSurfaceDistance.");
|
---|
[eee966] | 1422 | continue;
|
---|
| 1423 | }
|
---|
| 1424 | }
|
---|
[66fd49] | 1425 |
|
---|
| 1426 | // have we inserted any molecules?
|
---|
| 1427 | if (firstInsertion) {
|
---|
| 1428 | // If not remove filler
|
---|
| 1429 | for(molecule::iterator miter = filler->begin(); !filler->empty(); miter = filler->begin()) {
|
---|
| 1430 | atom *Walker = *miter;
|
---|
| 1431 | World::getInstance().destroyAtom(Walker);
|
---|
| 1432 | }
|
---|
| 1433 | World::getInstance().destroyMolecule(filler);
|
---|
| 1434 | } else {
|
---|
| 1435 | // otherwise translate and randomize to final position
|
---|
| 1436 | if (DoRandomRotation)
|
---|
[b81f1c] | 1437 | setRandomRotation(Rotations);
|
---|
[66fd49] | 1438 | else
|
---|
| 1439 | Rotations.setIdentity();
|
---|
[a5028f] | 1440 | RandomizeMoleculePositions(filler, RandomAtomDisplacement, Rotations, random);
|
---|
[66fd49] | 1441 | // translation
|
---|
| 1442 | filler->Translate(&firstInserter);
|
---|
| 1443 | // remove out-of-bounds atoms
|
---|
| 1444 | RemoveAtomsOutsideDomain(filler);
|
---|
[eee966] | 1445 | }
|
---|
[66fd49] | 1446 |
|
---|
[47d041] | 1447 | LOG(0, MolList->ListOfMolecules.size() << " molecules have been inserted.");
|
---|
[eee966] | 1448 |
|
---|
[6bd7e0] | 1449 | for (std::map<molecule *, LinkedCell_deprecated *>::iterator ListRunner = LCList.begin(); !LCList.empty(); ListRunner = LCList.begin()) {
|
---|
[eee966] | 1450 | delete ListRunner->second;
|
---|
| 1451 | LCList.erase(ListRunner);
|
---|
| 1452 | }
|
---|
| 1453 | };
|
---|
[8c54a3] | 1454 |
|
---|
[6ac7ee] | 1455 | /** Tesselates the non convex boundary by rolling a virtual sphere along the surface of the molecule.
|
---|
| 1456 | * \param *out output stream for debugging
|
---|
| 1457 | * \param *mol molecule structure with Atom's and Bond's
|
---|
[776b64] | 1458 | * \param *&TesselStruct Tesselation filled with points, lines and triangles on boundary on return
|
---|
[6bd7e0] | 1459 | * \param *&LCList atoms in LinkedCell_deprecated list
|
---|
[57066a] | 1460 | * \param RADIUS radius of the virtual sphere
|
---|
[6ac7ee] | 1461 | * \param *filename filename prefix for output of vertex data
|
---|
[4fc93f] | 1462 | * \return true - tesselation successful, false - tesselation failed
|
---|
[6ac7ee] | 1463 | */
|
---|
[6bd7e0] | 1464 | bool FindNonConvexBorder(molecule* const mol, Tesselation *&TesselStruct, const LinkedCell_deprecated *&LCList, const double RADIUS, const char *filename = NULL)
|
---|
[03648b] | 1465 | {
|
---|
[f67b6e] | 1466 | Info FunctionInfo(__func__);
|
---|
[3d919e] | 1467 | bool freeLC = false;
|
---|
[4fc93f] | 1468 | bool status = false;
|
---|
[6613ec] | 1469 | CandidateForTesselation *baseline = NULL;
|
---|
[1e168b] | 1470 | bool OneLoopWithoutSuccessFlag = true; // marks whether we went once through all baselines without finding any without two triangles
|
---|
[ad37ab] | 1471 | bool TesselationFailFlag = false;
|
---|
[357fba] | 1472 |
|
---|
[a7b761b] | 1473 | mol->getAtomCount();
|
---|
[357fba] | 1474 |
|
---|
[776b64] | 1475 | if (TesselStruct == NULL) {
|
---|
[47d041] | 1476 | LOG(1, "Allocating Tesselation struct ...");
|
---|
[776b64] | 1477 | TesselStruct= new Tesselation;
|
---|
[ef0e6d] | 1478 | } else {
|
---|
[776b64] | 1479 | delete(TesselStruct);
|
---|
[47d041] | 1480 | LOG(1, "Re-Allocating Tesselation struct ...");
|
---|
[776b64] | 1481 | TesselStruct = new Tesselation;
|
---|
[3d919e] | 1482 | }
|
---|
[ad37ab] | 1483 |
|
---|
[57066a] | 1484 | // initialise Linked Cell
|
---|
[caa06ef] | 1485 | PointCloudAdaptor< molecule > cloud(mol, mol->name);
|
---|
[3d919e] | 1486 | if (LCList == NULL) {
|
---|
[6bd7e0] | 1487 | LCList = new LinkedCell_deprecated(cloud, 2.*RADIUS);
|
---|
[3d919e] | 1488 | freeLC = true;
|
---|
| 1489 | }
|
---|
| 1490 |
|
---|
[57066a] | 1491 | // 1. get starting triangle
|
---|
[ce70970] | 1492 | if (!TesselStruct->FindStartingTriangle(RADIUS, LCList)) {
|
---|
[47d041] | 1493 | ELOG(0, "No valid starting triangle found.");
|
---|
[b5c2d7] | 1494 | //performCriticalExit();
|
---|
[ce70970] | 1495 | }
|
---|
[f8bd7d] | 1496 | if (filename != NULL) {
|
---|
| 1497 | if ((DoSingleStepOutput && ((TesselStruct->TrianglesOnBoundary.size() % SingleStepWidth == 0)))) { // if we have a new triangle and want to output each new triangle configuration
|
---|
[34c43a] | 1498 | TesselStruct->Output(filename, cloud);
|
---|
[f8bd7d] | 1499 | }
|
---|
| 1500 | }
|
---|
[3d919e] | 1501 |
|
---|
[57066a] | 1502 | // 2. expand from there
|
---|
[1e168b] | 1503 | while ((!TesselStruct->OpenLines.empty()) && (OneLoopWithoutSuccessFlag)) {
|
---|
[b40a93] | 1504 | (cerr << "There are " << TesselStruct->TrianglesOnBoundary.size() << " triangles and " << TesselStruct->OpenLines.size() << " open lines to scan for candidates." << endl);
|
---|
| 1505 | // 2a. print OpenLines without candidates
|
---|
[47d041] | 1506 | LOG(1, "There are the following open lines to scan for a candidates:");
|
---|
[1e168b] | 1507 | for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++)
|
---|
[b40a93] | 1508 | if (Runner->second->pointlist.empty())
|
---|
[47d041] | 1509 | LOG(1, " " << *(Runner->second));
|
---|
[1e168b] | 1510 |
|
---|
[734816] | 1511 | // 2b. find best candidate for each OpenLine
|
---|
[6613ec] | 1512 | TesselationFailFlag = TesselStruct->FindCandidatesforOpenLines(RADIUS, LCList);
|
---|
[3d919e] | 1513 |
|
---|
[734816] | 1514 | // 2c. print OpenLines with candidates again
|
---|
[47d041] | 1515 | LOG(1, "There are " << TesselStruct->OpenLines.size() << " open lines to scan for the best candidates:");
|
---|
[1e168b] | 1516 | for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++)
|
---|
[47d041] | 1517 | LOG(1, " " << *(Runner->second));
|
---|
[1e168b] | 1518 |
|
---|
[734816] | 1519 | // 2d. search for smallest ShortestAngle among all candidates
|
---|
| 1520 | double ShortestAngle = 4.*M_PI;
|
---|
[1e168b] | 1521 | for (CandidateMap::iterator Runner = TesselStruct->OpenLines.begin(); Runner != TesselStruct->OpenLines.end(); Runner++) {
|
---|
| 1522 | if (Runner->second->ShortestAngle < ShortestAngle) {
|
---|
| 1523 | baseline = Runner->second;
|
---|
| 1524 | ShortestAngle = baseline->ShortestAngle;
|
---|
[47d041] | 1525 | LOG(1, "New best candidate is " << *baseline->BaseLine << " with point " << *(*baseline->pointlist.begin()) << " and angle " << baseline->ShortestAngle);
|
---|
[1e168b] | 1526 | }
|
---|
| 1527 | }
|
---|
[734816] | 1528 | // 2e. if we found one, add candidate
|
---|
[f67b6e] | 1529 | if ((ShortestAngle == 4.*M_PI) || (baseline->pointlist.empty()))
|
---|
[7dea7c] | 1530 | OneLoopWithoutSuccessFlag = false;
|
---|
[1e168b] | 1531 | else {
|
---|
[474961] | 1532 | TesselStruct->AddCandidatePolygon(*baseline, RADIUS, LCList);
|
---|
[1e168b] | 1533 | }
|
---|
| 1534 |
|
---|
[734816] | 1535 | // 2f. write temporary envelope
|
---|
[1e168b] | 1536 | if (filename != NULL) {
|
---|
| 1537 | if ((DoSingleStepOutput && ((TesselStruct->TrianglesOnBoundary.size() % SingleStepWidth == 0)))) { // if we have a new triangle and want to output each new triangle configuration
|
---|
[34c43a] | 1538 | TesselStruct->Output(filename, cloud);
|
---|
[1e168b] | 1539 | }
|
---|
[3d919e] | 1540 | }
|
---|
| 1541 | }
|
---|
[4fc93f] | 1542 | // // check envelope for consistency
|
---|
| 1543 | // status = CheckListOfBaselines(TesselStruct);
|
---|
| 1544 | //
|
---|
| 1545 | // // look whether all points are inside of the convex envelope, otherwise add them via degenerated triangles
|
---|
| 1546 | // //->InsertStraddlingPoints(mol, LCList);
|
---|
[9879f6] | 1547 | // for (molecule::const_iterator iter = mol->begin(); iter != mol->end(); ++iter) {
|
---|
[57066a] | 1548 | // class TesselPoint *Runner = NULL;
|
---|
[9879f6] | 1549 | // Runner = *iter;
|
---|
[47d041] | 1550 | // LOG(1, "Checking on " << Runner->Name << " ... ");
|
---|
[e138de] | 1551 | // if (!->IsInnerPoint(Runner, LCList)) {
|
---|
[47d041] | 1552 | // LOG(2, Runner->Name << " is outside of envelope, adding via degenerated triangles.");
|
---|
[e138de] | 1553 | // ->AddBoundaryPointByDegeneratedTriangle(Runner, LCList);
|
---|
[57066a] | 1554 | // } else {
|
---|
[47d041] | 1555 | // LOG(2, Runner->Name << " is inside of or on envelope.");
|
---|
[57066a] | 1556 | // }
|
---|
| 1557 | // }
|
---|
[357fba] | 1558 |
|
---|
[f67b6e] | 1559 | // // Purges surplus triangles.
|
---|
| 1560 | // TesselStruct->RemoveDegeneratedTriangles();
|
---|
[b32dbb] | 1561 | //
|
---|
| 1562 | // // check envelope for consistency
|
---|
| 1563 | // status = CheckListOfBaselines(TesselStruct);
|
---|
[b998c3] | 1564 |
|
---|
[c72112] | 1565 | cout << "before correction" << endl;
|
---|
| 1566 |
|
---|
[b998c3] | 1567 | // store before correction
|
---|
[c72112] | 1568 | StoreTrianglesinFile(mol, TesselStruct, filename, "");
|
---|
[b998c3] | 1569 |
|
---|
[6613ec] | 1570 | // // correct degenerated polygons
|
---|
| 1571 | // TesselStruct->CorrectAllDegeneratedPolygons();
|
---|
| 1572 | //
|
---|
[e69c87] | 1573 | // check envelope for consistency
|
---|
| 1574 | status = CheckListOfBaselines(TesselStruct);
|
---|
[ef0e6d] | 1575 |
|
---|
[57066a] | 1576 | // write final envelope
|
---|
[e138de] | 1577 | CalculateConcavityPerBoundaryPoint(TesselStruct);
|
---|
[c72112] | 1578 | cout << "after correction" << endl;
|
---|
| 1579 | StoreTrianglesinFile(mol, TesselStruct, filename, "");
|
---|
[8c54a3] | 1580 |
|
---|
[3d919e] | 1581 | if (freeLC)
|
---|
| 1582 | delete(LCList);
|
---|
[4fc93f] | 1583 |
|
---|
| 1584 | return status;
|
---|
[6ac7ee] | 1585 | };
|
---|
[03648b] | 1586 |
|
---|
[57066a] | 1587 |
|
---|
[ad37ab] | 1588 | /** Finds a hole of sufficient size in \a *mols to embed \a *srcmol into it.
|
---|
[ca2587] | 1589 | * \param *out output stream for debugging
|
---|
[ad37ab] | 1590 | * \param *mols molecules in the domain to embed in between
|
---|
| 1591 | * \param *srcmol embedding molecule
|
---|
[ca2587] | 1592 | * \return *Vector new center of \a *srcmol for embedding relative to \a this
|
---|
| 1593 | */
|
---|
[e138de] | 1594 | Vector* FindEmbeddingHole(MoleculeListClass *mols, molecule *srcmol)
|
---|
[ca2587] | 1595 | {
|
---|
[f67b6e] | 1596 | Info FunctionInfo(__func__);
|
---|
[ca2587] | 1597 | Vector *Center = new Vector;
|
---|
| 1598 | Center->Zero();
|
---|
| 1599 | // calculate volume/shape of \a *srcmol
|
---|
| 1600 |
|
---|
| 1601 | // find embedding holes
|
---|
| 1602 |
|
---|
| 1603 | // if more than one, let user choose
|
---|
| 1604 |
|
---|
| 1605 | // return embedding center
|
---|
| 1606 | return Center;
|
---|
| 1607 | };
|
---|
| 1608 |
|
---|