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