Ignore:
Timestamp:
Oct 18, 2008, 2:06:51 PM (17 years ago)
Author:
Frederik Heber <heber@…>
Children:
b4c71a
Parents:
59f86c
Message:

Introduced new class HessianMatrix derived from MatrixContainer, which shall contain the hessians calculated by mpqc.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • molecuilder/src/parser.cpp

    r59f86c rd87482  
    673673};
    674674
     675// ======================================= CLASS HessianMatrix =============================
     676
     677/** Parsing force Indices of each fragment
     678 * \param *name directory with \a ForcesFile
     679 * \return parsing successful
     680 */
     681bool HessianMatrix::ParseIndices(char *name)
     682{
     683  ifstream input;
     684  char *FragmentNumber = NULL;
     685  char filename[1023];
     686  stringstream line;
     687 
     688  cout << "Parsing force indices for " << MatrixCounter << " matrices." << endl;
     689  Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "HessianMatrix::ParseIndices: **Indices");
     690  line << name << FRAGMENTPREFIX << FORCESFILE;
     691  input.open(line.str().c_str(), ios::in);
     692  //cout << "Opening " << line.str() << " ... "  << input << endl;
     693  if (input == NULL) {
     694    cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
     695    return false;
     696  }
     697  for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
     698    // get the number of atoms for this fragment
     699    input.getline(filename, 1023);
     700    line.str(filename);
     701    // parse the values
     702    Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "HessianMatrix::ParseIndices: *Indices[]");
     703    FragmentNumber = FixedDigitNumber(MatrixCounter, i);
     704    //cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
     705    Free((void **)&FragmentNumber, "HessianMatrix::ParseIndices: *FragmentNumber");
     706    for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
     707      line >> Indices[i][j];
     708      //cout << " " << Indices[i][j];
     709    }
     710    //cout << endl;
     711  }
     712  Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "HessianMatrix::ParseIndices: *Indices[]");
     713  for(int j=RowCounter[MatrixCounter];j--;) {
     714    Indices[MatrixCounter][j] = j;
     715  }
     716  input.close();
     717  return true;
     718};
     719
     720
     721/** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
     722 * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
     723 * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
     724 * \param Order bond order
     725 *  \param sign +1 or -1
     726 * \return true if summing was successful
     727 */
     728bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySet, int Order, double sign)
     729{
     730  int FragmentNr;
     731  // sum forces
     732  for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) {
     733    FragmentNr = KeySet.OrderSet[Order][i];
     734    for(int l=0;l<RowCounter[ FragmentNr ];l++) {
     735      int j = Indices[ FragmentNr ][l];
     736      if (j > RowCounter[MatrixCounter]) {
     737        cerr << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl;
     738        return false;
     739      }
     740      if (j != -1) {
     741        //for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
     742        for(int m=0;m<ColumnCounter;m++) {
     743          int k = Indices[ FragmentNr ][m];
     744//          if (k > ColumnCounter[MatrixCounter]) {
     745//            cerr << "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << "!" << endl;
     746          if (k > ColumnCounter) {
     747            cerr << "Current hessian index " << k << " is greater than " << ColumnCounter << "!" << endl;
     748            return false;
     749          }
     750          if (k != -1) {
     751            Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
     752          }
     753        }
     754      }
     755    }
     756  }
     757  return true;
     758};
     759
     760
     761/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
     762 * \param *name directory with files
     763 * \param *prefix prefix of each matrix file
     764 * \param *suffix suffix of each matrix file
     765 * \param skiplines number of inital lines to skip
     766 * \param skiplines number of inital columns to skip
     767 * \return parsing successful
     768 */
     769bool HessianMatrix::ParseFragmentMatrix(char *name, char *prefix, string suffix, int skiplines, int skipcolumns)
     770{
     771  char filename[1023];
     772  ifstream input;
     773  stringstream file;
     774  int nr;
     775  bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
     776
     777  if (status) {
     778    // count number of atoms for last plus one matrix
     779    file << name << FRAGMENTPREFIX << KEYSETFILE;
     780    input.open(file.str().c_str(), ios::in);
     781    if (input == NULL) {
     782      cout << endl << "Unable to open " << file.str() << ", is the directory correct?" << endl;
     783      return false;
     784    }
     785    RowCounter[MatrixCounter] = 0;
     786    while (!input.eof()) {
     787      input.getline(filename, 1023);
     788      stringstream zeile(filename);
     789      while (!zeile.eof()) {
     790        zeile >> nr;
     791        //cout << "Current index: " << nr << "." << endl;
     792        if (nr > RowCounter[MatrixCounter])
     793          RowCounter[MatrixCounter] = nr;
     794      }
     795    }
     796    RowCounter[MatrixCounter]++;    // nr start at 0, count starts at 1
     797    input.close();
     798 
     799    // allocate last plus one matrix
     800    cout << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter << " columns." << endl;
     801    Matrix[MatrixCounter] = (double **) Malloc(sizeof(double *)*(RowCounter[MatrixCounter]+1), "MatrixContainer::ParseFragmentMatrix: **Matrix[]");
     802    for(int j=0;j<=RowCounter[MatrixCounter];j++)
     803      Matrix[MatrixCounter][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseFragmentMatrix: *Matrix[][]");
     804
     805    // try independently to parse global forcesuffix file if present
     806    strncpy(filename, name, 1023);
     807    strncat(filename, prefix, 1023-strlen(filename));
     808    strncat(filename, suffix.c_str(), 1023-strlen(filename));
     809    ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
     810  }
     811 
     812
     813  return status;
     814};
     815
    675816// ======================================= CLASS KeySetsContainer =============================
    676817
Note: See TracChangeset for help on using the changeset viewer.