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