| [de0af2] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
| [5aaa43] | 5 |  * Copyright (C)  2013 Frederik Heber. All rights reserved.
 | 
|---|
| [de0af2] | 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 |  * ExportGraph_ToFiles.cpp
 | 
|---|
 | 26 |  *
 | 
|---|
 | 27 |  *  Created on: 08.03.2012
 | 
|---|
 | 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 "ExportGraph_ToFiles.hpp"
 | 
|---|
 | 39 | 
 | 
|---|
| [ca8bea] | 40 | #include "CodePatterns/Info.hpp"
 | 
|---|
| [de0af2] | 41 | #include "CodePatterns/Log.hpp"
 | 
|---|
| [ca8bea] | 42 | 
 | 
|---|
 | 43 | #include "Bond/bond.hpp"
 | 
|---|
 | 44 | #include "Element/element.hpp"
 | 
|---|
| [de0af2] | 45 | #include "Fragmentation/Graph.hpp"
 | 
|---|
 | 46 | #include "Fragmentation/KeySet.hpp"
 | 
|---|
| [ca8bea] | 47 | #include "Graph/ListOfLocalAtoms.hpp"
 | 
|---|
 | 48 | #include "molecule.hpp"
 | 
|---|
 | 49 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| [de0af2] | 50 | #include "World.hpp"
 | 
|---|
 | 51 | 
 | 
|---|
 | 52 | /** Constructor for class ExportGraph_ToFiles.
 | 
|---|
 | 53 |  *
 | 
|---|
 | 54 |  * @param _graph instance of Graph containing keyset of each fragment
 | 
|---|
| [276ac6] | 55 |  * @param _treatment whether to always add already present hydrogens or not
 | 
|---|
 | 56 |  * @param _saturation whether to saturate dangling bonds with hydrogen or not
 | 
|---|
| [98a293b] | 57 |  * @param _globalsaturationpositions possibly empty map with global information
 | 
|---|
 | 58 |  *        where to place saturation hydrogens to fulfill consistency principle
 | 
|---|
| [de0af2] | 59 |  */
 | 
|---|
| [276ac6] | 60 | ExportGraph_ToFiles::ExportGraph_ToFiles(
 | 
|---|
 | 61 |     const Graph &_graph,
 | 
|---|
 | 62 |     const enum HydrogenTreatment _treatment,
 | 
|---|
| [98a293b] | 63 |     const enum HydrogenSaturation _saturation,
 | 
|---|
 | 64 |     const SaturatedFragment::GlobalSaturationPositions_t &_globalsaturationpositions) :
 | 
|---|
 | 65 |                 ExportGraph(_graph, _treatment, _saturation, _globalsaturationpositions)
 | 
|---|
| [de0af2] | 66 | {}
 | 
|---|
 | 67 | 
 | 
|---|
| [ca8bea] | 68 | /** Destructor of class ExportGraph_ToFiles.
 | 
|---|
| [de0af2] | 69 |  *
 | 
|---|
| [ca8bea] | 70 |  * We free all created molecules again and also removed their copied atoms.
 | 
|---|
| [de0af2] | 71 |  */
 | 
|---|
| [ca8bea] | 72 | ExportGraph_ToFiles::~ExportGraph_ToFiles()
 | 
|---|
| [8652a30] | 73 | {}
 | 
|---|
| [ca8bea] | 74 | 
 | 
|---|
| [7cdf58] | 75 | /** Returns a string with \a i prefixed with 0s to match order of total number of molecules in digits.
 | 
|---|
 | 76 |  * \param FragmentNumber total number of fragments to determine necessary number of digits
 | 
|---|
 | 77 |  * \param digits number to create with 0 prefixed
 | 
|---|
 | 78 |  * \return allocated(!) char array with number in digits, ten base.
 | 
|---|
 | 79 |  */
 | 
|---|
 | 80 | static char *FixedDigitNumber(const int FragmentNumber, const int digits)
 | 
