Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/parser.cpp

    • Property mode changed from 100644 to 100755
    r25f5089 r848729  
    5656MatrixContainer::MatrixContainer() {
    5757  Indices = NULL;
    58   Header = (char **) Malloc(sizeof(char)*1, "MatrixContainer::MatrixContainer: **Header");
     58  Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::MatrixContainer: *Header");
    5959  Matrix = (double ***) Malloc(sizeof(double **)*(1), "MatrixContainer::MatrixContainer: ***Matrix"); // one more each for the total molecule
    6060  RowCounter = (int *) Malloc(sizeof(int)*(1), "MatrixContainer::MatrixContainer: *RowCounter");
    61   ColumnCounter = (int *) Malloc(sizeof(int)*(1), "MatrixContainer::MatrixContainer: *ColumnCounter");
    62   Header[0] = NULL;
    6361  Matrix[0] = NULL;
    6462  RowCounter[0] = -1;
    6563  MatrixCounter = 0;
    66   ColumnCounter[0] = -1;
     64  ColumnCounter = 0;
    6765};
    6866
     
    7270  if (Matrix != NULL) {
    7371    for(int i=MatrixCounter;i--;) {
    74       if ((ColumnCounter != NULL) && (RowCounter != NULL)) {
     72      if (RowCounter != NULL) {
    7573          for(int j=RowCounter[i]+1;j--;)
    7674            Free((void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]");
     
    7876      }
    7977    }
    80     if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
     78    if ((RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
    8179      for(int j=RowCounter[MatrixCounter]+1;j--;)
    8280        Free((void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]");
     
    9189  Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices");
    9290 
    93   if (Header != NULL)
    94     for(int i=MatrixCounter+1;i--;)
    95       Free((void **)&Header[i], "MatrixContainer::~MatrixContainer: *Header[]");
    96   Free((void **)&Header, "MatrixContainer::~MatrixContainer: **Header");
     91  Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header");
    9792  Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter");
    98   Free((void **)&ColumnCounter, "MatrixContainer::~MatrixContainer: *RowCounter");
    99 };
    100 
    101 /** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL.
    102  * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments.
    103  * \param *Matrix pointer to other MatrixContainer
    104  * \return true - copy/initialisation sucessful, false - dimension false for copying
    105  */
    106 bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix)
    107 {
    108   cout << "Initialising indices";
    109   if (Matrix == NULL) {
    110     cout << " with trivial mapping." << endl;
    111     Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "MatrixContainer::InitialiseIndices: **Indices");
    112     for(int i=MatrixCounter+1;i--;) {
    113       Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "MatrixContainer::InitialiseIndices: *Indices[]");
    114       for(int j=RowCounter[i];j--;)
    115         Indices[i][j] = j;
    116     }
    117   } else {
    118     cout << " from other MatrixContainer." << endl;
    119     if (MatrixCounter != Matrix->MatrixCounter)
    120       return false;
    121     Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "MatrixContainer::InitialiseIndices: **Indices");
    122     for(int i=MatrixCounter+1;i--;) {
    123       if (RowCounter[i] != Matrix->RowCounter[i])
    124         return false;
    125       Indices[i] = (int *) Malloc(sizeof(int)*Matrix->RowCounter[i], "MatrixContainer::InitialiseIndices: *Indices[]");
    126       for(int j=Matrix->RowCounter[i];j--;) {
    127         Indices[i][j] = Matrix->Indices[i][j];
    128         //cout << Indices[i][j] << "\t";
    129       }
    130       //cout << endl;
    131     }
    132   }
    133   return true;
    134 };
     93};
     94
    13595
    13696/** Parsing a number of matrices.
     
    160120    return false;
    161121  }
    162 
    163   // parse header
    164   Header[MatrixNr] = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseMatrix: *Header[]");
     122 
     123  // skip some initial lines
    165124  for (int m=skiplines+1;m--;)
    166     input.getline(Header[MatrixNr], 1023);
     125    input.getline(Header, 1023);
    167126 
    168127  // scan header for number of columns
    169   line.str(Header[MatrixNr]);
     128  line.str(Header);
    170129  for(int k=skipcolumns;k--;)
    171     line >> Header[MatrixNr];
     130    line >> Header;
    172131  //cout << line.str() << endl;
    173   ColumnCounter[MatrixNr]=0;
     132  ColumnCounter=0;
    174133  while ( getline(line,token, '\t') ) {
    175134    if (token.length() > 0)
    176       ColumnCounter[MatrixNr]++;
     135      ColumnCounter++;
    177136  }
    178137  //cout << line.str() << endl;
    179   //cout << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "." << endl;
    180   if (ColumnCounter[MatrixNr] == 0)
    181     cerr << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl;
     138  //cout << "ColumnCounter: " << ColumnCounter << "." << endl;
     139  if (ColumnCounter == 0)
     140    cerr << "ColumnCounter: " << ColumnCounter << " from file " << name << ", this is probably an error!" << endl;
    182141 
    183142  // scan rest for number of rows/lines
     
    196155 
    197156  // allocate matrix if it's not zero dimension in one direction
    198   if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) {
    199     Matrix[MatrixNr] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixNr]+1), "MatrixContainer::ParseMatrix: **Matrix[]");
     157  if ((ColumnCounter > 0) && (RowCounter[MatrixNr] > -1)) {
     158    Matrix[MatrixNr] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixNr]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    200159 
    201160    // parse in each entry for this matrix
     
    203162    input.seekg(ios::beg);
    204163    for (int m=skiplines+1;m--;)
    205       input.getline(Header[MatrixNr], 1023);    // skip header
    206     line.str(Header[MatrixNr]);
     164      input.getline(Header, 1023);    // skip header
     165    line.str(Header);
    207166    for(int k=skipcolumns;k--;)  // skip columns in header too
    208167      line >> filename;
    209     strncpy(Header[MatrixNr], line.str().c_str(), 1023); 
     168    strncpy(Header, line.str().c_str(), 1023); 
    210169    for(int j=0;j<RowCounter[MatrixNr];j++) {
    211       Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[][]");
     170      Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    212171      input.getline(filename, 1023);
    213172      stringstream lines(filename);
     
    215174      for(int k=skipcolumns;k--;)
    216175        lines >> filename;
    217       for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
     176      for(int k=0;(k<ColumnCounter) && (!lines.eof());k++) {
    218177        lines >> Matrix[MatrixNr][j][k];
    219178        //cout << " " << setprecision(2) << Matrix[MatrixNr][j][k];;
    220179      }
    221180      //cout << endl;
    222       Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[RowCounter[MatrixNr]][]");
    223       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--;)
    224183        Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.;
    225184    }
    226185  } else {
    227     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;
    228187  }
    229188  input.close();
     
    274233 
    275234  cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl;
    276   Header = (char **) ReAlloc(Header, sizeof(char *)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: **Header"); // one more each for the total molecule
    277235  Matrix = (double ***) ReAlloc(Matrix, sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule
    278236  RowCounter = (int *) ReAlloc(RowCounter, sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *RowCounter");
    279   ColumnCounter = (int *) ReAlloc(ColumnCounter, sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *ColumnCounter");
    280237  for(int i=MatrixCounter+1;i--;) {
    281238    Matrix[i] = NULL;
    282     Header[i] = NULL;
    283239    RowCounter[i] = -1;
    284     ColumnCounter[i] = -1;
    285240  }
    286241  for(int i=0; i < MatrixCounter;i++) {
     
    297252
    298253/** Allocates and resets the memory for a number \a MCounter of matrices.
    299  * \param **GivenHeader Header line for each matrix
     254 * \param *GivenHeader Header line
    300255 * \param MCounter number of matrices
    301256 * \param *RCounter number of rows for each matrix
    302  * \param *CCounter number of columns for each matrix
     257 * \param CCounter number of columns for all matrices
    303258 * \return Allocation successful
    304259 */
    305 bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter)
    306 {
     260bool MatrixContainer::AllocateMatrix(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
    307265  MatrixCounter = MCounter;
    308   Header = (char **) Malloc(sizeof(char *)*(MatrixCounter+1), "MatrixContainer::AllocateMatrix: *Header");
    309   Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::AllocateMatrix: ***Matrix"); // one more each for the total molecule
    310   RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::AllocateMatrix: *RowCounter");
    311   ColumnCounter = (int *) Malloc(sizeof(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");
    312269  for(int i=MatrixCounter+1;i--;) {
    313     Header[i] = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::AllocateMatrix: *Header[i]");
    314     strncpy(Header[i], GivenHeader[i], 1023);
    315270    RowCounter[i] = RCounter[i];
    316     ColumnCounter[i] = CCounter[i];
    317     Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::AllocateMatrix: **Matrix[]"); 
     271    Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]"); 
    318272    for(int j=RowCounter[i]+1;j--;) {
    319       Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter[i], "MatrixContainer::AllocateMatrix: *Matrix[][]");
    320       for(int k=ColumnCounter[i];k--;)
     273      Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     274      for(int k=ColumnCounter;k--;)
    321275        Matrix[i][j][k] = 0.;
    322276    }
     
    332286  for(int i=MatrixCounter+1;i--;)
    333287    for(int j=RowCounter[i]+1;j--;)
    334       for(int k=ColumnCounter[i];k--;)
     288      for(int k=ColumnCounter;k--;)
    335289        Matrix[i][j][k] = 0.;
    336290   return true;
     
    345299  for(int i=MatrixCounter+1;i--;)
    346300    for(int j=RowCounter[i]+1;j--;)
    347       for(int k=ColumnCounter[i];k--;)
     301      for(int k=ColumnCounter;k--;)
    348302        if (fabs(Matrix[i][j][k]) > max)
    349303          max = fabs(Matrix[i][j][k]);
     
    361315  for(int i=MatrixCounter+1;i--;)
    362316    for(int j=RowCounter[i]+1;j--;)
    363       for(int k=ColumnCounter[i];k--;)
     317      for(int k=ColumnCounter;k--;)
    364318        if (fabs(Matrix[i][j][k]) < min)
    365319          min = fabs(Matrix[i][j][k]);
     
    377331{
    378332  for(int j=RowCounter[MatrixCounter]+1;j--;)
    379     for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
     333    for(int k=skipcolumns;k<ColumnCounter;k++)
    380334      Matrix[MatrixCounter][j][k] = value;
    381335   return true;
     
    390344{
    391345  for(int j=RowCounter[MatrixCounter]+1;j--;)
    392     for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
     346    for(int k=skipcolumns;k<ColumnCounter;k++)
    393347      Matrix[MatrixCounter][j][k] = values[j][k];
    394348   return true;
    395349};
    396350
    397 /** Sums the entries 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.
    398352 * Sums over "E"-terms to create the "F"-terms
    399  * \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.
    400354 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    401355 * \param Order bond order
     
    430384              }
    431385              if (Order == SubOrder) { // equal order is always copy from Energies
    432                 for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
     386                for(int l=ColumnCounter;l--;) // then adds/subtract each column
    433387                  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    434388              } else {
    435                 for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;)
     389                for(int l=ColumnCounter;l--;)
    436390                  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    437391              }
    438392            }
    439             //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
     393            //if ((ColumnCounter>1) && (RowCounter[0]-1 >= 1))
    440394             //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
    441395          }
     
    472426      return false;
    473427    }
    474     output << Header[i] << endl;
     428    output << Header << endl;
    475429    for(int j=0;j<RowCounter[i];j++) {
    476       for(int k=0;k<ColumnCounter[i];k++)
     430      for(int k=0;k<ColumnCounter;k++)
    477431        output << scientific << Matrix[i][j][k] << "\t";
    478432      output << endl;
     
    501455    return false;
    502456  }
    503   output << Header[MatrixCounter] << endl;
     457  output << Header << endl;
    504458  for(int j=0;j<RowCounter[MatrixCounter];j++) {
    505     for(int k=0;k<ColumnCounter[MatrixCounter];k++)
     459    for(int k=0;k<ColumnCounter;k++)
    506460      output << scientific << Matrix[MatrixCounter][j][k] << "\t";
    507461    output << endl;
     
    513467// ======================================= CLASS EnergyMatrix =============================
    514468
     469/** Create a trivial energy index mapping.
     470 * This just maps 1 to 1, 2 to 2 and so on for all fragments.
     471 * \return creation sucessful
     472 */
     473bool EnergyMatrix::ParseIndices()
     474{
     475  cout << "Parsing energy indices." << endl;
     476  Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices");
     477  for(int i=MatrixCounter+1;i--;) {
     478    Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");
     479    for(int j=RowCounter[i];j--;)
     480      Indices[i][j] = j;
     481  }
     482  return true;
     483};
     484
    515485/** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
    516486 * Sums over the "F"-terms in ANOVA decomposition.
    517  * \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.
    518488 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
    519489 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
     
    528498    for(int i=KeySet.FragmentsPerOrder[Order];i--;)
    529499      for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;)
    530         for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;)
     500        for(int k=ColumnCounter;k--;)
    531501          Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k];
    532502  else
    533503    for(int i=KeySet.FragmentsPerOrder[Order];i--;)
    534504      for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;)
    535         for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;)
     505        for(int k=ColumnCounter;k--;)
    536506          Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySet.OrderSet[Order][i] ][j][k]);
    537507  return true;
     
    554524    // count maximum of columns
    555525    RowCounter[MatrixCounter] = 0;
    556     ColumnCounter[MatrixCounter] = 0;
    557     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)
    558527      if (RowCounter[j] > RowCounter[MatrixCounter])
    559528        RowCounter[MatrixCounter] = RowCounter[j];
    560       if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
    561         ColumnCounter[MatrixCounter] = ColumnCounter[j];
    562     }
    563529    // allocate last plus one matrix
    564     cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
     530    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl;
    565531    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    566532    for(int j=0;j<=RowCounter[MatrixCounter];j++)
    567       Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     533      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    568534   
    569535    // try independently to parse global energysuffix file if present
     
    623589
    624590/** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
    625  * \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.
    626592 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    627593 * \param Order bond order
     
    643609      if (j != -1) {
    644610        //if (j == 0) cout << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
    645         for(int k=2;k<ColumnCounter[MatrixCounter];k++)
     611        for(int k=2;k<ColumnCounter;k++)
    646612          Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
    647613      }
     
    689655    RowCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
    690656    input.close();
    691 
    692     ColumnCounter[MatrixCounter] = 0;
    693     for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
    694       if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
    695         ColumnCounter[MatrixCounter] = ColumnCounter[j];
    696     }
    697657 
    698658    // allocate last plus one matrix
    699     cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
     659    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl;
    700660    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    701661    for(int j=0;j<=RowCounter[MatrixCounter];j++)
    702       Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    703 
    704     // try independently to parse global forcesuffix file if present
    705     strncpy(filename, name, 1023);
    706     strncat(filename, prefix, 1023-strlen(filename));
    707     strncat(filename, suffix.c_str(), 1023-strlen(filename));
    708     ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
    709   }
    710  
    711 
    712   return status;
    713 };
    714 
    715 // ======================================= CLASS HessianMatrix =============================
    716 
    717 /** Parsing force Indices of each fragment
    718  * \param *name directory with \a ForcesFile
    719  * \return parsing successful
    720  */
    721 bool HessianMatrix::ParseIndices(char *name)
    722 {
    723   ifstream input;
    724   char *FragmentNumber = NULL;
    725   char filename[1023];
    726   stringstream line;
    727  
    728   cout << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl;
    729   Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "HessianMatrix::ParseIndices: **Indices");
    730   line << name << FRAGMENTPREFIX << FORCESFILE;
    731   input.open(line.str().c_str(), ios::in);
    732   //cout << "Opening " << line.str() << " ... "  << input << endl;
    733   if (input == NULL) {
    734     cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
    735     return false;
    736   }
    737   for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
    738     // get the number of atoms for this fragment
    739     input.getline(filename, 1023);
    740     line.str(filename);
    741     // parse the values
    742     Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "HessianMatrix::ParseIndices: *Indices[]");
    743     FragmentNumber = FixedDigitNumber(MatrixCounter, i);
    744     //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
    745     Free((void **)&FragmentNumber, "HessianMatrix::ParseIndices: *FragmentNumber");
    746     for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
    747       line >> Indices[i][j];
    748       //cout << " " << Indices[i][j];
    749     }
    750     //cout << endl;
    751   }
    752   Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "HessianMatrix::ParseIndices: *Indices[]");
    753   for(int j=RowCounter[MatrixCounter];j--;) {
    754     Indices[MatrixCounter][j] = j;
    755   }
    756   input.close();
    757   return true;
    758 };
    759 
    760 
    761 /** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
    762  * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
    763  * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    764  * \param Order bond order
    765  *  \param sign +1 or -1
    766  * \return true if summing was successful
    767  */
    768 bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySet, int Order, double sign)
    769 {
    770   int FragmentNr;
    771   // sum forces
    772   for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) {
    773     FragmentNr = KeySet.OrderSet[Order][i];
    774     for(int l=0;l<RowCounter[ FragmentNr ];l++) {
    775       int j = Indices[ FragmentNr ][l];
    776       if (j > RowCounter[MatrixCounter]) {
    777         cerr << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl;
    778         return false;
    779       }
    780       if (j != -1) {
    781         for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
    782           int k = Indices[ FragmentNr ][m];
    783           if (k > ColumnCounter[MatrixCounter]) {
    784             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;
    785             return false;
    786           }
    787           if (k != -1) {
    788                 //cout << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
    789             Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
    790           }
    791         }
    792       }
    793     }
    794   }
    795   return true;
    796 };
    797 
    798 /** Constructor for class HessianMatrix.
    799  */
    800 HessianMatrix::HessianMatrix() : MatrixContainer()
    801 {
    802    IsSymmetric = true;
    803 }
    804 
    805 /** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
    806  * Sums over "E"-terms to create the "F"-terms
    807  * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
    808  * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    809  * \param Order bond order
    810  * \return true if summing was successful
    811  */
    812 bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order)
    813 {
    814   // go through each order
    815   for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) {
    816     //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
    817     // then go per order through each suborder and pick together all the terms that contain this fragment
    818     for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
    819       for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
    820         if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) {
    821           //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl;
    822           // if the fragment's indices are all in the current fragment
    823           for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
    824             int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k];
    825             //cout << "Current row index is " << k << "/" << m << "." << endl;
    826             if (m != -1) { // if it's not an added hydrogen
    827               for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
    828                 //cout << "Comparing " << m << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
    829                 if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) {
    830                   m = l;
    831                   break; 
    832                 }
    833               }
    834               //cout << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
    835               if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
    836                 cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment]   << " current row index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
    837                 return false;
    838               }
    839              
    840               for(int l=0;l<ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l++) {
    841                 int n = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][l];
    842                 //cout << "Current column index is " << l << "/" << n << "." << endl;
    843                 if (n != -1) { // if it's not an added hydrogen
    844                   for (int p=0;p<ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
    845                     //cout << "Comparing " << n << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
    846                     if (n == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p]) {
    847                       n = p;
    848                       break; 
    849                     }
    850                   }
    851                   //cout << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
    852                   if (n > ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
    853                     cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment]   << " current column index " << n << " is greater than " << ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
    854                     return false;
    855                   }
    856                   if (Order == SubOrder) { // equal order is always copy from Energies
    857                         //cout << "Adding " << MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
    858                     Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    859                   } else {
    860                         //cout << "Subtracting " << Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
    861                     Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    862                   }
    863                 }
    864               }
    865             }
    866             //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
    867              //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
    868           }
    869         } else {
    870           //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
    871         }
    872       }
    873     }
    874    //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;
    875   }
    876  
    877   return true;
    878 };
    879 
    880 /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
    881  * \param *name directory with files
    882  * \param *prefix prefix of each matrix file
    883  * \param *suffix suffix of each matrix file
    884  * \param skiplines number of inital lines to skip
    885  * \param skiplines number of inital columns to skip
    886  * \return parsing successful
    887  */
    888 bool HessianMatrix::ParseFragmentMatrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns)
    889 {
    890   char filename[1023];
    891   ifstream input;
    892   stringstream file;
    893   int nr;
    894   bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
    895 
    896   if (status) {
    897     // count number of atoms for last plus one matrix
    898     file << name << FRAGMENTPREFIX << KEYSETFILE;
    899     input.open(file.str().c_str(), ios::in);
    900     if (input == NULL) {
    901       cout << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
    902       return false;
    903     }
    904     RowCounter[MatrixCounter] = 0;
    905     ColumnCounter[MatrixCounter] = 0;
    906     while (!input.eof()) {
    907       input.getline(filename, 1023);
    908       stringstream zeile(filename);
    909       while (!zeile.eof()) {
    910         zeile >> nr;
    911         //cout << "Current index: " << nr << "." << endl;
    912         if (nr > RowCounter[MatrixCounter]) {
    913           RowCounter[MatrixCounter] = nr;
    914           ColumnCounter[MatrixCounter] = nr;
    915         }
    916       }
    917     }
    918     RowCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
    919     ColumnCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
    920     input.close();
    921  
    922     // allocate last plus one matrix
    923     cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
    924     Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    925     for(int j=0;j<=RowCounter[MatrixCounter];j++)
    926       Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     662      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    927663
    928664    // try independently to parse global forcesuffix file if present
Note: See TracChangeset for help on using the changeset viewer.