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