[0b990d] | 1 | //
|
---|
| 2 | // energy.cc
|
---|
| 3 | //
|
---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc.
|
---|
| 5 | //
|
---|
| 6 | // Author: Curtis Janssen <cljanss@limitpt.com>
|
---|
| 7 | // Maintainer: LPS
|
---|
| 8 | //
|
---|
| 9 | // This file is part of the SC Toolkit.
|
---|
| 10 | //
|
---|
| 11 | // The SC Toolkit is free software; you can redistribute it and/or modify
|
---|
| 12 | // it under the terms of the GNU Library General Public License as published by
|
---|
| 13 | // the Free Software Foundation; either version 2, or (at your option)
|
---|
| 14 | // any later version.
|
---|
| 15 | //
|
---|
| 16 | // The SC Toolkit is distributed in the hope that it will be useful,
|
---|
| 17 | // but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 18 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 19 | // GNU Library General Public License for more details.
|
---|
| 20 | //
|
---|
| 21 | // You should have received a copy of the GNU Library General Public License
|
---|
| 22 | // along with the SC Toolkit; see the file COPYING.LIB. If not, write to
|
---|
| 23 | // the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
|
---|
| 24 | //
|
---|
| 25 | // The U.S. Government is granted a limited license as per AL 91-7.
|
---|
| 26 | //
|
---|
| 27 |
|
---|
| 28 | #ifdef __GNUC__
|
---|
| 29 | #pragma implementation
|
---|
| 30 | #endif
|
---|
| 31 |
|
---|
| 32 | #include <stdlib.h>
|
---|
| 33 | #include <math.h>
|
---|
| 34 | #include <stdexcept>
|
---|
| 35 | #include <util/misc/string.h>
|
---|
| 36 |
|
---|
| 37 | #include <util/class/scexception.h>
|
---|
| 38 | #include <util/misc/formio.h>
|
---|
| 39 | #include <util/state/stateio.h>
|
---|
| 40 | #include <math/scmat/local.h>
|
---|
| 41 | #include <util/keyval/keyval.h>
|
---|
| 42 | #include <chemistry/molecule/energy.h>
|
---|
| 43 |
|
---|
| 44 | using namespace std;
|
---|
| 45 | using namespace sc;
|
---|
| 46 |
|
---|
| 47 | /////////////////////////////////////////////////////////////////
|
---|
| 48 | // MolecularEnergy
|
---|
| 49 |
|
---|
| 50 | static ClassDesc MolecularEnergy_cd(
|
---|
| 51 | typeid(MolecularEnergy),"MolecularEnergy",6,"public Function",
|
---|
| 52 | 0, 0, 0);
|
---|
| 53 |
|
---|
| 54 | MolecularEnergy::MolecularEnergy(const MolecularEnergy& mole):
|
---|
| 55 | Function(mole)
|
---|
| 56 | {
|
---|
| 57 | print_molecule_when_changed_ = mole.print_molecule_when_changed_;
|
---|
| 58 | mc_ = mole.mc_;
|
---|
| 59 | moldim_ = mole.moldim_;
|
---|
| 60 | mol_ = mole.mol_;
|
---|
| 61 | initial_pg_ = new PointGroup(mol_->point_group());
|
---|
| 62 | ckpt_ = mole.ckpt_;
|
---|
| 63 | ckpt_file_ = strdup(mole.ckpt_file_);
|
---|
| 64 | ckpt_freq_ = mole.ckpt_freq_;
|
---|
| 65 | }
|
---|
| 66 |
|
---|
| 67 | MolecularEnergy::MolecularEnergy(const Ref<KeyVal>&keyval):
|
---|
| 68 | Function(keyval,1.0e-6,1.0e-6,1.0e-4)
|
---|
| 69 | {
|
---|
| 70 | // The following code is a solaris workshop 5.0 hack, since it doesn't
|
---|
| 71 | // seem to pass the right arguments in the Function CTOR. This code can
|
---|
| 72 | // be deleted with other C++ compilers.
|
---|
| 73 | KeyValValuedouble funcaccval(1.0e-6);
|
---|
| 74 | value_.set_desired_accuracy(keyval->doublevalue("value_accuracy",
|
---|
| 75 | funcaccval));
|
---|
| 76 | if (value_.desired_accuracy() < DBL_EPSILON)
|
---|
| 77 | value_.set_desired_accuracy(DBL_EPSILON);
|
---|
| 78 | KeyValValuedouble gradaccval(1.0e-6);
|
---|
| 79 | gradient_.set_desired_accuracy(keyval->doublevalue("gradient_accuracy",
|
---|
| 80 | gradaccval));
|
---|
| 81 | if (gradient_.desired_accuracy() < DBL_EPSILON)
|
---|
| 82 | gradient_.set_desired_accuracy(DBL_EPSILON);
|
---|
| 83 | KeyValValuedouble hessaccval(1.0e-4);
|
---|
| 84 | hessian_.set_desired_accuracy(keyval->doublevalue("hessian_accuracy",
|
---|
| 85 | hessaccval));
|
---|
| 86 | if (hessian_.desired_accuracy() < DBL_EPSILON)
|
---|
| 87 | hessian_.set_desired_accuracy(DBL_EPSILON);
|
---|
| 88 | // end of solaris workshop 5.0 hack
|
---|
| 89 |
|
---|
| 90 | print_molecule_when_changed_
|
---|
| 91 | = keyval->booleanvalue("print_molecule_when_changed");
|
---|
| 92 | if (keyval->error() != KeyVal::OK) print_molecule_when_changed_ = 1;
|
---|
| 93 |
|
---|
| 94 | mol_ << keyval->describedclassvalue("molecule");
|
---|
| 95 | if (mol_.null()) {
|
---|
| 96 | throw InputError("missing required input of type Molecule",
|
---|
| 97 | __FILE__, __LINE__, "molecule", 0,
|
---|
| 98 | class_desc());
|
---|
| 99 | }
|
---|
| 100 |
|
---|
| 101 | initial_pg_ = new PointGroup(mol_->point_group());
|
---|
| 102 |
|
---|
| 103 | hess_ << keyval->describedclassvalue("hessian");
|
---|
| 104 |
|
---|
| 105 | guesshess_ << keyval->describedclassvalue("guess_hessian");
|
---|
| 106 |
|
---|
| 107 | moldim_ = new SCDimension(3 * mol_->natom(), "3Natom");
|
---|
| 108 |
|
---|
| 109 | // the molecule coordinate object needs moldim_
|
---|
| 110 | // so constract a keyval that has it
|
---|
| 111 | Ref<AssignedKeyVal> assignedkeyval = new AssignedKeyVal;
|
---|
| 112 | Ref<DescribedClass> dc = moldim_.pointer();
|
---|
| 113 | assignedkeyval->assign("natom3", dc);
|
---|
| 114 | dc = matrixkit();
|
---|
| 115 | assignedkeyval->assign("matrixkit", dc);
|
---|
| 116 | Ref<KeyVal> asskeyval(assignedkeyval.pointer());
|
---|
| 117 | Ref<KeyVal> aggkeyval = new AggregateKeyVal(asskeyval, keyval);
|
---|
| 118 |
|
---|
| 119 | // Don't bother with internal coordinates if there is only 1 atom
|
---|
| 120 | if (mol_->natom() > 1) {
|
---|
| 121 | mc_ << aggkeyval->describedclassvalue("coor");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | RefSCDimension dim;
|
---|
| 125 | if (mc_.null()) {
|
---|
| 126 | dim = moldim_;
|
---|
| 127 | }
|
---|
| 128 | else {
|
---|
| 129 | dim = mc_->dim();
|
---|
| 130 | }
|
---|
| 131 | set_dimension(dim);
|
---|
| 132 |
|
---|
| 133 | ckpt_ = keyval->booleanvalue("checkpoint");
|
---|
| 134 | if (keyval->error() != KeyVal::OK) ckpt_ = false;
|
---|
| 135 | ckpt_file_ = keyval->pcharvalue("checkpoint_file");
|
---|
| 136 | if (keyval->error() != KeyVal::OK) {
|
---|
| 137 | char* filename = SCFormIO::fileext_to_filename(".wfn.ckpt");
|
---|
| 138 | ckpt_file_ = strdup(filename);
|
---|
| 139 | delete[] filename;
|
---|
| 140 | }
|
---|
| 141 | ckpt_freq_ = keyval->intvalue("checkpoint_freq");
|
---|
| 142 | if (keyval->error() != KeyVal::OK) {
|
---|
| 143 | ckpt_freq_ = 1;
|
---|
| 144 | }
|
---|
| 145 |
|
---|
| 146 | do_value(1);
|
---|
| 147 | do_gradient(0);
|
---|
| 148 | do_hessian(0);
|
---|
| 149 |
|
---|
| 150 | molecule_to_x();
|
---|
| 151 | }
|
---|
| 152 |
|
---|
| 153 | MolecularEnergy::~MolecularEnergy()
|
---|
| 154 | {
|
---|
| 155 | if (ckpt_file_) free(ckpt_file_);
|
---|
| 156 | ckpt_file_ = 0;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | MolecularEnergy::MolecularEnergy(StateIn&s):
|
---|
| 160 | SavableState(s),
|
---|
| 161 | Function(s)
|
---|
| 162 | {
|
---|
| 163 | mc_ << SavableState::restore_state(s);
|
---|
| 164 | moldim_ << SavableState::restore_state(s);
|
---|
| 165 | mol_ << SavableState::restore_state(s);
|
---|
| 166 | if (s.version(::class_desc<MolecularEnergy>()) >= 2)
|
---|
| 167 | s.get(print_molecule_when_changed_);
|
---|
| 168 | else print_molecule_when_changed_ = 1;
|
---|
| 169 | if (s.version(::class_desc<MolecularEnergy>()) >= 3) {
|
---|
| 170 | hess_ << SavableState::restore_state(s);
|
---|
| 171 | guesshess_ << SavableState::restore_state(s);
|
---|
| 172 | }
|
---|
| 173 | if (s.version(::class_desc<MolecularEnergy>()) >= 4)
|
---|
| 174 | initial_pg_ << SavableState::restore_state(s);
|
---|
| 175 | else initial_pg_ = new PointGroup(mol_->point_group());
|
---|
| 176 | if (s.version(::class_desc<MolecularEnergy>()) >= 5) {
|
---|
| 177 | int ckpt; s.get(ckpt); ckpt_ = (bool)ckpt;
|
---|
| 178 | s.getstring(ckpt_file_);
|
---|
| 179 | }
|
---|
| 180 | else {
|
---|
| 181 | ckpt_ = false;
|
---|
| 182 | char* filename = SCFormIO::fileext_to_filename(".wfn.ckpt");
|
---|
| 183 | ckpt_file_ = strdup(filename);
|
---|
| 184 | delete[] filename;
|
---|
| 185 | }
|
---|
| 186 | if (s.version(::class_desc<MolecularEnergy>()) >= 6)
|
---|
| 187 | s.get(ckpt_freq_);
|
---|
| 188 | else
|
---|
| 189 | ckpt_freq_ = 1;
|
---|
| 190 | }
|
---|
| 191 |
|
---|
| 192 | MolecularEnergy&
|
---|
| 193 | MolecularEnergy::operator=(const MolecularEnergy& mole)
|
---|
| 194 | {
|
---|
| 195 | Function::operator=(mole);
|
---|
| 196 | mc_ = mole.mc_;
|
---|
| 197 | moldim_ = mole.moldim_;
|
---|
| 198 | mol_ = mole.mol_;
|
---|
| 199 | print_molecule_when_changed_ = mole.print_molecule_when_changed_;
|
---|
| 200 | initial_pg_ = new PointGroup(mole.initial_pg_);
|
---|
| 201 | return *this;
|
---|
| 202 | }
|
---|
| 203 |
|
---|
| 204 | void
|
---|
| 205 | MolecularEnergy::save_data_state(StateOut&s)
|
---|
| 206 | {
|
---|
| 207 | Function::save_data_state(s);
|
---|
| 208 | SavableState::save_state(mc_.pointer(),s);
|
---|
| 209 | SavableState::save_state(moldim_.pointer(),s);
|
---|
| 210 | SavableState::save_state(mol_.pointer(),s);
|
---|
| 211 | s.put(print_molecule_when_changed_);
|
---|
| 212 | SavableState::save_state(hess_.pointer(),s);
|
---|
| 213 | SavableState::save_state(guesshess_.pointer(),s);
|
---|
| 214 | SavableState::save_state(initial_pg_.pointer(),s);
|
---|
| 215 | s.put((int)ckpt_);
|
---|
| 216 | s.putstring(ckpt_file_);
|
---|
| 217 | s.put(ckpt_freq_);
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | void
|
---|
| 221 | MolecularEnergy::set_checkpoint()
|
---|
| 222 | {
|
---|
| 223 | ckpt_ = true;
|
---|
| 224 | }
|
---|
| 225 |
|
---|
| 226 | void
|
---|
| 227 | MolecularEnergy::set_checkpoint_file(const char *path)
|
---|
| 228 | {
|
---|
| 229 | if (ckpt_file_) free(ckpt_file_);
|
---|
| 230 | if (path) {
|
---|
| 231 | ckpt_file_ = strdup(path);
|
---|
| 232 | } else
|
---|
| 233 | ckpt_file_ = 0;
|
---|
| 234 | }
|
---|
| 235 |
|
---|
| 236 | void
|
---|
| 237 | MolecularEnergy::set_checkpoint_freq(int freq)
|
---|
| 238 | {
|
---|
| 239 | if (freq >= 1)
|
---|
| 240 | ckpt_freq_ = freq;
|
---|
| 241 | else
|
---|
| 242 | throw std::runtime_error("MolecularEnergy::set_checkpoint_freq() -- invalid checkpointing frequency");
|
---|
| 243 | }
|
---|
| 244 |
|
---|
| 245 | bool
|
---|
| 246 | MolecularEnergy::if_to_checkpoint() const
|
---|
| 247 | {
|
---|
| 248 | return ckpt_;
|
---|
| 249 | }
|
---|
| 250 |
|
---|
| 251 | const char*
|
---|
| 252 | MolecularEnergy::checkpoint_file() const
|
---|
| 253 | {
|
---|
| 254 | return strdup(ckpt_file_);
|
---|
| 255 | }
|
---|
| 256 |
|
---|
| 257 | int
|
---|
| 258 | MolecularEnergy::checkpoint_freq() const
|
---|
| 259 | {
|
---|
| 260 | return ckpt_freq_;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | void
|
---|
| 264 | MolecularEnergy::failure(const char * msg)
|
---|
| 265 | {
|
---|
| 266 | throw SCException(msg, __FILE__, __LINE__, class_desc());
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | void
|
---|
| 270 | MolecularEnergy::set_energy(double e)
|
---|
| 271 | {
|
---|
| 272 | set_value(e);
|
---|
| 273 | }
|
---|
| 274 |
|
---|
| 275 | double
|
---|
| 276 | MolecularEnergy::energy()
|
---|
| 277 | {
|
---|
| 278 | return value();
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | void
|
---|
| 282 | MolecularEnergy::set_gradient(RefSCVector&g)
|
---|
| 283 | {
|
---|
| 284 | cartesian_gradient_ = g.copy();
|
---|
| 285 | if (mc_.null()) {
|
---|
| 286 | Function::set_gradient(g);
|
---|
| 287 | } else {
|
---|
| 288 | RefSCVector grad(dimension(), matrixkit());
|
---|
| 289 | mc_->to_internal(grad,g);
|
---|
| 290 | Function::set_gradient(grad);
|
---|
| 291 | }
|
---|
| 292 | }
|
---|
| 293 |
|
---|
| 294 | void
|
---|
| 295 | MolecularEnergy::set_hessian(RefSymmSCMatrix&h)
|
---|
| 296 | {
|
---|
| 297 | cartesian_hessian_ = h.copy();
|
---|
| 298 | if (mc_.null()) {
|
---|
| 299 | Function::set_hessian(h);
|
---|
| 300 | } else {
|
---|
| 301 | RefSymmSCMatrix hess(dimension(), matrixkit());
|
---|
| 302 | mc_->to_internal(hess,h);
|
---|
| 303 | Function::set_hessian(hess);
|
---|
| 304 | }
|
---|
| 305 | }
|
---|
| 306 |
|
---|
| 307 | void
|
---|
| 308 | MolecularEnergy::x_to_molecule()
|
---|
| 309 | {
|
---|
| 310 | RefSCVector x = get_x_no_copy();
|
---|
| 311 |
|
---|
| 312 | if (mc_.null()) {
|
---|
| 313 | int c = 0;
|
---|
| 314 |
|
---|
| 315 | for (int i=0; i<mol_->natom(); i++) {
|
---|
| 316 | mol_->r(i,0) = x(c); c++;
|
---|
| 317 | mol_->r(i,1) = x(c); c++;
|
---|
| 318 | mol_->r(i,2) = x(c); c++;
|
---|
| 319 | }
|
---|
| 320 | } else {
|
---|
| 321 | mc_->to_cartesian(get_x_no_copy());
|
---|
| 322 | }
|
---|
| 323 | mol_->cleanup_molecule(0.000001);
|
---|
| 324 | }
|
---|
| 325 |
|
---|
| 326 | void
|
---|
| 327 | MolecularEnergy::molecule_to_x()
|
---|
| 328 | {
|
---|
| 329 | if (mc_.null()) {
|
---|
| 330 | RefSCVector cartesian(moldim(),matrixkit());
|
---|
| 331 | int c = 0;
|
---|
| 332 | for (int i=0; i < mol_->natom(); i++) {
|
---|
| 333 | cartesian(c) = mol_->r(i,0); c++;
|
---|
| 334 | cartesian(c) = mol_->r(i,1); c++;
|
---|
| 335 | cartesian(c) = mol_->r(i,2); c++;
|
---|
| 336 | }
|
---|
| 337 | Function::set_x(cartesian);
|
---|
| 338 | } else {
|
---|
| 339 | mc_->to_internal(get_x_reference());
|
---|
| 340 | }
|
---|
| 341 | }
|
---|
| 342 |
|
---|
| 343 | void
|
---|
| 344 | MolecularEnergy::set_x(const RefSCVector&v)
|
---|
| 345 | {
|
---|
| 346 | Function::set_x(v);
|
---|
| 347 | x_to_molecule();
|
---|
| 348 | if (print_molecule_when_changed_) {
|
---|
| 349 | ExEnv::out0() << endl << indent << class_name()
|
---|
| 350 | << ": changing atomic coordinates:" << endl;
|
---|
| 351 | molecule()->print();
|
---|
| 352 | }
|
---|
| 353 | }
|
---|
| 354 |
|
---|
| 355 | RefSCVector
|
---|
| 356 | MolecularEnergy::get_cartesian_x()
|
---|
| 357 | {
|
---|
| 358 | RefSCVector cartesian(moldim(),matrixkit());
|
---|
| 359 | int c = 0;
|
---|
| 360 | for (int i=0; i < mol_->natom(); i++) {
|
---|
| 361 | cartesian(c) = mol_->r(i,0); c++;
|
---|
| 362 | cartesian(c) = mol_->r(i,1); c++;
|
---|
| 363 | cartesian(c) = mol_->r(i,2); c++;
|
---|
| 364 | }
|
---|
| 365 | return cartesian;
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | RefSCVector
|
---|
| 369 | MolecularEnergy::get_cartesian_gradient()
|
---|
| 370 | {
|
---|
| 371 | gradient();
|
---|
| 372 | if (cartesian_gradient_.null()) {
|
---|
| 373 | throw ProgrammingError("get_cartesian_gradient(): not available",
|
---|
| 374 | __FILE__, __LINE__, class_desc());
|
---|
| 375 | }
|
---|
| 376 | return cartesian_gradient_;
|
---|
| 377 | }
|
---|
| 378 |
|
---|
| 379 | RefSymmSCMatrix
|
---|
| 380 | MolecularEnergy::get_cartesian_hessian()
|
---|
| 381 | {
|
---|
| 382 | hessian();
|
---|
| 383 | if (cartesian_hessian_.null()) {
|
---|
| 384 | throw ProgrammingError("get_cartesian_hessian(): not available",
|
---|
| 385 | __FILE__, __LINE__, class_desc());
|
---|
| 386 | }
|
---|
| 387 | return cartesian_hessian_;
|
---|
| 388 | }
|
---|
| 389 |
|
---|
| 390 | RefSCDimension
|
---|
| 391 | MolecularEnergy::moldim() const
|
---|
| 392 | {
|
---|
| 393 | return moldim_;
|
---|
| 394 | }
|
---|
| 395 |
|
---|
| 396 | Ref<Molecule>
|
---|
| 397 | MolecularEnergy::molecule() const
|
---|
| 398 | {
|
---|
| 399 | return mol_;
|
---|
| 400 | }
|
---|
| 401 |
|
---|
| 402 | void
|
---|
| 403 | MolecularEnergy::guess_hessian(RefSymmSCMatrix&hessian)
|
---|
| 404 | {
|
---|
| 405 | if (guesshess_.nonnull()) {
|
---|
| 406 | int nullmole = (guesshess_->energy() == 0);
|
---|
| 407 | this->reference();
|
---|
| 408 | if (nullmole) guesshess_->set_energy(this);
|
---|
| 409 | RefSymmSCMatrix xhess = guesshess_->cartesian_hessian();
|
---|
| 410 | if (nullmole) guesshess_->set_energy(0);
|
---|
| 411 | this->dereference();
|
---|
| 412 | if (mc_.nonnull()) {
|
---|
| 413 | mc_->to_internal(hessian, xhess);
|
---|
| 414 | }
|
---|
| 415 | else {
|
---|
| 416 | hessian.assign(xhess);
|
---|
| 417 | }
|
---|
| 418 | }
|
---|
| 419 | else if (mc_.nonnull()) {
|
---|
| 420 | mc_->guess_hessian(hessian);
|
---|
| 421 | }
|
---|
| 422 | else {
|
---|
| 423 | Function::guess_hessian(hessian);
|
---|
| 424 | }
|
---|
| 425 | }
|
---|
| 426 |
|
---|
| 427 | RefSymmSCMatrix
|
---|
| 428 | MolecularEnergy::inverse_hessian(RefSymmSCMatrix&hessian)
|
---|
| 429 | {
|
---|
| 430 | if (mc_.nonnull()) {
|
---|
| 431 | return mc_->inverse_hessian(hessian);
|
---|
| 432 | }
|
---|
| 433 | else {
|
---|
| 434 | return Function::inverse_hessian(hessian);
|
---|
| 435 | }
|
---|
| 436 | }
|
---|
| 437 |
|
---|
| 438 | RefSymmSCMatrix
|
---|
| 439 | MolecularEnergy::hessian()
|
---|
| 440 | {
|
---|
| 441 | if (hess_.null()) return hessian_.result();
|
---|
| 442 |
|
---|
| 443 | if (hessian_.computed()) return hessian_.result();
|
---|
| 444 |
|
---|
| 445 | int nullmole = (hess_->energy() == 0);
|
---|
| 446 | this->reference();
|
---|
| 447 | if (nullmole) hess_->set_energy(this);
|
---|
| 448 | RefSymmSCMatrix xhess = hess_->cartesian_hessian();
|
---|
| 449 | if (nullmole) hess_->set_energy(0);
|
---|
| 450 | this->dereference();
|
---|
| 451 | set_hessian(xhess);
|
---|
| 452 | return hessian_.result();
|
---|
| 453 | }
|
---|
| 454 |
|
---|
| 455 | int
|
---|
| 456 | MolecularEnergy::hessian_implemented() const
|
---|
| 457 | {
|
---|
| 458 | return hess_.nonnull();
|
---|
| 459 | }
|
---|
| 460 |
|
---|
| 461 | void
|
---|
| 462 | MolecularEnergy::symmetry_changed()
|
---|
| 463 | {
|
---|
| 464 | obsolete();
|
---|
| 465 | }
|
---|
| 466 |
|
---|
| 467 | Ref<NonlinearTransform>
|
---|
| 468 | MolecularEnergy::change_coordinates()
|
---|
| 469 | {
|
---|
| 470 | if (!mc_) return 0;
|
---|
| 471 | Ref<NonlinearTransform> t = mc_->change_coordinates();
|
---|
| 472 | do_change_coordinates(t);
|
---|
| 473 | return t;
|
---|
| 474 | }
|
---|
| 475 |
|
---|
| 476 | void
|
---|
| 477 | MolecularEnergy::print_natom_3(const RefSCVector &v,
|
---|
| 478 | const char *title, ostream&o) const
|
---|
| 479 | {
|
---|
| 480 | int precision = 10;
|
---|
| 481 | int lwidth = precision + 4;
|
---|
| 482 | int n = v.n()/3;
|
---|
| 483 | if (title) {
|
---|
| 484 | o << indent << title << endl;
|
---|
| 485 | o << incindent;
|
---|
| 486 | }
|
---|
| 487 | for (int i=0,ii=0; i<n; i++) {
|
---|
| 488 | std::string symbol(molecule()->atom_symbol(i));
|
---|
| 489 | o << indent
|
---|
| 490 | << scprintf("%4d %3s",
|
---|
| 491 | i+1,symbol.c_str());
|
---|
| 492 | for (int j=0; j<3; j++,ii++) {
|
---|
| 493 | o << scprintf(" % *.*f", lwidth,precision,double(v(ii)));
|
---|
| 494 | }
|
---|
| 495 | o << endl;
|
---|
| 496 | }
|
---|
| 497 | if (title) {
|
---|
| 498 | o << decindent;
|
---|
| 499 | }
|
---|
| 500 | o.flush();
|
---|
| 501 | }
|
---|
| 502 |
|
---|
| 503 | void
|
---|
| 504 | MolecularEnergy::print_natom_3(double **vn3,
|
---|
| 505 | const char *title, ostream&o) const
|
---|
| 506 | {
|
---|
| 507 | int precision = 10;
|
---|
| 508 | int lwidth = precision + 4;
|
---|
| 509 | int n = molecule()->natom();
|
---|
| 510 | if (title) {
|
---|
| 511 | o << indent << title << endl;
|
---|
| 512 | o << incindent;
|
---|
| 513 | }
|
---|
| 514 | for (int i=0; i<n; i++) {
|
---|
| 515 | std::string symbol(molecule()->atom_symbol(i));
|
---|
| 516 | o << indent
|
---|
| 517 | << scprintf("%4d %3s",
|
---|
| 518 | i+1,symbol.c_str());
|
---|
| 519 | for (int j=0; j<3; j++) {
|
---|
| 520 | o << scprintf(" % *.*f", lwidth,precision,double(vn3[i][j]));
|
---|
| 521 | }
|
---|
| 522 | o << endl;
|
---|
| 523 | }
|
---|
| 524 | if (title) {
|
---|
| 525 | o << decindent;
|
---|
| 526 | }
|
---|
| 527 | o.flush();
|
---|
| 528 | }
|
---|
| 529 |
|
---|
| 530 | void
|
---|
| 531 | MolecularEnergy::print_natom_3(double *vn3,
|
---|
| 532 | const char *title, ostream&o) const
|
---|
| 533 | {
|
---|
| 534 | int precision = 10;
|
---|
| 535 | int lwidth = precision + 4;
|
---|
| 536 | int n = molecule()->natom();
|
---|
| 537 | if (title) {
|
---|
| 538 | o << indent << title << endl;
|
---|
| 539 | o << incindent;
|
---|
| 540 | }
|
---|
| 541 | for (int i=0; i<n; i++) {
|
---|
| 542 | std::string symbol(molecule()->atom_symbol(i));
|
---|
| 543 | o << indent
|
---|
| 544 | << scprintf("%4d %3s",
|
---|
| 545 | i+1,symbol.c_str());
|
---|
| 546 | for (int j=0; j<3; j++) {
|
---|
| 547 | o << scprintf(" % *.*f", lwidth,precision,double(vn3[3*i+j]));
|
---|
| 548 | }
|
---|
| 549 | o << endl;
|
---|
| 550 | }
|
---|
| 551 | if (title) {
|
---|
| 552 | o << decindent;
|
---|
| 553 | }
|
---|
| 554 | o.flush();
|
---|
| 555 | }
|
---|
| 556 |
|
---|
| 557 | void
|
---|
| 558 | MolecularEnergy::print(ostream&o) const
|
---|
| 559 | {
|
---|
| 560 | Function::print(o);
|
---|
| 561 | if (mc_.nonnull()) {
|
---|
| 562 | o << indent << "Molecular Coordinates:\n" << incindent;
|
---|
| 563 | mc_->print(o);
|
---|
| 564 | o << decindent;
|
---|
| 565 | }
|
---|
| 566 | else {
|
---|
| 567 | o << indent << "Molecule:\n" << incindent;
|
---|
| 568 | mol_->print(o);
|
---|
| 569 | o << decindent << endl;
|
---|
| 570 | }
|
---|
| 571 | }
|
---|
| 572 |
|
---|
| 573 | /////////////////////////////////////////////////////////////////
|
---|
| 574 | // SumMolecularEnergy
|
---|
| 575 |
|
---|
| 576 | static ClassDesc SumMolecularEnergy_cd(
|
---|
| 577 | typeid(SumMolecularEnergy),"SumMolecularEnergy",1,"public MolecularEnergy",
|
---|
| 578 | 0, create<SumMolecularEnergy>, create<SumMolecularEnergy>);
|
---|
| 579 |
|
---|
| 580 | SumMolecularEnergy::SumMolecularEnergy(const Ref<KeyVal> &keyval):
|
---|
| 581 | MolecularEnergy(keyval)
|
---|
| 582 | {
|
---|
| 583 | n_ = keyval->count("mole");
|
---|
| 584 | mole_ = new Ref<MolecularEnergy>[n_];
|
---|
| 585 | coef_ = new double[n_];
|
---|
| 586 | for (int i=0; i<n_; i++) {
|
---|
| 587 | mole_[i] << keyval->describedclassvalue("mole",i);
|
---|
| 588 | coef_[i] = keyval->intvalue("coef",i);
|
---|
| 589 | if (mole_[i].null()) {
|
---|
| 590 | throw InputError("a mole is null",
|
---|
| 591 | __FILE__, __LINE__, "mole", 0, class_desc());
|
---|
| 592 | }
|
---|
| 593 | else if (mole_[i]->molecule()->natom() != molecule()->natom()) {
|
---|
| 594 | throw InputError("a mole has the wrong number of atoms",
|
---|
| 595 | __FILE__, __LINE__, "mole", 0, class_desc());
|
---|
| 596 | }
|
---|
| 597 | }
|
---|
| 598 | }
|
---|
| 599 |
|
---|
| 600 | SumMolecularEnergy::SumMolecularEnergy(StateIn&s):
|
---|
| 601 | MolecularEnergy(s)
|
---|
| 602 | {
|
---|
| 603 | s.get(n_);
|
---|
| 604 | coef_ = new double[n_];
|
---|
| 605 | mole_ = new Ref<MolecularEnergy>[n_];
|
---|
| 606 | s.get_array_double(coef_,n_);
|
---|
| 607 | for (int i=0; i<n_; i++) {
|
---|
| 608 | mole_[i] << SavableState::restore_state(s);
|
---|
| 609 | }
|
---|
| 610 | }
|
---|
| 611 |
|
---|
| 612 | void
|
---|
| 613 | SumMolecularEnergy::save_data_state(StateOut&s)
|
---|
| 614 | {
|
---|
| 615 | MolecularEnergy::save_data_state(s);
|
---|
| 616 | s.put(n_);
|
---|
| 617 | s.put_array_double(coef_,n_);
|
---|
| 618 | for (int i=0; i<n_; i++) {
|
---|
| 619 | SavableState::save_state(mole_[i].pointer(),s);
|
---|
| 620 | }
|
---|
| 621 | }
|
---|
| 622 |
|
---|
| 623 | SumMolecularEnergy::~SumMolecularEnergy()
|
---|
| 624 | {
|
---|
| 625 | delete[] mole_;
|
---|
| 626 | delete[] coef_;
|
---|
| 627 | }
|
---|
| 628 |
|
---|
| 629 | int
|
---|
| 630 | SumMolecularEnergy::value_implemented() const
|
---|
| 631 | {
|
---|
| 632 | for (int i=0; i<n_; i++) {
|
---|
| 633 | if (!mole_[i]->value_implemented()) return 0;
|
---|
| 634 | }
|
---|
| 635 | return 1;
|
---|
| 636 | }
|
---|
| 637 |
|
---|
| 638 | int
|
---|
| 639 | SumMolecularEnergy::gradient_implemented() const
|
---|
| 640 | {
|
---|
| 641 | for (int i=0; i<n_; i++) {
|
---|
| 642 | if (!mole_[i]->gradient_implemented()) return 0;
|
---|
| 643 | }
|
---|
| 644 | return 1;
|
---|
| 645 | }
|
---|
| 646 |
|
---|
| 647 | int
|
---|
| 648 | SumMolecularEnergy::hessian_implemented() const
|
---|
| 649 | {
|
---|
| 650 | for (int i=0; i<n_; i++) {
|
---|
| 651 | if (!mole_[i]->hessian_implemented()) return 0;
|
---|
| 652 | }
|
---|
| 653 | return 1;
|
---|
| 654 | }
|
---|
| 655 |
|
---|
| 656 | void
|
---|
| 657 | SumMolecularEnergy::set_x(const RefSCVector&v)
|
---|
| 658 | {
|
---|
| 659 | MolecularEnergy::set_x(v);
|
---|
| 660 | for (int i=0; i<n_; i++) {
|
---|
| 661 | mole_[i]->set_x(v);
|
---|
| 662 | }
|
---|
| 663 | }
|
---|
| 664 |
|
---|
| 665 | void
|
---|
| 666 | SumMolecularEnergy::compute()
|
---|
| 667 | {
|
---|
| 668 | int i;
|
---|
| 669 |
|
---|
| 670 | int *old_do_value = new int[n_];
|
---|
| 671 | int *old_do_gradient = new int[n_];
|
---|
| 672 | int *old_do_hessian = new int[n_];
|
---|
| 673 |
|
---|
| 674 | for (i=0; i<n_; i++)
|
---|
| 675 | old_do_value[i] = mole_[i]->do_value(value_.compute());
|
---|
| 676 | for (i=0; i<n_; i++)
|
---|
| 677 | old_do_gradient[i]=mole_[i]->do_gradient(gradient_.compute());
|
---|
| 678 | for (i=0; i<n_; i++)
|
---|
| 679 | old_do_hessian[i] = mole_[i]->do_hessian(hessian_.compute());
|
---|
| 680 |
|
---|
| 681 | ExEnv::out0() << indent
|
---|
| 682 | << "SumMolecularEnergy: compute" << endl;
|
---|
| 683 |
|
---|
| 684 | ExEnv::out0() << incindent;
|
---|
| 685 |
|
---|
| 686 | if (value_needed()) {
|
---|
| 687 | double val = 0.0;
|
---|
| 688 | for (i=0; i<n_; i++) {
|
---|
| 689 | val += coef_[i] * mole_[i]->value();
|
---|
| 690 | }
|
---|
| 691 | ExEnv::out0() << endl << indent
|
---|
| 692 | << "SumMolecularEnergy =" << endl;
|
---|
| 693 | for (i=0; i<n_; i++) {
|
---|
| 694 | ExEnv::out0() << indent
|
---|
| 695 | << scprintf(" %c % 16.12f * % 16.12f",
|
---|
| 696 | (i==0?' ':'+'),
|
---|
| 697 | coef_[i], mole_[i]->value())
|
---|
| 698 | << endl;
|
---|
| 699 | }
|
---|
| 700 | ExEnv::out0() << indent
|
---|
| 701 | << scprintf(" = % 16.12f", val) << endl;
|
---|
| 702 | set_energy(val);
|
---|
| 703 | }
|
---|
| 704 | if (gradient_needed()) {
|
---|
| 705 | RefSCVector gradientvec = matrixkit()->vector(moldim());
|
---|
| 706 | gradientvec->assign(0.0);
|
---|
| 707 | for (i=0; i<n_; i++)
|
---|
| 708 | gradientvec.accumulate(coef_[i] * mole_[i]->gradient());
|
---|
| 709 | set_gradient(gradientvec);
|
---|
| 710 | }
|
---|
| 711 | if (hessian_needed()) {
|
---|
| 712 | RefSymmSCMatrix hessianmat = matrixkit()->symmmatrix(moldim());
|
---|
| 713 | hessianmat->assign(0.0);
|
---|
| 714 | for (i=0; i<n_; i++)
|
---|
| 715 | hessianmat.accumulate(coef_[i] * mole_[i]->hessian());
|
---|
| 716 | set_hessian(hessianmat);
|
---|
| 717 | }
|
---|
| 718 |
|
---|
| 719 | ExEnv::out0() << decindent;
|
---|
| 720 |
|
---|
| 721 | for (i=0; i<n_; i++) mole_[i]->do_value(old_do_value[i]);
|
---|
| 722 | for (i=0; i<n_; i++) mole_[i]->do_gradient(old_do_gradient[i]);
|
---|
| 723 | for (i=0; i<n_; i++) mole_[i]->do_hessian(old_do_hessian[i]);
|
---|
| 724 |
|
---|
| 725 | delete[] old_do_value;
|
---|
| 726 | delete[] old_do_gradient;
|
---|
| 727 | delete[] old_do_hessian;
|
---|
| 728 | }
|
---|
| 729 |
|
---|
| 730 | /////////////////////////////////////////////////////////////////
|
---|
| 731 | // MolEnergyConvergence
|
---|
| 732 |
|
---|
| 733 | static ClassDesc MolEnergyConvergence_cd(
|
---|
| 734 | typeid(MolEnergyConvergence),"MolEnergyConvergence",3,"public Convergence",
|
---|
| 735 | 0, create<MolEnergyConvergence>, create<MolEnergyConvergence>);
|
---|
| 736 |
|
---|
| 737 | MolEnergyConvergence::MolEnergyConvergence()
|
---|
| 738 | {
|
---|
| 739 | set_defaults();
|
---|
| 740 | }
|
---|
| 741 |
|
---|
| 742 | MolEnergyConvergence::MolEnergyConvergence(StateIn&s):
|
---|
| 743 | SavableState(s),
|
---|
| 744 | Convergence(s)
|
---|
| 745 | {
|
---|
| 746 | if (s.version(::class_desc<MolEnergyConvergence>()) >= 2)
|
---|
| 747 | s.get(cartesian_);
|
---|
| 748 | if (s.version(::class_desc<MolEnergyConvergence>()) >= 3)
|
---|
| 749 | mole_ << SavableState::restore_state(s);
|
---|
| 750 | }
|
---|
| 751 |
|
---|
| 752 | MolEnergyConvergence::MolEnergyConvergence(const Ref<KeyVal>&keyval)
|
---|
| 753 | {
|
---|
| 754 | mole_ << keyval->describedclassvalue("energy");
|
---|
| 755 | if (mole_.null()) {
|
---|
| 756 | throw InputError("required keyword missing",
|
---|
| 757 | __FILE__, __LINE__, "energy", 0,
|
---|
| 758 | class_desc());
|
---|
| 759 | }
|
---|
| 760 |
|
---|
| 761 | cartesian_ = keyval->booleanvalue("cartesian");
|
---|
| 762 | if (keyval->error() != KeyVal::OK) cartesian_ = 1;
|
---|
| 763 |
|
---|
| 764 | use_max_disp_ = keyval->exists("max_disp");
|
---|
| 765 | use_max_grad_ = keyval->exists("max_grad");
|
---|
| 766 | use_rms_disp_ = keyval->exists("rms_disp");
|
---|
| 767 | use_rms_grad_ = keyval->exists("rms_grad");
|
---|
| 768 | use_graddisp_ = keyval->exists("graddisp");
|
---|
| 769 | if (use_max_disp_) max_disp_ = keyval->doublevalue("max_disp");
|
---|
| 770 | if (use_max_grad_) max_grad_ = keyval->doublevalue("max_grad");
|
---|
| 771 | if (use_rms_disp_) rms_disp_ = keyval->doublevalue("rms_disp");
|
---|
| 772 | if (use_rms_grad_) rms_grad_ = keyval->doublevalue("rms_grad");
|
---|
| 773 | if (use_graddisp_) graddisp_ = keyval->doublevalue("graddisp");
|
---|
| 774 |
|
---|
| 775 | if (!use_max_disp_ && !use_max_grad_
|
---|
| 776 | && !use_rms_disp_ && !use_rms_grad_
|
---|
| 777 | && !use_graddisp_) {
|
---|
| 778 | set_defaults();
|
---|
| 779 | }
|
---|
| 780 | }
|
---|
| 781 |
|
---|
| 782 | MolEnergyConvergence::~MolEnergyConvergence()
|
---|
| 783 | {
|
---|
| 784 | }
|
---|
| 785 |
|
---|
| 786 | void
|
---|
| 787 | MolEnergyConvergence::save_data_state(StateOut&s)
|
---|
| 788 | {
|
---|
| 789 | Convergence::save_data_state(s);
|
---|
| 790 | s.put(cartesian_);
|
---|
| 791 | SavableState::save_state(mole_.pointer(),s);
|
---|
| 792 | }
|
---|
| 793 |
|
---|
| 794 | void
|
---|
| 795 | MolEnergyConvergence::set_defaults()
|
---|
| 796 | {
|
---|
| 797 | use_max_disp_ = 1;
|
---|
| 798 | use_max_grad_ = 1;
|
---|
| 799 | use_rms_disp_ = 0;
|
---|
| 800 | use_rms_grad_ = 0;
|
---|
| 801 | use_graddisp_ = 1;
|
---|
| 802 | max_disp_ = 1.0e-4;
|
---|
| 803 | max_grad_ = 1.0e-4;
|
---|
| 804 | graddisp_ = 1.0e-4;
|
---|
| 805 | }
|
---|
| 806 |
|
---|
| 807 | void
|
---|
| 808 | MolEnergyConvergence::get_x(const Ref<Function> &f)
|
---|
| 809 | {
|
---|
| 810 | Ref<MolecularEnergy> m; m << f;
|
---|
| 811 | if (cartesian_ && m.nonnull() && m->molecularcoor().nonnull()) {
|
---|
| 812 | x_ = m->get_cartesian_x();
|
---|
| 813 | }
|
---|
| 814 | else {
|
---|
| 815 | x_ = f->get_x();
|
---|
| 816 | }
|
---|
| 817 | }
|
---|
| 818 |
|
---|
| 819 |
|
---|
| 820 | void
|
---|
| 821 | MolEnergyConvergence::set_nextx(const RefSCVector& x)
|
---|
| 822 | {
|
---|
| 823 | if (cartesian_ && mole_.nonnull() && mole_->molecularcoor().nonnull()) {
|
---|
| 824 | Ref<Molecule> mol = new Molecule(*(mole_->molecule().pointer()));
|
---|
| 825 | mole_->molecularcoor()->to_cartesian(mol, x);
|
---|
| 826 | nextx_ = mole_->matrixkit()->vector(mole_->moldim());
|
---|
| 827 | int c = 0;
|
---|
| 828 | for (int i=0; i < mol->natom(); i++) {
|
---|
| 829 | nextx_(c) = mol->r(i,0); c++;
|
---|
| 830 | nextx_(c) = mol->r(i,1); c++;
|
---|
| 831 | nextx_(c) = mol->r(i,2); c++;
|
---|
| 832 | }
|
---|
| 833 | }
|
---|
| 834 | else if (mole_.null()) {
|
---|
| 835 | // this only happens after restoring state from old versions
|
---|
| 836 | // of MolEnergyConvergence
|
---|
| 837 | nextx_ = 0;
|
---|
| 838 | }
|
---|
| 839 | else {
|
---|
| 840 | nextx_ = x.copy();
|
---|
| 841 | }
|
---|
| 842 | }
|
---|
| 843 |
|
---|
| 844 | void
|
---|
| 845 | MolEnergyConvergence::get_grad(const Ref<Function> &f)
|
---|
| 846 | {
|
---|
| 847 | Ref<MolecularEnergy> m; m << f;
|
---|
| 848 | if (cartesian_ && m.nonnull() && m->molecularcoor().nonnull()) {
|
---|
| 849 | RefSCVector cartesian_grad = m->get_cartesian_gradient()->copy();
|
---|
| 850 | if (m->molecularcoor()->nconstrained()) {
|
---|
| 851 | // convert the gradient to internal coordinates and back
|
---|
| 852 | // this will project out the fixed coordinates
|
---|
| 853 | RefSCVector internal_grad(m->dimension(), m->matrixkit());
|
---|
| 854 | m->molecularcoor()->to_internal(internal_grad,cartesian_grad);
|
---|
| 855 | m->molecularcoor()->to_cartesian(cartesian_grad,internal_grad);
|
---|
| 856 | }
|
---|
| 857 | grad_ = cartesian_grad;
|
---|
| 858 | }
|
---|
| 859 | else {
|
---|
| 860 | grad_ = f->gradient();
|
---|
| 861 | }
|
---|
| 862 | }
|
---|
| 863 |
|
---|
| 864 | int
|
---|
| 865 | MolEnergyConvergence::converged()
|
---|
| 866 | {
|
---|
| 867 | return Convergence::converged();
|
---|
| 868 | }
|
---|
| 869 |
|
---|
| 870 | /////////////////////////////////////////////////////////////////////////////
|
---|
| 871 |
|
---|
| 872 | // Local Variables:
|
---|
| 873 | // mode: c++
|
---|
| 874 | // c-file-style: "CLJ"
|
---|
| 875 | // End:
|
---|