Changes in src/parser.cpp [042f82:29812d]
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/parser.cpp
r042f82 r29812d 8 8 9 9 #include "helpers.hpp" 10 #include "memoryallocator.hpp" 10 11 #include "parser.hpp" 11 12 … … 56 57 MatrixContainer::MatrixContainer() { 57 58 Indices = 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"); 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; 61 64 Matrix[0] = NULL; 62 65 RowCounter[0] = -1; 63 66 MatrixCounter = 0; 64 ColumnCounter = 0;67 ColumnCounter[0] = -1; 65 68 }; 66 69 … … 70 73 if (Matrix != NULL) { 71 74 for(int i=MatrixCounter;i--;) { 72 if ( RowCounter != NULL) {75 if ((ColumnCounter != NULL) && (RowCounter != NULL)) { 73 76 for(int j=RowCounter[i]+1;j--;) 74 Free( (void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]");75 Free( (void **)&Matrix[i], "MatrixContainer::~MatrixContainer: **Matrix[]");77 Free(&Matrix[i][j]); 78 Free(&Matrix[i]); 76 79 } 77 80 } 78 if (( RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))81 if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL)) 79 82 for(int j=RowCounter[MatrixCounter]+1;j--;) 80 Free( (void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]");83 Free(&Matrix[MatrixCounter][j]); 81 84 if (MatrixCounter != 0) 82 Free( (void **)&Matrix[MatrixCounter], "MatrixContainer::~MatrixContainer: **Matrix[MatrixCounter]");83 Free( (void **)&Matrix, "MatrixContainer::~MatrixContainer: ***Matrix");85 Free(&Matrix[MatrixCounter]); 86 Free(&Matrix); 84 87 } 85 88 if (Indices != NULL) 86 89 for(int i=MatrixCounter+1;i--;) { 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 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 }; 95 136 96 137 /** Parsing a number of matrices. … … 121 162 } 122 163 123 // skip some initial lines 164 // parse header 165 Header[MatrixNr] = Malloc<char>(1024, "MatrixContainer::ParseMatrix: *Header[]"); 124 166 for (int m=skiplines+1;m--;) 125 input.getline(Header , 1023);126 167 input.getline(Header[MatrixNr], 1023); 168 127 169 // scan header for number of columns 128 line.str(Header );170 line.str(Header[MatrixNr]); 129 171 for(int k=skipcolumns;k--;) 130 line >> Header ;172 line >> Header[MatrixNr]; 131 173 //cout << line.str() << endl; 132 ColumnCounter =0;174 ColumnCounter[MatrixNr]=0; 133 175 while ( getline(line,token, '\t') ) { 134 176 if (token.length() > 0) 135 ColumnCounter ++;177 ColumnCounter[MatrixNr]++; 136 178 } 137 179 //cout << line.str() << endl; 138 //cout << "ColumnCounter : " << ColumnCounter<< "." << endl;139 if (ColumnCounter == 0)140 cerr << "ColumnCounter : " << ColumnCounter<< " from file " << name << ", this is probably an error!" << endl;141 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 142 184 // scan rest for number of rows/lines 143 185 RowCounter[MatrixNr]=-1; // counts one line too much … … 155 197 156 198 // allocate matrix if it's not zero dimension in one direction 157 if ((ColumnCounter > 0) && (RowCounter[MatrixNr] > -1)) {158 Matrix[MatrixNr] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixNr]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");159 199 if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) { 200 Matrix[MatrixNr] = Malloc<double*>(RowCounter[MatrixNr] + 1, "MatrixContainer::ParseMatrix: **Matrix[]"); 201 160 202 // parse in each entry for this matrix 161 203 input.clear(); 162 204 input.seekg(ios::beg); 163 205 for (int m=skiplines+1;m--;) 164 input.getline(Header , 1023); // skip header165 line.str(Header );206 input.getline(Header[MatrixNr], 1023); // skip header 207 line.str(Header[MatrixNr]); 166 208 for(int k=skipcolumns;k--;) // skip columns in header too 167 209 line >> filename; 168 strncpy(Header , line.str().c_str(), 1023);210 strncpy(Header[MatrixNr], line.str().c_str(), 1023); 169 211 for(int j=0;j<RowCounter[MatrixNr];j++) { 170 Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");212 Matrix[MatrixNr][j] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[][]"); 171 213 input.getline(filename, 1023); 172 214 stringstream lines(filename); … … 174 216 for(int k=skipcolumns;k--;) 175 217 lines >> filename; 176 for(int k=0;(k<ColumnCounter ) && (!lines.eof());k++) {218 for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) { 177 219 lines >> Matrix[MatrixNr][j][k]; 178 220 //cout << " " << setprecision(2) << Matrix[MatrixNr][j][k];; 179 221 } 180 222 //cout << endl; 181 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[RowCounter[MatrixNr]][]");182 for(int j=ColumnCounter ;j--;)223 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = Malloc<double>(ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[RowCounter[MatrixNr]][]"); 224 for(int j=ColumnCounter[MatrixNr];j--;) 183 225 Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.; 184 226 } 185 227 } else { 186 cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl;228 cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl; 187 229 } 188 230 input.close(); … … 233 275 234 276 cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl; 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"); 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"); 237 281 for(int i=MatrixCounter+1;i--;) { 238 282 Matrix[i] = NULL; 283 Header[i] = NULL; 239 284 RowCounter[i] = -1; 285 ColumnCounter[i] = -1; 240 286 } 241 287 for(int i=0; i < MatrixCounter;i++) { … … 246 292 if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i)) 247 293 return false; 248 Free( (void **)&FragmentNumber, "MatrixContainer::ParseFragmentMatrix: *FragmentNumber");294 Free(&FragmentNumber); 249 295 } 250 296 return true; … … 252 298 253 299 /** Allocates and resets the memory for a number \a MCounter of matrices. 254 * \param * GivenHeader Header line300 * \param **GivenHeader Header line for each matrix 255 301 * \param MCounter number of matrices 256 302 * \param *RCounter number of rows for each matrix 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 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 { 265 308 MatrixCounter = MCounter; 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"); 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"); 269 313 for(int i=MatrixCounter+1;i--;) { 314 Header[i] = Malloc<char>(1024, "MatrixContainer::AllocateMatrix: *Header[i]"); 315 strncpy(Header[i], GivenHeader[i], 1023); 270 316 RowCounter[i] = RCounter[i]; 271 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 317 ColumnCounter[i] = CCounter[i]; 318 Matrix[i] = Malloc<double*>(RowCounter[i] + 1, "MatrixContainer::AllocateMatrix: **Matrix[]"); 272 319 for(int j=RowCounter[i]+1;j--;) { 273 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");274 for(int k=ColumnCounter ;k--;)320 Matrix[i][j] = Malloc<double>(ColumnCounter[i], "MatrixContainer::AllocateMatrix: *Matrix[][]"); 321 for(int k=ColumnCounter[i];k--;) 275 322 Matrix[i][j][k] = 0.; 276 323 } … … 286 333 for(int i=MatrixCounter+1;i--;) 287 334 for(int j=RowCounter[i]+1;j--;) 288 for(int k=ColumnCounter ;k--;)335 for(int k=ColumnCounter[i];k--;) 289 336 Matrix[i][j][k] = 0.; 290 337 return true; … … 299 346 for(int i=MatrixCounter+1;i--;) 300 347 for(int j=RowCounter[i]+1;j--;) 301 for(int k=ColumnCounter ;k--;)348 for(int k=ColumnCounter[i];k--;) 302 349 if (fabs(Matrix[i][j][k]) > max) 303 350 max = fabs(Matrix[i][j][k]); … … 315 362 for(int i=MatrixCounter+1;i--;) 316 363 for(int j=RowCounter[i]+1;j--;) 317 for(int k=ColumnCounter ;k--;)364 for(int k=ColumnCounter[i];k--;) 318 365 if (fabs(Matrix[i][j][k]) < min) 319 366 min = fabs(Matrix[i][j][k]); … … 331 378 { 332 379 for(int j=RowCounter[MatrixCounter]+1;j--;) 333 for(int k=skipcolumns;k<ColumnCounter ;k++)380 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) 334 381 Matrix[MatrixCounter][j][k] = value; 335 382 return true; … … 344 391 { 345 392 for(int j=RowCounter[MatrixCounter]+1;j--;) 346 for(int k=skipcolumns;k<ColumnCounter ;k++)393 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++) 347 394 Matrix[MatrixCounter][j][k] = values[j][k]; 348 395 return true; 349 396 }; 350 397 351 /** Sums the en ergy with each factor and put into last element of \a ***Energies.398 /** Sums the entries with each factor and put into last element of \a ***Matrix. 352 399 * Sums over "E"-terms to create the "F"-terms 353 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.400 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. 354 401 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 355 402 * \param Order bond order … … 384 431 } 385 432 if (Order == SubOrder) { // equal order is always copy from Energies 386 for(int l=ColumnCounter ;l--;) // then adds/subtract each column433 for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column 387 434 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 388 435 } else { 389 for(int l=ColumnCounter ;l--;)436 for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;) 390 437 Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; 391 438 } 392 439 } 393 //if ((ColumnCounter >1) && (RowCounter[0]-1 >= 1))440 //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1)) 394 441 //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl; 395 442 } … … 420 467 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 421 468 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix; 422 Free( (void **)&FragmentNumber, "*FragmentNumber");469 Free(&FragmentNumber); 423 470 output.open(line.str().c_str(), ios::out); 424 471 if (output == NULL) { … … 426 473 return false; 427 474 } 428 output << Header << endl;475 output << Header[i] << endl; 429 476 for(int j=0;j<RowCounter[i];j++) { 430 for(int k=0;k<ColumnCounter ;k++)477 for(int k=0;k<ColumnCounter[i];k++) 431 478 output << scientific << Matrix[i][j][k] << "\t"; 432 479 output << endl; … … 455 502 return false; 456 503 } 457 output << Header << endl;504 output << Header[MatrixCounter] << endl; 458 505 for(int j=0;j<RowCounter[MatrixCounter];j++) { 459 for(int k=0;k<ColumnCounter ;k++)506 for(int k=0;k<ColumnCounter[MatrixCounter];k++) 460 507 output << scientific << Matrix[MatrixCounter][j][k] << "\t"; 461 508 output << endl; … … 474 521 { 475 522 cout << "Parsing energy indices." << endl; 476 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices");523 Indices = Malloc<int*>(MatrixCounter + 1, "EnergyMatrix::ParseIndices: **Indices"); 477 524 for(int i=MatrixCounter+1;i--;) { 478 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");525 Indices[i] = Malloc<int>(RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]"); 479 526 for(int j=RowCounter[i];j--;) 480 527 Indices[i][j] = j; … … 485 532 /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix. 486 533 * Sums over the "F"-terms in ANOVA decomposition. 487 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.534 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. 488 535 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments 489 536 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order … … 498 545 for(int i=KeySet.FragmentsPerOrder[Order];i--;) 499 546 for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;) 500 for(int k=ColumnCounter ;k--;)547 for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;) 501 548 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k]; 502 549 else 503 550 for(int i=KeySet.FragmentsPerOrder[Order];i--;) 504 551 for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;) 505 for(int k=ColumnCounter ;k--;)552 for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;) 506 553 Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySet.OrderSet[Order][i] ][j][k]); 507 554 return true; … … 524 571 // count maximum of columns 525 572 RowCounter[MatrixCounter] = 0; 526 for(int j=0; j < MatrixCounter;j++) // (energy matrix might be bigger than number of atoms in terms of rows) 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) 527 575 if (RowCounter[j] > RowCounter[MatrixCounter]) 528 576 RowCounter[MatrixCounter] = RowCounter[j]; 577 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix 578 ColumnCounter[MatrixCounter] = ColumnCounter[j]; 579 } 529 580 // allocate last plus one 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[]");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[]"); 532 583 for(int j=0;j<=RowCounter[MatrixCounter];j++) 533 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");534 584 Matrix[MatrixCounter][j] = Malloc<double>(ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 585 535 586 // try independently to parse global energysuffix file if present 536 587 strncpy(filename, name, 1023); … … 556 607 557 608 cout << "Parsing force indices for " << MatrixCounter << " matrices." << endl; 558 Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "ForceMatrix::ParseIndices: **Indices");609 Indices = Malloc<int*>(MatrixCounter + 1, "ForceMatrix::ParseIndices: **Indices"); 559 610 line << name << FRAGMENTPREFIX << FORCESFILE; 560 611 input.open(line.str().c_str(), ios::in); … … 569 620 line.str(filename); 570 621 // parse the values 571 Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]");622 Indices[i] = Malloc<int>(RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]"); 572 623 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 573 624 //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:"; 574 Free( (void **)&FragmentNumber, "ForceMatrix::ParseIndices: *FragmentNumber");625 Free(&FragmentNumber); 575 626 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) { 576 627 line >> Indices[i][j]; … … 579 630 //cout << endl; 580 631 } 581 Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]");632 Indices[MatrixCounter] = Malloc<int>(RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]"); 582 633 for(int j=RowCounter[MatrixCounter];j--;) { 583 634 Indices[MatrixCounter][j] = j; … … 589 640 590 641 /** Sums the forces and puts into last element of \a ForceMatrix::Matrix. 591 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.642 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies. 592 643 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order 593 644 * \param Order bond order … … 609 660 if (j != -1) { 610 661 //if (j == 0) cout << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl; 611 for(int k=2;k<ColumnCounter ;k++)662 for(int k=2;k<ColumnCounter[MatrixCounter];k++) 612 663 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k]; 613 664 } … … 656 707 input.close(); 657 708 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 matrix 712 ColumnCounter[MatrixCounter] = ColumnCounter[j]; 713 } 714 658 715 // allocate last plus one 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[]");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[]"); 661 718 for(int j=0;j<=RowCounter[MatrixCounter];j++) 662 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 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[][]"); 663 944 664 945 // try independently to parse global forcesuffix file if present … … 690 971 KeySetsContainer::~KeySetsContainer() { 691 972 for(int i=FragmentCounter;i--;) 692 Free( (void **)&KeySets[i], "KeySetsContainer::~KeySetsContainer: *KeySets[]");973 Free(&KeySets[i]); 693 974 for(int i=Order;i--;) 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");975 Free(&OrderSet[i]); 976 Free(&KeySets); 977 Free(&OrderSet); 978 Free(&AtomCounter); 979 Free(&FragmentsPerOrder); 699 980 }; 700 981 … … 713 994 FragmentCounter = FCounter; 714 995 cout << "Parsing key sets." << endl; 715 KeySets = (int **) Malloc(sizeof(int *)*FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets");996 KeySets = Malloc<int*>(FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets"); 716 997 for(int i=FragmentCounter;i--;) 717 998 KeySets[i] = NULL; … … 723 1004 } 724 1005 725 AtomCounter = (int *) Malloc(sizeof(int)*FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter");1006 AtomCounter = Malloc<int>(FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter"); 726 1007 for(int i=0;(i<FragmentCounter) && (!input.eof());i++) { 727 1008 stringstream line; 728 1009 AtomCounter[i] = ACounter[i]; 729 1010 // parse the values 730 KeySets[i] = (int *) Malloc(sizeof(int)*AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]");1011 KeySets[i] = Malloc<int>(AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]"); 731 1012 for(int j=AtomCounter[i];j--;) 732 1013 KeySets[i][j] = -1; 733 1014 FragmentNumber = FixedDigitNumber(FragmentCounter, i); 734 1015 //cout << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:"; 735 Free( (void **)&FragmentNumber, "KeySetsContainer::ParseKeySets: *FragmentNumber");1016 Free(&FragmentNumber); 736 1017 input.getline(filename, 1023); 737 1018 line.str(filename); … … 767 1048 768 1049 // scan through all to determine fragments per order 769 FragmentsPerOrder = (int *) Malloc(sizeof(int)*Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder");1050 FragmentsPerOrder = Malloc<int>(Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder"); 770 1051 for(int i=Order;i--;) 771 1052 FragmentsPerOrder[i] = 0; … … 781 1062 782 1063 // scan through all to gather indices to each order set 783 OrderSet = (int **) Malloc(sizeof(int *)*Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet");1064 OrderSet = Malloc<int*>(Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet"); 784 1065 for(int i=Order;i--;) 785 OrderSet[i] = (int *) Malloc(sizeof(int)*FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]");1066 OrderSet[i] = Malloc<int>(FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]"); 786 1067 for(int i=Order;i--;) 787 1068 FragmentsPerOrder[i] = 0;
Note:
See TracChangeset
for help on using the changeset viewer.