source: src/Fragmentation/HessianMatrix.cpp@ 50e4e5

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 50e4e5 was 9758f7, checked in by Frederik Heber <heber@…>, 13 years ago

Removed C arrays in KeySetsContainer and MatrixContainer.

  • replaced by std::vector<>s.
  • transition is so far very minimal, external (range) counters are still present.
  • extracted allocation of memory into own block in MatrixContainer::parseMatrix().
  • Property mode set to 100644
File size: 12.6 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 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 */
38bool HessianMatrix::ParseIndices(char *name)
39{
40 std::ifstream input;
41 char *FragmentNumber = NULL;
42 char filename[1023];
43 stringstream line;
44
45 DoLog(0) && (Log() << Verbose(0) << "Parsing hessian indices for " << MatrixCounter << " matrices." << endl);
46 Indices.resize(MatrixCounter + 1);
47 line << name << FRAGMENTPREFIX << FORCESFILE;
48 input.open(line.str().c_str(), ios::in);
49 //Log() << Verbose(0) << "Opening " << line.str() << " ... " << input << endl;
50 if (input.fail()) {
51 DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseIndices: Unable to open " << line.str() << ", is the directory correct?" << endl);
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 //Log() << Verbose(0) << FRAGMENTPREFIX << FragmentNumber << "[" << RowCounter[i] << "]:";
62 delete[](FragmentNumber);
63 for(int j=0;(j<RowCounter[i]) && (!line.eof());j++) {
64 line >> Indices[i][j];
65 //Log() << Verbose(0) << " " << Indices[i][j];
66 }
67 //Log() << Verbose(0) << endl;
68 }
69 Indices[MatrixCounter].resize(RowCounter[MatrixCounter]);
70 for(int j=RowCounter[MatrixCounter];j--;) {
71 Indices[MatrixCounter][j] = j;
72 }
73 input.close();
74 return true;
75};
76
77
78/** Sums the hessian entries and puts into last element of \a HessianMatrix::Matrix.
79 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
80 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
81 * \param Order bond order
82 * \param sign +1 or -1
83 * \return true if summing was successful
84 */
85bool HessianMatrix::SumSubHessians(class HessianMatrix &Fragments, class KeySetsContainer &KeySets, int Order, double sign)
86{
87 int FragmentNr;
88 // sum forces
89 for(int i=0;i<KeySets.FragmentsPerOrder[Order];i++) {
90 FragmentNr = KeySets.OrderSet[Order][i];
91 for(int l=0;l<RowCounter[ FragmentNr ];l++) {
92 int j = Indices[ FragmentNr ][l];
93 if (j > RowCounter[MatrixCounter]) {
94 DoeLog(0) && (eLog()<< Verbose(0) << "Current hessian index " << j << " is greater than " << RowCounter[MatrixCounter] << ", where i=" << i << ", Order=" << Order << ", l=" << l << " and FragmentNr=" << FragmentNr << "!" << endl);
95 performCriticalExit();
96 return false;
97 }
98 if (j != -1) {
99 for(int m=0;m<ColumnCounter[ FragmentNr ];m++) {
100 int k = Indices[ FragmentNr ][m];
101 if (k > ColumnCounter[MatrixCounter]) {
102 DoeLog(0) && (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);
103 performCriticalExit();
104 return false;
105 }
106 if (k != -1) {
107 //Log() << Verbose(0) << "Adding " << sign*Fragments.Matrix[ FragmentNr ][l][m] << " from [" << l << "][" << m << "] onto [" << j << "][" << k << "]." << endl;
108 Matrix[MatrixCounter][j][k] += sign*Fragments.Matrix[ FragmentNr ][l][m];
109 }
110 }
111 }
112 }
113 }
114 return true;
115};
116
117/** Constructor for class HessianMatrix.
118 */
119HessianMatrix::HessianMatrix() :
120 MatrixContainer(),
121 IsSymmetric(true)
122{}
123
124/** Sums the hessian entries with each factor and put into last element of \a ***Matrix.
125 * Sums over "E"-terms to create the "F"-terms
126 * \param Matrix MatrixContainer with matrices (LevelCounter by *ColumnCounter) with all the energies.
127 * \param KeySets KeySetContainer with bond Order and association mapping of each fragment to an order
128 * \param Order bond order
129 * \return true if summing was successful
130 */
131bool HessianMatrix::SumSubManyBodyTerms(class MatrixContainer &MatrixValues, class KeySetsContainer &KeySets, int Order)
132{
133 // go through each order
134 for (int CurrentFragment=0;CurrentFragment<KeySets.FragmentsPerOrder[Order];CurrentFragment++) {
135 //Log() << Verbose(0) << "Current Fragment is " << CurrentFragment << "/" << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
136 // then go per order through each suborder and pick together all the terms that contain this fragment
137 for(int SubOrder=0;SubOrder<=Order;SubOrder++) { // go through all suborders up to the desired order
138 for (int j=0;j<KeySets.FragmentsPerOrder[SubOrder];j++) { // go through all possible fragments of size suborder
139 if (KeySets.Contains(KeySets.OrderSet[Order][CurrentFragment], KeySets.OrderSet[SubOrder][j])) {
140 //Log() << Verbose(0) << "Current other fragment is " << j << "/" << KeySets.OrderSet[SubOrder][j] << "." << endl;
141 // if the fragment's indices are all in the current fragment
142 for(int k=0;k<RowCounter[ KeySets.OrderSet[SubOrder][j] ];k++) { // go through all atoms in this fragment
143 int m = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][k];
144 //Log() << Verbose(0) << "Current row index is " << k << "/" << m << "." << endl;
145 if (m != -1) { // if it's not an added hydrogen
146 for (int l=0;l<RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ];l++) { // look for the corresponding index in the current fragment
147 //Log() << Verbose(0) << "Comparing " << m << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l] << "." << endl;
148 if (m == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][l]) {
149 m = l;
150 break;
151 }
152 }
153 //Log() << Verbose(0) << "Corresponding row index for " << k << " in CurrentFragment is " << m << "." << endl;
154 if (m > RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
155 DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current row index " << m << " is greater than " << RowCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
156 performCriticalExit();
157 return false;
158 }
159
160 for(int l=0;l<ColumnCounter[ KeySets.OrderSet[SubOrder][j] ];l++) {
161 int n = MatrixValues.Indices[ KeySets.OrderSet[SubOrder][j] ][l];
162 //Log() << Verbose(0) << "Current column index is " << l << "/" << n << "." << endl;
163 if (n != -1) { // if it's not an added hydrogen
164 for (int p=0;p<ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ];p++) { // look for the corresponding index in the current fragment
165 //Log() << Verbose(0) << "Comparing " << n << " with " << MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p] << "." << endl;
166 if (n == MatrixValues.Indices[ KeySets.OrderSet[Order][CurrentFragment] ][p]) {
167 n = p;
168 break;
169 }
170 }
171 //Log() << Verbose(0) << "Corresponding column index for " << l << " in CurrentFragment is " << n << "." << endl;
172 if (n > ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ]) {
173 DoeLog(0) && (eLog()<< Verbose(0) << "In fragment No. " << KeySets.OrderSet[Order][CurrentFragment] << " current column index " << n << " is greater than " << ColumnCounter[ KeySets.OrderSet[Order][CurrentFragment] ] << "!" << endl);
174 performCriticalExit();
175 return false;
176 }
177 if (Order == SubOrder) { // equal order is always copy from Energies
178 //Log() << Verbose(0) << "Adding " << MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
179 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] += MatrixValues.Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
180 } else {
181 //Log() << Verbose(0) << "Subtracting " << Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l] << " from [" << k << "][" << l << "] onto [" << m << "][" << n << "]." << endl;
182 Matrix[ KeySets.OrderSet[Order][CurrentFragment] ][m][n] -= Matrix[ KeySets.OrderSet[SubOrder][j] ][k][l];
183 }
184 }
185 }
186 }
187 //if ((ColumnCounter[ KeySets.OrderSet[SubOrder][j] ]>1) && (RowCounter[0]-1 >= 1))
188 //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;
189 }
190 } else {
191 //Log() << Verbose(0) << "Fragment " << KeySets.OrderSet[SubOrder][j] << " is not contained in fragment " << KeySets.OrderSet[Order][CurrentFragment] << "." << endl;
192 }
193 }
194 }
195 //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;
196 }
197
198 return true;
199};
200
201/** Calls MatrixContainer::ParseFragmentMatrix() and additionally allocates last plus one matrix.
202 * \param *name directory with files
203 * \param *prefix prefix of each matrix file
204 * \param *suffix suffix of each matrix file
205 * \param skiplines number of inital lines to skip
206 * \param skiplines number of inital columns to skip
207 * \return parsing successful
208 */
209bool HessianMatrix::ParseFragmentMatrix(const char *name, const char *prefix, string suffix, int skiplines, int skipcolumns)
210{
211 char filename[1023];
212 std::ifstream input;
213 stringstream file;
214 int nr;
215 bool status = MatrixContainer::ParseFragmentMatrix(name, prefix, suffix, skiplines, skipcolumns);
216
217 if (status) {
218 // count number of atoms for last plus one matrix
219 file << name << FRAGMENTPREFIX << KEYSETFILE;
220 input.open(file.str().c_str(), ios::in);
221 if (input.fail()) {
222 DoLog(0) && (Log() << Verbose(0) << endl << "HessianMatrix::ParseFragmentMatrix: Unable to open " << file.str() << ", is the directory correct?" << endl);
223 return false;
224 }
225 RowCounter[MatrixCounter] = 0;
226 ColumnCounter[MatrixCounter] = 0;
227 while (!input.eof()) {
228 input.getline(filename, 1023);
229 stringstream zeile(filename);
230 while (!zeile.eof()) {
231 zeile >> nr;
232 //Log() << Verbose(0) << "Current index: " << getNr() << "." << endl;
233 if (nr > RowCounter[MatrixCounter]) {
234 RowCounter[MatrixCounter] = nr;
235 ColumnCounter[MatrixCounter] = nr;
236 }
237 }
238 }
239 RowCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
240 ColumnCounter[MatrixCounter]++; // Nr start at 0, count starts at 1
241 input.close();
242
243 // allocate last plus one matrix
244 DoLog(0) && (Log() << Verbose(0) << "Allocating last plus one matrix with " << (RowCounter[MatrixCounter]+1) << " rows and " << ColumnCounter[MatrixCounter] << " columns." << endl);
245 if (Matrix[MatrixCounter].size() <= RowCounter[MatrixCounter] + 2)
246 Matrix[MatrixCounter].resize(RowCounter[MatrixCounter] + 1);
247 for(int j=0;j<=RowCounter[MatrixCounter];j++)
248 if (Matrix[MatrixCounter][j].size() <= ColumnCounter[MatrixCounter]+1)
249 Matrix[MatrixCounter][j].resize(ColumnCounter[MatrixCounter]);
250
251 // try independently to parse global forcesuffix file if present
252 strncpy(filename, name, 1023);
253 strncat(filename, prefix, 1023-strlen(filename));
254 strncat(filename, suffix.c_str(), 1023-strlen(filename));
255 std::ifstream input(filename);
256 ParseMatrix(input, skiplines, skipcolumns, MatrixCounter);
257 input.close();
258 }
259
260
261 return status;
262};
Note: See TracBrowser for help on using the repository browser.