1#ifndef MADNESS_MOLOPT_H
2#define MADNESS_MOLOPT_H
34 const double energy_precision;
35 const double gradient_precision;
36 const int print_level;
39 Tensor<double> hessian;
42 Tensor<double> new_search_direction(
const Tensor<double> &
g,
43 const Tensor<double> &
h)
const {
45 double tol = gradient_precision;
46 double trust = std::min(maxstep *
g.dim(0), 1.0);
51 print(
"hessian eigenvalues",
e);
54 Tensor<double> gv =
inner(
g,
v);
56 print(
"spectral gradient", gv);
59 int nneg = 0, nsmall = 0, nrestrict = 0;
60 for (
int i = 0; i <
e.dim(0); i++) {
64 print(
"skipping redundant mode", i);
68 printf(
" forcing negative eigenvalue to be positive %d %.1e\n", i,
72 }
else if (
e[i] < tol) {
74 printf(
" forcing small eigenvalue to be positive %d %.1e\n", i,
80 gv[i] = -gv[i] /
e[i];
83 double gvnew = trust *
std::abs(gv(i)) / gv[i];
85 printf(
" restricting step in spectral direction %d %.1e --> %.1e\n",
91 if (print_level > 0 && (nneg || nsmall || nrestrict))
92 printf(
" nneg=%d nsmall=%d nrestrict=%d\n", nneg, nsmall, nrestrict);
98 print(
"cartesian dx before restriction", gv);
101 bool printing =
false;
102 for (
int i = 0; i < gv.dim(0); i++) {
103 if (fabs(gv[i]) > maxstep) {
104 gv[i] = maxstep * gv[i] / fabs(gv[i]);
105 if (print_level > 0) {
107 printf(
" restricting step in Cartesian direction");
124 Tensor<double> make_projector(
const Molecule &
molecule)
const {
127 return Tensor<double>(0
L, 0
L);
128 const Tensor<double> coords =
molecule.get_all_coords();
132 Tensor<double>
V(6, natom, 3);
134 for (
int k = 0;
k < 3;
k++)
135 V(
k, _,
k) = 1.0 / std::sqrt(
static_cast<double>(natom));
137 Tensor<double> centroid(3);
138 for (
int k = 0;
k < 3;
k++)
139 centroid(
k) = coords(_,
k).
sum() / natom;
141 print(
"centroid", centroid);
143 for (
int i = 0; i < natom; i++) {
144 double x = coords(i, 0) - centroid[0];
145 double y = coords(i, 1) - centroid[1];
146 double z = coords(i, 2) - centroid[2];
161 V =
V.reshape(6, 3 * natom);
163 if (print_level > 1) {
164 print(
"V before orthonormal");
170 for (
int i = 3; i < 6; i++) {
171 for (
int j = 0; j < 3; j++) {
172 double proj =
V(i, _).trace(
V(j, _));
173 V(i, _) -=
V(j, _) * proj;
185 const Tensor<double>
R =
copy(
V(Slice(3, 5), _));
186 Tensor<double> U,
s, VT;
189 print(
"rotation singular values", s);
191 constexpr double rel_tol = 1.0e-12;
192 const double smax =
s(0
L);
193 for (
int m = 0;
m < 3;
m++) {
194 const int row = 3 +
m;
195 if (smax > 0.0 &&
s(
static_cast<long>(
m)) > rel_tol * smax) {
196 V(row, _) = VT(
m, _);
197 for (
int j = 0; j < row; j++) {
198 double proj =
V(row, _).trace(
V(j, _));
199 V(row, _) -=
V(j, _) * proj;
201 double vnorm =
V(row, _).normf();
203 V(row, _) *= 1.0 / vnorm;
214 for (
int i = 0; i < 3 * natom; i++)
234 void print_gradient(
const Molecule &
molecule,
const Tensor<double> &graw,
235 const Tensor<double> &
g)
const {
236 const size_t natom =
molecule.natom();
240 double net[3] = {0.0, 0.0, 0.0};
241 for (
size_t i = 0; i < natom; ++i)
242 for (
int c = 0;
c < 3; ++
c)
243 net[
c] += graw[i * 3 +
c];
245 print(
"\n Gradient (a.u.), translations and rotations projected out\n"
246 " ---------------------------------------------------------\n");
247 print(
" atom x y z dE/dx "
249 print(
" ------ ------------ ------------ ------------ ------------ "
250 "------------ ------------");
251 for (
size_t i = 0; i < natom; ++i) {
252 const Atom &atom =
molecule.get_atom(i);
253 printf(
" %5d %12.6f %12.6f %12.6f %12.6f %12.6f %12.6f\n",
int(i), atom.
x,
254 atom.
y, atom.
z,
g[i * 3 + 0],
g[i * 3 + 1],
g[i * 3 + 2]);
256 printf(
" max element of the projected gradient %12.3e\n",
g.absmax());
257 printf(
" net force removed by the projection %12.3e %12.3e %12.3e\n",
258 net[0], net[1], net[2]);
265 template <
typename targetT>
267 const Tensor<double> &dx,
double energy0,
double dxgrad,
271 const char *lsmode =
"";
273 Tensor<double> x =
molecule.get_all_coords().flat();
276 if (dxgrad *
a1 > 0.0) {
278 print(
" line search gradient +ve ",
a1, dxgrad);
283 energy1 =
target.value(x +
a1 * dx);
286 hess = 2.0 * (energy1 - energy0 -
a1 * dxgrad) / (
a1 *
a1);
293 }
else if (hess > 0.0) {
294 if ((energy1 - energy0) <= -energy_precision) {
305 if ((energy1 - energy0) < energy_precision) {
321 double energy2 = energy0 + dxgrad *
a2 + 0.5 * hess *
a2 *
a2;
323 if (print_level > 0) {
324 printf(
"\n line search grad=%.2e hess=%.2e mode=%s newstep=%.3f\n",
325 dxgrad, hess, lsmode,
a2);
326 printf(
" predicted %.12e\n\n", energy2);
333 MolOpt(
int maxiter = 20,
double maxstep = 0.1,
double etol = 1
e-4,
334 double gtol = 1
e-3,
double xtol = 1
e-3,
double energy_precision = 1
e-5,
335 double gradient_precision = 1
e-4,
int print_level = 1,
336 std::string
update =
"BFGS")
338 etol(std::
max(etol, energy_precision)),
339 gtol(std::
max(gtol, gradient_precision)), xtol(xtol),
340 energy_precision(energy_precision),
341 gradient_precision(gradient_precision), print_level(print_level),
345 if (print_level > 0) {
350 print(
" maximum step", maxstep);
351 print(
" energy convergence", etol);
352 print(
" gradient convergence", gtol);
353 print(
" cartesian convergence", xtol);
354 print(
" energy precision", energy_precision);
355 print(
" gradient precision", gradient_precision);
360 void set_hessian(
const Tensor<double> &
h) { hessian =
h; }
362 const Tensor<double> &get_hessian()
const {
return hessian; }
364 void initialize_hessian(
const Molecule &
molecule) {
366 hessian = Tensor<double>(
N,
N);
367 for (
int i = 0; i <
N; i++)
371 template <
typename targetT>
372 Molecule optimize(Molecule
molecule,
379 if (hessian.size() == 0)
383 Tensor<double> gp(3 * natom);
384 Tensor<double> dx(3 * natom);
388 for (
int iter = 0; iter <
maxiter; iter++) {
390 print(
"\n\n Geometry optimization iteration", iter,
"\n");
402 Tensor<double>
P = make_projector(
molecule);
403 const Tensor<double> graw =
copy(
g);
411 double dxmax = dx.absmax();
412 double gmax =
g.absmax();
414 "MolOpt::optimize: projected gradient is not finite");
416 bool dxconv = (iter > 0) && (dxmax < xtol);
417 bool gconv = gmax < gtol;
418 bool econv = (iter > 0) && (
std::abs(de) < etol);
419 bool converged = econv && dxconv && gconv;
421 if (!converged && gmax < gradient_precision) {
423 print(
"\nInsufficient precision in gradient to proceed further -- "
424 "forcing convergence\n");
428 if (print_level > 0) {
429 const char *tf[] = {
"F",
"T"};
432 " energy delta-e max-dx max-g e dx g\n");
433 printf(
" ---------------- --------- --------- --------- --- --- "
435 printf(
" %15.6f %9.2e %9.2e %9.2e %s %s %s\n",
e, de, dxmax,
436 gmax, tf[econv], tf[dxconv], tf[gconv]);
444 print(
"\n Geometry optimization converged!\n");
451 if ((
g - gp).absmax() < 2.0 * gradient_precision) {
453 print(
" skipping hessian update due to insufficient precision in "
455 }
else if (
update ==
"bfgs") {
457 }
else if (
update ==
"sr1") {
460 throw "unknown update";
468 const double shift = 1000.0;
470 if (print_level > 1) {
474 for (
int i = 0; i < 3 * natom; i++)
476 if (print_level > 1) {
483 dx = new_search_direction(
g, PHPS);
493 print(
"scaled dx", dx);
501 print(
"new molecular coords");
507 template <
typename targetT>
508 auto optimize_app(Molecule
molecule,
510 -> OptimizationResults {
515 OptimizationResults results;
517 if (hessian.size() == 0)
521 Tensor<double> gp(3 * natom);
522 Tensor<double> dx(3 * natom);
527 for (iter = 0; iter <
maxiter; iter++) {
529 print(
"\n\n Geometry optimization iteration", iter,
"\n");
541 Tensor<double>
P = make_projector(
molecule);
542 const Tensor<double> graw =
copy(
g);
550 double dxmax = dx.absmax();
551 results.max_gradient =
g.absmax();
553 "MolOpt::optimize_app: projected gradient is not finite");
555 bool dxconv = (iter > 0) && (dxmax < xtol);
556 bool gconv = results.max_gradient < gtol;
557 bool econv = (iter > 0) && (
std::abs(de) < etol);
558 bool converged = econv && dxconv && gconv;
560 if (!converged && results.max_gradient < gradient_precision) {
562 print(
"\nInsufficient precision in gradient to proceed further -- "
563 "forcing convergence\n");
567 if (print_level > 0) {
568 const char *tf[] = {
"F",
"T"};
571 " energy delta-e max-dx max-g e dx g\n");
572 printf(
" ---------------- --------- --------- --------- --- --- "
574 printf(
" %15.6f %9.2e %9.2e %9.2e %s %s %s\n",
e, de, dxmax,
575 results.max_gradient, tf[econv], tf[dxconv], tf[gconv]);
583 print(
"\n Geometry optimization converged!\n");
590 if ((
g - gp).absmax() < 2.0 * gradient_precision) {
592 print(
" skipping hessian update due to insufficient precision in "
594 }
else if (
update ==
"bfgs") {
596 }
else if (
update ==
"sr1") {
599 throw "unknown update";
607 const double shift = 1000.0;
609 if (print_level > 1) {
613 for (
int i = 0; i < 3 * natom; i++)
615 if (print_level > 1) {
622 dx = new_search_direction(
g, PHPS);
632 print(
"scaled dx", dx);
640 print(
"new molecular coords");
643 results.final_energy = ep;
644 results.nsteps = iter;
double x
Definition mentity.h:73
double y
Definition mentity.h:73
double z
Definition mentity.h:73
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
T sum() const
Returns the sum of all elements of the tensor.
Definition tensor.h:1667
Tensor< T > reshape(int ndimnew, const long *d)
Returns new view/tensor reshaping size/number of dimensions to conforming tensor.
Definition tensor.h:1389
Tensor< T > flat()
Returns new view/tensor rehshaping to flat (1-d) tensor.
Definition tensor.h:1560
static const double R
Definition csqrt.cc:46
static double shift
Definition dirac-hatom.cc:19
std::complex< double > inner(const Fcwf &psi, const Fcwf &phi)
Definition fcwf.cc:311
Fcwf copy(Fcwf psi)
Definition fcwf.cc:374
Tensor< T > transpose(const Tensor< T > &t)
Returns a new deep copy of the transpose of the input tensor.
Definition tensor.h:2039
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
#define MADNESS_CHECK_THROW(condition, msg)
Check a condition — even in a release build the condition is always evaluated so it can have side eff...
Definition madness_exception.h:207
void print(const tensorT &t)
Definition mcpfit.cc:140
Header to declare stuff which has not yet found a home.
Namespace for all elements and tools of MADNESS.
Definition DFConvergence.h:9
constexpr bool is_finite(double x) noexcept
Definition misc.h:48
void svd(const Tensor< T > &a, Tensor< T > &U, Tensor< typename Tensor< T >::scalar_type > &s, Tensor< T > &VT)
Compute the singluar value decomposition of an n-by-m matrix using *gesvd.
Definition lapack.cc:739
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:979
@ s
Definition gaussian.h:31
static long abs(long a)
Definition tensor.h:214
static const double c
Definition relops.cc:10
static const double m
Definition relops.cc:9
static const double L
Definition rk.cc:46
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