1 | /*
|
---|
2 | * Project: MoleCuilder
|
---|
3 | * Description: creates and alters molecular systems
|
---|
4 | * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
|
---|
5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
6 | */
|
---|
7 |
|
---|
8 | /*
|
---|
9 | * HessianMatrix.cpp
|
---|
10 | *
|
---|
11 | * Created on: Sep 15, 2011
|
---|
12 | * Author: heber
|
---|
13 | */
|
---|
14 |
|
---|
15 | // include config.h
|
---|
16 | #ifdef HAVE_CONFIG_H
|
---|
17 | #include <config.h>
|
---|
18 | #endif
|
---|
19 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <cstring>
|
---|
23 | #include <fstream>
|
---|
24 |
|
---|
25 | #include "CodePatterns/Log.hpp"
|
---|
26 | #include "KeySetsContainer.hpp"
|
---|
27 |
|
---|
28 | #include "Fragmentation/helpers.hpp"
|
---|
29 | #include "Helpers/defs.hpp"
|
---|
30 | #include "Helpers/helpers.hpp"
|
---|
31 |
|
---|
32 | #include "HessianMatrix.hpp"
|
---|
33 |
|
---|
34 | /** Parsing force Indices of each fragment
|
---|
35 | * \param *name directory with \a ForcesFile
|
---|
36 | * \return parsing successful
|
---|
37 | */
|
---|
38 | bool HessianMatrix::ParseIndices(char *name)
|
---|
39 | {
|
---|
40 | std::ifstream input;
|
---|
41 | char *FragmentNumber = NULL;
|
---|
42 | char filename[1023];
|
---|
43 | stringstream line;
|
---|
44 |
|
---|
45 | LOG(0, "Parsing hessian indices for " << MatrixCounter << " matrices.");
|
---|
46 | Indices.resize(MatrixCounter + 1);
|
---|
47 | line << name << FRAGMENTPREFIX << FORCESFILE;
|
---|
48 | input.open(line.str().c_str(), ios::in);
|
---|
49 | //LOG(0, "Opening " << line.str() << " ... " << input);
|
---|
50 | if (input.fail()) {
|
---|
51 | LOG(0, endl << "HessianMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?");
|
---|
52 | return false;
|
---|
53 | }
|
---|
54 | for (int i=0;(i<MatrixCounter) && (!input.eof());i++) {
|
---|
55 | // get the number of atoms for this fragment
|
---|
56 | input.getline(filename, 1023);
|
---|
57 | line.str(filename);
|
---|
58 | // parse the values
|
---|
59 | Indices[i].resize(RowCounter[i]);
|
---|
60 | FragmentNumber = FixedDigitNumber(MatrixCounter, i);
|
---|
61 | //std::stringstream output;
|
---|
62 | //output << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
|
---|
63 | delete[](FragmentNumber);
|
---|
64 | for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
|
---|
65 | line >> Indices[i][j];
|
---|
66 | //output << " " << Indices[i][j];
|
---|
67 | }
|
---|
68 | //LOG(0, output.str());
|
---|
69 | }
|
---|
70 | Indices[MatrixCounter].resize(RowCounter[MatrixCounter]);
|
---|
71 | for(int j=RowCounter[MatrixCounter];j--;) {
|
---|
72 | Indices[MatrixCounter][j] = j;
|
---|
73 | }
|
---|
74 | input.close();
|
---|
75 | return true;
|
---|
76 | };
|
---|
77 |
|
---|
78 |
|
---|
79 | /** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
|
---|
80 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
81 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
82 | * \param Order bond order
|
---|
83 | * \param sign +1 or -1
|
---|
84 | * \return true if summing was successful
|
---|
85 | */
|
---|
86 | bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
|
---|
87 | {
|
---|
88 | int FragmentNr;
|
---|
89 | // sum forces
|
---|
90 | for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
|
---|
91 | FragmentNr = KeySets.OrderSet[Order][i];
|
---|
92 | for(int l=0;l<RowCounter[ FragmentNr ];l++) {
|
---|
93 | int j = Indices[ FragmentNr ][l];
|
---|
94 | if (j > RowCounter[MatrixCounter]) {
|
---|
95 | ELOG(0, "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!");
|
---|
96 | performCriticalExit();
|
---|
97 | return false;
|
---|
98 | }
|
---|
99 | if (j != -1) {
|
---|
100 | for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
|
---|
101 | int k = Indices[ FragmentNr ][m];
|
---|
102 | if (k > ColumnCounter[MatrixCounter]) {
|
---|
103 | ELOG(0, "Current hessian index " << k << " is greater than " << ColumnCounter[MatrixCounter] << ", where m=" << m << ", j=" << j << ", i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!");
|
---|
104 | performCriticalExit();
|
---|
105 | return false;
|
---|
106 | }
|
---|
107 | if (k != -1) {
|
---|
108 | //LOG(0, "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "].");
|
---|
109 | Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
|
---|
110 | }
|
---|
111 | }
|
---|
112 | }
|
---|
113 | }
|
---|
114 | }
|
---|
115 | return true;
|
---|
116 | };
|
---|
117 |
|
---|
118 | /** Constructor for class HessianMatrix.
|
---|
119 | */
|
---|
120 | HessianMatrix::HessianMatrix() :
|
---|
121 | MatrixContainer(),
|
---|
122 | IsSymmetric(true)
|
---|
123 | {}
|
---|
124 |
|
---|
125 | /** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
|
---|
126 | * Sums over "E"-terms to create the "F"-terms
|
---|
127 | * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
|
---|
128 | * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
|
---|
129 | * \param Order bond order
|
---|
130 | * \return true if summing was successful
|
---|
131 | */
|
---|
132 | bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
|
---|
133 | {
|
---|
134 | // go through each order
|
---|
135 | for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
|
---|
136 | //LOG(0, "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
137 | // then go per order through each suborder and pick together all the terms that contain this fragment
|
---|
138 | for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
|
---|
139 | for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
|
---|
140 | if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
|
---|
141 | //LOG(0, "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << ".");
|
---|
142 | // if the fragment's indices are all in the current fragment
|
---|
143 | for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
|
---|
144 | int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
|
---|
145 | //LOG(0, "Current row index is " << k << "/" << m << ".");
|
---|
146 | if (m != -1) { // if it's not an added hydrogen
|
---|
147 | for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
|
---|
148 | //LOG(0, "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << ".");
|
---|
149 | if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
|
---|
150 | m = l;
|
---|
151 | break;
|
---|
152 | }
|
---|
153 | }
|
---|
154 | //LOG(0, "Corresponding row index for " << k << " in CurrentFragment is " << m << ".");
|
---|
155 | if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
156 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
|
---|
157 | performCriticalExit();
|
---|
158 | return false;
|
---|
159 | }
|
---|
160 |
|
---|
161 | for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
|
---|
162 | int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
|
---|
163 | //LOG(0, "Current column index is " << l << "/" << n << ".");
|
---|
164 | if (n != -1) { // if it's not an added hydrogen
|
---|
165 | for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
|
---|
166 | //LOG(0, "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << ".");
|
---|
167 | if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
|
---|
168 | n = p;
|
---|
169 | break;
|
---|
170 | }
|
---|
171 | }
|
---|
172 | //LOG(0, "Corresponding column index for " << l << " in CurrentFragment is " << n << ".");
|
---|
173 | if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
|
---|
174 | ELOG(0, "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!");
|
---|
175 | performCriticalExit();
|
---|
176 | return false;
|
---|
177 | }
|
---|
178 | if (Order == SubOrder) { // equal order is always copy from Energies
|
---|
179 | //LOG(0, "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "].");
|
---|
180 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
181 | } else {
|
---|
182 | //LOG(0, "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "].");
|
---|
183 | Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
|
---|
189 | //LOG(0, "Fragments[ KeySets.OrderSet[" << Order << "][" << CurrentFragment << "]=" << KeySets.OrderSet[Order][CurrentFragment] << " ][" << RowCounter[0]-1 << "][" << 1 << "] = " << Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][RowCounter[0]-1][1]);
|
---|
190 | }
|
---|
191 | } else {
|
---|
192 | //LOG(0, "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << ".");
|
---|
193 | }
|
---|
194 | }
|
---|
195 | }
|
---|
196 | //LOG(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]);
|
---|
197 | }
|
---|
198 |
|
---|
199 | return true;
|
---|
200 | };
|
---|
201 |
|
---|
202 | /** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
|
---|
203 | * \param *name directory with files
|
---|
204 | * \param *prefix prefix of each matrix file
|
---|
205 | * \param *suffix suffix of each matrix file
|
---|
206 | * \param skiplines number of inital lines to skip
|
---|
207 | * \param skiplines number of inital columns to skip
|
---|
208 | * \return parsing successful
|
---|
209 | */
|
---|
210 | bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, std::string suffix, int skiplines, int skipcolumns)
|
---|
211 | {
|
---|
212 | char filename[1023];
|
---|
213 | std::ifstream input;
|
---|
214 | stringstream file;
|
---|
215 | int nr;
|
---|
216 | bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
|
---|
217 |
|
---|
218 | if (status) {
|
---|
219 | // count number of atoms for last plus one matrix
|
---|
220 | file << name << FRAGMENTPREFIX << KEYSETFILE;
|
---|
221 | input.open(file.str().c_str(), ios::in);
|
---|
222 | if (input.fail()) {
|
---|
223 | LOG(0, endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?");
|
---|
224 | return false;
|
---|
225 | }
|
---|
226 | RowCounter[MatrixCounter] = 0;
|
---|
227 | ColumnCounter[MatrixCounter] = 0;
|
---|
228 | while (!input.eof()) {
|
---|
229 | input.getline(filename, 1023);
|
---|
230 | stringstream zeile(filename);
|
---|
231 | while (!zeile.eof()) {
|
---|
232 | zeile >> nr;
|
---|
233 | //LOG(0, "Current index: " << getNr() << ".");
|
---|
234 | if (nr > RowCounter[MatrixCounter]) {
|
---|
235 | RowCounter[MatrixCounter] = nr;
|
---|
236 | ColumnCounter[MatrixCounter] = nr;
|
---|
237 | }
|
---|
238 | }
|
---|
239 | }
|
---|
240 | RowCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
|
---|
241 | ColumnCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
|
---|
242 | input.close();
|
---|
243 |
|
---|
244 | // allocate last plus one matrix
|
---|
245 | LOG(0, "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns.");
|
---|
246 | if ((int)Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2)
|
---|
247 | Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1);
|
---|
248 | for(int j=0;j<=RowCounter[MatrixCounter];j++)
|
---|
249 | if ((int)Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1)
|
---|
250 | Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]);
|
---|
251 |
|
---|
252 | // try independently to parse global forcesuffix file if present
|
---|
253 | strncpy(filename, name, 1023);
|
---|
254 | strncat(filename, prefix, 1023-strlen(filename));
|
---|
255 | strncat(filename, suffix.c_str(), 1023-strlen(filename));
|
---|
256 | std::ifstream input(filename);
|
---|
257 | ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
|
---|
258 | input.close();
|
---|
259 | }
|
---|
260 |
|
---|
261 |
|
---|
262 | return status;
|
---|
263 | };
|
---|