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