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

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

Initial commit based on 3.0.0alpha (here claimed as 2.4).

  • simply added all files.
  • Property mode set to 100644
File size: 29.8 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("messagegrp", GetLongOpt::MandatoryValue,
236 "which message group to use", 0);
237 options.enroll("threadgrp", GetLongOpt::MandatoryValue,
238 "which thread group to use", 0);
239 options.enroll("memorygrp", GetLongOpt::MandatoryValue,
240 "which memory group to use", 0);
241 options.enroll("integral", GetLongOpt::MandatoryValue,
242 "which integral evaluator to use", 0);
243 options.enroll("l", GetLongOpt::MandatoryValue, "basis set limit", "0");
244 options.enroll("W", GetLongOpt::MandatoryValue,
245 "set the working directory", ".");
246 options.enroll("c", GetLongOpt::NoValue, "check input then exit", 0);
247 options.enroll("v", GetLongOpt::NoValue, "print the version number", 0);
248 options.enroll("w", GetLongOpt::NoValue, "print the warranty", 0);
249 options.enroll("L", GetLongOpt::NoValue, "print the license", 0);
250 options.enroll("k", GetLongOpt::NoValue, "print key/value assignments", 0);
251 options.enroll("i", GetLongOpt::NoValue, "convert simple to OO input", 0);
252 options.enroll("d", GetLongOpt::NoValue, "debug", 0);
253 options.enroll("h", GetLongOpt::NoValue, "print this message", 0);
254 options.enroll("cca-path", GetLongOpt::OptionalValue,
255 "cca component path", "");
256 options.enroll("cca-load", GetLongOpt::OptionalValue,
257 "cca components to load", "");
258
259 int optind = options.parse(argc, argv);
260
261 const char *output = options.retrieve("o");
262 ostream *outstream = 0;
263 if (output != 0) {
264 outstream = new ofstream(output);
265 ExEnv::set_out(outstream);
266 }
267
268 if (options.retrieve("h")) {
269 ExEnv::out0()
270 << indent << "MPQC version " << SC_VERSION << endl
271 << indent << "compiled for " << TARGET_ARCH << endl
272 << SCFormIO::copyright << endl;
273 options.usage(ExEnv::out0());
274 exit(0);
275 }
276
277 if (options.retrieve("v")) {
278 ExEnv::out0()
279 << indent << "MPQC version " << SC_VERSION << endl
280 << indent << "compiled for " << TARGET_ARCH << endl
281 << SCFormIO::copyright;
282 exit(0);
283 }
284
285 if (options.retrieve("w")) {
286 ExEnv::out0()
287 << indent << "MPQC version " << SC_VERSION << endl
288 << indent << "compiled for " << TARGET_ARCH << endl
289 << SCFormIO::copyright << endl
290 << SCFormIO::warranty;
291 exit(0);
292 }
293
294 if (options.retrieve("L")) {
295 ExEnv::out0()
296 << indent << "MPQC version " << SC_VERSION << endl
297 << indent << "compiled for " << TARGET_ARCH << endl
298 << SCFormIO::copyright << endl
299 << SCFormIO::license;
300 exit(0);
301 }
302
303 // set the working dir
304 if (strcmp(options.retrieve("W"),"."))
305 chdir(options.retrieve("W"));
306
307 // initialize keyval input
308 const char *object_input = options.retrieve("f");
309 const char *generic_input;
310 if (argc - optind == 0) {
311 generic_input = 0;
312 }
313 else if (argc - optind == 1) {
314 generic_input = argv[optind];
315 }
316 else {
317 options.usage();
318 throw invalid_argument("extra arguments given");
319 }
320
321 // get the message group. first try the commandline and environment
322 if (grp.null()) grp = MessageGrp::initial_messagegrp(argc, argv);
323 if (grp.nonnull())
324 MessageGrp::set_default_messagegrp(grp);
325 else
326 grp = MessageGrp::get_default_messagegrp();
327
328 if (object_input == 0 && generic_input == 0) {
329 generic_input = "mpqc.in";
330 }
331 else if (object_input && generic_input) {
332 options.usage();
333 throw invalid_argument("only one of -f and a file argument can be given");
334 }
335
336 const char *input;
337 if (object_input) input = object_input;
338 if (generic_input) input = generic_input;
339
340 Ref<ParsedKeyVal> parsedkv;
341 // read the input file on only node 0
342 char *in_char_array;
343 if (grp->me() == 0) {
344 ifstream is(input);
345#ifdef HAVE_SSTREAM
346 ostringstream ostrs;
347 is >> ostrs.rdbuf();
348 int n = 1 + strlen(ostrs.str().c_str());
349 in_char_array = strcpy(new char[n],ostrs.str().c_str());
350#else
351 ostrstream ostrs;
352 is >> ostrs.rdbuf();
353 ostrs << ends;
354 in_char_array = ostrs.str();
355 int n = ostrs.pcount();
356#endif
357 grp->bcast(n);
358 grp->bcast(in_char_array, n);
359 }
360 else {
361 int n;
362 grp->bcast(n);
363 in_char_array = new char[n];
364 grp->bcast(in_char_array, n);
365 }
366
367 int use_simple_input;
368 if (generic_input && grp->me() == 0) {
369 MPQCIn mpqcin;
370 use_simple_input = mpqcin.check_string(in_char_array);
371 }
372 else {
373 use_simple_input = 0;
374 }
375 grp->bcast(use_simple_input);
376
377 if (use_simple_input) {
378 MPQCIn mpqcin;
379 char *simple_input_text = mpqcin.parse_string(in_char_array);
380 if (options.retrieve("i")) {
381 ExEnv::out0() << "Generated object-oriented input file:" << endl
382 << simple_input_text
383 << endl;
384 exit(0);
385 }
386 parsedkv = new ParsedKeyVal();
387 parsedkv->parse_string(simple_input_text);
388 delete[] simple_input_text;
389 }
390 else {
391 parsedkv = new ParsedKeyVal();
392 parsedkv->parse_string(in_char_array);
393 }
394 delete[] in_char_array;
395
396 if (options.retrieve("k")) parsedkv->verbose(1);
397 Ref<KeyVal> keyval = new PrefixKeyVal(parsedkv.pointer(),"mpqc");
398
399 // get the basename for output files
400 const char *basename_source;
401 if (output) basename_source = output;
402 else basename_source = input;
403 int nfilebase = (int) (::strrchr(basename_source, '.') - basename_source);
404 char *basename = new char[nfilebase + 1];
405 strncpy(basename, basename_source, nfilebase);
406 basename[nfilebase] = '\0';
407 SCFormIO::set_default_basename(basename);
408
409 // set up output classes
410 SCFormIO::setindent(ExEnv::outn(), 2);
411 SCFormIO::setindent(ExEnv::errn(), 2);
412 SCFormIO::setindent(cout, 2);
413 SCFormIO::setindent(cerr, 2);
414
415 SCFormIO::set_printnode(0);
416 if (grp->n() > 1)
417 SCFormIO::init_mp(grp->me());
418
419 if (options.retrieve("d"))
420 SCFormIO::set_debug(1);
421
422 // initialize timing for mpqc
423 grp->sync(); // make sure nodes are sync'ed before starting timings
424 Ref<RegionTimer> tim;
425 if (keyval->exists("timer")) tim << keyval->describedclassvalue("timer");
426 else tim = new ParallelRegionTimer(grp,"mpqc",1,1);
427 RegionTimer::set_default_regiontimer(tim);
428
429 if (tim.nonnull()) tim->enter("input");
430
431 // announce ourselves
432 const char title1[] = "MPQC: Massively Parallel Quantum Chemistry";
433 int ntitle1 = sizeof(title1);
434 const char title2[] = "Version " SC_VERSION;
435 int ntitle2 = sizeof(title2);
436 ExEnv::out0() << endl;
437 ExEnv::out0() << indent;
438 for (i=0; i<(80-ntitle1)/2; i++) ExEnv::out0() << ' ';
439 ExEnv::out0() << title1 << endl;
440 ExEnv::out0() << indent;
441 for (i=0; i<(80-ntitle2)/2; i++) ExEnv::out0() << ' ';
442 ExEnv::out0() << title2 << endl << endl;
443
444 const char *tstr = 0;
445#if defined(HAVE_TIME) && defined(HAVE_CTIME)
446 time_t t;
447 time(&t);
448 tstr = ctime(&t);
449#endif
450 if (!tstr) {
451 tstr = "UNKNOWN";
452 }
453
454 ExEnv::out0()
455 << indent << scprintf("Machine: %s", TARGET_ARCH) << endl
456 << indent << scprintf("User: %s@%s",
457 ExEnv::username(), ExEnv::hostname()) << endl
458 << indent << scprintf("Start Time: %s", tstr) << endl;
459
460 // get the thread group. first try the commandline and environment
461 Ref<ThreadGrp> thread = ThreadGrp::initial_threadgrp(argc, argv);
462
463 // if we still don't have a group, try reading the thread group
464 // from the input
465 if (thread.null()) {
466 thread << keyval->describedclassvalue("thread");
467 }
468
469 if (thread.nonnull())
470 ThreadGrp::set_default_threadgrp(thread);
471 else
472 thread = ThreadGrp::get_default_threadgrp();
473
474 // get the memory group. first try the commandline and environment
475 Ref<MemoryGrp> memory = MemoryGrp::initial_memorygrp(argc, argv);
476
477 // if we still don't have a group, try reading the memory group
478 // from the input
479 if (memory.null()) {
480 memory << keyval->describedclassvalue("memory");
481 }
482
483 if (memory.nonnull())
484 MemoryGrp::set_default_memorygrp(memory);
485 else
486 memory = MemoryGrp::get_default_memorygrp();
487
488 ExEnv::out0() << indent
489 << "Using " << grp->class_name()
490 << " for message passing (number of nodes = " << grp->n() << ")." << endl
491 << indent
492 << "Using " << thread->class_name()
493 << " for threading (number of threads = " << thread->nthread() << ")." << endl
494 << indent
495 << "Using " << memory->class_name()
496 << " for distributed shared memory." << endl
497 << indent
498 << "Total number of processors = " << grp->n() * thread->nthread() << endl;
499
500#ifdef HAVE_CHEMISTRY_CCA
501 // initialize cca framework
502 KeyValValuestring emptystring("");
503 bool do_cca = keyval->booleanvalue("do_cca",falsevalue);
504
505 string cca_path(options.retrieve("cca-path"));
506 string cca_load(options.retrieve("cca-load"));
507 if(cca_path.size()==0)
508 cca_path = keyval->stringvalue("cca_path",emptystring);
509 if(cca_load.size()==0)
510 cca_load = keyval->stringvalue("cca_load",emptystring);
511
512 if( !do_cca && (cca_load.size() > 0 || cca_path.size() > 0) )
513 do_cca = true;
514
515 if(cca_path.size()==0) {
516 #ifdef CCA_PATH
517 cca_path = CCA_PATH;
518 #endif
519 }
520 if(cca_load.size()==0) {
521 cca_load += "MPQC.IntegralEvaluatorFactory";
522 }
523
524 if( cca_load.size() > 0 && cca_path.size() > 0 && do_cca ) {
525 string cca_args = "--path " + cca_path + " --load " + cca_load;
526 ExEnv::out0() << endl << indent << "Initializing CCA framework with args: "
527 << endl << indent << cca_args << endl;
528 CCAEnv::init( cca_args );
529 }
530#endif
531
532 // now set up the debugger
533 Ref<Debugger> debugger; debugger << keyval->describedclassvalue("debug");
534 if (debugger.nonnull()) {
535 Debugger::set_default_debugger(debugger);
536 debugger->set_exec(argv[0]);
537 debugger->set_prefix(grp->me());
538 if (options.retrieve("d"))
539 debugger->debug("Starting debugger because -d given on command line.");
540 }
541
542 // now check to see what matrix kit to use
543 if (keyval->exists("matrixkit"))
544 SCMatrixKit::set_default_matrixkit(
545 dynamic_cast<SCMatrixKit*>(
546 keyval->describedclassvalue("matrixkit").pointer()));
547
548 // get the integral factory. first try commandline and environment
549 Ref<Integral> integral = Integral::initial_integral(argc, argv);
550
551 // if we still don't have a integral, try reading the integral
552 // from the input
553 if (integral.null()) {
554 integral << keyval->describedclassvalue("integrals");
555 }
556
557 if (integral.nonnull())
558 Integral::set_default_integral(integral);
559 else
560 integral = Integral::get_default_integral();
561
562 ExEnv::out0() << endl << indent
563 << "Using " << integral->class_name()
564 << " by default for molecular integrals evaluation" << endl << endl;
565
566 // check for a molecular energy and optimizer
567 KeyValValueString molnamedef(basename);
568 char * molname = keyval->pcharvalue("filename", molnamedef);
569 if (strcmp(molname, basename))
570 SCFormIO::set_default_basename(molname);
571
572 char * ckptfile = new char[strlen(molname)+6];
573 sprintf(ckptfile,"%s.ckpt",molname);
574
575 KeyValValueString restartfiledef(ckptfile);
576 char * restartfile = keyval->pcharvalue("restart_file", restartfiledef);
577
578 char * wfn_file = keyval->pcharvalue("wfn_file");
579 if (wfn_file == 0) {
580 wfn_file = new char[strlen(molname)+6];
581 sprintf(wfn_file,"%s.wfn",molname);
582 }
583 char *mole_ckpt_file = new char[strlen(wfn_file)+1];
584 sprintf(mole_ckpt_file,"%s",wfn_file);
585
586 int restart = keyval->booleanvalue("restart",truevalue);
587
588 int checkpoint = keyval->booleanvalue("checkpoint",truevalue);
589 int checkpoint_freq = keyval->intvalue("checkpoint_freq",KeyValValueint(1));
590
591 int savestate = keyval->booleanvalue("savestate",truevalue);
592
593 struct stat sb;
594 Ref<MolecularEnergy> mole;
595 Ref<Optimize> opt;
596
597 int statresult, statsize;
598 if (restart) {
599 if (grp->me() == 0) {
600 statresult = stat(restartfile,&sb);
601 statsize = (statresult==0) ? sb.st_size : 0;
602 }
603 grp->bcast(statresult);
604 grp->bcast(statsize);
605 }
606 if (restart && statresult==0 && statsize) {
607 BcastStateInBin si(grp,restartfile);
608 if (keyval->exists("override")) {
609 si.set_override(new PrefixKeyVal(keyval,"override"));
610 }
611 char *suf = strrchr(restartfile,'.');
612 if (!strcmp(suf,".wfn")) {
613 mole << SavableState::key_restore_state(si,"mole");
614 ExEnv::out0() << endl
615 << indent << "Restored <" << mole->class_name()
616 << "> from " << restartfile << endl;
617
618 opt << keyval->describedclassvalue("opt");
619 if (opt.nonnull())
620 opt->set_function(mole.pointer());
621 }
622 else {
623 opt << SavableState::key_restore_state(si,"opt");
624 if (opt.nonnull()) {
625 mole << opt->function();
626 ExEnv::out0() << endl << indent
627 << "Restored <Optimize> from " << restartfile << endl;
628 }
629 }
630 } else {
631 mole << keyval->describedclassvalue("mole");
632 opt << keyval->describedclassvalue("opt");
633 }
634
635 if (mole.nonnull()) {
636 MolecularFormula mf(mole->molecule());
637 ExEnv::out0() << endl << indent
638 << "Molecular formula " << mf.formula() << endl;
639 if (checkpoint) {
640 mole->set_checkpoint();
641 if (grp->me() == 0) mole->set_checkpoint_file(mole_ckpt_file);
642 else mole->set_checkpoint_file(devnull);
643 mole->set_checkpoint_freq(checkpoint_freq);
644 }
645 }
646 delete[] mole_ckpt_file;
647
648 if (checkpoint && opt.nonnull()) {
649 opt->set_checkpoint();
650 if (grp->me() == 0) opt->set_checkpoint_file(ckptfile);
651 else opt->set_checkpoint_file(devnull);
652 }
653
654 // see if frequencies are wanted
655
656 Ref<MolecularHessian> molhess;
657 molhess << keyval->describedclassvalue("hess");
658 Ref<MolecularFrequencies> molfreq;
659 molfreq << keyval->describedclassvalue("freq");
660
661 int check = (options.retrieve("c") != 0);
662 int limit = atoi(options.retrieve("l"));
663 if (limit) {
664 Ref<Wavefunction> wfn; wfn << mole;
665 if (wfn.nonnull() && wfn->ao_dimension()->n() > limit) {
666 ExEnv::out0() << endl << indent
667 << "The limit of " << limit << " basis functions has been exceeded."
668 << endl;
669 check = 1;
670 }
671 }
672
673 if (check) {
674 ExEnv::out0() << endl << indent
675 << "Exiting since the check option is on." << endl;
676 exit(0);
677 }
678
679 if (tim.nonnull()) tim->change("calc");
680
681 int do_energy = keyval->booleanvalue("do_energy",truevalue);
682
683 int do_grad = keyval->booleanvalue("do_gradient",falsevalue);
684
685 int do_opt = keyval->booleanvalue("optimize",truevalue);
686
687 int do_pdb = keyval->booleanvalue("write_pdb",falsevalue);
688
689 int print_mole = keyval->booleanvalue("print_mole",truevalue);
690
691 int print_timings = keyval->booleanvalue("print_timings",truevalue);
692
693 // see if any pictures are desired
694 Ref<Render> renderer;
695 renderer << keyval->describedclassvalue("renderer");
696
697 // If we have a renderer, then we will read in some more info
698 // below. Otherwise we can get rid of the keyval's, to eliminate
699 // superfluous references to objects that we might otherwise be
700 // able to delete. We cannot read in the remaining rendering
701 // objects now, since some of their KeyVal CTOR's are heavyweight,
702 // requiring optimized geometries, etc.
703 if (renderer.null()) {
704 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
705 keyval = 0;
706 parsedkv = 0;
707 }
708
709 // sanity checks for the benefit of reasonable looking output
710 if (opt.null()) do_opt=0;
711
712 ExEnv::out0() << endl << indent
713 << "MPQC options:" << endl << incindent
714 << indent << "matrixkit = <"
715 << SCMatrixKit::default_matrixkit()->class_name() << ">" << endl
716 << indent << "filename = " << molname << endl
717 << indent << "restart_file = " << restartfile << endl
718 << indent << "restart = " << (restart ? "yes" : "no") << endl
719 << indent << "checkpoint = " << (checkpoint ? "yes" : "no") << endl
720 << indent << "savestate = " << (savestate ? "yes" : "no") << endl
721 << indent << "do_energy = " << (do_energy ? "yes" : "no") << endl
722 << indent << "do_gradient = " << (do_grad ? "yes" : "no") << endl
723 << indent << "optimize = " << (do_opt ? "yes" : "no") << endl
724 << indent << "write_pdb = " << (do_pdb ? "yes" : "no") << endl
725 << indent << "print_mole = " << (print_mole ? "yes" : "no") << endl
726 << indent << "print_timings = " << (print_timings ? "yes" : "no")
727 << endl << decindent;
728
729 delete[] restartfile;
730 delete[] ckptfile;
731
732 int ready_for_freq = 1;
733 if (mole.nonnull()) {
734 if (((do_opt && opt.nonnull()) || do_grad)
735 && !mole->gradient_implemented()) {
736 ExEnv::out0() << indent
737 << "WARNING: optimization or gradient requested but the given"
738 << endl
739 << " MolecularEnergy object cannot do gradients."
740 << endl;
741 }
742
743 if (do_opt && opt.nonnull() && mole->gradient_implemented()) {
744 int result = opt->optimize();
745 if (result) {
746 ExEnv::out0() << indent
747 << "The optimization has converged." << endl << endl;
748 ExEnv::out0() << indent
749 << scprintf("Value of the MolecularEnergy: %15.10f",
750 mole->energy())
751 << endl << endl;
752 } else {
753 ExEnv::out0() << indent
754 << "The optimization has NOT converged." << endl << endl;
755 ready_for_freq = 0;
756 }
757 } else if (do_grad && mole->gradient_implemented()) {
758 mole->do_gradient(1);
759 ExEnv::out0() << endl << indent
760 << scprintf("Value of the MolecularEnergy: %15.10f",
761 mole->energy())
762 << endl;
763 if (mole->value_result().actual_accuracy()
764 > mole->value_result().desired_accuracy()) {
765 ExEnv::out0() << indent
766 << "WARNING: desired accuracy not achieved in energy" << endl;
767 }
768 ExEnv::out0() << endl;
769 // Use result_noupdate since the energy might not have converged
770 // to the desired accuracy in which case grabbing the result will
771 // start up the calculation again. However the gradient might
772 // not have been computed (if we are restarting and the gradient
773 // isn't in the save file for example).
774 RefSCVector grad;
775 if (mole->gradient_result().computed()) {
776 grad = mole->gradient_result().result_noupdate();
777 }
778 else {
779 grad = mole->gradient();
780 }
781 if (grad.nonnull()) {
782 grad.print("Gradient of the MolecularEnergy:");
783 if (mole->gradient_result().actual_accuracy()
784 > mole->gradient_result().desired_accuracy()) {
785 ExEnv::out0() << indent
786 << "WARNING: desired accuracy not achieved in gradient" << endl;
787 }
788 }
789 } else if (do_energy && mole->value_implemented()) {
790 ExEnv::out0() << endl << indent
791 << scprintf("Value of the MolecularEnergy: %15.10f",
792 mole->energy())
793 << endl << endl;
794 }
795 }
796
797 if (tim.nonnull()) tim->exit("calc");
798
799 // save this before doing the frequency stuff since that obsoletes the
800 // function stuff
801 if (savestate) {
802 if (opt.nonnull()) {
803 if (grp->me() == 0) {
804 ckptfile = new char[strlen(molname)+6];
805 sprintf(ckptfile,"%s.ckpt",molname);
806 }
807 else {
808 ckptfile = new char[strlen(devnull)+1];
809 strcpy(ckptfile, devnull);
810 }
811
812 StateOutBin so(ckptfile);
813 SavableState::save_state(opt.pointer(),so);
814 so.close();
815
816 delete[] ckptfile;
817 }
818
819 if (mole.nonnull()) {
820 if (grp->me() == 0) {
821 if (wfn_file == 0) {
822 wfn_file = new char[strlen(molname)+6];
823 sprintf(wfn_file,"%s.wfn",molname);
824 }
825 }
826 else {
827 delete[] wfn_file;
828 wfn_file = new char[strlen(devnull)+1];
829 strcpy(wfn_file, devnull);
830 }
831
832 StateOutBin so(wfn_file);
833 SavableState::save_state(mole.pointer(),so);
834 so.close();
835
836 }
837 }
838 delete[] wfn_file;
839
840 // Frequency calculation.
841 if (ready_for_freq && molfreq.nonnull()) {
842 RefSymmSCMatrix xhessian;
843 if (molhess.nonnull()) {
844 // if "hess" input was given, use it to compute the hessian
845 xhessian = molhess->cartesian_hessian();
846 }
847 else if (mole->hessian_implemented()) {
848 // if mole can compute the hessian, use that hessian
849 xhessian = mole->get_cartesian_hessian();
850 }
851 else if (mole->gradient_implemented()) {
852 // if mole can compute gradients, use gradients at finite
853 // displacements to compute the hessian
854 molhess = new FinDispMolecularHessian(mole);
855 xhessian = molhess->cartesian_hessian();
856 }
857 else {
858 ExEnv::out0() << "mpqc: WARNING: Frequencies cannot be computed" << endl;
859 }
860
861 if (xhessian.nonnull()) {
862 char *hessfile = SCFormIO::fileext_to_filename(".hess");
863 MolecularHessian::write_cartesian_hessian(hessfile,
864 mole->molecule(), xhessian);
865 delete[] hessfile;
866
867 molfreq->compute_frequencies(xhessian);
868 // DEGENERACY IS NOT CORRECT FOR NON-SINGLET CASES:
869 molfreq->thermochemistry(1);
870 }
871 }
872
873 if (renderer.nonnull()) {
874 Ref<RenderedObject> rendered;
875 rendered << keyval->describedclassvalue("rendered");
876 Ref<AnimatedObject> animated;
877 animated << keyval->describedclassvalue("rendered");
878 if (rendered.nonnull()) {
879 if (tim.nonnull()) tim->enter("render");
880 if (grp->me() == 0) renderer->render(rendered);
881 if (tim.nonnull()) tim->exit("render");
882 }
883 else if (animated.nonnull()) {
884 if (tim.nonnull()) tim->enter("render");
885 if (grp->me() == 0) renderer->animate(animated);
886 if (tim.nonnull()) tim->exit("render");
887 }
888 else {
889 if (tim.nonnull()) tim->enter("render");
890 int n = keyval->count("rendered");
891 for (i=0; i<n; i++) {
892 rendered << keyval->describedclassvalue("rendered",i);
893 animated << keyval->describedclassvalue("rendered",i);
894 if (rendered.nonnull()) {
895 // make sure the object has a name so we don't overwrite its file
896 if (rendered->name() == 0) {
897 char ic[64];
898 sprintf(ic,"%02d",i);
899 rendered->set_name(ic);
900 }
901 if (grp->me() == 0) renderer->render(rendered);
902 }
903 else if (animated.nonnull()) {
904 // make sure the object has a name so we don't overwrite its file
905 if (animated->name() == 0) {
906 char ic[64];
907 sprintf(ic,"%02d",i);
908 animated->set_name(ic);
909 }
910 if (grp->me() == 0) renderer->animate(animated);
911 }
912 }
913 if (tim.nonnull()) tim->exit("render");
914 }
915 Ref<MolFreqAnimate> molfreqanim;
916 molfreqanim << keyval->describedclassvalue("animate_modes");
917 if (ready_for_freq && molfreq.nonnull()
918 && molfreqanim.nonnull()) {
919 if (tim.nonnull()) tim->enter("render");
920 molfreq->animate(renderer, molfreqanim);
921 if (tim.nonnull()) tim->exit("render");
922 }
923 }
924
925 if (mole.nonnull()) {
926 if (print_mole)
927 mole->print(ExEnv::out0());
928
929 if (do_pdb && grp->me() == 0) {
930 ckptfile = new char[strlen(molname)+5];
931 sprintf(ckptfile, "%s.pdb", molname);
932 ofstream pdbfile(ckptfile);
933 mole->molecule()->print_pdb(pdbfile);
934 delete[] ckptfile;
935 }
936
937 }
938 else {
939 ExEnv::out0() << "mpqc: The molecular energy object is null" << endl
940 << " make sure \"mole\" specifies a MolecularEnergy derivative"
941 << endl;
942 }
943
944 if (parsedkv.nonnull()) print_unseen(parsedkv, input);
945
946 if (print_timings)
947 if (tim.nonnull()) tim->print(ExEnv::out0());
948
949 delete[] basename;
950 delete[] molname;
951 SCFormIO::set_default_basename(0);
952
953 renderer = 0;
954 molfreq = 0;
955 molhess = 0;
956 opt = 0;
957 mole = 0;
958 integral = 0;
959 debugger = 0;
960 thread = 0;
961 tim = 0;
962 keyval = 0;
963 parsedkv = 0;
964 grp = 0;
965 memory = 0;
966 clean_up();
967
968#if defined(HAVE_TIME) && defined(HAVE_CTIME)
969 time(&t);
970 tstr = ctime(&t);
971#endif
972 if (!tstr) {
973 tstr = "UNKNOWN";
974 }
975 ExEnv::out0() << endl
976 << indent << scprintf("End Time: %s", tstr) << endl;
977
978 if (output != 0) {
979 ExEnv::set_out(&cout);
980 delete outstream;
981 }
982
983 return 0;
984}
985
986int
987main(int argc, char *argv[])
988{
989 try {
990 try_main(argc, argv);
991 }
992 catch (SCException &e) {
993 cout << argv[0] << ": ERROR: SC EXCEPTION RAISED:" << endl
994 << e.what()
995 << endl;
996 clean_up();
997 throw;
998 }
999 catch (bad_alloc &e) {
1000 cout << argv[0] << ": ERROR: MEMORY ALLOCATION FAILED:" << endl
1001 << e.what()
1002 << endl;
1003 clean_up();
1004 throw;
1005 }
1006 catch (exception &e) {
1007 cout << argv[0] << ": ERROR: EXCEPTION RAISED:" << endl
1008 << e.what()
1009 << endl;
1010 clean_up();
1011 throw;
1012 }
1013 catch (...) {
1014 cout << argv[0] << ": ERROR: UNKNOWN EXCEPTION RAISED" << endl;
1015 clean_up();
1016 throw;
1017 }
1018 return 0;
1019}
1020
1021/////////////////////////////////////////////////////////////////////////////
1022
1023// Local Variables:
1024// mode: c++
1025// c-file-style: "ETS"
1026// End:
Note: See TracBrowser for help on using the repository browser.