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