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 // data for one optimization step
51 class STEP_DATA {
52 
53  double *f_q; // Internal coordinate forces
54  double *geom; // cartesian coordinate values
55  double energy; // total energy
56  double DE_predicted; // energy drop predicted for next step
57  double *unit_step; // unit vector in direction of step in the basis of internal coordinates
58  double dq_norm; // norm of step in internal coordinates
59  double dq_gradient; // gradient along step
60  double dq_hessian; // hessian along step
61  double *dq; // step in internal coordinates
62 
63  public:
64  //STEP_DATA(ifstream & fin, int Nintco, int Ncart); // read in date for one step
65 
66  STEP_DATA(int Nintco, int Ncart); // allocate memory only
67 
68  ~STEP_DATA(); // free memory
69 
70  // save geometry and energy
71  void save_geom_energy(double *geom_in, double energy_in, int Ncart);
72 
73  // save rest of stuff
74  void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in,
75  double dq_gradient_in, double dq_hessian_in, int Nintco);
76 
77  // functions to read and write a step to the binary file
78  void write(int istep, int Nintco, int Ncart);
79  // read step from binary file
80  void read(int istep, int Nintco, int Ncart);
81 
82  // functions to retrieve data
83  double *g_forces_pointer(void) const { return f_q; }
84  double *g_geom_const_pointer(void) const { return geom; }
85  double *g_dq_pointer(void) const { return dq; }
86  double g_energy(void) const { return energy; }
87  double g_DE_predicted(void) const { return DE_predicted; }
88  double g_dq_norm(void) const { return dq_norm; }
89  double g_dq_gradient(void) const { return dq_gradient; }
90  double g_dq_hessian(void) const { return dq_hessian; }
91 };
92 
93 // data for an optimization
94 class OPT_DATA {
95  int Nintco; // num. of internal coordinates
96  int Ncart; // num. of cartesian coordinates
97  double **H; // Hessian matrix
98  int iteration; // num. of current iteration, 0, 1, 2, ...
99  // # of previous steps of data stored should be == iteration
100  int steps_since_last_H; // number of steps since H has been computed
101  int consecutive_backsteps; // # of consecutive steps backwards, if any
102  int previous_consecutive_backsteps; // only used in current memory; not saved
103  double *rfo_eigenvector; // for RFO root-following
104  std::vector<STEP_DATA *> steps;
105 
106  public:
107 
108  // allocates memory for this step; reads in old steps from binary file
109  OPT_DATA(int Nintco_in, int Ncart_in);
110 
111  // free memory
112  ~OPT_DATA();
113 
114  // write data to binary file
115  void write(void);
116 
117  // save geometry and energy to current (last) step
118  void save_geom_energy(double *geom_in, double energy_in) {
119  steps[steps.size()-1]->save_geom_energy(geom_in, energy_in, Ncart);
120  }
121 
122  // save rest of stuff to current (last) step
123  void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in,
124  double dq_gradient_in, double dq_hessian_in) {
125  steps[steps.size()-1]->save_step_info(DE_predicted_in, unit_step_in, dq_norm_in,
126  dq_gradient_in, dq_hessian_in, Nintco);
127  }
128 
129  // return (pointers) to current-step data
130  int g_iteration(void) const { return iteration; }
131  double **g_H_pointer(void) { return H; }
132  double g_energy(void) const { return steps[steps.size()-1]->g_energy(); }
133  double *g_rfo_eigenvector_pointer(void) const { return rfo_eigenvector; }
134  // return dimension of Hessian matrix
135  int Ncoord(void) const { return Nintco; }
136 
137  void set_rfo_eigenvector(double *evect_in) {
138  for (int i=0; i<Nintco; ++i)
139  rfo_eigenvector[i] = evect_in[i];
140  }
141 
142  // step data
143  double *g_forces_pointer(void) const {
144  return steps[steps.size()-1]->g_forces_pointer();
145  }
146  double *g_dq_pointer(void) const {
147  return steps[steps.size()-1]->g_dq_pointer();
148  }
149  // return energy from the previous step (last entry - 1)
150  double g_last_energy(void) const {
151  if (steps.size() > 1)
152  return steps[steps.size()-2]->g_energy();
153  else return 0.0;
154  }
155 
156  // return predicted energy change at the previous step (last entry - 1)
157  double g_last_DE_predicted(void) const {
158  if (steps.size() > 1)
159  return steps[steps.size()-2]->g_DE_predicted();
160  else return 0.0;
161  }
162 
163  // return pointers to arbitrary-step data (pass in index starting at 0 ...)
164  double g_energy(int i) const {
165  return steps[i]->g_energy();
166  }
167  double *g_forces_pointer(int i) const {
168  return steps.at(i)->g_forces_pointer();
169  }
170  double *g_last_forces_pointer(void) const {
171  if (steps.size() > 1)
172  return steps.at(steps.size()-2)->g_forces_pointer();
173  else return NULL;
174  }
175  double *g_geom_const_pointer(int i) const {
176  return steps.at(i)->g_geom_const_pointer();
177  }
178  double *g_dq_pointer(int i) const {
179  return steps.at(i)->g_dq_pointer();
180  }
181  double g_dq_norm(int i) const {
182  return steps.at(i)->g_dq_norm();
183  }
184  double g_last_dq_norm(void) const {
185  if (steps.size() > 1)
186  return steps[steps.size()-2]->g_dq_norm();
187  else return 0.0;
188  }
189  double g_dq_gradient(int i) const {
190  return steps.at(i)->g_dq_gradient();
191  }
192  double g_last_dq_gradient(void) const {
193  if (steps.size() > 1)
194  return steps[steps.size()-2]->g_dq_gradient();
195  else return 0.0;
196  }
197  double g_dq_hessian(int i) const {
198  return steps.at(i)->g_dq_hessian();
199  }
200 
201  bool previous_step_report() const;
202 
203  // check convergence of current step
204  bool conv_check(opt::MOLECULE &) const;
205 
206  // summarize optimization up til now
207  void summary(void) const;
208 
209  // perform Hessian update
210  void H_update(opt::MOLECULE & mol);
211 
212  // read in cartesian Hessian
213  double ** read_cartesian_H(void) const;
214 
215  // return number of steps present
216  int nsteps(void) const { return steps.size(); }
217 
218  void decrement_iteration(void) { --iteration; }
219 
222  previous_consecutive_backsteps = consecutive_backsteps; // only used in current memory; not saved
224  }
226  consecutive_backsteps = previous_consecutive_backsteps; // for last second aborts after reset has been done
227  }
229 
230  int g_steps_since_last_H(void) const { return steps_since_last_H; }
233 
234  void erase_last_step(void) { // free last step
235  delete steps.back();
236  steps.erase(steps.end()-1);
237  }
238  void erase_step(int i) {
239  delete steps[i];
240  steps.erase(steps.begin() + i);
241  }
243  iteration = steps.size() + 1;
244  }
245  void increase_trust_radius(void) const;
246  void decrease_trust_radius(void) const;
247  void reset_trust_radius(void) const;
248 
249 };
250 
251 }
252 
253 #endif
void write(int istep, int Nintco, int Ncart)
Definition: opt_data.cc:567
int g_consecutive_backsteps(void)
Definition: opt_data.h:228
void read(int istep, int Nintco, int Ncart)
Definition: opt_data.cc:543
double * g_geom_const_pointer(void) const
Definition: opt_data.h:84
double g_energy(void) const
Definition: opt_data.h:132
bool conv_check(opt::MOLECULE &) const
Definition: opt_data.cc:98
double * g_dq_pointer(int i) const
Definition: opt_data.h:178
double g_energy(int i) const
Definition: opt_data.h:164
int Ncart
Definition: opt_data.h:96
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:123
void summary(void) const
Definition: opt_data.cc:275
double * g_last_forces_pointer(void) const
Definition: opt_data.h:170
double dq_gradient
Definition: opt_data.h:59
void erase_last_step(void)
Definition: opt_data.h:234
int iteration
Definition: opt_data.h:98
double g_dq_norm(int i) const
Definition: opt_data.h:181
void reset_iteration_to_size(void)
Definition: opt_data.h:242
double * g_dq_pointer(void) const
Definition: opt_data.h:146
double g_DE_predicted(void) const
Definition: opt_data.h:87
double g_dq_norm(void) const
Definition: opt_data.h:88
double DE_predicted
Definition: opt_data.h:56
double dq_hessian
Definition: opt_data.h:60
double dq_norm
Definition: opt_data.h:58
double g_dq_gradient(void) const
Definition: opt_data.h:89
OPT_DATA(int Nintco_in, int Ncart_in)
Definition: opt_data.cc:590
double * g_rfo_eigenvector_pointer(void) const
Definition: opt_data.h:133
~OPT_DATA()
Definition: opt_data.cc:87
double ** read_cartesian_H(void) const
Definition: geom_gradients_io.cc:363
Definition: pointgrp.h:106
void H_update(opt::MOLECULE &mol)
Definition: opt_data.cc:310
double * g_geom_const_pointer(int i) const
Definition: opt_data.h:175
void increase_trust_radius(void) const
Definition: opt_data.cc:720
double * f_q
Definition: opt_data.h:53
Definition: optking/molecule.h:47
double ** H
Definition: opt_data.h:97
int g_steps_since_last_H(void) const
Definition: opt_data.h:230
double * geom
Definition: opt_data.h:54
double * unit_step
Definition: opt_data.h:57
std::vector< STEP_DATA * > steps
Definition: opt_data.h:104
int steps_since_last_H
Definition: opt_data.h:100
void reset_trust_radius(void) const
Definition: opt_data.cc:761
void reset_steps_since_last_H(void)
Definition: opt_data.h:231
double * g_forces_pointer(void) const
Definition: opt_data.h:83
int consecutive_backsteps
Definition: opt_data.h:101
void restore_previous_consecutive_backsteps(void)
Definition: opt_data.h:225
double * g_dq_pointer(void) const
Definition: opt_data.h:85
int Ncoord(void) const
Definition: opt_data.h:135
int previous_consecutive_backsteps
Definition: opt_data.h:102
double g_dq_hessian(void) const
Definition: opt_data.h:90
void erase_step(int i)
Definition: opt_data.h:238
void increment_consecutive_backsteps(void)
Definition: opt_data.h:220
int Nintco
Definition: opt_data.h:95
void save_geom_energy(double *geom_in, double energy_in, int Ncart)
Definition: opt_data.cc:72
Definition: opt_data.h:51
double g_last_energy(void) const
Definition: opt_data.h:150
double g_dq_gradient(int i) const
Definition: opt_data.h:189
void save_geom_energy(double *geom_in, double energy_in)
Definition: opt_data.h:118
int g_iteration(void) const
Definition: opt_data.h:130
void decrease_trust_radius(void) const
Definition: opt_data.cc:742
void set_rfo_eigenvector(double *evect_in)
Definition: opt_data.h:137
STEP_DATA(int Nintco, int Ncart)
Definition: opt_data.cc:51
double * rfo_eigenvector
Definition: opt_data.h:103
double g_last_DE_predicted(void) const
Definition: opt_data.h:157
void increment_steps_since_last_H(void)
Definition: opt_data.h:232
double g_last_dq_gradient(void) const
Definition: opt_data.h:192
void decrement_iteration(void)
Definition: opt_data.h:218
double * dq
Definition: opt_data.h:61
void write(void)
Definition: opt_data.cc:644
double g_energy(void) const
Definition: opt_data.h:86
double * g_forces_pointer(int i) const
Definition: opt_data.h:167
double g_dq_hessian(int i) const
Definition: opt_data.h:197
int nsteps(void) const
Definition: opt_data.h:216
Definition: opt_data.h:94
double energy
Definition: opt_data.h:55
void save_step_info(double DE_predicted_in, double *unit_step_in, double dq_norm_in, double dq_gradient_in, double dq_hessian_in, int Nintco)
Definition: opt_data.cc:78
bool previous_step_report() const
Definition: opt_data.cc:666
double g_last_dq_norm(void) const
Definition: opt_data.h:184
double * g_forces_pointer(void) const
Definition: opt_data.h:143
double ** g_H_pointer(void)
Definition: opt_data.h:131
void reset_consecutive_backsteps(void)
Definition: opt_data.h:221
~STEP_DATA()
Definition: opt_data.cc:64