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