| 1 | /*
 | 
|---|
| 2 |  * Project: MoleCuilder
 | 
|---|
| 3 |  * Description: creates and alters molecular systems
 | 
|---|
| 4 |  * Copyright (C)  2012 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 |  * ExportGraph_ToFiles.cpp
 | 
|---|
| 25 |  *
 | 
|---|
| 26 |  *  Created on: 08.03.2012
 | 
|---|
| 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 "ExportGraph_ToFiles.hpp"
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "CodePatterns/Info.hpp"
 | 
|---|
| 40 | #include "CodePatterns/Log.hpp"
 | 
|---|
| 41 | 
 | 
|---|
| 42 | #include "Bond/bond.hpp"
 | 
|---|
| 43 | #include "Element/element.hpp"
 | 
|---|
| 44 | #include "Fragmentation/Graph.hpp"
 | 
|---|
| 45 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| 46 | #include "Fragmentation/SortIndex.hpp"
 | 
|---|
| 47 | #include "Graph/ListOfLocalAtoms.hpp"
 | 
|---|
| 48 | #include "molecule.hpp"
 | 
|---|
| 49 | #include "MoleculeListClass.hpp"
 | 
|---|
| 50 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| 51 | #include "World.hpp"
 | 
|---|
| 52 | 
 | 
|---|
| 53 | /** Constructor for class ExportGraph_ToFiles.
 | 
|---|
| 54 |  *
 | 
|---|
| 55 |  * @param _graph instance of Graph containing keyset of each fragment
 | 
|---|
| 56 |  * @param _treatment whether to always add already present hydrogens or not
 | 
|---|
| 57 |  * @param _saturation whether to saturate dangling bonds with hydrogen or not
 | 
|---|
| 58 |  */
 | 
|---|
| 59 | ExportGraph_ToFiles::ExportGraph_ToFiles(
 | 
|---|
| 60 |     const Graph &_graph,
 | 
|---|
| 61 |     const enum HydrogenTreatment _treatment,
 | 
|---|
| 62 |     const enum HydrogenSaturation _saturation) :
 | 
|---|
| 63 |                 ExportGraph(_graph),
 | 
|---|
| 64 |                 BondFragments(World::getPointer()),
 | 
|---|
| 65 |                 treatment(_treatment),
 | 
|---|
| 66 |                 saturation(_saturation)
 | 
|---|
| 67 | {}
 | 
|---|
| 68 | 
 | 
|---|
| 69 | /** Destructor of class ExportGraph_ToFiles.
 | 
|---|
| 70 |  *
 | 
|---|
| 71 |  * We free all created molecules again and also removed their copied atoms.
 | 
|---|
| 72 |  */
 | 
|---|
| 73 | ExportGraph_ToFiles::~ExportGraph_ToFiles()
 | 
|---|
| 74 | {
 | 
|---|
| 75 |   // remove all create molecules again from the World including their atoms
 | 
|---|
| 76 |   for (MoleculeList::iterator iter = BondFragments.ListOfMolecules.begin();
 | 
|---|
| 77 |       !BondFragments.ListOfMolecules.empty();
 | 
|---|
| 78 |       iter = BondFragments.ListOfMolecules.begin()) {
 | 
|---|
| 79 |     // remove copied atoms and molecule again
 | 
|---|
| 80 |     molecule *mol = *iter;
 | 
|---|
| 81 |     mol->removeAtomsinMolecule();
 | 
|---|
| 82 |     World::getInstance().destroyMolecule(mol);
 | 
|---|
| 83 |     BondFragments.ListOfMolecules.erase(iter);
 | 
|---|
| 84 |   }
 | 
|---|
| 85 | }
 | 
|---|
| 86 | 
 | 
|---|
| 87 | /** Actual implementation of the export to files function.
 | 
|---|
| 88 |  */
 | 
|---|
| 89 | void ExportGraph_ToFiles::operator()()
 | 
