source: src/parser.cpp@ 986ed3

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 986ed3 was 36166d, checked in by Tillmann Crueger <crueger@…>, 14 years ago

Removed left over parts from old memory-tracker

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