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

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

Added as temporary output all that is needed to get density on a grid.

  • with this we are complete, the rest is only correct multiplication and how to get a value at a specific site r.
  • Property mode set to 100644
File size: 31.2 KB
Line 
1//
2// mpqc.cc
3//
4// Copyright (C) 1996 Limit Point Systems, Inc.
5//
6// Author: Edward Seidl <seidl@janed.com>
7// Maintainer: LPS
8//
9// This file is part of MPQC.
10//
11// MPQC is free software; you can redistribute it and/or modify
12// it under the terms of the GNU General Public License as published by
13// the Free Software Foundation; either version 2, or (at your option)
14// any later version.
15//
16// MPQC is distributed in the hope that it will be useful,
17// but WITHOUT ANY WARRANTY; without even the implied warranty of
18// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19// GNU General Public License for more details.
20//
21// You should have received a copy of the GNU General Public License
22// along with the MPQC; see the file COPYING. If not, write to
23// the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24//
25// The U.S. Government is granted a limited license as per AL 91-7.
26//
27
28// This is needed to make GNU extensions available, such as
29// feenableexcept and fedisableexcept.
30#ifndef _GNU_SOURCE
31# define _GNU_SOURCE
32#endif
33
34#ifdef HAVE_CONFIG_H
35#include <scconfig.h>
36#endif
37
38#ifdef HAVE_TIME_H
39#include <time.h>
40#endif
41
42#include <scdirlist.h>
43
44#include <new>
45#include <stdexcept>
46#include <string.h>
47#include <unistd.h>
48#include <sys/stat.h>
49#include <fstream>
50
51#include <scconfig.h>
52#ifdef HAVE_SSTREAM
53# include <sstream>
54#else
55# include <strstream.h>
56#endif
57
58#ifdef HAVE_SYS_RESOURCE_H
59# include <sys/resource.h>
60#endif
61#ifdef HAVE_SYS_TIME_H
62# include <sys/time.h>
63#endif
64
65#include <util/options/GetLongOpt.h>
66#include <util/class/scexception.h>
67#include <util/misc/newstring.h>
68#include <util/keyval/keyval.h>
69#include <util/state/state_bin.h>
70#include <util/group/message.h>
71#include <util/group/memory.h>
72#include <util/group/mstate.h>
73#include <util/group/thread.h>
74#include <util/group/pregtime.h>
75#include <util/misc/bug.h>
76#include <util/misc/formio.h>
77#include <util/misc/exenv.h>
78#ifdef HAVE_CHEMISTRY_CCA
79 #include <util/misc/ccaenv.h>
80#endif
81#include <util/render/render.h>
82
83#include <math/optimize/opt.h>
84
85#include <chemistry/molecule/coor.h>
86#include <chemistry/molecule/energy.h>
87#include <chemistry/molecule/molfreq.h>
88#include <chemistry/molecule/fdhess.h>
89#include <chemistry/molecule/formula.h>
90#include <chemistry/qc/wfn/wfn.h>
91
92// Force linkages:
93#include <util/group/linkage.h>
94#include <chemistry/qc/wfn/linkage.h>
95#include <chemistry/qc/scf/linkage.h>
96#include <chemistry/qc/dft/linkage.h>
97#include <chemistry/qc/mbpt/linkage.h>
98#ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_MBPTR12
99# include <chemistry/qc/mbptr12/linkage.h>
100#endif
101#ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_CINTS
102# include <chemistry/qc/cints/linkage.h>
103#endif
104//#include <chemistry/qc/psi/linkage.h>
105#include <util/state/linkage.h>
106#ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_CC
107# include <chemistry/qc/cc/linkage.h>
108#endif
109#ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_PSI
110# include <chemistry/qc/psi/linkage.h>
111#endif
112#ifdef HAVE_SC_SRC_LIB_CHEMISTRY_QC_INTCCA
113# include <chemistry/qc/intcca/linkage.h>
114#endif
115
116#ifdef HAVE_MPI
117#define MPICH_SKIP_MPICXX
118#include <mpi.h>
119#include <util/group/messmpi.h>
120#endif
121
122using namespace std;
123using namespace sc;
124
125#include "mpqcin.h"
126
127//////////////////////////////////////////////////////////////////////////
128
129static void
130trash_stack_b(int &i, char *&ichar)
131{
132 char stack;
133 ichar = &stack;
134 ichar -= 10;
135 for (i=0; i<1000; i++) {
136 *ichar-- = 0xfe;
137 }
138}
139
140static void
141trash_stack()
142{
143 int i;
144 char *ichar;
145 trash_stack_b(i,ichar);
146}
147
148static void
149clean_up(void)
150{
151 MemoryGrp::set_default_memorygrp(0);
152 MessageGrp::set_default_messagegrp(0);
153 ThreadGrp::set_default_threadgrp(0);
154 SCMatrixKit::set_default_matrixkit(0);
155 Integral::set_default_integral(0);
156 RegionTimer::set_default_regiontimer(0);
157}
158
159#include <signal.h>
160
161#ifdef HAVE_FENV_H
162# include <fenv.h>
163#endif
164
165static void
166print_unseen(const Ref<ParsedKeyVal> &parsedkv,
167 const char *input)
168{
169 if (parsedkv->have_unseen()) {
170 ExEnv::out0() << endl;
171 ExEnv::out0() << indent
172 << "The following keywords in \"" << input << "\" were ignored:"
173 << endl;
174 ExEnv::out0() << incindent;
175 parsedkv->print_unseen(ExEnv::out0());
176 ExEnv::out0() << decindent;
177 }
178}
179
180int
181try_main(int argc, char *argv[])
182{
183 //trash_stack();
184
185 KeyValValueboolean truevalue(1), falsevalue(0);
186 int i;
187 const char *devnull = "/dev/null";
188 atexit(clean_up);
189
190#ifdef HAVE_FEENABLEEXCEPT
191 // this uses a glibc extension to trap on individual exceptions
192# ifdef FE_DIVBYZERO
193 feenableexcept(FE_DIVBYZERO);
194# endif
195# ifdef FE_INVALID
196 feenableexcept(FE_INVALID);
197# endif
198# ifdef FE_OVERFLOW
199 feenableexcept(FE_OVERFLOW);
200# endif
201#endif
202
203#ifdef HAVE_FEDISABLEEXCEPT
204 // this uses a glibc extension to not trap on individual exceptions
205# ifdef FE_UNDERFLOW
206 fedisableexcept(FE_UNDERFLOW);
207# endif
208# ifdef FE_INEXACT
209 fedisableexcept(FE_INEXACT);
210# endif
211#endif
212
213#if defined(HAVE_SETRLIMIT)
214 struct rlimit rlim;
215 rlim.rlim_cur = 0;
216 rlim.rlim_max = 0;
217 setrlimit(RLIMIT_CORE,&rlim);
218#endif
219
220 ExEnv::init(argc, argv);
221
222 Ref<MessageGrp> grp;
223#if defined(HAVE_MPI) && defined(ALWAYS_USE_MPI)
224 grp = new MPIMessageGrp(&argc, &argv);
225#endif
226
227 // parse commandline options
228 GetLongOpt options;
229
230 options.usage("[options] [filename]");
231 options.enroll("f", GetLongOpt::MandatoryValue,
232 "the name of an object format input file", 0);
233 options.enroll("o", GetLongOpt::MandatoryValue,
234 "the name of the output file", 0);
235 options.enroll("n", GetLongOpt::NoValue,
236 "listen for incoming object format input files", 0);
237 options.enroll("messagegrp", GetLongOpt::MandatoryValue,
238 "which message group to use", 0);
239 options.enroll("threadgrp", GetLongOpt::MandatoryValue,
240 "which thread group to use", 0);
241 options.enroll("memorygrp", GetLongOpt::MandatoryValue,
242 "which memory group to use", 0);
243 options.enroll("integral", GetLongOpt::MandatoryValue,
244 "which integral evaluator to use", 0);
245 options.enroll("l", GetLongOpt::MandatoryValue, "basis set limit", "0");
246 options.enroll("W", GetLongOpt::MandatoryValue,
247 "set the working directory", ".");
248 options.enroll("c", GetLongOpt::NoValue, "check input then exit", 0);
249 options.enroll("v", GetLongOpt::NoValue, "print the version number", 0);
250 options.enroll("w", GetLongOpt::NoValue, "print the warranty", 0);
251 options.enroll("L", GetLongOpt::NoValue, "print the license", 0);
252 options.enroll("k", GetLongOpt::NoValue, "print key/value assignments", 0);
253 options.enroll("i", GetLongOpt::NoValue, "convert simple to OO input", 0);
254 options.enroll("d", GetLongOpt::NoValue, "debug", 0);
255 options.enroll("h", GetLongOpt::NoValue, "print this message", 0);
256 options.enroll("cca-path", GetLongOpt::OptionalValue,
257 "cca component path", "");
258 options.enroll("cca-load", GetLongOpt::OptionalValue,
259 "cca components to load", "");
260
261 int optind = options.parse(argc, argv);
262
263 const char *output = options.retrieve("o");
264 ostream *outstream = 0;
265 if (output != 0) {
266 outstream = new ofstream(output);
267 ExEnv::set_out(outstream);
268 }
269
270 if (options.retrieve("h")) {
271 ExEnv::out0()
272 << indent << "MPQC version " << SC_VERSION << endl
273 << indent << "compiled for " << TARGET_ARCH << endl
274 << SCFormIO::copyright << endl;
275 options.usage(ExEnv::out0());
276 exit(0);
277 }
278
279 if (options.retrieve("v")) {
280 ExEnv::out0()
281 << indent << "MPQC version " << SC_VERSION << endl
282 << indent << "compiled for " << TARGET_ARCH << endl
283 << SCFormIO::copyright;
284 exit(0);
285 }
286
287 if (options.retrieve("w")) {
288 ExEnv::out0()
289 << indent << "MPQC version " << SC_VERSION << endl
290 << indent << "compiled for " << TARGET_ARCH << endl
291 << SCFormIO::copyright << endl
292 << SCFormIO::warranty;
293 exit(0);
294 }
295
296 if (options.retrieve("L")) {
297 ExEnv::out0()
298 << indent << "MPQC version " << SC_VERSION << endl
299 << indent << "compiled for " << TARGET_ARCH << endl
300 << SCFormIO::copyright << endl
301 << SCFormIO::license;
302 exit(0);
303 }
304
305 // set the working dir
306 if (strcmp(options.retrieve("W"),"."))
307 chdir(options.retrieve("W"));
308
309 // check that n and f/o are not given at the same time
310 if ((options.retrieve("n")) && ((options.retrieve("f")) || (options.retrieve("o")))) {
311 throw invalid_argument("-n must not be given with -f or -o");
312 }
313
314 // initialize keyval input
315 const char *object_input = options.retrieve("f");
316 const char *generic_input;
317 if (argc - optind == 0) {
318 generic_input = 0;
319 }
320 else if (argc - optind == 1) {
321 generic_input = argv[optind];
322 }
323 else {
324 options.usage();
325 throw invalid_argument("extra arguments given");
326 }
327
328 // get the message group. first try the commandline and environment
329 if (grp.null()) grp = MessageGrp::initial_messagegrp(argc, argv);
330 if (grp.nonnull())
331 MessageGrp::set_default_messagegrp(grp);
332 else
333 grp = MessageGrp::get_default_messagegrp();
334
335 if (object_input == 0 && generic_input == 0) {
336 generic_input = "mpqc.in";
337 }
338 else if (object_input && generic_input) {
339 options.usage();
340 throw invalid_argument("only one of -f and a file argument can be given");
341 }
342
343 const char *input;
344 if (object_input) input = object_input;
345 if (generic_input) input = generic_input;
346
347
348 Ref<ParsedKeyVal> parsedkv;
349 // read the input file on only node 0
350 char *in_char_array;
351 if (grp->me() == 0) {
352 ifstream is(input);
353#ifdef HAVE_SSTREAM
354 ostringstream ostrs;
355 is >> ostrs.rdbuf();
356 int n = 1 + strlen(ostrs.str().c_str());
357 in_char_array = strcpy(new char[n],ostrs.str().c_str());
358#else
359 ostrstream ostrs;
360 is >> ostrs.rdbuf();
361 ostrs << ends;
362 in_char_array = ostrs.str();
363 int n = ostrs.pcount();
364#endif
365 grp->bcast(n);
366 grp->bcast(in_char_array, n);
367 }
368 else {
369 int n;
370 grp->bcast(n);
371 in_char_array = new char[n];
372 grp->bcast(in_char_array, n);
373 }
374
375 int use_simple_input;
376 if (generic_input && grp->me() == 0) {
377 MPQCIn mpqcin;
378 use_simple_input = mpqcin.check_string(in_char_array);
379 }
380 else {
381 use_simple_input = 0;
382 }
383 grp->bcast(use_simple_input);
384
385 if (use_simple_input) {
386 MPQCIn mpqcin;
387 char *simple_input_text = mpqcin.parse_string(in_char_array);
388 if (options.retrieve("i")) {
389 ExEnv::out0() << "Generated object-oriented input file:" << endl
390 << simple_input_text
391 << endl;
392 exit(0);
393 }
394 parsedkv = new ParsedKeyVal();
395 parsedkv->parse_string(simple_input_text);
396 delete[] simple_input_text;
397 }
398 else {
399 parsedkv = new ParsedKeyVal();
400 parsedkv->parse_string(in_char_array);
401 }
402 delete[] in_char_array;
403
404 if (options.retrieve("k")) parsedkv->verbose(1);
405 Ref<KeyVal> keyval = new PrefixKeyVal(parsedkv.pointer(),"mpqc");
406
407 // get the basename for output files
408 const char *basename_source;
409 if (output) basename_source = output;
410 else basename_source = input;
411 int nfilebase = (int) (::strrchr(basename_source, '.') - basename_source);
412 char *basename = new char[nfilebase + 1];
413 strncpy(basename, basename_source, nfilebase);
414 basename[nfilebase] = '\0';
415 SCFormIO::set_default_basename(basename);
416
417 // set up output classes
418 SCFormIO::setindent(ExEnv::outn(), 2);
419 SCFormIO::setindent(ExEnv::errn(), 2);
420 SCFormIO::setindent(cout, 2);
421 SCFormIO::setindent(cerr, 2);
422
423 SCFormIO::set_printnode(0);
424 if (grp->n() > 1)
425 SCFormIO::init_mp(grp->me());
426
427 if (options.retrieve("d"))
428 SCFormIO::set_debug(1);
429
430 // initialize timing for mpqc
431 grp->sync(); // make sure nodes are sync'ed before starting timings
432 Ref<RegionTimer> tim;
433 if (keyval->exists("timer")) tim << keyval->describedclassvalue("timer");
434 else tim = new ParallelRegionTimer(grp,"mpqc",1,1);
435 RegionTimer::set_default_regiontimer(tim);
436
437 if (tim.nonnull()) tim->enter("input");
438
439 // announce ourselves
440 const char title1[] = "MPQC: Massively Parallel Quantum Chemistry";
441 int ntitle1 = sizeof(title1);
442 const char title2[] = "Version " SC_VERSION;
443 int ntitle2 = sizeof(title2);
444 ExEnv::out0() << endl;
445 ExEnv::out0() << indent;
446 for (i=0; i<(80-ntitle1)/2; i++) ExEnv::out0() << ' ';
447 ExEnv::out0() << title1 << endl;
448 ExEnv::out0() << indent;
449 for (i=0; i<(80-ntitle2)/2; i++) ExEnv::out0() << ' ';
450 ExEnv::out0() << title2 << endl << endl;
451
452 const char *tstr = 0;
453#if defined(HAVE_TIME) && defined(HAVE_CTIME)
454 time_t t;
455 time(&t);
456 tstr = ctime(&t);
457#endif
458 if (!tstr) {
459 tstr = "UNKNOWN";
460 }
461
462 ExEnv::out0()
463 << indent << scprintf("Machine: %s", TARGET_ARCH) << endl
464 << indent << scprintf("User: %s@%s",
465 ExEnv::username(), ExEnv::hostname()) << endl
466 << indent << scprintf("Start Time: %s", tstr) << endl;
467
468 // get the thread group. first try the commandline and environment
469 Ref<ThreadGrp> thread = ThreadGrp::initial_threadgrp(argc, argv);
470
471 // if we still don't have a group, try reading the thread group
472 // from the input
473 if (thread.null()) {
474 thread << keyval->describedclassvalue("thread");
475 }
476
477 if (thread.nonnull())
478 ThreadGrp::set_default_threadgrp(thread);
479 else
480 thread = ThreadGrp::get_default_threadgrp();
481
482 // get the memory group. first try the commandline and environment
483 Ref<MemoryGrp> memory = MemoryGrp::initial_memorygrp(argc, argv);
484
485 // if we still don't have a group, try reading the memory group
486 // from the input
487 if (memory.null()) {
488 memory << keyval->describedclassvalue("memory");
489 }
490
491 if (memory.nonnull())
492 MemoryGrp::set_default_memorygrp(memory);
493 else
494 memory = MemoryGrp::get_default_memorygrp();
495
496 ExEnv::out0() << indent
497 << "Using " << grp->class_name()
498 << " for message passing (number of nodes = " << grp->n() << ")." << endl
499 << indent
500 << "Using " << thread->class_name()
501 << " for threading (number of threads = " << thread->nthread() << ")." << endl
502 << indent
503 << "Using " << memory->class_name()
504 << " for distributed shared memory." << endl
505 << indent
506 << "Total number of processors = " << grp->n() * thread->nthread() << endl;
507
508#ifdef HAVE_CHEMISTRY_CCA
509 // initialize cca framework
510 KeyValValuestring emptystring("");
511 bool do_cca = keyval->booleanvalue("do_cca",falsevalue);
512
513 string cca_path(options.retrieve("cca-path"));
514 string cca_load(options.retrieve("cca-load"));
515 if(cca_path.size()==0)
516 cca_path = keyval->stringvalue("cca_path",emptystring);
517 if(cca_load.size()==0)
518 cca_load = keyval->stringvalue("cca_load",emptystring);
519
520 if( !do_cca && (cca_load.size() > 0 || cca_path.size() > 0) )
521 do_cca = true;
522
523 if(cca_path.size()==0) {
524 #ifdef CCA_PATH
525 cca_path = CCA_PATH;
526 #endif
527 }
528 if(cca_load.size()==0) {
529 cca_load += "MPQC.IntegralEvaluatorFactory";
530 }
531
532 if( cca_load.size() > 0 && cca_path.size() > 0 && do_cca ) {
533 string cca_args = "--path " + cca_path + " --load " + cca_load;
534 ExEnv::out0() << endl << indent << "Initializing CCA framework with args: "
535 << endl << indent << cca_args << endl;
536 CCAEnv::init( cca_args );
537 }
538#endif
539
540 // now set up the debugger
541 Ref<Debugger> debugger; debugger << keyval->describedclassvalue("debug");
542 if (debugger.nonnull()) {
543 Debugger::set_default_debugger(debugger);
544 debugger->set_exec(argv[0]);
545 debugger->set_prefix(grp->me());
546 if (options.retrieve("d"))
547 debugger->debug("Starting debugger because -d given on command line.");
548 }
549
550 // now check to see what matrix kit to use
551 if (keyval->exists("matrixkit"))
552 SCMatrixKit::set_default_matrixkit(
553 dynamic_cast<SCMatrixKit*>(
554 keyval->describedclassvalue("matrixkit").pointer()));
555
556 // get the integral factory. first try commandline and environment
557 Ref<Integral> integral = Integral::initial_integral(argc, argv);
558
559 // if we still don't have a integral, try reading the integral
560 // from the input
561 if (integral.null()) {
562 integral << keyval->describedclassvalue("integrals");
563 }
564
565 if (integral.nonnull())
566 Integral::set_default_integral(integral);
567 else
568 integral = Integral::get_default_integral();
569
570 ExEnv::out0() << endl << indent
571 << "Using " << integral->class_name()
572 << " by default for molecular integrals evaluation" << endl << endl;
573
574 // check for a molecular energy and optimizer
575 KeyValValueString molnamedef(basename);
576 char * molname = keyval->pcharvalue("filename", molnamedef);
577 if (strcmp(molname, basename))
578 SCFormIO::set_default_basename(molname);
579
580 char * ckptfile = new char[strlen(molname)+6];
581 sprintf(ckptfile,"%s.ckpt",molname);
582
583 KeyValValueString restartfiledef(ckptfile);
584 char * restartfile = keyval->pcharvalue("restart_file", restartfiledef);
585
586 char * wfn_file = keyval->pcharvalue("wfn_file");
587 if (wfn_file == 0) {
588 wfn_file = new char[strlen(molname)+6];
589 sprintf(wfn_file,"%s.wfn",molname);
590 }
591 char *mole_ckpt_file = new char[strlen(wfn_file)+1];
592 sprintf(mole_ckpt_file,"%s",wfn_file);
593
594 int restart = keyval->booleanvalue("restart",truevalue);
595
596 int checkpoint = keyval->booleanvalue("checkpoint",truevalue);
597 int checkpoint_freq = keyval->intvalue("checkpoint_freq",KeyValValueint(1));
598
599 int savestate = keyval->booleanvalue("savestate",truevalue);
600
601 struct stat sb;
602 Ref<MolecularEnergy> mole;
603 Ref<Optimize> opt;
604
605 int statresult, statsize;
606 if (restart) {
607 if (grp->me() == 0) {
608 statresult = stat(restartfile,&sb);
609 statsize = (statresult==0) ? sb.st_size : 0;
610 }
611 grp->bcast(statresult);
612 grp->bcast(statsize);
613 }
614 if (restart && statresult==0 && statsize) {
615 BcastStateInBin si(grp,restartfile);
616 if (keyval->exists("override")) {
617 si.set_override(new PrefixKeyVal(keyval,"override"));
618 }
619 char *suf = strrchr(restartfile,'.');
620 if (!strcmp(suf,".wfn")) {
621 mole << SavableState::key_restore_state(si,"mole");
622 ExEnv::out0() << endl
623 << indent << "Restored <" << mole->class_name()
624 << "> from " << restartfile << endl;
625
626 opt << keyval->describedclassvalue("opt");
627 if (opt.nonnull())
628 opt->set_function(mole.pointer());
629 }
630 else {
631 opt << SavableState::key_restore_state(si,"opt");
632 if (opt.nonnull()) {
633 mole << opt->function();
634 ExEnv::out0() << endl << indent
635 << "Restored <Optimize> from " << restartfile << endl;
636 }
637 }
638 } else {
639 mole << keyval->describedclassvalue("mole");
640 opt << keyval->describedclassvalue("opt");
641 }
642
643 if (mole.nonnull()) {
644 MolecularFormula mf(mole->molecule());
645 ExEnv::out0() << endl << indent
646 << "Molecular formula " << mf.formula() << endl;
647 if (checkpoint) {
648 mole->set_checkpoint();
649 if (grp->me() == 0) mole->set_checkpoint_file(mole_ckpt_file);
650 else mole->set_checkpoint_file(devnull);
651 mole->set_checkpoint_freq(checkpoint_freq);
652 }
653 }
654 delete[] mole_ckpt_file;
655
656 if (checkpoint && opt.nonnull()) {
657 opt->set_checkpoint();
658 if (grp->me() == 0) opt->set_checkpoint_file(ckptfile);
659 else opt->set_checkpoint_file(devnull);
660 }
661
662 // see if frequencies are wanted
663
664 Ref<MolecularHessian> molhess;
665 molhess << keyval->describedclassvalue("hess");
666 Ref<MolecularFrequencies> molfreq;
667 molfreq << keyval->describedclassvalue("freq");
668
669 int check = (options.retrieve("c") != 0);
670 int limit = atoi(options.retrieve("l"));
671 if (limit) {
672 Ref<Wavefunction> wfn; wfn << mole;
673 if (wfn.nonnull() && wfn->ao_dimension()->n() > limit) {
674 ExEnv::out0() << endl << indent
675 << "The limit of " << limit << " basis functions has been exceeded."
676 << endl;
677 check = 1;
678 }
679 }
680
681 if (check) {
682 ExEnv::out0() << endl << indent
683 << "Exiting since the check option is on." << endl;
684 exit(0);
685 }
686
687 if (tim.nonnull()) tim->change("calc");
688
689 int do_energy = keyval->booleanvalue("do_energy",truevalue);
690
691 int do_grad = keyval->booleanvalue("do_gradient",falsevalue);
692
693 int do_opt = keyval->booleanvalue("optimize",truevalue);
694
695 int do_pdb = keyval->booleanvalue("write_pdb",falsevalue);
696
697 int print_mole = keyval->booleanvalue("print_mole",truevalue);
698
699 int print_timings = keyval->booleanvalue("print_timings",truevalue);
700
701 // see if any pictures are desired
702 Ref<Render> renderer;
703 renderer << keyval->describedclassvalue("renderer");
704
705 // If we have a renderer, then we will read in some more info
706 // below. Otherwise we can get rid of the keyval's, to eliminate
707 // superfluous references to objects that we might otherwise be
708 // able to delete. We cannot read in the remaining rendering
709 // objects now, since some of their KeyVal CTOR's are heavyweight,
710 // requiring optimized geometries, etc.
711 if (renderer.null()) {
712 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
713 keyval = 0;
714 parsedkv = 0;
715 }
716
717 // sanity checks for the benefit of reasonable looking output
718 if (opt.null()) do_opt=0;
719
720 ExEnv::out0() << endl << indent
721 << "MPQC options:" << endl << incindent
722 << indent << "matrixkit = <"
723 << SCMatrixKit::default_matrixkit()->class_name() << ">" << endl
724 << indent << "filename = " << molname << endl
725 << indent << "restart_file = " << restartfile << endl
726 << indent << "restart = " << (restart ? "yes" : "no") << endl
727 << indent << "checkpoint = " << (checkpoint ? "yes" : "no") << endl
728 << indent << "savestate = " << (savestate ? "yes" : "no") << endl
729 << indent << "do_energy = " << (do_energy ? "yes" : "no") << endl
730 << indent << "do_gradient = " << (do_grad ? "yes" : "no") << endl
731 << indent << "optimize = " << (do_opt ? "yes" : "no") << endl
732 << indent << "write_pdb = " << (do_pdb ? "yes" : "no") << endl
733 << indent << "print_mole = " << (print_mole ? "yes" : "no") << endl
734 << indent << "print_timings = " << (print_timings ? "yes" : "no")
735 << endl << decindent;
736
737 delete[] restartfile;
738 delete[] ckptfile;
739
740 int ready_for_freq = 1;
741 if (mole.nonnull()) {
742 if (((do_opt && opt.nonnull()) || do_grad)
743 && !mole->gradient_implemented()) {
744 ExEnv::out0() << indent
745 << "WARNING: optimization or gradient requested but the given"
746 << endl
747 << " MolecularEnergy object cannot do gradients."
748 << endl;
749 }
750
751 if (do_opt && opt.nonnull() && mole->gradient_implemented()) {
752 int result = opt->optimize();
753 if (result) {
754 ExEnv::out0() << indent
755 << "The optimization has converged." << endl << endl;
756 ExEnv::out0() << indent
757 << scprintf("Value of the MolecularEnergy: %15.10f",
758 mole->energy())
759 << endl << endl;
760 } else {
761 ExEnv::out0() << indent
762 << "The optimization has NOT converged." << endl << endl;
763 ready_for_freq = 0;
764 }
765 } else if (do_grad && mole->gradient_implemented()) {
766 mole->do_gradient(1);
767 ExEnv::out0() << endl << indent
768 << scprintf("Value of the MolecularEnergy: %15.10f",
769 mole->energy())
770 << endl;
771 if (mole->value_result().actual_accuracy()
772 > mole->value_result().desired_accuracy()) {
773 ExEnv::out0() << indent
774 << "WARNING: desired accuracy not achieved in energy" << endl;
775 }
776 ExEnv::out0() << endl;
777 // Use result_noupdate since the energy might not have converged
778 // to the desired accuracy in which case grabbing the result will
779 // start up the calculation again. However the gradient might
780 // not have been computed (if we are restarting and the gradient
781 // isn't in the save file for example).
782 RefSCVector grad;
783 if (mole->gradient_result().computed()) {
784 grad = mole->gradient_result().result_noupdate();
785 }
786 else {
787 grad = mole->gradient();
788 }
789 if (grad.nonnull()) {
790 grad.print("Gradient of the MolecularEnergy:");
791 if (mole->gradient_result().actual_accuracy()
792 > mole->gradient_result().desired_accuracy()) {
793 ExEnv::out0() << indent
794 << "WARNING: desired accuracy not achieved in gradient" << endl;
795 }
796 }
797 } else if (do_energy && mole->value_implemented()) {
798 ExEnv::out0() << endl << indent
799 << scprintf("Value of the MolecularEnergy: %15.10f",
800 mole->energy())
801 << endl << endl;
802 }
803 }
804
805 if (tim.nonnull()) tim->exit("calc");
806
807 // save this before doing the frequency stuff since that obsoletes the
808 // function stuff
809 if (savestate) {
810 if (opt.nonnull()) {
811 if (grp->me() == 0) {
812 ckptfile = new char[strlen(molname)+6];
813 sprintf(ckptfile,"%s.ckpt",molname);
814 }
815 else {
816 ckptfile = new char[strlen(devnull)+1];
817 strcpy(ckptfile, devnull);
818 }
819
820 StateOutBin so(ckptfile);
821 SavableState::save_state(opt.pointer(),so);
822 so.close();
823
824 delete[] ckptfile;
825 }
826
827 if (mole.nonnull()) {
828 if (grp->me() == 0) {
829 if (wfn_file == 0) {
830 wfn_file = new char[strlen(molname)+6];
831 sprintf(wfn_file,"%s.wfn",molname);
832 }
833 }
834 else {
835 delete[] wfn_file;
836 wfn_file = new char[strlen(devnull)+1];
837 strcpy(wfn_file, devnull);
838 }
839
840 StateOutBin so(wfn_file);
841 SavableState::save_state(mole.pointer(),so);
842 so.close();
843
844 }
845 }
846 delete[] wfn_file;
847
848 // Frequency calculation.
849 if (ready_for_freq && molfreq.nonnull()) {
850 RefSymmSCMatrix xhessian;
851 if (molhess.nonnull()) {
852 // if "hess" input was given, use it to compute the hessian
853 xhessian = molhess->cartesian_hessian();
854 }
855 else if (mole->hessian_implemented()) {
856 // if mole can compute the hessian, use that hessian
857 xhessian = mole->get_cartesian_hessian();
858 }
859 else if (mole->gradient_implemented()) {
860 // if mole can compute gradients, use gradients at finite
861 // displacements to compute the hessian
862 molhess = new FinDispMolecularHessian(mole);
863 xhessian = molhess->cartesian_hessian();
864 }
865 else {
866 ExEnv::out0() << "mpqc: WARNING: Frequencies cannot be computed" << endl;
867 }
868
869 if (xhessian.nonnull()) {
870 char *hessfile = SCFormIO::fileext_to_filename(".hess");
871 MolecularHessian::write_cartesian_hessian(hessfile,
872 mole->molecule(), xhessian);
873 delete[] hessfile;
874
875 molfreq->compute_frequencies(xhessian);
876 // DEGENERACY IS NOT CORRECT FOR NON-SINGLET CASES:
877 molfreq->thermochemistry(1);
878 }
879 }
880
881 if (renderer.nonnull()) {
882 Ref<RenderedObject> rendered;
883 rendered << keyval->describedclassvalue("rendered");
884 Ref<AnimatedObject> animated;
885 animated << keyval->describedclassvalue("rendered");
886 if (rendered.nonnull()) {
887 if (tim.nonnull()) tim->enter("render");
888 if (grp->me() == 0) renderer->render(rendered);
889 if (tim.nonnull()) tim->exit("render");
890 }
891 else if (animated.nonnull()) {
892 if (tim.nonnull()) tim->enter("render");
893 if (grp->me() == 0) renderer->animate(animated);
894 if (tim.nonnull()) tim->exit("render");
895 }
896 else {
897 if (tim.nonnull()) tim->enter("render");
898 int n = keyval->count("rendered");
899 for (i=0; i<n; i++) {
900 rendered << keyval->describedclassvalue("rendered",i);
901 animated << keyval->describedclassvalue("rendered",i);
902 if (rendered.nonnull()) {
903 // make sure the object has a name so we don't overwrite its file
904 if (rendered->name() == 0) {
905 char ic[64];
906 sprintf(ic,"%02d",i);
907 rendered->set_name(ic);
908 }
909 if (grp->me() == 0) renderer->render(rendered);
910 }
911 else if (animated.nonnull()) {
912 // make sure the object has a name so we don't overwrite its file
913 if (animated->name() == 0) {
914 char ic[64];
915 sprintf(ic,"%02d",i);
916 animated->set_name(ic);
917 }
918 if (grp->me() == 0) renderer->animate(animated);
919 }
920 }
921 if (tim.nonnull()) tim->exit("render");
922 }
923 Ref<MolFreqAnimate> molfreqanim;
924 molfreqanim << keyval->describedclassvalue("animate_modes");
925 if (ready_for_freq && molfreq.nonnull()
926 && molfreqanim.nonnull()) {
927 if (tim.nonnull()) tim->enter("render");
928 molfreq->animate(renderer, molfreqanim);
929 if (tim.nonnull()) tim->exit("render");
930 }
931 }
932
933 if (mole.nonnull()) {
934 if (print_mole)
935 mole->print(ExEnv::out0());
936
937 if (do_pdb && grp->me() == 0) {
938 ckptfile = new char[strlen(molname)+5];
939 sprintf(ckptfile, "%s.pdb", molname);
940 ofstream pdbfile(ckptfile);
941 mole->molecule()->print_pdb(pdbfile);
942 delete[] ckptfile;
943 }
944
945 }
946 else {
947 ExEnv::out0() << "mpqc: The molecular energy object is null" << endl
948 << " make sure \"mole\" specifies a MolecularEnergy derivative"
949 << endl;
950 }
951 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
952
953 if (print_timings)
954 if (tim.nonnull()) tim->print(ExEnv::out0());
955
956 // here, we may gather the results
957 // we start to construct the MPQC_Data object
958 {
959 Ref<Wavefunction> wfn;
960 wfn << mole;
961 ExEnv::out0() << "The number of atomic orbitals: " << wfn->ao_dimension()->n() << endl;
962 ExEnv::out0() << "The AO density matrix is ";
963 wfn->ao_density()->print(ExEnv::out0());
964 ExEnv::out0() << "The Gaussian basis is " << wfn->basis()->name() << endl;
965 ExEnv::out0() << "The Gaussians sit at the following centers: " << endl;
966 for (int nr = 0; nr< wfn->basis()->ncenter(); ++nr) {
967 ExEnv::out0() << nr << " basis function has its center at ";
968 for (int i=0; i < 3; ++i)
969 ExEnv::out0() << wfn->basis()->r(nr,i) << "\t";
970 ExEnv::out0() << endl;
971 }
972 // GaussianShell is the actual orbital functionas it seems ...
973 ExEnv::out0() << "There are the following Gaussian Shells: " << endl;
974 for (int nr = 0; nr< wfn->basis()->nshell(); ++nr) {
975 ExEnv::out0() << "Shell nr. " << nr << ": ";
976 (*wfn->basis())[nr].print(ExEnv::out0());
977 }
978 }
979
980
981 delete[] basename;
982 delete[] molname;
983 SCFormIO::set_default_basename(0);
984
985 renderer = 0;
986 molfreq = 0;
987 molhess = 0;
988 opt = 0;
989 mole = 0;
990 integral = 0;
991 debugger = 0;
992 thread = 0;
993 tim = 0;
994 keyval = 0;
995 parsedkv = 0;
996 grp = 0;
997 memory = 0;
998 clean_up();
999
1000#if defined(HAVE_TIME) && defined(HAVE_CTIME)
1001 time(&t);
1002 tstr = ctime(&t);
1003#endif
1004 if (!tstr) {
1005 tstr = "UNKNOWN";
1006 }
1007 ExEnv::out0() << endl
1008 << indent << scprintf("End Time: %s", tstr) << endl;
1009
1010 if (output != 0) {
1011 ExEnv::set_out(&cout);
1012 delete outstream;
1013 }
1014
1015 return 0;
1016}
1017
1018int
1019main(int argc, char *argv[])
1020{
1021 try {
1022 try_main(argc, argv);
1023 }
1024 catch (SCException &e) {
1025 cout << argv[0] << ": ERROR: SC EXCEPTION RAISED:" << endl
1026 << e.what()
1027 << endl;
1028 clean_up();
1029 throw;
1030 }
1031 catch (bad_alloc &e) {
1032 cout << argv[0] << ": ERROR: MEMORY ALLOCATION FAILED:" << endl
1033 << e.what()
1034 << endl;
1035 clean_up();
1036 throw;
1037 }
1038 catch (exception &e) {
1039 cout << argv[0] << ": ERROR: EXCEPTION RAISED:" << endl
1040 << e.what()
1041 << endl;
1042 clean_up();
1043 throw;
1044 }
1045 catch (...) {
1046 cout << argv[0] << ": ERROR: UNKNOWN EXCEPTION RAISED" << endl;
1047 clean_up();
1048 throw;
1049 }
1050 return 0;
1051}
1052
1053/////////////////////////////////////////////////////////////////////////////
1054
1055// Local Variables:
1056// mode: c++
1057// c-file-style: "ETS"
1058// End:
Note: See TracBrowser for help on using the repository browser.