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