| [7d5fcd] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2013 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * 
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *   This file is part of MoleCuilder.
 | 
|---|
 | 8 |  *
 | 
|---|
 | 9 |  *    MoleCuilder is free software: you can redistribute it and/or modify
 | 
|---|
 | 10 |  *    it under the terms of the GNU General Public License as published by
 | 
|---|
 | 11 |  *    the Free Software Foundation, either version 2 of the License, or
 | 
|---|
 | 12 |  *    (at your option) any later version.
 | 
|---|
 | 13 |  *
 | 
|---|
 | 14 |  *    MoleCuilder is distributed in the hope that it will be useful,
 | 
|---|
 | 15 |  *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | 
|---|
 | 16 |  *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | 
|---|
 | 17 |  *    GNU General Public License for more details.
 | 
|---|
 | 18 |  *
 | 
|---|
 | 19 |  *    You should have received a copy of the GNU General Public License
 | 
|---|
 | 20 |  *    along with MoleCuilder.  If not, see <http://www.gnu.org/licenses/>.
 | 
|---|
 | 21 |  */
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /*
 | 
|---|
 | 24 |  * SaturatedFragment.cpp
 | 
|---|
 | 25 |  *
 | 
|---|
 | 26 |  *  Created on: Mar 3, 2013
 | 
|---|
 | 27 |  *      Author: heber
 | 
|---|
 | 28 |  */
 | 
|---|
 | 29 | 
 | 
|---|
 | 30 | // include config.h
 | 
|---|
 | 31 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 32 | #include <config.h>
 | 
|---|
 | 33 | #endif
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 36 | 
 | 
|---|
 | 37 | #include "SaturatedFragment.hpp"
 | 
|---|
 | 38 | 
 | 
|---|
| [c39675] | 39 | #include <cmath>
 | 
|---|
 | 40 | #include <iostream>
 | 
|---|
 | 41 | 
 | 
|---|
| [7d5fcd] | 42 | #include "CodePatterns/Assert.hpp"
 | 
|---|
| [c39675] | 43 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 44 | 
 | 
|---|
 | 45 | #include "LinearAlgebra/Exceptions.hpp"
 | 
|---|
 | 46 | #include "LinearAlgebra/Plane.hpp"
 | 
|---|
 | 47 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
 | 48 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [7d5fcd] | 49 | 
 | 
|---|
| [c39675] | 50 | #include "Atom/atom.hpp"
 | 
|---|
 | 51 | #include "Bond/bond.hpp"
 | 
|---|
 | 52 | #include "config.hpp"
 | 
|---|
 | 53 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [7d5fcd] | 54 | #include "Fragmentation/Exporters/HydrogenPool.hpp"
 | 
|---|
| [c39675] | 55 | #include "Fragmentation/HydrogenSaturation_enum.hpp"
 | 
|---|
 | 56 | #include "Graph/BondGraph.hpp"
 | 
|---|
 | 57 | #include "World.hpp"
 | 
|---|
| [7d5fcd] | 58 | 
 | 
|---|
 | 59 | SaturatedFragment::SaturatedFragment(
 | 
|---|
 | 60 |     const KeySet &_set,
 | 
|---|
 | 61 |     KeySetsInUse_t &_container,
 | 
|---|
| [c39675] | 62 |     HydrogenPool &_hydrogens,
 | 
|---|
 | 63 |     const enum HydrogenTreatment _treatment,
 | 
|---|
 | 64 |     const enum HydrogenSaturation _saturation) :
 | 
|---|
| [7d5fcd] | 65 |   container(_container),
 | 
|---|
 | 66 |   set(_set),
 | 
|---|
 | 67 |   hydrogens(_hydrogens),
 | 
|---|
| [c39675] | 68 |   FullMolecule(set),
 | 
|---|
 | 69 |   treatment(_treatment),
 | 
|---|
 | 70 |   saturation(_saturation)
 | 
