1#ifndef MADNESS_MOLOPT_H
2#define MADNESS_MOLOPT_H
33 const double energy_precision;
34 const double gradient_precision;
35 const int print_level;
38 Tensor<double> hessian;
41 Tensor<double> new_search_direction(
const Tensor<double> &
g,
42 const Tensor<double> &
h)
const {
44 double tol = gradient_precision;
45 double trust = std::min(maxstep *
g.dim(0), 1.0);
50 print(
"hessian eigenvalues",
e);
53 Tensor<double> gv =
inner(
g,
v);
55 print(
"spectral gradient", gv);
58 int nneg = 0, nsmall = 0, nrestrict = 0;
59 for (
int i = 0; i <
e.dim(0); i++) {
63 print(
"skipping redundant mode", i);
67 printf(
" forcing negative eigenvalue to be positive %d %.1e\n", i,
71 }
else if (
e[i] < tol) {
73 printf(
" forcing small eigenvalue to be positive %d %.1e\n", i,
79 gv[i] = -gv[i] /
e[i];
82 double gvnew = trust *
std::abs(gv(i)) / gv[i];
84 printf(
" restricting step in spectral direction %d %.1e --> %.1e\n",
90 if (print_level > 0 && (nneg || nsmall || nrestrict))
91 printf(
" nneg=%d nsmall=%d nrestrict=%d\n", nneg, nsmall, nrestrict);
97 print(
"cartesian dx before restriction", gv);
100 bool printing =
false;
101 for (
int i = 0; i < gv.dim(0); i++) {
102 if (fabs(gv[i]) > maxstep) {
103 gv[i] = maxstep * gv[i] / fabs(gv[i]);
104 if (print_level > 0) {
106 printf(
" restricting step in Cartesian direction");
122 Tensor<double> make_projector(
const Molecule &
molecule) {
124 const Tensor<double> coords =
molecule.get_all_coords();
128 Tensor<double>
V(6, natom, 3);
130 for (
int k = 0;
k < 3;
k++)
131 V(
k, _,
k) = 1.0 / std::sqrt(
static_cast<double>(natom));
133 Tensor<double> centroid(3);
134 for (
int k = 0;
k < 3;
k++)
135 centroid(
k) = coords(_,
k).sum() / natom;
137 print(
"centroid", centroid);
139 for (
int i = 0; i < natom; i++) {
140 double x = coords(i, 0) - centroid[0];
141 double y = coords(i, 1) - centroid[1];
142 double z = coords(i, 2) - centroid[2];
157 V =
V.reshape(6, 3 * natom);
160 print(
"V before orthonormal");
166 for (
int i = 3; i < 6; i++) {
167 V(i, _).scale(1.0 /
V(i, _).normf());
168 for (
int j = 0; j < i; j++) {
169 double s =
V(i, _).trace(
V(j, _));
170 V(i, _) -=
V(j, _) *
s;
172 double vnorm =
V(i, _).normf();
174 V(i, _) *= 1.0 / vnorm;
182 for (
int i = 0; i < 3 * natom; i++)
192 template <
typename targetT>
194 const Tensor<double> &dx,
double energy0,
double dxgrad,
198 const char *lsmode =
"";
200 Tensor<double> x =
molecule.get_all_coords().flat();
203 if (dxgrad *
a1 > 0.0) {
205 print(
" line search gradient +ve ",
a1, dxgrad);
210 energy1 =
target.value(x +
a1 * dx);
213 hess = 2.0 * (energy1 - energy0 -
a1 * dxgrad) / (
a1 *
a1);
220 }
else if (hess > 0.0) {
221 if ((energy1 - energy0) <= -energy_precision) {
232 if ((energy1 - energy0) < energy_precision) {
248 double energy2 = energy0 + dxgrad *
a2 + 0.5 * hess *
a2 *
a2;
250 if (print_level > 0) {
251 printf(
"\n line search grad=%.2e hess=%.2e mode=%s newstep=%.3f\n",
252 dxgrad, hess, lsmode,
a2);
253 printf(
" predicted %.12e\n\n", energy2);
260 MolOpt(
int maxiter = 20,
double maxstep = 0.1,
double etol = 1
e-4,
261 double gtol = 1
e-3,
double xtol = 1
e-3,
double energy_precision = 1
e-5,
262 double gradient_precision = 1
e-4,
int print_level = 1,
263 std::string
update =
"BFGS")
265 etol(
std::
max(etol, energy_precision)),
266 gtol(
std::
max(gtol, gradient_precision)), xtol(xtol),
267 energy_precision(energy_precision),
268 gradient_precision(gradient_precision), print_level(print_level),
272 if (print_level > 0) {
277 print(
" maximum step", maxstep);
278 print(
" energy convergence", etol);
279 print(
" gradient convergence", gtol);
280 print(
" cartesian convergence", xtol);
281 print(
" energy precision", energy_precision);
282 print(
" gradient precision", gradient_precision);
287 void set_hessian(
const Tensor<double> &
h) { hessian =
h; }
289 const Tensor<double> &get_hessian()
const {
return hessian; }
291 void initialize_hessian(
const Molecule &
molecule) {
293 hessian = Tensor<double>(
N,
N);
294 for (
int i = 0; i <
N; i++)
298 template <
typename targetT>
299 Molecule optimize(Molecule
molecule,
306 if (hessian.size() == 0)
310 Tensor<double> gp(3 * natom);
311 Tensor<double> dx(3 * natom);
315 for (
int iter = 0; iter <
maxiter; iter++) {
317 print(
"\n\n Geometry optimization iteration", iter,
"\n");
327 double dxmax = dx.absmax();
328 double gmax =
g.absmax();
330 bool dxconv = (iter > 0) && (dxmax < xtol);
331 bool gconv = gmax < gtol;
332 bool econv = (iter > 0) && (
std::abs(de) < etol);
333 bool converged = econv && dxconv && gconv;
335 if (!converged && gmax < gradient_precision) {
337 print(
"\nInsufficient precision in gradient to proceed further -- "
338 "forcing convergence\n");
342 if (print_level > 0) {
343 const char *tf[] = {
"F",
"T"};
346 " energy delta-e max-dx max-g e dx g\n");
347 printf(
" ---------------- --------- --------- --------- --- --- "
349 printf(
" %15.6f %9.2e %9.2e %9.2e %s %s %s\n",
e, de, dxmax,
350 gmax, tf[econv], tf[dxconv], tf[gconv]);
358 print(
"\n Geometry optimization converged!\n");
365 Tensor<double>
P = make_projector(
molecule);
370 print(
"gradient after projection",
g);
373 if ((
g - gp).absmax() < 2.0 * gradient_precision) {
375 print(
" skipping hessian update due to insufficient precision in "
377 }
else if (
update ==
"bfgs") {
379 }
else if (
update ==
"sr1") {
382 throw "unknown update";
390 const double shift = 1000.0;
392 if (print_level > 1) {
396 for (
int i = 0; i < 3 * natom; i++)
398 if (print_level > 1) {
405 dx = new_search_direction(
g, PHPS);
415 print(
"scaled dx", dx);
418 Tensor<double> x =
molecule.get_all_coords().flat();
420 molecule.set_all_coords(x.reshape(natom, 3));
423 print(
"new molecular coords");
429 template <
typename targetT>
430 auto optimize_app(Molecule
molecule,
432 -> OptimizationResults {
437 OptimizationResults results;
439 if (hessian.size() == 0)
443 Tensor<double> gp(3 * natom);
444 Tensor<double> dx(3 * natom);
449 for (iter = 0; iter <
maxiter; iter++) {
451 print(
"\n\n Geometry optimization iteration", iter,
"\n");
461 double dxmax = dx.absmax();
462 results.max_gradient =
g.absmax();
464 bool dxconv = (iter > 0) && (dxmax < xtol);
465 bool gconv = results.max_gradient < gtol;
466 bool econv = (iter > 0) && (
std::abs(de) < etol);
467 bool converged = econv && dxconv && gconv;
469 if (!converged && results.max_gradient < gradient_precision) {
471 print(
"\nInsufficient precision in gradient to proceed further -- "
472 "forcing convergence\n");
476 if (print_level > 0) {
477 const char *tf[] = {
"F",
"T"};
480 " energy delta-e max-dx max-g e dx g\n");
481 printf(
" ---------------- --------- --------- --------- --- --- "
483 printf(
" %15.6f %9.2e %9.2e %9.2e %s %s %s\n",
e, de, dxmax,
484 results.max_gradient, tf[econv], tf[dxconv], tf[gconv]);
492 print(
"\n Geometry optimization converged!\n");
499 Tensor<double>
P = make_projector(
molecule);
504 print(
"gradient after projection",
g);
507 if ((
g - gp).absmax() < 2.0 * gradient_precision) {
509 print(
" skipping hessian update due to insufficient precision in "
511 }
else if (
update ==
"bfgs") {
513 }
else if (
update ==
"sr1") {
516 throw "unknown update";
524 const double shift = 1000.0;
526 if (print_level > 1) {
530 for (
int i = 0; i < 3 * natom; i++)
532 if (print_level > 1) {
539 dx = new_search_direction(
g, PHPS);
549 print(
"scaled dx", dx);
552 Tensor<double> x =
molecule.get_all_coords().flat();
554 molecule.set_all_coords(x.reshape(natom, 3));
557 print(
"new molecular coords");
560 results.final_energy = ep;
561 results.nsteps = iter;
void hessian_update_bfgs(const Tensor< double > &dx, const Tensor< double > &dg)
Definition kain.cc:329
void hessian_update_sr1(const Tensor< double > &s, const Tensor< double > &y)
Definition kain.cc:317
static double shift
Definition dirac-hatom.cc:19
std::complex< double > inner(const Fcwf &psi, const Fcwf &phi)
Definition fcwf.cc:275
Tensor< T > transpose(const Tensor< T > &t)
Returns a new deep copy of the transpose of the input tensor.
Definition tensor.h:2035
const int maxiter
Definition gygi_soltion.cc:68
static const double v
Definition hatom_sf_dirac.cc:20
#define max(a, b)
Definition lda.h:51
void print(const tensorT &t)
Definition mcpfit.cc:140
Namespace for all elements and tools of MADNESS.
Definition DFParameters.h:10
void print_justified(const char *s, int column, bool underline)
Print a string justified on the left to start at the given column with optional underlining.
Definition print.cc:75
void syev(const Tensor< T > &A, Tensor< T > &V, Tensor< typename Tensor< T >::scalar_type > &e)
Real-symmetric or complex-Hermitian eigenproblem.
Definition lapack.cc:969
static long abs(long a)
Definition tensor.h:219
static const long k
Definition rk.cc:44
Defines interfaces for optimization and non-linear equation solvers.
static double V(const coordT &r)
Definition tdse.cc:288
Defines and implements most of Tensor.
Prototypes for a partial interface from Tensor to LAPACK.
int P
Definition test_binsorter.cc:9
void e()
Definition test_sig.cc:75
#define N
Definition testconv.cc:37
vector_complex_function_3d update(World &world, const vector_complex_function_3d &psi, vector_complex_function_3d &vpsi, const tensor_real &e, int iter)
Definition testcosine.cc:210
static const double alpha
Definition testcosine.cc:10
double g(const coord_t &r)
Definition testgconv.cc:116
double h(const coord_1d &r)
Definition testgconv.cc:175
static Molecule molecule
Definition testperiodicdft.cc:39
const double a2
Definition vnucso.cc:86
const double a1
Definition vnucso.cc:85
FLOAT target(const FLOAT &x)
Definition y.cc:295