source: src/parser.cpp@ 89c8b2

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 89c8b2 was 36ec71, checked in by Frederik Heber <heber@…>, 15 years ago

Merge branch 'master' into ConcaveHull

Conflicts:

molecuilder/src/analyzer.cpp
molecuilder/src/bond.cpp
molecuilder/src/boundary.cpp
molecuilder/src/boundary.hpp
molecuilder/src/builder.cpp
molecuilder/src/config.cpp
molecuilder/src/datacreator.cpp
molecuilder/src/datacreator.hpp
molecuilder/src/defs.hpp
molecuilder/src/ellipsoid.cpp
molecuilder/src/joiner.cpp
molecuilder/src/molecules.cpp
molecuilder/src/molecules.hpp
molecuilder/src/parser.cpp
molecuilder/src/parser.hpp

merges:

compilation fixes:

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