|---|
| [7d5fcd] | 71 | {
 | 
|---|
 | 72 |   // add to in-use container
 | 
|---|
 | 73 |   ASSERT( container.find(set) == container.end(),
 | 
|---|
 | 74 |       "SaturatedFragment::SaturatedFragment() - the set "
 | 
|---|
 | 75 |       +toString(set)+" is already marked as in use.");
 | 
|---|
 | 76 |   container.insert(set);
 | 
|---|
 | 77 | 
 | 
|---|
| [c39675] | 78 |   // prepare saturation hydrogens
 | 
|---|
 | 79 |   saturate();
 | 
|---|
| [7d5fcd] | 80 | }
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 | SaturatedFragment::~SaturatedFragment()
 | 
|---|
 | 83 | {
 | 
|---|
 | 84 |   // release all saturation hydrogens if present
 | 
|---|
 | 85 |   for (KeySet::iterator iter = SaturationHydrogens.begin();
 | 
|---|
 | 86 |       !SaturationHydrogens.empty();
 | 
|---|
 | 87 |       iter = SaturationHydrogens.begin()) {
 | 
|---|
 | 88 |     hydrogens.releaseHydrogen(*iter);
 | 
|---|
 | 89 |     SaturationHydrogens.erase(iter);
 | 
|---|
 | 90 |   }
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |   // remove ourselves from in-use container
 | 
|---|
 | 93 |   KeySetsInUse_t::iterator iter = container.find(set);
 | 
|---|
 | 94 |   ASSERT( container.find(set) != container.end(),
 | 
|---|
 | 95 |       "SaturatedFragment::SaturatedFragment() - the set "
 | 
|---|
 | 96 |       +toString(set)+" is not marked as in use.");
 | 
|---|
 | 97 |   container.erase(iter);
 | 
|---|
 | 98 | }
 | 
|---|
| [c39675] | 99 | 
 | 
|---|
 | 100 | void SaturatedFragment::saturate()
 | 