|---|
 | 81 | {
 | 
|---|
 | 82 |   char *returnstring;
 | 
|---|
 | 83 |   int number = FragmentNumber;
 | 
|---|
 | 84 |   int order = 0;
 | 
|---|
 | 85 |   while (number != 0) { // determine number of digits needed
 | 
|---|
 | 86 |     number = (int)floor(((double)number / 10.));
 | 
|---|
 | 87 |     order++;
 | 
|---|
 | 88 |     //LOG(0, "Number is " << number << ", order is " << order << ".");
 | 
|---|
 | 89 |   }
 | 
|---|
 | 90 |   // allocate string
 | 
|---|
 | 91 |   returnstring = new char[order + 2];
 | 
|---|
 | 92 |   // terminate  and fill string array from end backward
 | 
|---|
 | 93 |   returnstring[order] = '\0';
 | 
|---|
 | 94 |   number = digits;
 | 
|---|
 | 95 |   for (int i=order;i--;) {
 | 
|---|
 | 96 |     returnstring[i] = '0' + (char)(number % 10);
 | 
|---|
 | 97 |     number = (int)floor(((double)number / 10.));
 | 
|---|
 | 98 |   }
 | 
|---|
 | 99 |   //LOG(0, returnstring);
 | 
|---|
 | 100 |   return returnstring;
 | 
|---|
 | 101 | };
 | 
|---|
 | 102 | 
 | 
|---|
| [ca8bea] | 103 | /** Actual implementation of the export to files function.
 | 
|---|
 | 104 |  */
 | 
|---|
 | 105 | void ExportGraph_ToFiles::operator()()
 | 
|---|
 | 106 | {
 | 
|---|
| [ac9ca4] | 107 |   LOG(1, "INFO: Writing " << TotalGraph.size() << " possible bond fragmentation configs.");
 | 
|---|
| [7cdf58] | 108 |   size_t FragmentCounter = 0;
 | 
|---|
 | 109 |   char *FragmentNumber = NULL;
 | 
|---|
 | 110 |   string filename(prefix);
 | 
|---|
 | 111 |   filename += FORCESFILE;
 | 
|---|
 | 112 |   std::ofstream ForcesFile(filename.c_str());
 | 
|---|
 | 113 |   SortIndex_t SortIndex;
 | 
|---|
| [ca8bea] | 114 | 
 | 
|---|
 | 115 |   // ===== 9. Save fragments' configuration and keyset files et al to disk ===
 | 
|---|
 | 116 |   bool write_status = true;
 | 
|---|
| [7cdf58] | 117 |   ExportGraph::SaturatedFragment_ptr CurrentFragment = getNextFragment();
 | 
|---|
 | 118 |   for (; (CurrentFragment != NULL) && (CurrentFragment->getKeySet() != ExportGraph::EmptySet);
 | 
|---|
 | 119 |       CurrentFragment = getNextFragment()) {
 | 
|---|
 | 120 |     const KeySet &set = CurrentFragment->getKeySet();
 | 
|---|
 | 121 |     LOG(2, "INFO: Writing bond fragments for set " << set << ".");
 | 
|---|
 | 122 |     // store config in stream
 | 
|---|
 | 123 |     {
 | 
|---|
 | 124 |       // open file
 | 
|---|
 | 125 |       FragmentNumber = FixedDigitNumber(TotalGraph.size(), FragmentCounter++);
 | 
|---|
 | 126 |       storeFragmentForAllTypes(
 | 
|---|
 | 127 |           CurrentFragment, FragmentNumber, FragmentCounter-1);
 | 
|---|
 | 128 |       delete[](FragmentNumber);
 | 
|---|
 | 129 |     }
 | 
|---|
 | 130 |     // store force index reference file
 | 
|---|
| [ca8bea] | 131 |     write_status = write_status
 | 
|---|
| [7cdf58] | 132 |         && appendToForcesFile(CurrentFragment, ForcesFile, SortIndex);
 | 
|---|
 | 133 |     // explicitly release fragment
 | 
|---|
 | 134 |     CurrentFragment.reset();
 | 
|---|
| [ca8bea] | 135 |   }
 | 
|---|
| [7cdf58] | 136 |   if (CurrentFragment == NULL) {
 | 
|---|
 | 137 |     ELOG(1, "Some error while obtaining the next fragment occured.");
 | 
|---|
 | 138 |     return;
 | 
|---|
 | 139 |   }
 | 
|---|
 | 140 |   ForcesFile.close();
 | 
|---|
 | 141 | 
 | 
|---|
| [ca8bea] | 142 |   if (write_status)
 | 
|---|
 | 143 |     LOG(1, "All configs written.");
 | 
|---|
 | 144 |   else
 | 
|---|
 | 145 |     LOG(1, "Some config writing failed.");
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 |   // store keysets file
 | 
|---|
 | 148 |   TotalGraph.StoreKeySetFile(prefix);
 | 
|---|
 | 149 | 
 | 
|---|
 | 150 |   // restore orbital and Stop values
 | 
|---|
 | 151 |   //CalculateOrbitals(*configuration);
 | 
|---|
| [de0af2] | 152 | }
 | 
