source: src/parser.cpp@ 8468cb

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 8468cb was 49e1ae, checked in by Frederik Heber <heber@…>, 15 years ago

cstring header was missing in files, supplying definition of strlen, strcpy, and so on.

This was noted on laptop with gcc 4.1 (on workstation we have gcc 4.2)

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