|---|
 | 101 | {
 | 
|---|
 | 102 |   // gather all atoms in a vector
 | 
|---|
 | 103 |   std::vector<atom *> atoms;
 | 
|---|
 | 104 |   for (KeySet::const_iterator iter = FullMolecule.begin();
 | 
|---|
 | 105 |       iter != FullMolecule.end();
 | 
|---|
 | 106 |       ++iter) {
 | 
|---|
 | 107 |     atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
 | 
|---|
 | 108 |     ASSERT( Walker != NULL,
 | 
|---|
 | 109 |         "SaturatedFragment::OutputConfig() - id "
 | 
|---|
 | 110 |         +toString(*iter)+" is unknown to World.");
 | 
|---|
 | 111 |     atoms.push_back(Walker);
 | 
|---|
 | 112 |   }
 | 
|---|
 | 113 | 
 | 
|---|
 | 114 | //  bool LonelyFlag = false;
 | 
|---|
 | 115 |   for (std::vector<atom *>::const_iterator iter = atoms.begin();
 | 
|---|
 | 116 |       iter != atoms.end();
 | 
|---|
 | 117 |       ++iter) {
 | 
|---|
 | 118 |     atom * const Walker = *iter;
 | 
|---|
 | 119 | 
 | 
|---|
 | 120 |     // go through all bonds
 | 
|---|
 | 121 |     const BondList& ListOfBonds = Walker->getListOfBonds();
 | 
|---|
 | 122 |     for (BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
 | 123 |         BondRunner != ListOfBonds.end();
 | 
|---|
 | 124 |         ++BondRunner) {
 | 
|---|
 | 125 |       atom * const OtherWalker = (*BondRunner)->GetOtherAtom(Walker);
 | 
|---|
 | 126 |       // if in set
 | 
|---|
 | 127 |       if (set.find(OtherWalker->getId()) != set.end()) {
 | 
|---|
 | 128 |         LOG(4, "DEBUG: Walker " << *Walker << " is bound to " << *OtherWalker << ".");
 | 
|---|
 | 129 | //        if (OtherWalker->getId() > Walker->getId()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
 | 
|---|
 | 130 | ////          std::stringstream output;
 | 
|---|
 | 131 | ////            output << "ACCEPT: Adding Bond: "
 | 
|---|
| [1f693d] | 132 | //          output << Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
 | 
|---|
| [c39675] | 133 | ////            LOG(3, output.str());
 | 
|---|
 | 134 | //          //NumBonds[(*iter)->getNr()]++;
 | 
|---|
 | 135 | //        } else {
 | 
|---|
 | 136 | ////            LOG(3, "REJECY: Not adding bond, labels in wrong order.");
 | 
|---|
 | 137 | //        }
 | 
|---|
 | 138 | //        LonelyFlag = false;
 | 
|---|
 | 139 |       } else {
 | 
|---|
 | 140 |         LOG(4, "DEBUG: Walker " << *Walker << " is bound to "
 | 
|---|
 | 141 |             << *OtherWalker << ", who is not in this fragment molecule.");
 | 
|---|
 | 142 |         if (saturation == DoSaturate) {
 | 
|---|
 | 143 | //          LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between.");
 | 
|---|
 | 144 |           if (!AddHydrogenReplacementAtom(
 | 
|---|
 | 145 |               (*BondRunner),
 | 
|---|
 | 146 |               Walker,
 | 
|---|
 | 147 |               OtherWalker,
 | 
|---|
 | 148 |               World::getInstance().getConfig()->IsAngstroem == 1))
 | 
|---|
 | 149 |             exit(1);
 | 
|---|
 | 150 |         }
 | 
|---|
 | 151 | //        } else if ((treatment == ExcludeHydrogen) && (OtherWalker->getElementNo() == (atomicNumber_t)1)) {
 | 
|---|
 | 152 | //          // just copy the atom if it's a hydrogen
 | 
|---|
 | 153 | //          atom * const OtherWalker = Leaf->AddCopyAtom(OtherWalker);
 | 
|---|
| [1f693d] | 154 | //          Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->getDegree());
 | 
|---|
| [c39675] | 155 | //        }
 | 
|---|
| [1f693d] | 156 |         //NumBonds[(*iter)->getNr()] += Binder->getDegree();
 | 
|---|
| [c39675] | 157 |       }
 | 
|---|
 | 158 |     }
 | 
|---|
 | 159 |   }
 | 
|---|
 | 160 | }
 | 
|---|
 | 161 | 
 | 
|---|
 | 162 | bool SaturatedFragment::OutputConfig(
 | 
|---|
 | 163 |     std::ostream &out,
 | 
|---|
 | 164 |     const ParserTypes _type) const
 | 
