| [48ab414] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [94d5ac6] | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
| [48ab414] | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * RandomInserter.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Feb 21, 2012
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | // include config.h
 | 
|---|
 | 32 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 33 | #include <config.h>
 | 
|---|
 | 34 | #endif
 | 
|---|
 | 35 | 
 | 
|---|
 | 36 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 37 | 
 | 
|---|
 | 38 | #include "RandomInserter.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
 | 40 | #include <algorithm>
 | 
|---|
 | 41 | 
 | 
|---|
 | 42 | #include "Atom/atom.hpp"
 | 
|---|
 | 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 44 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
 | 45 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
 | 
|---|
 | 46 | #include "RandomNumbers/RandomNumberGenerator.hpp"
 | 
|---|
 | 47 | 
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 | size_t RandomInserter::Max_Attempts = 10;
 | 
|---|
 | 50 | 
 | 
|---|
 | 51 | /** Sets given 3x3 matrix to a random rotation matrix.
 | 
|---|
 | 52 |  *
 | 
|---|
 | 53 |  * @param a matrix to set
 | 
|---|
 | 54 |  */
 | 
|---|
 | 55 | inline void setRandomRotation(RealSpaceMatrix &a)
 | 
|---|
 | 56 | {
 | 
|---|
 | 57 |   double phi[NDIM];
 | 
|---|
 | 58 |   RandomNumberGenerator &random = RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator();
 | 
|---|
 | 59 |   const double rng_min = random.min();
 | 
|---|
 | 60 |   const double rng_max = random.max();
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |   for (int i=0;i<NDIM;i++) {
 | 
|---|
 | 63 |     phi[i] = (random()/(rng_max-rng_min))*(2.*M_PI);
 | 
|---|
 | 64 |     LOG(4, "DEBUG: Random angle is " << phi[i] << ".");
 | 
|---|
 | 65 |   }
 | 
|---|
 | 66 | 
 | 
|---|
 | 67 |   a.setRotation(phi);
 | 
|---|
 | 68 | }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | /** Constructor for class RandomInserter.
 | 
|---|
 | 71 |  *
 | 
|---|
 | 72 |  * @param _MaxAtomComponent maximum component for random atom translations
 | 
|---|
 | 73 |  * @param _MaxMoleculeComponent maximum component for random molecule translations
 | 
|---|
 | 74 |  * @param  _DoRandomRotation whether to do random rotations
 | 
|---|
 | 75 |  */
 | 
|---|
 | 76 | RandomInserter::RandomInserter(
 | 
|---|
 | 77 |     const double _MaxAtomComponent,
 | 
|---|
 | 78 |     const double _MaxMoleculeComponent,
 | 
|---|
 | 79 |     const bool _DoRandomRotation) :
 | 
|---|
 | 80 |    random(RandomNumberGeneratorFactory::getInstance().makeRandomNumberGenerator()),
 | 
|---|
 | 81 |    rng_min(random.min()),
 | 
|---|
 | 82 |    rng_max(random.max()),
 | 
|---|
 | 83 |    MaxAtomComponent(_MaxAtomComponent),
 | 
|---|
 | 84 |    MaxMoleculeComponent(_MaxMoleculeComponent),
 | 
|---|
 | 85 |    DoRandomRotation(_DoRandomRotation)
 | 
|---|
 | 86 | {}
 | 
|---|
 | 87 | 
 | 
|---|
 | 88 | /** Destructor for class RandomInserter.
 | 
|---|
 | 89 |  *
 | 
|---|
 | 90 |  */
 | 
|---|
 | 91 | RandomInserter::~RandomInserter()
 | 
|---|
 | 92 | {}
 | 
|---|
 | 93 | 
 | 
|---|
 | 94 | /** Checks whether all atoms currently are inside the cluster's shape.
 | 
|---|
 | 95 |  *
 | 
|---|
 | 96 |  * @param cluster cluster to check
 | 
|---|
 | 97 |  * @return true - all atoms are inside cluster's shape, false - else
 | 
|---|
 | 98 |  */
 | 
|---|
 | 99 | bool RandomInserter::AreClustersAtomsInside(ClusterInterface::Cluster_impl cluster) const
 | 
|---|
 | 100 | {
 | 
|---|
 | 101 |   ClusterInterface::atomIdSet atoms = cluster->getAtomIds();
 | 
|---|
 | 102 |   bool status = true;
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   for (ClusterInterface::atomIdSet::const_iterator iter = atoms.begin();
 | 
|---|
 | 105 |       iter != atoms.end();
 | 
|---|
 | 106 |       ++iter)
 | 
|---|
 | 107 |     status = status && cluster->isInside(*iter);
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |   return status;
 | 
|---|
 | 110 | }
 | 
