source: src/bin/mpqc/mpqc.cc@ 203fe4

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

Extracted setting of output base name into own function.

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