|---|
 | 165 | {
 | 
|---|
 | 166 |   // gather all atoms in a vector
 | 
|---|
 | 167 |   std::vector<atom *> atoms;
 | 
|---|
 | 168 |   for (KeySet::const_iterator iter = FullMolecule.begin();
 | 
|---|
 | 169 |       iter != FullMolecule.end();
 | 
|---|
 | 170 |       ++iter) {
 | 
|---|
 | 171 |     atom * const Walker = World::getInstance().getAtom(AtomById(*iter));
 | 
|---|
 | 172 |     ASSERT( Walker != NULL,
 | 
|---|
 | 173 |         "SaturatedFragment::OutputConfig() - id "
 | 
|---|
 | 174 |         +toString(*iter)+" is unknown to World.");
 | 
|---|
 | 175 |     atoms.push_back(Walker);
 | 
|---|
 | 176 |   }
 | 
|---|
 | 177 | 
 | 
|---|
 | 178 |   // TODO: ScanForPeriodicCorrection() is missing so far!
 | 
|---|
 | 179 |   // note however that this is not straight-forward for the following reasons:
 | 
|---|
 | 180 |   // - we do not copy all atoms anymore, hence we are forced to shift the real
 | 
|---|
 | 181 |   //   atoms hither and back again
 | 
|---|
 | 182 |   // - we use a long-range potential that supports periodic boundary conditions.
 | 
|---|
 | 183 |   //   Hence, there we would like the original configuration (split through the
 | 
|---|
 | 184 |   //   the periodic boundaries). Otherwise, we would have to shift (and probably
 | 
|---|
 | 185 |   //   interpolate) the potential with OBCs applying.
 | 
|---|
 | 186 | 
 | 
|---|
 | 187 |   // list atoms in fragment for debugging
 | 
|---|
 | 188 |   {
 | 
|---|
 | 189 |     std::stringstream output;
 | 
|---|
 | 190 |     output << "INFO: Contained atoms: ";
 | 
|---|
 | 191 |     for (std::vector<atom *>::const_iterator iter = atoms.begin();
 | 
|---|
 | 192 |         iter != atoms.end(); ++iter) {
 | 
|---|
 | 193 |       output << (*iter)->getName() << " ";
 | 
|---|
 | 194 |     }
 | 
|---|
 | 195 |     LOG(3, output.str());
 | 
|---|
 | 196 |   }
 | 
|---|
 | 197 | 
 | 
|---|
 | 198 |   // store to stream via FragmentParser
 | 
|---|
 | 199 |   const bool intermediateResult =
 | 
|---|
 | 200 |       FormatParserStorage::getInstance().save(
 | 
|---|
 | 201 |           out,
 | 
|---|
 | 202 |           FormatParserStorage::getInstance().getSuffixFromType(_type),
 | 
|---|
 | 203 |           atoms);
 | 
|---|
 | 204 | 
 | 
|---|
 | 205 |   return intermediateResult;
 | 
|---|
 | 206 | }
 | 
|---|
 | 207 | 
 | 
|---|
 | 208 | atom * const SaturatedFragment::getHydrogenReplacement(atom * const replacement)
 | 
|---|
 | 209 | {
 | 
|---|
 | 210 |   atom * const _atom = hydrogens.leaseHydrogen();    // new atom
 | 
|---|
 | 211 |   _atom->setAtomicVelocity(replacement->getAtomicVelocity()); // copy velocity
 | 
|---|
 | 212 |   _atom->setFixedIon(replacement->getFixedIon());
 | 
|---|
 | 213 |   // if we replace hydrogen, we mark it as our father, otherwise we are just an added hydrogen with no father
 | 
|---|
 | 214 |   _atom->father = replacement;
 | 
|---|
 | 215 |   SaturationHydrogens.insert(_atom->getId());
 | 
|---|
 | 216 |   return _atom;
 | 
|---|
 | 217 | }
 | 
|---|
 | 218 | 
 | 
|---|
 | 219 | bool SaturatedFragment::AddHydrogenReplacementAtom(
 | 
|---|
 | 220 |     bond::ptr TopBond,
 | 
|---|
 | 221 |     atom *Origin,
 | 
|---|
 | 222 |     atom *Replacement,
 | 
|---|
 | 223 |     bool IsAngstroem)
 | 
