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