source: src/bin/mpqc/mpqc.cc@ 09bc09

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

Extracted getting integral factory into own function.

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