source: src/parser.cpp@ 791138

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 791138 was e359a8, checked in by Frederik Heber <heber@…>, 16 years ago

Fixing ticket #18.

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