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