| 1 | /* | 
|---|
| 2 | * Project: MoleCuilder | 
|---|
| 3 | * Description: creates and alters molecular systems | 
|---|
| 4 | * Copyright (C)  2013 University of Bonn. All rights reserved. | 
|---|
| 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| 6 | * | 
|---|
| 7 | * | 
|---|
| 8 | *   This file is part of MoleCuilder. | 
|---|
| 9 | * | 
|---|
| 10 | *    MoleCuilder is free software: you can redistribute it and/or modify | 
|---|
| 11 | *    it under the terms of the GNU General Public License as published by | 
|---|
| 12 | *    the Free Software Foundation, either version 2 of the License, or | 
|---|
| 13 | *    (at your option) any later version. | 
|---|
| 14 | * | 
|---|
| 15 | *    MoleCuilder is distributed in the hope that it will be useful, | 
|---|
| 16 | *    but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 17 | *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
|---|
| 18 | *    GNU General Public License for more details. | 
|---|
| 19 | * | 
|---|
| 20 | *    You should have received a copy of the GNU General Public License | 
|---|
| 21 | *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>. | 
|---|
| 22 | */ | 
|---|
| 23 |  | 
|---|
| 24 | /* | 
|---|
| 25 | * SaturatedFragment.cpp | 
|---|
| 26 | * | 
|---|
| 27 | *  Created on: Mar 3, 2013 | 
|---|
| 28 | *      Author: heber | 
|---|
| 29 | */ | 
|---|
| 30 |  | 
|---|
| 31 | // include config.h | 
|---|
| 32 | #ifdef HAVE_CONFIG_H | 
|---|
| 33 | #include <config.h> | 
|---|
| 34 | #endif | 
|---|
| 35 |  | 
|---|
| 36 | #include "CodePatterns/MemDebug.hpp" | 
|---|
| 37 |  | 
|---|
| 38 | #include "SaturatedFragment.hpp" | 
|---|
| 39 |  | 
|---|
| 40 | #include <algorithm> | 
|---|
| 41 | #include <cmath> | 
|---|
| 42 | #include <iostream> | 
|---|
| 43 |  | 
|---|
| 44 | #include "CodePatterns/Assert.hpp" | 
|---|
| 45 | #include "CodePatterns/Log.hpp" | 
|---|
| 46 |  | 
|---|
| 47 | #include "LinearAlgebra/Exceptions.hpp" | 
|---|
| 48 | #include "LinearAlgebra/Plane.hpp" | 
|---|
| 49 | #include "LinearAlgebra/RealSpaceMatrix.hpp" | 
|---|
| 50 | #include "LinearAlgebra/Vector.hpp" | 
|---|
| 51 |  | 
|---|
| 52 | #include "Atom/atom.hpp" | 
|---|
| 53 | #include "Bond/bond.hpp" | 
|---|
| 54 | #include "config.hpp" | 
|---|
| 55 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| 56 | #include "Fragmentation/Exporters/HydrogenPool.hpp" | 
|---|
| 57 | #include "Fragmentation/Exporters/SphericalPointDistribution.hpp" | 
|---|
| 58 | #include "Fragmentation/HydrogenSaturation_enum.hpp" | 
|---|
| 59 | #include "Graph/BondGraph.hpp" | 
|---|
| 60 | #include "World.hpp" | 
|---|
| 61 |  | 
|---|
| 62 | SaturatedFragment::SaturatedFragment( | 
|---|
| 63 | const KeySet &_set, | 
|---|
| 64 | KeySetsInUse_t &_container, | 
|---|
| 65 | HydrogenPool &_hydrogens, | 
|---|
| 66 | const enum HydrogenTreatment _treatment, | 
|---|
| 67 | const enum HydrogenSaturation _saturation) : | 
|---|
| 68 | container(_container), | 
|---|
| 69 | set(_set), | 
|---|
| 70 | hydrogens(_hydrogens), | 
|---|
| 71 | FullMolecule(set), | 
|---|
| 72 | treatment(_treatment), | 
|---|
| 73 | saturation(_saturation) | 
|---|
| 74 | { | 
|---|
| 75 | // add to in-use container | 
|---|
| 76 | ASSERT( container.find(set) == container.end(), | 
|---|
| 77 | "SaturatedFragment::SaturatedFragment() - the set " | 
|---|
| 78 | +toString(set)+" is already marked as in use."); | 
|---|
| 79 | container.insert(set); | 
|---|
| 80 |  | 
|---|
| 81 | // prepare saturation hydrogens | 
|---|
| 82 | saturate(); | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | SaturatedFragment::~SaturatedFragment() | 
|---|
| 86 | { | 
|---|
| 87 | // release all saturation hydrogens if present | 
|---|
| 88 | for (KeySet::iterator iter = SaturationHydrogens.begin(); | 
|---|
| 89 | !SaturationHydrogens.empty(); | 
|---|
| 90 | iter = SaturationHydrogens.begin()) { | 
|---|
| 91 | hydrogens.releaseHydrogen(*iter); | 
|---|
| 92 | SaturationHydrogens.erase(iter); | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | // remove ourselves from in-use container | 
|---|
| 96 | KeySetsInUse_t::iterator iter = container.find(set); | 
|---|
| 97 | ASSERT( container.find(set) != container.end(), | 
|---|
| 98 | "SaturatedFragment::SaturatedFragment() - the set " | 
|---|
| 99 | +toString(set)+" is not marked as in use."); | 
|---|
| 100 | container.erase(iter); | 
|---|
| 101 | } | 
|---|
| 102 |  | 
|---|
| 103 | void SaturatedFragment::saturate() | 
|---|
| 104 | { | 
|---|
| 105 | // so far, we just have a set of keys. Hence, convert to atom refs | 
|---|
| 106 | // and gather all atoms in a vector | 
|---|
| 107 | std::vector<atom *> atoms; | 
|---|
| 108 | for (KeySet::const_iterator iter = FullMolecule.begin(); | 
|---|
| 109 | iter != FullMolecule.end(); | 
|---|
| 110 | ++iter) { | 
|---|
| 111 | atom * const Walker = World::getInstance().getAtom(AtomById(*iter)); | 
|---|
| 112 | ASSERT( Walker != NULL, | 
|---|
| 113 | "SaturatedFragment::OutputConfig() - id " | 
|---|
| 114 | +toString(*iter)+" is unknown to World."); | 
|---|
| 115 | atoms.push_back(Walker); | 
|---|
| 116 | } | 
|---|
| 117 | LOG(4, "DEBUG: We have gathered the following atoms: " << atoms); | 
|---|
| 118 |  | 
|---|
| 119 | //  bool LonelyFlag = false; | 
|---|
| 120 | // go through each atom of the fragment and gather all cut bonds in list | 
|---|
| 121 | typedef std::map<atom *, BondList > CutBonds_t; | 
|---|
| 122 | CutBonds_t CutBonds; | 
|---|
| 123 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
| 124 | iter != atoms.end(); | 
|---|
| 125 | ++iter) { | 
|---|
| 126 | atom * const Walker = *iter; | 
|---|
| 127 | // start with an empty list | 
|---|
| 128 | CutBonds.insert( std::make_pair(Walker, BondList() )); | 
|---|
| 129 |  | 
|---|
| 130 | // go through all bonds | 
|---|
| 131 | const BondList& ListOfBonds = Walker->getListOfBonds(); | 
|---|
| 132 | for (BondList::const_iterator BondRunner = ListOfBonds.begin(); | 
|---|
| 133 | BondRunner != ListOfBonds.end(); | 
|---|
| 134 | ++BondRunner) { | 
|---|
| 135 | atom * const OtherWalker = (*BondRunner)->GetOtherAtom(Walker); | 
|---|
| 136 | // if other atom is in key set | 
|---|
| 137 | if (set.find(OtherWalker->getId()) != set.end()) { | 
|---|
| 138 | LOG(4, "DEBUG: Walker " << *Walker << " is bound to " << *OtherWalker << "."); | 
|---|
| 139 | //        if (OtherWalker->getId() > Walker->getId()) { // add bond (Nr check is for adding only one of both variants: ab, ba) | 
|---|
| 140 | ////          std::stringstream output; | 
|---|
| 141 | ////            output << "ACCEPT: Adding Bond: " | 
|---|
| 142 | //          output << Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree()); | 
|---|
| 143 | ////            LOG(3, output.str()); | 
|---|
| 144 | //          //NumBonds[(*iter)->getNr()]++; | 
|---|
| 145 | //        } else { | 
|---|
| 146 | ////            LOG(3, "REJECY: Not adding bond, labels in wrong order."); | 
|---|
| 147 | //        } | 
|---|
| 148 | //        LonelyFlag = false; | 
|---|
| 149 | } else { | 
|---|
| 150 | LOG(4, "DEBUG: Walker " << *Walker << " is bound to " | 
|---|
| 151 | << *OtherWalker << ", who is not in this fragment molecule."); | 
|---|
| 152 | if ((treatment == ExcludeHydrogen) && (OtherWalker->getElementNo() == (atomicNumber_t)1)) { | 
|---|
| 153 | LOG(4, "REJECT: " << *OtherWalker << " is a hydrogen, that are excluded from the set."); | 
|---|
| 154 | FullMolecule.insert(OtherWalker->getId()); | 
|---|
| 155 | } else { | 
|---|
| 156 | LOG(3, "ACCEPT: Adding " << **BondRunner << " as a cut bond."); | 
|---|
| 157 | // there is always at least an empty list | 
|---|
| 158 | CutBonds[Walker].push_back(*BondRunner); | 
|---|
| 159 | } | 
|---|
| 160 | } | 
|---|
| 161 | } | 
|---|
| 162 | } | 
|---|
| 163 | LOG(4, "DEBUG: We have gathered the following CutBonds: " << CutBonds); | 
|---|
| 164 |  | 
|---|
| 165 | // go through all cut bonds and replace with a hydrogen | 
|---|
| 166 | if (saturation == DoSaturate) { | 
|---|
| 167 | for (CutBonds_t::const_iterator atomiter = CutBonds.begin(); | 
|---|
| 168 | atomiter != CutBonds.end(); ++atomiter) { | 
|---|
| 169 | atom * const Walker = atomiter->first; | 
|---|
| 170 | LOG(4, "DEBUG: We are now saturating " << *Walker); | 
|---|
| 171 |  | 
|---|
| 172 | if (!saturateAtom(Walker, atomiter->second)) | 
|---|
| 173 | exit(1); | 
|---|
| 174 | } | 
|---|
| 175 | } else | 
|---|
| 176 | LOG(3, "INFO: We are not saturating cut bonds."); | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | bool SaturatedFragment::saturateAtom( | 
|---|
| 180 | atom * const _atom, | 
|---|
| 181 | const BondList &_cutbonds) | 
|---|
| 182 | { | 
|---|
| 183 | // OLD WAY: use AddHydrogenReplacementAtom() on each cut bond | 
|---|
| 184 | //  // go through each bond and replace | 
|---|
| 185 | //  for (BondList::const_iterator bonditer = _cutbonds.begin(); | 
|---|
| 186 | //      bonditer != _cutbonds.end(); ++bonditer) { | 
|---|
| 187 | //    atom * const OtherWalker = (*bonditer)->GetOtherAtom(_atom); | 
|---|
| 188 | //    if (!AddHydrogenReplacementAtom( | 
|---|
| 189 | //        (*bonditer), | 
|---|
| 190 | //        _atom, | 
|---|
| 191 | //        OtherWalker, | 
|---|
| 192 | //        World::getInstance().getConfig()->IsAngstroem == 1)) | 
|---|
| 193 | //      return false; | 
|---|
| 194 | //  } | 
|---|
| 195 |  | 
|---|
| 196 | SphericalPointDistribution::WeightedPolygon_t Polygon; | 
|---|
| 197 | { | 
|---|
| 198 | // prepare a list of "uncut" bonds via set_difference. For this both lists | 
|---|
| 199 | // have to be sorted. | 
|---|
| 200 | typedef std::vector<bond::ptr> BondVector_t; | 
|---|
| 201 | BondVector_t ListOfBonds(_atom->getListOfBonds().begin(),_atom->getListOfBonds().end()); | 
|---|
| 202 | std::sort(ListOfBonds.begin(), ListOfBonds.end()); | 
|---|
| 203 | BondVector_t CutBonds(_cutbonds.begin(), _cutbonds.end()); | 
|---|
| 204 | std::sort(CutBonds.begin(), CutBonds.end()); | 
|---|
| 205 | const BondVector_t::iterator eraseiter = std::set_difference( | 
|---|
| 206 | ListOfBonds.begin(), ListOfBonds.end(), | 
|---|
| 207 | CutBonds.begin(), CutBonds.end(), | 
|---|
| 208 | ListOfBonds.begin()); | 
|---|
| 209 | ListOfBonds.erase(eraseiter, ListOfBonds.end()); | 
|---|
| 210 |  | 
|---|
| 211 | // gather the nodes of the shape defined by the current set of bonded atoms | 
|---|
| 212 | for (BondVector_t::const_iterator bonditer = ListOfBonds.begin(); | 
|---|
| 213 | bonditer != ListOfBonds.end(); | 
|---|
| 214 | ++bonditer) { | 
|---|
| 215 | Vector DistanceVector; | 
|---|
| 216 | if ((*bonditer)->leftatom == _atom) | 
|---|
| 217 | DistanceVector = (*bonditer)->rightatom->getPosition() - (*bonditer)->leftatom->getPosition(); | 
|---|
| 218 | else | 
|---|
| 219 | DistanceVector = (*bonditer)->leftatom->getPosition() - (*bonditer)->rightatom->getPosition(); | 
|---|
| 220 | // always use unit distances | 
|---|
| 221 | DistanceVector.Normalize(); | 
|---|
| 222 | Polygon.push_back( std::make_pair(DistanceVector, (*bonditer)->getDegree()) ); | 
|---|
| 223 | } | 
|---|
| 224 | LOG(3, "DEBUG: Polygon of atom " << *_atom << " to saturate is " << Polygon); | 
|---|
| 225 | } | 
|---|
| 226 |  | 
|---|
| 227 | unsigned int NumberOfPoints = _atom->getElement().getNoValenceOrbitals(); | 
|---|
| 228 | LOG(3, "DEBUG: There are " << NumberOfPoints | 
|---|
| 229 | << " places to fill in in total for this atom " << *_atom << "."); | 
|---|
| 230 |  | 
|---|
| 231 | // get perfect node distribution for the given remaining atoms with respect | 
|---|
| 232 | // to valence of the atoms (for a saturated fragment, resembles number of bonds) | 
|---|
| 233 | SphericalPointDistribution polygonizer; | 
|---|
| 234 | SphericalPointDistribution::Polygon_t NewPolygon; | 
|---|
| 235 | switch (NumberOfPoints) | 
|---|
| 236 | { | 
|---|
| 237 | case 0: | 
|---|
| 238 | NewPolygon = polygonizer.get<0>(); | 
|---|
| 239 | break; | 
|---|
| 240 | case 1: | 
|---|
| 241 | NewPolygon = polygonizer.get<1>(); | 
|---|
| 242 | break; | 
|---|
| 243 | case 2: | 
|---|
| 244 | NewPolygon = polygonizer.get<2>(); | 
|---|
| 245 | break; | 
|---|
| 246 | case 3: | 
|---|
| 247 | NewPolygon = polygonizer.get<3>(); | 
|---|
| 248 | break; | 
|---|
| 249 | case 4: | 
|---|
| 250 | NewPolygon = polygonizer.get<4>(); | 
|---|
| 251 | break; | 
|---|
| 252 | case 5: | 
|---|
| 253 | NewPolygon = polygonizer.get<5>(); | 
|---|
| 254 | break; | 
|---|
| 255 | case 6: | 
|---|
| 256 | NewPolygon = polygonizer.get<6>(); | 
|---|
| 257 | break; | 
|---|
| 258 | case 7: | 
|---|
| 259 | NewPolygon = polygonizer.get<7>(); | 
|---|
| 260 | break; | 
|---|
| 261 | case 8: | 
|---|
| 262 | NewPolygon = polygonizer.get<8>(); | 
|---|
| 263 | break; | 
|---|
| 264 | case 9: | 
|---|
| 265 | NewPolygon = polygonizer.get<9>(); | 
|---|
| 266 | break; | 
|---|
| 267 | case 10: | 
|---|
| 268 | NewPolygon = polygonizer.get<10>(); | 
|---|
| 269 | break; | 
|---|
| 270 | case 11: | 
|---|
| 271 | NewPolygon = polygonizer.get<11>(); | 
|---|
| 272 | break; | 
|---|
| 273 | case 12: | 
|---|
| 274 | NewPolygon = polygonizer.get<12>(); | 
|---|
| 275 | break; | 
|---|
| 276 | case 14: | 
|---|
| 277 | NewPolygon = polygonizer.get<14>(); | 
|---|
| 278 | break; | 
|---|
| 279 | default: | 
|---|
| 280 | ASSERT(0, "SaturatedFragment::saturateAtom() - cannot deal with the case " | 
|---|
| 281 | +toString(NumberOfPoints)+"."); | 
|---|
| 282 | } | 
|---|
| 283 | LOG(3, "DEBUG: Possible Polygon is " << NewPolygon); | 
|---|
| 284 |  | 
|---|
| 285 | // then we need to match the old with the new | 
|---|
| 286 | SphericalPointDistribution::Polygon_t RemainingPoints = | 
|---|
| 287 | SphericalPointDistribution::matchSphericalPointDistributions(Polygon, NewPolygon); | 
|---|
| 288 |  | 
|---|
| 289 | LOG(3, "INFO: Points identified to fill are " << RemainingPoints); | 
|---|
| 290 |  | 
|---|
| 291 | // and place hydrogen atoms at each vacant spot in the distance given by the table | 
|---|
| 292 | for(SphericalPointDistribution::Polygon_t::const_iterator iter = RemainingPoints.begin(); | 
|---|
| 293 | iter != RemainingPoints.end(); ++iter) { | 
|---|
| 294 | // find nearest atom as father to this point | 
|---|
| 295 | atom * const _father = _atom; | 
|---|
| 296 | LOG(4, "DEBUG: Filling saturation hydrogen for atom " << _atom << " at " << *iter); | 
|---|
| 297 | const atom& hydrogen = setHydrogenReplacement( | 
|---|
| 298 | _atom, | 
|---|
| 299 | *iter, | 
|---|
| 300 | 1., | 
|---|
| 301 | _father); | 
|---|
| 302 | FullMolecule.insert(hydrogen.getId()); | 
|---|
| 303 | } | 
|---|
| 304 |  | 
|---|
| 305 | return true; | 
|---|
| 306 | } | 
|---|
| 307 |  | 
|---|
| 308 |  | 
|---|
| 309 | bool SaturatedFragment::OutputConfig( | 
|---|
| 310 | std::ostream &out, | 
|---|
| 311 | const ParserTypes _type) const | 
|---|
| 312 | { | 
|---|
| 313 | // gather all atoms in a vector | 
|---|
| 314 | std::vector<atom *> atoms; | 
|---|
| 315 | for (KeySet::const_iterator iter = FullMolecule.begin(); | 
|---|
| 316 | iter != FullMolecule.end(); | 
|---|
| 317 | ++iter) { | 
|---|
| 318 | atom * const Walker = World::getInstance().getAtom(AtomById(*iter)); | 
|---|
| 319 | ASSERT( Walker != NULL, | 
|---|
| 320 | "SaturatedFragment::OutputConfig() - id " | 
|---|
| 321 | +toString(*iter)+" is unknown to World."); | 
|---|
| 322 | atoms.push_back(Walker); | 
|---|
| 323 | } | 
|---|
| 324 |  | 
|---|
| 325 | // TODO: ScanForPeriodicCorrection() is missing so far! | 
|---|
| 326 | // note however that this is not straight-forward for the following reasons: | 
|---|
| 327 | // - we do not copy all atoms anymore, hence we are forced to shift the real | 
|---|
| 328 | //   atoms hither and back again | 
|---|
| 329 | // - we use a long-range potential that supports periodic boundary conditions. | 
|---|
| 330 | //   Hence, there we would like the original configuration (split through the | 
|---|
| 331 | //   the periodic boundaries). Otherwise, we would have to shift (and probably | 
|---|
| 332 | //   interpolate) the potential with OBCs applying. | 
|---|
| 333 |  | 
|---|
| 334 | // list atoms in fragment for debugging | 
|---|
| 335 | { | 
|---|
| 336 | std::stringstream output; | 
|---|
| 337 | output << "INFO: Contained atoms: "; | 
|---|
| 338 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
| 339 | iter != atoms.end(); ++iter) { | 
|---|
| 340 | output << (*iter)->getName() << " "; | 
|---|
| 341 | } | 
|---|
| 342 | LOG(3, output.str()); | 
|---|
| 343 | } | 
|---|
| 344 |  | 
|---|
| 345 | // store to stream via FragmentParser | 
|---|
| 346 | const bool intermediateResult = | 
|---|
| 347 | FormatParserStorage::getInstance().save( | 
|---|
| 348 | out, | 
|---|
| 349 | FormatParserStorage::getInstance().getSuffixFromType(_type), | 
|---|
| 350 | atoms); | 
|---|
| 351 |  | 
|---|
| 352 | return intermediateResult; | 
|---|
| 353 | } | 
|---|
| 354 |  | 
|---|
| 355 | atom * const SaturatedFragment::getHydrogenReplacement(atom * const replacement) | 
|---|
| 356 | { | 
|---|
| 357 | atom * const _atom = hydrogens.leaseHydrogen();    // new atom | 
|---|
| 358 | _atom->setAtomicVelocity(replacement->getAtomicVelocity()); // copy velocity | 
|---|
| 359 | _atom->setFixedIon(replacement->getFixedIon()); | 
|---|
| 360 | // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father | 
|---|
| 361 | _atom->father = replacement; | 
|---|
| 362 | SaturationHydrogens.insert(_atom->getId()); | 
|---|
| 363 | return _atom; | 
|---|
| 364 | } | 
|---|
| 365 |  | 
|---|
| 366 | const atom& SaturatedFragment::setHydrogenReplacement( | 
|---|
| 367 | const atom * const _OwnerAtom, | 
|---|
| 368 | const Vector &_position, | 
|---|
| 369 | const double _distance, | 
|---|
| 370 | atom * const _father) | 
|---|
| 371 | { | 
|---|
| 372 | atom * const _atom = hydrogens.leaseHydrogen();    // new atom | 
|---|
| 373 | _atom->setPosition( _OwnerAtom->getPosition() + _distance * _position ); | 
|---|
| 374 | // always set as fixed ion (not moving during molecular dynamics simulation) | 
|---|
| 375 | _atom->setFixedIon(true); | 
|---|
| 376 | // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father | 
|---|
| 377 | _atom->father = _father; | 
|---|
| 378 | SaturationHydrogens.insert(_atom->getId()); | 
|---|
| 379 | return *_atom; | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | bool SaturatedFragment::AddHydrogenReplacementAtom( | 
|---|
| 383 | bond::ptr TopBond, | 
|---|
| 384 | atom *Origin, | 
|---|
| 385 | atom *Replacement, | 
|---|
| 386 | bool IsAngstroem) | 
|---|
| 387 | { | 
|---|
| 388 | //  Info info(__func__); | 
|---|
| 389 | bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit | 
|---|
| 390 | double bondlength;  // bond length of the bond to be replaced/cut | 
|---|
| 391 | double bondangle;  // bond angle of the bond to be replaced/cut | 
|---|
| 392 | double BondRescale;   // rescale value for the hydrogen bond length | 
|---|
| 393 | bond::ptr FirstBond; | 
|---|
| 394 | bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane | 
|---|
| 395 | atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added | 
|---|
| 396 | double b,l,d,f,g, alpha, factors[NDIM];    // hold temporary values in triple bond case for coordination determination | 
|---|
| 397 | Vector Orthovector1, Orthovector2;  // temporary vectors in coordination construction | 
|---|
| 398 | Vector InBondvector;    // vector in direction of *Bond | 
|---|
| 399 | const RealSpaceMatrix &matrix =  World::getInstance().getDomain().getM(); | 
|---|
| 400 | bond::ptr Binder; | 
|---|
| 401 |  | 
|---|
| 402 | // create vector in direction of bond | 
|---|
| 403 | InBondvector = Replacement->getPosition() - Origin->getPosition(); | 
|---|
| 404 | bondlength = InBondvector.Norm(); | 
|---|
| 405 |  | 
|---|
| 406 | // is greater than typical bond distance? Then we have to correct periodically | 
|---|
| 407 | // the problem is not the H being out of the box, but InBondvector have the wrong direction | 
|---|
| 408 | // due to Replacement or Origin being on the wrong side! | 
|---|
| 409 | const BondGraph * const BG = World::getInstance().getBondGraph(); | 
|---|
| 410 | const range<double> MinMaxBondDistance( | 
|---|
| 411 | BG->getMinMaxDistance(Origin,Replacement)); | 
|---|
| 412 | if (!MinMaxBondDistance.isInRange(bondlength)) { | 
|---|
| 413 | //    LOG(4, "InBondvector is: " << InBondvector << "."); | 
|---|
| 414 | Orthovector1.Zero(); | 
|---|
| 415 | for (int i=NDIM;i--;) { | 
|---|
| 416 | l = Replacement->at(i) - Origin->at(i); | 
|---|
| 417 | if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here) | 
|---|
| 418 | Orthovector1[i] = (l < 0) ? -1. : +1.; | 
|---|
| 419 | } // (signs are correct, was tested!) | 
|---|
| 420 | } | 
|---|
| 421 | Orthovector1 *= matrix; | 
|---|
| 422 | InBondvector -= Orthovector1; // subtract just the additional translation | 
|---|
| 423 | bondlength = InBondvector.Norm(); | 
|---|
| 424 | //    LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << "."); | 
|---|
| 425 | } // periodic correction finished | 
|---|
| 426 |  | 
|---|
| 427 | InBondvector.Normalize(); | 
|---|
| 428 | // get typical bond length and store as scale factor for later | 
|---|
| 429 | ASSERT(Origin->getType() != NULL, | 
|---|
| 430 | "SaturatedFragment::AddHydrogenReplacementAtom() - element of Origin is not given."); | 
|---|
| 431 | BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()-1); | 
|---|
| 432 | if (BondRescale == -1) { | 
|---|
| 433 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!"); | 
|---|
| 434 | BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()); | 
|---|
| 435 | if (BondRescale == -1) { | 
|---|
| 436 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of any degree!"); | 
|---|
| 437 | return false; | 
|---|
| 438 | BondRescale = bondlength; | 
|---|
| 439 | } | 
|---|
| 440 | } else { | 
|---|
| 441 | if (!IsAngstroem) | 
|---|
| 442 | BondRescale /= (1.*AtomicLengthToAngstroem); | 
|---|
| 443 | } | 
|---|
| 444 |  | 
|---|
| 445 | // discern single, double and triple bonds | 
|---|
| 446 | switch(TopBond->getDegree()) { | 
|---|
| 447 | case 1: | 
|---|
| 448 | // check whether replacement has been an excluded hydrogen | 
|---|
| 449 | if (Replacement->getType()->getAtomicNumber() == HydrogenPool::HYDROGEN) { // neither rescale nor replace if it's already hydrogen | 
|---|
| 450 | FirstOtherAtom = Replacement; | 
|---|
| 451 | BondRescale = bondlength; | 
|---|
| 452 | } else { | 
|---|
| 453 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 454 | InBondvector *= BondRescale;   // rescale the distance vector to Hydrogen bond length | 
|---|
| 455 | FirstOtherAtom->setPosition(Origin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom | 
|---|
| 456 | } | 
|---|
| 457 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
| 458 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
| 459 | break; | 
|---|
| 460 | case 2: | 
|---|
| 461 | { | 
|---|
| 462 | // determine two other bonds (warning if there are more than two other) plus valence sanity check | 
|---|
| 463 | const BondList& ListOfBonds = Origin->getListOfBonds(); | 
|---|
| 464 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
| 465 | Runner != ListOfBonds.end(); | 
|---|
| 466 | ++Runner) { | 
|---|
| 467 | if ((*Runner) != TopBond) { | 
|---|
| 468 | if (FirstBond == NULL) { | 
|---|
| 469 | FirstBond = (*Runner); | 
|---|
| 470 | FirstOtherAtom = (*Runner)->GetOtherAtom(Origin); | 
|---|
| 471 | } else if (SecondBond == NULL) { | 
|---|
| 472 | SecondBond = (*Runner); | 
|---|
| 473 | SecondOtherAtom = (*Runner)->GetOtherAtom(Origin); | 
|---|
| 474 | } else { | 
|---|
| 475 | ELOG(2, "Detected more than four bonds for atom " << Origin->getName()); | 
|---|
| 476 | } | 
|---|
| 477 | } | 
|---|
| 478 | } | 
|---|
| 479 | } | 
|---|
| 480 | if (SecondOtherAtom == NULL) {  // then we have an atom with valence four, but only 3 bonds: one to replace and one which is TopBond (third is FirstBond) | 
|---|
| 481 | SecondBond = TopBond; | 
|---|
| 482 | SecondOtherAtom = Replacement; | 
|---|
| 483 | } | 
|---|
| 484 | if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all | 
|---|
| 485 | //        LOG(3, "Regarding the double bond (" << Origin->Name << "<->" << Replacement->Name << ") to be constructed: Taking " << FirstOtherAtom->Name << " and " << SecondOtherAtom->Name << " along with " << Origin->Name << " to determine orthogonal plane."); | 
|---|
| 486 |  | 
|---|
| 487 | // determine the plane of these two with the *origin | 
|---|
| 488 | try { | 
|---|
| 489 | Orthovector1 = Plane(Origin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal(); | 
|---|
| 490 | } | 
|---|
| 491 | catch(LinearDependenceException &excp){ | 
|---|
| 492 | LOG(0, boost::diagnostic_information(excp)); | 
|---|
| 493 | // TODO: figure out what to do with the Orthovector in this case | 
|---|
| 494 | AllWentWell = false; | 
|---|
| 495 | } | 
|---|
| 496 | } else { | 
|---|
| 497 | Orthovector1.GetOneNormalVector(InBondvector); | 
|---|
| 498 | } | 
|---|
| 499 | //LOG(3, "INFO: Orthovector1: " << Orthovector1 << "."); | 
|---|
| 500 | // orthogonal vector and bond vector between origin and replacement form the new plane | 
|---|
| 501 | Orthovector1.MakeNormalTo(InBondvector); | 
|---|
| 502 | Orthovector1.Normalize(); | 
|---|
| 503 | //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << "."); | 
|---|
| 504 |  | 
|---|
| 505 | // create the two Hydrogens ... | 
|---|
| 506 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 507 | SecondOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 508 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
| 509 | FullMolecule.insert(SecondOtherAtom->getId()); | 
|---|
| 510 | bondangle = Origin->getType()->getHBondAngle(1); | 
|---|
| 511 | if (bondangle == -1) { | 
|---|
| 512 | ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!"); | 
|---|
| 513 | return false; | 
|---|
| 514 | bondangle = 0; | 
|---|
| 515 | } | 
|---|
| 516 | bondangle *= M_PI/180./2.; | 
|---|
| 517 | //      LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << "."); | 
|---|
| 518 | //      LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle)); | 
|---|
| 519 | FirstOtherAtom->Zero(); | 
|---|
| 520 | SecondOtherAtom->Zero(); | 
|---|
| 521 | for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction) | 
|---|
| 522 | FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle))); | 
|---|
| 523 | SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle))); | 
|---|
| 524 | } | 
|---|
| 525 | FirstOtherAtom->Scale(BondRescale);  // rescale by correct BondDistance | 
|---|
| 526 | SecondOtherAtom->Scale(BondRescale); | 
|---|
| 527 | //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << "."); | 
|---|
| 528 | *FirstOtherAtom += Origin->getPosition(); | 
|---|
| 529 | *SecondOtherAtom += Origin->getPosition(); | 
|---|
| 530 | // ... and add to molecule | 
|---|
| 531 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
| 532 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << "."); | 
|---|
| 533 | break; | 
|---|
| 534 | case 3: | 
|---|
| 535 | // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid) | 
|---|
| 536 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 537 | SecondOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 538 | ThirdOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
| 539 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
| 540 | FullMolecule.insert(SecondOtherAtom->getId()); | 
|---|
| 541 | FullMolecule.insert(ThirdOtherAtom->getId()); | 
|---|
| 542 |  | 
|---|
| 543 | // we need to vectors orthonormal the InBondvector | 
|---|
| 544 | AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector); | 
|---|
| 545 | //      LOG(3, "INFO: Orthovector1: " << Orthovector1 << "."); | 
|---|
| 546 | try{ | 
|---|
| 547 | Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal(); | 
|---|
| 548 | } | 
|---|
| 549 | catch(LinearDependenceException &excp) { | 
|---|
| 550 | LOG(0, boost::diagnostic_information(excp)); | 
|---|
| 551 | AllWentWell = false; | 
|---|
| 552 | } | 
|---|
| 553 | //      LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".") | 
|---|
| 554 |  | 
|---|
| 555 | // create correct coordination for the three atoms | 
|---|
| 556 | alpha = (Origin->getType()->getHBondAngle(2))/180.*M_PI/2.;  // retrieve triple bond angle from database | 
|---|
| 557 | l = BondRescale;        // desired bond length | 
|---|
| 558 | b = 2.*l*sin(alpha);    // base length of isosceles triangle | 
|---|
| 559 | d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.);   // length for InBondvector | 
|---|
| 560 | f = b/sqrt(3.);   // length for Orthvector1 | 
|---|
| 561 | g = b/2.;         // length for Orthvector2 | 
|---|
| 562 | //      LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", "); | 
|---|
| 563 | //      LOG(3, "The three Bond lengths: " << sqrt(d*d+f*f) << ", " << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g) << ", "  << sqrt(d*d+(-0.5*f)*(-0.5*f)+g*g)); | 
|---|
| 564 | factors[0] = d; | 
|---|
| 565 | factors[1] = f; | 
|---|
| 566 | factors[2] = 0.; | 
|---|
| 567 | FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
| 568 | factors[1] = -0.5*f; | 
|---|
| 569 | factors[2] = g; | 
|---|
| 570 | SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
| 571 | factors[2] = -g; | 
|---|
| 572 | ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
| 573 |  | 
|---|
| 574 | // rescale each to correct BondDistance | 
|---|
| 575 | //      FirstOtherAtom->x.Scale(&BondRescale); | 
|---|
| 576 | //      SecondOtherAtom->x.Scale(&BondRescale); | 
|---|
| 577 | //      ThirdOtherAtom->x.Scale(&BondRescale); | 
|---|
| 578 |  | 
|---|
| 579 | // and relative to *origin atom | 
|---|
| 580 | *FirstOtherAtom += Origin->getPosition(); | 
|---|
| 581 | *SecondOtherAtom += Origin->getPosition(); | 
|---|
| 582 | *ThirdOtherAtom += Origin->getPosition(); | 
|---|
| 583 |  | 
|---|
| 584 | // ... and add to molecule | 
|---|
| 585 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
| 586 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << "."); | 
|---|
| 587 | //      LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << "."); | 
|---|
| 588 | break; | 
|---|
| 589 | default: | 
|---|
| 590 | ELOG(1, "BondDegree does not state single, double or triple bond!"); | 
|---|
| 591 | AllWentWell = false; | 
|---|
| 592 | break; | 
|---|
| 593 | } | 
|---|
| 594 |  | 
|---|
| 595 | return AllWentWell; | 
|---|
| 596 | }; | 
|---|