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
|
---|
19 | inline bool FilePresent(const char *filename)
|
---|
20 | #else
|
---|
21 | bool FilePresent(const char *filename)
|
---|
22 | #endif
|
---|
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 |
|
---|
45 | line << argv[1] << FRAGMENTPREFIX << KEYSETFILE;
|
---|
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() {
|
---|
65 | if (Matrix != NULL) {
|
---|
66 | for(int i=0;i<MatrixCounter;i++) {
|
---|
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 | }
|
---|
72 | }
|
---|
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 | }
|
---|
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
|
---|
109 | line << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
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()) {
|
---|
116 | input.getline(filename, 1023);
|
---|
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");
|
---|
125 | for(int i=0;i<=MatrixCounter;i++) {
|
---|
126 | Matrix[i] = NULL;
|
---|
127 | RowCounter[i] = -1;
|
---|
128 | }
|
---|
129 | for(int i=0;i<=MatrixCounter;i++) {
|
---|
130 | // open matrix file
|
---|
131 | stringstream line;
|
---|
132 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
133 | if (i != MatrixCounter)
|
---|
134 | line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix << suffix;
|
---|
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 | // skip some initial lines
|
---|
145 | for (int m=0;m<=skiplines;m++)
|
---|
146 | input.getline(Header, 1023);
|
---|
147 | // scan header for number of columns
|
---|
148 | line.str(Header);
|
---|
149 | for(int k=0;k<skipcolumns;k++)
|
---|
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);
|
---|
175 | for (int m=0;m<=skiplines;m++)
|
---|
176 | input.getline(Header, 1023); // skip header
|
---|
177 | line.str(Header);
|
---|
178 | for(int k=0;k<skipcolumns;k++) // skip columns in header too
|
---|
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);
|
---|
184 | stringstream line(filename);
|
---|
185 | //cout << "Matrix at level " << j << ":";// << filename << endl;
|
---|
186 | for(int k=0;k<skipcolumns;k++)
|
---|
187 | line >> filename;
|
---|
188 | for(int k=0;(k<ColumnCounter) && (!line.eof());k++) {
|
---|
189 | line >> Matrix[i][j][k];
|
---|
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]][]");
|
---|
195 | for(int j=0;j<ColumnCounter;j++)
|
---|
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");
|
---|
218 | for(int i=0;i<=MatrixCounter;i++) {
|
---|
219 | RowCounter[i] = RCounter[i];
|
---|
220 | Matrix[i] = (double **) Malloc(sizeof(double *)*(RowCounter[i]+1), "MatrixContainer::ParseMatrix: **Matrix[]");
|
---|
221 | for(int j=0;j<=RowCounter[i];j++) {
|
---|
222 | Matrix[i][j] = (double *) Malloc(sizeof(double)*ColumnCounter, "MatrixContainer::ParseMatrix: *Matrix[][]");
|
---|
223 | for(int k=0;k<ColumnCounter;k++)
|
---|
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 | {
|
---|
235 | for(int i=0;i<=MatrixCounter;i++)
|
---|
236 | for(int j=0;j<=RowCounter[i];j++)
|
---|
237 | for(int k=0;k<ColumnCounter;k++)
|
---|
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 | {
|
---|
249 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
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 | {
|
---|
262 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
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.
|
---|
269 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
|
---|
270 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
271 | * \param Order bond order
|
---|
272 | * \return true if summing was successful
|
---|
273 | */
|
---|
274 | bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySet, int Order)
|
---|
275 | {
|
---|
276 | // go through each order
|
---|
277 | for (int CurrentFragment=0;CurrentFragment<KeySet.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
278 | //cout << "Current Fragment is " << CurrentFragment << "/" << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
279 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
280 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
281 | for (int j=0;j<KeySet.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
282 | if (KeySet.Contains(KeySet.OrderSet[Order][CurrentFragment], KeySet.OrderSet[SubOrder][j])) {
|
---|
283 | //cout << "Current other fragment is " << j << "/" << KeySet.OrderSet[SubOrder][j] << "." << endl;
|
---|
284 | // if the fragment's indices are all in the current fragment
|
---|
285 | for(int k=0;k<RowCounter[ KeySet.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
286 | int m = MatrixValues.Indices[ KeySet.OrderSet[SubOrder][j] ][k];
|
---|
287 | //cout << "Current index is " << k << "/" << m << "." << endl;
|
---|
288 | if (m != -1) { // if it's not an added hydrogen
|
---|
289 | for (int l=0;l<RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
290 | //cout << "Comparing " << m << " with " << MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
|
---|
291 | if (m == MatrixValues.Indices[ KeySet.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
292 | m = l;
|
---|
293 | break;
|
---|
294 | }
|
---|
295 | }
|
---|
296 | //cout << "Corresponding index in CurrentFragment is " << m << "." << endl;
|
---|
297 | if (m > RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ]) {
|
---|
298 | cerr << "In fragment No. " << KeySet.OrderSet[Order][CurrentFragment] << " current force index " << m << " is greater than " << RowCounter[ KeySet.OrderSet[Order][CurrentFragment] ] << "!" << endl;
|
---|
299 | return false;
|
---|
300 | }
|
---|
301 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
302 | for(int l=0;l<ColumnCounter;l++) // then adds/subtract each column
|
---|
303 | Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
|
---|
304 | } else {
|
---|
305 | for(int l=0;l<ColumnCounter;l++)
|
---|
306 | Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySet.OrderSet[SubOrder][j] ][k][l];
|
---|
307 | }
|
---|
308 | }
|
---|
309 | //if ((ColumnCounter>1) && (RowCounter[0]-1 >= 1))
|
---|
310 | //cout << "Fragments[ KeySet.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySet.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySet.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
|
---|
311 | }
|
---|
312 | } else {
|
---|
313 | //cout << "Fragment " << KeySet.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySet.OrderSet[Order][CurrentFragment] << "." << endl;
|
---|
314 | }
|
---|
315 | }
|
---|
316 | }
|
---|
317 | //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;
|
---|
318 | }
|
---|
319 |
|
---|
320 | return true;
|
---|
321 | };
|
---|
322 |
|
---|
323 | /** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
|
---|
324 | * \param *name inputdir
|
---|
325 | * \param *prefix prefix before \a EnergySuffix
|
---|
326 | * \return file was written
|
---|
327 | */
|
---|
328 | bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix)
|
---|
329 | {
|
---|
330 | ofstream output;
|
---|
331 | char *FragmentNumber = NULL;
|
---|
332 |
|
---|
333 | cout << "Writing fragment files." << endl;
|
---|
334 | for(int i=0;i<MatrixCounter;i++) {
|
---|
335 | stringstream line;
|
---|
336 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
337 | line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
|
---|
338 | Free((void **)&FragmentNumber, "*FragmentNumber");
|
---|
339 | output.open(line.str().c_str(), ios::out);
|
---|
340 | if (output == NULL) {
|
---|
341 | cerr << "Unable to open output energy file " << line.str() << "!" << endl;
|
---|
342 | return false;
|
---|
343 | }
|
---|
344 | output << Header << endl;
|
---|
345 | for(int j=0;j<RowCounter[i];j++) {
|
---|
346 | for(int k=0;k<ColumnCounter;k++)
|
---|
347 | output << scientific << Matrix[i][j][k] << "\t";
|
---|
348 | output << endl;
|
---|
349 | }
|
---|
350 | output.close();
|
---|
351 | }
|
---|
352 | return true;
|
---|
353 | };
|
---|
354 |
|
---|
355 | /** Writes the summed total values in the last matrix to file.
|
---|
356 | * \param *name inputdir
|
---|
357 | * \param *prefix prefix
|
---|
358 | * \param *suffix suffix
|
---|
359 | * \return file was written
|
---|
360 | */
|
---|
361 | bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix)
|
---|
362 | {
|
---|
363 | ofstream output;
|
---|
364 | stringstream line;
|
---|
365 |
|
---|
366 | cout << "Writing matrix values of " << suffix << "." << endl;
|
---|
367 | line << name << prefix << suffix;
|
---|
368 | output.open(line.str().c_str(), ios::out);
|
---|
369 | if (output == NULL) {
|
---|
370 | cerr << "Unable to open output matrix file " << line.str() << "!" << endl;
|
---|
371 | return false;
|
---|
372 | }
|
---|
373 | output << Header << endl;
|
---|
374 | for(int j=0;j<RowCounter[MatrixCounter];j++) {
|
---|
375 | for(int k=0;k<ColumnCounter;k++)
|
---|
376 | output << scientific << Matrix[MatrixCounter][j][k] << "\t";
|
---|
377 | output << endl;
|
---|
378 | }
|
---|
379 | output.close();
|
---|
380 | return true;
|
---|
381 | };
|
---|
382 |
|
---|
383 | // ======================================= CLASS EnergyMatrix =============================
|
---|
384 |
|
---|
385 | /** Create a trivial energy index mapping.
|
---|
386 | * This just maps 1 to 1, 2 to 2 and so on for all fragments.
|
---|
387 | * \return creation sucessful
|
---|
388 | */
|
---|
389 | bool EnergyMatrix::ParseIndices()
|
---|
390 | {
|
---|
391 | cout << "Parsing energy indices." << endl;
|
---|
392 | Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "EnergyMatrix::ParseIndices: **Indices");
|
---|
393 | for(int i=0;i<=MatrixCounter;i++) {
|
---|
394 | Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "EnergyMatrix::ParseIndices: *Indices[]");
|
---|
395 | for(int j=0;j<RowCounter[i];j++)
|
---|
396 | Indices[i][j] = j;
|
---|
397 | }
|
---|
398 | return true;
|
---|
399 | };
|
---|
400 |
|
---|
401 | /** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
|
---|
402 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
|
---|
403 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
404 | * \param Order bond order
|
---|
405 | * \return true if summing was successful
|
---|
406 | */
|
---|
407 | bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class KeySetsContainer &KeySet, int Order)
|
---|
408 | {
|
---|
409 | // sum energy
|
---|
410 | for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++)
|
---|
411 | for(int j=0;j<RowCounter[ KeySet.OrderSet[Order][i] ];j++)
|
---|
412 | for(int k=0;k<ColumnCounter;k++)
|
---|
413 | Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][j][k];
|
---|
414 | return true;
|
---|
415 | };
|
---|
416 |
|
---|
417 | // ======================================= CLASS ForceMatrix =============================
|
---|
418 |
|
---|
419 | /** Parsing force Indices of each fragment
|
---|
420 | * \param *name directory with \a ForcesFile
|
---|
421 | * \return parsing successful
|
---|
422 | */
|
---|
423 | bool ForceMatrix::ParseIndices(char *name)
|
---|
424 | {
|
---|
425 | ifstream input;
|
---|
426 | char *FragmentNumber = NULL;
|
---|
427 | char filename[1023];
|
---|
428 | stringstream line;
|
---|
429 |
|
---|
430 | cout << "Parsing force indices." << endl;
|
---|
431 | Indices = (int **) Malloc(sizeof(int *)*(MatrixCounter+1), "ForceMatrix::ParseIndices: **Indices");
|
---|
432 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
433 | input.open(line.str().c_str(), ios::in);
|
---|
434 | //cout << "Opening " << line.str() << " ... " << input << endl;
|
---|
435 | if (input == NULL) {
|
---|
436 | cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
|
---|
437 | return false;
|
---|
438 | }
|
---|
439 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
440 | // get the number of atoms for this fragment
|
---|
441 | input.getline(filename, 1023);
|
---|
442 | line.str(filename);
|
---|
443 | // parse the values
|
---|
444 | Indices[i] = (int *) Malloc(sizeof(int)*RowCounter[i], "ForceMatrix::ParseIndices: *Indices[]");
|
---|
445 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
446 | cout << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
447 | Free((void **)&FragmentNumber, "ForceMatrix::ParseIndices: *FragmentNumber");
|
---|
448 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
449 | line >> Indices[i][j];
|
---|
450 | cout << " " << Indices[i][j];
|
---|
451 | }
|
---|
452 | cout << endl;
|
---|
453 | }
|
---|
454 | Indices[MatrixCounter] = (int *) Malloc(sizeof(int)*RowCounter[MatrixCounter], "ForceMatrix::ParseIndices: *Indices[]");
|
---|
455 | for(int j=0;j<RowCounter[MatrixCounter];j++) {
|
---|
456 | Indices[MatrixCounter][j] = j;
|
---|
457 | }
|
---|
458 | input.close();
|
---|
459 | return true;
|
---|
460 | };
|
---|
461 |
|
---|
462 |
|
---|
463 | /** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
|
---|
464 | * \param Matrix MatrixContainer with matrices (LevelCounter by ColumnCounter) with all the energies.
|
---|
465 | * \param KeySet KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
466 | * \param Order bond order
|
---|
467 | * \return true if summing was successful
|
---|
468 | */
|
---|
469 | bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySet, int Order)
|
---|
470 | {
|
---|
471 | // sum forces
|
---|
472 | for(int i=0;i<KeySet.FragmentsPerOrder[Order];i++)
|
---|
473 | for(int l=0;l<RowCounter[ KeySet.OrderSet[Order][i] ];l++) {
|
---|
474 | int j = Indices[ KeySet.OrderSet[Order][i] ][l];
|
---|
475 | if (j > RowCounter[MatrixCounter]) {
|
---|
476 | cerr << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl;
|
---|
477 | return false;
|
---|
478 | }
|
---|
479 | if (j != -1)
|
---|
480 | for(int k=2;k<ColumnCounter;k++)
|
---|
481 | Matrix[MatrixCounter][j][k] += Fragments.Matrix[ KeySet.OrderSet[Order][i] ][l][k];
|
---|
482 | }
|
---|
483 | return true;
|
---|
484 | };
|
---|
485 |
|
---|
486 |
|
---|
487 | // ======================================= CLASS KeySetsContainer =============================
|
---|
488 |
|
---|
489 | /** Constructor of KeySetsContainer class.
|
---|
490 | */
|
---|
491 | KeySetsContainer::KeySetsContainer() {
|
---|
492 | KeySets = NULL;
|
---|
493 | AtomCounter = NULL;
|
---|
494 | FragmentCounter = 0;
|
---|
495 | Order = 0;
|
---|
496 | FragmentsPerOrder = 0;
|
---|
497 | OrderSet = NULL;
|
---|
498 | };
|
---|
499 |
|
---|
500 | /** Destructor of KeySetsContainer class.
|
---|
501 | */
|
---|
502 | KeySetsContainer::~KeySetsContainer() {
|
---|
503 | for(int i=0;i<FragmentCounter;i++)
|
---|
504 | Free((void **)&KeySets[i], "KeySetsContainer::~KeySetsContainer: *KeySets[]");
|
---|
505 | for(int i=0;i<Order;i++)
|
---|
506 | Free((void **)&OrderSet[i], "KeySetsContainer::~KeySetsContainer: *OrderSet[]");
|
---|
507 | Free((void **)&KeySets, "KeySetsContainer::~KeySetsContainer: **KeySets");
|
---|
508 | Free((void **)&OrderSet, "KeySetsContainer::~KeySetsContainer: **OrderSet");
|
---|
509 | Free((void **)&AtomCounter, "KeySetsContainer::~KeySetsContainer: *AtomCounter");
|
---|
510 | Free((void **)&FragmentsPerOrder, "KeySetsContainer::~KeySetsContainer: *FragmentsPerOrder");
|
---|
511 | };
|
---|
512 |
|
---|
513 | /** Parsing KeySets into array.
|
---|
514 | * \param *name directory with keyset file
|
---|
515 | * \param *ACounter number of atoms per fragment
|
---|
516 | * \param FCounter number of fragments
|
---|
517 | * \return parsing succesful
|
---|
518 | */
|
---|
519 | bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
|
---|
520 | ifstream input;
|
---|
521 | char *FragmentNumber = NULL;
|
---|
522 | stringstream line;
|
---|
523 | char filename[1023];
|
---|
524 |
|
---|
525 | FragmentCounter = FCounter;
|
---|
526 | cout << "Parsing key sets." << endl;
|
---|
527 | KeySets = (int **) Malloc(sizeof(int *)*FragmentCounter, "KeySetsContainer::ParseKeySets: **KeySets");
|
---|
528 | for(int i=0;i<FragmentCounter;i++)
|
---|
529 | KeySets[i] = NULL;
|
---|
530 | line << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
531 | input.open(line.str().c_str(), ios::in);
|
---|
532 | if (input == NULL) {
|
---|
533 | cout << endl << "Unable to open " << line.str() << ", is the directory correct?" << endl;
|
---|
534 | return false;
|
---|
535 | }
|
---|
536 |
|
---|
537 | AtomCounter = (int *) Malloc(sizeof(int)*FragmentCounter, "KeySetsContainer::ParseKeySets: *RowCounter");
|
---|
538 | for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
|
---|
539 | stringstream line;
|
---|
540 | AtomCounter[i] = ACounter[i];
|
---|
541 | // parse the values
|
---|
542 | KeySets[i] = (int *) Malloc(sizeof(int)*AtomCounter[i], "KeySetsContainer::ParseKeySets: *KeySets[]");
|
---|
543 | for(int j=0;j<AtomCounter[i];j++)
|
---|
544 | KeySets[i][j] = -1;
|
---|
545 | FragmentNumber = FixedDigitNumber(FragmentCounter, i);
|
---|
546 | cout << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
|
---|
547 | Free((void **)&FragmentNumber, "KeySetsContainer::ParseKeySets: *FragmentNumber");
|
---|
548 | input.getline(filename, 1023);
|
---|
549 | line.str(filename);
|
---|
550 | for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
|
---|
551 | line >> KeySets[i][j];
|
---|
552 | cout << " " << KeySets[i][j];
|
---|
553 | }
|
---|
554 | cout << endl;
|
---|
555 | }
|
---|
556 | input.close();
|
---|
557 | return true;
|
---|
558 | };
|
---|
559 |
|
---|
560 | /** Parse many body terms, associating each fragment to a certain bond order.
|
---|
561 | * \return parsing succesful
|
---|
562 | */
|
---|
563 | bool KeySetsContainer::ParseManyBodyTerms()
|
---|
564 | {
|
---|
565 | int Counter;
|
---|
566 |
|
---|
567 | cout << "Creating Fragment terms." << endl;
|
---|
568 | // scan through all to determine order
|
---|
569 | Order=0;
|
---|
570 | for(int i=0;i<FragmentCounter;i++) {
|
---|
571 | Counter=0;
|
---|
572 | for(int j=0;j<AtomCounter[i];j++)
|
---|
573 | if (KeySets[i][j] != -1)
|
---|
574 | Counter++;
|
---|
575 | if (Counter > Order)
|
---|
576 | Order = Counter;
|
---|
577 | }
|
---|
578 | cout << "Found Order is " << Order << "." << endl;
|
---|
579 |
|
---|
580 | // scan through all to determine fragments per order
|
---|
581 | FragmentsPerOrder = (int *) Malloc(sizeof(int)*Order, "KeySetsContainer::ParseManyBodyTerms: *FragmentsPerOrder");
|
---|
582 | for(int i=0;i<Order;i++)
|
---|
583 | FragmentsPerOrder[i] = 0;
|
---|
584 | for(int i=0;i<FragmentCounter;i++) {
|
---|
585 | Counter=0;
|
---|
586 | for(int j=0;j<AtomCounter[i];j++)
|
---|
587 | if (KeySets[i][j] != -1)
|
---|
588 | Counter++;
|
---|
589 | FragmentsPerOrder[Counter-1]++;
|
---|
590 | }
|
---|
591 | for(int i=0;i<Order;i++)
|
---|
592 | cout << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl;
|
---|
593 |
|
---|
594 | // scan through all to gather indices to each order set
|
---|
595 | OrderSet = (int **) Malloc(sizeof(int *)*Order, "KeySetsContainer::ParseManyBodyTerms: **OrderSet");
|
---|
596 | for(int i=0;i<Order;i++)
|
---|
597 | OrderSet[i] = (int *) Malloc(sizeof(int)*FragmentsPerOrder[i], "KeySetsContainer::ParseManyBodyTermsKeySetsContainer::ParseManyBodyTerms: *OrderSet[]");
|
---|
598 | for(int i=0;i<Order;i++)
|
---|
599 | FragmentsPerOrder[i] = 0;
|
---|
600 | for(int i=0;i<FragmentCounter;i++) {
|
---|
601 | Counter=0;
|
---|
602 | for(int j=0;j<AtomCounter[i];j++)
|
---|
603 | if (KeySets[i][j] != -1)
|
---|
604 | Counter++;
|
---|
605 | OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
|
---|
606 | FragmentsPerOrder[Counter-1]++;
|
---|
607 | }
|
---|
608 | cout << "Printing OrderSet." << endl;
|
---|
609 | for(int i=0;i<Order;i++) {
|
---|
610 | for (int j=0;j<FragmentsPerOrder[i];j++) {
|
---|
611 | cout << " " << OrderSet[i][j];
|
---|
612 | }
|
---|
613 | cout << endl;
|
---|
614 | }
|
---|
615 | cout << endl;
|
---|
616 |
|
---|
617 |
|
---|
618 | return true;
|
---|
619 | };
|
---|
620 |
|
---|
621 | /** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
|
---|
622 | * \param GreaterSet index to greater set
|
---|
623 | * \param SmallerSet index to smaller set
|
---|
624 | * \return true if all keys of SmallerSet contained in GreaterSet
|
---|
625 | */
|
---|
626 | bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
|
---|
627 | {
|
---|
628 | bool result = true;
|
---|
629 | bool intermediate;
|
---|
630 | if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
|
---|
631 | return false;
|
---|
632 | for(int i=0;i<AtomCounter[SmallerSet];i++) {
|
---|
633 | intermediate = false;
|
---|
634 | for (int j=0;j<AtomCounter[GreaterSet];j++)
|
---|
635 | intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
|
---|
636 | result = result && intermediate;
|
---|
637 | }
|
---|
638 |
|
---|
639 | return result;
|
---|
640 | };
|
---|
641 |
|
---|
642 |
|
---|
643 | // ======================================= END =============================================
|
---|