source: src/bin/mpqc/mpqc.cc@ 2fbba84

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

tempcommit: getInputFileNames() needs optind.

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