|---|
| 90 | {
 | 
|---|
| 91 |   if (BondFragments.ListOfMolecules.size() == 0)
 | 
|---|
| 92 |     prepareMolecule();
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   // ===== 9. Save fragments' configuration and keyset files et al to disk ===
 | 
|---|
| 95 |   LOG(1, "Writing " << BondFragments.ListOfMolecules.size() << " possible bond fragmentation configs");
 | 
|---|
| 96 |   bool write_status = true;
 | 
|---|
| 97 |   for (std::vector<std::string>::const_iterator iter = typelist.begin();
 | 
|---|
| 98 |       iter != typelist.end();
 | 
|---|
| 99 |       ++iter) {
 | 
|---|
| 100 |     LOG(2, "INFO: Writing bond fragments for type " << (*iter) << ".");
 | 
|---|
| 101 |     write_status = write_status
 | 
|---|
| 102 |     && BondFragments.OutputConfigForListOfFragments(
 | 
|---|
| 103 |         prefix,
 | 
|---|
| 104 |         FormatParserStorage::getInstance().getTypeFromName(*iter));
 | 
|---|
| 105 |   }
 | 
|---|
| 106 |   if (write_status)
 | 
|---|
| 107 |     LOG(1, "All configs written.");
 | 
|---|
| 108 |   else
 | 
|---|
| 109 |     LOG(1, "Some config writing failed.");
 | 
|---|
| 110 | 
 | 
|---|
| 111 |   // store force index reference file
 | 
|---|
| 112 |   {
 | 
|---|
| 113 |     SortIndex_t SortIndex;
 | 
|---|
| 114 |     BondFragments.StoreForcesFile(prefix, SortIndex);
 | 
|---|
| 115 |   }
 | 
|---|
| 116 | 
 | 
|---|
| 117 |   // store keysets file
 | 
|---|
| 118 |   TotalGraph.StoreKeySetFile(prefix);
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   // store Hydrogen saturation correction file
 | 
|---|
| 121 |   BondFragments.AddHydrogenCorrection(prefix);
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // restore orbital and Stop values
 | 
|---|
| 124 |   //CalculateOrbitals(*configuration);
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | /** Internal helper to create from each keyset a molecule
 | 
|---|
| 128 |  *
 | 
|---|
| 129 |  */
 | 
|---|
| 130 | void ExportGraph_ToFiles::prepareMolecule()
 | 
|---|
| 131 | {
 | 
|---|
| 132 |   size_t count = 0;
 | 
|---|
| 133 |   for(Graph::const_iterator runner = TotalGraph.begin(); runner != TotalGraph.end(); runner++) {
 | 
|---|
| 134 |     KeySet test = (*runner).first;
 | 
|---|
| 135 |     LOG(2, "DEBUG: Fragment No." << (*runner).second.first << " with TEFactor "
 | 
|---|
| 136 |                 << (*runner).second.second << ".");
 | 
|---|
| 137 |     BondFragments.insert(StoreFragmentFromKeySet(test, World::getInstance().getConfig()));
 | 
|---|
| 138 |     ++count;
 | 
|---|
| 139 |   }
 | 
|---|
| 140 |   LOG(1, "INFO: " << count << "/" << BondFragments.ListOfMolecules.size()
 | 
|---|
| 141 |                 << " fragments generated from the keysets.");
 | 
|---|
| 142 | }
 | 
|---|
| 143 | 
 | 
|---|
| 144 | /** Stores a fragment from \a KeySet into \a molecule.
 | 
|---|
| 145 |  * First creates the minimal set of atoms from the KeySet, then creates the bond structure from the complete
 | 
|---|
| 146 |  * molecule and adds missing hydrogen where bonds were cut.
 | 
|---|
| 147 |  * \param &Leaflet pointer to KeySet structure
 | 
|---|
| 148 |  * \param IsAngstroem whether we have Ansgtroem or bohrradius
 | 
|---|
| 149 |  * \return pointer to constructed molecule
 | 
|---|
| 150 |  */
 | 
|---|
| 151 | molecule * ExportGraph_ToFiles::StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem)
 | 
