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