source: src/parser.cpp@ 0d0316

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 0d0316 was 0d0316, checked in by Frederik Heber <heber@…>, 14 years ago

Removed FixedDigitNumber from Helpers/helpers.

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