|---|
 | 224 | {
 | 
|---|
 | 225 | //  Info info(__func__);
 | 
|---|
 | 226 |   bool AllWentWell = true;    // flag gathering the boolean return value of molecule::AddAtom and other functions, as return value on exit
 | 
|---|
 | 227 |   double bondlength;  // bond length of the bond to be replaced/cut
 | 
|---|
 | 228 |   double bondangle;  // bond angle of the bond to be replaced/cut
 | 
|---|
 | 229 |   double BondRescale;   // rescale value for the hydrogen bond length
 | 
|---|
 | 230 |   bond::ptr FirstBond;
 | 
|---|
 | 231 |   bond::ptr SecondBond; // Other bonds in double bond case to determine "other" plane
 | 
|---|
 | 232 |   atom *FirstOtherAtom = NULL, *SecondOtherAtom = NULL, *ThirdOtherAtom = NULL; // pointer to hydrogen atoms to be added
 | 
|---|
 | 233 |   double b,l,d,f,g, alpha, factors[NDIM];    // hold temporary values in triple bond case for coordination determination
 | 
|---|
 | 234 |   Vector Orthovector1, Orthovector2;  // temporary vectors in coordination construction
 | 
|---|
 | 235 |   Vector InBondvector;    // vector in direction of *Bond
 | 
|---|
 | 236 |   const RealSpaceMatrix &matrix =  World::getInstance().getDomain().getM();
 | 
|---|
 | 237 |   bond::ptr Binder;
 | 
|---|
 | 238 | 
 | 
|---|
 | 239 |   // create vector in direction of bond
 | 
|---|
 | 240 |   InBondvector = Replacement->getPosition() - Origin->getPosition();
 | 
|---|
 | 241 |   bondlength = InBondvector.Norm();
 | 
|---|
 | 242 | 
 | 
|---|
 | 243 |    // is greater than typical bond distance? Then we have to correct periodically
 | 
|---|
 | 244 |    // the problem is not the H being out of the box, but InBondvector have the wrong direction
 | 
|---|
 | 245 |    // due to Replacement or Origin being on the wrong side!
 | 
|---|
 | 246 |   const BondGraph * const BG = World::getInstance().getBondGraph();
 | 
|---|
 | 247 |   const range<double> MinMaxBondDistance(
 | 
|---|
 | 248 |       BG->getMinMaxDistance(Origin,Replacement));
 | 
|---|
 | 249 |   if (!MinMaxBondDistance.isInRange(bondlength)) {
 | 
|---|
 | 250 | //    LOG(4, "InBondvector is: " << InBondvector << ".");
 | 
|---|
 | 251 |     Orthovector1.Zero();
 | 
|---|
 | 252 |     for (int i=NDIM;i--;) {
 | 
|---|
 | 253 |       l = Replacement->at(i) - Origin->at(i);
 | 
|---|
 | 254 |       if (fabs(l) > MinMaxBondDistance.last) { // is component greater than bond distance (check against min not useful here)
 | 
|---|
 | 255 |         Orthovector1[i] = (l < 0) ? -1. : +1.;
 | 
|---|
 | 256 |       } // (signs are correct, was tested!)
 | 
|---|
 | 257 |     }
 | 
|---|
 | 258 |     Orthovector1 *= matrix;
 | 
|---|
 | 259 |     InBondvector -= Orthovector1; // subtract just the additional translation
 | 
|---|
 | 260 |     bondlength = InBondvector.Norm();
 | 
|---|
 | 261 | //    LOG(4, "INFO: Corrected InBondvector is now: " << InBondvector << ".");
 | 
|---|
 | 262 |   } // periodic correction finished
 | 
|---|
 | 263 | 
 | 
|---|
 | 264 |   InBondvector.Normalize();
 | 
|---|
 | 265 |   // get typical bond length and store as scale factor for later
 | 
|---|
 | 266 |   ASSERT(Origin->getType() != NULL,
 | 
|---|
 | 267 |       "SaturatedFragment::AddHydrogenReplacementAtom() - element of Origin is not given.");
 | 
|---|
| [1f693d] | 268 |   BondRescale = Origin->getType()->getHBondDistance(TopBond->getDegree()-1);
 | 
|---|
| [c39675] | 269 |   if (BondRescale == -1) {
 | 
|---|
| [1f693d] | 270 |     ELOG(1, "There is no typical hydrogen bond distance in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
 | 
|---|
| [c39675] | 271 |     return false;
 | 
|---|
 | 272 |     BondRescale = bondlength;
 | 
|---|
 | 273 |   } else {
 | 
|---|
 | 274 |     if (!IsAngstroem)
 | 
|---|
 | 275 |       BondRescale /= (1.*AtomicLengthToAngstroem);
 | 
|---|
 | 276 |   }
 | 
|---|
 | 277 | 
 | 
|---|
 | 278 |   // discern single, double and triple bonds
 | 
|---|
| [1f693d] | 279 |   switch(TopBond->getDegree()) {
 | 
|---|
| [c39675] | 280 |     case 1:
 | 
|---|
 | 281 |       // check whether replacement has been an excluded hydrogen
 | 
|---|
 | 282 |       if (Replacement->getType()->getAtomicNumber() == HydrogenPool::HYDROGEN) { // neither rescale nor replace if it's already hydrogen
 | 
|---|
 | 283 |         FirstOtherAtom = Replacement;
 | 
|---|
 | 284 |         BondRescale = bondlength;
 | 
|---|
 | 285 |       } else {
 | 
|---|
 | 286 |         FirstOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 287 |         InBondvector *= BondRescale;   // rescale the distance vector to Hydrogen bond length
 | 
|---|
 | 288 |         FirstOtherAtom->setPosition(Origin->getPosition() + InBondvector); // set coordination to origin and add distance vector to replacement atom
 | 
|---|
 | 289 |       }
 | 
|---|
 | 290 |       FullMolecule.insert(FirstOtherAtom->getId());
 | 
|---|
 | 291 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 292 |       break;
 | 
|---|
 | 293 |     case 2:
 | 
|---|
 | 294 |       {
 | 
|---|
 | 295 |         // determine two other bonds (warning if there are more than two other) plus valence sanity check
 | 
|---|
 | 296 |         const BondList& ListOfBonds = Origin->getListOfBonds();
 | 
|---|
 | 297 |         for (BondList::const_iterator Runner = ListOfBonds.begin();
 | 
|---|
 | 298 |             Runner != ListOfBonds.end();
 | 
|---|
 | 299 |             ++Runner) {
 | 
|---|
 | 300 |           if ((*Runner) != TopBond) {
 | 
|---|
 | 301 |             if (FirstBond == NULL) {
 | 
|---|
 | 302 |               FirstBond = (*Runner);
 | 
|---|
 | 303 |               FirstOtherAtom = (*Runner)->GetOtherAtom(Origin);
 | 
|---|
 | 304 |             } else if (SecondBond == NULL) {
 | 
|---|
 | 305 |               SecondBond = (*Runner);
 | 
|---|
 | 306 |               SecondOtherAtom = (*Runner)->GetOtherAtom(Origin);
 | 
|---|
 | 307 |             } else {
 | 
|---|
 | 308 |               ELOG(2, "Detected more than four bonds for atom " << Origin->getName());
 | 
|---|
 | 309 |             }
 | 
|---|
 | 310 |           }
 | 
|---|
 | 311 |         }
 | 
|---|
 | 312 |       }
 | 
|---|
 | 313 |       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)
 | 
|---|
 | 314 |         SecondBond = TopBond;
 | 
|---|
 | 315 |         SecondOtherAtom = Replacement;
 | 
|---|
 | 316 |       }
 | 
|---|
 | 317 |       if (FirstOtherAtom != NULL) { // then we just have this double bond and the plane does not matter at all
 | 
|---|
 | 318 | //        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.");
 | 
|---|
 | 319 | 
 | 
|---|
 | 320 |         // determine the plane of these two with the *origin
 | 
|---|
 | 321 |         try {
 | 
|---|
 | 322 |           Orthovector1 = Plane(Origin->getPosition(), FirstOtherAtom->getPosition(), SecondOtherAtom->getPosition()).getNormal();
 | 
|---|
 | 323 |         }
 | 
|---|
 | 324 |         catch(LinearDependenceException &excp){
 | 
|---|
 | 325 |           LOG(0, boost::diagnostic_information(excp));
 | 
|---|
 | 326 |           // TODO: figure out what to do with the Orthovector in this case
 | 
|---|
 | 327 |           AllWentWell = false;
 | 
|---|
 | 328 |         }
 | 
|---|
 | 329 |       } else {
 | 
|---|
 | 330 |         Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
 | 331 |       }
 | 
|---|
 | 332 |       //LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
 | 333 |       // orthogonal vector and bond vector between origin and replacement form the new plane
 | 
|---|
 | 334 |       Orthovector1.MakeNormalTo(InBondvector);
 | 
|---|
 | 335 |       Orthovector1.Normalize();
 | 
|---|
 | 336 |       //LOG(3, "ReScaleCheck: " << Orthovector1.Norm() << " and " << InBondvector.Norm() << ".");
 | 
|---|
 | 337 | 
 | 
|---|
 | 338 |       // create the two Hydrogens ...
 | 
|---|
 | 339 |       FirstOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 340 |       SecondOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 341 |       FullMolecule.insert(FirstOtherAtom->getId());
 | 
|---|
 | 342 |       FullMolecule.insert(SecondOtherAtom->getId());
 | 
|---|
 | 343 |       bondangle = Origin->getType()->getHBondAngle(1);
 | 
|---|
 | 344 |       if (bondangle == -1) {
 | 
|---|
| [1f693d] | 345 |         ELOG(1, "There is no typical hydrogen bond angle in replacing bond (" << Origin->getName() << "<->" << Replacement->getName() << ") of degree " << TopBond->getDegree() << "!");
 | 
|---|
| [c39675] | 346 |         return false;
 | 
|---|
 | 347 |         bondangle = 0;
 | 
|---|
 | 348 |       }
 | 
|---|
 | 349 |       bondangle *= M_PI/180./2.;
 | 
|---|
 | 350 | //      LOG(3, "INFO: ReScaleCheck: InBondvector " << InBondvector << ", " << Orthovector1 << ".");
 | 
|---|
 | 351 | //      LOG(3, "Half the bond angle is " << bondangle << ", sin and cos of it: " << sin(bondangle) << ", " << cos(bondangle));
 | 
|---|
 | 352 |       FirstOtherAtom->Zero();
 | 
|---|
 | 353 |       SecondOtherAtom->Zero();
 | 
|---|
 | 354 |       for(int i=NDIM;i--;) { // rotate by half the bond angle in both directions (InBondvector is bondangle = 0 direction)
 | 
|---|
 | 355 |         FirstOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (sin(bondangle)));
 | 
|---|
 | 356 |         SecondOtherAtom->set(i, InBondvector[i] * cos(bondangle) + Orthovector1[i] * (-sin(bondangle)));
 | 
|---|
 | 357 |       }
 | 