|---|
 | 153 | 
 | 
|---|
| [7cdf58] | 154 | bool ExportGraph_ToFiles::storeFragmentForAllTypes(
 | 
|---|
 | 155 |     SaturatedFragment_ptr &CurrentFragment,
 | 
|---|
 | 156 |     char *FragmentNumber,
 | 
|---|
 | 157 |     size_t FragmentCounter) const
 | 
|---|
 | 158 | {
 | 
|---|
 | 159 |   bool write_status = true;
 | 
|---|
 | 160 | 
 | 
|---|
 | 161 |   // go through all desired types
 | 
|---|
 | 162 |   for (std::vector<std::string>::const_iterator typeiter = typelist.begin();
 | 
|---|
 | 163 |       typeiter != typelist.end(); ++typeiter) {
 | 
|---|
 | 164 |     const std::string &typeName = *typeiter;
 | 
|---|
 | 165 |     const ParserTypes type =
 | 
|---|
 | 166 |         FormatParserStorage::getInstance().getTypeFromName(typeName);
 | 
|---|
 | 167 |     // create filenname and open
 | 
|---|
 | 168 |     const std::string FragmentName =
 | 
|---|
 | 169 |         prefix + FragmentNumber + "." + FormatParserStorage::getInstance().getSuffixFromType(type);
 | 
|---|
 | 170 |     std::ofstream outputFragment(FragmentName.c_str(), ios::out);
 | 
|---|
 | 171 | 
 | 
|---|
 | 172 |     // write to this stream
 | 
|---|
 | 173 |     {
 | 
|---|
 | 174 |       std::stringstream output;
 | 
|---|
 | 175 |       output << "INFO: Saving bond fragment No. " << FragmentNumber << "/"
 | 
|---|
 | 176 |           << FragmentCounter << " as " << typeName << " ... ";
 | 
|---|
 | 177 |       const bool intermediate_result = CurrentFragment->OutputConfig(outputFragment,type);
 | 
|---|
 | 178 |       write_status &= intermediate_result;
 | 
|---|
 | 179 |       if (intermediate_result)
 | 
|---|
 | 180 |         output << " done.";
 | 
|---|
 | 181 |       else
 | 
|---|
 | 182 |         output << " failed.";
 | 
|---|
 | 183 |       LOG(2, output.str());
 | 
|---|
 | 184 |     }
 | 
|---|
 | 185 | 
 | 
|---|
 | 186 |     // close file
 | 
|---|
 | 187 |     outputFragment.close();
 | 
|---|
 | 188 |     outputFragment.clear();
 | 
|---|
 | 189 |   }
 | 
|---|
 | 190 | 
 | 
|---|
 | 191 |   return write_status;
 | 
|---|
 | 192 | }
 | 
|---|
| [ca8bea] | 193 | 
 | 
