| [a0bcf1] | 1 | /** \file parsing.cpp | 
|---|
|  | 2 | * | 
|---|
|  | 3 | * Declarations of various class functions for the parsing of value files. | 
|---|
|  | 4 | * | 
|---|
|  | 5 | */ | 
|---|
|  | 6 |  | 
|---|
|  | 7 | using namespace std; | 
|---|
|  | 8 |  | 
|---|
|  | 9 | // ======================================= INCLUDES ========================================== | 
|---|
|  | 10 |  | 
|---|
|  | 11 | #include "helpers.hpp" | 
|---|
|  | 12 | #include "parser.hpp" | 
|---|
|  | 13 |  | 
|---|
|  | 14 | // ======================================= FUNCTIONS ========================================== | 
|---|
|  | 15 |  | 
|---|
|  | 16 | /** Test if given filename can be opened. | 
|---|
|  | 17 | * \param filename name of file | 
|---|
|  | 18 | * \return given file exists | 
|---|
|  | 19 | */ | 
|---|
|  | 20 | #ifdef HAVE_INLINE | 
|---|
|  | 21 | inline | 
|---|
|  | 22 | #endif | 
|---|
|  | 23 | bool FilePresent(const char *filename) | 
|---|
|  | 24 | { | 
|---|
|  | 25 | ifstream input; | 
|---|
|  | 26 |  | 
|---|
|  | 27 | input.open(filename, ios::in); | 
|---|
|  | 28 | if (input == NULL) { | 
|---|
|  | 29 | cout << endl << "Unable to open " << filename << ", is the directory correct?" << endl; | 
|---|
|  | 30 | return false; | 
|---|
|  | 31 | } | 
|---|
|  | 32 | input.close(); | 
|---|
|  | 33 | return true; | 
|---|
|  | 34 | }; | 
|---|
|  | 35 |  | 
|---|
|  | 36 | /** Test the given parameters. | 
|---|
|  | 37 | * \param argc argument count | 
|---|
|  | 38 | * \param **argv arguments array | 
|---|
|  | 39 | * \return given inputdir is valid | 
|---|
|  | 40 | */ | 
|---|
|  | 41 | #ifdef HAVE_INLINE | 
|---|
|  | 42 | inline | 
|---|
|  | 43 | #endif | 
|---|
|  | 44 | bool TestParams(int argc, char **argv) | 
|---|
|  | 45 | { | 
|---|
|  | 46 | ifstream input; | 
|---|
|  | 47 | stringstream line; | 
|---|
|  | 48 |  | 
|---|
|  | 49 | line << argv[1] << FactorsFile; | 
|---|
|  | 50 | return FilePresent(line.str().c_str()); | 
|---|
|  | 51 | }; | 
|---|
|  | 52 |  | 
|---|
|  | 53 | // ======================================= CLASS MatrixContainer ============================= | 
|---|
|  | 54 |  | 
|---|
|  | 55 | /** Constructor of MatrixContainer class. | 
|---|
|  | 56 | */ | 
|---|
|  | 57 | MatrixContainer::MatrixContainer() { | 
|---|
|  | 58 | Matrix = NULL; | 
|---|
|  | 59 | Indices = NULL; | 
|---|
|  | 60 | Header = NULL; | 
|---|
|  | 61 | MatrixCounter = 0; | 
|---|
|  | 62 | RowCounter = NULL; | 
|---|
|  | 63 | ColumnCounter = 0; | 
|---|
|  | 64 | }; | 
|---|
|  | 65 |  | 
|---|
|  | 66 | /** Destructor of MatrixContainer class. | 
|---|
|  | 67 | */ | 
|---|
|  | 68 | MatrixContainer::~MatrixContainer() { | 
|---|
|  | 69 | if (Matrix != NULL) | 
|---|
|  | 70 | for(int i=0;i<MatrixCounter;i++) { | 
|---|
|  | 71 | if (RowCounter != NULL) | 
|---|
|  | 72 | for(int j=0;j<=RowCounter[i];j++) | 
|---|
|  | 73 | Free((void **)&Matrix[i][j], "MatrixContainer::~MatrixContainer: *Matrix[][]"); | 
|---|
|  | 74 | Free((void **)&Matrix[i], "MatrixContainer::~MatrixContainer: **Matrix[]"); | 
|---|
|  | 75 | } | 
|---|
|  | 76 | if ((RowCounter != NULL) && (Matrix[MatrixCounter] != NULL)) | 
|---|
|  | 77 | for(int j=0;j<=RowCounter[MatrixCounter];j++) | 
|---|
|  | 78 | Free((void **)&Matrix[MatrixCounter][j], "MatrixContainer::~MatrixContainer: *Matrix[MatrixCounter][]"); | 
|---|
|  | 79 | Free((void **)&Matrix[MatrixCounter], "MatrixContainer::~MatrixContainer: **Matrix[MatrixCounter]"); | 
|---|
|  | 80 | Free((void **)&Matrix, "MatrixContainer::~MatrixContainer: ***Matrix"); | 
|---|
|  | 81 |  | 
|---|
|  | 82 | if (Indices != NULL) | 
|---|
|  | 83 | for(int i=0;i<=MatrixCounter;i++) { | 
|---|
|  | 84 | Free((void **)&Indices[i], "MatrixContainer::~MatrixContainer: *Indices[]"); | 
|---|
|  | 85 | } | 
|---|
|  | 86 | Free((void **)&Indices, "MatrixContainer::~MatrixContainer: **Indices"); | 
|---|
|  | 87 |  | 
|---|
|  | 88 | Free((void **)&Header, "MatrixContainer::~MatrixContainer: *Header"); | 
|---|
|  | 89 | Free((void **)&RowCounter, "MatrixContainer::~MatrixContainer: *RowCounter"); | 
|---|
|  | 90 | }; | 
|---|
|  | 91 |  | 
|---|
|  | 92 | /** Parsing a number of matrices. | 
|---|
|  | 93 | * \param *name directory with files | 
|---|
|  | 94 | * \param *prefix prefix of each matrix file | 
|---|
|  | 95 | * \param *suffix suffix of each matrix file | 
|---|
|  | 96 | * \param skiplines number of inital lines to skip | 
|---|
|  | 97 | * \param skiplines number of inital columns to skip | 
|---|
|  | 98 | * \return parsing successful | 
|---|
|  | 99 | */ | 
|---|
|  | 100 | bool MatrixContainer::ParseMatrix(char *name, char *prefix, char *suffix, int skiplines, int skipcolumns) | 
|---|
|  | 101 | { | 
|---|
|  | 102 | char filename[1023]; | 
|---|
|  | 103 | ifstream input; | 
|---|
|  | 104 | char *FragmentNumber = NULL; | 
|---|
|  | 105 | stringstream line; | 
|---|
|  | 106 | double tmp; | 
|---|
|  | 107 |  | 
|---|
|  | 108 | Header = (char *) Malloc(sizeof(char)*1023, "MatrixContainer::ParseMatrix: *EnergyHeader"); | 
|---|
|  | 109 |  | 
|---|
|  | 110 | // count the number of matrices | 
|---|
|  | 111 | MatrixCounter = -1; // we count one too much | 
|---|
|  | 112 | line << name << FactorsFile; | 
|---|
|  | 113 | input.open(line.str().c_str(), ios::in); | 
|---|
|  | 114 | if (input == NULL) { | 
|---|
|  | 115 | cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl; | 
|---|
|  | 116 | return false; | 
|---|
|  | 117 | } | 
|---|
|  | 118 | while (!input.eof()) { | 
|---|
|  | 119 | input >> tmp; | 
|---|
|  | 120 | MatrixCounter++; | 
|---|
|  | 121 | } | 
|---|
|  | 122 | input.close(); | 
|---|
|  | 123 | cout << "Determined " << MatrixCounter << " fragments." << endl; | 
|---|
|  | 124 |  | 
|---|
|  | 125 | cout << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl; | 
|---|
|  | 126 | Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: ***Matrix"); // one more each for the total molecule | 
|---|
|  | 127 | RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: *RowCounter"); | 
|---|
|  | 128 | for(int i=0;i<=MatrixCounter;i++) { | 
|---|
|  | 129 | // energy | 
|---|
|  | 130 | stringstream line; | 
|---|
|  | 131 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
|  | 132 | if (i != MatrixCounter) | 
|---|
|  | 133 | line << name << FragmentPrefix << FragmentNumber << "/" << prefix << suffix; | 
|---|
|  | 134 | else | 
|---|
|  | 135 | line << name << prefix << suffix; | 
|---|
|  | 136 | Free((void **)&FragmentNumber, "MatrixContainer::ParseMatrix: *FragmentNumber"); | 
|---|
|  | 137 | input.open(line.str().c_str(), ios::in); | 
|---|
|  | 138 | //cout << "Opening " << line.str() << " ... "  << input << endl; | 
|---|
|  | 139 | if (input == NULL) { | 
|---|
|  | 140 | cerr << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl; | 
|---|
|  | 141 | return false; | 
|---|
|  | 142 | } | 
|---|
|  | 143 | // scan header for number of columns | 
|---|
|  | 144 | for (int m=0;m<=skiplines;m++) | 
|---|
|  | 145 | input.getline(Header, 1023); | 
|---|
|  | 146 | line.str(Header); | 
|---|
|  | 147 | for(int k=0;k<skipcolumns;k++) | 
|---|
|  | 148 | line >> Header; | 
|---|
|  | 149 | //cout << line.str() << endl; | 
|---|
|  | 150 | ColumnCounter=0; | 
|---|
|  | 151 | while (!line.eof()) { | 
|---|
|  | 152 | strcpy(filename,"@"); | 
|---|
|  | 153 | line >> filename; | 
|---|
|  | 154 | if (filename[0] != '@') | 
|---|
|  | 155 | ColumnCounter++; | 
|---|
|  | 156 | } | 
|---|
|  | 157 | //cout << line.str() << endl; | 
|---|
|  | 158 | //cout << "ColumnCounter: " << ColumnCounter << endl; | 
|---|
|  | 159 | // scan rest for number of rows/lines | 
|---|
|  | 160 | RowCounter[i]=-1;    // counts one line too much | 
|---|
|  | 161 | while (!input.eof()) { | 
|---|
|  | 162 | input.getline(filename, 1023); | 
|---|
|  | 163 | //cout << "Comparing: " << strncmp(filename,"MeanForce",9) << endl; | 
|---|
|  | 164 | RowCounter[i]++; // then line was not last MeanForce | 
|---|
|  | 165 | if (strncmp(filename,"MeanForce", 9) == 0) { | 
|---|
|  | 166 | break; | 
|---|
|  | 167 | } | 
|---|
|  | 168 | } | 
|---|
|  | 169 | cout << "RowCounter[" << i << "]: " << RowCounter[i] << endl; | 
|---|
|  | 170 | Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]"); | 
|---|
|  | 171 | input.clear(); | 
|---|
|  | 172 | input.seekg(ios::beg); | 
|---|
|  | 173 | for (int m=0;m<=skiplines;m++) | 
|---|
|  | 174 | input.getline(Header, 1023);    // skip header | 
|---|
|  | 175 | line.str(Header); | 
|---|
|  | 176 | for(int k=0;k<skipcolumns;k++)  // skip columns in header too | 
|---|
|  | 177 | line >> filename; | 
|---|
|  | 178 | strncpy(Header, line.str().c_str(), 1023); | 
|---|
|  | 179 | for(int j=0;j<RowCounter[i];j++) { | 
|---|
|  | 180 | Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]"); | 
|---|
|  | 181 | input.getline(filename, 1023); | 
|---|
|  | 182 | stringstream line(filename); | 
|---|
|  | 183 | //cout << "Matrix at level " << j << ":";// << filename << endl; | 
|---|
|  | 184 | for(int k=0;k<skipcolumns;k++) | 
|---|
|  | 185 | line >> filename; | 
|---|
|  | 186 | for(int k=0;(k<ColumnCounter) && (!line.eof());k++) { | 
|---|
|  | 187 | line >> Matrix[i][j][k]; | 
|---|
|  | 188 | //cout << " " << setprecision(2) << Matrix[i][j][k];; | 
|---|
|  | 189 | } | 
|---|
|  | 190 | //cout << endl; | 
|---|
|  | 191 | } | 
|---|
|  | 192 | Matrix[i][ RowCounter[i] ] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[RowCounter[i]][]"); | 
|---|
|  | 193 | for(int j=0;j<ColumnCounter;j++) | 
|---|
|  | 194 | Matrix[i][ RowCounter[i] ][j] = 0.; | 
|---|
|  | 195 | input.close(); | 
|---|
|  | 196 | } | 
|---|
|  | 197 | return true; | 
|---|
|  | 198 | }; | 
|---|
|  | 199 |  | 
|---|
|  | 200 | /** Allocates and resets the memory for a number \a MCounter of matrices. | 
|---|
|  | 201 | * \param *GivenHeader Header line | 
|---|
|  | 202 | * \param MCounter number of matrices | 
|---|
|  | 203 | * \param *RCounter number of rows for each matrix | 
|---|
|  | 204 | * \param CCounter number of columns for all matrices | 
|---|
|  | 205 | * \return Allocation successful | 
|---|
|  | 206 | */ | 
|---|
|  | 207 | bool MatrixContainer::AllocateMatrix(char *GivenHeader, int MCounter, int *RCounter, int CCounter) | 
|---|
|  | 208 | { | 
|---|
|  | 209 | Header = (char *) Malloc(sizeof(char)*1024, "MatrixContainer::ParseMatrix: *EnergyHeader"); | 
|---|
|  | 210 | strncpy(Header, GivenHeader, 1023); | 
|---|
|  | 211 |  | 
|---|
|  | 212 | MatrixCounter = MCounter; | 
|---|
|  | 213 | ColumnCounter = CCounter; | 
|---|
|  | 214 | Matrix = (double ***) Malloc(sizeof(double **)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: ***Matrix"); // one more each for the total molecule | 
|---|
|  | 215 | RowCounter = (int *) Malloc(sizeof(int)*(MatrixCounter+1), "MatrixContainer::ParseMatrix: *RowCounter"); | 
|---|
|  | 216 | for(int i=0;i<=MatrixCounter;i++) { | 
|---|
|  | 217 | RowCounter[i] = RCounter[i]; | 
|---|
|  | 218 | Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]"); | 
|---|
|  | 219 | for(int j=0;j<=RowCounter[i];j++) { | 
|---|
|  | 220 | Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]"); | 
|---|
|  | 221 | for(int k=0;k<ColumnCounter;k++) | 
|---|
|  | 222 | Matrix[i][j][k] = 0.; | 
|---|
|  | 223 | } | 
|---|
|  | 224 | } | 
|---|
|  | 225 | return true; | 
|---|
|  | 226 | }; | 
|---|
|  | 227 |  | 
|---|
|  | 228 | /** Resets all values in MatrixContainer::Matrix. | 
|---|
|  | 229 | * \return true if successful | 
|---|
|  | 230 | */ | 
|---|
|  | 231 | bool MatrixContainer::ResetMatrix() | 
|---|
|  | 232 | { | 
|---|
|  | 233 | for(int i=0;i<=MatrixCounter;i++) | 
|---|
|  | 234 | for(int j=0;j<=RowCounter[i];j++) | 
|---|
|  | 235 | for(int k=0;k<ColumnCounter;k++) | 
|---|
|  | 236 | Matrix[i][j][k] = 0.; | 
|---|
|  | 237 | return true; | 
|---|
|  | 238 | }; | 
|---|
|  | 239 |  | 
|---|
|  | 240 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
|  | 241 | * \param value reset value | 
|---|
|  | 242 | * \param skipcolumns skip initial columns | 
|---|
|  | 243 | * \return true if successful | 
|---|
|  | 244 | */ | 
|---|
|  | 245 | bool MatrixContainer::SetLastMatrix(double value, int skipcolumns) | 
|---|
|  | 246 | { | 
|---|
|  | 247 | for(int j=0;j<=RowCounter[MatrixCounter];j++) | 
|---|
|  | 248 | for(int k=skipcolumns;k<ColumnCounter;k++) | 
|---|
|  | 249 | Matrix[MatrixCounter][j][k] = value; | 
|---|
|  | 250 | return true; | 
|---|
|  | 251 | }; | 
|---|
|  | 252 |  | 
|---|
|  | 253 | /** Sets all values in the last of MatrixContainer::Matrix to \a value. | 
|---|
|  | 254 | * \param **values matrix with each value (must have at least same dimensions!) | 
|---|
|  | 255 | * \param skipcolumns skip initial columns | 
|---|
|  | 256 | * \return true if successful | 
|---|
|  | 257 | */ | 
|---|
|  | 258 | bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns) | 
|---|
|  | 259 | { | 
|---|
|  | 260 | for(int j=0;j<=RowCounter[MatrixCounter];j++) | 
|---|
|  | 261 | for(int k=skipcolumns;k<ColumnCounter;k++) | 
|---|
|  | 262 | Matrix[MatrixCounter][j][k] = values[j][k]; | 
|---|
|  | 263 | return true; | 
|---|
|  | 264 | }; | 
|---|
|  | 265 |  | 
|---|
|  | 266 | /** Sums the energy with each factor and put into last element of \a ***Energies. | 
|---|
|  | 267 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. | 
|---|
|  | 268 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
|  | 269 | * \param Order bond order | 
|---|
|  | 270 | * \return true if summing was successful | 
|---|
|  | 271 | */ | 
|---|
|  | 272 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order) | 
|---|
|  | 273 | { | 
|---|
|  | 274 | // go through each order | 
|---|
|  | 275 | for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) { | 
|---|
|  | 276 | //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl; | 
|---|
|  | 277 | // then go per order through each suborder and pick together all the terms that contain this fragment | 
|---|
|  | 278 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order | 
|---|
|  | 279 | for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder | 
|---|
|  | 280 | if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) { | 
|---|
|  | 281 | //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl; | 
|---|
|  | 282 | // if the fragment's indices are all in the current fragment | 
|---|
|  | 283 | for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment | 
|---|
|  | 284 | int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k]; | 
|---|
|  | 285 | //cout << "Current index is " << k << "/" << m << "." << endl; | 
|---|
|  | 286 | if (m != -1) { // if it's not an added hydrogen | 
|---|
|  | 287 | for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment | 
|---|
|  | 288 | //cout << "Comparing " << m << " with " << Matrix.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl; | 
|---|
|  | 289 | if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) { | 
|---|
|  | 290 | m = l; | 
|---|
|  | 291 | break; | 
|---|
|  | 292 | } | 
|---|
|  | 293 | } | 
|---|
|  | 294 | //cout << "Corresponding index in CurrentFragment is " << m << "." << endl; | 
|---|
|  | 295 | if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) { | 
|---|
|  | 296 | cerr << "Current force index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl; | 
|---|
|  | 297 | return false; | 
|---|
|  | 298 | } | 
|---|
|  | 299 | if (Order == SubOrder) { // equal order is always copy from Energies | 
|---|
|  | 300 | for(int l=0;l<ColumnCounter;l++) // then adds/subtract each column | 
|---|
|  | 301 | Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; | 
|---|
|  | 302 | } else { | 
|---|
|  | 303 | for(int l=0;l<ColumnCounter;l++) | 
|---|
|  | 304 | Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l]; | 
|---|
|  | 305 | } | 
|---|
|  | 306 | } | 
|---|
|  | 307 | //if ((ColumnCounter>1) && (RowCounter >= 1)) | 
|---|
|  | 308 | //cout << "Fragments[ KeySet.OrderSet[" << i << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " <<  Fragments[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl; | 
|---|
|  | 309 | } | 
|---|
|  | 310 | } else { | 
|---|
|  | 311 | //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet. KeySet.OrderSet[Order][CurrentFragment] << "." << endl; | 
|---|
|  | 312 | } | 
|---|
|  | 313 | } | 
|---|
|  | 314 | } | 
|---|
|  | 315 | //cout << "Final Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << KeySet.AtomCounter[0]-1 << "][" << 1 << "] = " <<  Fragments[ KeySet.OrderSet[Order][CurrentFragment] ][KeySet.AtomCounter[0]-1][1] << endl; | 
|---|
|  | 316 | } | 
|---|
|  | 317 |  | 
|---|
|  | 318 | return true; | 
|---|
|  | 319 | }; | 
|---|
|  | 320 |  | 
|---|
|  | 321 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file. | 
|---|
|  | 322 | * \param *name inputdir | 
|---|
|  | 323 | * \param *prefix prefix before \a EnergySuffix | 
|---|
|  | 324 | * \return file was written | 
|---|
|  | 325 | */ | 
|---|
|  | 326 | bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix) | 
|---|
|  | 327 | { | 
|---|
|  | 328 | ofstream output; | 
|---|
|  | 329 | char *FragmentNumber = NULL; | 
|---|
|  | 330 |  | 
|---|
|  | 331 | cout << "Writing fragment files." << endl; | 
|---|
|  | 332 | for(int i=0;i<MatrixCounter;i++) { | 
|---|
|  | 333 | stringstream line; | 
|---|
|  | 334 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
|  | 335 | line << name << FragmentPrefix << FragmentNumber << "/" << prefix; | 
|---|
|  | 336 | Free((void **)&FragmentNumber, "*FragmentNumber"); | 
|---|
|  | 337 | output.open(line.str().c_str(), ios::out); | 
|---|
|  | 338 | if (output == NULL) { | 
|---|
|  | 339 | cerr << "Unable to open output energy file " << line.str() << "!" << endl; | 
|---|
|  | 340 | return false; | 
|---|
|  | 341 | } | 
|---|
|  | 342 | output << Header << endl; | 
|---|
|  | 343 | for(int j=0;j<RowCounter[i];j++) { | 
|---|
|  | 344 | for(int k=0;k<ColumnCounter;k++) | 
|---|
|  | 345 | output << scientific << Matrix[i][j][k] << "\t"; | 
|---|
|  | 346 | output << endl; | 
|---|
|  | 347 | } | 
|---|
|  | 348 | output.close(); | 
|---|
|  | 349 | } | 
|---|
|  | 350 | return true; | 
|---|
|  | 351 | }; | 
|---|
|  | 352 |  | 
|---|
|  | 353 | /** Writes the summed total values in the last matrix to file. | 
|---|
|  | 354 | * \param *name inputdir | 
|---|
|  | 355 | * \param *prefix prefix | 
|---|
|  | 356 | * \param *suffix suffix | 
|---|
|  | 357 | * \return file was written | 
|---|
|  | 358 | */ | 
|---|
|  | 359 | bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix) | 
|---|
|  | 360 | { | 
|---|
|  | 361 | ofstream output; | 
|---|
|  | 362 | stringstream line; | 
|---|
|  | 363 |  | 
|---|
|  | 364 | cout << "Writing matrix values of " << suffix << "." << endl; | 
|---|
|  | 365 | line << name << prefix << suffix; | 
|---|
|  | 366 | output.open(line.str().c_str(), ios::out); | 
|---|
|  | 367 | if (output == NULL) { | 
|---|
|  | 368 | cerr << "Unable to open output matrix file " << line.str() << "!" << endl; | 
|---|
|  | 369 | return false; | 
|---|
|  | 370 | } | 
|---|
|  | 371 | output << Header << endl; | 
|---|
|  | 372 | for(int j=0;j<RowCounter[MatrixCounter];j++) { | 
|---|
|  | 373 | for(int k=0;k<ColumnCounter;k++) | 
|---|
|  | 374 | output << scientific << Matrix[MatrixCounter][j][k] << "\t"; | 
|---|
|  | 375 | output << endl; | 
|---|
|  | 376 | } | 
|---|
|  | 377 | output.close(); | 
|---|
|  | 378 | return true; | 
|---|
|  | 379 | }; | 
|---|
|  | 380 |  | 
|---|
|  | 381 | // ======================================= CLASS EnergyMatrix ============================= | 
|---|
|  | 382 |  | 
|---|
|  | 383 | /** Create a trivial energy index mapping. | 
|---|
|  | 384 | * This just maps 1 to 1, 2 to 2 and so on for all fragments. | 
|---|
|  | 385 | * \return creation sucessful | 
|---|
|  | 386 | */ | 
|---|
|  | 387 | bool EnergyMatrix::ParseIndices() | 
|---|
|  | 388 | { | 
|---|
|  | 389 | cout << "Parsing energy indices." << endl; | 
|---|
|  | 390 | Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices"); | 
|---|
|  | 391 | for(int i=0;i<=MatrixCounter;i++) { | 
|---|
|  | 392 | Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]"); | 
|---|
|  | 393 | for(int j=0;j<RowCounter[i];j++) | 
|---|
|  | 394 | Indices[i][j] = j; | 
|---|
|  | 395 | } | 
|---|
|  | 396 | return true; | 
|---|
|  | 397 | }; | 
|---|
|  | 398 |  | 
|---|
|  | 399 | /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix. | 
|---|
|  | 400 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. | 
|---|
|  | 401 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
|  | 402 | * \param Order bond order | 
|---|
|  | 403 | * \return true if summing was successful | 
|---|
|  | 404 | */ | 
|---|
|  | 405 | bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class KeySetsContainer &KeySet, int Order) | 
|---|
|  | 406 | { | 
|---|
|  | 407 | // sum energy | 
|---|
|  | 408 | for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) | 
|---|
|  | 409 | for(int j=0;j<RowCounter[ KeySet.OrderSet[Order][i] ];j++) | 
|---|
|  | 410 | for(int k=0;k<ColumnCounter;k++) | 
|---|
|  | 411 | Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k]; | 
|---|
|  | 412 | return true; | 
|---|
|  | 413 | }; | 
|---|
|  | 414 |  | 
|---|
|  | 415 | // ======================================= CLASS ForceMatrix ============================= | 
|---|
|  | 416 |  | 
|---|
|  | 417 | /** Parsing force Indices of each fragment | 
|---|
|  | 418 | * \param *name directory with \a ForcesFile | 
|---|
|  | 419 | * \return parsing successful | 
|---|
|  | 420 | */ | 
|---|
|  | 421 | bool ForceMatrix::ParseIndices(char *name) | 
|---|
|  | 422 | { | 
|---|
|  | 423 | ifstream input; | 
|---|
|  | 424 | char *FragmentNumber = NULL; | 
|---|
|  | 425 | char filename[1023]; | 
|---|
|  | 426 | stringstream line; | 
|---|
|  | 427 |  | 
|---|
|  | 428 | cout << "Parsing force indices." << endl; | 
|---|
|  | 429 | Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "ForceMatrix::ParseIndices: **Indices"); | 
|---|
|  | 430 | line << name << ForcesFile; | 
|---|
|  | 431 | input.open(line.str().c_str(), ios::in); | 
|---|
|  | 432 | //cout << "Opening " << line.str() << " ... "  << input << endl; | 
|---|
|  | 433 | if (input == NULL) { | 
|---|
|  | 434 | cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl; | 
|---|
|  | 435 | return false; | 
|---|
|  | 436 | } | 
|---|
|  | 437 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) { | 
|---|
|  | 438 | // get the number of atoms for this fragment | 
|---|
|  | 439 | stringstream line; | 
|---|
|  | 440 | input.getline(filename, 1023); | 
|---|
|  | 441 | line.str(filename); | 
|---|
|  | 442 | // parse the values | 
|---|
|  | 443 | Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]"); | 
|---|
|  | 444 | FragmentNumber = FixedDigitNumber(MatrixCounter, i); | 
|---|
|  | 445 | cout << FragmentPrefix << FragmentNumber << "[" << RowCounter[i] << "]:"; | 
|---|
|  | 446 | Free((void **)&FragmentNumber, "ForceMatrix::ParseIndices: *FragmentNumber"); | 
|---|
|  | 447 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) { | 
|---|
|  | 448 | line >> Indices[i][j]; | 
|---|
|  | 449 | cout << " " << Indices[i][j]; | 
|---|
|  | 450 | } | 
|---|
|  | 451 | cout << endl; | 
|---|
|  | 452 | } | 
|---|
|  | 453 | Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]"); | 
|---|
|  | 454 | for(int j=0;j<RowCounter[MatrixCounter];j++) { | 
|---|
|  | 455 | Indices[MatrixCounter][j] = j; | 
|---|
|  | 456 | } | 
|---|
|  | 457 | input.close(); | 
|---|
|  | 458 | return true; | 
|---|
|  | 459 | }; | 
|---|
|  | 460 |  | 
|---|
|  | 461 |  | 
|---|
|  | 462 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix. | 
|---|
|  | 463 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies. | 
|---|
|  | 464 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order | 
|---|
|  | 465 | * \param Order bond order | 
|---|
|  | 466 | * \return true if summing was successful | 
|---|
|  | 467 | */ | 
|---|
|  | 468 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySet, int Order) | 
|---|
|  | 469 | { | 
|---|
|  | 470 | // sum forces | 
|---|
|  | 471 | for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++) | 
|---|
|  | 472 | for(int l=0;l<RowCounter[ KeySet.OrderSet[Order][i] ];l++) { | 
|---|
|  | 473 | int j = Indices[ KeySet.OrderSet[Order][i] ][l]; | 
|---|
|  | 474 | if (j > RowCounter[MatrixCounter]) { | 
|---|
|  | 475 | cerr << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl; | 
|---|
|  | 476 | return false; | 
|---|
|  | 477 | } | 
|---|
|  | 478 | if (j != -1) | 
|---|
|  | 479 | for(int k=2;k<ColumnCounter;k++) | 
|---|
|  | 480 | Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][l][k]; | 
|---|
|  | 481 | } | 
|---|
|  | 482 | return true; | 
|---|
|  | 483 | }; | 
|---|
|  | 484 |  | 
|---|
|  | 485 |  | 
|---|
|  | 486 | // ======================================= CLASS KeySetsContainer ============================= | 
|---|
|  | 487 |  | 
|---|
|  | 488 | /** Constructor of KeySetsContainer class. | 
|---|
|  | 489 | */ | 
|---|
|  | 490 | KeySetsContainer::KeySetsContainer() { | 
|---|
|  | 491 | KeySets = NULL; | 
|---|
|  | 492 | AtomCounter = NULL; | 
|---|
|  | 493 | FragmentCounter = 0; | 
|---|
|  | 494 | Order = 0; | 
|---|
|  | 495 | FragmentsPerOrder = 0; | 
|---|
|  | 496 | OrderSet = NULL; | 
|---|
|  | 497 | }; | 
|---|
|  | 498 |  | 
|---|
|  | 499 | /** Destructor of KeySetsContainer class. | 
|---|
|  | 500 | */ | 
|---|
|  | 501 | KeySetsContainer::~KeySetsContainer() { | 
|---|
|  | 502 | for(int i=0;i<FragmentCounter;i++) | 
|---|
|  | 503 | Free((void **)&KeySets[i], "KeySetsContainer::~KeySetsContainer: *KeySets[]"); | 
|---|
|  | 504 | for(int i=0;i<Order;i++) | 
|---|
|  | 505 | Free((void **)&OrderSet[i], "KeySetsContainer::~KeySetsContainer: *OrderSet[]"); | 
|---|
|  | 506 | Free((void **)&KeySets, "KeySetsContainer::~KeySetsContainer: **KeySets"); | 
|---|
|  | 507 | Free((void **)&OrderSet, "KeySetsContainer::~KeySetsContainer: **OrderSet"); | 
|---|
|  | 508 | Free((void **)&AtomCounter, "KeySetsContainer::~KeySetsContainer: *AtomCounter"); | 
|---|
|  | 509 | Free((void **)&FragmentsPerOrder, "KeySetsContainer::~KeySetsContainer: *FragmentsPerOrder"); | 
|---|
|  | 510 | }; | 
|---|
|  | 511 |  | 
|---|
|  | 512 | /** Parsing KeySets into array. | 
|---|
|  | 513 | * \param *name directory with keyset file | 
|---|
|  | 514 | * \param *ACounter number of atoms per fragment | 
|---|
|  | 515 | * \param FCounter number of fragments | 
|---|
|  | 516 | * \return parsing succesful | 
|---|
|  | 517 | */ | 
|---|
|  | 518 | bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) { | 
|---|
|  | 519 | ifstream input; | 
|---|
|  | 520 | char *FragmentNumber = NULL; | 
|---|
|  | 521 | stringstream line; | 
|---|
|  | 522 | char filename[1023]; | 
|---|
|  | 523 |  | 
|---|
|  | 524 | FragmentCounter = FCounter; | 
|---|
|  | 525 | cout << "Parsing key sets." << endl; | 
|---|
|  | 526 | KeySets = (int **) Malloc(sizeof(int *)*FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets"); | 
|---|
|  | 527 | line << name << KeySetsFile; | 
|---|
|  | 528 | input.open(line.str().c_str(), ios::in); | 
|---|
|  | 529 | if (input == NULL) { | 
|---|
|  | 530 | cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl; | 
|---|
|  | 531 | return false; | 
|---|
|  | 532 | } | 
|---|
|  | 533 |  | 
|---|
|  | 534 | AtomCounter = (int *) Malloc(sizeof(int)*FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter"); | 
|---|
|  | 535 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) { | 
|---|
|  | 536 | stringstream line; | 
|---|
|  | 537 | AtomCounter[i] = ACounter[i]; | 
|---|
|  | 538 | // parse the values | 
|---|
|  | 539 | KeySets[i] = (int *) Malloc(sizeof(int)*AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]"); | 
|---|
|  | 540 | FragmentNumber = FixedDigitNumber(FragmentCounter, i); | 
|---|
|  | 541 | cout << FragmentPrefix << FragmentNumber << "[" << AtomCounter[i] << "]:"; | 
|---|
|  | 542 | Free((void **)&FragmentNumber, "KeySetsContainer::ParseKeySets: *FragmentNumber"); | 
|---|
|  | 543 | input.getline(filename, 1023); | 
|---|
|  | 544 | line.str(filename); | 
|---|
|  | 545 | for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) { | 
|---|
|  | 546 | line >> KeySets[i][j]; | 
|---|
|  | 547 | cout << " " << KeySets[i][j]; | 
|---|
|  | 548 | } | 
|---|
|  | 549 | cout << endl; | 
|---|
|  | 550 | } | 
|---|
|  | 551 | input.close(); | 
|---|
|  | 552 | return true; | 
|---|
|  | 553 | }; | 
|---|
|  | 554 |  | 
|---|
|  | 555 | /** Parse many body terms, associating each fragment to a certain bond order. | 
|---|
|  | 556 | * \return parsing succesful | 
|---|
|  | 557 | */ | 
|---|
|  | 558 | bool KeySetsContainer::ParseManyBodyTerms() | 
|---|
|  | 559 | { | 
|---|
|  | 560 | int Counter; | 
|---|
|  | 561 |  | 
|---|
|  | 562 | cout << "Creating Fragment terms." << endl; | 
|---|
|  | 563 | // scan through all to determine order | 
|---|
|  | 564 | Order=0; | 
|---|
|  | 565 | for(int i=0;i<FragmentCounter;i++) { | 
|---|
|  | 566 | Counter=0; | 
|---|
|  | 567 | for(int j=0;j<AtomCounter[i];j++) | 
|---|
|  | 568 | if (KeySets[i][j] != -1) | 
|---|
|  | 569 | Counter++; | 
|---|
|  | 570 | if (Counter > Order) | 
|---|
|  | 571 | Order = Counter; | 
|---|
|  | 572 | } | 
|---|
|  | 573 | cout << "Found Order is " << Order << "." << endl; | 
|---|
|  | 574 |  | 
|---|
|  | 575 | // scan through all to determine fragments per order | 
|---|
|  | 576 | FragmentsPerOrder = (int *) Malloc(sizeof(int)*Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder"); | 
|---|
|  | 577 | for(int i=0;i<Order;i++) | 
|---|
|  | 578 | FragmentsPerOrder[i] = 0; | 
|---|
|  | 579 | for(int i=0;i<FragmentCounter;i++) { | 
|---|
|  | 580 | Counter=0; | 
|---|
|  | 581 | for(int j=0;j<AtomCounter[i];j++) | 
|---|
|  | 582 | if (KeySets[i][j] != -1) | 
|---|
|  | 583 | Counter++; | 
|---|
|  | 584 | FragmentsPerOrder[Counter-1]++; | 
|---|
|  | 585 | } | 
|---|
|  | 586 | for(int i=0;i<Order;i++) | 
|---|
|  | 587 | cout << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl; | 
|---|
|  | 588 |  | 
|---|
|  | 589 | // scan through all to gather indices to each order set | 
|---|
|  | 590 | OrderSet = (int **) Malloc(sizeof(int *)*Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet"); | 
|---|
|  | 591 | for(int i=0;i<Order;i++) | 
|---|
|  | 592 | OrderSet[i] = (int *) Malloc(sizeof(int)*FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]"); | 
|---|
|  | 593 | for(int i=0;i<Order;i++) | 
|---|
|  | 594 | FragmentsPerOrder[i] = 0; | 
|---|
|  | 595 | for(int i=0;i<FragmentCounter;i++) { | 
|---|
|  | 596 | Counter=0; | 
|---|
|  | 597 | for(int j=0;j<AtomCounter[i];j++) | 
|---|
|  | 598 | if (KeySets[i][j] != -1) | 
|---|
|  | 599 | Counter++; | 
|---|
|  | 600 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i; | 
|---|
|  | 601 | FragmentsPerOrder[Counter-1]++; | 
|---|
|  | 602 | } | 
|---|
|  | 603 | cout << "Printing OrderSet." << endl; | 
|---|
|  | 604 | for(int i=0;i<Order;i++) { | 
|---|
|  | 605 | for (int j=0;j<FragmentsPerOrder[i];j++) { | 
|---|
|  | 606 | cout << " " << OrderSet[i][j]; | 
|---|
|  | 607 | } | 
|---|
|  | 608 | cout << endl; | 
|---|
|  | 609 | } | 
|---|
|  | 610 | cout << endl; | 
|---|
|  | 611 |  | 
|---|
|  | 612 |  | 
|---|
|  | 613 | return true; | 
|---|
|  | 614 | }; | 
|---|
|  | 615 |  | 
|---|
|  | 616 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet. | 
|---|
|  | 617 | * \param GreaterSet index to greater set | 
|---|
|  | 618 | * \param SmallerSet index to smaller set | 
|---|
|  | 619 | * \return true if all keys of SmallerSet contained in GreaterSet | 
|---|
|  | 620 | */ | 
|---|
|  | 621 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet) | 
|---|
|  | 622 | { | 
|---|
|  | 623 | bool result = true; | 
|---|
|  | 624 | bool intermediate; | 
|---|
|  | 625 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds | 
|---|
|  | 626 | return false; | 
|---|
|  | 627 | for(int i=0;i<AtomCounter[SmallerSet];i++) { | 
|---|
|  | 628 | intermediate = false; | 
|---|
|  | 629 | for (int j=0;j<AtomCounter[GreaterSet];j++) | 
|---|
|  | 630 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1))); | 
|---|
|  | 631 | result = result && intermediate; | 
|---|
|  | 632 | } | 
|---|
|  | 633 |  | 
|---|
|  | 634 | return result; | 
|---|
|  | 635 | }; | 
|---|
|  | 636 |  | 
|---|
|  | 637 |  | 
|---|
|  | 638 | // ======================================= END ============================================= | 
|---|