|---|
 | 358 |       FirstOtherAtom->Scale(BondRescale);  // rescale by correct BondDistance
 | 
|---|
 | 359 |       SecondOtherAtom->Scale(BondRescale);
 | 
|---|
 | 360 |       //LOG(3, "ReScaleCheck: " << FirstOtherAtom->x.Norm() << " and " << SecondOtherAtom->x.Norm() << ".");
 | 
|---|
 | 361 |       *FirstOtherAtom += Origin->getPosition();
 | 
|---|
 | 362 |       *SecondOtherAtom += Origin->getPosition();
 | 
|---|
 | 363 |       // ... and add to molecule
 | 
|---|
 | 364 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 365 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
 | 
|---|
 | 366 |       break;
 | 
|---|
 | 367 |     case 3:
 | 
|---|
 | 368 |       // take the "usual" tetraoidal angle and add the three Hydrogen in direction of the bond (height of the tetraoid)
 | 
|---|
 | 369 |       FirstOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 370 |       SecondOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 371 |       ThirdOtherAtom = getHydrogenReplacement(Replacement);
 | 
|---|
 | 372 |       FullMolecule.insert(FirstOtherAtom->getId());
 | 
|---|
 | 373 |       FullMolecule.insert(SecondOtherAtom->getId());
 | 
