source: src/bin/mpqc/mpqc.cc@ 9b827f

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

Extracted setting up of molecular checkpoint file into own function.

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