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);
49 if (print_level > 1)
print(
"hessian eigenvalues",
e);
52 Tensor<double> gv =
inner(
g,
v);
53 if (print_level > 1)
print(
"spectral gradient", gv);
56 int nneg = 0, nsmall = 0, nrestrict = 0;
57 for (
int i = 0; i <
e.dim(0); i++) {
60 if (print_level > 1)
print(
"skipping redundant mode", i);
64 printf(
" forcing negative eigenvalue to be positive %d %.1e\n", i,
68 }
else if (
e[i] < tol) {
70 printf(
" forcing small eigenvalue to be positive %d %.1e\n", i,
76 gv[i] = -gv[i] /
e[i];
79 double gvnew = trust *
std::abs(gv(i)) / gv[i];
81 printf(
" restricting step in spectral direction %d %.1e --> %.1e\n",
87 if (print_level > 0 && (nneg || nsmall || nrestrict))
88 printf(
" nneg=%d nsmall=%d nrestrict=%d\n", nneg, nsmall, nrestrict);
93 if (print_level > 1)
print(
"cartesian dx before restriction", gv);
96 bool printing =
false;
97 for (
int i = 0; i < gv.dim(0); i++) {
98 if (fabs(gv[i]) > maxstep) {
99 gv[i] = maxstep * gv[i] / fabs(gv[i]);
100 if (print_level > 0) {
101 if (!printing) printf(
" restricting step in Cartesian direction");
107 if (printing) printf(
"\n");
116 Tensor<double> make_projector(
const Molecule&
molecule) {
118 const Tensor<double> coords =
molecule.get_all_coords();
122 Tensor<double>
V(6, natom, 3);
124 for (
int k = 0;
k < 3;
k++)
125 V(
k, _,
k) = 1.0 / std::sqrt(
static_cast<double>(natom));
127 Tensor<double> centroid(3);
128 for (
int k = 0;
k < 3;
k++) centroid(
k) = coords(_,
k).sum() / natom;
129 if (print_level > 1)
print(
"centroid", centroid);
131 for (
int i = 0; i < natom; i++) {
132 double x = coords(i, 0) - centroid[0];
133 double y = coords(i, 1) - centroid[1];
134 double z = coords(i, 2) - centroid[2];
149 V =
V.reshape(6, 3 * natom);
151 if (print_level > 1)
print(
"V before orthonormal");
152 if (print_level > 1)
print(
V);
156 for (
int i = 3; i < 6; i++) {
157 V(i, _).scale(1.0 /
V(i, _).normf());
158 for (
int j = 0; j < i; j++) {
159 double s =
V(i, _).trace(
V(j, _));
160 V(i, _) -=
V(j, _) *
s;
162 double vnorm =
V(i, _).normf();
164 V(i, _) *= 1.0 / vnorm;
172 for (
int i = 0; i < 3 * natom; i++)
V(i, i) += 1.0;
181 template <
typename targetT>
183 const Tensor<double>& dx,
double energy0,
double dxgrad,
187 const char* lsmode =
"";
189 Tensor<double> x =
molecule.get_all_coords().flat();
192 if (dxgrad *
a1 > 0.0) {
193 if (print_level > 0)
print(
" line search gradient +ve ",
a1, dxgrad);
198 energy1 =
target.value(x +
a1 * dx);
201 hess = 2.0 * (energy1 - energy0 -
a1 * dxgrad) / (
a1 *
a1);
208 }
else if (hess > 0.0) {
209 if ((energy1 - energy0) <= -energy_precision) {
220 if ((energy1 - energy0) < energy_precision) {
236 double energy2 = energy0 + dxgrad *
a2 + 0.5 * hess *
a2 *
a2;
238 if (print_level > 0) {
239 printf(
"\n line search grad=%.2e hess=%.2e mode=%s newstep=%.3f\n",
240 dxgrad, hess, lsmode,
a2);
241 printf(
" predicted %.12e\n\n", energy2);
248 MolOpt(
int maxiter = 20,
double maxstep = 0.1,
double etol = 1
e-4,
249 double gtol = 1
e-3,
double xtol = 1
e-3,
double energy_precision = 1
e-5,
250 double gradient_precision = 1
e-4,
int print_level = 1,
251 std::string
update =
"BFGS")
254 etol(
std::
max(etol, energy_precision)),
255 gtol(
std::
max(gtol, gradient_precision)),
257 energy_precision(energy_precision),
258 gradient_precision(gradient_precision),
259 print_level(print_level),
263 if (print_level > 0) {
268 print(
" maximum step", maxstep);
269 print(
" energy convergence", etol);
270 print(
" gradient convergence", gtol);
271 print(
" cartesian convergence", xtol);
272 print(
" energy precision", energy_precision);
273 print(
" gradient precision", gradient_precision);
278 void set_hessian(
const Tensor<double>&
h) { hessian =
h; }
280 const Tensor<double>& get_hessian()
const {
return hessian; }
282 void initialize_hessian(
const Molecule&
molecule) {
284 hessian = Tensor<double>(
N,
N);
285 for (
int i = 0; i <
N; i++) hessian(i, i) = 0.5;
288 template <
typename targetT>
289 Molecule optimize(Molecule
molecule,
296 if (hessian.size() == 0) initialize_hessian(
molecule);
299 Tensor<double> gp(3 * natom);
300 Tensor<double> dx(3 * natom);
304 for (
int iter = 0; iter <
maxiter; iter++) {
306 print(
"\n\n Geometry optimization iteration", iter,
"\n");
307 if (print_level > 0)
molecule.print();
315 double dxmax = dx.absmax();
316 double gmax =
g.absmax();
318 bool dxconv = (iter > 0) && (dxmax < xtol);
319 bool gconv = gmax < gtol;
320 bool econv = (iter > 0) && (
std::abs(de) < etol);
321 bool converged = econv && dxconv && gconv;
323 if (!converged && gmax < gradient_precision) {
326 "\nInsufficient precision in gradient to proceed further -- "
327 "forcing convergence\n");
331 if (print_level > 0) {
332 const char* tf[] = {
"F",
"T"};
335 " energy delta-e max-dx max-g e dx g\n");
337 " ---------------- --------- --------- --------- --- --- "
339 printf(
" %15.6f %9.2e %9.2e %9.2e %s %s %s\n",
e, de, dxmax,
340 gmax, tf[econv], tf[dxconv], tf[gconv]);
347 if (print_level > 0)
print(
"\n Geometry optimization converged!\n");
348 if (print_level > 0)
molecule.print();
353 Tensor<double>
P = make_projector(
molecule);
357 if (print_level > 1)
print(
"gradient after projection",
g);
360 if ((
g - gp).absmax() < 2.0 * gradient_precision) {
363 " skipping hessian update due to insufficient precision in "
365 }
else if (
update ==
"bfgs") {
367 }
else if (
update ==
"sr1") {
370 throw "unknown update";
378 const double shift = 1000.0;
380 if (print_level > 1) {
384 for (
int i = 0; i < 3 * natom; i++) PHPS(i, i) +=
shift;
385 if (print_level > 1) {
392 dx = new_search_direction(
g, PHPS);
393 if (print_level > 1)
print(
"dx", dx);
399 if (print_level > 1)
print(
"scaled dx", dx);
402 Tensor<double> x =
molecule.get_all_coords().flat();
404 molecule.set_all_coords(x.reshape(natom, 3));
406 if (print_level > 1)
print(
"new molecular coords");
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
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:218
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.
template Tensor< int > transpose(const Tensor< int > &t)
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