Changeset a2d653


Ignore:
Timestamp:
Jul 2, 2012, 7:54:12 AM (13 years ago)
Author:
Frederik Heber <heber@…>
Branches:
Action_Thermostats, Add_AtomRandomPerturbation, Add_FitFragmentPartialChargesAction, Add_RotateAroundBondAction, Add_SelectAtomByNameAction, Added_ParseSaveFragmentResults, AddingActions_SaveParseParticleParameters, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_ParticleName_to_Atom, Adding_StructOpt_integration_tests, AtomFragments, Automaking_mpqc_open, AutomationFragmentation_failures, Candidate_v1.5.4, Candidate_v1.6.0, Candidate_v1.6.1, ChangeBugEmailaddress, ChangingTestPorts, ChemicalSpaceEvaluator, CombiningParticlePotentialParsing, Combining_Subpackages, Debian_Package_split, Debian_package_split_molecuildergui_only, Disabling_MemDebug, Docu_Python_wait, EmpiricalPotential_contain_HomologyGraph, EmpiricalPotential_contain_HomologyGraph_documentation, Enable_parallel_make_install, Enhance_userguide, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Example_ManyWaysToTranslateAtom, Exclude_Hydrogens_annealWithBondGraph, FitPartialCharges_GlobalError, Fix_BoundInBox_CenterInBox_MoleculeActions, Fix_ChargeSampling_PBC, Fix_ChronosMutex, Fix_FitPartialCharges, Fix_FitPotential_needs_atomicnumbers, Fix_ForceAnnealing, Fix_IndependentFragmentGrids, Fix_ParseParticles, Fix_ParseParticles_split_forward_backward_Actions, Fix_PopActions, Fix_QtFragmentList_sorted_selection, Fix_Restrictedkeyset_FragmentMolecule, Fix_StatusMsg, Fix_StepWorldTime_single_argument, Fix_Verbose_Codepatterns, Fix_fitting_potentials, Fixes, ForceAnnealing_goodresults, ForceAnnealing_oldresults, ForceAnnealing_tocheck, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, FragmentAction_writes_AtomFragments, FragmentMolecule_checks_bonddegrees, GeometryObjects, Gui_Fixes, Gui_displays_atomic_force_velocity, ImplicitCharges, IndependentFragmentGrids, IndependentFragmentGrids_IndividualZeroInstances, IndependentFragmentGrids_IntegrationTest, IndependentFragmentGrids_Sole_NN_Calculation, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, JobMarket_unresolvable_hostname_fix, MoreRobust_FragmentAutomation, ODR_violation_mpqc_open, PartialCharges_OrthogonalSummation, PdbParser_setsAtomName, PythonUI_with_named_parameters, QtGui_reactivate_TimeChanged_changes, Recreated_GuiChecks, Rewrite_FitPartialCharges, RotateToPrincipalAxisSystem_UndoRedo, SaturateAtoms_findBestMatching, SaturateAtoms_singleDegree, StoppableMakroAction, Subpackage_CodePatterns, Subpackage_JobMarket, Subpackage_LinearAlgebra, Subpackage_levmar, Subpackage_mpqc_open, Subpackage_vmg, Switchable_LogView, ThirdParty_MPQC_rebuilt_buildsystem, TrajectoryDependenant_MaxOrder, TremoloParser_IncreasedPrecision, TremoloParser_MultipleTimesteps, TremoloParser_setsAtomName, Ubuntu_1604_changes, stable
Children:
e11611
Parents:
d7bb9b1
git-author:
Frederik Heber <heber@…> (04/19/12 18:22:48)
git-committer:
Frederik Heber <heber@…> (07/02/12 07:54:12)
Message:

Added MatrixContainer::AddMatrix() which takes of the functionality of ::ParseMatrix().

  • AddMatrix simply adds a received MatrixArray to the internal matrix. Also, we restructured the function a bit: check preconditions, allocate, then set.
  • ParseMatrix() creates first such a MatrixArray which it parses from a file.
