source: src/Fragmentation/Graph.cpp@ 75363b

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 75363b was 75363b, checked in by Frederik Heber <heber@…>, 14 years ago

Extracted functions from fragmentation_helpers and placed them in KeySet or Graph class.

  • Property mode set to 100644
File size: 6.8 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * Graph.cpp
10 *
11 * Created on: Oct 20, 2011
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "CodePatterns/MemDebug.hpp"
21
22#include <fstream>
23#include <iostream>
24#include <sstream>
25#include <string>
26
27#include "Graph.hpp"
28
29#include "CodePatterns/Log.hpp"
30
31#include "Helpers/defs.hpp"
32#include "Helpers/helpers.hpp"
33
34/** Constructor for class Graph.
35 *
36 */
37Graph::Graph()
38{}
39
40/** Destructor for class Graph.
41 *
42 */
43Graph::~Graph()
44{}
45
46/** Inserts each KeySet in \a graph into \a this.
47 * \param graph1 graph whose KeySet are inserted into \this graph
48 * \param *counter keyset counter that gets increased
49 */
50void Graph::InsertGraph(Graph &graph, int *counter)
51{
52 GraphTestPair testGraphInsert;
53
54 for(Graph::iterator runner = graph.begin(); runner != graph.end(); runner++) {
55 testGraphInsert = insert(GraphPair ((*runner).first,pair<int,double>((*counter)++,((*runner).second).second))); // store fragment number and current factor
56 if (testGraphInsert.second) {
57 LOG(2, "INFO: KeySet " << (*counter)-1 << " successfully inserted.");
58 } else {
59 LOG(2, "INFO: KeySet " << (*counter)-1 << " failed to insert, present fragment is " << ((*(testGraphInsert.first)).second).first);
60 ((*(testGraphInsert.first)).second).second += (*runner).second.second;
61 LOG(2, "INFO: New factor is " << (*(testGraphInsert.first)).second.second << ".");
62 }
63 }
64};
65
66/** Parses the KeySet file and fills \a this from the known molecule structure.
67 * Does two-pass scanning:
68 * -# Scans the keyset file and initialises a temporary graph
69 * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
70 * Finally, the temporary graph is inserted into the given \a FragmentList for return.
71 * \param &path path to file
72 * \return true - parsing successfully, false - failure on parsing (FragmentList will be NULL)
73 */
74bool Graph::ParseKeySetFile(std::string &path)
75{
76 bool status = true;
77 std::ifstream InputFile;
78 std::stringstream line;
79 GraphTestPair testGraphInsert;
80 int NumberOfFragments = 0;
81 std::string filename;
82
83 // 1st pass: open file and read
84 LOG(1, "INFO: Parsing the KeySet file ... ");
85 filename = path + KEYSETFILE;
86 InputFile.open(filename.c_str());
87 if (InputFile.good()) {
88 // each line represents a new fragment
89 char buffer[MAXSTRINGSIZE];
90 // 1. parse keysets and insert into temp. graph
91 while (!InputFile.eof()) {
92 InputFile.getline(buffer, MAXSTRINGSIZE);
93 KeySet CurrentSet;
94 if ((strlen(buffer) > 0) && (CurrentSet.ScanBufferIntoKeySet(buffer))) { // if at least one valid atom was added, write config
95 testGraphInsert = insert(GraphPair (CurrentSet,pair<int,double>(NumberOfFragments++,1))); // store fragment number and current factor
96 if (!testGraphInsert.second) {
97 ELOG(0, "KeySet file must be corrupt as there are two equal key sets therein!");
98 performCriticalExit();
99 }
100 }
101 }
102 // 2. Free and done
103 InputFile.close();
104 InputFile.clear();
105 LOG(1, "INFO: ... done.");
106 } else {
107 ELOG(1, "File " << filename << " not found.");
108 status = false;
109 }
110
111 return status;
112};
113
114/** Stores key sets to file.
115 * \param &path path to file
116 * \return true - file written successfully, false - writing failed
117 */
118bool Graph::StoreKeySetFile(std::string &path)
119{
120 bool status = true;
121 std::string line = path + KEYSETFILE;
122 std::ofstream output(line.c_str());
123
124 // open KeySet file
125 LOG(1, "INFO: Saving key sets of the total graph ... ");
126 if(output.good()) {
127 for(Graph::iterator runner = begin(); runner != end(); runner++) {
128 for (KeySet::iterator sprinter = (*runner).first.begin();sprinter != (*runner).first.end(); sprinter++) {
129 if (sprinter != (*runner).first.begin())
130 output << "\t";
131 output << *sprinter;
132 }
133 output << std::endl;
134 }
135 LOG(1, "INFO: done.");
136 } else {
137 ELOG(0, "Unable to open " << line << " for writing keysets!");
138 performCriticalExit();
139 status = false;
140 }
141 output.close();
142 output.clear();
143
144 return status;
145};
146
147/** Parses the TE factors file and fills \a *FragmentList from the known molecule structure.
148 * -# Scans TEFactors file and sets the TEFactor of each key set in the temporary graph accordingly
149 * \param *path path to file
150 * \return true - parsing successfully, false - failure on parsing
151 */
152bool Graph::ParseTEFactorsFile(char *path)
153{
154 bool status = true;
155 std::ifstream InputFile;
156 std::stringstream line;
157 GraphTestPair testGraphInsert;
158 int NumberOfFragments = 0;
159 double TEFactor;
160 char filename[MAXSTRINGSIZE];
161
162 // 2nd pass: open TEFactors file and read
163 LOG(1, "INFO: Parsing the TEFactors file ... ");
164 sprintf(filename, "%s/%s%s", path, FRAGMENTPREFIX, TEFACTORSFILE);
165 InputFile.open(filename);
166 if (InputFile != NULL) {
167 // 3. add found TEFactors to each keyset
168 NumberOfFragments = 0;
169 for(Graph::iterator runner = begin();runner != end(); runner++) {
170 if (!InputFile.eof()) {
171 InputFile >> TEFactor;
172 (*runner).second.second = TEFactor;
173 LOG(2, "INFO: Setting " << ++NumberOfFragments << " fragment's TEFactor to " << (*runner).second.second << ".");
174 } else {
175 status = false;
176 break;
177 }
178 }
179 // 4. Free and done
180 InputFile.close();
181 LOG(1, "INFO: done.");
182 } else {
183 LOG(1, "INFO: File " << filename << " not found.");
184 status = false;
185 }
186
187 return status;
188};
189
190/** Stores TEFactors to file.
191 * \param *out output stream for debugging
192 * \param KeySetList Graph with factors
193 * \param *path path to file
194 * \return true - file written successfully, false - writing failed
195 */
196bool Graph::StoreTEFactorsFile(char *path)
197{
198 ofstream output;
199 bool status = true;
200 string line;
201
202 // open TEFactors file
203 line = path;
204 line.append("/");
205 line += FRAGMENTPREFIX;
206 line += TEFACTORSFILE;
207 output.open(line.c_str(), ios::out);
208 LOG(1, "INFO: Saving TEFactors of the total graph ... ");
209 if(output != NULL) {
210 for(Graph::iterator runner = begin(); runner != end(); runner++)
211 output << (*runner).second.second << endl;
212 LOG(1, "INFO: done." << endl);
213 } else {
214 ELOG(2, "INFO: failed to open " << line << "." << endl);
215 status = false;
216 }
217 output.close();
218
219 return status;
220};
221
222/** For a given graph, sorts KeySets into a (index, keyset) map.
223 * \return ref to allocated map from index to keyset
224 */
225std::map<int,KeySet> * Graph::GraphToIndexedKeySet() const
226{
227 map<int,KeySet> *IndexKeySetList = new map<int,KeySet>;
228 for(const_iterator runner = begin(); runner != end(); runner++) {
229 IndexKeySetList->insert( pair<int,KeySet>(runner->second.first,runner->first) );
230 }
231 return IndexKeySetList;
232};
Note: See TracBrowser for help on using the repository browser.