source: src/parser.cpp@ 436f04

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since 436f04 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

  • Property mode set to 100755
File size: 45.7 KB
Line 
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/MemDebug.hpp"
10
11#include <cstring>
12
13#include "helpers.hpp"
14#include "memoryallocator.hpp"
15#include "parser.hpp"
16
17// include config.h
18#ifdef HAVE_CONFIG_H
19#include <config.h>
20#endif
21
22// ======================================= FUNCTIONS ==========================================
23
24/** Test if given filename can be opened.
25 * \param filename name of file
26 * \param test true - no error message, false - print error
27 * \return given file exists
28 */
29bool FilePresent(const char *filename, bool test)
30{
31 ifstream input;
32
33 input.open(filename, ios::in);
34 if (input == NULL) {
35 if (!test)
36 DoLog(0) && (Log() << Verbose(0) << endl << "FilePresent: Unable to open " << filename << ", is the directory correct?" << endl);
37 return false;
38 }
39 input.close();
40 return true;
41};
42
43/** Test the given parameters.
44 * \param argc argument count
45 * \param **argv arguments array
46 * \return given inputdir is valid
47 */
48bool TestParams(int argc, char **argv)
49{
50 ifstream input;
51 stringstream line;
52
53 line << argv[1] << FRAGMENTPREFIX << KEYSETFILE;
54 return FilePresent(line.str().c_str(), false);
55};
56
57// ======================================= CLASS MatrixContainer =============================
58
59/** Constructor of MatrixContainer class.
60 */
61MatrixContainer::MatrixContainer() {
62 Indices = NULL;
63 Header = new char*[1];
64 Matrix = new double**[1]; // one more each for the total molecule
65 RowCounter = new int[1];
66 ColumnCounter = new int[1];
67 Header[0] = NULL;
68 Matrix[0] = NULL;
69 RowCounter[0] = -1;
70 MatrixCounter = 0;
71 ColumnCounter[0] = -1;
72};
73
74/** Destructor of MatrixContainer class.
75 */
76MatrixContainer::~MatrixContainer() {
77 if (Matrix != NULL) {
78 for(int i=MatrixCounter;i--;) {
79 if ((ColumnCounter != NULL) && (RowCounter != NULL)) {
80 for(int j=RowCounter[i]+1;j--;)
81 delete[](Matrix[i][j]);
82 delete[](Matrix[i]);
83 }
84 }
85 if ((ColumnCounter != NULL) && (RowCounter != NULL) && (Matrix[MatrixCounter] != NULL))
86 for(int j=RowCounter[MatrixCounter]+1;j--;)
87 delete[](Matrix[MatrixCounter][j]);
88 if (MatrixCounter != 0)
89 delete[](Matrix[MatrixCounter]);
90 delete[](Matrix);
91 }
92 if (Indices != NULL)
93 for(int i=MatrixCounter+1;i--;) {
94 delete[](Indices[i]);
95 }
96 delete[](Indices);
97
98 if (Header != NULL)
99 for(int i=MatrixCounter+1;i--;)
100 delete[](Header[i]);
101 delete[](Header);
102 delete[](RowCounter);
103 delete[](ColumnCounter);
104};
105
106/** Either copies index matrix from another MatrixContainer or initialises with trivial mapping if NULL.
107 * This either copies the index matrix or just maps 1 to 1, 2 to 2 and so on for all fragments.
108 * \param *Matrix pointer to other MatrixContainer
109 * \return true - copy/initialisation sucessful, false - dimension false for copying
110 */
111bool MatrixContainer::InitialiseIndices(class MatrixContainer *Matrix)
112{
113 DoLog(0) && (Log() << Verbose(0) << "Initialising indices");
114 if (Matrix == NULL) {
115 DoLog(0) && (Log() << Verbose(0) << " with trivial mapping." << endl);
116 Indices = new int*[MatrixCounter + 1];
117 for(int i=MatrixCounter+1;i--;) {
118 Indices[i] = new int[RowCounter[i]];
119 for(int j=RowCounter[i];j--;)
120 Indices[i][j] = j;
121 }
122 } else {
123 DoLog(0) && (Log() << Verbose(0) << " from other MatrixContainer." << endl);
124 if (MatrixCounter != Matrix->MatrixCounter)
125 return false;
126 Indices = new int*[MatrixCounter + 1];
127 for(int i=MatrixCounter+1;i--;) {
128 if (RowCounter[i] != Matrix->RowCounter[i])
129 return false;
130 Indices[i] = new int[Matrix->RowCounter[i]];
131 for(int j=Matrix->RowCounter[i];j--;) {
132 Indices[i][j] = Matrix->Indices[i][j];
133 //Log() << Verbose(0) << Indices[i][j] << "\t";
134 }
135 //Log() << Verbose(0) << endl;
136 }
137 }
138 return true;
139};
140
141/** Parsing a number of matrices.
142 * -# open the matrix file
143 * -# skip some lines (\a skiplines)
144 * -# scan header lines for number of columns
145 * -# scan lines for number of rows
146 * -# allocate matrix
147 * -# loop over found column and row counts and parse in each entry
148 * \param *name directory with files
149 * \param skiplines number of inital lines to skip
150 * \param skiplines number of inital columns to skip
151 * \param MatrixNr index number in Matrix array to parse into
152 * \return parsing successful
153 */
154bool MatrixContainer::ParseMatrix(const char *name, int skiplines, int skipcolumns, int MatrixNr)
155{
156 ifstream input;
157 stringstream line;
158 string token;
159 char filename[1023];
160
161 input.open(name, ios::in);
162 //Log() << Verbose(1) << "Opening " << name << " ... " << input << endl;
163 if (input == NULL) {
164 DoeLog(1) && (eLog()<< Verbose(1) << endl << "MatrixContainer::ParseMatrix: Unable to open " << name << ", is the directory correct?" << endl);
165 //performCriticalExit();
166 return false;
167 }
168
169 // parse header
170 Header[MatrixNr] = new char[1024];
171 for (int m=skiplines+1;m--;)
172 input.getline(Header[MatrixNr], 1023);
173
174 // scan header for number of columns
175 line.str(Header[MatrixNr]);
176 for(int k=skipcolumns;k--;)
177 line >> Header[MatrixNr];
178 //Log() << Verbose(0) << line.str() << endl;
179 ColumnCounter[MatrixNr]=0;
180 while ( getline(line,token, '\t') ) {
181 if (token.length() > 0)
182 ColumnCounter[MatrixNr]++;
183 }
184 //Log() << Verbose(0) << line.str() << endl;
185 //Log() << Verbose(1) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << "." << endl;
186 if (ColumnCounter[MatrixNr] == 0) {
187 DoeLog(0) && (eLog()<< Verbose(0) << "ColumnCounter[" << MatrixNr << "]: " << ColumnCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl);
188 performCriticalExit();
189 }
190
191 // scan rest for number of rows/lines
192 RowCounter[MatrixNr]=-1; // counts one line too much
193 while (!input.eof()) {
194 input.getline(filename, 1023);
195 //Log() << Verbose(0) << "Comparing: " << strncmp(filename,"MeanForce",9) << endl;
196 RowCounter[MatrixNr]++; // then line was not last MeanForce
197 if (strncmp(filename,"MeanForce", 9) == 0) {
198 break;
199 }
200 }
201 //Log() << Verbose(1) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << "." << endl;
202 if (RowCounter[MatrixNr] == 0) {
203 DoeLog(0) && (eLog()<< Verbose(0) << "RowCounter[" << MatrixNr << "]: " << RowCounter[MatrixNr] << " from file " << name << ", this is probably an error!" << endl);
204 performCriticalExit();
205 }
206
207 // allocate matrix if it's not zero dimension in one direction
208 if ((ColumnCounter[MatrixNr] > 0) && (RowCounter[MatrixNr] > -1)) {
209 Matrix[MatrixNr] = new double*[RowCounter[MatrixNr] + 1];
210
211 // parse in each entry for this matrix
212 input.clear();
213 input.seekg(ios::beg);
214 for (int m=skiplines+1;m--;)
215 input.getline(Header[MatrixNr], 1023); // skip header
216 line.str(Header[MatrixNr]);
217 for(int k=skipcolumns;k--;) // skip columns in header too
218 line >> filename;
219 strncpy(Header[MatrixNr], line.str().c_str(), 1023);
220 for(int j=0;j<RowCounter[MatrixNr];j++) {
221 Matrix[MatrixNr][j] = new double[ColumnCounter[MatrixNr]];
222 input.getline(filename, 1023);
223 stringstream lines(filename);
224 //Log() << Verbose(2) << "Matrix at level " << j << ":";// << filename << endl;
225 for(int k=skipcolumns;k--;)
226 lines >> filename;
227 for(int k=0;(k<ColumnCounter[MatrixNr]) && (!lines.eof());k++) {
228 lines >> Matrix[MatrixNr][j][k];
229 //Log() << Verbose(1) << " " << setprecision(2) << Matrix[MatrixNr][j][k] << endl;
230 }
231 Matrix[MatrixNr][ RowCounter[MatrixNr] ] = new double[ColumnCounter[MatrixNr]];
232 for(int j=ColumnCounter[MatrixNr];j--;)
233 Matrix[MatrixNr][ RowCounter[MatrixNr] ][j] = 0.;
234 }
235 } else {
236 DoeLog(1) && (eLog()<< Verbose(1) << "Matrix nr. " << MatrixNr << " has column and row count of (" << ColumnCounter[MatrixNr] << "," << RowCounter[MatrixNr] << "), could not allocate nor parse!" << endl);
237 }
238 input.close();
239 return true;
240};
241
242/** Parsing a number of matrices.
243 * -# First, count the number of matrices by counting lines in KEYSETFILE
244 * -# Then,
245 * -# construct the fragment number
246 * -# open the matrix file
247 * -# skip some lines (\a skiplines)
248 * -# scan header lines for number of columns
249 * -# scan lines for number of rows
250 * -# allocate matrix
251 * -# loop over found column and row counts and parse in each entry
252 * -# Finally, allocate one additional matrix (\a MatrixCounter) containing combined or temporary values
253 * \param *name directory with files
254 * \param *prefix prefix of each matrix file
255 * \param *suffix suffix of each matrix file
256 * \param skiplines number of inital lines to skip
257 * \param skiplines number of inital columns to skip
258 * \return parsing successful
259 */
260bool MatrixContainer::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
261{
262 char filename[1023];
263 ifstream input;
264 char *FragmentNumber = NULL;
265 stringstream file;
266 string token;
267
268 // count the number of matrices
269 MatrixCounter = -1; // we count one too much
270 file << name << FRAGMENTPREFIX << KEYSETFILE;
271 input.open(file.str().c_str(), ios::in);
272 if (input == NULL) {
273 DoLog(0) && (Log() << Verbose(0) << endl << "MatrixContainer::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
274 return false;
275 }
276 while (!input.eof()) {
277 input.getline(filename, 1023);
278 stringstream zeile(filename);
279 MatrixCounter++;
280 }
281 input.close();
282 DoLog(0) && (Log() << Verbose(0) << "Determined " << MatrixCounter << " fragments." << endl);
283
284 DoLog(0) && (Log() << Verbose(0) << "Parsing through each fragment and retrieving " << prefix << suffix << "." << endl);
285 delete[](Header);
286 delete[](Matrix);
287 delete[](RowCounter);
288 delete[](ColumnCounter);
289 Header = new char*[MatrixCounter + 1]; // one more each for the total molecule
290 Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule
291 RowCounter = new int[MatrixCounter + 1];
292 ColumnCounter = new int[MatrixCounter + 1];
293 for(int i=MatrixCounter+1;i--;) {
294 Matrix[i] = NULL;
295 Header[i] = NULL;
296 RowCounter[i] = -1;
297 ColumnCounter[i] = -1;
298 }
299 for(int i=0; i < MatrixCounter;i++) {
300 // open matrix file
301 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
302 file.str(" ");
303 file << name << FRAGMENTPREFIX << FragmentNumber << prefix << suffix;
304 if (!ParseMatrix(file.str().c_str(), skiplines, skipcolumns, i))
305 return false;
306 delete[](FragmentNumber);
307 }
308 return true;
309};
310
311/** Allocates and resets the memory for a number \a MCounter of matrices.
312 * \param **GivenHeader Header line for each matrix
313 * \param MCounter number of matrices
314 * \param *RCounter number of rows for each matrix
315 * \param *CCounter number of columns for each matrix
316 * \return Allocation successful
317 */
318bool MatrixContainer::AllocateMatrix(char **GivenHeader, int MCounter, int *RCounter, int *CCounter)
319{
320 MatrixCounter = MCounter;
321 Header = new char*[MatrixCounter + 1];
322 Matrix = new double**[MatrixCounter + 1]; // one more each for the total molecule
323 RowCounter = new int[MatrixCounter + 1];
324 ColumnCounter = new int[MatrixCounter + 1];
325 for(int i=MatrixCounter+1;i--;) {
326 Header[i] = new char[1024];
327 strncpy(Header[i], GivenHeader[i], 1023);
328 RowCounter[i] = RCounter[i];
329 ColumnCounter[i] = CCounter[i];
330 Matrix[i] = new double*[RowCounter[i] + 1];
331 for(int j=RowCounter[i]+1;j--;) {
332 Matrix[i][j] = new double[ColumnCounter[i]];
333 for(int k=ColumnCounter[i];k--;)
334 Matrix[i][j][k] = 0.;
335 }
336 }
337 return true;
338};
339
340/** Resets all values in MatrixContainer::Matrix.
341 * \return true if successful
342 */
343bool MatrixContainer::ResetMatrix()
344{
345 for(int i=MatrixCounter+1;i--;)
346 for(int j=RowCounter[i]+1;j--;)
347 for(int k=ColumnCounter[i];k--;)
348 Matrix[i][j][k] = 0.;
349 return true;
350};
351
352/** Scans all elements of MatrixContainer::Matrix for greatest absolute value.
353 * \return greatest value of MatrixContainer::Matrix
354 */
355double MatrixContainer::FindMaxValue()
356{
357 double max = Matrix[0][0][0];
358 for(int i=MatrixCounter+1;i--;)
359 for(int j=RowCounter[i]+1;j--;)
360 for(int k=ColumnCounter[i];k--;)
361 if (fabs(Matrix[i][j][k]) > max)
362 max = fabs(Matrix[i][j][k]);
363 if (fabs(max) < MYEPSILON)
364 max += MYEPSILON;
365 return max;
366};
367
368/** Scans all elements of MatrixContainer::Matrix for smallest absolute value.
369 * \return smallest value of MatrixContainer::Matrix
370 */
371double MatrixContainer::FindMinValue()
372{
373 double min = Matrix[0][0][0];
374 for(int i=MatrixCounter+1;i--;)
375 for(int j=RowCounter[i]+1;j--;)
376 for(int k=ColumnCounter[i];k--;)
377 if (fabs(Matrix[i][j][k]) < min)
378 min = fabs(Matrix[i][j][k]);
379 if (fabs(min) < MYEPSILON)
380 min += MYEPSILON;
381 return min;
382};
383
384/** Sets all values in the last of MatrixContainer::Matrix to \a value.
385 * \param value reset value
386 * \param skipcolumns skip initial columns
387 * \return true if successful
388 */
389bool MatrixContainer::SetLastMatrix(double value, int skipcolumns)
390{
391 for(int j=RowCounter[MatrixCounter]+1;j--;)
392 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
393 Matrix[MatrixCounter][j][k] = value;
394 return true;
395};
396
397/** Sets all values in the last of MatrixContainer::Matrix to \a value.
398 * \param **values matrix with each value (must have at least same dimensions!)
399 * \param skipcolumns skip initial columns
400 * \return true if successful
401 */
402bool MatrixContainer::SetLastMatrix(double **values, int skipcolumns)
403{
404 for(int j=RowCounter[MatrixCounter]+1;j--;)
405 for(int k=skipcolumns;k<ColumnCounter[MatrixCounter];k++)
406 Matrix[MatrixCounter][j][k] = values[j][k];
407 return true;
408};
409
410/** Sums the entries with each factor and put into last element of \a ***Matrix.
411 * Sums over "E"-terms to create the "F"-terms
412 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
413 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
414 * \param Order bond order
415 * \return true if summing was successful
416 */
417bool MatrixContainer::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
418{
419 // go through each order
420 for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
421 //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
422 // then go per order through each suborder and pick together all the terms that contain this fragment
423 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
424 for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
425 if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
426 //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
427 // if the fragment's indices are all in the current fragment
428 for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
429 int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
430 //Log() << Verbose(0) << "Current index is " << k << "/" << m << "." << endl;
431 if (m != -1) { // if it's not an added hydrogen
432 for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
433 //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
434 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
435 m = l;
436 break;
437 }
438 }
439 //Log() << Verbose(0) << "Corresponding index in CurrentFragment is " << m << "." << endl;
440 if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
441 DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current force index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
442 performCriticalExit();
443 return false;
444 }
445 if (Order == SubOrder) { // equal order is always copy from Energies
446 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;) // then adds/subtract each column
447 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
448 } else {
449 for(int l=ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l--;)
450 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][l] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
451 }
452 }
453 //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
454 //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
455 }
456 } else {
457 //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
458 }
459 }
460 }
461 //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
462 }
463
464 return true;
465};
466
467/** Writes the summed total fragment terms \f$F_{ij}\f$ to file.
468 * \param *name inputdir
469 * \param *prefix prefix before \a EnergySuffix
470 * \return file was written
471 */
472bool MatrixContainer::WriteTotalFragments(const char *name, const char *prefix)
473{
474 ofstream output;
475 char *FragmentNumber = NULL;
476
477 DoLog(0) && (Log() << Verbose(0) << "Writing fragment files." << endl);
478 for(int i=0;i<MatrixCounter;i++) {
479 stringstream line;
480 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
481 line << name << FRAGMENTPREFIX << FragmentNumber << "/" << prefix;
482 delete[](FragmentNumber);
483 output.open(line.str().c_str(), ios::out);
484 if (output == NULL) {
485 DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteTotalFragments: Unable to open output energy file " << line.str() << "!" << endl);
486 performCriticalExit();
487 return false;
488 }
489 output << Header[i] << endl;
490 for(int j=0;j<RowCounter[i];j++) {
491 for(int k=0;k<ColumnCounter[i];k++)
492 output << scientific << Matrix[i][j][k] << "\t";
493 output << endl;
494 }
495 output.close();
496 }
497 return true;
498};
499
500/** Writes the summed total values in the last matrix to file.
501 * \param *name inputdir
502 * \param *prefix prefix
503 * \param *suffix suffix
504 * \return file was written
505 */
506bool MatrixContainer::WriteLastMatrix(const char *name, const char *prefix, const char *suffix)
507{
508 ofstream output;
509 stringstream line;
510
511 DoLog(0) && (Log() << Verbose(0) << "Writing matrix values of " << suffix << "." << endl);
512 line << name << prefix << suffix;
513 output.open(line.str().c_str(), ios::out);
514 if (output == NULL) {
515 DoeLog(0) && (eLog()<< Verbose(0) << "MatrixContainer::WriteLastMatrix: Unable to open output matrix file " << line.str() << "!" << endl);
516 performCriticalExit();
517 return false;
518 }
519 output << Header[MatrixCounter] << endl;
520 for(int j=0;j<RowCounter[MatrixCounter];j++) {
521 for(int k=0;k<ColumnCounter[MatrixCounter];k++)
522 output << scientific << Matrix[MatrixCounter][j][k] << "\t";
523 output << endl;
524 }
525 output.close();
526 return true;
527};
528
529// ======================================= CLASS EnergyMatrix =============================
530
531/** Create a trivial energy index mapping.
532 * This just maps 1 to 1, 2 to 2 and so on for all fragments.
533 * \return creation sucessful
534 */
535bool EnergyMatrix::ParseIndices()
536{
537 DoLog(0) && (Log() << Verbose(0) << "Parsing energy indices." << endl);
538 Indices = new int*[MatrixCounter + 1];
539 for(int i=MatrixCounter+1;i--;) {
540 Indices[i] = new int[RowCounter[i]];
541 for(int j=RowCounter[i];j--;)
542 Indices[i][j] = j;
543 }
544 return true;
545};
546
547/** Sums the energy with each factor and put into last element of \a EnergyMatrix::Matrix.
548 * Sums over the "F"-terms in ANOVA decomposition.
549 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
550 * \param CorrectionFragments MatrixContainer with hydrogen saturation correction per fragments
551 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
552 * \param Order bond order
553 * \parsm sign +1 or -1
554 * \return true if summing was successful
555 */
556bool EnergyMatrix::SumSubEnergy(class EnergyMatrix &Fragments, class EnergyMatrix *CorrectionFragments, class KeySetsContainer &KeySets, int Order, double sign)
557{
558 // sum energy
559 if (CorrectionFragments == NULL)
560 for(int i=KeySets.FragmentsPerOrder[Order];i--;)
561 for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
562 for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
563 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k];
564 else
565 for(int i=KeySets.FragmentsPerOrder[Order];i--;)
566 for(int j=RowCounter[ KeySets.OrderSet[Order][i] ];j--;)
567 for(int k=ColumnCounter[ KeySets.OrderSet[Order][i] ];k--;)
568 Matrix[MatrixCounter][j][k] += sign*(Fragments.Matrix[ KeySets.OrderSet[Order][i] ][j][k] + CorrectionFragments->Matrix[ KeySets.OrderSet[Order][i] ][j][k]);
569 return true;
570};
571
572/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
573 * \param *name directory with files
574 * \param *prefix prefix of each matrix file
575 * \param *suffix suffix of each matrix file
576 * \param skiplines number of inital lines to skip
577 * \param skiplines number of inital columns to skip
578 * \return parsing successful
579 */
580bool EnergyMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
581{
582 char filename[1024];
583 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
584
585 if (status) {
586 // count maximum of columns
587 RowCounter[MatrixCounter] = 0;
588 ColumnCounter[MatrixCounter] = 0;
589 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
590 if (RowCounter[j] > RowCounter[MatrixCounter])
591 RowCounter[MatrixCounter] = RowCounter[j];
592 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
593 ColumnCounter[MatrixCounter] = ColumnCounter[j];
594 }
595 // allocate last plus one matrix
596 DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
597 Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
598 for(int j=0;j<=RowCounter[MatrixCounter];j++)
599 Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
600
601 // try independently to parse global energysuffix file if present
602 strncpy(filename, name, 1023);
603 strncat(filename, prefix, 1023-strlen(filename));
604 strncat(filename, suffix.c_str(), 1023-strlen(filename));
605 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
606 }
607 return status;
608};
609
610// ======================================= CLASS ForceMatrix =============================
611
612/** Parsing force Indices of each fragment
613 * \param *name directory with \a ForcesFile
614 * \return parsing successful
615 */
616bool ForceMatrix::ParseIndices(const char *name)
617{
618 ifstream input;
619 char *FragmentNumber = NULL;
620 char filename[1023];
621 stringstream line;
622
623 DoLog(0) && (Log() << Verbose(0) << "Parsing force indices for " << MatrixCounter << " matrices." << endl);
624 Indices = new int*[MatrixCounter + 1];
625 line << name << FRAGMENTPREFIX << FORCESFILE;
626 input.open(line.str().c_str(), ios::in);
627 //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
628 if (input == NULL) {
629 DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
630 return false;
631 }
632 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
633 // get the number of atoms for this fragment
634 input.getline(filename, 1023);
635 line.str(filename);
636 // parse the values
637 Indices[i] = new int[RowCounter[i]];
638 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
639 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
640 delete[](FragmentNumber);
641 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
642 line >> Indices[i][j];
643 //Log() << Verbose(0) << " " << Indices[i][j];
644 }
645 //Log() << Verbose(0) << endl;
646 }
647 Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
648 for(int j=RowCounter[MatrixCounter];j--;) {
649 Indices[MatrixCounter][j] = j;
650 }
651 input.close();
652 return true;
653};
654
655
656/** Sums the forces and puts into last element of \a ForceMatrix::Matrix.
657 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
658 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
659 * \param Order bond order
660 * \param sign +1 or -1
661 * \return true if summing was successful
662 */
663bool ForceMatrix::SumSubForces(class ForceMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
664{
665 int FragmentNr;
666 // sum forces
667 for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
668 FragmentNr = KeySets.OrderSet[Order][i];
669 for(int l=0;l<RowCounter[ FragmentNr ];l++) {
670 int j = Indices[ FragmentNr ][l];
671 if (j > RowCounter[MatrixCounter]) {
672 DoeLog(0) && (eLog()<< Verbose(0) << "Current force index " << j << " is greater than " << RowCounter[MatrixCounter] << "!" << endl);
673 performCriticalExit();
674 return false;
675 }
676 if (j != -1) {
677 //if (j == 0) Log() << Verbose(0) << "Summing onto ion 0, type 0 from fragment " << FragmentNr << ", ion " << l << "." << endl;
678 for(int k=2;k<ColumnCounter[MatrixCounter];k++)
679 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][k];
680 }
681 }
682 }
683 return true;
684};
685
686
687/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
688 * \param *name directory with files
689 * \param *prefix prefix of each matrix file
690 * \param *suffix suffix of each matrix file
691 * \param skiplines number of inital lines to skip
692 * \param skiplines number of inital columns to skip
693 * \return parsing successful
694 */
695bool ForceMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
696{
697 char filename[1023];
698 ifstream input;
699 stringstream file;
700 int nr;
701 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
702
703 if (status) {
704 // count number of atoms for last plus one matrix
705 file << name << FRAGMENTPREFIX << KEYSETFILE;
706 input.open(file.str().c_str(), ios::in);
707 if (input == NULL) {
708 DoLog(0) && (Log() << Verbose(0) << endl << "ForceMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
709 return false;
710 }
711 RowCounter[MatrixCounter] = 0;
712 while (!input.eof()) {
713 input.getline(filename, 1023);
714 stringstream zeile(filename);
715 while (!zeile.eof()) {
716 zeile >> nr;
717 //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
718 if (nr > RowCounter[MatrixCounter])
719 RowCounter[MatrixCounter] = nr;
720 }
721 }
722 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
723 input.close();
724
725 ColumnCounter[MatrixCounter] = 0;
726 for(int j=0; j < MatrixCounter;j++) { // (energy matrix might be bigger than number of atoms in terms of rows)
727 if (ColumnCounter[j] > ColumnCounter[MatrixCounter]) // take maximum of all for last matrix
728 ColumnCounter[MatrixCounter] = ColumnCounter[j];
729 }
730
731 // allocate last plus one matrix
732 DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
733 Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
734 for(int j=0;j<=RowCounter[MatrixCounter];j++)
735 Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
736
737 // try independently to parse global forcesuffix file if present
738 strncpy(filename, name, 1023);
739 strncat(filename, prefix, 1023-strlen(filename));
740 strncat(filename, suffix.c_str(), 1023-strlen(filename));
741 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
742 }
743
744
745 return status;
746};
747
748// ======================================= CLASS HessianMatrix =============================
749
750/** Parsing force Indices of each fragment
751 * \param *name directory with \a ForcesFile
752 * \return parsing successful
753 */
754bool HessianMatrix::ParseIndices(char *name)
755{
756 ifstream input;
757 char *FragmentNumber = NULL;
758 char filename[1023];
759 stringstream line;
760
761 DoLog(0) && (Log() << Verbose(0) << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl);
762 Indices = new int*[MatrixCounter + 1];
763 line << name << FRAGMENTPREFIX << FORCESFILE;
764 input.open(line.str().c_str(), ios::in);
765 //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
766 if (input == NULL) {
767 DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
768 return false;
769 }
770 for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
771 // get the number of atoms for this fragment
772 input.getline(filename, 1023);
773 line.str(filename);
774 // parse the values
775 Indices[i] = new int[RowCounter[i]];
776 FragmentNumber = FixedDigitNumber(MatrixCounter, i);
777 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
778 delete[](FragmentNumber);
779 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
780 line >> Indices[i][j];
781 //Log() << Verbose(0) << " " << Indices[i][j];
782 }
783 //Log() << Verbose(0) << endl;
784 }
785 Indices[MatrixCounter] = new int[RowCounter[MatrixCounter]];
786 for(int j=RowCounter[MatrixCounter];j--;) {
787 Indices[MatrixCounter][j] = j;
788 }
789 input.close();
790 return true;
791};
792
793
794/** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
795 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
796 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
797 * \param Order bond order
798 * \param sign +1 or -1
799 * \return true if summing was successful
800 */
801bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
802{
803 int FragmentNr;
804 // sum forces
805 for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
806 FragmentNr = KeySets.OrderSet[Order][i];
807 for(int l=0;l<RowCounter[ FragmentNr ];l++) {
808 int j = Indices[ FragmentNr ][l];
809 if (j > RowCounter[MatrixCounter]) {
810 DoeLog(0) && (eLog()<< Verbose(0) << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl);
811 performCriticalExit();
812 return false;
813 }
814 if (j != -1) {
815 for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
816 int k = Indices[ FragmentNr ][m];
817 if (k > ColumnCounter[MatrixCounter]) {
818 DoeLog(0) && (eLog()<< Verbose(0) << "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl);
819 performCriticalExit();
820 return false;
821 }
822 if (k != -1) {
823 //Log() << Verbose(0) << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
824 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
825 }
826 }
827 }
828 }
829 }
830 return true;
831};
832
833/** Constructor for class HessianMatrix.
834 */
835HessianMatrix::HessianMatrix() : MatrixContainer()
836{
837 IsSymmetric = true;
838}
839
840/** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
841 * Sums over "E"-terms to create the "F"-terms
842 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
843 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
844 * \param Order bond order
845 * \return true if summing was successful
846 */
847bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
848{
849 // go through each order
850 for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
851 //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
852 // then go per order through each suborder and pick together all the terms that contain this fragment
853 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
854 for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
855 if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
856 //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
857 // if the fragment's indices are all in the current fragment
858 for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
859 int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
860 //Log() << Verbose(0) << "Current row index is " << k << "/" << m << "." << endl;
861 if (m != -1) { // if it's not an added hydrogen
862 for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
863 //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
864 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
865 m = l;
866 break;
867 }
868 }
869 //Log() << Verbose(0) << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
870 if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
871 DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
872 performCriticalExit();
873 return false;
874 }
875
876 for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
877 int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
878 //Log() << Verbose(0) << "Current column index is " << l << "/" << n << "." << endl;
879 if (n != -1) { // if it's not an added hydrogen
880 for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
881 //Log() << Verbose(0) << "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
882 if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
883 n = p;
884 break;
885 }
886 }
887 //Log() << Verbose(0) << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
888 if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
889 DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
890 performCriticalExit();
891 return false;
892 }
893 if (Order == SubOrder) { // equal order is always copy from Energies
894 //Log() << Verbose(0) << "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
895 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
896 } else {
897 //Log() << Verbose(0) << "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
898 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
899 }
900 }
901 }
902 }
903 //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
904 //Log() << Verbose(0) << "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1] << endl;
905 }
906 } else {
907 //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
908 }
909 }
910 }
911 //Log() << Verbose(0) << "Final Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << KeySets.AtomCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][KeySets.AtomCounter[0]-1][1] << endl;
912 }
913
914 return true;
915};
916
917/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
918 * \param *name directory with files
919 * \param *prefix prefix of each matrix file
920 * \param *suffix suffix of each matrix file
921 * \param skiplines number of inital lines to skip
922 * \param skiplines number of inital columns to skip
923 * \return parsing successful
924 */
925bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
926{
927 char filename[1023];
928 ifstream input;
929 stringstream file;
930 int nr;
931 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
932
933 if (status) {
934 // count number of atoms for last plus one matrix
935 file << name << FRAGMENTPREFIX << KEYSETFILE;
936 input.open(file.str().c_str(), ios::in);
937 if (input == NULL) {
938 DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
939 return false;
940 }
941 RowCounter[MatrixCounter] = 0;
942 ColumnCounter[MatrixCounter] = 0;
943 while (!input.eof()) {
944 input.getline(filename, 1023);
945 stringstream zeile(filename);
946 while (!zeile.eof()) {
947 zeile >> nr;
948 //Log() << Verbose(0) << "Current index: " << nr << "." << endl;
949 if (nr > RowCounter[MatrixCounter]) {
950 RowCounter[MatrixCounter] = nr;
951 ColumnCounter[MatrixCounter] = nr;
952 }
953 }
954 }
955 RowCounter[MatrixCounter]++; // nr start at 0, count starts at 1
956 ColumnCounter[MatrixCounter]++; // nr start at 0, count starts at 1
957 input.close();
958
959 // allocate last plus one matrix
960 DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
961 Matrix[MatrixCounter] = new double*[RowCounter[MatrixCounter] + 1];
962 for(int j=0;j<=RowCounter[MatrixCounter];j++)
963 Matrix[MatrixCounter][j] = new double[ColumnCounter[MatrixCounter]];
964
965 // try independently to parse global forcesuffix file if present
966 strncpy(filename, name, 1023);
967 strncat(filename, prefix, 1023-strlen(filename));
968 strncat(filename, suffix.c_str(), 1023-strlen(filename));
969 ParseMatrix(filename, skiplines, skipcolumns, MatrixCounter);
970 }
971
972
973 return status;
974};
975
976// ======================================= CLASS KeySetsContainer =============================
977
978/** Constructor of KeySetsContainer class.
979 */
980KeySetsContainer::KeySetsContainer() {
981 KeySets = NULL;
982 AtomCounter = NULL;
983 FragmentCounter = 0;
984 Order = 0;
985 FragmentsPerOrder = 0;
986 OrderSet = NULL;
987};
988
989/** Destructor of KeySetsContainer class.
990 */
991KeySetsContainer::~KeySetsContainer() {
992 for(int i=FragmentCounter;i--;)
993 delete[](KeySets[i]);
994 for(int i=Order;i--;)
995 delete[](OrderSet[i]);
996 delete[](KeySets);
997 delete[](OrderSet);
998 delete[](AtomCounter);
999 delete[](FragmentsPerOrder);
1000};
1001
1002/** Parsing KeySets into array.
1003 * \param *name directory with keyset file
1004 * \param *ACounter number of atoms per fragment
1005 * \param FCounter number of fragments
1006 * \return parsing succesful
1007 */
1008bool KeySetsContainer::ParseKeySets(const char *name, const int *ACounter, const int FCounter) {
1009 ifstream input;
1010 char *FragmentNumber = NULL;
1011 stringstream file;
1012 char filename[1023];
1013
1014 FragmentCounter = FCounter;
1015 DoLog(0) && (Log() << Verbose(0) << "Parsing key sets." << endl);
1016 KeySets = new int*[FragmentCounter];
1017 for(int i=FragmentCounter;i--;)
1018 KeySets[i] = NULL;
1019 file << name << FRAGMENTPREFIX << KEYSETFILE;
1020 input.open(file.str().c_str(), ios::in);
1021 if (input == NULL) {
1022 DoLog(0) && (Log() << Verbose(0) << endl << "KeySetsContainer::ParseKeySets: Unable to open " << file.str() << ", is the directory correct?" << endl);
1023 return false;
1024 }
1025
1026 AtomCounter = new int[FragmentCounter];
1027 for(int i=0;(i<FragmentCounter) && (!input.eof());i++) {
1028 stringstream line;
1029 AtomCounter[i] = ACounter[i];
1030 // parse the values
1031 KeySets[i] = new int[AtomCounter[i]];
1032 for(int j=AtomCounter[i];j--;)
1033 KeySets[i][j] = -1;
1034 FragmentNumber = FixedDigitNumber(FragmentCounter, i);
1035 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << AtomCounter[i] << "]:";
1036 delete[](FragmentNumber);
1037 input.getline(filename, 1023);
1038 line.str(filename);
1039 for(int j=0;(j<AtomCounter[i]) && (!line.eof());j++) {
1040 line >> KeySets[i][j];
1041 //Log() << Verbose(0) << " " << KeySets[i][j];
1042 }
1043 //Log() << Verbose(0) << endl;
1044 }
1045 input.close();
1046 return true;
1047};
1048
1049/** Parse many body terms, associating each fragment to a certain bond order.
1050 * \return parsing succesful
1051 */
1052bool KeySetsContainer::ParseManyBodyTerms()
1053{
1054 int Counter;
1055
1056 DoLog(0) && (Log() << Verbose(0) << "Creating Fragment terms." << endl);
1057 // scan through all to determine maximum order
1058 Order=0;
1059 for(int i=FragmentCounter;i--;) {
1060 Counter=0;
1061 for(int j=AtomCounter[i];j--;)
1062 if (KeySets[i][j] != -1)
1063 Counter++;
1064 if (Counter > Order)
1065 Order = Counter;
1066 }
1067 DoLog(0) && (Log() << Verbose(0) << "Found Order is " << Order << "." << endl);
1068
1069 // scan through all to determine fragments per order
1070 FragmentsPerOrder = new int[Order];
1071 for(int i=Order;i--;)
1072 FragmentsPerOrder[i] = 0;
1073 for(int i=FragmentCounter;i--;) {
1074 Counter=0;
1075 for(int j=AtomCounter[i];j--;)
1076 if (KeySets[i][j] != -1)
1077 Counter++;
1078 FragmentsPerOrder[Counter-1]++;
1079 }
1080 for(int i=0;i<Order;i++)
1081 DoLog(0) && (Log() << Verbose(0) << "Found No. of Fragments of Order " << i+1 << " is " << FragmentsPerOrder[i] << "." << endl);
1082
1083 // scan through all to gather indices to each order set
1084 OrderSet = new int*[Order];
1085 for(int i=Order;i--;)
1086 OrderSet[i] = new int[FragmentsPerOrder[i]];
1087 for(int i=Order;i--;)
1088 FragmentsPerOrder[i] = 0;
1089 for(int i=FragmentCounter;i--;) {
1090 Counter=0;
1091 for(int j=AtomCounter[i];j--;)
1092 if (KeySets[i][j] != -1)
1093 Counter++;
1094 OrderSet[Counter-1][FragmentsPerOrder[Counter-1]] = i;
1095 FragmentsPerOrder[Counter-1]++;
1096 }
1097 DoLog(0) && (Log() << Verbose(0) << "Printing OrderSet." << endl);
1098 for(int i=0;i<Order;i++) {
1099 for (int j=0;j<FragmentsPerOrder[i];j++) {
1100 DoLog(0) && (Log() << Verbose(0) << " " << OrderSet[i][j]);
1101 }
1102 DoLog(0) && (Log() << Verbose(0) << endl);
1103 }
1104 DoLog(0) && (Log() << Verbose(0) << endl);
1105
1106
1107 return true;
1108};
1109
1110/** Compares each entry in \a *SmallerSet if it is containted in \a *GreaterSet.
1111 * \param GreaterSet index to greater set
1112 * \param SmallerSet index to smaller set
1113 * \return true if all keys of SmallerSet contained in GreaterSet
1114 */
1115bool KeySetsContainer::Contains(const int GreaterSet, const int SmallerSet)
1116{
1117 bool result = true;
1118 bool intermediate;
1119 if ((GreaterSet < 0) || (SmallerSet < 0) || (GreaterSet > FragmentCounter) || (SmallerSet > FragmentCounter)) // index out of bounds
1120 return false;
1121 for(int i=AtomCounter[SmallerSet];i--;) {
1122 intermediate = false;
1123 for (int j=AtomCounter[GreaterSet];j--;)
1124 intermediate = (intermediate || ((KeySets[SmallerSet][i] == KeySets[GreaterSet][j]) || (KeySets[SmallerSet][i] == -1)));
1125 result = result && intermediate;
1126 }
1127
1128 return result;
1129};
1130
1131
1132// ======================================= END =============================================
Note: See TracBrowser for help on using the repository browser.