Location:
src/Fragmentation
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • src/Fragmentation/MatrixContainer.cpp

    rd7bb9b1 ra2d653  
    8888 *    -# scan header lines for number of columns
    8989 *    -# scan lines for number of rows
    90  *    -# allocate matrix
     90 *    -# allocate a temporary matrix
    9191 *    -# loop over found column and row counts and parse in each entry
     92 *    -# use MatrixContainer::AddMatrix() to add the parsed matrix to the internal
    9293 * \param &input input stream
    9394 * \param skiplines number of inital lines to skip
     
    109110
    110111  // parse header
    111   if (Header.size() <= MatrixNr)
    112     Header.resize(MatrixNr);
    113   Header[MatrixNr] = std::string("");
     112  std::string header;
    114113  char dummy[1024];
    115114  for (int m=skiplines+1;m--;)
     
    117116  line.str(dummy);
    118117  for(int k=skipcolumns;k--;)
    119     line >> Header[MatrixNr];
     118    line >> header;
    120119  LOG(3, "INFO: Header of Matrix " << MatrixNr << " :" << line.str());
    121120
    122121  // scan header for number of columns
    123   if (ColumnCounter.size() <= MatrixNr)
    124     ColumnCounter.resize(MatrixNr);
    125   ColumnCounter[MatrixNr]=0;
     122  size_t ColumnCounter = 0;
    126123  while ( getline(line,token, '\t') ) {
    127124    if (token.length() > 0)
    128       ColumnCounter[MatrixNr]++;
     125      ColumnCounter++;
    129126  }
    130127  LOG(3, "INFO: "+line.str());
    131   LOG(4, "INFO: ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << ".");
    132   if (ColumnCounter[MatrixNr] == 0) {
    133     ELOG(0, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream.");
    134     performCriticalExit();
    135   }
    136128
    137129  // scan rest for number of rows/lines
    138   if (RowCounter.size() <= MatrixNr)
    139     RowCounter.resize(MatrixNr);
    140   RowCounter[MatrixNr]=-1;    // counts one line too much
     130  size_t RowCounter = -1;
    141131  while (!input.eof()) {
    142132    input.getline(filename, 1023);
    143133    LOG(3, "INFO: Comparing: " << strncmp(filename,"MeanForce",9));
    144     RowCounter[MatrixNr]++; // then line was not last MeanForce
     134    RowCounter++; // then line was not last MeanForce
    145135    if (strncmp(filename,"MeanForce", 9) == 0) {
    146136      break;
    147137    }
    148138  }
    149   LOG(4, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
    150   if (RowCounter[MatrixNr] == 0) {
    151     ELOG(0, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
    152     performCriticalExit();
    153   }
    154 
    155   // allocate matrix if it's not zero dimension in one direction
    156   if (Matrix.size() <= MatrixNr)
    157     Matrix.resize(MatrixNr+1);
    158   if ((Matrix[MatrixNr].size() <= (size_t)RowCounter[MatrixNr] + 1) && (RowCounter[MatrixNr] > -1)) {
    159     Matrix[MatrixNr].resize(RowCounter[MatrixNr] + 2);
    160     for(int j=0;j<=RowCounter[MatrixNr];j++) {
    161       if ((Matrix[MatrixNr][j].size() <= (size_t)ColumnCounter[MatrixNr]) && (ColumnCounter[MatrixNr] > -1))
    162         Matrix[MatrixNr][j].resize(ColumnCounter[MatrixNr]+1);
    163       // clear
    164       for(int k=0;k<=ColumnCounter[MatrixNr];k++)
    165         Matrix[MatrixNr][j][k] = 0;
    166     }
    167   } else {
    168     ELOG(1, "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!");
    169     return false;
    170   }
     139
     140  // allocate temporary matrix
     141  MatrixArray temp_matrix;
     142  temp_matrix.resize(RowCounter);
     143  for(MatrixArray::iterator iter = temp_matrix.begin(); iter != temp_matrix.end(); ++iter)
     144    (*iter).resize(ColumnCounter);
    171145
    172146  // parse in each entry for this matrix
     
    179153  for(int k=skipcolumns;k--;)  // skip columns in header too
    180154    line >> filename;
    181   Header[MatrixNr] = line.str();
    182   for(int j=0;j<RowCounter[MatrixNr];j++) {
     155  header = line.str();
     156  for(size_t j=0;j<RowCounter;j++) {
    183157    input.getline(filename, 1023);
    184158    std::stringstream lines(filename);
     
    187161    for(int k=skipcolumns;k--;)
    188162      lines >> filename;
    189     for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
    190       lines >> Matrix[MatrixNr][j][k];
    191       output << " " << std::setprecision(2) << Matrix[MatrixNr][j][k] << endl;
     163    for(size_t k=0;(k<ColumnCounter) && (!lines.eof());k++) {
     164      lines >> temp_matrix[j][k];
     165      output << " " << std::setprecision(2) << temp_matrix[j][k] << endl;
    192166    }
    193167    LOG(3, output.str());
    194168  }
    195169
    196   return true;
    197 };
     170  // finally add the matrix
     171  return AddMatrix(header, temp_matrix, MatrixNr);
     172}
     173
     174/** Adds a matrix at position \a MatrixNr to MatrixContainer::Matrix.
     175 *
     176 * @param header header to add for this matrix
     177 * @param matrix to add
     178 * @param MatrixNr position in MatrixContainer::Matrix.
     179 * @return true - insertion ok, false - invalid matrix
     180 */
     181bool MatrixContainer::AddMatrix(const std::string &header, const MatrixArray &matrix, size_t MatrixNr)
     182{
     183  // make some pre-checks
     184  if (header.size() == 0)
     185    ELOG(2, "The given header of the matrix to add is empty.");
     186  if (matrix.size() == 0) {
     187    ELOG(1, "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
     188    return false;
     189  }
     190  if (matrix[0].size() == 0) {
     191    ELOG(1, "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from ostream.");
     192    return false;
     193  }
     194
     195  // add header
     196  if (Header.size() <= MatrixNr)
     197    Header.resize(MatrixNr+1);
     198  Header[MatrixNr] = header;
     199
     200  // row count
     201  if (RowCounter.size() <= MatrixNr)
     202    RowCounter.resize(MatrixNr+1);
     203  RowCounter[MatrixNr] = matrix.size();
     204  LOG(4, "INFO: RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from input stream.");
     205
     206  // column count
     207  if (ColumnCounter.size() <= MatrixNr)
     208    ColumnCounter.resize(MatrixNr+1);
     209  ColumnCounter[MatrixNr] = matrix[0].size();
     210  LOG(4, "INFO: ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << ".");
     211
     212  // allocate matrix ...
     213  if (Matrix.size() <= MatrixNr)
     214    Matrix.resize(MatrixNr+1);
     215  MatrixCounter = Matrix.size()-1;
     216  Matrix[MatrixNr].resize(RowCounter[MatrixNr] + 1);
     217  for(int j=0;j<=RowCounter[MatrixNr];++j)
     218    Matrix[MatrixNr][j].resize(ColumnCounter[MatrixNr]+1);
     219
     220  // .. and copy values
     221  for(int j=0;j<RowCounter[MatrixNr];++j)
     222    for(int k=0;k<ColumnCounter[MatrixNr];++k)
     223      Matrix[MatrixNr][j][k] = matrix[j][k];
     224  // reset last column
     225  for(int j=0;j<RowCounter[MatrixNr];++j)
     226    Matrix[MatrixNr][j][ ColumnCounter[MatrixNr] ] = 0.;
     227  // reset last row
     228  for(int k=0;k<=ColumnCounter[MatrixNr];++k)
     229    Matrix[MatrixNr][ RowCounter[MatrixNr] ][ k ] = 0.;
     230
     231  return true;
     232}
    198233
    199234/** Parsing a number of matrices.
  • src/Fragmentation/MatrixContainer.hpp

    rd7bb9b1 ra2d653  
    4141  bool InitialiseIndices(class MatrixContainer *Matrix = NULL);
    4242  bool ParseMatrix(std::istream &input, int skiplines, int skipcolumns, size_t MatrixNr);
     43  bool AddMatrix(const std::string &header, const MatrixArray &matrix, size_t MatrixNr);
    4344  virtual bool ParseFragmentMatrix(const std::string name, const std::string prefix, std::string suffix, int skiplines, int skipcolumns);
    4445  bool AllocateMatrix(StringVector GivenHeader, int MCounter, IntVector RCounter, IntVector CCounter);
Note: See TracChangeset for help on using the changeset viewer.