source: src/parser.cpp@ 3e50ff

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

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

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

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