| [7d5fcd] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2013 University of Bonn. All rights reserved. | 
|---|
| [5aaa43] | 5 | * Copyright (C)  2013 Frederik Heber. All rights reserved. | 
|---|
| [7d5fcd] | 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 |  | 
|---|
| [e139180] | 40 | #include <algorithm> | 
|---|
| [c39675] | 41 | #include <cmath> | 
|---|
|  | 42 | #include <iostream> | 
|---|
|  | 43 |  | 
|---|
| [7d5fcd] | 44 | #include "CodePatterns/Assert.hpp" | 
|---|
| [c39675] | 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" | 
|---|
| [7d5fcd] | 51 |  | 
|---|
| [c39675] | 52 | #include "Atom/atom.hpp" | 
|---|
|  | 53 | #include "Bond/bond.hpp" | 
|---|
|  | 54 | #include "config.hpp" | 
|---|
|  | 55 | #include "Descriptors/AtomIdDescriptor.hpp" | 
|---|
| [7d5fcd] | 56 | #include "Fragmentation/Exporters/HydrogenPool.hpp" | 
|---|
| [97dff0] | 57 | #include "Fragmentation/Exporters/SphericalPointDistribution.hpp" | 
|---|
| [c39675] | 58 | #include "Fragmentation/HydrogenSaturation_enum.hpp" | 
|---|
|  | 59 | #include "Graph/BondGraph.hpp" | 
|---|
|  | 60 | #include "World.hpp" | 
|---|
| [7d5fcd] | 61 |  | 
|---|
|  | 62 | SaturatedFragment::SaturatedFragment( | 
|---|
|  | 63 | const KeySet &_set, | 
|---|
|  | 64 | KeySetsInUse_t &_container, | 
|---|
| [c39675] | 65 | HydrogenPool &_hydrogens, | 
|---|
|  | 66 | const enum HydrogenTreatment _treatment, | 
|---|
|  | 67 | const enum HydrogenSaturation _saturation) : | 
|---|
| [7d5fcd] | 68 | container(_container), | 
|---|
|  | 69 | set(_set), | 
|---|
|  | 70 | hydrogens(_hydrogens), | 
|---|
| [c39675] | 71 | FullMolecule(set), | 
|---|
|  | 72 | treatment(_treatment), | 
|---|
|  | 73 | saturation(_saturation) | 
|---|
| [7d5fcd] | 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 |  | 
|---|
| [c39675] | 81 | // prepare saturation hydrogens | 
|---|
|  | 82 | saturate(); | 
|---|
| [7d5fcd] | 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 | } | 
|---|
| [c39675] | 102 |  | 
|---|
|  | 103 | void SaturatedFragment::saturate() | 
|---|
|  | 104 | { | 
|---|
| [9d3264] | 105 | // so far, we just have a set of keys. Hence, convert to atom refs | 
|---|
|  | 106 | // and gather all atoms in a vector | 
|---|
| [c39675] | 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 | } | 
|---|
| [e139180] | 117 | LOG(4, "DEBUG: We have gathered the following atoms: " << atoms); | 
|---|
| [c39675] | 118 |  | 
|---|
|  | 119 | //  bool LonelyFlag = false; | 
|---|
| [9d3264] | 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; | 
|---|
| [c39675] | 123 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
|  | 124 | iter != atoms.end(); | 
|---|
|  | 125 | ++iter) { | 
|---|
|  | 126 | atom * const Walker = *iter; | 
|---|
| [e139180] | 127 | // start with an empty list | 
|---|
|  | 128 | CutBonds.insert( std::make_pair(Walker, BondList() )); | 
|---|
| [c39675] | 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); | 
|---|
| [9d3264] | 136 | // if other atom is in key set | 
|---|
| [c39675] | 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: " | 
|---|
| [1f693d] | 142 | //          output << Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree()); | 
|---|
| [c39675] | 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."); | 
|---|
| [e139180] | 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 | 
|---|
| [9d3264] | 158 | CutBonds[Walker].push_back(*BondRunner); | 
|---|
| [c39675] | 159 | } | 
|---|
|  | 160 | } | 
|---|
|  | 161 | } | 
|---|
|  | 162 | } | 
|---|
| [e139180] | 163 | LOG(4, "DEBUG: We have gathered the following CutBonds: " << CutBonds); | 
|---|
| [9d3264] | 164 |  | 
|---|
|  | 165 | // go through all cut bonds and replace with a hydrogen | 
|---|
| [e139180] | 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."); | 
|---|
| [97dff0] | 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 |  | 
|---|
| [260540] | 196 | SphericalPointDistribution::WeightedPolygon_t Polygon; | 
|---|
| [e139180] | 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(); | 
|---|
| [260540] | 222 | Polygon.push_back( std::make_pair(DistanceVector, (*bonditer)->getDegree()) ); | 
|---|
| [e139180] | 223 | } | 
|---|
|  | 224 | LOG(3, "DEBUG: Polygon of atom " << *_atom << " to saturate is " << Polygon); | 
|---|
| [97dff0] | 225 | } | 
|---|
| [e139180] | 226 |  | 
|---|
| [260540] | 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 << "."); | 
|---|
| [97dff0] | 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) | 
|---|
| [e6ca85] | 233 | // then get the number of vacant spots | 
|---|
| [97dff0] | 234 | SphericalPointDistribution polygonizer; | 
|---|
|  | 235 | SphericalPointDistribution::Polygon_t RemainingPoints = | 
|---|
| [e6ca85] | 236 | polygonizer.getRemainingPoints(Polygon, NumberOfPoints); | 
|---|
| [97dff0] | 237 |  | 
|---|
| [90426a] | 238 | LOG(3, "INFO: Points identified to fill are " << RemainingPoints); | 
|---|
|  | 239 |  | 
|---|
| [97dff0] | 240 | // and place hydrogen atoms at each vacant spot in the distance given by the table | 
|---|
|  | 241 | for(SphericalPointDistribution::Polygon_t::const_iterator iter = RemainingPoints.begin(); | 
|---|
|  | 242 | iter != RemainingPoints.end(); ++iter) { | 
|---|
|  | 243 | // find nearest atom as father to this point | 
|---|
|  | 244 | atom * const _father = _atom; | 
|---|
| [90426a] | 245 | LOG(4, "DEBUG: Filling saturation hydrogen for atom " << _atom << " at " << *iter); | 
|---|
| [c1413b] | 246 | double BondRescale = _atom->getType()->getHBondDistance(1.); | 
|---|
|  | 247 | if (BondRescale == -1) { | 
|---|
|  | 248 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond from atom " | 
|---|
|  | 249 | << _atom->getName() << " of degree 1!"); | 
|---|
|  | 250 | BondRescale = 1.; | 
|---|
|  | 251 | } | 
|---|
| [e139180] | 252 | const atom& hydrogen = setHydrogenReplacement( | 
|---|
| [97dff0] | 253 | _atom, | 
|---|
|  | 254 | *iter, | 
|---|
| [c1413b] | 255 | BondRescale, | 
|---|
| [97dff0] | 256 | _father); | 
|---|
| [e139180] | 257 | FullMolecule.insert(hydrogen.getId()); | 
|---|
| [97dff0] | 258 | } | 
|---|
|  | 259 |  | 
|---|
|  | 260 | return true; | 
|---|
| [c39675] | 261 | } | 
|---|
|  | 262 |  | 
|---|
| [97dff0] | 263 |  | 
|---|
| [c39675] | 264 | bool SaturatedFragment::OutputConfig( | 
|---|
|  | 265 | std::ostream &out, | 
|---|
|  | 266 | const ParserTypes _type) const | 
|---|
|  | 267 | { | 
|---|
|  | 268 | // gather all atoms in a vector | 
|---|
|  | 269 | std::vector<atom *> atoms; | 
|---|
|  | 270 | for (KeySet::const_iterator iter = FullMolecule.begin(); | 
|---|
|  | 271 | iter != FullMolecule.end(); | 
|---|
|  | 272 | ++iter) { | 
|---|
|  | 273 | atom * const Walker = World::getInstance().getAtom(AtomById(*iter)); | 
|---|
|  | 274 | ASSERT( Walker != NULL, | 
|---|
|  | 275 | "SaturatedFragment::OutputConfig() - id " | 
|---|
|  | 276 | +toString(*iter)+" is unknown to World."); | 
|---|
|  | 277 | atoms.push_back(Walker); | 
|---|
|  | 278 | } | 
|---|
|  | 279 |  | 
|---|
|  | 280 | // TODO: ScanForPeriodicCorrection() is missing so far! | 
|---|
|  | 281 | // note however that this is not straight-forward for the following reasons: | 
|---|
|  | 282 | // - we do not copy all atoms anymore, hence we are forced to shift the real | 
|---|
|  | 283 | //   atoms hither and back again | 
|---|
|  | 284 | // - we use a long-range potential that supports periodic boundary conditions. | 
|---|
|  | 285 | //   Hence, there we would like the original configuration (split through the | 
|---|
|  | 286 | //   the periodic boundaries). Otherwise, we would have to shift (and probably | 
|---|
|  | 287 | //   interpolate) the potential with OBCs applying. | 
|---|
|  | 288 |  | 
|---|
|  | 289 | // list atoms in fragment for debugging | 
|---|
|  | 290 | { | 
|---|
|  | 291 | std::stringstream output; | 
|---|
|  | 292 | output << "INFO: Contained atoms: "; | 
|---|
|  | 293 | for (std::vector<atom *>::const_iterator iter = atoms.begin(); | 
|---|
|  | 294 | iter != atoms.end(); ++iter) { | 
|---|
|  | 295 | output << (*iter)->getName() << " "; | 
|---|
|  | 296 | } | 
|---|
|  | 297 | LOG(3, output.str()); | 
|---|
|  | 298 | } | 
|---|
|  | 299 |  | 
|---|
|  | 300 | // store to stream via FragmentParser | 
|---|
|  | 301 | const bool intermediateResult = | 
|---|
|  | 302 | FormatParserStorage::getInstance().save( | 
|---|
|  | 303 | out, | 
|---|
|  | 304 | FormatParserStorage::getInstance().getSuffixFromType(_type), | 
|---|
|  | 305 | atoms); | 
|---|
|  | 306 |  | 
|---|
|  | 307 | return intermediateResult; | 
|---|
|  | 308 | } | 
|---|
|  | 309 |  | 
|---|
|  | 310 | atom * const SaturatedFragment::getHydrogenReplacement(atom * const replacement) | 
|---|
|  | 311 | { | 
|---|
|  | 312 | atom * const _atom = hydrogens.leaseHydrogen();    // new atom | 
|---|
|  | 313 | _atom->setAtomicVelocity(replacement->getAtomicVelocity()); // copy velocity | 
|---|
|  | 314 | _atom->setFixedIon(replacement->getFixedIon()); | 
|---|
|  | 315 | // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father | 
|---|
|  | 316 | _atom->father = replacement; | 
|---|
|  | 317 | SaturationHydrogens.insert(_atom->getId()); | 
|---|
|  | 318 | return _atom; | 
|---|
|  | 319 | } | 
|---|
|  | 320 |  | 
|---|
| [e139180] | 321 | const atom& SaturatedFragment::setHydrogenReplacement( | 
|---|
| [97dff0] | 322 | const atom * const _OwnerAtom, | 
|---|
|  | 323 | const Vector &_position, | 
|---|
|  | 324 | const double _distance, | 
|---|
|  | 325 | atom * const _father) | 
|---|
|  | 326 | { | 
|---|
|  | 327 | atom * const _atom = hydrogens.leaseHydrogen();    // new atom | 
|---|
|  | 328 | _atom->setPosition( _OwnerAtom->getPosition() + _distance * _position ); | 
|---|
|  | 329 | // always set as fixed ion (not moving during molecular dynamics simulation) | 
|---|
|  | 330 | _atom->setFixedIon(true); | 
|---|
|  | 331 | // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father | 
|---|
|  | 332 | _atom->father = _father; | 
|---|
|  | 333 | SaturationHydrogens.insert(_atom->getId()); | 
|---|
| [e139180] | 334 | return *_atom; | 
|---|
| [97dff0] | 335 | } | 
|---|
|  | 336 |  | 
|---|
| [c39675] | 337 | bool SaturatedFragment::AddHydrogenReplacementAtom( | 
|---|
|  | 338 | bond::ptr TopBond, | 
|---|
|  | 339 | atom *Origin, | 
|---|
|  | 340 | atom *Replacement, | 
|---|
|  | 341 | bool IsAngstroem) | 
|---|
|  | 342 | { | 
|---|
|  | 343 | //  Info info(__func__); | 
|---|
|  | 344 | bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit | 
|---|
|  | 345 | double bondlength;  // bond length of the bond to be replaced/cut | 
|---|
|  | 346 | double bondangle;  // bond angle of the bond to be replaced/cut | 
|---|
|  | 347 | double BondRescale;   // rescale value for the hydrogen bond length | 
|---|
|  | 348 | bond::ptr FirstBond; | 
|---|
|  | 349 | bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane | 
|---|
|  | 350 | atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added | 
|---|
|  | 351 | double b,l,d,f,g, alpha, factors[NDIM];    // hold temporary values in triple bond case for coordination determination | 
|---|
|  | 352 | Vector Orthovector1, Orthovector2;  // temporary vectors in coordination construction | 
|---|
|  | 353 | Vector InBondvector;    // vector in direction of *Bond | 
|---|
|  | 354 | const RealSpaceMatrix &matrix =  World::getInstance().getDomain().getM(); | 
|---|
|  | 355 | bond::ptr Binder; | 
|---|
|  | 356 |  | 
|---|
|  | 357 | // create vector in direction of bond | 
|---|
|  | 358 | InBondvector = Replacement->getPosition() - Origin->getPosition(); | 
|---|
|  | 359 | bondlength = InBondvector.Norm(); | 
|---|
|  | 360 |  | 
|---|
|  | 361 | // is greater than typical bond distance? Then we have to correct periodically | 
|---|
|  | 362 | // the problem is not the H being out of the box, but InBondvector have the wrong direction | 
|---|
|  | 363 | // due to Replacement or Origin being on the wrong side! | 
|---|
|  | 364 | const BondGraph * const BG = World::getInstance().getBondGraph(); | 
|---|
|  | 365 | const range<double> MinMaxBondDistance( | 
|---|
|  | 366 | BG->getMinMaxDistance(Origin,Replacement)); | 
|---|
|  | 367 | if (!MinMaxBondDistance.isInRange(bondlength)) { | 
|---|
|  | 368 | //    LOG(4, "InBondvector is: " << InBondvector << "."); | 
|---|
|  | 369 | Orthovector1.Zero(); | 
|---|
|  | 370 | for (int i=NDIM;i--;) { | 
|---|
|  | 371 | l = Replacement->at(i) - Origin->at(i); | 
|---|
|  | 372 | if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here) | 
|---|
|  | 373 | Orthovector1[i] = (l < 0) ? -1. : +1.; | 
|---|
|  | 374 | } // (signs are correct, was tested!) | 
|---|
|  | 375 | } | 
|---|
|  | 376 | Orthovector1 *= matrix; | 
|---|
|  | 377 | InBondvector -= Orthovector1; // subtract just the additional translation | 
|---|
|  | 378 | bondlength = InBondvector.Norm(); | 
|---|
|  | 379 | //    LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << "."); | 
|---|
|  | 380 | } // periodic correction finished | 
|---|
|  | 381 |  | 
|---|
|  | 382 | InBondvector.Normalize(); | 
|---|
|  | 383 | // get typical bond length and store as scale factor for later | 
|---|
|  | 384 | ASSERT(Origin->getType() != NULL, | 
|---|
|  | 385 | "SaturatedFragment::AddHydrogenReplacementAtom() - element of Origin is not given."); | 
|---|
| [1f693d] | 386 | BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()-1); | 
|---|
| [c39675] | 387 | if (BondRescale == -1) { | 
|---|
| [1f693d] | 388 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!"); | 
|---|
| [3fbdca] | 389 | BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()); | 
|---|
|  | 390 | if (BondRescale == -1) { | 
|---|
|  | 391 | ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of any degree!"); | 
|---|
|  | 392 | return false; | 
|---|
|  | 393 | BondRescale = bondlength; | 
|---|
|  | 394 | } | 
|---|
| [c39675] | 395 | } else { | 
|---|
|  | 396 | if (!IsAngstroem) | 
|---|
|  | 397 | BondRescale /= (1.*AtomicLengthToAngstroem); | 
|---|
|  | 398 | } | 
|---|
|  | 399 |  | 
|---|
|  | 400 | // discern single, double and triple bonds | 
|---|
| [1f693d] | 401 | switch(TopBond->getDegree()) { | 
|---|
| [c39675] | 402 | case 1: | 
|---|
|  | 403 | // check whether replacement has been an excluded hydrogen | 
|---|
|  | 404 | if (Replacement->getType()->getAtomicNumber() == HydrogenPool::HYDROGEN) { // neither rescale nor replace if it's already hydrogen | 
|---|
|  | 405 | FirstOtherAtom = Replacement; | 
|---|
|  | 406 | BondRescale = bondlength; | 
|---|
|  | 407 | } else { | 
|---|
|  | 408 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 409 | InBondvector *= BondRescale;   // rescale the distance vector to Hydrogen bond length | 
|---|
|  | 410 | FirstOtherAtom->setPosition(Origin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom | 
|---|
|  | 411 | } | 
|---|
|  | 412 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
|  | 413 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
|  | 414 | break; | 
|---|
|  | 415 | case 2: | 
|---|
|  | 416 | { | 
|---|
|  | 417 | // determine two other bonds (warning if there are more than two other) plus valence sanity check | 
|---|
|  | 418 | const BondList& ListOfBonds = Origin->getListOfBonds(); | 
|---|
|  | 419 | for (BondList::const_iterator Runner = ListOfBonds.begin(); | 
|---|
|  | 420 | Runner != ListOfBonds.end(); | 
|---|
|  | 421 | ++Runner) { | 
|---|
|  | 422 | if ((*Runner) != TopBond) { | 
|---|
|  | 423 | if (FirstBond == NULL) { | 
|---|
|  | 424 | FirstBond = (*Runner); | 
|---|
|  | 425 | FirstOtherAtom = (*Runner)->GetOtherAtom(Origin); | 
|---|
|  | 426 | } else if (SecondBond == NULL) { | 
|---|
|  | 427 | SecondBond = (*Runner); | 
|---|
|  | 428 | SecondOtherAtom = (*Runner)->GetOtherAtom(Origin); | 
|---|
|  | 429 | } else { | 
|---|
|  | 430 | ELOG(2, "Detected more than four bonds for atom " << Origin->getName()); | 
|---|
|  | 431 | } | 
|---|
|  | 432 | } | 
|---|
|  | 433 | } | 
|---|
|  | 434 | } | 
|---|
|  | 435 | 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) | 
|---|
|  | 436 | SecondBond = TopBond; | 
|---|
|  | 437 | SecondOtherAtom = Replacement; | 
|---|
|  | 438 | } | 
|---|
|  | 439 | if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all | 
|---|
|  | 440 | //        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."); | 
|---|
|  | 441 |  | 
|---|
|  | 442 | // determine the plane of these two with the *origin | 
|---|
|  | 443 | try { | 
|---|
|  | 444 | Orthovector1 = Plane(Origin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal(); | 
|---|
|  | 445 | } | 
|---|
|  | 446 | catch(LinearDependenceException &excp){ | 
|---|
|  | 447 | LOG(0, boost::diagnostic_information(excp)); | 
|---|
|  | 448 | // TODO: figure out what to do with the Orthovector in this case | 
|---|
|  | 449 | AllWentWell = false; | 
|---|
|  | 450 | } | 
|---|
|  | 451 | } else { | 
|---|
|  | 452 | Orthovector1.GetOneNormalVector(InBondvector); | 
|---|
|  | 453 | } | 
|---|
|  | 454 | //LOG(3, "INFO: Orthovector1: " << Orthovector1 << "."); | 
|---|
|  | 455 | // orthogonal vector and bond vector between origin and replacement form the new plane | 
|---|
|  | 456 | Orthovector1.MakeNormalTo(InBondvector); | 
|---|
|  | 457 | Orthovector1.Normalize(); | 
|---|
|  | 458 | //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << "."); | 
|---|
|  | 459 |  | 
|---|
|  | 460 | // create the two Hydrogens ... | 
|---|
|  | 461 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 462 | SecondOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 463 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
|  | 464 | FullMolecule.insert(SecondOtherAtom->getId()); | 
|---|
|  | 465 | bondangle = Origin->getType()->getHBondAngle(1); | 
|---|
|  | 466 | if (bondangle == -1) { | 
|---|
| [1f693d] | 467 | ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!"); | 
|---|
| [c39675] | 468 | return false; | 
|---|
|  | 469 | bondangle = 0; | 
|---|
|  | 470 | } | 
|---|
|  | 471 | bondangle *= M_PI/180./2.; | 
|---|
|  | 472 | //      LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << "."); | 
|---|
|  | 473 | //      LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle)); | 
|---|
|  | 474 | FirstOtherAtom->Zero(); | 
|---|
|  | 475 | SecondOtherAtom->Zero(); | 
|---|
|  | 476 | for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction) | 
|---|
|  | 477 | FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle))); | 
|---|
|  | 478 | SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle))); | 
|---|
|  | 479 | } | 
|---|
|  | 480 | FirstOtherAtom->Scale(BondRescale);  // rescale by correct BondDistance | 
|---|
|  | 481 | SecondOtherAtom->Scale(BondRescale); | 
|---|
|  | 482 | //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << "."); | 
|---|
|  | 483 | *FirstOtherAtom += Origin->getPosition(); | 
|---|
|  | 484 | *SecondOtherAtom += Origin->getPosition(); | 
|---|
|  | 485 | // ... and add to molecule | 
|---|
|  | 486 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
|  | 487 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << "."); | 
|---|
|  | 488 | break; | 
|---|
|  | 489 | case 3: | 
|---|
|  | 490 | // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid) | 
|---|
|  | 491 | FirstOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 492 | SecondOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 493 | ThirdOtherAtom = getHydrogenReplacement(Replacement); | 
|---|
|  | 494 | FullMolecule.insert(FirstOtherAtom->getId()); | 
|---|
|  | 495 | FullMolecule.insert(SecondOtherAtom->getId()); | 
|---|
|  | 496 | FullMolecule.insert(ThirdOtherAtom->getId()); | 
|---|
|  | 497 |  | 
|---|
|  | 498 | // we need to vectors orthonormal the InBondvector | 
|---|
|  | 499 | AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector); | 
|---|
|  | 500 | //      LOG(3, "INFO: Orthovector1: " << Orthovector1 << "."); | 
|---|
|  | 501 | try{ | 
|---|
|  | 502 | Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal(); | 
|---|
|  | 503 | } | 
|---|
|  | 504 | catch(LinearDependenceException &excp) { | 
|---|
|  | 505 | LOG(0, boost::diagnostic_information(excp)); | 
|---|
|  | 506 | AllWentWell = false; | 
|---|
|  | 507 | } | 
|---|
|  | 508 | //      LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".") | 
|---|
|  | 509 |  | 
|---|
|  | 510 | // create correct coordination for the three atoms | 
|---|
|  | 511 | alpha = (Origin->getType()->getHBondAngle(2))/180.*M_PI/2.;  // retrieve triple bond angle from database | 
|---|
|  | 512 | l = BondRescale;        // desired bond length | 
|---|
|  | 513 | b = 2.*l*sin(alpha);    // base length of isosceles triangle | 
|---|
|  | 514 | d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.);   // length for InBondvector | 
|---|
|  | 515 | f = b/sqrt(3.);   // length for Orthvector1 | 
|---|
|  | 516 | g = b/2.;         // length for Orthvector2 | 
|---|
|  | 517 | //      LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", "); | 
|---|
|  | 518 | //      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)); | 
|---|
|  | 519 | factors[0] = d; | 
|---|
|  | 520 | factors[1] = f; | 
|---|
|  | 521 | factors[2] = 0.; | 
|---|
|  | 522 | FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
|  | 523 | factors[1] = -0.5*f; | 
|---|
|  | 524 | factors[2] = g; | 
|---|
|  | 525 | SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
|  | 526 | factors[2] = -g; | 
|---|
|  | 527 | ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors); | 
|---|
|  | 528 |  | 
|---|
|  | 529 | // rescale each to correct BondDistance | 
|---|
|  | 530 | //      FirstOtherAtom->x.Scale(&BondRescale); | 
|---|
|  | 531 | //      SecondOtherAtom->x.Scale(&BondRescale); | 
|---|
|  | 532 | //      ThirdOtherAtom->x.Scale(&BondRescale); | 
|---|
|  | 533 |  | 
|---|
|  | 534 | // and relative to *origin atom | 
|---|
|  | 535 | *FirstOtherAtom += Origin->getPosition(); | 
|---|
|  | 536 | *SecondOtherAtom += Origin->getPosition(); | 
|---|
|  | 537 | *ThirdOtherAtom += Origin->getPosition(); | 
|---|
|  | 538 |  | 
|---|
|  | 539 | // ... and add to molecule | 
|---|
|  | 540 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << "."); | 
|---|
|  | 541 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << "."); | 
|---|
|  | 542 | //      LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << "."); | 
|---|
|  | 543 | break; | 
|---|
|  | 544 | default: | 
|---|
|  | 545 | ELOG(1, "BondDegree does not state single, double or triple bond!"); | 
|---|
|  | 546 | AllWentWell = false; | 
|---|
|  | 547 | break; | 
|---|
|  | 548 | } | 
|---|
|  | 549 |  | 
|---|
|  | 550 | return AllWentWell; | 
|---|
|  | 551 | }; | 
|---|