Ignore:
Timestamp:
Jul 24, 2009, 10:38:32 AM (16 years ago)
Author:
Frederik Heber <heber@…>
Children:
53d153
Parents:
a048fa (diff), 47548d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
git-author:
Frederik Heber <heber@…> (07/23/09 14:23:32)
git-committer:
Frederik Heber <heber@…> (07/24/09 10:38:32)
Message:

Merge branch 'master' into ConcaveHull

Conflicts:

molecuilder/src/analyzer.cpp
molecuilder/src/bond.cpp
molecuilder/src/boundary.cpp
molecuilder/src/boundary.hpp
molecuilder/src/builder.cpp
molecuilder/src/config.cpp
molecuilder/src/datacreator.cpp
molecuilder/src/datacreator.hpp
molecuilder/src/defs.hpp
molecuilder/src/ellipsoid.cpp
molecuilder/src/joiner.cpp
molecuilder/src/molecules.cpp
molecuilder/src/molecules.hpp
molecuilder/src/parser.cpp
molecuilder/src/parser.hpp

merges:

compilation fixes:

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/parser.cpp

    ra048fa rc3a303  
    5656MatrixContainer::MatrixContainer() {
    5757  Indices = NULL;
    58   Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::MatrixContainer: *Header");
     58  Header = (char **) Malloc(sizeof(char)*1, "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;
    6163  Matrix[0] = NULL;
    6264  RowCounter[0] = -1;
    6365  MatrixCounter = 0;
    64   ColumnCounter = 0;
     66  ColumnCounter[0] = -1;
    6567};
    6668
     
    7072  if (Matrix != NULL) {
    7173    for(int i=MatrixCounter;i--;) {
    72       if (RowCounter != NULL) {
     74      if ((ColumnCounter != NULL) && (RowCounter != NULL)) {
    7375          for(int j=RowCounter[i]+1;j--;)
    7476            Free((void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]");
     
    7678      }
    7779    }
    78     if ((RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
     80    if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
    7981      for(int j=RowCounter[MatrixCounter]+1;j--;)
    8082        Free((void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]");
     
    8789      Free((void **)&Indices[i], "MatrixContainer::~MatrixContainer: *Indices[]");
    8890    }
    89   Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices");
    90 
    91   Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header");
     91  Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices");
     92 
     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");
    9297  Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter");
    93 };
    94 
     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 */
     106bool 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};
    95135
    96136/** Parsing a number of matrices.
     
    121161  }
    122162
    123   // skip some initial lines
     163  // parse header
     164  Header[MatrixNr] = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseMatrix: *Header[]");
    124165  for (int m=skiplines+1;m--;)
    125     input.getline(Header, 1023);
    126 
     166    input.getline(Header[MatrixNr], 1023);
     167 
    127168  // scan header for number of columns
    128   line.str(Header);
     169  line.str(Header[MatrixNr]);
    129170  for(int k=skipcolumns;k--;)
    130     line >> Header;
     171    line >> Header[MatrixNr];
    131172  //cout << line.str() << endl;
    132   ColumnCounter=0;
     173  ColumnCounter[MatrixNr]=0;
    133174  while ( getline(line,token, '\t') ) {
    134175    if (token.length() > 0)
    135       ColumnCounter++;
     176      ColumnCounter[MatrixNr]++;
    136177  }
    137178  //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 
     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;
     182 
    142183  // scan rest for number of rows/lines
    143184  RowCounter[MatrixNr]=-1;    // counts one line too much
     
    155196
    156197  // 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 
     198  if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) {
     199    Matrix[MatrixNr] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixNr]+1), "MatrixContainer::ParseMatrix: **Matrix[]");
     200 
    160201    // parse in each entry for this matrix
    161202    input.clear();
    162203    input.seekg(ios::beg);
    163204    for (int m=skiplines+1;m--;)
    164       input.getline(Header, 1023);    // skip header
    165     line.str(Header);
     205      input.getline(Header[MatrixNr], 1023);    // skip header
     206    line.str(Header[MatrixNr]);
    166207    for(int k=skipcolumns;k--;)  // skip columns in header too
    167208      line >> filename;
    168     strncpy(Header, line.str().c_str(), 1023);
     209    strncpy(Header[MatrixNr], line.str().c_str(), 1023); 
    169210    for(int j=0;j<RowCounter[MatrixNr];j++) {
    170       Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     211      Matrix[MatrixNr][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[][]");
    171212      input.getline(filename, 1023);
    172213      stringstream lines(filename);
     
    174215      for(int k=skipcolumns;k--;)
    175216        lines >> filename;
    176       for(int k=0;(k<ColumnCounter) && (!lines.eof());k++) {
     217      for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
    177218        lines >> Matrix[MatrixNr][j][k];
    178219        //cout << " " << setprecision(2) << Matrix[MatrixNr][j][k];;
    179220      }
    180221      //cout << endl;
    181       Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[RowCounter[MatrixNr]][]");
    182       for(int j=ColumnCounter;j--;)
     222      Matrix[MatrixNr][ RowCounter[MatrixNr] ] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixNr], "MatrixContainer::ParseMatrix: *Matrix[RowCounter[MatrixNr]][]");
     223      for(int j=ColumnCounter[MatrixNr];j--;)
    183224        Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.;
    184225    }
    185226  } else {
    186     cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl;
     227    cerr << "ERROR: Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl;
    187228  }
    188229  input.close();
     
    233274
    234275  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
    235277  Matrix = (double ***) ReAlloc(Matrix, sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: ***Matrix"); // one more each for the total molecule
    236278  RowCounter = (int *) ReAlloc(RowCounter, sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *RowCounter");
     279  ColumnCounter = (int *) ReAlloc(ColumnCounter, sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseFragmentMatrix: *ColumnCounter");
    237280  for(int i=MatrixCounter+1;i--;) {
    238281    Matrix[i] = NULL;
     282    Header[i] = NULL;
    239283    RowCounter[i] = -1;
     284    ColumnCounter[i] = -1;
    240285  }
    241286  for(int i=0; i < MatrixCounter;i++) {
     
    252297
    253298/** Allocates and resets the memory for a number \a MCounter of matrices.
    254  * \param *GivenHeader Header line
     299 * \param **GivenHeader Header line for each matrix
    255300 * \param MCounter number of matrices
    256301 * \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 
     302 * \param *CCounter number of columns for each matrix
     303 * \return Allocation successful
     304 */
     305bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter)
     306{
    265307  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");
     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");
    269312  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);
    270315    RowCounter[i] = RCounter[i];
    271     Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
     316    ColumnCounter[i] = CCounter[i];
     317    Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::AllocateMatrix: **Matrix[]"); 
    272318    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--;)
     319      Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter[i], "MatrixContainer::AllocateMatrix: *Matrix[][]");
     320      for(int k=ColumnCounter[i];k--;)
    275321        Matrix[i][j][k] = 0.;
    276322    }
     
    286332  for(int i=MatrixCounter+1;i--;)
    287333    for(int j=RowCounter[i]+1;j--;)
    288       for(int k=ColumnCounter;k--;)
     334      for(int k=ColumnCounter[i];k--;)
    289335        Matrix[i][j][k] = 0.;
    290336   return true;
     
    299345  for(int i=MatrixCounter+1;i--;)
    300346    for(int j=RowCounter[i]+1;j--;)
    301       for(int k=ColumnCounter;k--;)
     347      for(int k=ColumnCounter[i];k--;)
    302348        if (fabs(Matrix[i][j][k]) > max)
    303349          max = fabs(Matrix[i][j][k]);
     
    315361  for(int i=MatrixCounter+1;i--;)
    316362    for(int j=RowCounter[i]+1;j--;)
    317       for(int k=ColumnCounter;k--;)
     363      for(int k=ColumnCounter[i];k--;)
    318364        if (fabs(Matrix[i][j][k]) < min)
    319365          min = fabs(Matrix[i][j][k]);
     
    331377{
    332378  for(int j=RowCounter[MatrixCounter]+1;j--;)
    333     for(int k=skipcolumns;k<ColumnCounter;k++)
     379    for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
    334380      Matrix[MatrixCounter][j][k] = value;
    335381   return true;
     
    344390{
    345391  for(int j=RowCounter[MatrixCounter]+1;j--;)
    346     for(int k=skipcolumns;k<ColumnCounter;k++)
     392    for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
    347393      Matrix[MatrixCounter][j][k] = values[j][k];
    348394   return true;
    349395};
    350396
    351 /** Sums the energy with each factor and put into last element of \a ***Energies.
     397/** Sums the entries with each factor and put into last element of \a ***Matrix.
    352398 * Sums over "E"-terms to create the "F"-terms
    353  * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
     399 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
    354400 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    355401 * \param Order bond order
     
    384430              }
    385431              if (Order == SubOrder) { // equal order is always copy from Energies
    386                 for(int l=ColumnCounter;l--;) // then adds/subtract each column
     432                for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
    387433                  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    388434              } else {
    389                 for(int l=ColumnCounter;l--;)
     435                for(int l=ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l--;)
    390436                  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
    391437              }
    392438            }
    393             //if ((ColumnCounter>1) && (RowCounter[0]-1 >= 1))
     439            //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
    394440             //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
    395441          }
     
    426472      return false;
    427473    }
    428     output << Header << endl;
     474    output << Header[i] << endl;
    429475    for(int j=0;j<RowCounter[i];j++) {
    430       for(int k=0;k<ColumnCounter;k++)
     476      for(int k=0;k<ColumnCounter[i];k++)
    431477        output << scientific << Matrix[i][j][k] << "\t";
    432478      output << endl;
     
    455501    return false;
    456502  }
    457   output << Header << endl;
     503  output << Header[MatrixCounter] << endl;
    458504  for(int j=0;j<RowCounter[MatrixCounter];j++) {
    459     for(int k=0;k<ColumnCounter;k++)
     505    for(int k=0;k<ColumnCounter[MatrixCounter];k++)
    460506      output << scientific << Matrix[MatrixCounter][j][k] << "\t";
    461507    output << endl;
     
    485531/** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
    486532 * Sums over the "F"-terms in ANOVA decomposition.
    487  * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
     533 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
    488534 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
    489535 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
     
    498544    for(int i=KeySet.FragmentsPerOrder[Order];i--;)
    499545      for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;)
    500         for(int k=ColumnCounter;k--;)
     546        for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;)
    501547          Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k];
    502548  else
    503549    for(int i=KeySet.FragmentsPerOrder[Order];i--;)
    504550      for(int j=RowCounter[ KeySet.OrderSet[Order][i] ];j--;)
    505         for(int k=ColumnCounter;k--;)
     551        for(int k=ColumnCounter[ KeySet.OrderSet[Order][i] ];k--;)
    506552          Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySet.OrderSet[Order][i] ][j][k]);
    507553  return true;
     
    524570    // count maximum of columns
    525571    RowCounter[MatrixCounter] = 0;
    526     for(int j=0; j < MatrixCounter;j++) // (energy matrix might be bigger than number of atoms in terms of rows)
     572    ColumnCounter[MatrixCounter] = 0;
     573    for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
    527574      if (RowCounter[j] > RowCounter[MatrixCounter])
    528575        RowCounter[MatrixCounter] = RowCounter[j];
     576      if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
     577        ColumnCounter[MatrixCounter] = ColumnCounter[j];
     578    }
    529579    // allocate last plus one matrix
    530     cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl;
     580    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
    531581    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    532582    for(int j=0;j<=RowCounter[MatrixCounter];j++)
    533       Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    534 
     583      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     584   
    535585    // try independently to parse global energysuffix file if present
    536586    strncpy(filename, name, 1023);
     
    589639
    590640/** 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.
     641 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
    592642 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
    593643 * \param Order bond order
     
    609659      if (j != -1) {
    610660        //if (j == 0) cout << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
    611         for(int k=2;k<ColumnCounter;k++)
     661        for(int k=2;k<ColumnCounter[MatrixCounter];k++)
    612662          Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
    613663      }
     
    656706    input.close();
    657707
     708    ColumnCounter[MatrixCounter] = 0;
     709    for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
     710      if (ColumnCounter[j] > ColumnCounter[MatrixCounter])  // take maximum of all for last matrix
     711        ColumnCounter[MatrixCounter] = ColumnCounter[j];
     712    }
     713 
    658714    // allocate last plus one matrix
    659     cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl;
     715    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
    660716    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
    661717    for(int j=0;j<=RowCounter[MatrixCounter];j++)
    662       Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     718      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     719
     720    // try independently to parse global forcesuffix file if present
     721    strncpy(filename, name, 1023);
     722    strncat(filename, prefix, 1023-strlen(filename));
     723    strncat(filename, suffix.c_str(), 1023-strlen(filename));
     724    ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
     725  }
     726 
     727
     728  return status;
     729};
     730
     731// ======================================= CLASS HessianMatrix =============================
     732
     733/** Parsing force Indices of each fragment
     734 * \param *name directory with \a ForcesFile
     735 * \return parsing successful
     736 */
     737bool HessianMatrix::ParseIndices(char *name)
     738{
     739  ifstream input;
     740  char *FragmentNumber = NULL;
     741  char filename[1023];
     742  stringstream line;
     743 
     744  cout << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl;
     745  Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "HessianMatrix::ParseIndices: **Indices");
     746  line << name << FRAGMENTPREFIX << FORCESFILE;
     747  input.open(line.str().c_str(), ios::in);
     748  //cout << "Opening " << line.str() << " ... "  << input << endl;
     749  if (input == NULL) {
     750    cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
     751    return false;
     752  }
     753  for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
     754    // get the number of atoms for this fragment
     755    input.getline(filename, 1023);
     756    line.str(filename);
     757    // parse the values
     758    Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "HessianMatrix::ParseIndices: *Indices[]");
     759    FragmentNumber = FixedDigitNumber(MatrixCounter, i);
     760    //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
     761    Free((void **)&FragmentNumber, "HessianMatrix::ParseIndices: *FragmentNumber");
     762    for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
     763      line >> Indices[i][j];
     764      //cout << " " << Indices[i][j];
     765    }
     766    //cout << endl;
     767  }
     768  Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "HessianMatrix::ParseIndices: *Indices[]");
     769  for(int j=RowCounter[MatrixCounter];j--;) {
     770    Indices[MatrixCounter][j] = j;
     771  }
     772  input.close();
     773  return true;
     774};
     775
     776
     777/** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
     778 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
     779 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
     780 * \param Order bond order
     781 *  \param sign +1 or -1
     782 * \return true if summing was successful
     783 */
     784bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySet, int Order, double sign)
     785{
     786  int FragmentNr;
     787  // sum forces
     788  for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) {
     789    FragmentNr = KeySet.OrderSet[Order][i];
     790    for(int l=0;l<RowCounter[ FragmentNr ];l++) {
     791      int j = Indices[ FragmentNr ][l];
     792      if (j > RowCounter[MatrixCounter]) {
     793        cerr << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl;
     794        return false;
     795      }
     796      if (j != -1) {
     797        for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
     798          int k = Indices[ FragmentNr ][m];
     799          if (k > ColumnCounter[MatrixCounter]) {
     800            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;
     801            return false;
     802          }
     803          if (k != -1) {
     804            //cout << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
     805            Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
     806          }
     807        }
     808      }
     809    }
     810  }
     811  return true;
     812};
     813
     814/** Constructor for class HessianMatrix.
     815 */
     816HessianMatrix::HessianMatrix() : MatrixContainer()
     817{
     818   IsSymmetric = true;
     819}
     820
     821/** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
     822 * Sums over "E"-terms to create the "F"-terms
     823 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
     824 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
     825 * \param Order bond order
     826 * \return true if summing was successful
     827 */
     828bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order)
     829{
     830  // go through each order
     831  for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) {
     832    //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
     833    // then go per order through each suborder and pick together all the terms that contain this fragment
     834    for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
     835      for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
     836        if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) {
     837          //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl;
     838          // if the fragment's indices are all in the current fragment
     839          for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
     840            int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k];
     841            //cout << "Current row index is " << k << "/" << m << "." << endl;
     842            if (m != -1) { // if it's not an added hydrogen
     843              for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
     844                //cout << "Comparing " << m << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
     845                if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) {
     846                  m = l;
     847                  break; 
     848                }
     849              }
     850              //cout << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
     851              if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
     852                cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment]   << " current row index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
     853                return false;
     854              }
     855             
     856              for(int l=0;l<ColumnCounter[ KeySet.OrderSet[SubOrder][j] ];l++) {
     857                int n = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][l];
     858                //cout << "Current column index is " << l << "/" << n << "." << endl;
     859                if (n != -1) { // if it's not an added hydrogen
     860                  for (int p=0;p<ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
     861                    //cout << "Comparing " << n << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
     862                    if (n == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][p]) {
     863                      n = p;
     864                      break; 
     865                    }
     866                  }
     867                  //cout << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
     868                  if (n > ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
     869                    cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment]   << " current column index " << n << " is greater than " << ColumnCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
     870                    return false;
     871                  }
     872                  if (Order == SubOrder) { // equal order is always copy from Energies
     873                    //cout << "Adding " << MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
     874                    Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
     875                  } else {
     876                    //cout << "Subtracting " << Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
     877                    Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
     878                  }
     879                }
     880              }
     881            }
     882            //if ((ColumnCounter[ KeySet.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
     883             //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
     884          }
     885        } else {
     886          //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
     887        }
     888      }
     889    }
     890   //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;
     891  }
     892 
     893  return true;
     894};
     895
     896/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
     897 * \param *name directory with files
     898 * \param *prefix prefix of each matrix file
     899 * \param *suffix suffix of each matrix file
     900 * \param skiplines number of inital lines to skip
     901 * \param skiplines number of inital columns to skip
     902 * \return parsing successful
     903 */
     904bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
     905{
     906  char filename[1023];
     907  ifstream input;
     908  stringstream file;
     909  int nr;
     910  bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
     911
     912  if (status) {
     913    // count number of atoms for last plus one matrix
     914    file << name << FRAGMENTPREFIX << KEYSETFILE;
     915    input.open(file.str().c_str(), ios::in);
     916    if (input == NULL) {
     917      cout << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
     918      return false;
     919    }
     920    RowCounter[MatrixCounter] = 0;
     921    ColumnCounter[MatrixCounter] = 0;
     922    while (!input.eof()) {
     923      input.getline(filename, 1023);
     924      stringstream zeile(filename);
     925      while (!zeile.eof()) {
     926        zeile >> nr;
     927        //cout << "Current index: " << nr << "." << endl;
     928        if (nr > RowCounter[MatrixCounter]) {
     929          RowCounter[MatrixCounter] = nr;
     930          ColumnCounter[MatrixCounter] = nr;
     931        }
     932      }
     933    }
     934    RowCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
     935    ColumnCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
     936    input.close();
     937 
     938    // allocate last plus one matrix
     939    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl;
     940    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
     941    for(int j=0;j<=RowCounter[MatrixCounter];j++)
     942      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter[MatrixCounter], "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
    663943
    664944    // try independently to parse global forcesuffix file if present
Note: See TracChangeset for help on using the changeset viewer.