source: src/parser.cpp@ 353e82

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 353e82 was 97b825, checked in by Frederik Heber <heber@…>, 15 years ago

Shortened constructors [Meyers, "Effective C++" item 12]

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