|---|
| [7cdf58] | 194 | bool ExportGraph_ToFiles::appendToForcesFile(
 | 
|---|
 | 195 |     SaturatedFragment_ptr &CurrentFragment,
 | 
|---|
 | 196 |     std::ostream &ForcesFile,
 | 
|---|
 | 197 |     const SortIndex_t &SortIndex) const
 | 
|---|
 | 198 | {
 | 
|---|
 | 199 |   bool status = true;
 | 
|---|
 | 200 | //  periodentafel *periode=World::getInstance().getPeriode();
 | 
|---|
 | 201 | 
 | 
|---|
 | 202 |   // open file for the force factors
 | 
|---|
 | 203 |   if (ForcesFile.good()) {
 | 
|---|
 | 204 |     //output << prefix << "Forces" << endl;
 | 
|---|
 | 205 |     const KeySet &FullMolecule = CurrentFragment->getFullMolecule();
 | 
|---|
 | 206 |     const KeySet &SaturationHydrogens = CurrentFragment->getSaturationHydrogens();
 | 
|---|
 | 207 |     for (KeySet::const_iterator keyiter = FullMolecule.begin();
 | 
|---|
 | 208 |         keyiter != FullMolecule.end();
 | 
|---|
 | 209 |         ++keyiter) {
 | 
|---|
 | 210 |       if (SaturationHydrogens.find(*keyiter) == SaturationHydrogens.end()) {
 | 
|---|
 | 211 |         ForcesFile << SortIndex.find(*keyiter) << "\t";
 | 
|---|
 | 212 |       } else {
 | 
|---|
 | 213 |         // otherwise a -1 to indicate an added saturation hydrogen
 | 
|---|
 | 214 |         ForcesFile << "-1\t";
 | 
|---|
 | 215 |       }
 | 
|---|
 | 216 |     }
 | 
|---|
 | 217 | //    for (MoleculeList::iterator ListRunner = ListOfMolecules.begin(); ListRunner != ListOfMolecules.end(); ListRunner++) {
 | 
|---|
 | 218 | //      periodentafel::const_iterator elemIter;
 | 
|---|
 | 219 | //      for(elemIter=periode->begin();elemIter!=periode->end();++elemIter){
 | 
|---|
 | 220 | //        if ((*ListRunner)->hasElement((*elemIter).first)) { // if this element got atoms
 | 
|---|
 | 221 | //          for(molecule::iterator atomIter = (*ListRunner)->begin(); atomIter !=(*ListRunner)->end();++atomIter){
 | 
|---|
 | 222 | //            if ((*atomIter)->getType()->getAtomicNumber() == (*elemIter).first) {
 | 
|---|
 | 223 | //              if (((*atomIter)->GetTrueFather() != NULL) && ((*atomIter)->GetTrueFather() != (*atomIter))) {// if there is a rea
 | 
|---|
 | 224 | //                const atomId_t fatherid = (*atomIter)->GetTrueFather()->getId();
 | 
|---|
 | 225 | //                ForcesFile << SortIndex.find(fatherid) << "\t";
 | 
|---|
 | 226 | //              } else
 | 
|---|
 | 227 | //                // otherwise a -1 to indicate an added saturation hydrogen
 | 
|---|
 | 228 | //                ForcesFile << "-1\t";
 | 
|---|
 | 229 | //            }
 | 
|---|
 | 230 | //          }
 | 
|---|
 | 231 | //        }
 | 
|---|
 | 232 | //      }
 | 
|---|
 | 233 | //      ForcesFile << endl;
 | 
|---|
 | 234 | //    }
 | 
|---|
 | 235 |     ForcesFile << std::endl;
 | 
|---|
 | 236 |   } else {
 | 
|---|
 | 237 |     status = false;
 | 
|---|
 | 238 |     ELOG(1, "Failure on appending to ForcesFile.");
 | 
|---|
 | 239 |   }
 | 
|---|
 | 240 | 
 | 
|---|
 | 241 |   return status;
 | 
|---|
 | 242 | }
 | 
|---|