| [0331ee] | 1 | /* | 
|---|
|  | 2 | * Project: MoleCuilder | 
|---|
|  | 3 | * Description: creates and alters molecular systems | 
|---|
|  | 4 | * Copyright (C)  2013 Frederik Heber. 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 | * Interfragmenter.cpp | 
|---|
|  | 25 | * | 
|---|
|  | 26 | *  Created on: Jul 5, 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 "Interfragmenter.hpp" | 
|---|
|  | 38 |  | 
|---|
|  | 39 | #include <list> | 
|---|
|  | 40 | #include <map> | 
|---|
|  | 41 |  | 
|---|
|  | 42 | #include "CodePatterns/Assert.hpp" | 
|---|
|  | 43 | #include "CodePatterns/Log.hpp" | 
|---|
|  | 44 |  | 
|---|
|  | 45 | #include "LinearAlgebra/Vector.hpp" | 
|---|
|  | 46 |  | 
|---|
|  | 47 | #include "AtomIdSet.hpp" | 
|---|
|  | 48 | #include "Element/element.hpp" | 
|---|
|  | 49 | #include "Fragmentation/Graph.hpp" | 
|---|
|  | 50 | #include "Fragmentation/KeySet.hpp" | 
|---|
|  | 51 | #include "LinkedCell/LinkedCell_View.hpp" | 
|---|
|  | 52 | #include "LinkedCell/types.hpp" | 
|---|
|  | 53 | #include "World.hpp" | 
|---|
|  | 54 |  | 
|---|
|  | 55 | void Interfragmenter::operator()( | 
|---|
|  | 56 | size_t MaxOrder, | 
|---|
|  | 57 | double Rcut, | 
|---|
|  | 58 | const enum HydrogenTreatment treatment) | 
|---|
|  | 59 | { | 
|---|
|  | 60 | /// create a map of atom to keyset (below equal MaxOrder) | 
|---|
|  | 61 | typedef std::list<const KeySet *> keysets_t; | 
|---|
|  | 62 | typedef std::map<const atom *, keysets_t > atomkeyset_t; | 
|---|
|  | 63 | atomkeyset_t atomkeyset; | 
|---|
|  | 64 | LOG(1, "INFO: Placing all atoms and their keysets into a map."); | 
|---|
|  | 65 | for (Graph::const_iterator keysetiter = TotalGraph.begin(); | 
|---|
|  | 66 | keysetiter != TotalGraph.end(); ++keysetiter) { | 
|---|
|  | 67 | const KeySet &keyset = keysetiter->first; | 
|---|
|  | 68 | LOG(2, "DEBUG: Current keyset is " << keyset); | 
|---|
|  | 69 | const AtomIdSet atoms(keyset); | 
|---|
|  | 70 | const size_t atoms_size = atoms.getAtomIds().size(); | 
|---|
|  | 71 | if ((atoms_size > MaxOrder) || (atoms_size == 0)) | 
|---|
|  | 72 | continue; | 
|---|
|  | 73 | for (AtomIdSet::const_iterator atomiter = atoms.begin(); | 
|---|
|  | 74 | atomiter != atoms.end(); ++atomiter) { | 
|---|
|  | 75 | // either create new list ... | 
|---|
|  | 76 | std::pair<atomkeyset_t::iterator, bool> inserter = | 
|---|
|  | 77 | atomkeyset.insert( std::make_pair(*atomiter, keysets_t(1, &keyset) )); | 
|---|
|  | 78 | // ... or push to end | 
|---|
|  | 79 | if (inserter.second) { | 
|---|
|  | 80 | LOG(3, "DEBUG: Created new entry in map."); | 
|---|
|  | 81 | } else { | 
|---|
|  | 82 | LOG(3, "DEBUG: Added keyset to present entry."); | 
|---|
|  | 83 | inserter.first->second.push_back(&keyset); | 
|---|
|  | 84 | } | 
|---|
|  | 85 | } | 
|---|
|  | 86 | } | 
|---|
|  | 87 | LOG(2, "DEBUG: There are " << atomkeyset.size() << " entries in lookup."); | 
|---|
|  | 88 |  | 
|---|
|  | 89 | Graph InterFragments; | 
|---|
|  | 90 | int counter = TotalGraph.size(); | 
|---|
|  | 91 |  | 
|---|
|  | 92 | /// go through all fragments up to MaxOrder | 
|---|
|  | 93 | LOG(1, "INFO: Creating inter-fragments."); | 
|---|
|  | 94 | for (Graph::const_iterator keysetiter = TotalGraph.begin(); | 
|---|
|  | 95 | keysetiter != TotalGraph.end(); ++keysetiter) { | 
|---|
|  | 96 | const KeySet &keyset = keysetiter->first; | 
|---|
|  | 97 | LOG(2, "DEBUG: Current keyset is " << keyset); | 
|---|
|  | 98 | const AtomIdSet atoms(keyset); | 
|---|
|  | 99 | const size_t atoms_size = atoms.getAtomIds().size(); | 
|---|
|  | 100 | if ((atoms_size > MaxOrder) || (atoms_size == 0)) | 
|---|
|  | 101 | continue; | 
|---|
|  | 102 |  | 
|---|
|  | 103 | /// go through linked cell and get all neighboring atoms up to Rcut | 
|---|
|  | 104 | Vector center; | 
|---|
|  | 105 | molecule *_mol = (*atoms.begin())->getMolecule(); | 
|---|
|  | 106 | for (AtomIdSet::const_iterator iter = atoms.begin(); | 
|---|
|  | 107 | iter != atoms.end(); ++iter) { | 
|---|
|  | 108 | center += (*iter)->getPosition(); | 
|---|
|  | 109 | ASSERT ( _mol == (*iter)->getMolecule(), | 
|---|
|  | 110 | "Interfragmenter::operator() - ids in same keyset belong to different molecule."); | 
|---|
|  | 111 | } | 
|---|
|  | 112 | center *= 1./(double)atoms_size; | 
|---|
|  | 113 | LinkedCell::LinkedCell_View view = World::getInstance().getLinkedCell(Rcut); | 
|---|
|  | 114 | LinkedCell::LinkedList neighbors = view.getAllNeighbors(Rcut, center); | 
|---|
|  | 115 | LOG(4, "DEBUG: Obtained " << neighbors.size() << " neighbors in distance of " | 
|---|
|  | 116 | << Rcut << " around " << center << "."); | 
|---|
|  | 117 |  | 
|---|
|  | 118 | /// remove all atoms that belong to same molecule as does one of the | 
|---|
|  | 119 | /// fragment's atoms | 
|---|
|  | 120 | typedef std::vector<const atom *> candidates_t; | 
|---|
|  | 121 | candidates_t candidates; | 
|---|
|  | 122 | candidates.reserve(neighbors.size()); | 
|---|
|  | 123 | for (LinkedCell::LinkedList::const_iterator iter = neighbors.begin(); | 
|---|
|  | 124 | iter != neighbors.end(); ++iter) { | 
|---|
|  | 125 | const atom * const _atom = static_cast<const atom * const >(*iter); | 
|---|
|  | 126 | ASSERT( _atom != NULL, | 
|---|
|  | 127 | "Interfragmenter::operator() - a neighbor is not actually an atom?"); | 
|---|
|  | 128 | if ((_atom->getMolecule() != _mol) | 
|---|
|  | 129 | && (_atom->getPosition().DistanceSquared(center) < Rcut*Rcut) | 
|---|
|  | 130 | && ((treatment == IncludeHydrogen) || (_atom->getType()->getAtomicNumber() != 1))) { | 
|---|
|  | 131 | candidates.push_back(_atom); | 
|---|
|  | 132 | } | 
|---|
|  | 133 | } | 
|---|
|  | 134 | LOG(3, "DEBUG: There remain " << candidates.size() << " candidates."); | 
|---|
|  | 135 |  | 
|---|
|  | 136 | // create a lookup specific to this fragment | 
|---|
|  | 137 | atomkeyset_t fragmentmap; | 
|---|
| [09806a] | 138 | for (candidates_t::const_iterator candidateiter = candidates.begin(); | 
|---|
|  | 139 | candidateiter != candidates.end(); ++candidateiter) { | 
|---|
|  | 140 | const atom * _atom = *candidateiter; | 
|---|
| [0331ee] | 141 | atomkeyset_t::const_iterator iter = atomkeyset.find(_atom); | 
|---|
|  | 142 | ASSERT( iter != atomkeyset.end(), | 
|---|
|  | 143 | "Interfragmenter::operator() - could not find atom " | 
|---|
|  | 144 | +toString(_atom->getId())+" in lookup."); | 
|---|
|  | 145 | fragmentmap.insert( std::make_pair( _atom, iter->second) ); | 
|---|
|  | 146 | } | 
|---|
|  | 147 | LOG(4, "DEBUG: Copied part of lookup map contains " << fragmentmap.size() << " keys."); | 
|---|
|  | 148 |  | 
|---|
|  | 149 | /// combine each remaining fragment with current fragment to a new fragment | 
|---|
|  | 150 | /// if keyset is less | 
|---|
|  | 151 | for (candidates_t::const_iterator candidateiter = candidates.begin(); | 
|---|
|  | 152 | candidateiter != candidates.end(); ++candidateiter) { | 
|---|
|  | 153 | const atom *_atom = *candidateiter; | 
|---|
|  | 154 | LOG(3, "DEBUG: Current candidate is " << *_atom << "."); | 
|---|
| [09806a] | 155 | atomkeyset_t::iterator finditer = fragmentmap.find(_atom); | 
|---|
|  | 156 | ASSERT( finditer != fragmentmap.end(), | 
|---|
| [0331ee] | 157 | "Interfragmenter::operator() - could not find atom " | 
|---|
|  | 158 | +toString(_atom->getId())+" in fragment specific lookup."); | 
|---|
| [09806a] | 159 | keysets_t &othersets = finditer->second; | 
|---|
| [0331ee] | 160 | keysets_t::iterator otheriter = othersets.begin(); | 
|---|
|  | 161 | while (otheriter != othersets.end()) { | 
|---|
|  | 162 | const KeySet &otherset = **otheriter; | 
|---|
|  | 163 | LOG(3, "DEBUG: Current keyset is " << otherset << "."); | 
|---|
|  | 164 | // only add them one way round and not the other | 
|---|
|  | 165 | if (otherset < keyset) { | 
|---|
|  | 166 | ++otheriter; | 
|---|
|  | 167 | continue; | 
|---|
|  | 168 | } | 
|---|
|  | 169 | KeySet newset(otherset); | 
|---|
|  | 170 | newset.insert(keyset.begin(), keyset.end()); | 
|---|
|  | 171 | LOG(3, "DEBUG: Inserting new combined set " << newset << "."); | 
|---|
|  | 172 | InterFragments.insert( std::make_pair(newset, std::make_pair(++counter, 1.))); | 
|---|
|  | 173 | // finally, remove the set such that no other combination exists | 
|---|
|  | 174 | otheriter = othersets.erase(otheriter); | 
|---|
|  | 175 | } | 
|---|
|  | 176 | } | 
|---|
|  | 177 | } | 
|---|
|  | 178 |  | 
|---|
|  | 179 | /// eventually, add all new fragments to the Graph | 
|---|
|  | 180 | counter = TotalGraph.size(); | 
|---|
|  | 181 | TotalGraph.InsertGraph(InterFragments, counter); | 
|---|
|  | 182 | } | 
|---|