| 1 | // | 
|---|
| 2 | // mpqc.cc | 
|---|
| 3 | // | 
|---|
| 4 | // Copyright (C) 1996 Limit Point Systems, Inc. | 
|---|
| 5 | // | 
|---|
| 6 | // Author: Edward Seidl <seidl@janed.com> | 
|---|
| 7 | // Maintainer: LPS | 
|---|
| 8 | // | 
|---|
| 9 | // This file is part of MPQC. | 
|---|
| 10 | // | 
|---|
| 11 | // MPQC is free software; you can redistribute it and/or modify | 
|---|
| 12 | // it under the terms of the GNU General Public License as published by | 
|---|
| 13 | // the Free Software Foundation; either version 2, or (at your option) | 
|---|
| 14 | // any later version. | 
|---|
| 15 | // | 
|---|
| 16 | // MPQC 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 General Public License for more details. | 
|---|
| 20 | // | 
|---|
| 21 | // You should have received a copy of the GNU General Public License | 
|---|
| 22 | // along with the MPQC; see the file COPYING.  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 | // This is needed to make GNU extensions available, such as | 
|---|
| 29 | // feenableexcept and fedisableexcept. | 
|---|
| 30 | #ifndef _GNU_SOURCE | 
|---|
| 31 | #  define _GNU_SOURCE | 
|---|
| 32 | #endif | 
|---|
| 33 |  | 
|---|
| 34 | #ifdef HAVE_CONFIG_H | 
|---|
| 35 | #include <scconfig.h> | 
|---|
| 36 | #endif | 
|---|
| 37 |  | 
|---|
| 38 | #ifdef HAVE_TIME_H | 
|---|
| 39 | #include <time.h> | 
|---|
| 40 | #endif | 
|---|
| 41 |  | 
|---|
| 42 | #include <scdirlist.h> | 
|---|
| 43 |  | 
|---|
| 44 | #include <new> | 
|---|
| 45 | #include <stdexcept> | 
|---|
| 46 | #include <string.h> | 
|---|
| 47 | #include <unistd.h> | 
|---|
| 48 | #include <sys/stat.h> | 
|---|
| 49 | #include <fstream> | 
|---|
| 50 |  | 
|---|
| 51 | #include <boost/bind.hpp> | 
|---|
| 52 | #include <boost/function.hpp> | 
|---|
| 53 |  | 
|---|
| 54 | #include <scconfig.h> | 
|---|
| 55 | #ifdef HAVE_SSTREAM | 
|---|
| 56 | #  include <sstream> | 
|---|
| 57 | #else | 
|---|
| 58 | #  include <strstream.h> | 
|---|
| 59 | #endif | 
|---|
| 60 |  | 
|---|
| 61 | #ifdef HAVE_SYS_RESOURCE_H | 
|---|
| 62 | #  include <sys/resource.h> | 
|---|
| 63 | #endif | 
|---|
| 64 | #ifdef HAVE_SYS_TIME_H | 
|---|
| 65 | #  include <sys/time.h> | 
|---|
| 66 | #endif | 
|---|
| 67 |  | 
|---|
| 68 | #include <util/options/GetLongOpt.h> | 
|---|
| 69 | #include <util/class/scexception.h> | 
|---|
| 70 | #include <util/misc/newstring.h> | 
|---|
| 71 | #include <util/keyval/keyval.h> | 
|---|
| 72 | #include <util/state/state_bin.h> | 
|---|
| 73 | #include <util/group/message.h> | 
|---|
| 74 | #include <util/group/memory.h> | 
|---|
| 75 | #include <util/group/mstate.h> | 
|---|
| 76 | #include <util/group/thread.h> | 
|---|
| 77 | #include <util/group/pregtime.h> | 
|---|
| 78 | #include <util/misc/bug.h> | 
|---|
| 79 | #include <util/misc/formio.h> | 
|---|
| 80 | #include <util/misc/exenv.h> | 
|---|
| 81 | #ifdef HAVE_CHEMISTRY_CCA | 
|---|
| 82 | #include <util/misc/ccaenv.h> | 
|---|
| 83 | #endif | 
|---|
| 84 | #include <util/render/render.h> | 
|---|
| 85 |  | 
|---|
| 86 | #include <math/optimize/opt.h> | 
|---|
| 87 |  | 
|---|
| 88 | #include <chemistry/molecule/coor.h> | 
|---|
| 89 | #include <chemistry/molecule/energy.h> | 
|---|
| 90 | #include <chemistry/molecule/molfreq.h> | 
|---|
| 91 | #include <chemistry/molecule/fdhess.h> | 
|---|
| 92 | #include <chemistry/molecule/formula.h> | 
|---|
| 93 | #include <chemistry/qc/wfn/wfn.h> | 
|---|
| 94 |  | 
|---|
| 95 | // Force linkages: | 
|---|
| 96 | #include <util/group/linkage.h> | 
|---|
| 97 | #include <chemistry/qc/wfn/linkage.h> | 
|---|
| 98 | #include <chemistry/qc/scf/linkage.h> | 
|---|
| 99 | #include <chemistry/qc/dft/linkage.h> | 
|---|
| 100 | #include <chemistry/qc/mbpt/linkage.h> | 
|---|
| 101 | #ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_MBPTR12 | 
|---|
| 102 | #  include <chemistry/qc/mbptr12/linkage.h> | 
|---|
| 103 | #endif | 
|---|
| 104 | #ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_CINTS | 
|---|
| 105 | #  include <chemistry/qc/cints/linkage.h> | 
|---|
| 106 | #endif | 
|---|
| 107 | //#include <chemistry/qc/psi/linkage.h> | 
|---|
| 108 | #include <util/state/linkage.h> | 
|---|
| 109 | #ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_CC | 
|---|
| 110 | #  include <chemistry/qc/cc/linkage.h> | 
|---|
| 111 | #endif | 
|---|
| 112 | #ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_PSI | 
|---|
| 113 | #  include <chemistry/qc/psi/linkage.h> | 
|---|
| 114 | #endif | 
|---|
| 115 | #ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_INTCCA | 
|---|
| 116 | #  include <chemistry/qc/intcca/linkage.h> | 
|---|
| 117 | #endif | 
|---|
| 118 |  | 
|---|
| 119 | #ifdef HAVE_MPI | 
|---|
| 120 | #define MPICH_SKIP_MPICXX | 
|---|
| 121 | #include <mpi.h> | 
|---|
| 122 | #include <util/group/messmpi.h> | 
|---|
| 123 | #endif | 
|---|
| 124 |  | 
|---|
| 125 | using namespace std; | 
|---|
| 126 | using namespace sc; | 
|---|
| 127 |  | 
|---|
| 128 | #include "mpqcin.h" | 
|---|
| 129 |  | 
|---|
| 130 | ////////////////////////////////////////////////////////////////////////// | 
|---|
| 131 |  | 
|---|
| 132 | const KeyValValueboolean truevalue(1), falsevalue(0); | 
|---|
| 133 | const char *devnull = "/dev/null"; | 
|---|
| 134 |  | 
|---|
| 135 |  | 
|---|
| 136 | static void | 
|---|
| 137 | trash_stack_b(int &i, char *&ichar) | 
|---|
| 138 | { | 
|---|
| 139 | char stack; | 
|---|
| 140 | ichar = &stack; | 
|---|
| 141 | ichar -= 10; | 
|---|
| 142 | for (i=0; i<1000; i++) { | 
|---|
| 143 | *ichar-- = 0xfe; | 
|---|
| 144 | } | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | static void | 
|---|
| 148 | trash_stack() | 
|---|
| 149 | { | 
|---|
| 150 | int i; | 
|---|
| 151 | char *ichar; | 
|---|
| 152 | trash_stack_b(i,ichar); | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | static void | 
|---|
| 156 | clean_up(void) | 
|---|
| 157 | { | 
|---|
| 158 | MemoryGrp::set_default_memorygrp(0); | 
|---|
| 159 | MessageGrp::set_default_messagegrp(0); | 
|---|
| 160 | ThreadGrp::set_default_threadgrp(0); | 
|---|
| 161 | SCMatrixKit::set_default_matrixkit(0); | 
|---|
| 162 | Integral::set_default_integral(0); | 
|---|
| 163 | RegionTimer::set_default_regiontimer(0); | 
|---|
| 164 | } | 
|---|
| 165 |  | 
|---|
| 166 | #include <signal.h> | 
|---|
| 167 |  | 
|---|
| 168 | #ifdef HAVE_FENV_H | 
|---|
| 169 | #  include <fenv.h> | 
|---|
| 170 | #endif | 
|---|
| 171 |  | 
|---|
| 172 | static void | 
|---|
| 173 | print_unseen(const Ref<ParsedKeyVal> &parsedkv, | 
|---|
| 174 | const char *input) | 
|---|
| 175 | { | 
|---|
| 176 | if (parsedkv->have_unseen()) { | 
|---|
| 177 | ExEnv::out0() << endl; | 
|---|
| 178 | ExEnv::out0() << indent | 
|---|
| 179 | << "The following keywords in \"" << input << "\" were ignored:" | 
|---|
| 180 | << endl; | 
|---|
| 181 | ExEnv::out0() << incindent; | 
|---|
| 182 | parsedkv->print_unseen(ExEnv::out0()); | 
|---|
| 183 | ExEnv::out0() << decindent; | 
|---|
| 184 | } | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | double EvaluateDensity( | 
|---|
| 188 | SCVector3 &r, | 
|---|
| 189 | Ref<Integral> &intgrl, | 
|---|
| 190 | GaussianBasisSet::ValueData &vdat, | 
|---|
| 191 | Ref<Wavefunction> &wfn); | 
|---|
| 192 |  | 
|---|
| 193 | /** Places all known options into \a options and parses them from argc,argv. | 
|---|
| 194 | * | 
|---|
| 195 | * \param options options structure | 
|---|
| 196 | * \param argc argument count | 
|---|
| 197 | * \param argv argument array | 
|---|
| 198 | * \return return value by GetLongOpt::parse() function | 
|---|
| 199 | */ | 
|---|
| 200 | int ParseOptions( | 
|---|
| 201 | GetLongOpt &options, | 
|---|
| 202 | int argc, | 
|---|
| 203 | char **argv) | 
|---|
| 204 | { | 
|---|
| 205 | options.usage("[options] [filename]"); | 
|---|
| 206 | options.enroll("f", GetLongOpt::MandatoryValue, | 
|---|
| 207 | "the name of an object format input file", 0); | 
|---|
| 208 | options.enroll("o", GetLongOpt::MandatoryValue, | 
|---|
| 209 | "the name of the output file", 0); | 
|---|
| 210 | options.enroll("n", GetLongOpt::NoValue, | 
|---|
| 211 | "listen for incoming object format input files", 0); | 
|---|
| 212 | options.enroll("messagegrp", GetLongOpt::MandatoryValue, | 
|---|
| 213 | "which message group to use", 0); | 
|---|
| 214 | options.enroll("threadgrp", GetLongOpt::MandatoryValue, | 
|---|
| 215 | "which thread group to use", 0); | 
|---|
| 216 | options.enroll("memorygrp", GetLongOpt::MandatoryValue, | 
|---|
| 217 | "which memory group to use", 0); | 
|---|
| 218 | options.enroll("integral", GetLongOpt::MandatoryValue, | 
|---|
| 219 | "which integral evaluator to use", 0); | 
|---|
| 220 | options.enroll("l", GetLongOpt::MandatoryValue, "basis set limit", "0"); | 
|---|
| 221 | options.enroll("W", GetLongOpt::MandatoryValue, | 
|---|
| 222 | "set the working directory", "."); | 
|---|
| 223 | options.enroll("c", GetLongOpt::NoValue, "check input then exit", 0); | 
|---|
| 224 | options.enroll("v", GetLongOpt::NoValue, "print the version number", 0); | 
|---|
| 225 | options.enroll("w", GetLongOpt::NoValue, "print the warranty", 0); | 
|---|
| 226 | options.enroll("L", GetLongOpt::NoValue, "print the license", 0); | 
|---|
| 227 | options.enroll("k", GetLongOpt::NoValue, "print key/value assignments", 0); | 
|---|
| 228 | options.enroll("i", GetLongOpt::NoValue, "convert simple to OO input", 0); | 
|---|
| 229 | options.enroll("d", GetLongOpt::NoValue, "debug", 0); | 
|---|
| 230 | options.enroll("h", GetLongOpt::NoValue, "print this message", 0); | 
|---|
| 231 | options.enroll("cca-path", GetLongOpt::OptionalValue, | 
|---|
| 232 | "cca component path", ""); | 
|---|
| 233 | options.enroll("cca-load", GetLongOpt::OptionalValue, | 
|---|
| 234 | "cca components to load", ""); | 
|---|
| 235 |  | 
|---|
| 236 | int optind = options.parse(argc, argv); | 
|---|
| 237 |  | 
|---|
| 238 | return optind; | 
|---|
| 239 | } | 
|---|
| 240 |  | 
|---|
| 241 | /** Checks for each known option and acts accordingly. | 
|---|
| 242 | * | 
|---|
| 243 | * \param options option structure | 
|---|
| 244 | * \param *output name of outputfile on return | 
|---|
| 245 | * \param *outstream open output stream on return | 
|---|
| 246 | */ | 
|---|
| 247 | void ComputeOptions( | 
|---|
| 248 | GetLongOpt &options, | 
|---|
| 249 | const char *&output, | 
|---|
| 250 | ostream *&outstream) | 
|---|
| 251 | { | 
|---|
| 252 | output = options.retrieve("o"); | 
|---|
| 253 | outstream = 0; | 
|---|
| 254 | if (output != 0) { | 
|---|
| 255 | outstream = new ofstream(output); | 
|---|
| 256 | ExEnv::set_out(outstream); | 
|---|
| 257 | } | 
|---|
| 258 |  | 
|---|
| 259 | if (options.retrieve("h")) { | 
|---|
| 260 | ExEnv::out0() | 
|---|
| 261 | << indent << "MPQC version " << SC_VERSION << endl | 
|---|
| 262 | << indent << "compiled for " << TARGET_ARCH << endl | 
|---|
| 263 | << SCFormIO::copyright << endl; | 
|---|
| 264 | options.usage(ExEnv::out0()); | 
|---|
| 265 | exit(0); | 
|---|
| 266 | } | 
|---|
| 267 |  | 
|---|
| 268 | if (options.retrieve("v")) { | 
|---|
| 269 | ExEnv::out0() | 
|---|
| 270 | << indent << "MPQC version " << SC_VERSION << endl | 
|---|
| 271 | << indent << "compiled for " << TARGET_ARCH << endl | 
|---|
| 272 | << SCFormIO::copyright; | 
|---|
| 273 | exit(0); | 
|---|
| 274 | } | 
|---|
| 275 |  | 
|---|
| 276 | if (options.retrieve("w")) { | 
|---|
| 277 | ExEnv::out0() | 
|---|
| 278 | << indent << "MPQC version " << SC_VERSION << endl | 
|---|
| 279 | << indent << "compiled for " << TARGET_ARCH << endl | 
|---|
| 280 | << SCFormIO::copyright << endl | 
|---|
| 281 | << SCFormIO::warranty; | 
|---|
| 282 | exit(0); | 
|---|
| 283 | } | 
|---|
| 284 |  | 
|---|
| 285 | if (options.retrieve("L")) { | 
|---|
| 286 | ExEnv::out0() | 
|---|
| 287 | << indent << "MPQC version " << SC_VERSION << endl | 
|---|
| 288 | << indent << "compiled for " << TARGET_ARCH << endl | 
|---|
| 289 | << SCFormIO::copyright << endl | 
|---|
| 290 | << SCFormIO::license; | 
|---|
| 291 | exit(0); | 
|---|
| 292 | } | 
|---|
| 293 |  | 
|---|
| 294 | if (options.retrieve("d")) | 
|---|
| 295 | SCFormIO::set_debug(1); | 
|---|
| 296 |  | 
|---|
| 297 | // set the working dir | 
|---|
| 298 | if (strcmp(options.retrieve("W"),".")) | 
|---|
| 299 | int retval = chdir(options.retrieve("W")); | 
|---|
| 300 |  | 
|---|
| 301 | // check that n and f/o are not given at the same time | 
|---|
| 302 | if ((options.retrieve("n")) && ((options.retrieve("f")) || (options.retrieve("o")))) { | 
|---|
| 303 | throw invalid_argument("-n must not be given with -f or -o"); | 
|---|
| 304 | } | 
|---|
| 305 | } | 
|---|
| 306 |  | 
|---|
| 307 | /** Temporary structure for storing information from command-line | 
|---|
| 308 | * | 
|---|
| 309 | * This structure has been introduced to gather the various calls to GetLongOpts | 
|---|
| 310 | * at one (initial) place and to abstract it from the source of command-lines. | 
|---|
| 311 | * This temporary object can be set by other means, too. I.e. we become | 
|---|
| 312 | * independent of usage in command-line programs. | 
|---|
| 313 | */ | 
|---|
| 314 | struct OptionValues { | 
|---|
| 315 | const char *keyvalue; // option "k" | 
|---|
| 316 | const char *debug; // option "" | 
|---|
| 317 | int limit; // option "l" | 
|---|
| 318 | const char *check; // option "c" | 
|---|
| 319 | const char *simple_input; // option "i" | 
|---|
| 320 | string executablename; | 
|---|
| 321 |  | 
|---|
| 322 | #ifdef HAVE_CHEMISTRY_CCA | 
|---|
| 323 | string cca_load;  // option "cca-path" | 
|---|
| 324 | string cc_path; // option "cca-load" | 
|---|
| 325 | #endif | 
|---|
| 326 | }; | 
|---|
| 327 |  | 
|---|
| 328 | /** Parse remainder options not treated by ComputeOptions() into temporary storage. | 
|---|
| 329 | * | 
|---|
| 330 | * \param options option structure to obtain values from | 
|---|
| 331 | * \param values remaining option values which are processed later and now | 
|---|
| 332 | *        stored in a temporary structure | 
|---|
| 333 | */ | 
|---|
| 334 | void parseRemainderOptions( | 
|---|
| 335 | GetLongOpt &options, | 
|---|
| 336 | struct OptionValues &values, | 
|---|
| 337 | int argc, | 
|---|
| 338 | char **argv) | 
|---|
| 339 | { | 
|---|
| 340 | values.keyvalue = options.retrieve("k"); | 
|---|
| 341 | values.debug = options.retrieve("d"); | 
|---|
| 342 | values.limit = atoi(options.retrieve("l")); | 
|---|
| 343 | values.check = options.retrieve("c"); | 
|---|
| 344 | values.simple_input = options.retrieve("i"); | 
|---|
| 345 | values.executablename = argv[0]; | 
|---|
| 346 |  | 
|---|
| 347 | #ifdef HAVE_CHEMISTRY_CCA | 
|---|
| 348 | values.cca_load = options.retrieve("cca-load"); | 
|---|
| 349 | values.cca_path = options.retrieve("cca-path"); | 
|---|
| 350 | #endif | 
|---|
| 351 | } | 
|---|
| 352 |  | 
|---|
| 353 | /** Sets object and generic input file names. | 
|---|
| 354 | * | 
|---|
| 355 | * \param object_input filename of object-oriented input | 
|---|
| 356 | * \param generic_input filename of generic input | 
|---|
| 357 | * \param options (command-line)option structure | 
|---|
| 358 | * \param argc argument count | 
|---|
| 359 | * \param argv argument array | 
|---|
| 360 | */ | 
|---|
| 361 | void getInputFileNames( | 
|---|
| 362 | const char *&object_input, | 
|---|
| 363 | const char *&generic_input, | 
|---|
| 364 | GetLongOpt &options, | 
|---|
| 365 | int argc, | 
|---|
| 366 | char **argv) | 
|---|
| 367 | { | 
|---|
| 368 | // initialize keyval input | 
|---|
| 369 | object_input = options.retrieve("f"); | 
|---|
| 370 | generic_input = 0; | 
|---|
| 371 | if (argc - optind == 0) { | 
|---|
| 372 | generic_input = 0; | 
|---|
| 373 | } | 
|---|
| 374 | else if (argc - optind == 1) { | 
|---|
| 375 | generic_input = argv[optind]; | 
|---|
| 376 | } | 
|---|
| 377 | else { | 
|---|
| 378 | options.usage(); | 
|---|
| 379 | throw invalid_argument("extra arguments given"); | 
|---|
| 380 | } | 
|---|
| 381 |  | 
|---|
| 382 | if (object_input == 0 && generic_input == 0) { | 
|---|
| 383 | generic_input = "mpqc.in"; | 
|---|
| 384 | } | 
|---|
| 385 | else if (object_input && generic_input) { | 
|---|
| 386 | options.usage(); | 
|---|
| 387 | throw invalid_argument("only one of -f and a file argument can be given"); | 
|---|
| 388 | } | 
|---|
| 389 | } | 
|---|
| 390 |  | 
|---|
| 391 | /** Gets the MPI Message group. | 
|---|
| 392 | * | 
|---|
| 393 | * \param grp reference to obtained group | 
|---|
| 394 | * \param argc argument count | 
|---|
| 395 | * \param argv argument array | 
|---|
| 396 | */ | 
|---|
| 397 | void getMessageGroup( | 
|---|
| 398 | Ref<MessageGrp> &grp, | 
|---|
| 399 | int argc, | 
|---|
| 400 | char **argv) | 
|---|
| 401 | { | 
|---|
| 402 | #if defined(HAVE_MPI) && defined(ALWAYS_USE_MPI) | 
|---|
| 403 | grp = new MPIMessageGrp(&argc, &argv); | 
|---|
| 404 | #endif | 
|---|
| 405 | if (grp.null()) grp = MessageGrp::initial_messagegrp(argc, argv); | 
|---|
| 406 | if (grp.nonnull()) | 
|---|
| 407 | MessageGrp::set_default_messagegrp(grp); | 
|---|
| 408 | else | 
|---|
| 409 | grp = MessageGrp::get_default_messagegrp(); | 
|---|
| 410 | } | 
|---|
| 411 |  | 
|---|
| 412 | /** Sets the base name of output files. | 
|---|
| 413 | * | 
|---|
| 414 | * \param input input file name | 
|---|
| 415 | * \param output output file name | 
|---|
| 416 | */ | 
|---|
| 417 | void setOutputBaseName(const char *input, const char *output) | 
|---|
| 418 | { | 
|---|
| 419 | const char *basename_source; | 
|---|
| 420 | if (output) basename_source = output; | 
|---|
| 421 | else        basename_source = input; | 
|---|
| 422 | int nfilebase = (int) (::strrchr(basename_source, '.') - basename_source); | 
|---|
| 423 | char *basename = new char[nfilebase + 1]; | 
|---|
| 424 | strncpy(basename, basename_source, nfilebase); | 
|---|
| 425 | basename[nfilebase] = '\0'; | 
|---|
| 426 | SCFormIO::set_default_basename(basename); | 
|---|
| 427 | delete[] basename; | 
|---|
| 428 | } | 
|---|
| 429 |  | 
|---|
| 430 | /** Prints current key values. | 
|---|
| 431 | * | 
|---|
| 432 | * \param keyval key value structure | 
|---|
| 433 | * \param opt optimization structure | 
|---|
| 434 | * \param molname name of molecule | 
|---|
| 435 | * \param restartfile name of restartfile | 
|---|
| 436 | */ | 
|---|
| 437 | void printOptions( | 
|---|
| 438 | Ref<KeyVal> &keyval, | 
|---|
| 439 | Ref<Optimize> &opt, | 
|---|
| 440 | const char *molname, | 
|---|
| 441 | const char *restartfile) | 
|---|
| 442 | { | 
|---|
| 443 | int restart = keyval->booleanvalue("restart",truevalue); | 
|---|
| 444 |  | 
|---|
| 445 | int checkpoint = keyval->booleanvalue("checkpoint",truevalue); | 
|---|
| 446 |  | 
|---|
| 447 | int savestate = keyval->booleanvalue("savestate",truevalue); | 
|---|
| 448 |  | 
|---|
| 449 | int do_energy = keyval->booleanvalue("do_energy",truevalue); | 
|---|
| 450 |  | 
|---|
| 451 | int do_grad = keyval->booleanvalue("do_gradient",falsevalue); | 
|---|
| 452 |  | 
|---|
| 453 | int do_opt = keyval->booleanvalue("optimize",truevalue); | 
|---|
| 454 |  | 
|---|
| 455 | int do_pdb = keyval->booleanvalue("write_pdb",falsevalue); | 
|---|
| 456 |  | 
|---|
| 457 | int print_mole = keyval->booleanvalue("print_mole",truevalue); | 
|---|
| 458 |  | 
|---|
| 459 | int print_timings = keyval->booleanvalue("print_timings",truevalue); | 
|---|
| 460 |  | 
|---|
| 461 | // sanity checks for the benefit of reasonable looking output | 
|---|
| 462 | if (opt.null()) do_opt=0; | 
|---|
| 463 |  | 
|---|
| 464 | ExEnv::out0() << endl << indent | 
|---|
| 465 | << "MPQC options:" << endl << incindent | 
|---|
| 466 | << indent << "matrixkit     = <" | 
|---|
| 467 | << SCMatrixKit::default_matrixkit()->class_name() << ">" << endl | 
|---|
| 468 | << indent << "filename      = " << molname << endl | 
|---|
| 469 | << indent << "restart_file  = " << restartfile << endl | 
|---|
| 470 | << indent << "restart       = " << (restart ? "yes" : "no") << endl | 
|---|
| 471 | << indent << "checkpoint    = " << (checkpoint ? "yes" : "no") << endl | 
|---|
| 472 | << indent << "savestate     = " << (savestate ? "yes" : "no") << endl | 
|---|
| 473 | << indent << "do_energy     = " << (do_energy ? "yes" : "no") << endl | 
|---|
| 474 | << indent << "do_gradient   = " << (do_grad ? "yes" : "no") << endl | 
|---|
| 475 | << indent << "optimize      = " << (do_opt ? "yes" : "no") << endl | 
|---|
| 476 | << indent << "write_pdb     = " << (do_pdb ? "yes" : "no") << endl | 
|---|
| 477 | << indent << "print_mole    = " << (print_mole ? "yes" : "no") << endl | 
|---|
| 478 | << indent << "print_timings = " << (print_timings ? "yes" : "no") | 
|---|
| 479 | << endl << decindent; | 
|---|
| 480 |  | 
|---|
| 481 | } | 
|---|
| 482 |  | 
|---|
| 483 | /** Saves the current state to checkpoint file. | 
|---|
| 484 | * | 
|---|
| 485 | * \param keyval key value structure | 
|---|
| 486 | * \param opt optimization structure | 
|---|
| 487 | * \param grp message group | 
|---|
| 488 | * \param mole MolecularEnergy object | 
|---|
| 489 | * \param molname name of molecule | 
|---|
| 490 | * \param ckptfile name of check point file | 
|---|
| 491 | */ | 
|---|
| 492 | void saveState( | 
|---|
| 493 | char *wfn_file, | 
|---|
| 494 | int savestate, | 
|---|
| 495 | Ref<Optimize> &opt, | 
|---|
| 496 | Ref<MessageGrp> &grp, | 
|---|
| 497 | Ref<MolecularEnergy> &mole, | 
|---|
| 498 | char *&molname, | 
|---|
| 499 | char *&ckptfile) | 
|---|
| 500 | { | 
|---|
| 501 | // function stuff | 
|---|
| 502 | if (savestate) { | 
|---|
| 503 | if (opt.nonnull()) { | 
|---|
| 504 | if (grp->me() == 0) { | 
|---|
| 505 | ckptfile = new char[strlen(molname)+6]; | 
|---|
| 506 | sprintf(ckptfile,"%s.ckpt",molname); | 
|---|
| 507 | } | 
|---|
| 508 | else { | 
|---|
| 509 | ckptfile = new char[strlen(devnull)+1]; | 
|---|
| 510 | strcpy(ckptfile, devnull); | 
|---|
| 511 | } | 
|---|
| 512 |  | 
|---|
| 513 | StateOutBin so(ckptfile); | 
|---|
| 514 | SavableState::save_state(opt.pointer(),so); | 
|---|
| 515 | so.close(); | 
|---|
| 516 |  | 
|---|
| 517 | delete[] ckptfile; | 
|---|
| 518 | } | 
|---|
| 519 |  | 
|---|
| 520 | if (mole.nonnull()) { | 
|---|
| 521 | if (grp->me() == 0) { | 
|---|
| 522 | if (wfn_file == 0) { | 
|---|
| 523 | wfn_file = new char[strlen(molname)+6]; | 
|---|
| 524 | sprintf(wfn_file,"%s.wfn",molname); | 
|---|
| 525 | } | 
|---|
| 526 | } | 
|---|
| 527 | else { | 
|---|
| 528 | delete[] wfn_file; | 
|---|
| 529 | wfn_file = new char[strlen(devnull)+1]; | 
|---|
| 530 | strcpy(wfn_file, devnull); | 
|---|
| 531 | } | 
|---|
| 532 |  | 
|---|
| 533 | StateOutBin so(wfn_file); | 
|---|
| 534 | SavableState::save_state(mole.pointer(),so); | 
|---|
| 535 | so.close(); | 
|---|
| 536 |  | 
|---|
| 537 | } | 
|---|
| 538 | } | 
|---|
| 539 | delete[] wfn_file; | 
|---|
| 540 | } | 
|---|
| 541 |  | 
|---|
| 542 | /** Sets up indentation and output modes. | 
|---|
| 543 | * | 
|---|
| 544 | * \param grp message group | 
|---|
| 545 | */ | 
|---|
| 546 | void setupSCFormIO( | 
|---|
| 547 | Ref<MessageGrp> &grp | 
|---|
| 548 | ) | 
|---|
| 549 | { | 
|---|
| 550 | SCFormIO::setindent(ExEnv::outn(), 2); | 
|---|
| 551 | SCFormIO::setindent(ExEnv::errn(), 2); | 
|---|
| 552 | SCFormIO::setindent(cout, 2); | 
|---|
| 553 | SCFormIO::setindent(cerr, 2); | 
|---|
| 554 |  | 
|---|
| 555 | SCFormIO::set_printnode(0); | 
|---|
| 556 | if (grp->n() > 1) | 
|---|
| 557 | SCFormIO::init_mp(grp->me()); | 
|---|
| 558 | } | 
|---|
| 559 |  | 
|---|
| 560 | /** Initialises the timer. | 
|---|
| 561 | * | 
|---|
| 562 | * \param grp message group | 
|---|
| 563 | * \param keyval key value structure | 
|---|
| 564 | * \param tim timing structure | 
|---|
| 565 | */ | 
|---|
| 566 | void initTimings( | 
|---|
| 567 | Ref<MessageGrp> &grp, | 
|---|
| 568 | Ref<KeyVal> &keyval, | 
|---|
| 569 | Ref<RegionTimer> &tim | 
|---|
| 570 | ) | 
|---|
| 571 | { | 
|---|
| 572 | grp->sync(); // make sure nodes are sync'ed before starting timings | 
|---|
| 573 | if (keyval->exists("timer")) tim << keyval->describedclassvalue("timer"); | 
|---|
| 574 | else                         tim = new ParallelRegionTimer(grp,"mpqc",1,1); | 
|---|
| 575 | RegionTimer::set_default_regiontimer(tim); | 
|---|
| 576 |  | 
|---|
| 577 | if (tim.nonnull()) tim->enter("input"); | 
|---|
| 578 | } | 
|---|
| 579 |  | 
|---|
| 580 | /** Prints the header of the output. | 
|---|
| 581 | * | 
|---|
| 582 | * \param tim timing structure | 
|---|
| 583 | */ | 
|---|
| 584 | void makeAnnouncement( | 
|---|
| 585 | Ref<RegionTimer> &tim | 
|---|
| 586 | ) | 
|---|
| 587 | { | 
|---|
| 588 | const char title1[] = "MPQC: Massively Parallel Quantum Chemistry"; | 
|---|
| 589 | int ntitle1 = sizeof(title1); | 
|---|
| 590 | const char title2[] = "Version " SC_VERSION; | 
|---|
| 591 | int ntitle2 = sizeof(title2); | 
|---|
| 592 | ExEnv::out0() << endl; | 
|---|
| 593 | ExEnv::out0() << indent; | 
|---|
| 594 | for (int i=0; i<(80-ntitle1)/2; i++) ExEnv::out0() << ' '; | 
|---|
| 595 | ExEnv::out0() << title1 << endl; | 
|---|
| 596 | ExEnv::out0() << indent; | 
|---|
| 597 | for (int i=0; i<(80-ntitle2)/2; i++) ExEnv::out0() << ' '; | 
|---|
| 598 | ExEnv::out0() << title2 << endl << endl; | 
|---|
| 599 |  | 
|---|
| 600 | const char *tstr = 0; | 
|---|
| 601 | #if defined(HAVE_TIME) && defined(HAVE_CTIME) | 
|---|
| 602 | time_t t; | 
|---|
| 603 | time(&t); | 
|---|
| 604 | tstr = ctime(&t); | 
|---|
| 605 | #endif | 
|---|
| 606 | if (!tstr) { | 
|---|
| 607 | tstr = "UNKNOWN"; | 
|---|
| 608 | } | 
|---|
| 609 |  | 
|---|
| 610 | ExEnv::out0() | 
|---|
| 611 | << indent << scprintf("Machine:    %s", TARGET_ARCH) << endl | 
|---|
| 612 | << indent << scprintf("User:       %s@%s", | 
|---|
| 613 | ExEnv::username(), ExEnv::hostname()) << endl | 
|---|
| 614 | << indent << scprintf("Start Time: %s", tstr) << endl; | 
|---|
| 615 | } | 
|---|
| 616 |  | 
|---|
| 617 | /** Parse the input file into the key value container. | 
|---|
| 618 | * | 
|---|
| 619 | * \param grp message group | 
|---|
| 620 | * \param parsedkev keyvalue container on return | 
|---|
| 621 | * \param values (command-line) options structure | 
|---|
| 622 | * \param input input file name | 
|---|
| 623 | * \param generic_input filename of generic input | 
|---|
| 624 | */ | 
|---|
| 625 | void parseInputfile( | 
|---|
| 626 | Ref<MessageGrp> &grp, | 
|---|
| 627 | Ref<ParsedKeyVal> &parsedkv, | 
|---|
| 628 | struct OptionValues &values, | 
|---|
| 629 | const char *&input, | 
|---|
| 630 | const char *&generic_input | 
|---|
| 631 | ) | 
|---|
| 632 | { | 
|---|
| 633 | // read the input file on only node 0 | 
|---|
| 634 | char *in_char_array; | 
|---|
| 635 | if (grp->me() == 0) { | 
|---|
| 636 | ifstream is(input); | 
|---|
| 637 | #ifdef HAVE_SSTREAM | 
|---|
| 638 | ostringstream ostrs; | 
|---|
| 639 | is >> ostrs.rdbuf(); | 
|---|
| 640 | int n = 1 + strlen(ostrs.str().c_str()); | 
|---|
| 641 | in_char_array = strcpy(new char[n],ostrs.str().c_str()); | 
|---|
| 642 | #else | 
|---|
| 643 | ostrstream ostrs; | 
|---|
| 644 | is >> ostrs.rdbuf(); | 
|---|
| 645 | ostrs << ends; | 
|---|
| 646 | in_char_array = ostrs.str(); | 
|---|
| 647 | int n = ostrs.pcount(); | 
|---|
| 648 | #endif | 
|---|
| 649 | grp->bcast(n); | 
|---|
| 650 | grp->bcast(in_char_array, n); | 
|---|
| 651 | } | 
|---|
| 652 | else { | 
|---|
| 653 | int n; | 
|---|
| 654 | grp->bcast(n); | 
|---|
| 655 | in_char_array = new char[n]; | 
|---|
| 656 | grp->bcast(in_char_array, n); | 
|---|
| 657 | } | 
|---|
| 658 |  | 
|---|
| 659 | int use_simple_input; | 
|---|
| 660 | if (generic_input && grp->me() == 0) { | 
|---|
| 661 | MPQCIn mpqcin; | 
|---|
| 662 | use_simple_input = mpqcin.check_string(in_char_array); | 
|---|
| 663 | } | 
|---|
| 664 | else { | 
|---|
| 665 | use_simple_input = 0; | 
|---|
| 666 | } | 
|---|
| 667 | grp->bcast(use_simple_input); | 
|---|
| 668 |  | 
|---|
| 669 | if (use_simple_input) { | 
|---|
| 670 | MPQCIn mpqcin; | 
|---|
| 671 | char *simple_input_text = mpqcin.parse_string(in_char_array); | 
|---|
| 672 | if (values.simple_input) { | 
|---|
| 673 | ExEnv::out0() << "Generated object-oriented input file:" << endl | 
|---|
| 674 | << simple_input_text | 
|---|
| 675 | << endl; | 
|---|
| 676 | exit(0); | 
|---|
| 677 | } | 
|---|
| 678 | parsedkv = new ParsedKeyVal(); | 
|---|
| 679 | parsedkv->parse_string(simple_input_text); | 
|---|
| 680 | delete[] simple_input_text; | 
|---|
| 681 | } | 
|---|
| 682 | else { | 
|---|
| 683 | parsedkv = new ParsedKeyVal(); | 
|---|
| 684 | parsedkv->parse_string(in_char_array); | 
|---|
| 685 | } | 
|---|
| 686 | delete[] in_char_array; | 
|---|
| 687 | } | 
|---|
| 688 |  | 
|---|
| 689 | /** Get the thread group. | 
|---|
| 690 | * | 
|---|
| 691 | * \param keyval keyvalue container | 
|---|
| 692 | * \param thread thread group on return | 
|---|
| 693 | * \param argc argument count | 
|---|
| 694 | * \param argv argument array | 
|---|
| 695 | */ | 
|---|
| 696 | void getThreadGroup( | 
|---|
| 697 | Ref<KeyVal> &keyval, | 
|---|
| 698 | Ref<ThreadGrp> &thread, | 
|---|
| 699 | int argc, | 
|---|
| 700 | char **argv) | 
|---|
| 701 | { | 
|---|
| 702 | //first try the commandline and environment | 
|---|
| 703 | thread = ThreadGrp::initial_threadgrp(argc, argv); | 
|---|
| 704 |  | 
|---|
| 705 | // if we still don't have a group, try reading the thread group | 
|---|
| 706 | // from the input | 
|---|
| 707 | if (thread.null()) { | 
|---|
| 708 | thread << keyval->describedclassvalue("thread"); | 
|---|
| 709 | } | 
|---|
| 710 |  | 
|---|
| 711 | if (thread.nonnull()) | 
|---|
| 712 | ThreadGrp::set_default_threadgrp(thread); | 
|---|
| 713 | else | 
|---|
| 714 | thread = ThreadGrp::get_default_threadgrp(); | 
|---|
| 715 | } | 
|---|
| 716 |  | 
|---|
| 717 | /** Get the memory group. | 
|---|
| 718 | * | 
|---|
| 719 | * \param keyval keyvalue container | 
|---|
| 720 | * \param memory memory group on return | 
|---|
| 721 | * \param argc argument count | 
|---|
| 722 | * \param argv argument array | 
|---|
| 723 | */ | 
|---|
| 724 | void getMemoryGroup( | 
|---|
| 725 | Ref<KeyVal> &keyval, | 
|---|
| 726 | Ref<MemoryGrp> &memory, | 
|---|
| 727 | int argc, | 
|---|
| 728 | char **argv) | 
|---|
| 729 | { | 
|---|
| 730 | // first try the commandline and environment | 
|---|
| 731 | memory = MemoryGrp::initial_memorygrp(argc, argv); | 
|---|
| 732 |  | 
|---|
| 733 | // if we still don't have a group, try reading the memory group | 
|---|
| 734 | // from the input | 
|---|
| 735 | if (memory.null()) { | 
|---|
| 736 | memory << keyval->describedclassvalue("memory"); | 
|---|
| 737 | } | 
|---|
| 738 |  | 
|---|
| 739 | if (memory.nonnull()) | 
|---|
| 740 | MemoryGrp::set_default_memorygrp(memory); | 
|---|
| 741 | else | 
|---|
| 742 | memory = MemoryGrp::get_default_memorygrp(); | 
|---|
| 743 | } | 
|---|
| 744 |  | 
|---|
| 745 | /** Prepares CCA component if available. | 
|---|
| 746 | * | 
|---|
| 747 | * \param keyval keyvalue container | 
|---|
| 748 | * \param values parsed (command-line) options | 
|---|
| 749 | */ | 
|---|
| 750 | void prepareCCA( | 
|---|
| 751 | Ref<KeyVal> &keyval, | 
|---|
| 752 | struct OptionValues &values | 
|---|
| 753 | ) | 
|---|
| 754 | { | 
|---|
| 755 | #ifdef HAVE_CHEMISTRY_CCA | 
|---|
| 756 | // initialize cca framework | 
|---|
| 757 | KeyValValuestring emptystring(""); | 
|---|
| 758 | bool do_cca = keyval->booleanvalue("do_cca",falsevalue); | 
|---|
| 759 |  | 
|---|
| 760 | string cca_path(values.cca_path); | 
|---|
| 761 | string cca_load(values.cca_load); | 
|---|
| 762 | if(cca_path.size()==0) | 
|---|
| 763 | cca_path = keyval->stringvalue("cca_path",emptystring); | 
|---|
| 764 | if(cca_load.size()==0) | 
|---|
| 765 | cca_load = keyval->stringvalue("cca_load",emptystring); | 
|---|
| 766 |  | 
|---|
| 767 | if( !do_cca && (cca_load.size() > 0 || cca_path.size() > 0) ) | 
|---|
| 768 | do_cca = true; | 
|---|
| 769 |  | 
|---|
| 770 | if(cca_path.size()==0) { | 
|---|
| 771 | #ifdef CCA_PATH | 
|---|
| 772 | cca_path = CCA_PATH; | 
|---|
| 773 | #endif | 
|---|
| 774 | } | 
|---|
| 775 | if(cca_load.size()==0) { | 
|---|
| 776 | cca_load += "MPQC.IntegralEvaluatorFactory"; | 
|---|
| 777 | } | 
|---|
| 778 |  | 
|---|
| 779 | if( cca_load.size() > 0 && cca_path.size() > 0 && do_cca ) { | 
|---|
| 780 | string cca_args = "--path " + cca_path + " --load " + cca_load; | 
|---|
| 781 | ExEnv::out0() << endl << indent << "Initializing CCA framework with args: " | 
|---|
| 782 | << endl << indent << cca_args << endl; | 
|---|
| 783 | CCAEnv::init( cca_args ); | 
|---|
| 784 | } | 
|---|
| 785 | #endif | 
|---|
| 786 | } | 
|---|
| 787 |  | 
|---|
| 788 | /** Setup debugger. | 
|---|
| 789 | * | 
|---|
| 790 | * \param keyval keyvalue container | 
|---|
| 791 | * \param grp message group | 
|---|
| 792 | * \param debugger debugger structure | 
|---|
| 793 | * \param options parsed command line options | 
|---|
| 794 | */ | 
|---|
| 795 | void setupDebugger( | 
|---|
| 796 | Ref<KeyVal> &keyval, | 
|---|
| 797 | Ref<MessageGrp> &grp, | 
|---|
| 798 | Ref<Debugger> &debugger, | 
|---|
| 799 | struct OptionValues &values) | 
|---|
| 800 | { | 
|---|
| 801 | debugger << keyval->describedclassvalue("debug"); | 
|---|
| 802 | if (debugger.nonnull()) { | 
|---|
| 803 | Debugger::set_default_debugger(debugger); | 
|---|
| 804 | debugger->set_exec(values.executablename.c_str()); | 
|---|
| 805 | debugger->set_prefix(grp->me()); | 
|---|
| 806 | if (values.debug) | 
|---|
| 807 | debugger->debug("Starting debugger because -d given on command line."); | 
|---|
| 808 | } | 
|---|
| 809 | } | 
|---|
| 810 |  | 
|---|
| 811 | /** Get integral factory. | 
|---|
| 812 | * | 
|---|
| 813 | * \param keyval keyvalue container | 
|---|
| 814 | * \param integral integral group on return | 
|---|
| 815 | * \param argc argument count | 
|---|
| 816 | * \param argv argument array | 
|---|
| 817 | */ | 
|---|
| 818 | void getIntegralFactory( | 
|---|
| 819 | Ref<KeyVal> &keyval, | 
|---|
| 820 | Ref<Integral> &integral, | 
|---|
| 821 | int argc, | 
|---|
| 822 | char **argv) | 
|---|
| 823 | { | 
|---|
| 824 | // first try commandline and environment | 
|---|
| 825 | integral = Integral::initial_integral(argc, argv); | 
|---|
| 826 |  | 
|---|
| 827 | // if we still don't have a integral, try reading the integral | 
|---|
| 828 | // from the input | 
|---|
| 829 | if (integral.null()) { | 
|---|
| 830 | integral << keyval->describedclassvalue("integrals"); | 
|---|
| 831 | } | 
|---|
| 832 |  | 
|---|
| 833 | if (integral.nonnull()) | 
|---|
| 834 | Integral::set_default_integral(integral); | 
|---|
| 835 | else | 
|---|
| 836 | integral = Integral::get_default_integral(); | 
|---|
| 837 |  | 
|---|
| 838 | } | 
|---|
| 839 |  | 
|---|
| 840 | void performRestart( | 
|---|
| 841 | Ref<KeyVal> &keyval, | 
|---|
| 842 | Ref<MessageGrp> &grp, | 
|---|
| 843 | Ref<Optimize> &opt, | 
|---|
| 844 | Ref<MolecularEnergy> &mole, | 
|---|
| 845 | char *&restartfile | 
|---|
| 846 | ) | 
|---|
| 847 | { | 
|---|
| 848 | int restart = keyval->booleanvalue("restart",truevalue); | 
|---|
| 849 | struct stat sb; | 
|---|
| 850 | int statresult, statsize; | 
|---|
| 851 | if (restart) { | 
|---|
| 852 | if (grp->me() == 0) { | 
|---|
| 853 | statresult = stat(restartfile,&sb); | 
|---|
| 854 | statsize = (statresult==0) ? sb.st_size : 0; | 
|---|
| 855 | } | 
|---|
| 856 | grp->bcast(statresult); | 
|---|
| 857 | grp->bcast(statsize); | 
|---|
| 858 | } | 
|---|
| 859 | if (restart && statresult==0 && statsize) { | 
|---|
| 860 | BcastStateInBin si(grp,restartfile); | 
|---|
| 861 | if (keyval->exists("override")) { | 
|---|
| 862 | si.set_override(new PrefixKeyVal(keyval,"override")); | 
|---|
| 863 | } | 
|---|
| 864 | char *suf = strrchr(restartfile,'.'); | 
|---|
| 865 | if (!strcmp(suf,".wfn")) { | 
|---|
| 866 | mole << SavableState::key_restore_state(si,"mole"); | 
|---|
| 867 | ExEnv::out0() << endl | 
|---|
| 868 | << indent << "Restored <" << mole->class_name() | 
|---|
| 869 | << "> from " << restartfile << endl; | 
|---|
| 870 |  | 
|---|
| 871 | opt << keyval->describedclassvalue("opt"); | 
|---|
| 872 | if (opt.nonnull()) | 
|---|
| 873 | opt->set_function(mole.pointer()); | 
|---|
| 874 | } | 
|---|
| 875 | else { | 
|---|
| 876 | opt << SavableState::key_restore_state(si,"opt"); | 
|---|
| 877 | if (opt.nonnull()) { | 
|---|
| 878 | mole << opt->function(); | 
|---|
| 879 | ExEnv::out0() << endl << indent | 
|---|
| 880 | << "Restored <Optimize> from " << restartfile << endl; | 
|---|
| 881 | } | 
|---|
| 882 | } | 
|---|
| 883 | } else { | 
|---|
| 884 | mole << keyval->describedclassvalue("mole"); | 
|---|
| 885 | opt << keyval->describedclassvalue("opt"); | 
|---|
| 886 | } | 
|---|
| 887 | } | 
|---|
| 888 |  | 
|---|
| 889 | char *setMolecularCheckpointFile( | 
|---|
| 890 | Ref<KeyVal> &keyval, | 
|---|
| 891 | Ref<MessageGrp> &grp, | 
|---|
| 892 | Ref<MolecularEnergy> &mole, | 
|---|
| 893 | char *mole_ckpt_file | 
|---|
| 894 | ) | 
|---|
| 895 | { | 
|---|
| 896 | int checkpoint = keyval->booleanvalue("checkpoint",truevalue); | 
|---|
| 897 | int checkpoint_freq = keyval->intvalue("checkpoint_freq",KeyValValueint(1)); | 
|---|
| 898 | if (mole.nonnull()) { | 
|---|
| 899 | MolecularFormula mf(mole->molecule()); | 
|---|
| 900 | ExEnv::out0() << endl << indent | 
|---|
| 901 | << "Molecular formula " << mf.formula() << endl; | 
|---|
| 902 | if (checkpoint) { | 
|---|
| 903 | mole->set_checkpoint(); | 
|---|
| 904 | if (grp->me() == 0) mole->set_checkpoint_file(mole_ckpt_file); | 
|---|
| 905 | else mole->set_checkpoint_file(devnull); | 
|---|
| 906 | mole->set_checkpoint_freq(checkpoint_freq); | 
|---|
| 907 | } | 
|---|
| 908 | } | 
|---|
| 909 | } | 
|---|
| 910 |  | 
|---|
| 911 | /** Checks whether limit on command-line exceeds the basis functions. | 
|---|
| 912 | * | 
|---|
| 913 | * \param mole molecular energy object | 
|---|
| 914 | * \param values temporarily storage for (command-line) options | 
|---|
| 915 | * \return 0 - not exceeded, 1 - exceeded | 
|---|
| 916 | */ | 
|---|
| 917 | int checkBasisSetLimit( | 
|---|
| 918 | Ref<MolecularEnergy> &mole, | 
|---|
| 919 | struct OptionValues &values | 
|---|
| 920 | ) | 
|---|
| 921 | { | 
|---|
| 922 | int check = (values.check != (const char *)0); | 
|---|
| 923 | int limit = values.limit; | 
|---|
| 924 | if (limit) { | 
|---|
| 925 | Ref<Wavefunction> wfn; wfn << mole; | 
|---|
| 926 | if (wfn.nonnull() && wfn->ao_dimension()->n() > limit) { | 
|---|
| 927 | ExEnv::out0() << endl << indent | 
|---|
| 928 | << "The limit of " << limit << " basis functions has been exceeded." | 
|---|
| 929 | << endl; | 
|---|
| 930 | check = 1; | 
|---|
| 931 | } | 
|---|
| 932 | } | 
|---|
| 933 | return check; | 
|---|
| 934 | } | 
|---|
| 935 |  | 
|---|
| 936 | /** Performs the energy optimization. | 
|---|
| 937 | * | 
|---|
| 938 | * \param opt optimization object | 
|---|
| 939 | * \param mole molecular energy object | 
|---|
| 940 | * \return 0 - not read for frequency calculation, 1 - ready | 
|---|
| 941 | */ | 
|---|
| 942 | int performEnergyOptimization( | 
|---|
| 943 | Ref<Optimize> &opt, | 
|---|
| 944 | Ref<MolecularEnergy> &mole | 
|---|
| 945 | ) | 
|---|
| 946 | { | 
|---|
| 947 | int ready_for_freq = 0; | 
|---|
| 948 | int result = opt->optimize(); | 
|---|
| 949 | if (result) { | 
|---|
| 950 | ExEnv::out0() << indent | 
|---|
| 951 | << "The optimization has converged." << endl << endl; | 
|---|
| 952 | ExEnv::out0() << indent | 
|---|
| 953 | << scprintf("Value of the MolecularEnergy: %15.10f", | 
|---|
| 954 | mole->energy()) | 
|---|
| 955 | << endl << endl; | 
|---|
| 956 | ready_for_freq = 1; | 
|---|
| 957 | } else { | 
|---|
| 958 | ExEnv::out0() << indent | 
|---|
| 959 | << "The optimization has NOT converged." << endl << endl; | 
|---|
| 960 | ready_for_freq = 0; | 
|---|
| 961 | } | 
|---|
| 962 | return ready_for_freq; | 
|---|
| 963 | } | 
|---|
| 964 |  | 
|---|
| 965 | /** Performs gradient calculation. | 
|---|
| 966 | * | 
|---|
| 967 | * \param mole molecular energy object | 
|---|
| 968 | */ | 
|---|
| 969 | void performGradientCalculation( | 
|---|
| 970 | Ref<MolecularEnergy> &mole | 
|---|
| 971 | ) | 
|---|
| 972 | { | 
|---|
| 973 | mole->do_gradient(1); | 
|---|
| 974 | ExEnv::out0() << endl << indent | 
|---|
| 975 | << scprintf("Value of the MolecularEnergy: %15.10f", | 
|---|
| 976 | mole->energy()) | 
|---|
| 977 | << endl; | 
|---|
| 978 | if (mole->value_result().actual_accuracy() | 
|---|
| 979 | > mole->value_result().desired_accuracy()) { | 
|---|
| 980 | ExEnv::out0() << indent | 
|---|
| 981 | << "WARNING: desired accuracy not achieved in energy" << endl; | 
|---|
| 982 | } | 
|---|
| 983 | ExEnv::out0() << endl; | 
|---|
| 984 | // Use result_noupdate since the energy might not have converged | 
|---|
| 985 | // to the desired accuracy in which case grabbing the result will | 
|---|
| 986 | // start up the calculation again.  However the gradient might | 
|---|
| 987 | // not have been computed (if we are restarting and the gradient | 
|---|
| 988 | // isn't in the save file for example). | 
|---|
| 989 | RefSCVector grad; | 
|---|
| 990 | if (mole->gradient_result().computed()) { | 
|---|
| 991 | grad = mole->gradient_result().result_noupdate(); | 
|---|
| 992 | } | 
|---|
| 993 | else { | 
|---|
| 994 | grad = mole->gradient(); | 
|---|
| 995 | } | 
|---|
| 996 | if (grad.nonnull()) { | 
|---|
| 997 | grad.print("Gradient of the MolecularEnergy:"); | 
|---|
| 998 | if (mole->gradient_result().actual_accuracy() | 
|---|
| 999 | > mole->gradient_result().desired_accuracy()) { | 
|---|
| 1000 | ExEnv::out0() << indent | 
|---|
| 1001 | << "WARNING: desired accuracy not achieved in gradient" << endl; | 
|---|
| 1002 | } | 
|---|
| 1003 | } | 
|---|
| 1004 | } | 
|---|
| 1005 |  | 
|---|
| 1006 | /** Performs frequency calculation. | 
|---|
| 1007 | * | 
|---|
| 1008 | * \param mole molecular energy object | 
|---|
| 1009 | * \param molhess molecular hessian object | 
|---|
| 1010 | * \param molfreq molecular frequency object | 
|---|
| 1011 | */ | 
|---|
| 1012 | void performFrequencyCalculation( | 
|---|
| 1013 | Ref<MolecularEnergy> &mole, | 
|---|
| 1014 | Ref<MolecularHessian> &molhess, | 
|---|
| 1015 | Ref<MolecularFrequencies> &molfreq | 
|---|
| 1016 |  | 
|---|
| 1017 | ) | 
|---|
| 1018 | { | 
|---|
| 1019 | RefSymmSCMatrix xhessian; | 
|---|
| 1020 | if (molhess.nonnull()) { | 
|---|
| 1021 | // if "hess" input was given, use it to compute the hessian | 
|---|
| 1022 | xhessian = molhess->cartesian_hessian(); | 
|---|
| 1023 | } | 
|---|
| 1024 | else if (mole->hessian_implemented()) { | 
|---|
| 1025 | // if mole can compute the hessian, use that hessian | 
|---|
| 1026 | xhessian = mole->get_cartesian_hessian(); | 
|---|
| 1027 | } | 
|---|
| 1028 | else if (mole->gradient_implemented()) { | 
|---|
| 1029 | // if mole can compute gradients, use gradients at finite | 
|---|
| 1030 | // displacements to compute the hessian | 
|---|
| 1031 | molhess = new FinDispMolecularHessian(mole); | 
|---|
| 1032 | xhessian = molhess->cartesian_hessian(); | 
|---|
| 1033 | } | 
|---|
| 1034 | else { | 
|---|
| 1035 | ExEnv::out0() << "mpqc: WARNING: Frequencies cannot be computed" << endl; | 
|---|
| 1036 | } | 
|---|
| 1037 |  | 
|---|
| 1038 | if (xhessian.nonnull()) { | 
|---|
| 1039 | char *hessfile = SCFormIO::fileext_to_filename(".hess"); | 
|---|
| 1040 | MolecularHessian::write_cartesian_hessian(hessfile, | 
|---|
| 1041 | mole->molecule(), xhessian); | 
|---|
| 1042 | delete[] hessfile; | 
|---|
| 1043 |  | 
|---|
| 1044 | molfreq->compute_frequencies(xhessian); | 
|---|
| 1045 | // DEGENERACY IS NOT CORRECT FOR NON-SINGLET CASES: | 
|---|
| 1046 | molfreq->thermochemistry(1); | 
|---|
| 1047 | } | 
|---|
| 1048 | } | 
|---|
| 1049 |  | 
|---|
| 1050 | /** Renders some objects. | 
|---|
| 1051 | * | 
|---|
| 1052 | * \param renderer renderer object | 
|---|
| 1053 | * \param keyval keyvalue container | 
|---|
| 1054 | * \param tim timing object | 
|---|
| 1055 | * \param grp message group | 
|---|
| 1056 | */ | 
|---|
| 1057 | void renderObjects( | 
|---|
| 1058 | Ref<Render> &renderer, | 
|---|
| 1059 | Ref<KeyVal> &keyval, | 
|---|
| 1060 | Ref<RegionTimer> &tim, | 
|---|
| 1061 | Ref<MessageGrp> &grp | 
|---|
| 1062 | ) | 
|---|
| 1063 | { | 
|---|
| 1064 | Ref<RenderedObject> rendered; | 
|---|
| 1065 | rendered << keyval->describedclassvalue("rendered"); | 
|---|
| 1066 | Ref<AnimatedObject> animated; | 
|---|
| 1067 | animated << keyval->describedclassvalue("rendered"); | 
|---|
| 1068 | if (rendered.nonnull()) { | 
|---|
| 1069 | if (tim.nonnull()) tim->enter("render"); | 
|---|
| 1070 | if (grp->me() == 0) renderer->render(rendered); | 
|---|
| 1071 | if (tim.nonnull()) tim->exit("render"); | 
|---|
| 1072 | } | 
|---|
| 1073 | else if (animated.nonnull()) { | 
|---|
| 1074 | if (tim.nonnull()) tim->enter("render"); | 
|---|
| 1075 | if (grp->me() == 0) renderer->animate(animated); | 
|---|
| 1076 | if (tim.nonnull()) tim->exit("render"); | 
|---|
| 1077 | } | 
|---|
| 1078 | else { | 
|---|
| 1079 | if (tim.nonnull()) tim->enter("render"); | 
|---|
| 1080 | int n = keyval->count("rendered"); | 
|---|
| 1081 | for (int i=0; i<n; i++) { | 
|---|
| 1082 | rendered << keyval->describedclassvalue("rendered",i); | 
|---|
| 1083 | animated << keyval->describedclassvalue("rendered",i); | 
|---|
| 1084 | if (rendered.nonnull()) { | 
|---|
| 1085 | // make sure the object has a name so we don't overwrite its file | 
|---|
| 1086 | if (rendered->name() == 0) { | 
|---|
| 1087 | char ic[64]; | 
|---|
| 1088 | sprintf(ic,"%02d",i); | 
|---|
| 1089 | rendered->set_name(ic); | 
|---|
| 1090 | } | 
|---|
| 1091 | if (grp->me() == 0) renderer->render(rendered); | 
|---|
| 1092 | } | 
|---|
| 1093 | else if (animated.nonnull()) { | 
|---|
| 1094 | // make sure the object has a name so we don't overwrite its file | 
|---|
| 1095 | if (animated->name() == 0) { | 
|---|
| 1096 | char ic[64]; | 
|---|
| 1097 | sprintf(ic,"%02d",i); | 
|---|
| 1098 | animated->set_name(ic); | 
|---|
| 1099 | } | 
|---|
| 1100 | if (grp->me() == 0) renderer->animate(animated); | 
|---|
| 1101 | } | 
|---|
| 1102 | } | 
|---|
| 1103 | if (tim.nonnull()) tim->exit("render"); | 
|---|
| 1104 | } | 
|---|
| 1105 | } | 
|---|
| 1106 |  | 
|---|
| 1107 | /** Save the molecule to PDB file. | 
|---|
| 1108 | * | 
|---|
| 1109 | * \param do_pdb whether to save as pdb (1) or not (0) | 
|---|
| 1110 | * \param grp message group | 
|---|
| 1111 | * \param mole molecular energy object | 
|---|
| 1112 | * \param molname name of output file | 
|---|
| 1113 | */ | 
|---|
| 1114 | void saveToPdb( | 
|---|
| 1115 | int do_pdb, | 
|---|
| 1116 | Ref<MessageGrp> &grp, | 
|---|
| 1117 | Ref<MolecularEnergy> &mole, | 
|---|
| 1118 | const char *molname | 
|---|
| 1119 | ) | 
|---|
| 1120 | { | 
|---|
| 1121 | if (do_pdb && grp->me() == 0) { | 
|---|
| 1122 | char *ckptfile = new char[strlen(molname)+5]; | 
|---|
| 1123 | sprintf(ckptfile, "%s.pdb", molname); | 
|---|
| 1124 | ofstream pdbfile(ckptfile); | 
|---|
| 1125 | mole->molecule()->print_pdb(pdbfile); | 
|---|
| 1126 | delete[] ckptfile; | 
|---|
| 1127 | } | 
|---|
| 1128 | } | 
|---|
| 1129 |  | 
|---|
| 1130 | void Work(ostream *&outstream, struct OptionValues &values, const char *&input, const char *&generic_input, int argc, char **argv); | 
|---|
| 1131 |  | 
|---|
| 1132 | int | 
|---|
| 1133 | try_main(int argc, char *argv[]) | 
|---|
| 1134 | { | 
|---|
| 1135 | //trash_stack(); | 
|---|
| 1136 |  | 
|---|
| 1137 | int i; | 
|---|
| 1138 | atexit(clean_up); | 
|---|
| 1139 |  | 
|---|
| 1140 | #ifdef HAVE_FEENABLEEXCEPT | 
|---|
| 1141 | // this uses a glibc extension to trap on individual exceptions | 
|---|
| 1142 | # ifdef FE_DIVBYZERO | 
|---|
| 1143 | feenableexcept(FE_DIVBYZERO); | 
|---|
| 1144 | # endif | 
|---|
| 1145 | # ifdef FE_INVALID | 
|---|
| 1146 | feenableexcept(FE_INVALID); | 
|---|
| 1147 | # endif | 
|---|
| 1148 | # ifdef FE_OVERFLOW | 
|---|
| 1149 | feenableexcept(FE_OVERFLOW); | 
|---|
| 1150 | # endif | 
|---|
| 1151 | #endif | 
|---|
| 1152 |  | 
|---|
| 1153 | #ifdef HAVE_FEDISABLEEXCEPT | 
|---|
| 1154 | // this uses a glibc extension to not trap on individual exceptions | 
|---|
| 1155 | # ifdef FE_UNDERFLOW | 
|---|
| 1156 | fedisableexcept(FE_UNDERFLOW); | 
|---|
| 1157 | # endif | 
|---|
| 1158 | # ifdef FE_INEXACT | 
|---|
| 1159 | fedisableexcept(FE_INEXACT); | 
|---|
| 1160 | # endif | 
|---|
| 1161 | #endif | 
|---|
| 1162 |  | 
|---|
| 1163 | #if defined(HAVE_SETRLIMIT) | 
|---|
| 1164 | struct rlimit rlim; | 
|---|
| 1165 | rlim.rlim_cur = 0; | 
|---|
| 1166 | rlim.rlim_max = 0; | 
|---|
| 1167 | setrlimit(RLIMIT_CORE,&rlim); | 
|---|
| 1168 | #endif | 
|---|
| 1169 |  | 
|---|
| 1170 | ExEnv::init(argc, argv); | 
|---|
| 1171 |  | 
|---|
| 1172 | // parse commandline options | 
|---|
| 1173 | GetLongOpt options; | 
|---|
| 1174 | int optind = ParseOptions(options, argc, argv); | 
|---|
| 1175 | const char *output = 0; | 
|---|
| 1176 | ostream *outstream = 0; | 
|---|
| 1177 | ComputeOptions(options, output, outstream); | 
|---|
| 1178 | OptionValues values; | 
|---|
| 1179 | parseRemainderOptions(options, values, argc, argv); | 
|---|
| 1180 |  | 
|---|
| 1181 | // get input file names, either object-oriented or generic | 
|---|
| 1182 | const char *object_input = 0; | 
|---|
| 1183 | const char *generic_input = 0; | 
|---|
| 1184 | getInputFileNames(object_input, generic_input, options, argc, argv); | 
|---|
| 1185 | const char *input; | 
|---|
| 1186 | if (object_input) input = object_input; | 
|---|
| 1187 | if (generic_input) input = generic_input; | 
|---|
| 1188 |  | 
|---|
| 1189 | // get the basename for output files | 
|---|
| 1190 | setOutputBaseName(input, output); | 
|---|
| 1191 |  | 
|---|
| 1192 | // now comes the actual work | 
|---|
| 1193 | Work(outstream, values, input, generic_input, argc, argv); | 
|---|
| 1194 |  | 
|---|
| 1195 | if (output != 0) { | 
|---|
| 1196 | ExEnv::set_out(&cout); | 
|---|
| 1197 | delete outstream; | 
|---|
| 1198 | } | 
|---|
| 1199 |  | 
|---|
| 1200 | return 0; | 
|---|
| 1201 | } | 
|---|
| 1202 |  | 
|---|
| 1203 | void Work(ostream *&outstream, struct OptionValues &values, const char *&input, const char *&generic_input, int argc, char **argv) | 
|---|
| 1204 | { | 
|---|
| 1205 | // get the message group.  first try the commandline and environment | 
|---|
| 1206 | Ref<MessageGrp> grp; | 
|---|
| 1207 | getMessageGroup(grp, argc, argv); | 
|---|
| 1208 |  | 
|---|
| 1209 | // parse input into keyvalue container | 
|---|
| 1210 | Ref<ParsedKeyVal> parsedkv; | 
|---|
| 1211 | parseInputfile(grp, parsedkv, values, input, generic_input); | 
|---|
| 1212 |  | 
|---|
| 1213 | // prefix parsed values wit "mpqc" | 
|---|
| 1214 | if (values.keyvalue) parsedkv->verbose(1); | 
|---|
| 1215 | Ref<KeyVal> keyval = new PrefixKeyVal(parsedkv.pointer(),"mpqc"); | 
|---|
| 1216 |  | 
|---|
| 1217 | // set up output classes | 
|---|
| 1218 | setupSCFormIO(grp); | 
|---|
| 1219 |  | 
|---|
| 1220 | // initialize timing for mpqc | 
|---|
| 1221 | Ref<RegionTimer> tim; | 
|---|
| 1222 | initTimings(grp, keyval, tim); | 
|---|
| 1223 |  | 
|---|
| 1224 | // announce ourselves | 
|---|
| 1225 | makeAnnouncement(tim); | 
|---|
| 1226 |  | 
|---|
| 1227 | // get the thread group. | 
|---|
| 1228 | Ref<ThreadGrp> thread; | 
|---|
| 1229 | getThreadGroup(keyval, thread, argc, argv); | 
|---|
| 1230 |  | 
|---|
| 1231 | // get the memory group. | 
|---|
| 1232 | Ref<MemoryGrp> memory; | 
|---|
| 1233 | getMemoryGroup(keyval, memory, argc, argv); | 
|---|
| 1234 |  | 
|---|
| 1235 | ExEnv::out0() << indent | 
|---|
| 1236 | << "Using " << grp->class_name() | 
|---|
| 1237 | << " for message passing (number of nodes = " << grp->n() << ")." << endl | 
|---|
| 1238 | << indent | 
|---|
| 1239 | << "Using " << thread->class_name() | 
|---|
| 1240 | << " for threading (number of threads = " << thread->nthread() << ")." << endl | 
|---|
| 1241 | << indent | 
|---|
| 1242 | << "Using " << memory->class_name() | 
|---|
| 1243 | << " for distributed shared memory." << endl | 
|---|
| 1244 | << indent | 
|---|
| 1245 | << "Total number of processors = " << grp->n() * thread->nthread() << endl; | 
|---|
| 1246 |  | 
|---|
| 1247 | // prepare CCA if available | 
|---|
| 1248 | prepareCCA(keyval, values); | 
|---|
| 1249 |  | 
|---|
| 1250 | // now set up the debugger | 
|---|
| 1251 | Ref<Debugger> debugger; | 
|---|
| 1252 | setupDebugger(keyval, grp, debugger, values); | 
|---|
| 1253 |  | 
|---|
| 1254 | // now check to see what matrix kit to use | 
|---|
| 1255 | if (keyval->exists("matrixkit")) | 
|---|
| 1256 | SCMatrixKit::set_default_matrixkit( | 
|---|
| 1257 | dynamic_cast<SCMatrixKit*>( | 
|---|
| 1258 | keyval->describedclassvalue("matrixkit").pointer())); | 
|---|
| 1259 |  | 
|---|
| 1260 | // get the integral factory. | 
|---|
| 1261 | Ref<Integral> integral; | 
|---|
| 1262 | getIntegralFactory(keyval, integral, argc, argv); | 
|---|
| 1263 | ExEnv::out0() << endl << indent | 
|---|
| 1264 | << "Using " << integral->class_name() | 
|---|
| 1265 | << " by default for molecular integrals evaluation" << endl << endl; | 
|---|
| 1266 |  | 
|---|
| 1267 | // create some filenames for molecule, checkpoint, basename of output | 
|---|
| 1268 | const char *basename = SCFormIO::default_basename(); | 
|---|
| 1269 | KeyValValueString molnamedef(basename); | 
|---|
| 1270 | char * molname = keyval->pcharvalue("filename", molnamedef); | 
|---|
| 1271 | if (strcmp(molname, basename)) | 
|---|
| 1272 | SCFormIO::set_default_basename(molname); | 
|---|
| 1273 |  | 
|---|
| 1274 | char * ckptfile = new char[strlen(molname)+6]; | 
|---|
| 1275 | sprintf(ckptfile,"%s.ckpt",molname); | 
|---|
| 1276 |  | 
|---|
| 1277 | KeyValValueString restartfiledef(ckptfile); | 
|---|
| 1278 | char * restartfile = keyval->pcharvalue("restart_file", restartfiledef); | 
|---|
| 1279 |  | 
|---|
| 1280 | char * wfn_file = keyval->pcharvalue("wfn_file"); | 
|---|
| 1281 | if (wfn_file == 0) { | 
|---|
| 1282 | wfn_file = new char[strlen(molname)+6]; | 
|---|
| 1283 | sprintf(wfn_file,"%s.wfn",molname); | 
|---|
| 1284 | } | 
|---|
| 1285 | char *mole_ckpt_file = new char[strlen(wfn_file)+1]; | 
|---|
| 1286 | sprintf(mole_ckpt_file,"%s",wfn_file); | 
|---|
| 1287 |  | 
|---|
| 1288 | int savestate = keyval->booleanvalue("savestate",truevalue); | 
|---|
| 1289 |  | 
|---|
| 1290 | // setup molecular energy and optimization instances | 
|---|
| 1291 | Ref<MolecularEnergy> mole; | 
|---|
| 1292 | Ref<Optimize> opt; | 
|---|
| 1293 |  | 
|---|
| 1294 | // read in restart file if we do restart | 
|---|
| 1295 | performRestart(keyval, grp, opt, mole, restartfile); | 
|---|
| 1296 |  | 
|---|
| 1297 | // setup molecule checkpoint file | 
|---|
| 1298 | setMolecularCheckpointFile(keyval, grp, mole, mole_ckpt_file); | 
|---|
| 1299 | delete[] mole_ckpt_file; | 
|---|
| 1300 |  | 
|---|
| 1301 | int checkpoint = keyval->booleanvalue("checkpoint",truevalue); | 
|---|
| 1302 | if (checkpoint && opt.nonnull()) { | 
|---|
| 1303 | opt->set_checkpoint(); | 
|---|
| 1304 | if (grp->me() == 0) opt->set_checkpoint_file(ckptfile); | 
|---|
| 1305 | else opt->set_checkpoint_file(devnull); | 
|---|
| 1306 | } | 
|---|
| 1307 |  | 
|---|
| 1308 | // see if frequencies are wanted | 
|---|
| 1309 | Ref<MolecularHessian> molhess; | 
|---|
| 1310 | molhess << keyval->describedclassvalue("hess"); | 
|---|
| 1311 | Ref<MolecularFrequencies> molfreq; | 
|---|
| 1312 | molfreq << keyval->describedclassvalue("freq"); | 
|---|
| 1313 |  | 
|---|
| 1314 | // check basis set limit | 
|---|
| 1315 | const int check = checkBasisSetLimit(mole, values); | 
|---|
| 1316 | if (check) { | 
|---|
| 1317 | ExEnv::out0() << endl << indent | 
|---|
| 1318 | << "Exiting since the check option is on." << endl; | 
|---|
| 1319 | exit(0); | 
|---|
| 1320 | } | 
|---|
| 1321 |  | 
|---|
| 1322 | // from now on we time the calculations | 
|---|
| 1323 | if (tim.nonnull()) tim->change("calc"); | 
|---|
| 1324 |  | 
|---|
| 1325 | int do_energy = keyval->booleanvalue("do_energy",truevalue); | 
|---|
| 1326 |  | 
|---|
| 1327 | int do_grad = keyval->booleanvalue("do_gradient",falsevalue); | 
|---|
| 1328 |  | 
|---|
| 1329 | int do_opt = keyval->booleanvalue("optimize",truevalue); | 
|---|
| 1330 |  | 
|---|
| 1331 | int do_pdb = keyval->booleanvalue("write_pdb",falsevalue); | 
|---|
| 1332 |  | 
|---|
| 1333 | int print_mole = keyval->booleanvalue("print_mole",truevalue); | 
|---|
| 1334 |  | 
|---|
| 1335 | int print_timings = keyval->booleanvalue("print_timings",truevalue); | 
|---|
| 1336 |  | 
|---|
| 1337 | // print all current options (keyvalues) | 
|---|
| 1338 | printOptions(keyval, opt, molname, restartfile); | 
|---|
| 1339 |  | 
|---|
| 1340 | // see if any pictures are desired | 
|---|
| 1341 | Ref<Render> renderer; | 
|---|
| 1342 | renderer << keyval->describedclassvalue("renderer"); | 
|---|
| 1343 |  | 
|---|
| 1344 | // If we have a renderer, then we will read in some more info | 
|---|
| 1345 | // below.  Otherwise we can get rid of the keyval's, to eliminate | 
|---|
| 1346 | // superfluous references to objects that we might otherwise be | 
|---|
| 1347 | // able to delete.  We cannot read in the remaining rendering | 
|---|
| 1348 | // objects now, since some of their KeyVal CTOR's are heavyweight, | 
|---|
| 1349 | // requiring optimized geometries, etc. | 
|---|
| 1350 | if (renderer.null()) { | 
|---|
| 1351 | if (parsedkv.nonnull()) print_unseen(parsedkv, input); | 
|---|
| 1352 | keyval = 0; | 
|---|
| 1353 | parsedkv = 0; | 
|---|
| 1354 | } | 
|---|
| 1355 |  | 
|---|
| 1356 | delete[] restartfile; | 
|---|
| 1357 | delete[] ckptfile; | 
|---|
| 1358 |  | 
|---|
| 1359 | int ready_for_freq = 1; | 
|---|
| 1360 | if (mole.nonnull()) { | 
|---|
| 1361 | if (((do_opt && opt.nonnull()) || do_grad) | 
|---|
| 1362 | && !mole->gradient_implemented()) { | 
|---|
| 1363 | ExEnv::out0() << indent | 
|---|
| 1364 | << "WARNING: optimization or gradient requested but the given" | 
|---|
| 1365 | << endl | 
|---|
| 1366 | << "         MolecularEnergy object cannot do gradients." | 
|---|
| 1367 | << endl; | 
|---|
| 1368 | } | 
|---|
| 1369 |  | 
|---|
| 1370 | if (do_opt && opt.nonnull() && mole->gradient_implemented()) { | 
|---|
| 1371 |  | 
|---|
| 1372 | ready_for_freq = performEnergyOptimization(opt, mole); | 
|---|
| 1373 |  | 
|---|
| 1374 | } else if (do_grad && mole->gradient_implemented()) { | 
|---|
| 1375 |  | 
|---|
| 1376 | performGradientCalculation(mole); | 
|---|
| 1377 |  | 
|---|
| 1378 | } else if (do_energy && mole->value_implemented()) { | 
|---|
| 1379 | ExEnv::out0() << endl << indent | 
|---|
| 1380 | << scprintf("Value of the MolecularEnergy: %15.10f", | 
|---|
| 1381 | mole->energy()) | 
|---|
| 1382 | << endl << endl; | 
|---|
| 1383 | } | 
|---|
| 1384 | } | 
|---|
| 1385 |  | 
|---|
| 1386 | // stop timing of calculations | 
|---|
| 1387 | if (tim.nonnull()) tim->exit("calc"); | 
|---|
| 1388 |  | 
|---|
| 1389 | // save this before doing the frequency stuff since that obsoletes the | 
|---|
| 1390 | saveState(wfn_file, savestate, opt, grp, mole, molname, ckptfile); | 
|---|
| 1391 |  | 
|---|
| 1392 | // Frequency calculation. | 
|---|
| 1393 | if (ready_for_freq && molfreq.nonnull()) { | 
|---|
| 1394 | performFrequencyCalculation(mole, molhess, molfreq); | 
|---|
| 1395 | } | 
|---|
| 1396 |  | 
|---|
| 1397 | if (renderer.nonnull()) { | 
|---|
| 1398 | renderObjects(renderer, keyval, tim, grp); | 
|---|
| 1399 |  | 
|---|
| 1400 | Ref<MolFreqAnimate> molfreqanim; | 
|---|
| 1401 | molfreqanim << keyval->describedclassvalue("animate_modes"); | 
|---|
| 1402 | if (ready_for_freq && molfreq.nonnull() | 
|---|
| 1403 | && molfreqanim.nonnull()) { | 
|---|
| 1404 | if (tim.nonnull()) tim->enter("render"); | 
|---|
| 1405 | molfreq->animate(renderer, molfreqanim); | 
|---|
| 1406 | if (tim.nonnull()) tim->exit("render"); | 
|---|
| 1407 | } | 
|---|
| 1408 | } | 
|---|
| 1409 |  | 
|---|
| 1410 | if (mole.nonnull()) { | 
|---|
| 1411 | if (print_mole) | 
|---|
| 1412 | mole->print(ExEnv::out0()); | 
|---|
| 1413 |  | 
|---|
| 1414 | saveToPdb(do_pdb, grp, mole, molname); | 
|---|
| 1415 |  | 
|---|
| 1416 | } | 
|---|
| 1417 | else { | 
|---|
| 1418 | ExEnv::out0() << "mpqc: The molecular energy object is null" << endl | 
|---|
| 1419 | << " make sure \"mole\" specifies a MolecularEnergy derivative" | 
|---|
| 1420 | << endl; | 
|---|
| 1421 | } | 
|---|
| 1422 | if (parsedkv.nonnull()) print_unseen(parsedkv, input); | 
|---|
| 1423 |  | 
|---|
| 1424 | if (print_timings) | 
|---|
| 1425 | if (tim.nonnull()) tim->print(ExEnv::out0()); | 
|---|
| 1426 |  | 
|---|
| 1427 | // here, we may gather the results | 
|---|
| 1428 | // we start to construct the MPQC_Data object | 
|---|
| 1429 | { | 
|---|
| 1430 | Ref<Wavefunction> wfn; | 
|---|
| 1431 | wfn << mole; | 
|---|
| 1432 | ExEnv::out0() << "The number of atomic orbitals: " << wfn->ao_dimension()->n() << endl; | 
|---|
| 1433 | ExEnv::out0() << "The AO density matrix is "; | 
|---|
| 1434 | wfn->ao_density()->print(ExEnv::out0()); | 
|---|
| 1435 | ExEnv::out0() << "The natural density matrix is "; | 
|---|
| 1436 | wfn->natural_density()->print(ExEnv::out0()); | 
|---|
| 1437 | ExEnv::out0() << "The Gaussian basis is " << wfn->basis()->name() << endl; | 
|---|
| 1438 | ExEnv::out0() << "The Gaussians sit at the following centers: " << endl; | 
|---|
| 1439 | for (int nr = 0; nr< wfn->basis()->ncenter(); ++nr) { | 
|---|
| 1440 | ExEnv::out0() << nr << " basis function has its center at "; | 
|---|
| 1441 | for (int i=0; i < 3; ++i) | 
|---|
| 1442 | ExEnv::out0() << wfn->basis()->r(nr,i) << "\t"; | 
|---|
| 1443 | ExEnv::out0() << endl; | 
|---|
| 1444 | } | 
|---|
| 1445 | // GaussianShell is the actual orbital functions it seems ... | 
|---|
| 1446 | //ExEnv::out0() << "There are the following Gaussian Shells: " << endl; | 
|---|
| 1447 | SCVector3 r; | 
|---|
| 1448 | r.x() = r.y() = r.z() = 10; | 
|---|
| 1449 | ExEnv::out0() << "We get the following value at " << r << "." << endl; | 
|---|
| 1450 | Ref<Integral> intgrl = Integral::get_default_integral(); | 
|---|
| 1451 | GaussianBasisSet::ValueData vdat(wfn->basis(), integral); | 
|---|
| 1452 | ExEnv::out0() << "Value at (10,10,10) is " << EvaluateDensity(r, intgrl, vdat, wfn) << endl; | 
|---|
| 1453 | boost::function<double (SCVector3 &r)> evaluator = | 
|---|
| 1454 | boost::bind(&EvaluateDensity, _1, boost::ref(intgrl), boost::ref(vdat), boost::ref(wfn)); | 
|---|
| 1455 | ExEnv::out0() << "Check against values at " << r << "." << endl; | 
|---|
| 1456 | int nbasis = wfn->basis()->nbasis(); | 
|---|
| 1457 | double *b_val = new double[nbasis]; | 
|---|
| 1458 | wfn->basis()->values(r, &vdat, b_val); | 
|---|
| 1459 | for (int i=0; i<nbasis; i++) { | 
|---|
| 1460 | //ExEnv::out0() << "Shell nr. " << nr << ": "; | 
|---|
| 1461 | ExEnv::out0() << "Value at (10,10,10) is " << b_val[i] << endl; | 
|---|
| 1462 | } | 
|---|
| 1463 | // perform test integration of density | 
|---|
| 1464 | double delta = 1.; | 
|---|
| 1465 | double sum = 0.; | 
|---|
| 1466 | for (r.x() = -10. ; r.x() < 10.; r.x() += delta) | 
|---|
| 1467 | for (r.y() = -10. ; r.y() < 10.; r.y() += delta) | 
|---|
| 1468 | for (r.z() = -10. ; r.z() < 10.; r.z() += delta) { | 
|---|
| 1469 | wfn->basis()->values(r, &vdat, b_val); | 
|---|
| 1470 | for (int i=0; i<nbasis; i++) | 
|---|
| 1471 | sum += wfn->ao_density()->get_element(i,i)*b_val[i]; | 
|---|
| 1472 | } | 
|---|
| 1473 | sum /= pow(20/delta,3); | 
|---|
| 1474 | ExEnv::out0() << "Sum over domain [0:20]^3 with " << delta << " delta is " << sum << "." << endl; | 
|---|
| 1475 | delete[] b_val; | 
|---|
| 1476 | } | 
|---|
| 1477 |  | 
|---|
| 1478 | delete[] molname; | 
|---|
| 1479 | SCFormIO::set_default_basename(0); | 
|---|
| 1480 |  | 
|---|
| 1481 | renderer = 0; | 
|---|
| 1482 | molfreq = 0; | 
|---|
| 1483 | molhess = 0; | 
|---|
| 1484 | opt = 0; | 
|---|
| 1485 | mole = 0; | 
|---|
| 1486 | integral = 0; | 
|---|
| 1487 | debugger = 0; | 
|---|
| 1488 | thread = 0; | 
|---|
| 1489 | tim = 0; | 
|---|
| 1490 | keyval = 0; | 
|---|
| 1491 | parsedkv = 0; | 
|---|
| 1492 | grp = 0; | 
|---|
| 1493 | memory = 0; | 
|---|
| 1494 | clean_up(); | 
|---|
| 1495 |  | 
|---|
| 1496 | #if defined(HAVE_TIME) && defined(HAVE_CTIME) | 
|---|
| 1497 | time_t t; | 
|---|
| 1498 | time(&t); | 
|---|
| 1499 | const char *tstr = ctime(&t); | 
|---|
| 1500 | #endif | 
|---|
| 1501 | if (!tstr) { | 
|---|
| 1502 | tstr = "UNKNOWN"; | 
|---|
| 1503 | } | 
|---|
| 1504 | ExEnv::out0() << endl | 
|---|
| 1505 | << indent << scprintf("End Time: %s", tstr) << endl; | 
|---|
| 1506 | } | 
|---|
| 1507 |  | 
|---|
| 1508 | double EvaluateDensity(SCVector3 &r, Ref<Integral> &intgrl, GaussianBasisSet::ValueData &vdat, Ref<Wavefunction> &wfn) | 
|---|
| 1509 | { | 
|---|
| 1510 | ExEnv::out0() << "We get the following values at " << r << "." << endl; | 
|---|
| 1511 | int nbasis = wfn->basis()->nbasis(); | 
|---|
| 1512 | double *b_val = new double[nbasis]; | 
|---|
| 1513 | wfn->basis()->values(r, &vdat, b_val); | 
|---|
| 1514 | double sum=0.; | 
|---|
| 1515 | for (int i=0; i<nbasis; i++) | 
|---|
| 1516 | sum += b_val[i]; | 
|---|
| 1517 | delete[] b_val; | 
|---|
| 1518 | return sum; | 
|---|
| 1519 | } | 
|---|
| 1520 |  | 
|---|
| 1521 | int | 
|---|
| 1522 | main(int argc, char *argv[]) | 
|---|
| 1523 | { | 
|---|
| 1524 | try { | 
|---|
| 1525 | try_main(argc, argv); | 
|---|
| 1526 | } | 
|---|
| 1527 | catch (SCException &e) { | 
|---|
| 1528 | cout << argv[0] << ": ERROR: SC EXCEPTION RAISED:" << endl | 
|---|
| 1529 | << e.what() | 
|---|
| 1530 | << endl; | 
|---|
| 1531 | clean_up(); | 
|---|
| 1532 | throw; | 
|---|
| 1533 | } | 
|---|
| 1534 | catch (bad_alloc &e) { | 
|---|
| 1535 | cout << argv[0] << ": ERROR: MEMORY ALLOCATION FAILED:" << endl | 
|---|
| 1536 | << e.what() | 
|---|
| 1537 | << endl; | 
|---|
| 1538 | clean_up(); | 
|---|
| 1539 | throw; | 
|---|
| 1540 | } | 
|---|
| 1541 | catch (exception &e) { | 
|---|
| 1542 | cout << argv[0] << ": ERROR: EXCEPTION RAISED:" << endl | 
|---|
| 1543 | << e.what() | 
|---|
| 1544 | << endl; | 
|---|
| 1545 | clean_up(); | 
|---|
| 1546 | throw; | 
|---|
| 1547 | } | 
|---|
| 1548 | catch (...) { | 
|---|
| 1549 | cout << argv[0] << ": ERROR: UNKNOWN EXCEPTION RAISED" << endl; | 
|---|
| 1550 | clean_up(); | 
|---|
| 1551 | throw; | 
|---|
| 1552 | } | 
|---|
| 1553 | return 0; | 
|---|
| 1554 | } | 
|---|
| 1555 |  | 
|---|
| 1556 | ///////////////////////////////////////////////////////////////////////////// | 
|---|
| 1557 |  | 
|---|
| 1558 | // Local Variables: | 
|---|
| 1559 | // mode: c++ | 
|---|
| 1560 | // c-file-style: "ETS" | 
|---|
| 1561 | // End: | 
|---|