source: src/bin/mpqc/mpqc.cc@ a8458d

Last change on this file since a8458d was a8458d, checked in by Frederik Heber <heber@…>, 13 years ago

Made truevalue and falsevalue into global constants.

  • Property mode set to 100644
File size: 35.9 KB
Line 
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
125using namespace std;
126using namespace sc;
127
128#include "mpqcin.h"
129
130//////////////////////////////////////////////////////////////////////////
131
132const KeyValValueboolean truevalue(1), falsevalue(0);
133
134
135static void
136trash_stack_b(int &i, char *&ichar)
137{
138 char stack;
139 ichar = &stack;
140 ichar -= 10;
141 for (i=0; i<1000; i++) {
142 *ichar-- = 0xfe;
143 }
144}
145
146static void
147trash_stack()
148{
149 int i;
150 char *ichar;
151 trash_stack_b(i,ichar);
152}
153
154static void
155clean_up(void)
156{
157 MemoryGrp::set_default_memorygrp(0);
158 MessageGrp::set_default_messagegrp(0);
159 ThreadGrp::set_default_threadgrp(0);
160 SCMatrixKit::set_default_matrixkit(0);
161 Integral::set_default_integral(0);
162 RegionTimer::set_default_regiontimer(0);
163}
164
165#include <signal.h>
166
167#ifdef HAVE_FENV_H
168# include <fenv.h>
169#endif
170
171static void
172print_unseen(const Ref<ParsedKeyVal> &parsedkv,
173 const char *input)
174{
175 if (parsedkv->have_unseen()) {
176 ExEnv::out0() << endl;
177 ExEnv::out0() << indent
178 << "The following keywords in \"" << input << "\" were ignored:"
179 << endl;
180 ExEnv::out0() << incindent;
181 parsedkv->print_unseen(ExEnv::out0());
182 ExEnv::out0() << decindent;
183 }
184}
185
186double EvaluateDensity(
187 SCVector3 &r,
188 Ref<Integral> &intgrl,
189 GaussianBasisSet::ValueData &vdat,
190 Ref<Wavefunction> &wfn);
191
192/** Places all known options into \a options and parses them from argc,argv.
193 *
194 * \param options options structure
195 * \param argc argument count
196 * \param argv argument array
197 * \return return value by GetLongOpt::parse() function
198 */
199int ParseOptions(
200 GetLongOpt &options,
201 int argc,
202 char **argv)
203{
204 options.usage("[options] [filename]");
205 options.enroll("f", GetLongOpt::MandatoryValue,
206 "the name of an object format input file", 0);
207 options.enroll("o", GetLongOpt::MandatoryValue,
208 "the name of the output file", 0);
209 options.enroll("n", GetLongOpt::NoValue,
210 "listen for incoming object format input files", 0);
211 options.enroll("messagegrp", GetLongOpt::MandatoryValue,
212 "which message group to use", 0);
213 options.enroll("threadgrp", GetLongOpt::MandatoryValue,
214 "which thread group to use", 0);
215 options.enroll("memorygrp", GetLongOpt::MandatoryValue,
216 "which memory group to use", 0);
217 options.enroll("integral", GetLongOpt::MandatoryValue,
218 "which integral evaluator to use", 0);
219 options.enroll("l", GetLongOpt::MandatoryValue, "basis set limit", "0");
220 options.enroll("W", GetLongOpt::MandatoryValue,
221 "set the working directory", ".");
222 options.enroll("c", GetLongOpt::NoValue, "check input then exit", 0);
223 options.enroll("v", GetLongOpt::NoValue, "print the version number", 0);
224 options.enroll("w", GetLongOpt::NoValue, "print the warranty", 0);
225 options.enroll("L", GetLongOpt::NoValue, "print the license", 0);
226 options.enroll("k", GetLongOpt::NoValue, "print key/value assignments", 0);
227 options.enroll("i", GetLongOpt::NoValue, "convert simple to OO input", 0);
228 options.enroll("d", GetLongOpt::NoValue, "debug", 0);
229 options.enroll("h", GetLongOpt::NoValue, "print this message", 0);
230 options.enroll("cca-path", GetLongOpt::OptionalValue,
231 "cca component path", "");
232 options.enroll("cca-load", GetLongOpt::OptionalValue,
233 "cca components to load", "");
234
235 int optind = options.parse(argc, argv);
236
237 return optind;
238}
239
240/** Checks for each known option and acts accordingly.
241 *
242 * \param options option structure
243 * \param *output name of outputfile on return
244 * \param *outstream open output stream on return
245 */
246void ComputeOptions(
247 GetLongOpt &options,
248 const char *&output,
249 ostream *&outstream)
250{
251 output = options.retrieve("o");
252 outstream = 0;
253 if (output != 0) {
254 outstream = new ofstream(output);
255 ExEnv::set_out(outstream);
256 }
257
258 if (options.retrieve("h")) {
259 ExEnv::out0()
260 << indent << "MPQC version " << SC_VERSION << endl
261 << indent << "compiled for " << TARGET_ARCH << endl
262 << SCFormIO::copyright << endl;
263 options.usage(ExEnv::out0());
264 exit(0);
265 }
266
267 if (options.retrieve("v")) {
268 ExEnv::out0()
269 << indent << "MPQC version " << SC_VERSION << endl
270 << indent << "compiled for " << TARGET_ARCH << endl
271 << SCFormIO::copyright;
272 exit(0);
273 }
274
275 if (options.retrieve("w")) {
276 ExEnv::out0()
277 << indent << "MPQC version " << SC_VERSION << endl
278 << indent << "compiled for " << TARGET_ARCH << endl
279 << SCFormIO::copyright << endl
280 << SCFormIO::warranty;
281 exit(0);
282 }
283
284 if (options.retrieve("L")) {
285 ExEnv::out0()
286 << indent << "MPQC version " << SC_VERSION << endl
287 << indent << "compiled for " << TARGET_ARCH << endl
288 << SCFormIO::copyright << endl
289 << SCFormIO::license;
290 exit(0);
291 }
292
293 // set the working dir
294 if (strcmp(options.retrieve("W"),"."))
295 int retval = chdir(options.retrieve("W"));
296
297 // check that n and f/o are not given at the same time
298 if ((options.retrieve("n")) && ((options.retrieve("f")) || (options.retrieve("o")))) {
299 throw invalid_argument("-n must not be given with -f or -o");
300 }
301}
302
303/** Sets object and generic input file names.
304 *
305 * \param object_input filename of object-oriented input
306 * \param generic_input filename of generic input
307 * \param options option structure
308 * \param argc argument count
309 * \param argv argument array
310 */
311void getInputFileNames(
312 const char *&object_input,
313 const char *&generic_input,
314 GetLongOpt &options,
315 int argc,
316 char **argv)
317{
318 // initialize keyval input
319 object_input = options.retrieve("f");
320 generic_input = 0;
321 if (argc - optind == 0) {
322 generic_input = 0;
323 }
324 else if (argc - optind == 1) {
325 generic_input = argv[optind];
326 }
327 else {
328 options.usage();
329 throw invalid_argument("extra arguments given");
330 }
331
332 if (object_input == 0 && generic_input == 0) {
333 generic_input = "mpqc.in";
334 }
335 else if (object_input && generic_input) {
336 options.usage();
337 throw invalid_argument("only one of -f and a file argument can be given");
338 }
339}
340
341/** Gets the MPI Message group.
342 *
343 * \param grp reference to obtained group
344 * \param argc argument count
345 * \param argv argument array
346 */
347void getMessageGroup(
348 Ref<MessageGrp> &grp,
349 int argc,
350 char **argv)
351{
352#if defined(HAVE_MPI) && defined(ALWAYS_USE_MPI)
353 grp = new MPIMessageGrp(&argc, &argv);
354#endif
355 if (grp.null()) grp = MessageGrp::initial_messagegrp(argc, argv);
356 if (grp.nonnull())
357 MessageGrp::set_default_messagegrp(grp);
358 else
359 grp = MessageGrp::get_default_messagegrp();
360}
361
362/** Sets the base name of output files.
363 *
364 * \param input input file name
365 * \param output output file name
366 */
367void setOutputBaseName(const char *input, const char *output)
368{
369 const char *basename_source;
370 if (output) basename_source = output;
371 else basename_source = input;
372 int nfilebase = (int) (::strrchr(basename_source, '.') - basename_source);
373 char *basename = new char[nfilebase + 1];
374 strncpy(basename, basename_source, nfilebase);
375 basename[nfilebase] = '\0';
376 SCFormIO::set_default_basename(basename);
377 delete[] basename;
378}
379
380/** Prints current key values.
381 *
382 * \param keyval key value structure
383 * \param opt optimization structure
384 * \param molname name of molecule
385 * \param name of restartfile
386 */
387void printOptions(
388 Ref<KeyVal> &keyval,
389 Ref<Optimize> &opt,
390 const char *molname,
391 const char *restartfile)
392{
393 int restart = keyval->booleanvalue("restart",truevalue);
394
395 int checkpoint = keyval->booleanvalue("checkpoint",truevalue);
396
397 int savestate = keyval->booleanvalue("savestate",truevalue);
398
399 int do_energy = keyval->booleanvalue("do_energy",truevalue);
400
401 int do_grad = keyval->booleanvalue("do_gradient",falsevalue);
402
403 int do_opt = keyval->booleanvalue("optimize",truevalue);
404
405 int do_pdb = keyval->booleanvalue("write_pdb",falsevalue);
406
407 int print_mole = keyval->booleanvalue("print_mole",truevalue);
408
409 int print_timings = keyval->booleanvalue("print_timings",truevalue);
410
411 // sanity checks for the benefit of reasonable looking output
412 if (opt.null()) do_opt=0;
413
414 ExEnv::out0() << endl << indent
415 << "MPQC options:" << endl << incindent
416 << indent << "matrixkit = <"
417 << SCMatrixKit::default_matrixkit()->class_name() << ">" << endl
418 << indent << "filename = " << molname << endl
419 << indent << "restart_file = " << restartfile << endl
420 << indent << "restart = " << (restart ? "yes" : "no") << endl
421 << indent << "checkpoint = " << (checkpoint ? "yes" : "no") << endl
422 << indent << "savestate = " << (savestate ? "yes" : "no") << endl
423 << indent << "do_energy = " << (do_energy ? "yes" : "no") << endl
424 << indent << "do_gradient = " << (do_grad ? "yes" : "no") << endl
425 << indent << "optimize = " << (do_opt ? "yes" : "no") << endl
426 << indent << "write_pdb = " << (do_pdb ? "yes" : "no") << endl
427 << indent << "print_mole = " << (print_mole ? "yes" : "no") << endl
428 << indent << "print_timings = " << (print_timings ? "yes" : "no")
429 << endl << decindent;
430
431}
432
433int
434try_main(int argc, char *argv[])
435{
436 //trash_stack();
437
438 int i;
439 const char *devnull = "/dev/null";
440 atexit(clean_up);
441
442#ifdef HAVE_FEENABLEEXCEPT
443 // this uses a glibc extension to trap on individual exceptions
444# ifdef FE_DIVBYZERO
445 feenableexcept(FE_DIVBYZERO);
446# endif
447# ifdef FE_INVALID
448 feenableexcept(FE_INVALID);
449# endif
450# ifdef FE_OVERFLOW
451 feenableexcept(FE_OVERFLOW);
452# endif
453#endif
454
455#ifdef HAVE_FEDISABLEEXCEPT
456 // this uses a glibc extension to not trap on individual exceptions
457# ifdef FE_UNDERFLOW
458 fedisableexcept(FE_UNDERFLOW);
459# endif
460# ifdef FE_INEXACT
461 fedisableexcept(FE_INEXACT);
462# endif
463#endif
464
465#if defined(HAVE_SETRLIMIT)
466 struct rlimit rlim;
467 rlim.rlim_cur = 0;
468 rlim.rlim_max = 0;
469 setrlimit(RLIMIT_CORE,&rlim);
470#endif
471
472 ExEnv::init(argc, argv);
473
474 // parse commandline options
475 GetLongOpt options;
476 int optind = ParseOptions(options, argc, argv);
477 const char *output = 0;
478 ostream *outstream = 0;
479 ComputeOptions(options, output, outstream);
480
481 // get the message group. first try the commandline and environment
482 Ref<MessageGrp> grp;
483 getMessageGroup(grp, argc, argv);
484
485 // get input file names, either object-oriented or generic
486 const char *object_input = 0;
487 const char *generic_input = 0;
488 getInputFileNames(object_input, generic_input, options, argc, argv);
489 const char *input;
490 if (object_input) input = object_input;
491 if (generic_input) input = generic_input;
492
493 Ref<ParsedKeyVal> parsedkv;
494 // read the input file on only node 0
495 char *in_char_array;
496 if (grp->me() == 0) {
497 ifstream is(input);
498#ifdef HAVE_SSTREAM
499 ostringstream ostrs;
500 is >> ostrs.rdbuf();
501 int n = 1 + strlen(ostrs.str().c_str());
502 in_char_array = strcpy(new char[n],ostrs.str().c_str());
503#else
504 ostrstream ostrs;
505 is >> ostrs.rdbuf();
506 ostrs << ends;
507 in_char_array = ostrs.str();
508 int n = ostrs.pcount();
509#endif
510 grp->bcast(n);
511 grp->bcast(in_char_array, n);
512 }
513 else {
514 int n;
515 grp->bcast(n);
516 in_char_array = new char[n];
517 grp->bcast(in_char_array, n);
518 }
519
520 int use_simple_input;
521 if (generic_input && grp->me() == 0) {
522 MPQCIn mpqcin;
523 use_simple_input = mpqcin.check_string(in_char_array);
524 }
525 else {
526 use_simple_input = 0;
527 }
528 grp->bcast(use_simple_input);
529
530 if (use_simple_input) {
531 MPQCIn mpqcin;
532 char *simple_input_text = mpqcin.parse_string(in_char_array);
533 if (options.retrieve("i")) {
534 ExEnv::out0() << "Generated object-oriented input file:" << endl
535 << simple_input_text
536 << endl;
537 exit(0);
538 }
539 parsedkv = new ParsedKeyVal();
540 parsedkv->parse_string(simple_input_text);
541 delete[] simple_input_text;
542 }
543 else {
544 parsedkv = new ParsedKeyVal();
545 parsedkv->parse_string(in_char_array);
546 }
547 delete[] in_char_array;
548
549 if (options.retrieve("k")) parsedkv->verbose(1);
550 Ref<KeyVal> keyval = new PrefixKeyVal(parsedkv.pointer(),"mpqc");
551
552 // get the basename for output files
553 setOutputBaseName(input, output);
554
555 // set up output classes
556 SCFormIO::setindent(ExEnv::outn(), 2);
557 SCFormIO::setindent(ExEnv::errn(), 2);
558 SCFormIO::setindent(cout, 2);
559 SCFormIO::setindent(cerr, 2);
560
561 SCFormIO::set_printnode(0);
562 if (grp->n() > 1)
563 SCFormIO::init_mp(grp->me());
564
565 if (options.retrieve("d"))
566 SCFormIO::set_debug(1);
567
568 // initialize timing for mpqc
569 grp->sync(); // make sure nodes are sync'ed before starting timings
570 Ref<RegionTimer> tim;
571 if (keyval->exists("timer")) tim << keyval->describedclassvalue("timer");
572 else tim = new ParallelRegionTimer(grp,"mpqc",1,1);
573 RegionTimer::set_default_regiontimer(tim);
574
575 if (tim.nonnull()) tim->enter("input");
576
577 // announce ourselves
578 const char title1[] = "MPQC: Massively Parallel Quantum Chemistry";
579 int ntitle1 = sizeof(title1);
580 const char title2[] = "Version " SC_VERSION;
581 int ntitle2 = sizeof(title2);
582 ExEnv::out0() << endl;
583 ExEnv::out0() << indent;
584 for (i=0; i<(80-ntitle1)/2; i++) ExEnv::out0() << ' ';
585 ExEnv::out0() << title1 << endl;
586 ExEnv::out0() << indent;
587 for (i=0; i<(80-ntitle2)/2; i++) ExEnv::out0() << ' ';
588 ExEnv::out0() << title2 << endl << endl;
589
590 const char *tstr = 0;
591#if defined(HAVE_TIME) && defined(HAVE_CTIME)
592 time_t t;
593 time(&t);
594 tstr = ctime(&t);
595#endif
596 if (!tstr) {
597 tstr = "UNKNOWN";
598 }
599
600 ExEnv::out0()
601 << indent << scprintf("Machine: %s", TARGET_ARCH) << endl
602 << indent << scprintf("User: %s@%s",
603 ExEnv::username(), ExEnv::hostname()) << endl
604 << indent << scprintf("Start Time: %s", tstr) << endl;
605
606 // get the thread group. first try the commandline and environment
607 Ref<ThreadGrp> thread = ThreadGrp::initial_threadgrp(argc, argv);
608
609 // if we still don't have a group, try reading the thread group
610 // from the input
611 if (thread.null()) {
612 thread << keyval->describedclassvalue("thread");
613 }
614
615 if (thread.nonnull())
616 ThreadGrp::set_default_threadgrp(thread);
617 else
618 thread = ThreadGrp::get_default_threadgrp();
619
620 // get the memory group. first try the commandline and environment
621 Ref<MemoryGrp> memory = MemoryGrp::initial_memorygrp(argc, argv);
622
623 // if we still don't have a group, try reading the memory group
624 // from the input
625 if (memory.null()) {
626 memory << keyval->describedclassvalue("memory");
627 }
628
629 if (memory.nonnull())
630 MemoryGrp::set_default_memorygrp(memory);
631 else
632 memory = MemoryGrp::get_default_memorygrp();
633
634 ExEnv::out0() << indent
635 << "Using " << grp->class_name()
636 << " for message passing (number of nodes = " << grp->n() << ")." << endl
637 << indent
638 << "Using " << thread->class_name()
639 << " for threading (number of threads = " << thread->nthread() << ")." << endl
640 << indent
641 << "Using " << memory->class_name()
642 << " for distributed shared memory." << endl
643 << indent
644 << "Total number of processors = " << grp->n() * thread->nthread() << endl;
645
646#ifdef HAVE_CHEMISTRY_CCA
647 // initialize cca framework
648 KeyValValuestring emptystring("");
649 bool do_cca = keyval->booleanvalue("do_cca",falsevalue);
650
651 string cca_path(options.retrieve("cca-path"));
652 string cca_load(options.retrieve("cca-load"));
653 if(cca_path.size()==0)
654 cca_path = keyval->stringvalue("cca_path",emptystring);
655 if(cca_load.size()==0)
656 cca_load = keyval->stringvalue("cca_load",emptystring);
657
658 if( !do_cca && (cca_load.size() > 0 || cca_path.size() > 0) )
659 do_cca = true;
660
661 if(cca_path.size()==0) {
662 #ifdef CCA_PATH
663 cca_path = CCA_PATH;
664 #endif
665 }
666 if(cca_load.size()==0) {
667 cca_load += "MPQC.IntegralEvaluatorFactory";
668 }
669
670 if( cca_load.size() > 0 && cca_path.size() > 0 && do_cca ) {
671 string cca_args = "--path " + cca_path + " --load " + cca_load;
672 ExEnv::out0() << endl << indent << "Initializing CCA framework with args: "
673 << endl << indent << cca_args << endl;
674 CCAEnv::init( cca_args );
675 }
676#endif
677
678 // now set up the debugger
679 Ref<Debugger> debugger; debugger << keyval->describedclassvalue("debug");
680 if (debugger.nonnull()) {
681 Debugger::set_default_debugger(debugger);
682 debugger->set_exec(argv[0]);
683 debugger->set_prefix(grp->me());
684 if (options.retrieve("d"))
685 debugger->debug("Starting debugger because -d given on command line.");
686 }
687
688 // now check to see what matrix kit to use
689 if (keyval->exists("matrixkit"))
690 SCMatrixKit::set_default_matrixkit(
691 dynamic_cast<SCMatrixKit*>(
692 keyval->describedclassvalue("matrixkit").pointer()));
693
694 // get the integral factory. first try commandline and environment
695 Ref<Integral> integral = Integral::initial_integral(argc, argv);
696
697 // if we still don't have a integral, try reading the integral
698 // from the input
699 if (integral.null()) {
700 integral << keyval->describedclassvalue("integrals");
701 }
702
703 if (integral.nonnull())
704 Integral::set_default_integral(integral);
705 else
706 integral = Integral::get_default_integral();
707
708 ExEnv::out0() << endl << indent
709 << "Using " << integral->class_name()
710 << " by default for molecular integrals evaluation" << endl << endl;
711
712 // check for a molecular energy and optimizer
713 const char *basename = SCFormIO::default_basename();
714 KeyValValueString molnamedef(basename);
715 char * molname = keyval->pcharvalue("filename", molnamedef);
716 if (strcmp(molname, basename))
717 SCFormIO::set_default_basename(molname);
718
719 char * ckptfile = new char[strlen(molname)+6];
720 sprintf(ckptfile,"%s.ckpt",molname);
721
722 KeyValValueString restartfiledef(ckptfile);
723 char * restartfile = keyval->pcharvalue("restart_file", restartfiledef);
724
725 char * wfn_file = keyval->pcharvalue("wfn_file");
726 if (wfn_file == 0) {
727 wfn_file = new char[strlen(molname)+6];
728 sprintf(wfn_file,"%s.wfn",molname);
729 }
730 char *mole_ckpt_file = new char[strlen(wfn_file)+1];
731 sprintf(mole_ckpt_file,"%s",wfn_file);
732
733 int restart = keyval->booleanvalue("restart",truevalue);
734
735 int checkpoint = keyval->booleanvalue("checkpoint",truevalue);
736 int checkpoint_freq = keyval->intvalue("checkpoint_freq",KeyValValueint(1));
737
738 int savestate = keyval->booleanvalue("savestate",truevalue);
739
740 struct stat sb;
741 Ref<MolecularEnergy> mole;
742 Ref<Optimize> opt;
743
744 int statresult, statsize;
745 if (restart) {
746 if (grp->me() == 0) {
747 statresult = stat(restartfile,&sb);
748 statsize = (statresult==0) ? sb.st_size : 0;
749 }
750 grp->bcast(statresult);
751 grp->bcast(statsize);
752 }
753 if (restart && statresult==0 && statsize) {
754 BcastStateInBin si(grp,restartfile);
755 if (keyval->exists("override")) {
756 si.set_override(new PrefixKeyVal(keyval,"override"));
757 }
758 char *suf = strrchr(restartfile,'.');
759 if (!strcmp(suf,".wfn")) {
760 mole << SavableState::key_restore_state(si,"mole");
761 ExEnv::out0() << endl
762 << indent << "Restored <" << mole->class_name()
763 << "> from " << restartfile << endl;
764
765 opt << keyval->describedclassvalue("opt");
766 if (opt.nonnull())
767 opt->set_function(mole.pointer());
768 }
769 else {
770 opt << SavableState::key_restore_state(si,"opt");
771 if (opt.nonnull()) {
772 mole << opt->function();
773 ExEnv::out0() << endl << indent
774 << "Restored <Optimize> from " << restartfile << endl;
775 }
776 }
777 } else {
778 mole << keyval->describedclassvalue("mole");
779 opt << keyval->describedclassvalue("opt");
780 }
781
782 if (mole.nonnull()) {
783 MolecularFormula mf(mole->molecule());
784 ExEnv::out0() << endl << indent
785 << "Molecular formula " << mf.formula() << endl;
786 if (checkpoint) {
787 mole->set_checkpoint();
788 if (grp->me() == 0) mole->set_checkpoint_file(mole_ckpt_file);
789 else mole->set_checkpoint_file(devnull);
790 mole->set_checkpoint_freq(checkpoint_freq);
791 }
792 }
793 delete[] mole_ckpt_file;
794
795 if (checkpoint && opt.nonnull()) {
796 opt->set_checkpoint();
797 if (grp->me() == 0) opt->set_checkpoint_file(ckptfile);
798 else opt->set_checkpoint_file(devnull);
799 }
800
801 // see if frequencies are wanted
802
803 Ref<MolecularHessian> molhess;
804 molhess << keyval->describedclassvalue("hess");
805 Ref<MolecularFrequencies> molfreq;
806 molfreq << keyval->describedclassvalue("freq");
807
808 int check = (options.retrieve("c") != 0);
809 int limit = atoi(options.retrieve("l"));
810 if (limit) {
811 Ref<Wavefunction> wfn; wfn << mole;
812 if (wfn.nonnull() && wfn->ao_dimension()->n() > limit) {
813 ExEnv::out0() << endl << indent
814 << "The limit of " << limit << " basis functions has been exceeded."
815 << endl;
816 check = 1;
817 }
818 }
819
820 if (check) {
821 ExEnv::out0() << endl << indent
822 << "Exiting since the check option is on." << endl;
823 exit(0);
824 }
825
826 if (tim.nonnull()) tim->change("calc");
827
828 int do_energy = keyval->booleanvalue("do_energy",truevalue);
829
830 int do_grad = keyval->booleanvalue("do_gradient",falsevalue);
831
832 int do_opt = keyval->booleanvalue("optimize",truevalue);
833
834 int do_pdb = keyval->booleanvalue("write_pdb",falsevalue);
835
836 int print_mole = keyval->booleanvalue("print_mole",truevalue);
837
838 int print_timings = keyval->booleanvalue("print_timings",truevalue);
839
840 // print all current options (keyvalues)
841 printOptions(keyval, opt, molname, restartfile);
842
843 // see if any pictures are desired
844 Ref<Render> renderer;
845 renderer << keyval->describedclassvalue("renderer");
846
847 // If we have a renderer, then we will read in some more info
848 // below. Otherwise we can get rid of the keyval's, to eliminate
849 // superfluous references to objects that we might otherwise be
850 // able to delete. We cannot read in the remaining rendering
851 // objects now, since some of their KeyVal CTOR's are heavyweight,
852 // requiring optimized geometries, etc.
853 if (renderer.null()) {
854 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
855 keyval = 0;
856 parsedkv = 0;
857 }
858
859 delete[] restartfile;
860 delete[] ckptfile;
861
862 int ready_for_freq = 1;
863 if (mole.nonnull()) {
864 if (((do_opt && opt.nonnull()) || do_grad)
865 && !mole->gradient_implemented()) {
866 ExEnv::out0() << indent
867 << "WARNING: optimization or gradient requested but the given"
868 << endl
869 << " MolecularEnergy object cannot do gradients."
870 << endl;
871 }
872
873 if (do_opt && opt.nonnull() && mole->gradient_implemented()) {
874 int result = opt->optimize();
875 if (result) {
876 ExEnv::out0() << indent
877 << "The optimization has converged." << endl << endl;
878 ExEnv::out0() << indent
879 << scprintf("Value of the MolecularEnergy: %15.10f",
880 mole->energy())
881 << endl << endl;
882 } else {
883 ExEnv::out0() << indent
884 << "The optimization has NOT converged." << endl << endl;
885 ready_for_freq = 0;
886 }
887 } else if (do_grad && mole->gradient_implemented()) {
888 mole->do_gradient(1);
889 ExEnv::out0() << endl << indent
890 << scprintf("Value of the MolecularEnergy: %15.10f",
891 mole->energy())
892 << endl;
893 if (mole->value_result().actual_accuracy()
894 > mole->value_result().desired_accuracy()) {
895 ExEnv::out0() << indent
896 << "WARNING: desired accuracy not achieved in energy" << endl;
897 }
898 ExEnv::out0() << endl;
899 // Use result_noupdate since the energy might not have converged
900 // to the desired accuracy in which case grabbing the result will
901 // start up the calculation again. However the gradient might
902 // not have been computed (if we are restarting and the gradient
903 // isn't in the save file for example).
904 RefSCVector grad;
905 if (mole->gradient_result().computed()) {
906 grad = mole->gradient_result().result_noupdate();
907 }
908 else {
909 grad = mole->gradient();
910 }
911 if (grad.nonnull()) {
912 grad.print("Gradient of the MolecularEnergy:");
913 if (mole->gradient_result().actual_accuracy()
914 > mole->gradient_result().desired_accuracy()) {
915 ExEnv::out0() << indent
916 << "WARNING: desired accuracy not achieved in gradient" << endl;
917 }
918 }
919 } else if (do_energy && mole->value_implemented()) {
920 ExEnv::out0() << endl << indent
921 << scprintf("Value of the MolecularEnergy: %15.10f",
922 mole->energy())
923 << endl << endl;
924 }
925 }
926
927 if (tim.nonnull()) tim->exit("calc");
928
929 // save this before doing the frequency stuff since that obsoletes the
930 // function stuff
931 if (savestate) {
932 if (opt.nonnull()) {
933 if (grp->me() == 0) {
934 ckptfile = new char[strlen(molname)+6];
935 sprintf(ckptfile,"%s.ckpt",molname);
936 }
937 else {
938 ckptfile = new char[strlen(devnull)+1];
939 strcpy(ckptfile, devnull);
940 }
941
942 StateOutBin so(ckptfile);
943 SavableState::save_state(opt.pointer(),so);
944 so.close();
945
946 delete[] ckptfile;
947 }
948
949 if (mole.nonnull()) {
950 if (grp->me() == 0) {
951 if (wfn_file == 0) {
952 wfn_file = new char[strlen(molname)+6];
953 sprintf(wfn_file,"%s.wfn",molname);
954 }
955 }
956 else {
957 delete[] wfn_file;
958 wfn_file = new char[strlen(devnull)+1];
959 strcpy(wfn_file, devnull);
960 }
961
962 StateOutBin so(wfn_file);
963 SavableState::save_state(mole.pointer(),so);
964 so.close();
965
966 }
967 }
968 delete[] wfn_file;
969
970 // Frequency calculation.
971 if (ready_for_freq && molfreq.nonnull()) {
972 RefSymmSCMatrix xhessian;
973 if (molhess.nonnull()) {
974 // if "hess" input was given, use it to compute the hessian
975 xhessian = molhess->cartesian_hessian();
976 }
977 else if (mole->hessian_implemented()) {
978 // if mole can compute the hessian, use that hessian
979 xhessian = mole->get_cartesian_hessian();
980 }
981 else if (mole->gradient_implemented()) {
982 // if mole can compute gradients, use gradients at finite
983 // displacements to compute the hessian
984 molhess = new FinDispMolecularHessian(mole);
985 xhessian = molhess->cartesian_hessian();
986 }
987 else {
988 ExEnv::out0() << "mpqc: WARNING: Frequencies cannot be computed" << endl;
989 }
990
991 if (xhessian.nonnull()) {
992 char *hessfile = SCFormIO::fileext_to_filename(".hess");
993 MolecularHessian::write_cartesian_hessian(hessfile,
994 mole->molecule(), xhessian);
995 delete[] hessfile;
996
997 molfreq->compute_frequencies(xhessian);
998 // DEGENERACY IS NOT CORRECT FOR NON-SINGLET CASES:
999 molfreq->thermochemistry(1);
1000 }
1001 }
1002
1003 if (renderer.nonnull()) {
1004 Ref<RenderedObject> rendered;
1005 rendered << keyval->describedclassvalue("rendered");
1006 Ref<AnimatedObject> animated;
1007 animated << keyval->describedclassvalue("rendered");
1008 if (rendered.nonnull()) {
1009 if (tim.nonnull()) tim->enter("render");
1010 if (grp->me() == 0) renderer->render(rendered);
1011 if (tim.nonnull()) tim->exit("render");
1012 }
1013 else if (animated.nonnull()) {
1014 if (tim.nonnull()) tim->enter("render");
1015 if (grp->me() == 0) renderer->animate(animated);
1016 if (tim.nonnull()) tim->exit("render");
1017 }
1018 else {
1019 if (tim.nonnull()) tim->enter("render");
1020 int n = keyval->count("rendered");
1021 for (i=0; i<n; i++) {
1022 rendered << keyval->describedclassvalue("rendered",i);
1023 animated << keyval->describedclassvalue("rendered",i);
1024 if (rendered.nonnull()) {
1025 // make sure the object has a name so we don't overwrite its file
1026 if (rendered->name() == 0) {
1027 char ic[64];
1028 sprintf(ic,"%02d",i);
1029 rendered->set_name(ic);
1030 }
1031 if (grp->me() == 0) renderer->render(rendered);
1032 }
1033 else if (animated.nonnull()) {
1034 // make sure the object has a name so we don't overwrite its file
1035 if (animated->name() == 0) {
1036 char ic[64];
1037 sprintf(ic,"%02d",i);
1038 animated->set_name(ic);
1039 }
1040 if (grp->me() == 0) renderer->animate(animated);
1041 }
1042 }
1043 if (tim.nonnull()) tim->exit("render");
1044 }
1045 Ref<MolFreqAnimate> molfreqanim;
1046 molfreqanim << keyval->describedclassvalue("animate_modes");
1047 if (ready_for_freq && molfreq.nonnull()
1048 && molfreqanim.nonnull()) {
1049 if (tim.nonnull()) tim->enter("render");
1050 molfreq->animate(renderer, molfreqanim);
1051 if (tim.nonnull()) tim->exit("render");
1052 }
1053 }
1054
1055 if (mole.nonnull()) {
1056 if (print_mole)
1057 mole->print(ExEnv::out0());
1058
1059 if (do_pdb && grp->me() == 0) {
1060 ckptfile = new char[strlen(molname)+5];
1061 sprintf(ckptfile, "%s.pdb", molname);
1062 ofstream pdbfile(ckptfile);
1063 mole->molecule()->print_pdb(pdbfile);
1064 delete[] ckptfile;
1065 }
1066
1067 }
1068 else {
1069 ExEnv::out0() << "mpqc: The molecular energy object is null" << endl
1070 << " make sure \"mole\" specifies a MolecularEnergy derivative"
1071 << endl;
1072 }
1073 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
1074
1075 if (print_timings)
1076 if (tim.nonnull()) tim->print(ExEnv::out0());
1077
1078 // here, we may gather the results
1079 // we start to construct the MPQC_Data object
1080 {
1081 Ref<Wavefunction> wfn;
1082 wfn << mole;
1083 ExEnv::out0() << "The number of atomic orbitals: " << wfn->ao_dimension()->n() << endl;
1084 ExEnv::out0() << "The AO density matrix is ";
1085 wfn->ao_density()->print(ExEnv::out0());
1086 ExEnv::out0() << "The natural density matrix is ";
1087 wfn->natural_density()->print(ExEnv::out0());
1088 ExEnv::out0() << "The Gaussian basis is " << wfn->basis()->name() << endl;
1089 ExEnv::out0() << "The Gaussians sit at the following centers: " << endl;
1090 for (int nr = 0; nr< wfn->basis()->ncenter(); ++nr) {
1091 ExEnv::out0() << nr << " basis function has its center at ";
1092 for (int i=0; i < 3; ++i)
1093 ExEnv::out0() << wfn->basis()->r(nr,i) << "\t";
1094 ExEnv::out0() << endl;
1095 }
1096 // GaussianShell is the actual orbital functions it seems ...
1097 //ExEnv::out0() << "There are the following Gaussian Shells: " << endl;
1098 SCVector3 r;
1099 r.x() = r.y() = r.z() = 10;
1100 ExEnv::out0() << "We get the following value at " << r << "." << endl;
1101 Ref<Integral> intgrl = Integral::get_default_integral();
1102 GaussianBasisSet::ValueData vdat(wfn->basis(), integral);
1103 ExEnv::out0() << "Value at (10,10,10) is " << EvaluateDensity(r, intgrl, vdat, wfn) << endl;
1104 boost::function<double (SCVector3 &r)> evaluator =
1105 boost::bind(&EvaluateDensity, _1, boost::ref(intgrl), boost::ref(vdat), boost::ref(wfn));
1106 ExEnv::out0() << "Check against values at " << r << "." << endl;
1107 int nbasis = wfn->basis()->nbasis();
1108 double *b_val = new double[nbasis];
1109 wfn->basis()->values(r, &vdat, b_val);
1110 for (int i=0; i<nbasis; i++) {
1111 //ExEnv::out0() << "Shell nr. " << nr << ": ";
1112 ExEnv::out0() << "Value at (10,10,10) is " << b_val[i] << endl;
1113 }
1114 // perform test integration of density
1115 double delta = 1.;
1116 double sum = 0.;
1117 for (r.x() = -10. ; r.x() < 10.; r.x() += delta)
1118 for (r.y() = -10. ; r.y() < 10.; r.y() += delta)
1119 for (r.z() = -10. ; r.z() < 10.; r.z() += delta) {
1120 wfn->basis()->values(r, &vdat, b_val);
1121 for (int i=0; i<nbasis; i++)
1122 sum += wfn->ao_density()->get_element(i,i)*b_val[i];
1123 }
1124 sum /= pow(20/delta,3);
1125 ExEnv::out0() << "Sum over domain [0:20]^3 with " << delta << " delta is " << sum << "." << endl;
1126 delete[] b_val;
1127 }
1128
1129 delete[] molname;
1130 SCFormIO::set_default_basename(0);
1131
1132 renderer = 0;
1133 molfreq = 0;
1134 molhess = 0;
1135 opt = 0;
1136 mole = 0;
1137 integral = 0;
1138 debugger = 0;
1139 thread = 0;
1140 tim = 0;
1141 keyval = 0;
1142 parsedkv = 0;
1143 grp = 0;
1144 memory = 0;
1145 clean_up();
1146
1147#if defined(HAVE_TIME) && defined(HAVE_CTIME)
1148 time(&t);
1149 tstr = ctime(&t);
1150#endif
1151 if (!tstr) {
1152 tstr = "UNKNOWN";
1153 }
1154 ExEnv::out0() << endl
1155 << indent << scprintf("End Time: %s", tstr) << endl;
1156
1157 if (output != 0) {
1158 ExEnv::set_out(&cout);
1159 delete outstream;
1160 }
1161
1162 return 0;
1163}
1164
1165double EvaluateDensity(SCVector3 &r, Ref<Integral> &intgrl, GaussianBasisSet::ValueData &vdat, Ref<Wavefunction> &wfn)
1166{
1167 ExEnv::out0() << "We get the following values at " << r << "." << endl;
1168 int nbasis = wfn->basis()->nbasis();
1169 double *b_val = new double[nbasis];
1170 wfn->basis()->values(r, &vdat, b_val);
1171 double sum=0.;
1172 for (int i=0; i<nbasis; i++)
1173 sum += b_val[i];
1174 delete[] b_val;
1175 return sum;
1176}
1177
1178int
1179main(int argc, char *argv[])
1180{
1181 try {
1182 try_main(argc, argv);
1183 }
1184 catch (SCException &e) {
1185 cout << argv[0] << ": ERROR: SC EXCEPTION RAISED:" << endl
1186 << e.what()
1187 << endl;
1188 clean_up();
1189 throw;
1190 }
1191 catch (bad_alloc &e) {
1192 cout << argv[0] << ": ERROR: MEMORY ALLOCATION FAILED:" << endl
1193 << e.what()
1194 << endl;
1195 clean_up();
1196 throw;
1197 }
1198 catch (exception &e) {
1199 cout << argv[0] << ": ERROR: EXCEPTION RAISED:" << endl
1200 << e.what()
1201 << endl;
1202 clean_up();
1203 throw;
1204 }
1205 catch (...) {
1206 cout << argv[0] << ": ERROR: UNKNOWN EXCEPTION RAISED" << endl;
1207 clean_up();
1208 throw;
1209 }
1210 return 0;
1211}
1212
1213/////////////////////////////////////////////////////////////////////////////
1214
1215// Local Variables:
1216// mode: c++
1217// c-file-style: "ETS"
1218// End:
Note: See TracBrowser for help on using the repository browser.