source: src/parser.cpp@ 0d1ad0

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 0d1ad0 was 112b09, checked in by Tillmann Crueger <crueger@…>, 15 years ago

Added #include "Helpers/MemDebug.hpp" to all .cpp files

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