Changes in src/parser.cpp [29812d:042f82]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/parser.cpp
r29812d r042f82 8 8 9 9 #include "helpers.hpp" 10 #include "memoryallocator.hpp"11 10 #include "parser.hpp" 12 11 … … 57 56 MatrixContainer::MatrixContainer() { 58 57 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; 58 Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::MatrixContainer: *Header"); 59 Matrix = (double ***) Malloc(sizeof(double **)*(1), "MatrixContainer::MatrixContainer: ***Matrix"); // one more each for the total molecule 60 RowCounter = (int *) Malloc(sizeof(int)*(1), "MatrixContainer::MatrixContainer: *RowCounter"); 64 61 Matrix[0] = NULL; 65 62 RowCounter[0] = -1; 66 63 MatrixCounter = 0; 67 ColumnCounter [0] = -1;64 ColumnCounter = 0; 68 65 }; 69 66 … … 73 70 if (Matrix != NULL) { 74 71 for(int i=MatrixCounter;i--;) { 75 if ( (ColumnCounter != NULL) && (RowCounter != NULL)) {72 if (RowCounter != NULL) { 76 73 for(int j=RowCounter[i]+1;j--;) 77 Free( &Matrix[i][j]);78 Free( &Matrix[i]);74 Free((void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]"); 75 Free((void **)&Matrix[i], "MatrixContainer::~MatrixContainer: **Matrix[]"); 79 76 } 80 77 } 81 if (( ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))78 if ((RowCounter != NULL) && (Matrix[MatrixCounter] != NULL)) 82 79 for(int j=RowCounter[MatrixCounter]+1;j--;) 83 Free( &Matrix[MatrixCounter][j]);80 Free((void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]"); 84 81 if (MatrixCounter != 0) 85 Free( &Matrix[MatrixCounter]);86 Free( &Matrix);82 Free((void **)&Matrix[MatrixCounter], "MatrixContainer::~MatrixContainer: **Matrix[MatrixCounter]"); 83 Free((void **)&Matrix, "MatrixContainer::~MatrixContainer: ***Matrix"); 87 84 } 88 85 if (Indices != NULL) 89 86 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 */ 107 bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix) 108 { 109 cout << "Initialising indices"; 110 if (Matrix == NULL) { 111 cout << " 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 cout << " 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 //cout << Indices[i][j] << "\t"; 130 } 131 //cout << endl; 132 } 133 } 134 return true; 135 }; 87 Free((void **)&Indices[i], "MatrixContainer::~MatrixContainer: *Indices[]"); 88 } 89 Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices"); 90 91 Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header"); 92 Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter"); 93 }; 94 136 95 137 96 /** Parsing a number of matrices. … … 162 121 } 163 122 164 // parse header 165 Header[MatrixNr] = Malloc<char>(1024, "MatrixContainer::ParseMatrix: *Header[]"); 123 // skip some initial lines 166 124 for (int m=skiplines+1;m--;) 167 input.getline(Header [MatrixNr], 1023);168 125 input.getline(Header, 1023); 126 169 127 // scan header for number of columns 170 line.str(Header [MatrixNr]);128 line.str(Header); 171 129 for(int k=skipcolumns;k--;) 172 line >> Header [MatrixNr];130 line >> Header; 173 131 //cout << line.str() << endl; 174 ColumnCounter [MatrixNr]=0;132 ColumnCounter=0; 175 133 while ( getline(line,token, '\t') ) { 176 134 if (token.length() > 0) 177 ColumnCounter [MatrixNr]++;135 ColumnCounter++; 178 136 } 179 137 //cout << line.str() << endl; 180 //cout << "ColumnCounter [" << MatrixNr << "]: " << ColumnCounter[MatrixNr]<< "." << endl;181 if (ColumnCounter [MatrixNr]== 0)182 cerr << "ColumnCounter [" << MatrixNr << "]: " << ColumnCounter[MatrixNr]<< " from file " << name << ", this is probably an error!" << endl;183 138 //cout << "ColumnCounter: " << ColumnCounter << "." << endl; 139 if (ColumnCounter == 0) 140 cerr << "ColumnCounter: " << ColumnCounter << " from file " << name << ", this is probably an error!" << endl; 141 184 142 // scan rest for number of rows/lines 185 143 RowCounter[MatrixNr]=-1; // counts one line too much … … 197 155 198 156 // allocate matrix if it's not zero dimension in one direction 199 if ((ColumnCounter [MatrixNr]> 0) && (RowCounter[MatrixNr] > -1)) {200 Matrix[MatrixNr] = Malloc<double*>(RowCounter[MatrixNr] + 1, "MatrixContainer::ParseMatrix: **Matrix[]");201 157 if ((ColumnCounter > 0) && (RowCounter[MatrixNr] > -1)) { 158 Matrix[MatrixNr] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixNr]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 159 202 160 // parse in each entry for this matrix 203 161 input.clear(); 204 162 input.seekg(ios::beg); 205 163 for (int m=skiplines+1;m--;) 206 input.getline(Header [MatrixNr], 1023); // skip header207 line.str(Header [MatrixNr]);164 input.getline(Header, 1023); // skip header 165 line.str(Header); 208 166 for(int k=skipcolumns;k--;) // skip columns in header too 209 167 line >> filename; 210 strncpy(Header [MatrixNr], line.str().c_str(), 1023);168 strncpy(Header, line.str().c_str(), 1023); 211 169 for(int j=0;j<RowCounter[MatrixNr];j++) { 212 Matrix[MatrixNr][j] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[][]");170 Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 213 171 input.getline(filename, 1023); 214 172 stringstream lines(filename); … … 216 174 for(int k=skipcolumns;k--;) 217 175 lines >> filename; 218 for(int k=0;(k<ColumnCounter [MatrixNr]) && (!lines.eof());k++) {176 for(int k=0;(k<ColumnCounter) && (!lines.eof());k++) { 219 177 lines >> Matrix[MatrixNr][j][k]; 220 178 //cout << " " << setprecision(2) << Matrix[MatrixNr][j][k];; 221 179 } 222 180 //cout << endl; 223 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[RowCounter[MatrixNr]][]");224 for(int j=ColumnCounter [MatrixNr];j--;)181 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[RowCounter[MatrixNr]][]"); 182 for(int j=ColumnCounter;j--;) 225 183 Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.; 226 184 } 227 185 } else { 228 cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter [MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl;186 cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl; 229 187 } 230 188 input.close(); … … 275 233 276 234 cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl; 277 Header = ReAlloc<char*>(Header, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: **Header"); // one more each for the total molecule 278 Matrix = ReAlloc<double**>(Matrix, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule 279 RowCounter = ReAlloc<int>(RowCounter, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: *RowCounter"); 280 ColumnCounter = ReAlloc<int>(ColumnCounter, MatrixCounter + 1, "MatrixContainer::ParseFragmentMatrix: *ColumnCounter"); 235 Matrix = (double ***) ReAlloc(Matrix, sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule 236 RowCounter = (int *) ReAlloc(RowCounter, sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *RowCounter"); 281 237 for(int i=MatrixCounter+1;i--;) { 282 238 Matrix[i] = NULL; 283 Header[i] = NULL;284 239 RowCounter[i] = -1; 285 ColumnCounter[i] = -1;286 240 } 287 241 for(int i=0; i < MatrixCounter;i++) { … … 292 246 if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i)) 293 247 return false; 294 Free( &FragmentNumber);248 Free((void **)&FragmentNumber, "MatrixContainer::ParseFragmentMatrix: *FragmentNumber"); 295 249 } 296 250 return true; … … 298 252 299 253 /** Allocates and resets the memory for a number \a MCounter of matrices. 300 * \param * *GivenHeader Header line for each matrix254 * \param *GivenHeader Header line 301 255 * \param MCounter number of matrices 302 256 * \param *RCounter number of rows for each matrix 303 * \param *CCounter number of columns for each matrix 304 * \return Allocation successful 305 */ 306 bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter) 307 { 257 * \param CCounter number of columns for all matrices 258 * \return Allocation successful 259 */ 260 bool MatrixContainer::AllocateMatrix(const char *GivenHeader, int MCounter, int *RCounter, int CCounter) 261 { 262 Header = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseFragmentMatrix: *EnergyHeader"); 263 strncpy(Header, GivenHeader, 1023); 264 308 265 MatrixCounter = MCounter; 309 Header = Malloc<char*>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *Header"); 310 Matrix = Malloc<double**>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: ***Matrix"); // one more each for the total molecule 311 RowCounter = Malloc<int>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *RowCounter"); 312 ColumnCounter = Malloc<int>(MatrixCounter + 1, "MatrixContainer::AllocateMatrix: *ColumnCounter"); 266 ColumnCounter = CCounter; 267 Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule 268 RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *RowCounter"); 313 269 for(int i=MatrixCounter+1;i--;) { 314 Header[i] = Malloc<char>(1024, "MatrixContainer::AllocateMatrix: *Header[i]");315 strncpy(Header[i], GivenHeader[i], 1023);316 270 RowCounter[i] = RCounter[i]; 317 ColumnCounter[i] = CCounter[i]; 318 Matrix[i] = Malloc<double*>(RowCounter[i] + 1, "MatrixContainer::AllocateMatrix: **Matrix[]"); 271 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 319 272 for(int j=RowCounter[i]+1;j--;) { 320 Matrix[i][j] = Malloc<double>(ColumnCounter[i], "MatrixContainer::AllocateMatrix: *Matrix[][]");321 for(int k=ColumnCounter [i];k--;)273 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 274 for(int k=ColumnCounter;k--;) 322 275 Matrix[i][j][k] = 0.; 323 276 } … … 333 286 for(int i=MatrixCounter+1;i--;) 334 287 for(int j=RowCounter[i]+1;j--;) 335 for(int k=ColumnCounter [i];k--;)288 for(int k=ColumnCounter;k--;) 336 289 Matrix[i][j][k] = 0.; 337 290 return true; … … 346 299 for(int i=MatrixCounter+1;i--;) 347 300 for(int j=RowCounter[i]+1;j--;) 348 for(int k=ColumnCounter [i];k--;)301 for(int k=ColumnCounter;k--;) 349 302 if (fabs(Matrix[i][j][k]) > max) 350 303 max = fabs(Matrix[i][j][k]); … … 362 315 for(int i=MatrixCounter+1;i--;) 363 316 for(int j=RowCounter[i]+1;j--;) 364 for(int k=ColumnCounter [i];k--;)317 for(int k=ColumnCounter;k--;) 365 318 if (fabs(Matrix[i][j][k]) < min) 366 319 min = fabs(Matrix[i][j][k]); … … 378 331 { 379 332 for(int j=RowCounter[MatrixCounter]+1;j--;) 380 for(int k=skipcolumns;k<ColumnCounter [MatrixCounter];k++)333 for(int k=skipcolumns;k<ColumnCounter;k++) 381 334 Matrix[MatrixCounter][j][k] = value; 382 335 return true; … … 391 344 { 392 345 for(int j=RowCounter[MatrixCounter]+1;j--;) 393 for(int k=skipcolumns;k<ColumnCounter [MatrixCounter];k++)346 for(int k=skipcolumns;k<ColumnCounter;k++) 394 347 Matrix[MatrixCounter][j][k] = values[j][k]; 395 348 return true; 396 349 }; 397 350 398 /** Sums the en tries with each factor and put into last element of \a ***Matrix.351 /** Sums the energy with each factor and put into last element of \a ***Energies. 399 352 * Sums over "E"-terms to create the "F"-terms 400 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.353 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. 401 354 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 402 355 * \param Order bond order … … 431 384 } 432 385 if (Order == SubOrder) { // equal order is always copy from Energies 433 for(int l=ColumnCounter [ KeySet.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column386 for(int l=ColumnCounter;l--;) // then adds/subtract each column 434 387 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 435 388 } else { 436 for(int l=ColumnCounter [ KeySet.OrderSet[SubOrder][j] ];l--;)389 for(int l=ColumnCounter;l--;) 437 390 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 438 391 } 439 392 } 440 //if ((ColumnCounter [ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))393 //if ((ColumnCounter>1) && (RowCounter[0]-1 >= 1)) 441 394 //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl; 442 395 } … … 467 420 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 468 421 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix; 469 Free( &FragmentNumber);422 Free((void **)&FragmentNumber, "*FragmentNumber"); 470 423 output.open(line.str().c_str(), ios::out); 471 424 if (output == NULL) { … … 473 426 return false; 474 427 } 475 output << Header [i]<< endl;428 output << Header << endl; 476 429 for(int j=0;j<RowCounter[i];j++) { 477 for(int k=0;k<ColumnCounter [i];k++)430 for(int k=0;k<ColumnCounter;k++) 478 431 output << scientific << Matrix[i][j][k] << "\t"; 479 432 output << endl; … … 502 455 return false; 503 456 } 504 output << Header [MatrixCounter]<< endl;457 output << Header << endl; 505 458 for(int j=0;j<RowCounter[MatrixCounter];j++) { 506 for(int k=0;k<ColumnCounter [MatrixCounter];k++)459 for(int k=0;k<ColumnCounter;k++) 507 460 output << scientific << Matrix[MatrixCounter][j][k] << "\t"; 508 461 output << endl; … … 521 474 { 522 475 cout << "Parsing energy indices." << endl; 523 Indices = Malloc<int*>(MatrixCounter + 1, "EnergyMatrix::ParseIndices: **Indices");476 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices"); 524 477 for(int i=MatrixCounter+1;i--;) { 525 Indices[i] = Malloc<int>(RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");478 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]"); 526 479 for(int j=RowCounter[i];j--;) 527 480 Indices[i][j] = j; … … 532 485 /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix. 533 486 * Sums over the "F"-terms in ANOVA decomposition. 534 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.487 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. 535 488 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments 536 489 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order … … 545 498 for(int i=KeySet.FragmentsPerOrder[Order];i--;) 546 499 for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;) 547 for(int k=ColumnCounter [ KeySet.OrderSet[Order][i] ];k--;)500 for(int k=ColumnCounter;k--;) 548 501 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k]; 549 502 else 550 503 for(int i=KeySet.FragmentsPerOrder[Order];i--;) 551 504 for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;) 552 for(int k=ColumnCounter [ KeySet.OrderSet[Order][i] ];k--;)505 for(int k=ColumnCounter;k--;) 553 506 Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySet.OrderSet[Order][i] ][j][k]); 554 507 return true; … … 571 524 // count maximum of columns 572 525 RowCounter[MatrixCounter] = 0; 573 ColumnCounter[MatrixCounter] = 0; 574 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows) 526 for(int j=0; j < MatrixCounter;j++) // (energy matrix might be bigger than number of atoms in terms of rows) 575 527 if (RowCounter[j] > RowCounter[MatrixCounter]) 576 528 RowCounter[MatrixCounter] = RowCounter[j]; 577 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix578 ColumnCounter[MatrixCounter] = ColumnCounter[j];579 }580 529 // allocate last plus one matrix 581 cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter [MatrixCounter]<< " columns." << endl;582 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]");530 cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl; 531 Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 583 532 for(int j=0;j<=RowCounter[MatrixCounter];j++) 584 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");585 533 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 534 586 535 // try independently to parse global energysuffix file if present 587 536 strncpy(filename, name, 1023); … … 607 556 608 557 cout << "Parsing force indices for " << MatrixCounter << " matrices." << endl; 609 Indices = Malloc<int*>(MatrixCounter + 1, "ForceMatrix::ParseIndices: **Indices");558 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "ForceMatrix::ParseIndices: **Indices"); 610 559 line << name << FRAGMENTPREFIX << FORCESFILE; 611 560 input.open(line.str().c_str(), ios::in); … … 620 569 line.str(filename); 621 570 // parse the values 622 Indices[i] = Malloc<int>(RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]");571 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]"); 623 572 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 624 573 //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:"; 625 Free( &FragmentNumber);574 Free((void **)&FragmentNumber, "ForceMatrix::ParseIndices: *FragmentNumber"); 626 575 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) { 627 576 line >> Indices[i][j]; … … 630 579 //cout << endl; 631 580 } 632 Indices[MatrixCounter] = Malloc<int>(RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]");581 Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]"); 633 582 for(int j=RowCounter[MatrixCounter];j--;) { 634 583 Indices[MatrixCounter][j] = j; … … 640 589 641 590 /** Sums the forces and puts into last element of \a ForceMatrix::Matrix. 642 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.591 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. 643 592 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 644 593 * \param Order bond order … … 660 609 if (j != -1) { 661 610 //if (j == 0) cout << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl; 662 for(int k=2;k<ColumnCounter [MatrixCounter];k++)611 for(int k=2;k<ColumnCounter;k++) 663 612 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k]; 664 613 } … … 707 656 input.close(); 708 657 709 ColumnCounter[MatrixCounter] = 0;710 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)711 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix712 ColumnCounter[MatrixCounter] = ColumnCounter[j];713 }714 715 658 // allocate last plus one matrix 716 cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter [MatrixCounter]<< " columns." << endl;717 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]");659 cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl; 660 Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 718 661 for(int j=0;j<=RowCounter[MatrixCounter];j++) 719 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 720 721 // try independently to parse global forcesuffix file if present 722 strncpy(filename, name, 1023); 723 strncat(filename, prefix, 1023-strlen(filename)); 724 strncat(filename, suffix.c_str(), 1023-strlen(filename)); 725 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter); 726 } 727 728 729 return status; 730 }; 731 732 // ======================================= CLASS HessianMatrix ============================= 733 734 /** Parsing force Indices of each fragment 735 * \param *name directory with \a ForcesFile 736 * \return parsing successful 737 */ 738 bool HessianMatrix::ParseIndices(char *name) 739 { 740 ifstream input; 741 char *FragmentNumber = NULL; 742 char filename[1023]; 743 stringstream line; 744 745 cout << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl; 746 Indices = Malloc<int*>(MatrixCounter + 1, "HessianMatrix::ParseIndices: **Indices"); 747 line << name << FRAGMENTPREFIX << FORCESFILE; 748 input.open(line.str().c_str(), ios::in); 749 //cout << "Opening " << line.str() << " ... " << input << endl; 750 if (input == NULL) { 751 cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl; 752 return false; 753 } 754 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) { 755 // get the number of atoms for this fragment 756 input.getline(filename, 1023); 757 line.str(filename); 758 // parse the values 759 Indices[i] = Malloc<int>(RowCounter[i], "HessianMatrix::ParseIndices: *Indices[]"); 760 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 761 //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:"; 762 Free(&FragmentNumber); 763 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) { 764 line >> Indices[i][j]; 765 //cout << " " << Indices[i][j]; 766 } 767 //cout << endl; 768 } 769 Indices[MatrixCounter] = Malloc<int>(RowCounter[MatrixCounter], "HessianMatrix::ParseIndices: *Indices[]"); 770 for(int j=RowCounter[MatrixCounter];j--;) { 771 Indices[MatrixCounter][j] = j; 772 } 773 input.close(); 774 return true; 775 }; 776 777 778 /** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix. 779 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. 780 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 781 * \param Order bond order 782 * \param sign +1 or -1 783 * \return true if summing was successful 784 */ 785 bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySet, int Order, double sign) 786 { 787 int FragmentNr; 788 // sum forces 789 for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) { 790 FragmentNr = KeySet.OrderSet[Order][i]; 791 for(int l=0;l<RowCounter[ FragmentNr ];l++) { 792 int j = Indices[ FragmentNr ][l]; 793 if (j > RowCounter[MatrixCounter]) { 794 cerr << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl; 795 return false; 796 } 797 if (j != -1) { 798 for(int m=0;m<ColumnCounter[ FragmentNr ];m++) { 799 int k = Indices[ FragmentNr ][m]; 800 if (k > ColumnCounter[MatrixCounter]) { 801 cerr << "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl; 802 return false; 803 } 804 if (k != -1) { 805 //cout << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl; 806 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m]; 807 } 808 } 809 } 810 } 811 } 812 return true; 813 }; 814 815 /** Constructor for class HessianMatrix. 816 */ 817 HessianMatrix::HessianMatrix() : MatrixContainer() 818 { 819 IsSymmetric = true; 820 } 821 822 /** Sums the hessian entries with each factor and put into last element of \a ***Matrix. 823 * Sums over "E"-terms to create the "F"-terms 824 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. 825 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 826 * \param Order bond order 827 * \return true if summing was successful 828 */ 829 bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order) 830 { 831 // go through each order 832 for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) { 833 //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl; 834 // then go per order through each suborder and pick together all the terms that contain this fragment 835 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order 836 for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder 837 if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) { 838 //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl; 839 // if the fragment's indices are all in the current fragment 840 for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment 841 int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k]; 842 //cout << "Current row index is " << k << "/" << m << "." << endl; 843 if (m != -1) { // if it's not an added hydrogen 844 for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment 845 //cout << "Comparing " << m << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl; 846 if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) { 847 m = l; 848 break; 849 } 850 } 851 //cout << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl; 852 if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) { 853 cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl; 854 return false; 855 } 856 857 for(int l=0;l<ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l++) { 858 int n = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][l]; 859 //cout << "Current column index is " << l << "/" << n << "." << endl; 860 if (n != -1) { // if it's not an added hydrogen 861 for (int p=0;p<ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment 862 //cout << "Comparing " << n << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p] << "." << endl; 863 if (n == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p]) { 864 n = p; 865 break; 866 } 867 } 868 //cout << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl; 869 if (n > ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) { 870 cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl; 871 return false; 872 } 873 if (Order == SubOrder) { // equal order is always copy from Energies 874 //cout << "Adding " << MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl; 875 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 876 } else { 877 //cout << "Subtracting " << Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl; 878 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 879 } 880 } 881 } 882 } 883 //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1)) 884 //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl; 885 } 886 } else { 887 //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet.OrderSet[Order][CurrentFragment] << "." << endl; 888 } 889 } 890 } 891 //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; 892 } 893 894 return true; 895 }; 896 897 /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix. 898 * \param *name directory with files 899 * \param *prefix prefix of each matrix file 900 * \param *suffix suffix of each matrix file 901 * \param skiplines number of inital lines to skip 902 * \param skiplines number of inital columns to skip 903 * \return parsing successful 904 */ 905 bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns) 906 { 907 char filename[1023]; 908 ifstream input; 909 stringstream file; 910 int nr; 911 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns); 912 913 if (status) { 914 // count number of atoms for last plus one matrix 915 file << name << FRAGMENTPREFIX << KEYSETFILE; 916 input.open(file.str().c_str(), ios::in); 917 if (input == NULL) { 918 cout << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl; 919 return false; 920 } 921 RowCounter[MatrixCounter] = 0; 922 ColumnCounter[MatrixCounter] = 0; 923 while (!input.eof()) { 924 input.getline(filename, 1023); 925 stringstream zeile(filename); 926 while (!zeile.eof()) { 927 zeile >> nr; 928 //cout << "Current index: " << nr << "." << endl; 929 if (nr > RowCounter[MatrixCounter]) { 930 RowCounter[MatrixCounter] = nr; 931 ColumnCounter[MatrixCounter] = nr; 932 } 933 } 934 } 935 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1 936 ColumnCounter[MatrixCounter]++; // nr start at 0, count starts at 1 937 input.close(); 938 939 // allocate last plus one matrix 940 cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl; 941 Matrix[MatrixCounter] = Malloc<double*>(RowCounter[MatrixCounter] + 1, "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 942 for(int j=0;j<=RowCounter[MatrixCounter];j++) 943 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 662 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 944 663 945 664 // try independently to parse global forcesuffix file if present … … 971 690 KeySetsContainer::~KeySetsContainer() { 972 691 for(int i=FragmentCounter;i--;) 973 Free( &KeySets[i]);692 Free((void **)&KeySets[i], "KeySetsContainer::~KeySetsContainer: *KeySets[]"); 974 693 for(int i=Order;i--;) 975 Free( &OrderSet[i]);976 Free( &KeySets);977 Free( &OrderSet);978 Free( &AtomCounter);979 Free( &FragmentsPerOrder);694 Free((void **)&OrderSet[i], "KeySetsContainer::~KeySetsContainer: *OrderSet[]"); 695 Free((void **)&KeySets, "KeySetsContainer::~KeySetsContainer: **KeySets"); 696 Free((void **)&OrderSet, "KeySetsContainer::~KeySetsContainer: **OrderSet"); 697 Free((void **)&AtomCounter, "KeySetsContainer::~KeySetsContainer: *AtomCounter"); 698 Free((void **)&FragmentsPerOrder, "KeySetsContainer::~KeySetsContainer: *FragmentsPerOrder"); 980 699 }; 981 700 … … 994 713 FragmentCounter = FCounter; 995 714 cout << "Parsing key sets." << endl; 996 KeySets = Malloc<int*>(FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets");715 KeySets = (int **) Malloc(sizeof(int *)*FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets"); 997 716 for(int i=FragmentCounter;i--;) 998 717 KeySets[i] = NULL; … … 1004 723 } 1005 724 1006 AtomCounter = Malloc<int>(FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter");725 AtomCounter = (int *) Malloc(sizeof(int)*FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter"); 1007 726 for(int i=0;(i<FragmentCounter) && (!input.eof());i++) { 1008 727 stringstream line; 1009 728 AtomCounter[i] = ACounter[i]; 1010 729 // parse the values 1011 KeySets[i] = Malloc<int>(AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]");730 KeySets[i] = (int *) Malloc(sizeof(int)*AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]"); 1012 731 for(int j=AtomCounter[i];j--;) 1013 732 KeySets[i][j] = -1; 1014 733 FragmentNumber = FixedDigitNumber(FragmentCounter, i); 1015 734 //cout << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:"; 1016 Free( &FragmentNumber);735 Free((void **)&FragmentNumber, "KeySetsContainer::ParseKeySets: *FragmentNumber"); 1017 736 input.getline(filename, 1023); 1018 737 line.str(filename); … … 1048 767 1049 768 // scan through all to determine fragments per order 1050 FragmentsPerOrder = Malloc<int>(Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder");769 FragmentsPerOrder = (int *) Malloc(sizeof(int)*Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder"); 1051 770 for(int i=Order;i--;) 1052 771 FragmentsPerOrder[i] = 0; … … 1062 781 1063 782 // scan through all to gather indices to each order set 1064 OrderSet = Malloc<int*>(Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet");783 OrderSet = (int **) Malloc(sizeof(int *)*Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet"); 1065 784 for(int i=Order;i--;) 1066 OrderSet[i] = Malloc<int>(FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]");785 OrderSet[i] = (int *) Malloc(sizeof(int)*FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]"); 1067 786 for(int i=Order;i--;) 1068 787 FragmentsPerOrder[i] = 0;
Note:
See TracChangeset
for help on using the changeset viewer.