|---|
 | 111 | 
 | 
|---|
 | 112 | /** Perform the given random translations and rotations on a cluster.
 | 
|---|
 | 113 |  *
 | 
|---|
 | 114 |  * @param cluster cluster to translate and rotate
 | 
|---|
 | 115 |  * @param Rotations random rotation matrix
 | 
|---|
 | 116 |  * @param RandomAtomTranslations vector with random translation for each atom in cluster
 | 
|---|
 | 117 |  * @param RandomMoleculeTranslation vector with random translation for cluster
 | 
|---|
 | 118 |  * @param offset vector with offset for cluster
 | 
|---|
 | 119 |  */
 | 
|---|
 | 120 | void RandomInserter::doTranslation(
 | 
|---|
 | 121 |     ClusterInterface::Cluster_impl cluster,
 | 
|---|
 | 122 |     const RealSpaceMatrix &Rotations,
 | 
|---|
 | 123 |     const std::vector<Vector> &RandomAtomTranslations,
 | 
|---|
 | 124 |     const Vector &RandomMoleculeTranslation) const
 | 
|---|
 | 125 | {
 | 
|---|
 | 126 |   AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
 | 127 | 
 | 
|---|
 | 128 |   ASSERT( atoms.size() <=  RandomAtomTranslations.size(),
 | 
|---|
 | 129 |       "RandomInserter::doTranslation() - insufficient random atom translations given.");
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   cluster->transform(Rotations);
 | 
|---|
 | 132 |   cluster->translate(RandomMoleculeTranslation);
 | 
|---|
 | 133 |   AtomIdSet::iterator miter = atoms.begin();
 | 
|---|
 | 134 |   std::vector<Vector>::const_iterator aiter = RandomAtomTranslations.begin();
 | 
|---|
 | 135 |   for(;miter != atoms.end(); ++miter, ++aiter)
 | 
|---|
 | 136 |     (*miter)->setPosition((*miter)->getPosition() + *aiter);
 | 
|---|
 | 137 | }
 | 
|---|
 | 138 | 
 | 
|---|
 | 139 | /** Undos a given random translations and rotations on a cluster.
 | 
|---|
 | 140 |  *
 | 
|---|
 | 141 |  * @param cluster cluster to translate and rotate
 | 
|---|
 | 142 |  * @param Rotations random rotation matrix
 | 
|---|
 | 143 |  * @param RandomAtomTranslations vector with random translation for each atom in cluster
 | 
|---|
 | 144 |  * @param RandomMoleculeTranslations vector with random translation for cluster
 | 
|---|
 | 145 |  * @param offset vector with offset for cluster
 | 
|---|
 | 146 |  */
 | 
|---|
 | 147 | void RandomInserter::undoTranslation(
 | 
|---|
 | 148 |     ClusterInterface::Cluster_impl cluster,
 | 
|---|
 | 149 |     const RealSpaceMatrix &Rotations,
 | 
|---|
 | 150 |     const std::vector<Vector> &RandomAtomTranslations,
 | 
|---|
 | 151 |     const Vector &RandomMoleculeTranslation) const
 | 
|---|
 | 152 | {
 | 
|---|
 | 153 |   AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
 | 154 |   ASSERT( atoms.size() <=  RandomAtomTranslations.size(),
 | 
|---|
 | 155 |       "RandomInserter::doTranslation() - insufficient random atom translations given.");
 | 
|---|
 | 156 | 
 | 
|---|
 | 157 |   // get inverse rotation
 | 
|---|
 | 158 |   RealSpaceMatrix inverseRotations = Rotations.invert();
 | 
|---|
 | 159 | 
 | 
|---|
 | 160 |   AtomIdSet::iterator miter = atoms.begin();
 | 
|---|
 | 161 |   std::vector<Vector>::const_iterator aiter = RandomAtomTranslations.begin();
 | 
|---|
 | 162 |   for(;miter != atoms.end(); ++miter, ++aiter) {
 | 
|---|
 | 163 |     (*miter)->setPosition((*miter)->getPosition() - *aiter);
 | 
|---|
 | 164 |   }
 | 
|---|
 | 165 |   cluster->translate(zeroVec-RandomMoleculeTranslation);
 | 
|---|
 | 166 |   cluster->transform(inverseRotations);
 | 
|---|
 | 167 | }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 | /** Creates a random vector
 | 
|---|
 | 170 |  *
 | 
|---|
 | 171 |  * @param range range of components, i.e. \f$ v[i] \in [0,range)\f$
 | 
|---|
 | 172 |  * @param offset offset for each component
 | 
|---|
 | 173 |  * @return \a range * rnd() + \a offset
 | 
|---|
 | 174 |  */
 | 
