source: molecuilder/src/parser.cpp@ 48efc3

Last change on this file since 48efc3 was 48efc3, checked in by Frederik Heber <heber@…>, 17 years ago

MatrixContainer::MatrixContainer() debug message fixes only, ForceMatrix::ParseIndices() re-done parsing of Forces-Factors file

ParseIndices() was hacked down to just ...[j]=j entries in the belief that we do not need ForcesFactors anymore. However, we do due to the saturated hydrogen and hence the index parsing thrown out before is needed again. This caused joiner to return faulty forces of course.

  • Property mode set to 100644
File size: 24.2 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 "parser.hpp"
11
12// ======================================= FUNCTIONS ==========================================
13
14/** Test if given filename can be opened.
15 * \param filename name of file
16 * \return given file exists
17 */
18#ifdef HAVE_INLINE
19inline bool FilePresent(const char *filename)
20#else
21bool FilePresent(const char *filename)
22#endif
23{
24 ifstream input;
25
26 input.open(filename, ios::in);
27 if (input == NULL) {
28 cout << endl << "Unable to open " << filename << ", is the directory correct?" << endl;
29 return false;
30 }
31 input.close();
32 return true;
33};
34
35/** Test the given parameters.
36 * \param argc argument count
37 * \param **argv arguments array
38 * \return given inputdir is valid
39 */
40bool TestParams(int argc, char **argv)
41{
42 ifstream input;
43 stringstream line;
44
45 line << argv[1] << FRAGMENTPREFIX << KEYSETFILE;
46 return FilePresent(line.str().c_str());
47};
48
49// ======================================= CLASS MatrixContainer =============================
50
51/** Constructor of MatrixContainer class.
52 */
53MatrixContainer::MatrixContainer() {
54 Matrix = NULL;
55 Indices = NULL;
56 Header = NULL;
57 MatrixCounter = 0;
58 RowCounter = NULL;
59 ColumnCounter = 0;
60};
61
62/** Destructor of MatrixContainer class.
63 */
64MatrixContainer::~MatrixContainer() {
65 if (Matrix != NULL) {
66 for(int i=0;i<MatrixCounter;i++) {
67 if (RowCounter != NULL) {
68 for(int j=0;j<=RowCounter[i];j++)
69 Free((void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]");
70 Free((void **)&Matrix[i], "MatrixContainer::~MatrixContainer: **Matrix[]");
71 }
72 }
73 if ((RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
74 for(int j=0;j<=RowCounter[MatrixCounter];j++)
75 Free((void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]");
76 if (MatrixCounter != 0)
77 Free((void **)&Matrix[MatrixCounter], "MatrixContainer::~MatrixContainer: **Matrix[MatrixCounter]");
78 Free((void **)&Matrix, "MatrixContainer::~MatrixContainer: ***Matrix");
79 }
80 if (Indices != NULL)
81 for(int i=0;i<=MatrixCounter;i++) {
82 Free((void **)&Indices[i], "MatrixContainer::~MatrixContainer: *Indices[]");
83 }
84 Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices");
85
86 Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header");
87 Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter");
88};
89
90/** Parsing a number of matrices.
91 * \param *name directory with files
92 * \param *prefix prefix of each matrix file
93 * \param *suffix suffix of each matrix file
94 * \param skiplines number of inital lines to skip
95 * \param skiplines number of inital columns to skip
96 * \return parsing successful
97 */
98bool MatrixContainer::ParseMatrix(char *name, char *prefix, char *suffix, int skiplines, int skipcolumns)
99{
100 char filename[1023];
101 ifstream input;
102 char *FragmentNumber = NULL;
103 stringstream line;
104
105 Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::ParseMatrix: *EnergyHeader");
106
107 // count the number of matrices
108 MatrixCounter = -1; // we count one too much
109 line << name << FRAGMENTPREFIX << KEYSETFILE;
110 input.open(line.str().c_str(), ios::in);
111 if (input == NULL) {
112 cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
113 return false;
114 }
115 while (!input.eof()) {
116 input.getline(filename, 1023);
117 MatrixCounter++;
118 }
119 input.close();
120 cout << "Determined " << MatrixCounter << " fragments." << endl;
121
122 cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl;
123 Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: ***Matrix"); // one more each for the total molecule
124 RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: *RowCounter");
125 for(int i=0;i<=MatrixCounter;i++) {
126 Matrix[i] = NULL;
127 RowCounter[i] = -1;
128 }
129 for(int i=0;i<=MatrixCounter;i++) {
130 // energy
131 stringstream line;
132 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
133 if (i != MatrixCounter)
134 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix << suffix;
135 else
136 line << name << prefix << suffix;
137 Free((void **)&FragmentNumber, "MatrixContainer::ParseMatrix: *FragmentNumber");
138 input.open(line.str().c_str(), ios::in);
139 //cout << "Opening " << line.str() << " ... " << input << endl;
140 if (input == NULL) {
141 cerr << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
142 return false;
143 }
144 // scan header for number of columns
145 for (int m=0;m<=skiplines;m++)
146 input.getline(Header, 1023);
147 line.str(Header);
148 for(int k=0;k<skipcolumns;k++)
149 line >> Header;
150 //cout << line.str() << endl;
151 ColumnCounter=0;
152 while (!line.eof()) {
153 strcpy(filename,"@");
154 line >> filename;
155 if (filename[0] != '@')
156 ColumnCounter++;
157 }
158 //cout << line.str() << endl;
159 //cout << "ColumnCounter: " << ColumnCounter << endl;
160 // scan rest for number of rows/lines
161 RowCounter[i]=-1; // counts one line too much
162 while (!input.eof()) {
163 input.getline(filename, 1023);
164 //cout << "Comparing: " << strncmp(filename,"MeanForce",9) << endl;
165 RowCounter[i]++; // then line was not last MeanForce
166 if (strncmp(filename,"MeanForce", 9) == 0) {
167 break;
168 }
169 }
170 cout << "RowCounter[" << i << "]: " << RowCounter[i] << endl;
171 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]");
172 input.clear();
173 input.seekg(ios::beg);
174 for (int m=0;m<=skiplines;m++)
175 input.getline(Header, 1023); // skip header
176 line.str(Header);
177 for(int k=0;k<skipcolumns;k++) // skip columns in header too
178 line >> filename;
179 strncpy(Header, line.str().c_str(), 1023);
180 for(int j=0;j<RowCounter[i];j++) {
181 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]");
182 input.getline(filename, 1023);
183 stringstream line(filename);
184 //cout << "Matrix at level " << j << ":";// << filename << endl;
185 for(int k=0;k<skipcolumns;k++)
186 line >> filename;
187 for(int k=0;(k<ColumnCounter) && (!line.eof());k++) {
188 line >> Matrix[i][j][k];
189 //cout << " " << setprecision(2) << Matrix[i][j][k];;
190 }
191 //cout << endl;
192 }
193 Matrix[i][ RowCounter[i] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[RowCounter[i]][]");
194 for(int j=0;j<ColumnCounter;j++)
195 Matrix[i][ RowCounter[i] ][j] = 0.;
196 input.close();
197 }
198 return true;
199};
200
201/** Allocates and resets the memory for a number \a MCounter of matrices.
202 * \param *GivenHeader Header line
203 * \param MCounter number of matrices
204 * \param *RCounter number of rows for each matrix
205 * \param CCounter number of columns for all matrices
206 * \return Allocation successful
207 */
208bool MatrixContainer::AllocateMatrix(char *GivenHeader, int MCounter, int *RCounter, int CCounter)
209{
210 Header = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseMatrix: *EnergyHeader");
211 strncpy(Header, GivenHeader, 1023);
212
213 MatrixCounter = MCounter;
214 ColumnCounter = CCounter;
215 Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: ***Matrix"); // one more each for the total molecule
216 RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: *RowCounter");
217 for(int i=0;i<=MatrixCounter;i++) {
218 RowCounter[i] = RCounter[i];
219 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]");
220 for(int j=0;j<=RowCounter[i];j++) {
221 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]");
222 for(int k=0;k<ColumnCounter;k++)
223 Matrix[i][j][k] = 0.;
224 }
225 }
226 return true;
227};
228
229/** Resets all values in MatrixContainer::Matrix.
230 * \return true if successful
231 */
232bool MatrixContainer::ResetMatrix()
233{
234 for(int i=0;i<=MatrixCounter;i++)
235 for(int j=0;j<=RowCounter[i];j++)
236 for(int k=0;k<ColumnCounter;k++)
237 Matrix[i][j][k] = 0.;
238 return true;
239};
240
241/** Sets all values in the last of MatrixContainer::Matrix to \a value.
242 * \param value reset value
243 * \param skipcolumns skip initial columns
244 * \return true if successful
245 */
246bool MatrixContainer::SetLastMatrix(double value, int skipcolumns)
247{
248 for(int j=0;j<=RowCounter[MatrixCounter];j++)
249 for(int k=skipcolumns;k<ColumnCounter;k++)
250 Matrix[MatrixCounter][j][k] = value;
251 return true;
252};
253
254/** Sets all values in the last of MatrixContainer::Matrix to \a value.
255 * \param **values matrix with each value (must have at least same dimensions!)
256 * \param skipcolumns skip initial columns
257 * \return true if successful
258 */
259bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns)
260{
261 for(int j=0;j<=RowCounter[MatrixCounter];j++)
262 for(int k=skipcolumns;k<ColumnCounter;k++)
263 Matrix[MatrixCounter][j][k] = values[j][k];
264 return true;
265};
266
267/** Sums the energy with each factor and put into last element of \a ***Energies.
268 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
269 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
270 * \param Order bond order
271 * \return true if summing was successful
272 */
273bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order)
274{
275 // go through each order
276 for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) {
277 //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
278 // then go per order through each suborder and pick together all the terms that contain this fragment
279 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
280 for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
281 if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) {
282 //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl;
283 // if the fragment's indices are all in the current fragment
284 for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
285 int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k];
286 //cout << "Current index is " << k << "/" << m << "." << endl;
287 if (m != -1) { // if it's not an added hydrogen
288 for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
289 //cout << "Comparing " << m << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
290 if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) {
291 m = l;
292 break;
293 }
294 }
295 //cout << "Corresponding index in CurrentFragment is " << m << "." << endl;
296 if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
297 cerr << "Current force index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
298 return false;
299 }
300 if (Order == SubOrder) { // equal order is always copy from Energies
301 for(int l=0;l<ColumnCounter;l++) // then adds/subtract each column
302 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
303 } else {
304 for(int l=0;l<ColumnCounter;l++)
305 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
306 }
307 }
308 //if ((ColumnCounter>1) && (RowCounter[0]-1 >= 1))
309 //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
310 }
311 } else {
312 //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
313 }
314 }
315 }
316 //cout << "Final Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << KeySet.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][KeySet.AtomCounter[0]-1][1] << endl;
317 }
318
319 return true;
320};
321
322/** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
323 * \param *name inputdir
324 * \param *prefix prefix before \a EnergySuffix
325 * \return file was written
326 */
327bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix)
328{
329 ofstream output;
330 char *FragmentNumber = NULL;
331
332 cout << "Writing fragment files." << endl;
333 for(int i=0;i<MatrixCounter;i++) {
334 stringstream line;
335 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
336 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
337 Free((void **)&FragmentNumber, "*FragmentNumber");
338 output.open(line.str().c_str(), ios::out);
339 if (output == NULL) {
340 cerr << "Unable to open output energy file " << line.str() << "!" << endl;
341 return false;
342 }
343 output << Header << endl;
344 for(int j=0;j<RowCounter[i];j++) {
345 for(int k=0;k<ColumnCounter;k++)
346 output << scientific << Matrix[i][j][k] << "\t";
347 output << endl;
348 }
349 output.close();
350 }
351 return true;
352};
353
354/** Writes the summed total values in the last matrix to file.
355 * \param *name inputdir
356 * \param *prefix prefix
357 * \param *suffix suffix
358 * \return file was written
359 */
360bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix)
361{
362 ofstream output;
363 stringstream line;
364
365 cout << "Writing matrix values of " << suffix << "." << endl;
366 line << name << prefix << suffix;
367 output.open(line.str().c_str(), ios::out);
368 if (output == NULL) {
369 cerr << "Unable to open output matrix file " << line.str() << "!" << endl;
370 return false;
371 }
372 output << Header << endl;
373 for(int j=0;j<RowCounter[MatrixCounter];j++) {
374 for(int k=0;k<ColumnCounter;k++)
375 output << scientific << Matrix[MatrixCounter][j][k] << "\t";
376 output << endl;
377 }
378 output.close();
379 return true;
380};
381
382// ======================================= CLASS EnergyMatrix =============================
383
384/** Create a trivial energy index mapping.
385 * This just maps 1 to 1, 2 to 2 and so on for all fragments.
386 * \return creation sucessful
387 */
388bool EnergyMatrix::ParseIndices()
389{
390 cout << "Parsing energy indices." << endl;
391 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices");
392 for(int i=0;i<=MatrixCounter;i++) {
393 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");
394 for(int j=0;j<RowCounter[i];j++)
395 Indices[i][j] = j;
396 }
397 return true;
398};
399
400/** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
401 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
402 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
403 * \param Order bond order
404 * \return true if summing was successful
405 */
406bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class KeySetsContainer &KeySet, int Order)
407{
408 // sum energy
409 for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++)
410 for(int j=0;j<RowCounter[ KeySet.OrderSet[Order][i] ];j++)
411 for(int k=0;k<ColumnCounter;k++)
412 Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k];
413 return true;
414};
415
416// ======================================= CLASS ForceMatrix =============================
417
418/** Parsing force Indices of each fragment
419 * \param *name directory with \a ForcesFile
420 * \return parsing successful
421 */
422bool ForceMatrix::ParseIndices(char *name)
423{
424 ifstream input;
425 char *FragmentNumber = NULL;
426 char filename[1023];
427 stringstream line;
428
429 cout << "Parsing force indices." << endl;
430 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "ForceMatrix::ParseIndices: **Indices");
431 line << name << FRAGMENTPREFIX << FORCESFILE;
432 input.open(line.str().c_str(), ios::in);
433 //cout << "Opening " << line.str() << " ... " << input << endl;
434 if (input == NULL) {
435 cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
436 return false;
437 }
438 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
439 // get the number of atoms for this fragment
440 input.getline(filename, 1023);
441 line.str(filename);
442 // parse the values
443 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]");
444 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
445 cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
446 Free((void **)&FragmentNumber, "ForceMatrix::ParseIndices: *FragmentNumber");
447 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
448 line >> Indices[i][j];
449 cout << " " << Indices[i][j];
450 }
451 cout << endl;
452 }
453 Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]");
454 for(int j=0;j<RowCounter[MatrixCounter];j++) {
455 Indices[MatrixCounter][j] = j;
456 }
457 input.close();
458 return true;
459};
460
461
462/** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
463 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
464 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
465 * \param Order bond order
466 * \return true if summing was successful
467 */
468bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySet, int Order)
469{
470 // sum forces
471 for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++)
472 for(int l=0;l<RowCounter[ KeySet.OrderSet[Order][i] ];l++) {
473 int j = Indices[ KeySet.OrderSet[Order][i] ][l];
474 if (j > RowCounter[MatrixCounter]) {
475 cerr << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl;
476 return false;
477 }
478 if (j != -1)
479 for(int k=2;k<ColumnCounter;k++)
480 Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][l][k];
481 }
482 return true;
483};
484
485
486// ======================================= CLASS KeySetsContainer =============================
487
488/** Constructor of KeySetsContainer class.
489 */
490KeySetsContainer::KeySetsContainer() {
491 KeySets = NULL;
492 AtomCounter = NULL;
493 FragmentCounter = 0;
494 Order = 0;
495 FragmentsPerOrder = 0;
496 OrderSet = NULL;
497};
498
499/** Destructor of KeySetsContainer class.
500 */
501KeySetsContainer::~KeySetsContainer() {
502 for(int i=0;i<FragmentCounter;i++)
503 Free((void **)&KeySets[i], "KeySetsContainer::~KeySetsContainer: *KeySets[]");
504 for(int i=0;i<Order;i++)
505 Free((void **)&OrderSet[i], "KeySetsContainer::~KeySetsContainer: *OrderSet[]");
506 Free((void **)&KeySets, "KeySetsContainer::~KeySetsContainer: **KeySets");
507 Free((void **)&OrderSet, "KeySetsContainer::~KeySetsContainer: **OrderSet");
508 Free((void **)&AtomCounter, "KeySetsContainer::~KeySetsContainer: *AtomCounter");
509 Free((void **)&FragmentsPerOrder, "KeySetsContainer::~KeySetsContainer: *FragmentsPerOrder");
510};
511
512/** Parsing KeySets into array.
513 * \param *name directory with keyset file
514 * \param *ACounter number of atoms per fragment
515 * \param FCounter number of fragments
516 * \return parsing succesful
517 */
518bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
519 ifstream input;
520 char *FragmentNumber = NULL;
521 stringstream line;
522 char filename[1023];
523
524 FragmentCounter = FCounter;
525 cout << "Parsing key sets." << endl;
526 KeySets = (int **) Malloc(sizeof(int *)*FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets");
527 for(int i=0;i<FragmentCounter;i++)
528 KeySets[i] = NULL;
529 line << name << FRAGMENTPREFIX << KEYSETFILE;
530 input.open(line.str().c_str(), ios::in);
531 if (input == NULL) {
532 cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
533 return false;
534 }
535
536 AtomCounter = (int *) Malloc(sizeof(int)*FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter");
537 for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
538 stringstream line;
539 AtomCounter[i] = ACounter[i];
540 // parse the values
541 KeySets[i] = (int *) Malloc(sizeof(int)*AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]");
542 for(int j=0;j<AtomCounter[i];j++)
543 KeySets[i][j] = -1;
544 FragmentNumber = FixedDigitNumber(FragmentCounter, i);
545 cout << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
546 Free((void **)&FragmentNumber, "KeySetsContainer::ParseKeySets: *FragmentNumber");
547 input.getline(filename, 1023);
548 line.str(filename);
549 for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
550 line >> KeySets[i][j];
551 cout << " " << KeySets[i][j];
552 }
553 cout << endl;
554 }
555 input.close();
556 return true;
557};
558
559/** Parse many body terms, associating each fragment to a certain bond order.
560 * \return parsing succesful
561 */
562bool KeySetsContainer::ParseManyBodyTerms()
563{
564 int Counter;
565
566 cout << "Creating Fragment terms." << endl;
567 // scan through all to determine order
568 Order=0;
569 for(int i=0;i<FragmentCounter;i++) {
570 Counter=0;
571 for(int j=0;j<AtomCounter[i];j++)
572 if (KeySets[i][j] != -1)
573 Counter++;
574 if (Counter > Order)
575 Order = Counter;
576 }
577 cout << "Found Order is " << Order << "." << endl;
578
579 // scan through all to determine fragments per order
580 FragmentsPerOrder = (int *) Malloc(sizeof(int)*Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder");
581 for(int i=0;i<Order;i++)
582 FragmentsPerOrder[i] = 0;
583 for(int i=0;i<FragmentCounter;i++) {
584 Counter=0;
585 for(int j=0;j<AtomCounter[i];j++)
586 if (KeySets[i][j] != -1)
587 Counter++;
588 FragmentsPerOrder[Counter-1]++;
589 }
590 for(int i=0;i<Order;i++)
591 cout << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl;
592
593 // scan through all to gather indices to each order set
594 OrderSet = (int **) Malloc(sizeof(int *)*Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet");
595 for(int i=0;i<Order;i++)
596 OrderSet[i] = (int *) Malloc(sizeof(int)*FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]");
597 for(int i=0;i<Order;i++)
598 FragmentsPerOrder[i] = 0;
599 for(int i=0;i<FragmentCounter;i++) {
600 Counter=0;
601 for(int j=0;j<AtomCounter[i];j++)
602 if (KeySets[i][j] != -1)
603 Counter++;
604 OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
605 FragmentsPerOrder[Counter-1]++;
606 }
607 cout << "Printing OrderSet." << endl;
608 for(int i=0;i<Order;i++) {
609 for (int j=0;j<FragmentsPerOrder[i];j++) {
610 cout << " " << OrderSet[i][j];
611 }
612 cout << endl;
613 }
614 cout << endl;
615
616
617 return true;
618};
619
620/** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
621 * \param GreaterSet index to greater set
622 * \param SmallerSet index to smaller set
623 * \return true if all keys of SmallerSet contained in GreaterSet
624 */
625bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
626{
627 bool result = true;
628 bool intermediate;
629 if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
630 return false;
631 for(int i=0;i<AtomCounter[SmallerSet];i++) {
632 intermediate = false;
633 for (int j=0;j<AtomCounter[GreaterSet];j++)
634 intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
635 result = result && intermediate;
636 }
637
638 return result;
639};
640
641
642// ======================================= END =============================================
Note: See TracBrowser for help on using the repository browser.