source: src/Fragmentation/Exporters/ExportGraph_ToFiles.cpp@ 97d6ab

Last change on this file since 97d6ab was 5aaa43, checked in by Frederik Heber <heber@…>, 12 years ago

FIX: Fixed new copyright line since start of 2013 in CodeChecks test.

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