|---|
 | 175 | Vector RandomInserter::getRandomVector(const double range, const double offset) const
 | 
|---|
 | 176 | {
 | 
|---|
 | 177 |   Vector returnVector;
 | 
|---|
 | 178 |   for (size_t i=0; i<NDIM; ++i)
 | 
|---|
 | 179 |     returnVector[i] = (range*random()/((rng_max-rng_min)/2.)) + offset;
 | 
|---|
 | 180 |   return returnVector;
 | 
|---|
 | 181 | }
 | 
|---|
 | 182 | 
 | 
|---|
 | 183 | /** Inserter operator that randomly translates and rotates the Cluster.
 | 
|---|
 | 184 |  *
 | 
|---|
 | 185 |  * \note we assume that clusters are always cloned at origin.
 | 
|---|
 | 186 |  *
 | 
|---|
 | 187 |  * @param offset
 | 
|---|
 | 188 |  * @return true - random translations and rotations did not violate bounding shape, false - else
 | 
|---|
 | 189 |  */
 | 
|---|
 | 190 | bool RandomInserter::operator()(ClusterInterface::Cluster_impl cluster, const Vector &offset) const
 | 
|---|
 | 191 | {
 | 
|---|
 | 192 |   // calculate center
 | 
|---|
 | 193 |   AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
 | 194 |   Vector center;
 | 
|---|
 | 195 |   center.Zero();
 | 
|---|
 | 196 |   for(AtomIdSet::iterator miter = atoms.begin();miter != atoms.end(); ++miter)
 | 
|---|
 | 197 |     center += (*miter)->getPosition();
 | 
|---|
 | 198 |   center *= 1./atoms.size();
 | 
|---|
 | 199 | 
 | 
|---|
 | 200 |   // shift cluster to center
 | 
|---|
 | 201 |   cluster->translate(zeroVec-center);
 | 
|---|
 | 202 | 
 | 
|---|
 | 203 |   size_t attempt = 0;
 | 
|---|
 | 204 |   for (;attempt < Max_Attempts; ++attempt) {
 | 
|---|
 | 205 |     // generate random rotation matrix
 | 
|---|
 | 206 |     RealSpaceMatrix Rotations;
 | 
|---|
 | 207 |     if (DoRandomRotation)
 | 
|---|
 | 208 |       setRandomRotation(Rotations);
 | 
|---|
 | 209 |     else
 | 
|---|
 | 210 |       Rotations.setIdentity();
 | 
|---|
 | 211 | 
 | 
|---|
 | 212 |     // generate random molecule translation vector
 | 
|---|
 | 213 |     Vector RandomMoleculeTranslation(getRandomVector(MaxMoleculeComponent, -MaxMoleculeComponent));
 | 
|---|
 | 214 | 
 | 
|---|
 | 215 |     // generate random atom translation vector
 | 
|---|
 | 216 |     std::vector<Vector> RandomAtomTranslations(atoms.size(), zeroVec);
 | 
|---|
 | 217 |     std::generate_n(RandomAtomTranslations.begin(), atoms.size(),
 | 
|---|
 | 218 |         boost::bind(&RandomInserter::getRandomVector, boost::cref(this), MaxAtomComponent, -MaxAtomComponent));
 | 
|---|
 | 219 | 
 | 
|---|
 | 220 |     // apply!
 | 
|---|
 | 221 |     doTranslation(cluster, Rotations, RandomAtomTranslations, RandomMoleculeTranslation);
 | 
|---|
 | 222 | 
 | 
|---|
 | 223 |     // ... and check
 | 
|---|
 | 224 |     if (!AreClustersAtomsInside(cluster)) {
 | 
|---|
 | 225 |       undoTranslation(cluster, Rotations, RandomAtomTranslations, RandomMoleculeTranslation);
 | 
|---|
 | 226 |     } else {
 | 
|---|
 | 227 |       break;
 | 
|---|
 | 228 |     }
 | 
|---|
 | 229 |   }
 | 
|---|
 | 230 | 
 | 
|---|
 | 231 |   // and move to final position
 | 
|---|
 | 232 |   cluster->translate(offset+center);
 | 
|---|
 | 233 | 
 | 
|---|
 | 234 |   return attempt != Max_Attempts;
 | 
|---|
 | 235 | }
 | 
|---|