source: src/parser.cpp@ cb2146

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 cb2146 was 244a84, checked in by Frederik Heber <heber@…>, 15 years ago

GetDistanceToSurface() separated, filling now with water instead of boron, DissectMoleculeIntoConnectedSubgraphs() now working on list of molecules instead of single one.

Signed-off-by: Frederik Heber <heber@…>

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