Psi4
opt_data.h
Go to the documentation of this file.
1 /*
2  * @BEGIN LICENSE
3  *
4  * Psi4: an open-source quantum chemistry software package
5  *
6  * Copyright (c) 2007-2017 The Psi4 Developers.
7  *
8  * The copyrights for code used from other parties are included in
9  * the corresponding files.
10  *
11  * This program is free software; you can redistribute it and/or modify
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License along
22  * with this program; if not, write to the Free Software Foundation, Inc.,
23  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24  *
25  * @END LICENSE
26  */
27 
33 #ifndef _opt_opt_data_h_
34 #define _opt_opt_data_h_
35 
36 #include <fstream>
37 #include <vector>
38 
39 #include "package.h"
40 
41 #include "linear_algebra.h"
42 #include "molecule.h"
43 #include "print.h"
44 
45 #include "io.h"
46 
47 namespace opt {
48 
49 using namespace std;
50 
51 // data for one optimization step
52 class STEP_DATA {
53 
54  double *f_q; // Internal coordinate forces
55  double *geom; // cartesian coordinate values
56  double energy; // total energy
57  double DE_predicted; // energy drop predicted for next step
58  double *unit_step; // unit vector in direction of step in the basis of internal coordinates
59  double dq_norm; // norm of step in internal coordinates
60  double dq_gradient; // gradient along step
61  double dq_hessian; // hessian along step
62  double *dq; // step in internal coordinates
63 
64  public:
65  //STEP_DATA(ifstream & fin, int Nintco, int Ncart); // read in date for one step
66 
67  STEP_DATA(int Nintco, int Ncart); // allocate memory only
68 
69  ~STEP_DATA(); // free memory
70 
71  // save geometry and energy
72  void save_geom_energy(double *geom_in, double energy_in, int Ncart);
73 
74  // save rest of stuff
75  void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in,
76  double dq_gradient_in, double dq_hessian_in, int Nintco);
77 
78  // functions to read and write a step to the binary file
79  void write(int istep, int Nintco, int Ncart);
80  // read step from binary file
81  void read(int istep, int Nintco, int Ncart);
82 
83  // functions to retrieve data
84  double *g_forces_pointer(void) const { return f_q; }
85  double *g_geom_const_pointer(void) const { return geom; }
86  double *g_dq_pointer(void) const { return dq; }
87  double g_energy(void) const { return energy; }
88  double g_DE_predicted(void) const { return DE_predicted; }
89  double g_dq_norm(void) const { return dq_norm; }
90  double g_dq_gradient(void) const { return dq_gradient; }
91  double g_dq_hessian(void) const { return dq_hessian; }
92 };
93 
94 // data for an optimization
95 class OPT_DATA {
96  int Nintco; // num. of internal coordinates
97  int Ncart; // num. of cartesian coordinates
98  double **H; // Hessian matrix
99  int iteration; // num. of current iteration, 0, 1, 2, ...
100  // # of previous steps of data stored should be == iteration
101  int steps_since_last_H; // number of steps since H has been computed
102  int consecutive_backsteps; // # of consecutive steps backwards, if any
103  int previous_consecutive_backsteps; // only used in current memory; not saved
104  double *rfo_eigenvector; // for RFO root-following
105  std::vector<STEP_DATA *> steps;
106 
107  public:
108 
109  // allocates memory for this step; reads in old steps from binary file
110  OPT_DATA(int Nintco_in, int Ncart_in);
111 
112  // free memory
113  ~OPT_DATA();
114 
115  // write data to binary file
116  void write(void);
117 
118  // save geometry and energy to current (last) step
119  void save_geom_energy(double *geom_in, double energy_in) {
120  steps[steps.size()-1]->save_geom_energy(geom_in, energy_in, Ncart);
121  }
122 
123  // save rest of stuff to current (last) step
124  void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in,
125  double dq_gradient_in, double dq_hessian_in) {
126  steps[steps.size()-1]->save_step_info(DE_predicted_in, unit_step_in, dq_norm_in,
127  dq_gradient_in, dq_hessian_in, Nintco);
128  }
129 
130  // return (pointers) to current-step data
131  int g_iteration(void) const { return iteration; }
132  double **g_H_pointer(void) { return H; }
133  double g_energy(void) const { return steps[steps.size()-1]->g_energy(); }
134  double *g_rfo_eigenvector_pointer(void) const { return rfo_eigenvector; }
135  // return dimension of Hessian matrix
136  int Ncoord(void) const { return Nintco; }
137 
138  void set_rfo_eigenvector(double *evect_in) {
139  for (int i=0; i<Nintco; ++i)
140  rfo_eigenvector[i] = evect_in[i];
141  }
142 
143  // step data
144  double *g_forces_pointer(void) const {
145  return steps[steps.size()-1]->g_forces_pointer();
146  }
147  double *g_dq_pointer(void) const {
148  return steps[steps.size()-1]->g_dq_pointer();
149  }
150  // return energy from the previous step (last entry - 1)
151  double g_last_energy(void) const {
152  if (steps.size() > 1)
153  return steps[steps.size()-2]->g_energy();
154  else return 0.0;
155  }
156 
157  // return predicted energy change at the previous step (last entry - 1)
158  double g_last_DE_predicted(void) const {
159  if (steps.size() > 1)
160  return steps[steps.size()-2]->g_DE_predicted();
161  else return 0.0;
162  }
163 
164  // return pointers to arbitrary-step data (pass in index starting at 0 ...)
165  double g_energy(int i) const {
166  return steps[i]->g_energy();
167  }
168  double *g_forces_pointer(int i) const {
169  return steps.at(i)->g_forces_pointer();
170  }
171  double *g_last_forces_pointer(void) const {
172  if (steps.size() > 1)
173  return steps.at(steps.size()-2)->g_forces_pointer();
174  else return NULL;
175  }
176  double *g_geom_const_pointer(int i) const {
177  return steps.at(i)->g_geom_const_pointer();
178  }
179  double *g_dq_pointer(int i) const {
180  return steps.at(i)->g_dq_pointer();
181  }
182  double g_dq_norm(int i) const {
183  return steps.at(i)->g_dq_norm();
184  }
185  double g_last_dq_norm(void) const {
186  if (steps.size() > 1)
187  return steps[steps.size()-2]->g_dq_norm();
188  else return 0.0;
189  }
190  double g_dq_gradient(int i) const {
191  return steps.at(i)->g_dq_gradient();
192  }
193  double g_last_dq_gradient(void) const {
194  if (steps.size() > 1)
195  return steps[steps.size()-2]->g_dq_gradient();
196  else return 0.0;
197  }
198  double g_dq_hessian(int i) const {
199  return steps.at(i)->g_dq_hessian();
200  }
201 
202  bool previous_step_report() const;
203 
204  // check convergence of current step
205  bool conv_check(opt::MOLECULE &) const;
206 
207  // summarize optimization up til now
208  void summary(void) const;
209 
210  // perform Hessian update
211  void H_update(opt::MOLECULE & mol);
212 
213  // read in cartesian Hessian
214  double ** read_cartesian_H(void) const;
215 
216  // return number of steps present
217  int nsteps(void) const { return steps.size(); }
218 
219  void decrement_iteration(void) { --iteration; }
220 
221  void increment_consecutive_backsteps(void) { ++consecutive_backsteps; }
223  previous_consecutive_backsteps = consecutive_backsteps; // only used in current memory; not saved
224  consecutive_backsteps = 0;
225  }
227  consecutive_backsteps = previous_consecutive_backsteps; // for last second aborts after reset has been done
228  }
229  int g_consecutive_backsteps(void) { return consecutive_backsteps; }
230 
231  int g_steps_since_last_H(void) const { return steps_since_last_H; }
232  void reset_steps_since_last_H(void) { steps_since_last_H = 0; }
233  void increment_steps_since_last_H(void) { ++steps_since_last_H; }
234 
235  void erase_last_step(void) { // free last step
236  delete steps.back();
237  steps.erase(steps.end()-1);
238  }
239  void erase_step(int i) {
240  delete steps[i];
241  steps.erase(steps.begin() + i);
242  }
244  iteration = steps.size() + 1;
245  }
246  void increase_trust_radius(void) const;
247  void decrease_trust_radius(void) const;
248  void reset_trust_radius(void) const;
249 
250 };
251 
252 }
253 
254 #endif
int g_consecutive_backsteps(void)
Definition: opt_data.h:229
double * g_geom_const_pointer(void) const
Definition: opt_data.h:85
double g_energy(void) const
Definition: opt_data.h:133
double * g_dq_pointer(int i) const
Definition: opt_data.h:179
double g_energy(int i) const
Definition: opt_data.h:165
int Ncart
Definition: opt_data.h:97
void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in, double dq_gradient_in, double dq_hessian_in)
Definition: opt_data.h:124
double * g_last_forces_pointer(void) const
Definition: opt_data.h:171
double dq_gradient
Definition: opt_data.h:60
void erase_last_step(void)
Definition: opt_data.h:235
int iteration
Definition: opt_data.h:99
double g_dq_norm(int i) const
Definition: opt_data.h:182
void reset_iteration_to_size(void)
Definition: opt_data.h:243
double * g_dq_pointer(void) const
Definition: opt_data.h:147
double g_DE_predicted(void) const
Definition: opt_data.h:88
double g_dq_norm(void) const
Definition: opt_data.h:89
double DE_predicted
Definition: opt_data.h:57
double dq_hessian
Definition: opt_data.h:61
double dq_norm
Definition: opt_data.h:59
double g_dq_gradient(void) const
Definition: opt_data.h:90
double * g_rfo_eigenvector_pointer(void) const
Definition: opt_data.h:134
Definition: pointgrp.h:105
double * g_geom_const_pointer(int i) const
Definition: opt_data.h:176
double * f_q
Definition: opt_data.h:54
Definition: optking/molecule.h:46
double ** H
Definition: opt_data.h:98
int g_steps_since_last_H(void) const
Definition: opt_data.h:231
double * geom
Definition: opt_data.h:55
double * unit_step
Definition: opt_data.h:58
std::vector< STEP_DATA * > steps
Definition: opt_data.h:105
int steps_since_last_H
Definition: opt_data.h:101
void reset_steps_since_last_H(void)
Definition: opt_data.h:232
double * g_forces_pointer(void) const
Definition: opt_data.h:84
int consecutive_backsteps
Definition: opt_data.h:102
void restore_previous_consecutive_backsteps(void)
Definition: opt_data.h:226
double * g_dq_pointer(void) const
Definition: opt_data.h:86
int Ncoord(void) const
Definition: opt_data.h:136
int previous_consecutive_backsteps
Definition: opt_data.h:103
double g_dq_hessian(void) const
Definition: opt_data.h:91
void erase_step(int i)
Definition: opt_data.h:239
void increment_consecutive_backsteps(void)
Definition: opt_data.h:221
int Nintco
Definition: opt_data.h:96
Definition: opt_data.h:52
double g_last_energy(void) const
Definition: opt_data.h:151
double g_dq_gradient(int i) const
Definition: opt_data.h:190
void save_geom_energy(double *geom_in, double energy_in)
Definition: opt_data.h:119
int g_iteration(void) const
Definition: opt_data.h:131
void set_rfo_eigenvector(double *evect_in)
Definition: opt_data.h:138
double * rfo_eigenvector
Definition: opt_data.h:104
double g_last_DE_predicted(void) const
Definition: opt_data.h:158
void increment_steps_since_last_H(void)
Definition: opt_data.h:233
double g_last_dq_gradient(void) const
Definition: opt_data.h:193
void decrement_iteration(void)
Definition: opt_data.h:219
double * dq
Definition: opt_data.h:62
double g_energy(void) const
Definition: opt_data.h:87
double * g_forces_pointer(int i) const
Definition: opt_data.h:168
double g_dq_hessian(int i) const
Definition: opt_data.h:198
int nsteps(void) const
Definition: opt_data.h:217
Definition: opt_data.h:95
double energy
Definition: opt_data.h:56
double g_last_dq_norm(void) const
Definition: opt_data.h:185
double * g_forces_pointer(void) const
Definition: opt_data.h:144
void energy(struct RHO_Params)
Definition: ccdensity/energy.cc:75
double ** g_H_pointer(void)
Definition: opt_data.h:132
void reset_consecutive_backsteps(void)
Definition: opt_data.h:222