source: src/parser.cpp@ 9df680

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 9df680 was ad011c, checked in by Frederik Heber <heber@…>, 14 years ago

CodePatterns places all includes now in subfolder CodePatterns/.

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