Changeset 7b67a3 for molecuilder/src/parser.cpp
- Timestamp:
- Aug 18, 2008, 8:35:11 AM (17 years ago)
- Children:
- e6971b
- Parents:
- d24e8f0
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
molecuilder/src/parser.cpp
rd24e8f0 r7b67a3 55 55 */ 56 56 MatrixContainer::MatrixContainer() { 57 Matrix = NULL;58 57 Indices = NULL; 59 Header = 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"); 61 Matrix[0] = NULL; 62 RowCounter[0] = -1; 60 63 MatrixCounter = 0; 61 RowCounter = NULL;62 64 ColumnCounter = 0; 63 65 }; … … 89 91 Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header"); 90 92 Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter"); 93 }; 94 95 96 /** Parsing a number of matrices. 97 * -# open the matrix file 98 * -# skip some lines (\a skiplines) 99 * -# scan header lines for number of columns 100 * -# scan lines for number of rows 101 * -# allocate matrix 102 * -# loop over found column and row counts and parse in each entry 103 * \param *name directory with files 104 * \param skiplines number of inital lines to skip 105 * \param skiplines number of inital columns to skip 106 * \param MatrixNr index number in Matrix array to parse into 107 * \return parsing successful 108 */ 109 bool MatrixContainer::ParseMatrix(const char *name, int skiplines, int skipcolumns, int MatrixNr) 110 { 111 ifstream input; 112 stringstream line; 113 string token; 114 char filename[1023]; 115 116 input.open(name, ios::in); 117 //cout << "Opening " << name << " ... " << input << endl; 118 if (input == NULL) { 119 cerr << endl << "Unable to open " << name << ", is the directory correct?" << endl; 120 return false; 121 } 122 123 // skip some initial lines 124 for (int m=skiplines+1;m--;) 125 input.getline(Header, 1023); 126 127 // scan header for number of columns 128 line.str(Header); 129 for(int k=skipcolumns;k--;) 130 line >> Header; 131 //cout << line.str() << endl; 132 ColumnCounter=0; 133 while ( getline(line,token, '\t') ) { 134 if (token.length() > 0) 135 ColumnCounter++; 136 } 137 //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 142 // scan rest for number of rows/lines 143 RowCounter[MatrixNr]=-1; // counts one line too much 144 while (!input.eof()) { 145 input.getline(filename, 1023); 146 //cout << "Comparing: " << strncmp(filename,"MeanForce",9) << endl; 147 RowCounter[MatrixNr]++; // then line was not last MeanForce 148 if (strncmp(filename,"MeanForce", 9) == 0) { 149 break; 150 } 151 } 152 //cout << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << "." << endl; 153 if (RowCounter[MatrixNr] == 0) 154 cerr << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl; 155 156 // 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 160 // parse in each entry for this matrix 161 input.clear(); 162 input.seekg(ios::beg); 163 for (int m=skiplines+1;m--;) 164 input.getline(Header, 1023); // skip header 165 line.str(Header); 166 for(int k=skipcolumns;k--;) // skip columns in header too 167 line >> filename; 168 strncpy(Header, line.str().c_str(), 1023); 169 for(int j=0;j<RowCounter[MatrixNr];j++) { 170 Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 171 input.getline(filename, 1023); 172 stringstream lines(filename); 173 //cout << "Matrix at level " << j << ":";// << filename << endl; 174 for(int k=skipcolumns;k--;) 175 lines >> filename; 176 for(int k=0;(k<ColumnCounter) && (!lines.eof());k++) { 177 lines >> Matrix[MatrixNr][j][k]; 178 //cout << " " << setprecision(2) << Matrix[MatrixNr][j][k];; 179 } 180 //cout << endl; 181 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[RowCounter[MatrixNr]][]"); 182 for(int j=ColumnCounter;j--;) 183 Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.; 184 } 185 } else { 186 cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl; 187 } 188 input.close(); 189 return true; 91 190 }; 92 191 … … 109 208 * \return parsing successful 110 209 */ 111 bool MatrixContainer::Parse Matrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns)210 bool MatrixContainer::ParseFragmentMatrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns) 112 211 { 113 212 char filename[1023]; … … 117 216 string token; 118 217 119 Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::ParseMatrix: *EnergyHeader");120 121 218 // count the number of matrices 122 219 MatrixCounter = -1; // we count one too much … … 129 226 while (!input.eof()) { 130 227 input.getline(filename, 1023); 228 stringstream zeile(filename); 131 229 MatrixCounter++; 132 230 } 133 input.close(); 231 input.close(); 134 232 cout << "Determined " << MatrixCounter << " fragments." << endl; 135 233 136 234 cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl; 137 Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: ***Matrix"); // one more each for the total molecule138 RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: *RowCounter");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"); 139 237 for(int i=MatrixCounter+1;i--;) { 140 238 Matrix[i] = NULL; 141 239 RowCounter[i] = -1; 142 240 } 143 for(int i= MatrixCounter+1;i--;) {241 for(int i=0; i < MatrixCounter;i++) { 144 242 // open matrix file 145 stringstream line;146 243 FragmentNumber = FixedDigitNumber(MatrixCounter, i); 147 244 file.str(" "); 148 if (i != MatrixCounter) 149 file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix; 150 else 151 file << name << prefix << suffix; 152 Free((void **)&FragmentNumber, "MatrixContainer::ParseMatrix: *FragmentNumber"); 153 input.open(file.str().c_str(), ios::in); 154 //cout << "Opening " << file.str() << " ... " << input << endl; 155 if (input == NULL) { 156 cerr << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl; 245 file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix; 246 if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i)) 157 247 return false; 158 } 159 160 // skip some initial lines 161 for (int m=skiplines+1;m--;) 162 input.getline(Header, 1023); 163 164 // scan header for number of columns 165 line.str(Header); 166 for(int k=skipcolumns;k--;) 167 line >> Header; 168 //cout << line.str() << endl; 169 ColumnCounter=0; 170 while ( getline(line,token, '\t') ) { 171 if (token.length() > 0) 172 ColumnCounter++; 173 } 174 //cout << line.str() << endl; 175 //cout << "ColumnCounter: " << ColumnCounter << endl; 176 177 // scan rest for number of rows/lines 178 RowCounter[i]=-1; // counts one line too much 179 while (!input.eof()) { 180 input.getline(filename, 1023); 181 //cout << "Comparing: " << strncmp(filename,"MeanForce",9) << endl; 182 RowCounter[i]++; // then line was not last MeanForce 183 if (strncmp(filename,"MeanForce", 9) == 0) { 184 break; 185 } 186 } 187 //cout << "RowCounter[" << i << "]: " << RowCounter[i] << " from file " << file.str() << "." << endl; 188 189 // allocate matrix if it's not zero dimension in one direction 190 if ((ColumnCounter > 0) && (RowCounter[i] > -1)) { 191 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]"); 192 193 // parse in each entry for this matrix 194 input.clear(); 195 input.seekg(ios::beg); 196 for (int m=skiplines+1;m--;) 197 input.getline(Header, 1023); // skip header 198 line.str(Header); 199 for(int k=skipcolumns;k--;) // skip columns in header too 200 line >> filename; 201 strncpy(Header, line.str().c_str(), 1023); 202 for(int j=0;j<RowCounter[i];j++) { 203 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]"); 204 input.getline(filename, 1023); 205 stringstream lines(filename); 206 //cout << "Matrix at level " << j << ":";// << filename << endl; 207 for(int k=skipcolumns;k--;) 208 lines >> filename; 209 for(int k=0;(k<ColumnCounter) && (!lines.eof());k++) { 210 lines >> Matrix[i][j][k]; 211 //cout << " " << setprecision(2) << Matrix[i][j][k];; 212 } 213 //cout << endl; 214 Matrix[i][ RowCounter[i] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[RowCounter[i]][]"); 215 for(int j=ColumnCounter;j--;) 216 Matrix[i][ RowCounter[i] ][j] = 0.; 217 } 218 } else { 219 cerr << "ERROR: Matrix nr. " << i << " has column and row count of (" << ColumnCounter << "," << RowCounter[i] << "), could not allocate nor parse!" << endl; 220 } 221 input.close(); 248 Free((void **)&FragmentNumber, "MatrixContainer::ParseFragmentMatrix: *FragmentNumber"); 222 249 } 223 250 return true; … … 233 260 bool MatrixContainer::AllocateMatrix(char *GivenHeader, int MCounter, int *RCounter, int CCounter) 234 261 { 235 Header = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::Parse Matrix: *EnergyHeader");262 Header = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseFragmentMatrix: *EnergyHeader"); 236 263 strncpy(Header, GivenHeader, 1023); 237 264 238 265 MatrixCounter = MCounter; 239 266 ColumnCounter = CCounter; 240 Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::Parse Matrix: ***Matrix"); // one more each for the total molecule241 RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::Parse Matrix: *RowCounter");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"); 242 269 for(int i=MatrixCounter+1;i--;) { 243 270 RowCounter[i] = RCounter[i]; 244 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::Parse Matrix: **Matrix[]");271 Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 245 272 for(int j=RowCounter[i]+1;j--;) { 246 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::Parse Matrix: *Matrix[][]");273 Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 247 274 for(int k=ColumnCounter;k--;) 248 275 Matrix[i][j][k] = 0.; … … 481 508 }; 482 509 510 /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix. 511 * \param *name directory with files 512 * \param *prefix prefix of each matrix file 513 * \param *suffix suffix of each matrix file 514 * \param skiplines number of inital lines to skip 515 * \param skiplines number of inital columns to skip 516 * \return parsing successful 517 */ 518 bool EnergyMatrix::ParseFragmentMatrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns) 519 { 520 char filename[1024]; 521 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns); 522 523 if (status) { 524 // count maximum of columns 525 RowCounter[MatrixCounter] = 0; 526 for(int j=0; j < MatrixCounter;j++) // (energy matrix might be bigger than number of atoms in terms of rows) 527 if (RowCounter[j] > RowCounter[MatrixCounter]) 528 RowCounter[MatrixCounter] = RowCounter[j]; 529 // 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[]"); 532 for(int j=0;j<=RowCounter[MatrixCounter];j++) 533 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 534 535 // try independently to parse global energysuffix file if present 536 strncpy(filename, name, 1023); 537 strncat(filename, prefix, 1023-strlen(filename)); 538 strncat(filename, suffix.c_str(), 1023-strlen(filename)); 539 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter); 540 } 541 return status; 542 }; 543 483 544 // ======================================= CLASS ForceMatrix ============================= 484 545 … … 556 617 }; 557 618 619 620 /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix. 621 * \param *name directory with files 622 * \param *prefix prefix of each matrix file 623 * \param *suffix suffix of each matrix file 624 * \param skiplines number of inital lines to skip 625 * \param skiplines number of inital columns to skip 626 * \return parsing successful 627 */ 628 bool ForceMatrix::ParseFragmentMatrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns) 629 { 630 char filename[1023]; 631 ifstream input; 632 stringstream file; 633 int nr; 634 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns); 635 636 if (status) { 637 // count number of atoms for last plus one matrix 638 file << name << FRAGMENTPREFIX << KEYSETFILE; 639 input.open(file.str().c_str(), ios::in); 640 if (input == NULL) { 641 cout << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl; 642 return false; 643 } 644 RowCounter[MatrixCounter] = 0; 645 while (!input.eof()) { 646 input.getline(filename, 1023); 647 stringstream zeile(filename); 648 while (!zeile.eof()) { 649 zeile >> nr; 650 //cout << "Current index: " << nr << "." << endl; 651 if (nr > RowCounter[MatrixCounter]) 652 RowCounter[MatrixCounter] = nr; 653 } 654 } 655 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1 656 input.close(); 657 658 // 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[]"); 661 for(int j=0;j<=RowCounter[MatrixCounter];j++) 662 Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]"); 663 664 // try independently to parse global forcesuffix file if present 665 strncpy(filename, name, 1023); 666 strncat(filename, prefix, 1023-strlen(filename)); 667 strncat(filename, suffix.c_str(), 1023-strlen(filename)); 668 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter); 669 } 670 671 672 return status; 673 }; 558 674 559 675 // ======================================= CLASS KeySetsContainer =============================
Note:
See TracChangeset
for help on using the changeset viewer.