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

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

Extracted function saveState().

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