|---|
| 152 | {
 | 
|---|
| 153 |   Info info(__func__);
 | 
|---|
| 154 |   ListOfLocalAtoms_t SonList;
 | 
|---|
| 155 |   molecule *Leaf = World::getInstance().createMolecule();
 | 
|---|
| 156 | 
 | 
|---|
| 157 |   StoreFragmentFromKeySet_Init(Leaf, Leaflet, SonList);
 | 
|---|
| 158 |   // create the bonds between all: Make it an induced subgraph and add hydrogen
 | 
|---|
| 159 | //  LOG(2, "Creating bonds from father graph (i.e. induced subgraph creation).");
 | 
|---|
| 160 |   CreateInducedSubgraphOfFragment(Leaf, SonList, IsAngstroem);
 | 
|---|
| 161 | 
 | 
|---|
| 162 |   //Leaflet->Leaf->ScanForPeriodicCorrection(out);
 | 
|---|
| 163 |   return Leaf;
 | 
|---|
| 164 | }
 | 
|---|
| 165 | 
 | 
|---|
| 166 | /** Initializes some value for putting fragment of \a *mol into \a *Leaf.
 | 
|---|
| 167 |  * \param *Leaf fragment molecule
 | 
|---|
| 168 |  * \param &Leaflet pointer to KeySet structure
 | 
|---|
| 169 |  * \param SonList calloc'd list which atom of \a *Leaf is a son of which atom in \a *mol
 | 
|---|
| 170 |  * \return number of atoms in fragment
 | 
|---|
| 171 |  */
 | 
|---|
| 172 | int ExportGraph_ToFiles::StoreFragmentFromKeySet_Init(molecule *Leaf, KeySet &Leaflet, ListOfLocalAtoms_t &SonList)
 | 
|---|
| 173 | {
 | 
|---|
| 174 |   atom *FatherOfRunner = NULL;
 | 
|---|
| 175 | 
 | 
|---|
| 176 |   // first create the minimal set of atoms from the KeySet
 | 
|---|
| 177 |   World &world = World::getInstance();
 | 
|---|
| 178 |   int size = 0;
 | 
|---|
| 179 |   for(KeySet::const_iterator runner = Leaflet.begin(); runner != Leaflet.end(); runner++) {
 | 
|---|
| 180 |     FatherOfRunner = world.getAtom(AtomById(*runner));  // find the id
 | 
|---|
| 181 |     SonList.insert( std::make_pair(FatherOfRunner->getNr(), Leaf->AddCopyAtom(FatherOfRunner) ) );
 | 
|---|
| 182 |     size++;
 | 
|---|
| 183 |   }
 | 
|---|
| 184 |   return size;
 | 
|---|
| 185 | }
 | 
|---|
| 186 | 
 | 
|---|
| 187 | /** Creates an induced subgraph out of a fragmental key set, adding bonds and hydrogens (if treated specially).
 | 
|---|
| 188 |  * \param *Leaf fragment molecule
 | 
|---|
| 189 |  * \param IsAngstroem whether we have Ansgtroem or bohrradius
 | 
|---|
| 190 |  * \param SonList list which atom of \a *Leaf is another atom's son
 | 
|---|
| 191 |  */
 | 
|---|
| 192 | void ExportGraph_ToFiles::CreateInducedSubgraphOfFragment(molecule *Leaf, ListOfLocalAtoms_t &SonList, bool IsAngstroem)
 | 
