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