|---|
 | 374 |       FullMolecule.insert(ThirdOtherAtom->getId());
 | 
|---|
 | 375 | 
 | 
|---|
 | 376 |       // we need to vectors orthonormal the InBondvector
 | 
|---|
 | 377 |       AllWentWell = AllWentWell && Orthovector1.GetOneNormalVector(InBondvector);
 | 
|---|
 | 378 | //      LOG(3, "INFO: Orthovector1: " << Orthovector1 << ".");
 | 
|---|
 | 379 |       try{
 | 
|---|
 | 380 |         Orthovector2 = Plane(InBondvector, Orthovector1,0).getNormal();
 | 
|---|
 | 381 |       }
 | 
|---|
 | 382 |       catch(LinearDependenceException &excp) {
 | 
|---|
 | 383 |         LOG(0, boost::diagnostic_information(excp));
 | 
|---|
 | 384 |         AllWentWell = false;
 | 
|---|
 | 385 |       }
 | 
|---|
 | 386 | //      LOG(3, "INFO: Orthovector2: " << Orthovector2 << ".")
 | 
|---|
 | 387 | 
 | 
|---|
 | 388 |       // create correct coordination for the three atoms
 | 
|---|
 | 389 |       alpha = (Origin->getType()->getHBondAngle(2))/180.*M_PI/2.;  // retrieve triple bond angle from database
 | 