|---|
| 193 | {
 | 
|---|
| 194 |   bool LonelyFlag = false;
 | 
|---|
| 195 |   atom *OtherFather = NULL;
 | 
|---|
| 196 |   atom *FatherOfRunner = NULL;
 | 
|---|
| 197 | 
 | 
|---|
| 198 |   // we increment the iter just before skipping the hydrogen
 | 
|---|
| 199 |   // as we use AddBond, we cannot have a const_iterator here
 | 
|---|
| 200 |   for (molecule::iterator iter = Leaf->begin(); iter != Leaf->end();) {
 | 
|---|
| 201 |     LonelyFlag = true;
 | 
|---|
| 202 |     FatherOfRunner = (*iter)->father;
 | 
|---|
| 203 |     ASSERT(FatherOfRunner,"Atom without father found");
 | 
|---|
| 204 |     if (SonList.find(FatherOfRunner->getNr()) != SonList.end())  {  // check if this, our father, is present in list
 | 
|---|
| 205 |       // create all bonds
 | 
|---|
| 206 |       const BondList& ListOfBonds = FatherOfRunner->getListOfBonds();
 | 
|---|
| 207 |       for (BondList::const_iterator BondRunner = ListOfBonds.begin();
 | 
|---|
| 208 |           BondRunner != ListOfBonds.end();
 | 
|---|
| 209 |           ++BondRunner) {
 | 
|---|
| 210 |         OtherFather = (*BondRunner)->GetOtherAtom(FatherOfRunner);
 | 
|---|
| 211 |         if (SonList.find(OtherFather->getNr()) != SonList.end()) {
 | 
|---|
| 212 | //          LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
 | 
|---|
| 213 | //              << " is bound to " << *OtherFather << ", whose son is "
 | 
|---|
| 214 | //              << *SonList[OtherFather->getNr()] << ".");
 | 
|---|
| 215 |           if (OtherFather->getNr() > FatherOfRunner->getNr()) { // add bond (Nr check is for adding only one of both variants: ab, ba)
 | 
|---|
| 216 |             std::stringstream output;
 | 
|---|
| 217 | //            output << "ACCEPT: Adding Bond: "
 | 
|---|
| 218 |             output << Leaf->AddBond((*iter), SonList[OtherFather->getNr()], (*BondRunner)->BondDegree);
 | 
|---|
| 219 | //            LOG(3, output.str());
 | 
|---|
| 220 |             //NumBonds[(*iter)->getNr()]++;
 | 
|---|
| 221 |           } else {
 | 
|---|
| 222 | //            LOG(3, "REJECY: Not adding bond, labels in wrong order.");
 | 
|---|
| 223 |           }
 | 
|---|
| 224 |           LonelyFlag = false;
 | 
|---|
| 225 |         } else {
 | 
|---|
| 226 | //          LOG(2, "INFO: Father " << *FatherOfRunner << " of son " << *SonList[FatherOfRunner->getNr()]
 | 
|---|
| 227 | //              << " is bound to " << *OtherFather << ", who has no son in this fragment molecule.");
 | 
|---|
| 228 |           if (saturation == DoSaturate) {
 | 
|---|
| 229 | //          LOG(3, "ACCEPT: Adding Hydrogen to " << (*iter)->Name << " and a bond in between.");
 | 
|---|
| 230 |             if (!Leaf->AddHydrogenReplacementAtom((*BondRunner), (*iter), FatherOfRunner, OtherFather, IsAngstroem))
 | 
|---|
| 231 |               exit(1);
 | 
|---|
| 232 |           } else if ((treatment == ExcludeHydrogen) && (OtherFather->getElementNo() == (atomicNumber_t)1)) {
 | 
|---|
| 233 |             // just copy the atom if it's a hydrogen
 | 
|---|
| 234 |             atom * const OtherWalker = Leaf->AddCopyAtom(OtherFather);
 | 
|---|
| 235 |             Leaf->AddBond((*iter), OtherWalker, (*BondRunner)->BondDegree);
 | 
|---|
| 236 |           }
 | 
|---|
| 237 |           //NumBonds[(*iter)->getNr()] += Binder->BondDegree;
 | 
|---|
| 238 |         }
 | 
|---|
| 239 |       }
 | 
|---|
| 240 |     } else {
 | 
|---|
| 241 |       ELOG(1, "Son " << (*iter)->getName() << " has father " << FatherOfRunner->getName() << " but its entry in SonList is " << SonList[FatherOfRunner->getNr()] << "!");
 | 
|---|
| 242 |     }
 | 
|---|
| 243 |     if ((LonelyFlag) && (Leaf->getAtomCount() > 1)) {
 | 
|---|
| 244 |       LOG(0, **iter << "has got bonds only to hydrogens!");
 | 
|---|
| 245 |     }
 | 
|---|
| 246 |     ++iter;
 | 
|---|
| 247 |     if (saturation == DoSaturate) {
 | 
|---|
| 248 |       while ((iter != Leaf->end()) && ((*iter)->getType()->getAtomicNumber() == 1)){ // skip added hydrogen
 | 
|---|
| 249 |         iter++;
 | 
|---|
| 250 |       }
 | 
|---|
| 251 |     }
 | 
|---|
| 252 |   }
 | 
|---|
| 253 | }
 | 
|---|
| 254 | 
 | 
|---|