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

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

Extracted getting of MPI Message group into own function.

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