|---|
 | 390 |       l = BondRescale;        // desired bond length
 | 
|---|
 | 391 |       b = 2.*l*sin(alpha);    // base length of isosceles triangle
 | 
|---|
 | 392 |       d = l*sqrt(cos(alpha)*cos(alpha) - sin(alpha)*sin(alpha)/3.);   // length for InBondvector
 | 
|---|
 | 393 |       f = b/sqrt(3.);   // length for Orthvector1
 | 
|---|
 | 394 |       g = b/2.;         // length for Orthvector2
 | 
|---|
 | 395 | //      LOG(3, "Bond length and half-angle: " << l << ", " << alpha << "\t (b,d,f,g) = " << b << ", " << d << ", " << f << ", " << g << ", ");
 | 
|---|
 | 396 | //      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));
 | 
|---|
 | 397 |       factors[0] = d;
 | 
|---|
 | 398 |       factors[1] = f;
 | 
|---|
 | 399 |       factors[2] = 0.;
 | 
|---|
 | 400 |       FirstOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
 | 401 |       factors[1] = -0.5*f;
 | 
|---|
 | 402 |       factors[2] = g;
 | 
|---|
 | 403 |       SecondOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
 | 404 |       factors[2] = -g;
 | 
|---|
 | 405 |       ThirdOtherAtom->LinearCombinationOfVectors(InBondvector, Orthovector1, Orthovector2, factors);
 | 
|---|
 | 406 | 
 | 
|---|
 | 407 |       // rescale each to correct BondDistance
 | 
|---|
 | 408 | //      FirstOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 409 | //      SecondOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 410 | //      ThirdOtherAtom->x.Scale(&BondRescale);
 | 
|---|
 | 411 | 
 | 
|---|
 | 412 |       // and relative to *origin atom
 | 
|---|
 | 413 |       *FirstOtherAtom += Origin->getPosition();
 | 
|---|
 | 414 |       *SecondOtherAtom += Origin->getPosition();
 | 
|---|
 | 415 |       *ThirdOtherAtom += Origin->getPosition();
 | 
|---|
 | 416 | 
 | 
|---|
 | 417 |       // ... and add to molecule
 | 
|---|
 | 418 | //      LOG(4, "INFO: Added " << *FirstOtherAtom << " at: " << FirstOtherAtom->x << ".");
 | 
|---|
 | 419 | //      LOG(4, "INFO: Added " << *SecondOtherAtom << " at: " << SecondOtherAtom->x << ".");
 | 
|---|
 | 420 | //      LOG(4, "INFO: Added " << *ThirdOtherAtom << " at: " << ThirdOtherAtom->x << ".");
 | 
|---|
 | 421 |       break;
 | 
|---|
 | 422 |     default:
 | 
|---|
 | 423 |       ELOG(1, "BondDegree does not state single, double or triple bond!");
 | 
|---|
 | 424 |       AllWentWell = false;
 | 
|---|
 | 425 |       break;
 | 
|---|
 | 426 |   }
 | 
|---|
 | 427 | 
 | 
|---|
 | 428 |   return AllWentWell;
 | 
|---|
 | 429 | };
 | 
|---|