36 #ifndef MADNESS_CHEM_MOLECULAR_OPTIMIZER_H__INCLUDED
37 #define MADNESS_CHEM_MOLECULAR_OPTIMIZER_H__INCLUDED
61 initialize < std::string > (
"update",
"bfgs",
"Quasi-Newton update method",{
"bfgs",
"sr1"});
62 initialize < int > (
"maxiter", 20);
63 initialize < double > (
"tol", 1.e-4,
"geometry convergence threshold for the gradient norm");
64 initialize < double > (
"value_precision", 1.e-12);
65 initialize < double > (
"gradient_precision", 1.e-12);
66 initialize < bool > (
"printtest",
false);
67 initialize < std::string > (
"cg_method",
"polak_ribiere",
"conjugate gradients update",{
"polak_ribiere",
"fletcher-reeves"});
68 initialize < std::vector<std::string> >
69 (
"remove_dof", {
"tx",
"ty",
"tz",
"rx",
"ry",
"rz"},
"degree of freedom projected out: translation/rotation");
82 std::string
update()
const {
return get<std::string>(
"update");}
83 std::string
cg_method()
const {
return get<std::string>(
"cg_method");}
84 int maxiter()
const {
return get<int>(
"maxiter");}
85 double tol()
const {
return get<double>(
"tol");}
88 bool printtest()
const {
return get<bool>(
"printtest");}
89 std::vector<std::string>
remove_dof()
const {
return get<std::vector<std::string> >(
"remove_dof");}
103 std::shared_ptr<MolecularOptimizationTargetInterface>
target;
113 const std::shared_ptr<MolecularOptimizationTargetInterface>& tar)
151 bool h_is_identity = (
h.
size() == 0);
157 for (
size_t i=0; i<
target->molecule().natom(); ++i) {
158 h(3*i ,3*i )=1.0/(
target->molecule().get_atom(i).mass);
159 h(3*i+1,3*i+1)=1.0/(
target->molecule().get_atom(i).mass);
160 h(3*i+2,3*i+2)=1.0/(
target->molecule().get_atom(i).mass);
168 madness::print(
"\ngopt: projected vibrational frequencies (cm-1)\n");
169 printf(
"frequency in cm-1 ");
170 for (
int i=0; i<freq.
size(); ++i) {
186 target->value_and_gradient(x,
f, gradient);
193 gradient=
inner(gradient,project_ext);
217 madness::print(
"\ngopt: projected vibrational frequencies (cm-1)\n");
218 printf(
"frequency in cm-1 ");
219 for (
int i=0; i<freq.
size(); ++i) {
239 printf(
" QuasiNewton iteration %2d energy: %16.8f gradient %.2e displacement %.2e \n", iter,
f,
gnorm, disp2);
244 print(
"final hessian");
263 old_displacement.
fill(0.0);
278 gradient=
inner(gradient,project_ext);
290 beta=gradient.
normf()/(gradient-oldgradient).normf();
321 int nneg=0, nsmall=0, nrestrict=0;
322 for (
int i=0; i<
e.size(); ++i) {
325 " forcing negative eigenvalue to be positive %d %.1e\n", i,
e[i]);
330 else if (
e[i] < tol) {
332 " forcing small eigenvalue to be zero %d %.1e\n", i,
e[i]);
339 gv[i] = -gv[i] /
e[i];
341 double gvnew = trust*
std::abs(gv(i))/gv[i];
343 " restricting step in spectral direction %d %.1e --> %.1e\n",
349 if (nneg || nsmall || nrestrict)
350 printf(
" nneg=%d nsmall=%d nrestrict=%d\n", nneg, nsmall, nrestrict);
367 const std::vector<std::string>& remove_dof) {
373 for (
size_t i=0; i<mol.
natom(); ++i) {
404 for (
size_t iatom=0; iatom<mol.
natom(); ++iatom) {
420 rotx(3*iatom + 0)=
p(1)*
v(0,2)-
p(2)*
v(0,1);
421 rotx(3*iatom + 1)=
p(1)*
v(1,2)-
p(2)*
v(1,1);
422 rotx(3*iatom + 2)=
p(1)*
v(2,2)-
p(2)*
v(2,1);
424 roty(3*iatom + 0)=
p(2)*
v(0,0)-
p(0l)*
v(0,2);
425 roty(3*iatom + 1)=
p(2)*
v(1,0)-
p(0l)*
v(1,2);
426 roty(3*iatom + 2)=
p(2)*
v(2,0)-
p(0l)*
v(2,2);
428 rotz(3*iatom + 0)=
p(0l)*
v(0,1)-
p(1)*
v(0,0);
429 rotz(3*iatom + 1)=
p(0l)*
v(1,1)-
p(1)*
v(1,0);
430 rotz(3*iatom + 2)=
p(0l)*
v(2,1)-
p(1)*
v(2,0);
437 bool remove_Tx=std::find(tmp.begin(), tmp.end(),
"tx")!=tmp.end();
438 bool remove_Ty=std::find(tmp.begin(), tmp.end(),
"ty")!=tmp.end();
439 bool remove_Tz=std::find(tmp.begin(), tmp.end(),
"tz")!=tmp.end();
440 bool remove_Rx=std::find(tmp.begin(), tmp.end(),
"rx")!=tmp.end();
441 bool remove_Ry=std::find(tmp.begin(), tmp.end(),
"ry")!=tmp.end();
442 bool remove_Rz=std::find(tmp.begin(), tmp.end(),
"rz")!=tmp.end();
444 if (remove_Tx) ext_dof(0l,
_)=transx;
445 if (remove_Ty) ext_dof(1l,
_)=transy;
446 if (remove_Tz) ext_dof(2l,
_)=transz;
448 if (remove_Rx) ext_dof(3l,
_)=rotx;
449 if (remove_Ry) ext_dof(4l,
_)=roty;
450 if (remove_Rz) ext_dof(5l,
_)=rotz;
451 print(
"removing dof ",remove_Tx, remove_Ty, remove_Tz, remove_Rx, remove_Ry, remove_Rz);
454 for (
int i=0; i<6; ++i) {
457 else ext_dof(i,
_)=0.0;
463 ext_dof=
inner(
v,ext_dof,0,0);
466 for (
int i=0; i<6; ++i) {
470 ext_dof(i,
_).
scale(1.0/sqrt(
e(i)));
476 for (
size_t i=0; i<3*mol.
natom(); ++i) projector(i,i)=1.0;
480 projector-=
inner(ext_dof,ext_dof,0,0);
490 const std::vector<std::string>& remove_dof) {
496 hessian=
inner(projector_ext,
inner(hessian,projector_ext),0,0);
509 const std::vector<std::string>& remove_dof={},
const bool print_hessian=
false) {
514 for (
size_t i=0; i<3*
molecule.natom(); ++i) Minv(i,i)=1.0/M(i,i);
523 if (remove_dof.size()>0) {
524 print(
"mass-weighted hessian with translation and rotation projected out");
526 print(
"mass-weighted unprojected hessian");
528 Tensor<double> mmhessian=
inner(Minv,
inner(mwhessian,Minv));
530 print(
"mass-weighted unprojected hessian; mass-weighing undone");
535 syev(mwhessian,normalmodes,freq);
536 for (
long i=0; i<freq.size(); ++i) {
537 if (freq(i)>0.0) freq(i)=sqrt(freq(i));
538 else freq(i)=-sqrt(-freq(i));
554 for (
size_t i=0; i<3*
molecule.natom(); ++i) {
556 for (
size_t j=0; j<3*
molecule.natom(); ++j) mu1+=MDL(j,i)*MDL(j,i);
double y
Definition: molecule.h:60
double x
Definition: molecule.h:60
double z
Definition: molecule.h:60
double get_mass_in_au() const
return the mass in atomic units (electron mass = 1 a.u.)
Definition: molecule.h:104
long dim(int i) const
Returns the size of dimension i.
Definition: basetensor.h:147
long size() const
Returns the number of elements in the tensor.
Definition: basetensor.h:138
Definition: molecular_optimizer.h:56
MolecularOptimizationParameters(World &world, const commandlineparser &parser)
Definition: molecular_optimizer.h:72
MolecularOptimizationParameters()
ctor reading out the input file
Definition: molecular_optimizer.h:60
bool printtest() const
Definition: molecular_optimizer.h:88
double tol() const
Definition: molecular_optimizer.h:85
std::string cg_method() const
Definition: molecular_optimizer.h:83
double gradient_precision() const
Definition: molecular_optimizer.h:87
double value_precision() const
Definition: molecular_optimizer.h:86
std::string update() const
Definition: molecular_optimizer.h:82
std::vector< std::string > remove_dof() const
Definition: molecular_optimizer.h:89
void set_derived_values()
Definition: molecular_optimizer.h:79
int maxiter() const
Definition: molecular_optimizer.h:84
Molecular optimizer derived from the QuasiNewton optimizer.
Definition: molecular_optimizer.h:100
void set_hessian(const Tensor< double > &hess)
set an (initial) hessian
Definition: molecular_optimizer.h:140
bool optimize_quasi_newton(Tensor< double > &x)
Definition: molecular_optimizer.h:147
bool optimize(Tensor< double > &x)
optimize the underlying molecule
Definition: molecular_optimizer.h:120
Tensor< double > h
Definition: molecular_optimizer.h:104
double gradient_norm() const
Definition: molecular_optimizer.h:137
static Tensor< double > compute_reduced_mass(const Molecule &molecule, const Tensor< double > &normalmodes)
Definition: molecular_optimizer.h:544
static Tensor< double > compute_frequencies(const Molecule &molecule, const Tensor< double > &hessian, Tensor< double > &normalmodes, const std::vector< std::string > &remove_dof={}, const bool print_hessian=false)
returns the vibrational frequencies
Definition: molecular_optimizer.h:507
MolecularOptimizationParameters parameters
Definition: molecular_optimizer.h:109
static Tensor< double > projector_external_dof(const Molecule &mol, const std::vector< std::string > &remove_dof)
compute the projector to remove transl. and rot. degrees of freedom
Definition: molecular_optimizer.h:366
bool converged() const
Definition: molecular_optimizer.h:127
bool converged(const Tensor< double > &displacement) const
Definition: molecular_optimizer.h:131
double f
Definition: molecular_optimizer.h:105
bool optimize_conjugate_gradients(Tensor< double > &x)
Definition: molecular_optimizer.h:250
MolecularOptimizer(World &world, const commandlineparser &parser, const std::shared_ptr< MolecularOptimizationTargetInterface > &tar)
same ctor as the QuasiNewton optimizer
Definition: molecular_optimizer.h:112
std::shared_ptr< MolecularOptimizationTargetInterface > target
How to update the hessian: BFGS or SR1.
Definition: molecular_optimizer.h:103
Tensor< double > new_search_direction2(const Tensor< double > &g, const Tensor< double > &hessian) const
effectively invert the hessian and multiply with the gradient
Definition: molecular_optimizer.h:304
static void remove_external_dof(Tensor< double > &hessian, const Molecule &mol, const std::vector< std::string > &remove_dof)
remove translational degrees of freedom from the hessian
Definition: molecular_optimizer.h:489
double gnorm
Definition: molecular_optimizer.h:106
double value() const
Definition: molecular_optimizer.h:135
Definition: molecule.h:124
void translate(const Tensor< double > &translation)
translate the molecule
Definition: molecule.cc:670
const Atom & get_atom(unsigned int i) const
Definition: molecule.cc:447
Tensor< double > center_of_mass() const
compute the center of mass
Definition: molecule.cc:839
size_t natom() const
Definition: molecule.h:387
Tensor< double > moment_of_inertia() const
Definition: molecule.cc:855
class for holding the parameters for calculation
Definition: QCCalculationParametersBase.h:290
virtual void read_input_and_commandline_options(World &world, const commandlineparser &parser, const std::string tag)
Definition: QCCalculationParametersBase.h:325
static double line_search(double a1, double f0, double dxgrad, const Tensor< double > &x, const Tensor< double > &dx, std::shared_ptr< OptimizationTargetInterface > target, double value_precision)
make this static for other QN classed to have access to it
Definition: solvers.cc:113
static void hessian_update_bfgs(const Tensor< double > &dx, const Tensor< double > &dg, Tensor< double > &hessian)
make this static for other QN classed to have access to it
Definition: solvers.cc:179
static void hessian_update_sr1(const Tensor< double > &s, const Tensor< double > &y, Tensor< double > &hessian)
make this static for other QN classed to have access to it
Definition: solvers.cc:166
scalar_type absmax(long *ind=0) const
Return the absolute maximum value (and if ind is non-null, its index) in the Tensor.
Definition: tensor.h:1754
float_scalar_type normf() const
Returns the Frobenius norm of the tensor.
Definition: tensor.h:1726
Tensor< T > & fill(T x)
Inplace fill with a scalar (legacy name)
Definition: tensor.h:562
T trace(const Tensor< T > &t) const
Return the trace of two tensors (no complex conjugate invoked)
Definition: tensor.h:1776
IsSupported< TensorTypeData< Q >, Tensor< T > & >::type scale(Q x)
Inplace multiplication by scalar of supported type (legacy name)
Definition: tensor.h:686
A parallel world class.
Definition: world.h:132
double(* energy)()
Definition: derivatives.cc:58
char * p(char *buf, const char *name, int k, int initial_level, double thresh, int order)
Definition: derivatives.cc:72
const double beta
Definition: gygi_soltion.cc:62
static const double v
Definition: hatom_sf_dirac.cc:20
#define MADNESS_EXCEPTION(msg, value)
Macro for throwing a MADNESS exception.
Definition: madness_exception.h:119
const double au2invcm
conversion from atomic units in reciprocal centimeter
Definition: constants.h:272
const double atomic_mass_in_au
Atomic mass in atomic units.
Definition: constants.h:269
double norm(const T &t)
Definition: adquad.h:42
File holds all helper structures necessary for the CC_Operator and CC2 class.
Definition: DFParameters.h:10
Function< T, NDIM > copy(const Function< T, NDIM > &f, const std::shared_ptr< WorldDCPmapInterface< Key< NDIM > > > &pmap, bool fence=true)
Create a new copy of the function with different distribution and optional fence.
Definition: mra.h:2002
response_space transpose(response_space &f)
Definition: basic_operators.cc:10
static const Slice _(0,-1, 1)
void print(const T &t, const Ts &... ts)
Print items to std::cout (items separated by spaces) and terminate with a new line.
Definition: print.h:225
NDIM const Function< R, NDIM > & g
Definition: mra.h:2416
double inner(response_space &a, response_space &b)
Definition: response_functions.h:442
Key< NDIM > displacement(const Key< NDIM > &source, const Key< NDIM > &target)
given a source and a target, return the displacement in translation
Definition: key.h:359
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
const double mu
Definition: navstokes_cosines.cc:95
static const double L
Definition: rk.cc:46
Defines interfaces for optimization and non-linear equation solvers.
Definition: test_ar.cc:204
Definition: molecular_optimizer.h:46
virtual Molecule & molecule()
return the molecule of the target
Definition: molecular_optimizer.h:49
The interface to be provided by functions to be optimized.
Definition: solvers.h:176
The interface to be provided by optimizers.
Definition: solvers.h:206
very simple command line parser
Definition: commandlineparser.h:15
static std::string tolower(std::string s)
make lower case
Definition: commandlineparser.h:78
static const double_complex I
Definition: tdse1d.cc:164
void converge(World &world, functionT &potn, functionT &psi, double &eps)
Definition: tdse.cc:441
void e()
Definition: test_sig.cc:75
static Molecule molecule
Definition: testperiodicdft.cc:38