source: src/parser.cpp@ 1fa107

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 1fa107 was e359a8, checked in by Frederik Heber <heber@…>, 15 years ago

Fixing ticket #18.

  • each eLog() << Verbose(0) is now followed by performCriticalExit().
  • Property mode set to 100755
File size: 47.0 KB
Line 
1/** \file parsing.cpp
2 *
3 * Declarations of various class functions for the parsing of value files.
4 *
5 */
6
7// ======================================= INCLUDES ==========================================
8
9#include "helpers.hpp"
10#include "memoryallocator.hpp"
11#include "parser.hpp"
12
13// include config.h
14#ifdef HAVE_CONFIG_H
15#include <config.h>
16#endif
17
18// ======================================= FUNCTIONS ==========================================
19
20/** Test if given filename can be opened.
21 * \param filename name of file
22 * \param test true - no error message, false - print error
23 * \return given file exists
24 */
25bool FilePresent(const char *filename, bool test)
26{
27 ifstream input;
28
29 input.open(filename, ios::in);
30 if (input == NULL) {
31 if (!test)
32 Log() << Verbose(0) << endl << "Unable to open " << filename << ", is the directory correct?" << endl;
33 return false;
34 }
35 input.close();
36 return true;
37};
38
39/** Test the given parameters.
40 * \param argc argument count
41 * \param **argv arguments array
42 * \return given inputdir is valid
43 */
44bool TestParams(int argc, char **argv)
45{
46 ifstream input;
47 stringstream line;
48
49 line << argv[1] << FRAGMENTPREFIX << KEYSETFILE;
50 return FilePresent(line.str().c_str(), false);
51};
52
53// ======================================= CLASS MatrixContainer =============================
54
55/** Constructor of MatrixContainer class.
56 */
57MatrixContainer::MatrixContainer() {
58 Indices = NULL;
59 Header = Malloc<char*>(1, "MatrixContainer::MatrixContainer: **Header");
60 Matrix = Malloc<double**>(1, "MatrixContainer::MatrixContainer: ***Matrix"); // one more each for the total molecule
61 RowCounter = Malloc<int>(1, "MatrixContainer::MatrixContainer: *RowCounter");
62 ColumnCounter = Malloc<int>(1, "MatrixContainer::MatrixContainer: *ColumnCounter");
63 Header[0] = NULL;
64 Matrix[0] = NULL;
65 RowCounter[0] = -1;
66 MatrixCounter = 0;
67 ColumnCounter[0] = -1;
68};
69
70/** Destructor of MatrixContainer class.
71 */
72MatrixContainer::~MatrixContainer() {
73 if (Matrix != NULL) {
74 for(int i=MatrixCounter;i--;) {
75 if ((ColumnCounter != NULL) && (RowCounter != NULL)) {
76 for(int j=RowCounter[i]+1;j--;)
77 Free(&Matrix[i][j]);
78 Free(&Matrix[i]);
79 }
80 }
81 if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
82 for(int j=RowCounter[MatrixCounter]+1;j--;)
83 Free(&Matrix[MatrixCounter][j]);
84 if (MatrixCounter != 0)
85 Free(&Matrix[MatrixCounter]);
86 Free(&Matrix);
87 }
88 if (Indices != NULL)
89 for(int i=MatrixCounter+1;i--;) {
90 Free(&Indices[i]);
91 }
92 Free(&Indices);
93
94 if (Header != NULL)
95 for(int i=MatrixCounter+1;i--;)
96 Free(&Header[i]);
97 Free(&Header);
98 Free(&RowCounter);
99 Free(&ColumnCounter);
100};
101
102/** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL.
103 * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments.
104 * \param *Matrix pointer to other MatrixContainer
105 * \return true - copy/initialisation sucessful, false - dimension false for copying
106 */
107bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix)
108{
109 Log() << Verbose(0) << "Initialising indices";
110 if (Matrix == NULL) {
111 Log() << Verbose(0) << " with trivial mapping." << endl;
112 Indices = Malloc<int*>(MatrixCounter + 1, "MatrixContainer::InitialiseIndices: **Indices");
113 for(int i=MatrixCounter+1;i--;) {
114 Indices[i] = Malloc<int>(RowCounter[i], "MatrixContainer::InitialiseIndices: *Indices[]");
115 for(int j=RowCounter[i];j--;)
116 Indices[i][j] = j;
117 }
118 } else {
119 Log() << Verbose(0) << " from other MatrixContainer." << endl;
120 if (MatrixCounter != Matrix->MatrixCounter)
121 return false;
122 Indices = Malloc<int*>(MatrixCounter + 1, "MatrixContainer::InitialiseIndices: **Indices");
123 for(int i=MatrixCounter+1;i--;) {
124 if (RowCounter[i] != Matrix->RowCounter[i])
125 return false;
126 Indices[i] = Malloc<int>(Matrix->RowCounter[i], "MatrixContainer::InitialiseIndices: *Indices[]");
127 for(int j=Matrix->RowCounter[i];j--;) {
128 Indices[i][j] = Matrix->Indices[i][j];
129 //Log() << Verbose(0) << Indices[i][j] << "\t";
130 }
131 //Log() << Verbose(0) << endl;
132 }
133 }
134 return true;
135};
136
137/** Parsing a number of matrices.
138 * -# open the matrix file
139 * -# skip some lines (\a skiplines)
140 * -# scan header lines for number of columns
141 * -# scan lines for number of rows
142 * -# allocate matrix
143 * -# loop over found column and row counts and parse in each entry
144 * \param *name directory with files
145 * \param skiplines number of inital lines to skip
146 * \param skiplines number of inital columns to skip
147 * \param MatrixNr index number in Matrix array to parse into
148 * \return parsing successful
149 */
150bool MatrixContainer::ParseMatrix(const char *name, int skiplines, int skipcolumns, int MatrixNr)
151{
152 ifstream input;
153 stringstream line;
154 string token;
155 char filename[1023];
156
157 input.open(name, ios::in);
158 //Log() << Verbose(0) << "Opening " << name << " ... " << input << endl;
159 if (input == NULL) {
160 eLog() << Verbose(0) << endl << "Unable to open " << name << ", is the directory correct?" << endl;
161 performCriticalExit();
162 return false;
163 }
164
165 // parse header
166 Header[MatrixNr] = Malloc<char>(1024, "MatrixContainer::ParseMatrix: *Header[]");
167 for (int m=skiplines+1;m--;)
168 input.getline(Header[MatrixNr], 1023);
169
170 // scan header for number of columns
171 line.str(Header[MatrixNr]);
172 for(int k=skipcolumns;k--;)
173 line >> Header[MatrixNr];
174 //Log() << Verbose(0) << line.str() << endl;
175 ColumnCounter[MatrixNr]=0;
176 while ( getline(line,token, '\t') ) {
177 if (token.length() > 0)
178 ColumnCounter[MatrixNr]++;
179 }
180 //Log() << Verbose(0) << line.str() << endl;
181 //Log() << Verbose(0) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "." << endl;
182 if (ColumnCounter[MatrixNr] == 0) {
183 eLog() << Verbose(0) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl;
184 performCriticalExit();
185 }
186
187 // scan rest for number of rows/lines
188 RowCounter[MatrixNr]=-1; // counts one line too much
189 while (!input.eof()) {
190 input.getline(filename, 1023);
191 //Log() << Verbose(0) << "Comparing: " << strncmp(filename,"MeanForce",9) << endl;
192 RowCounter[MatrixNr]++; // then line was not last MeanForce
193 if (strncmp(filename,"MeanForce", 9) == 0) {
194 break;
195 }
196 }
197 //Log() << Verbose(0) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << "." << endl;
198 if (RowCounter[MatrixNr] == 0) {
199 eLog() << Verbose(0) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl;
200 performCriticalExit();
201 }
202
203 // allocate matrix if it's not zero dimension in one direction
204 if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) {
205 Matrix[MatrixNr] = Malloc<double*>(RowCounter[MatrixNr] + 1, "MatrixContainer::ParseMatrix: **Matrix[]");
206
207 // parse in each entry for this matrix
208 input.clear();
209 input.seekg(ios::beg);
210 for (int m=skiplines+1;m--;)
211 input.getline(Header[MatrixNr], 1023); // skip header
212 line.str(Header[MatrixNr]);
213 for(int k=skipcolumns;k--;) // skip columns in header too
214 line >> filename;
215 strncpy(Header[MatrixNr], line.str().c_str(), 1023);
216 for(int j=0;j<RowCounter[MatrixNr];j++) {
217 Matrix[MatrixNr][j] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[][]");
218 input.getline(filename, 1023);
219 stringstream lines(filename);
220 //Log() << Verbose(0) << "Matrix at level " << j << ":";// << filename << endl;
221 for(int k=skipcolumns;k--;)
222 lines >> filename;
223 for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
224 lines >> Matrix[MatrixNr][j][k];
225 //Log() << Verbose(0) << " " << setprecision(2) << Matrix[MatrixNr][j][k];;
226 }
227 //Log() << Verbose(0) << endl;
228 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[RowCounter[MatrixNr]][]");
229 for(int j=ColumnCounter[MatrixNr];j--;)
230 Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.;
231 }
232 } else {
233 eLog() << Verbose(1) << "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl;
234 }
235 input.close();
236 return true;
237};
238
239/** Parsing a number of matrices.
240 * -# First, count the number of matrices by counting lines in KEYSETFILE
241 * -# Then,
242 * -# construct the fragment number
243 * -# open the matrix file
244 * -# skip some lines (\a skiplines)
245 * -# scan header lines for number of columns
246 * -# scan lines for number of rows
247 * -# allocate matrix
248 * -# loop over found column and row counts and parse in each entry
249 * -# Finally, allocate one additional matrix (\a MatrixCounter) containing combined or temporary values
250 * \param *name directory with files
251 * \param *prefix prefix of each matrix file
252 * \param *suffix suffix of each matrix file
253 * \param skiplines number of inital lines to skip
254 * \param skiplines number of inital columns to skip
255 * \return parsing successful
256 */
257bool MatrixContainer::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
258{
259 char filename[1023];
260 ifstream input;
261 char *FragmentNumber = NULL;
262 stringstream file;
263 string token;
264
265 // count the number of matrices
266 MatrixCounter = -1; // we count one too much
267 file << name << FRAGMENTPREFIX << KEYSETFILE;
268 input.open(file.str().c_str(), ios::in);
269 if (input == NULL) {
270 Log() << Verbose(0) << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
271 return false;
272 }
273 while (!input.eof()) {
274 input.getline(filename, 1023);
275 stringstream zeile(filename);
276 MatrixCounter++;
277 }
278 input.close();
279 Log() << Verbose(0) << "Determined " << MatrixCounter << " fragments." << endl;
280
281 Log() << Verbose(0) << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl;
282 Header = ReAlloc<char*>(Header, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: **Header"); // one more each for the total molecule
283 Matrix = ReAlloc<double**>(Matrix, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule
284 RowCounter = ReAlloc<int>(RowCounter, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: *RowCounter");
285 ColumnCounter = ReAlloc<int>(ColumnCounter, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: *ColumnCounter");
286 for(int i=MatrixCounter+1;i--;) {
287 Matrix[i] = NULL;
288 Header[i] = NULL;
289 RowCounter[i] = -1;
290 ColumnCounter[i] = -1;
291 }
292 for(int i=0; i < MatrixCounter;i++) {
293 // open matrix file
294 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
295 file.str(" ");
296 file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix;
297 if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i))
298 return false;
299 Free(&FragmentNumber);
300 }
301 return true;
302};
303
304/** Allocates and resets the memory for a number \a MCounter of matrices.
305 * \param **GivenHeader Header line for each matrix
306 * \param MCounter number of matrices
307 * \param *RCounter number of rows for each matrix
308 * \param *CCounter number of columns for each matrix
309 * \return Allocation successful
310 */
311bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter)
312{
313 MatrixCounter = MCounter;
314 Header = Malloc<char*>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *Header");
315 Matrix = Malloc<double**>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: ***Matrix"); // one more each for the total molecule
316 RowCounter = Malloc<int>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *RowCounter");
317 ColumnCounter = Malloc<int>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *ColumnCounter");
318 for(int i=MatrixCounter+1;i--;) {
319 Header[i] = Malloc<char>(1024, "MatrixContainer::AllocateMatrix: *Header[i]");
320 strncpy(Header[i], GivenHeader[i], 1023);
321 RowCounter[i] = RCounter[i];
322 ColumnCounter[i] = CCounter[i];
323 Matrix[i] = Malloc<double*>(RowCounter[i] + 1, "MatrixContainer::AllocateMatrix: **Matrix[]");
324 for(int j=RowCounter[i]+1;j--;) {
325 Matrix[i][j] = Malloc<double>(ColumnCounter[i], "MatrixContainer::AllocateMatrix: *Matrix[][]");
326 for(int k=ColumnCounter[i];k--;)
327 Matrix[i][j][k] = 0.;
328 }
329 }
330 return true;
331};
332
333/** Resets all values in MatrixContainer::Matrix.
334 * \return true if successful
335 */
336bool MatrixContainer::ResetMatrix()
337{
338 for(int i=MatrixCounter+1;i--;)
339 for(int j=RowCounter[i]+1;j--;)
340 for(int k=ColumnCounter[i];k--;)
341 Matrix[i][j][k] = 0.;
342 return true;
343};
344
345/** Scans all elements of MatrixContainer::Matrix for greatest absolute value.
346 * \return greatest value of MatrixContainer::Matrix
347 */
348double MatrixContainer::FindMaxValue()
349{
350 double max = Matrix[0][0][0];
351 for(int i=MatrixCounter+1;i--;)
352 for(int j=RowCounter[i]+1;j--;)
353 for(int k=ColumnCounter[i];k--;)
354 if (fabs(Matrix[i][j][k]) > max)
355 max = fabs(Matrix[i][j][k]);
356 if (fabs(max) < MYEPSILON)
357 max += MYEPSILON;
358 return max;
359};
360
361/** Scans all elements of MatrixContainer::Matrix for smallest absolute value.
362 * \return smallest value of MatrixContainer::Matrix
363 */
364double MatrixContainer::FindMinValue()
365{
366 double min = Matrix[0][0][0];
367 for(int i=MatrixCounter+1;i--;)
368 for(int j=RowCounter[i]+1;j--;)
369 for(int k=ColumnCounter[i];k--;)
370 if (fabs(Matrix[i][j][k]) < min)
371 min = fabs(Matrix[i][j][k]);
372 if (fabs(min) < MYEPSILON)
373 min += MYEPSILON;
374 return min;
375};
376
377/** Sets all values in the last of MatrixContainer::Matrix to \a value.
378 * \param value reset value
379 * \param skipcolumns skip initial columns
380 * \return true if successful
381 */
382bool MatrixContainer::SetLastMatrix(double value, int skipcolumns)
383{
384 for(int j=RowCounter[MatrixCounter]+1;j--;)
385 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
386 Matrix[MatrixCounter][j][k] = value;
387 return true;
388};
389
390/** Sets all values in the last of MatrixContainer::Matrix to \a value.
391 * \param **values matrix with each value (must have at least same dimensions!)
392 * \param skipcolumns skip initial columns
393 * \return true if successful
394 */
395bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns)
396{
397 for(int j=RowCounter[MatrixCounter]+1;j--;)
398 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
399 Matrix[MatrixCounter][j][k] = values[j][k];
400 return true;
401};
402
403/** Sums the entries with each factor and put into last element of \a ***Matrix.
404 * Sums over "E"-terms to create the "F"-terms
405 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
406 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
407 * \param Order bond order
408 * \return true if summing was successful
409 */
410bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
411{
412 // go through each order
413 for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
414 //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
415 // then go per order through each suborder and pick together all the terms that contain this fragment
416 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
417 for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
418 if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
419 //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
420 // if the fragment's indices are all in the current fragment
421 for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
422 int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
423 //Log() << Verbose(0) << "Current index is " << k << "/" << m << "." << endl;
424 if (m != -1) { // if it's not an added hydrogen
425 for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
426 //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
427 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
428 m = l;
429 break;
430 }
431 }
432 //Log() << Verbose(0) << "Corresponding index in CurrentFragment is " << m << "." << endl;
433 if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
434 eLog() << Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl;
435 performCriticalExit();
436 return false;
437 }
438 if (Order == SubOrder) { // equal order is always copy from Energies
439 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
440 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
441 } else {
442 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;)
443 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
444 }
445 }
446 //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
447 //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
448 }
449 } else {
450 //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
451 }
452 }
453 }
454 //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
455 }
456
457 return true;
458};
459
460/** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
461 * \param *name inputdir
462 * \param *prefix prefix before \a EnergySuffix
463 * \return file was written
464 */
465bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix)
466{
467 ofstream output;
468 char *FragmentNumber = NULL;
469
470 Log() << Verbose(0) << "Writing fragment files." << endl;
471 for(int i=0;i<MatrixCounter;i++) {
472 stringstream line;
473 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
474 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
475 Free(&FragmentNumber);
476 output.open(line.str().c_str(), ios::out);
477 if (output == NULL) {
478 eLog() << Verbose(0) << "Unable to open output energy file " << line.str() << "!" << endl;
479 performCriticalExit();
480 return false;
481 }
482 output << Header[i] << endl;
483 for(int j=0;j<RowCounter[i];j++) {
484 for(int k=0;k<ColumnCounter[i];k++)
485 output << scientific << Matrix[i][j][k] << "\t";
486 output << endl;
487 }
488 output.close();
489 }
490 return true;
491};
492
493/** Writes the summed total values in the last matrix to file.
494 * \param *name inputdir
495 * \param *prefix prefix
496 * \param *suffix suffix
497 * \return file was written
498 */
499bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix)
500{
501 ofstream output;
502 stringstream line;
503
504 Log() << Verbose(0) << "Writing matrix values of " << suffix << "." << endl;
505 line << name << prefix << suffix;
506 output.open(line.str().c_str(), ios::out);
507 if (output == NULL) {
508 eLog() << Verbose(0) << "Unable to open output matrix file " << line.str() << "!" << endl;
509 performCriticalExit();
510 return false;
511 }
512 output << Header[MatrixCounter] << endl;
513 for(int j=0;j<RowCounter[MatrixCounter];j++) {
514 for(int k=0;k<ColumnCounter[MatrixCounter];k++)
515 output << scientific << Matrix[MatrixCounter][j][k] << "\t";
516 output << endl;
517 }
518 output.close();
519 return true;
520};
521
522// ======================================= CLASS EnergyMatrix =============================
523
524/** Create a trivial energy index mapping.
525 * This just maps 1 to 1, 2 to 2 and so on for all fragments.
526 * \return creation sucessful
527 */
528bool EnergyMatrix::ParseIndices()
529{
530 Log() << Verbose(0) << "Parsing energy indices." << endl;
531 Indices = Malloc<int*>(MatrixCounter + 1, "EnergyMatrix::ParseIndices: **Indices");
532 for(int i=MatrixCounter+1;i--;) {
533 Indices[i] = Malloc<int>(RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");
534 for(int j=RowCounter[i];j--;)
535 Indices[i][j] = j;
536 }
537 return true;
538};
539
540/** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
541 * Sums over the "F"-terms in ANOVA decomposition.
542 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
543 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
544 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
545 * \param Order bond order
546 * \parsm sign +1 or -1
547 * \return true if summing was successful
548 */
549bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class EnergyMatrix *CorrectionFragments, class KeySetsContainer &KeySets, int Order, double sign)
550{
551 // sum energy
552 if (CorrectionFragments == NULL)
553 for(int i=KeySets.FragmentsPerOrder[Order];i--;)
554 for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
555 for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
556 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k];
557 else
558 for(int i=KeySets.FragmentsPerOrder[Order];i--;)
559 for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
560 for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
561 Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySets.OrderSet[Order][i] ][j][k]);
562 return true;
563};
564
565/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
566 * \param *name directory with files
567 * \param *prefix prefix of each matrix file
568 * \param *suffix suffix of each matrix file
569 * \param skiplines number of inital lines to skip
570 * \param skiplines number of inital columns to skip
571 * \return parsing successful
572 */
573bool EnergyMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
574{
575 char filename[1024];
576 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
577
578 if (status) {
579 // count maximum of columns
580 RowCounter[MatrixCounter] = 0;
581 ColumnCounter[MatrixCounter] = 0;
582 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
583 if (RowCounter[j] > RowCounter[MatrixCounter])
584 RowCounter[MatrixCounter] = RowCounter[j];
585 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
586 ColumnCounter[MatrixCounter] = ColumnCounter[j];
587 }
588 // allocate last plus one matrix
589 Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
590 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
591 for(int j=0;j<=RowCounter[MatrixCounter];j++)
592 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
593
594 // try independently to parse global energysuffix file if present
595 strncpy(filename, name, 1023);
596 strncat(filename, prefix, 1023-strlen(filename));
597 strncat(filename, suffix.c_str(), 1023-strlen(filename));
598 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
599 }
600 return status;
601};
602
603// ======================================= CLASS ForceMatrix =============================
604
605/** Parsing force Indices of each fragment
606 * \param *name directory with \a ForcesFile
607 * \return parsing successful
608 */
609bool ForceMatrix::ParseIndices(const char *name)
610{
611 ifstream input;
612 char *FragmentNumber = NULL;
613 char filename[1023];
614 stringstream line;
615
616 Log() << Verbose(0) << "Parsing force indices for " << MatrixCounter << " matrices." << endl;
617 Indices = Malloc<int*>(MatrixCounter + 1, "ForceMatrix::ParseIndices: **Indices");
618 line << name << FRAGMENTPREFIX << FORCESFILE;
619 input.open(line.str().c_str(), ios::in);
620 //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
621 if (input == NULL) {
622 Log() << Verbose(0) << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
623 return false;
624 }
625 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
626 // get the number of atoms for this fragment
627 input.getline(filename, 1023);
628 line.str(filename);
629 // parse the values
630 Indices[i] = Malloc<int>(RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]");
631 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
632 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
633 Free(&FragmentNumber);
634 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
635 line >> Indices[i][j];
636 //Log() << Verbose(0) << " " << Indices[i][j];
637 }
638 //Log() << Verbose(0) << endl;
639 }
640 Indices[MatrixCounter] = Malloc<int>(RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]");
641 for(int j=RowCounter[MatrixCounter];j--;) {
642 Indices[MatrixCounter][j] = j;
643 }
644 input.close();
645 return true;
646};
647
648
649/** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
650 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
651 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
652 * \param Order bond order
653 * \param sign +1 or -1
654 * \return true if summing was successful
655 */
656bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
657{
658 int FragmentNr;
659 // sum forces
660 for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
661 FragmentNr = KeySets.OrderSet[Order][i];
662 for(int l=0;l<RowCounter[ FragmentNr ];l++) {
663 int j = Indices[ FragmentNr ][l];
664 if (j > RowCounter[MatrixCounter]) {
665 eLog() << Verbose(0) << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl;
666 performCriticalExit();
667 return false;
668 }
669 if (j != -1) {
670 //if (j == 0) Log() << Verbose(0) << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
671 for(int k=2;k<ColumnCounter[MatrixCounter];k++)
672 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
673 }
674 }
675 }
676 return true;
677};
678
679
680/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
681 * \param *name directory with files
682 * \param *prefix prefix of each matrix file
683 * \param *suffix suffix of each matrix file
684 * \param skiplines number of inital lines to skip
685 * \param skiplines number of inital columns to skip
686 * \return parsing successful
687 */
688bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
689{
690 char filename[1023];
691 ifstream input;
692 stringstream file;
693 int nr;
694 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
695
696 if (status) {
697 // count number of atoms for last plus one matrix
698 file << name << FRAGMENTPREFIX << KEYSETFILE;
699 input.open(file.str().c_str(), ios::in);
700 if (input == NULL) {
701 Log() << Verbose(0) << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
702 return false;
703 }
704 RowCounter[MatrixCounter] = 0;
705 while (!input.eof()) {
706 input.getline(filename, 1023);
707 stringstream zeile(filename);
708 while (!zeile.eof()) {
709 zeile >> nr;
710 //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
711 if (nr > RowCounter[MatrixCounter])
712 RowCounter[MatrixCounter] = nr;
713 }
714 }
715 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
716 input.close();
717
718 ColumnCounter[MatrixCounter] = 0;
719 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
720 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
721 ColumnCounter[MatrixCounter] = ColumnCounter[j];
722 }
723
724 // allocate last plus one matrix
725 Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
726 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
727 for(int j=0;j<=RowCounter[MatrixCounter];j++)
728 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
729
730 // try independently to parse global forcesuffix file if present
731 strncpy(filename, name, 1023);
732 strncat(filename, prefix, 1023-strlen(filename));
733 strncat(filename, suffix.c_str(), 1023-strlen(filename));
734 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
735 }
736
737
738 return status;
739};
740
741// ======================================= CLASS HessianMatrix =============================
742
743/** Parsing force Indices of each fragment
744 * \param *name directory with \a ForcesFile
745 * \return parsing successful
746 */
747bool HessianMatrix::ParseIndices(char *name)
748{
749 ifstream input;
750 char *FragmentNumber = NULL;
751 char filename[1023];
752 stringstream line;
753
754 Log() << Verbose(0) << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl;
755 Indices = Malloc<int*>(MatrixCounter + 1, "HessianMatrix::ParseIndices: **Indices");
756 line << name << FRAGMENTPREFIX << FORCESFILE;
757 input.open(line.str().c_str(), ios::in);
758 //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
759 if (input == NULL) {
760 Log() << Verbose(0) << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
761 return false;
762 }
763 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
764 // get the number of atoms for this fragment
765 input.getline(filename, 1023);
766 line.str(filename);
767 // parse the values
768 Indices[i] = Malloc<int>(RowCounter[i], "HessianMatrix::ParseIndices: *Indices[]");
769 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
770 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
771 Free(&FragmentNumber);
772 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
773 line >> Indices[i][j];
774 //Log() << Verbose(0) << " " << Indices[i][j];
775 }
776 //Log() << Verbose(0) << endl;
777 }
778 Indices[MatrixCounter] = Malloc<int>(RowCounter[MatrixCounter], "HessianMatrix::ParseIndices: *Indices[]");
779 for(int j=RowCounter[MatrixCounter];j--;) {
780 Indices[MatrixCounter][j] = j;
781 }
782 input.close();
783 return true;
784};
785
786
787/** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
788 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
789 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
790 * \param Order bond order
791 * \param sign +1 or -1
792 * \return true if summing was successful
793 */
794bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
795{
796 int FragmentNr;
797 // sum forces
798 for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
799 FragmentNr = KeySets.OrderSet[Order][i];
800 for(int l=0;l<RowCounter[ FragmentNr ];l++) {
801 int j = Indices[ FragmentNr ][l];
802 if (j > RowCounter[MatrixCounter]) {
803 eLog() << Verbose(0) << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl;
804 performCriticalExit();
805 return false;
806 }
807 if (j != -1) {
808 for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
809 int k = Indices[ FragmentNr ][m];
810 if (k > ColumnCounter[MatrixCounter]) {
811 eLog() << Verbose(0) << "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl;
812 performCriticalExit();
813 return false;
814 }
815 if (k != -1) {
816 //Log() << Verbose(0) << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
817 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
818 }
819 }
820 }
821 }
822 }
823 return true;
824};
825
826/** Constructor for class HessianMatrix.
827 */
828HessianMatrix::HessianMatrix() : MatrixContainer()
829{
830 IsSymmetric = true;
831}
832
833/** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
834 * Sums over "E"-terms to create the "F"-terms
835 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
836 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
837 * \param Order bond order
838 * \return true if summing was successful
839 */
840bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
841{
842 // go through each order
843 for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
844 //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
845 // then go per order through each suborder and pick together all the terms that contain this fragment
846 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
847 for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
848 if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
849 //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
850 // if the fragment's indices are all in the current fragment
851 for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
852 int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
853 //Log() << Verbose(0) << "Current row index is " << k << "/" << m << "." << endl;
854 if (m != -1) { // if it's not an added hydrogen
855 for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
856 //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
857 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
858 m = l;
859 break;
860 }
861 }
862 //Log() << Verbose(0) << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
863 if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
864 eLog() << Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl;
865 performCriticalExit();
866 return false;
867 }
868
869 for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
870 int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
871 //Log() << Verbose(0) << "Current column index is " << l << "/" << n << "." << endl;
872 if (n != -1) { // if it's not an added hydrogen
873 for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
874 //Log() << Verbose(0) << "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
875 if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
876 n = p;
877 break;
878 }
879 }
880 //Log() << Verbose(0) << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
881 if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
882 eLog() << Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl;
883 performCriticalExit();
884 return false;
885 }
886 if (Order == SubOrder) { // equal order is always copy from Energies
887 //Log() << Verbose(0) << "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
888 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
889 } else {
890 //Log() << Verbose(0) << "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
891 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
892 }
893 }
894 }
895 }
896 //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
897 //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
898 }
899 } else {
900 //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
901 }
902 }
903 }
904 //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
905 }
906
907 return true;
908};
909
910/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
911 * \param *name directory with files
912 * \param *prefix prefix of each matrix file
913 * \param *suffix suffix of each matrix file
914 * \param skiplines number of inital lines to skip
915 * \param skiplines number of inital columns to skip
916 * \return parsing successful
917 */
918bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
919{
920 char filename[1023];
921 ifstream input;
922 stringstream file;
923 int nr;
924 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
925
926 if (status) {
927 // count number of atoms for last plus one matrix
928 file << name << FRAGMENTPREFIX << KEYSETFILE;
929 input.open(file.str().c_str(), ios::in);
930 if (input == NULL) {
931 Log() << Verbose(0) << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
932 return false;
933 }
934 RowCounter[MatrixCounter] = 0;
935 ColumnCounter[MatrixCounter] = 0;
936 while (!input.eof()) {
937 input.getline(filename, 1023);
938 stringstream zeile(filename);
939 while (!zeile.eof()) {
940 zeile >> nr;
941 //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
942 if (nr > RowCounter[MatrixCounter]) {
943 RowCounter[MatrixCounter] = nr;
944 ColumnCounter[MatrixCounter] = nr;
945 }
946 }
947 }
948 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
949 ColumnCounter[MatrixCounter]++; // nr start at 0, count starts at 1
950 input.close();
951
952 // allocate last plus one matrix
953 Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
954 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
955 for(int j=0;j<=RowCounter[MatrixCounter];j++)
956 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
957
958 // try independently to parse global forcesuffix file if present
959 strncpy(filename, name, 1023);
960 strncat(filename, prefix, 1023-strlen(filename));
961 strncat(filename, suffix.c_str(), 1023-strlen(filename));
962 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
963 }
964
965
966 return status;
967};
968
969// ======================================= CLASS KeySetsContainer =============================
970
971/** Constructor of KeySetsContainer class.
972 */
973KeySetsContainer::KeySetsContainer() {
974 KeySets = NULL;
975 AtomCounter = NULL;
976 FragmentCounter = 0;
977 Order = 0;
978 FragmentsPerOrder = 0;
979 OrderSet = NULL;
980};
981
982/** Destructor of KeySetsContainer class.
983 */
984KeySetsContainer::~KeySetsContainer() {
985 for(int i=FragmentCounter;i--;)
986 Free(&KeySets[i]);
987 for(int i=Order;i--;)
988 Free(&OrderSet[i]);
989 Free(&KeySets);
990 Free(&OrderSet);
991 Free(&AtomCounter);
992 Free(&FragmentsPerOrder);
993};
994
995/** Parsing KeySets into array.
996 * \param *name directory with keyset file
997 * \param *ACounter number of atoms per fragment
998 * \param FCounter number of fragments
999 * \return parsing succesful
1000 */
1001bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
1002 ifstream input;
1003 char *FragmentNumber = NULL;
1004 stringstream file;
1005 char filename[1023];
1006
1007 FragmentCounter = FCounter;
1008 Log() << Verbose(0) << "Parsing key sets." << endl;
1009 KeySets = Malloc<int*>(FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets");
1010 for(int i=FragmentCounter;i--;)
1011 KeySets[i] = NULL;
1012 file << name << FRAGMENTPREFIX << KEYSETFILE;
1013 input.open(file.str().c_str(), ios::in);
1014 if (input == NULL) {
1015 Log() << Verbose(0) << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
1016 return false;
1017 }
1018
1019 AtomCounter = Malloc<int>(FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter");
1020 for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
1021 stringstream line;
1022 AtomCounter[i] = ACounter[i];
1023 // parse the values
1024 KeySets[i] = Malloc<int>(AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]");
1025 for(int j=AtomCounter[i];j--;)
1026 KeySets[i][j] = -1;
1027 FragmentNumber = FixedDigitNumber(FragmentCounter, i);
1028 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
1029 Free(&FragmentNumber);
1030 input.getline(filename, 1023);
1031 line.str(filename);
1032 for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
1033 line >> KeySets[i][j];
1034 //Log() << Verbose(0) << " " << KeySets[i][j];
1035 }
1036 //Log() << Verbose(0) << endl;
1037 }
1038 input.close();
1039 return true;
1040};
1041
1042/** Parse many body terms, associating each fragment to a certain bond order.
1043 * \return parsing succesful
1044 */
1045bool KeySetsContainer::ParseManyBodyTerms()
1046{
1047 int Counter;
1048
1049 Log() << Verbose(0) << "Creating Fragment terms." << endl;
1050 // scan through all to determine maximum order
1051 Order=0;
1052 for(int i=FragmentCounter;i--;) {
1053 Counter=0;
1054 for(int j=AtomCounter[i];j--;)
1055 if (KeySets[i][j] != -1)
1056 Counter++;
1057 if (Counter > Order)
1058 Order = Counter;
1059 }
1060 Log() << Verbose(0) << "Found Order is " << Order << "." << endl;
1061
1062 // scan through all to determine fragments per order
1063 FragmentsPerOrder = Malloc<int>(Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder");
1064 for(int i=Order;i--;)
1065 FragmentsPerOrder[i] = 0;
1066 for(int i=FragmentCounter;i--;) {
1067 Counter=0;
1068 for(int j=AtomCounter[i];j--;)
1069 if (KeySets[i][j] != -1)
1070 Counter++;
1071 FragmentsPerOrder[Counter-1]++;
1072 }
1073 for(int i=0;i<Order;i++)
1074 Log() << Verbose(0) << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl;
1075
1076 // scan through all to gather indices to each order set
1077 OrderSet = Malloc<int*>(Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet");
1078 for(int i=Order;i--;)
1079 OrderSet[i] = Malloc<int>(FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]");
1080 for(int i=Order;i--;)
1081 FragmentsPerOrder[i] = 0;
1082 for(int i=FragmentCounter;i--;) {
1083 Counter=0;
1084 for(int j=AtomCounter[i];j--;)
1085 if (KeySets[i][j] != -1)
1086 Counter++;
1087 OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
1088 FragmentsPerOrder[Counter-1]++;
1089 }
1090 Log() << Verbose(0) << "Printing OrderSet." << endl;
1091 for(int i=0;i<Order;i++) {
1092 for (int j=0;j<FragmentsPerOrder[i];j++) {
1093 Log() << Verbose(0) << " " << OrderSet[i][j];
1094 }
1095 Log() << Verbose(0) << endl;
1096 }
1097 Log() << Verbose(0) << endl;
1098
1099
1100 return true;
1101};
1102
1103/** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
1104 * \param GreaterSet index to greater set
1105 * \param SmallerSet index to smaller set
1106 * \return true if all keys of SmallerSet contained in GreaterSet
1107 */
1108bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
1109{
1110 bool result = true;
1111 bool intermediate;
1112 if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
1113 return false;
1114 for(int i=AtomCounter[SmallerSet];i--;) {
1115 intermediate = false;
1116 for (int j=AtomCounter[GreaterSet];j--;)
1117 intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
1118 result = result && intermediate;
1119 }
1120
1121 return result;
1122};
1123
1124
1125// ======================================= END =============================================
Note: See TracBrowser for help on using the repository browser.