source: src/parser.cpp@ cd5047

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 cd5047 was 920c70, checked in by Frederik Heber <heber@…>, 15 years ago

Removed all Malloc/Calloc/ReAlloc (&Free) and replaced by new and delete/delete[].

Due to the new MemDebug framework there is no need (or even unnecessary/unwanted competition between it and) for the MemoryAllocator and ..UsageObserver anymore.
They can however still be used with c codes such as pcp and alikes.

In Molecuilder lots of glibc corruptions arose and the C-like syntax make it generally harder to get allocation and deallocation straight.

Signed-off-by: Frederik Heber <heber@…>

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