Changeset 62dabe


Ignore:
Timestamp:
Jul 6, 2012, 5:43:58 PM (13 years ago)
Author:
Frederik Heber <heber@…>
Children:
aa42a9
Parents:
d11832
Message:

Extracted Option parsing and setting into extra functions.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/bin/mpqc/mpqc.cc

    rd11832 r62dabe  
    4949#include <fstream>
    5050
     51#include <boost/bind.hpp>
     52#include <boost/function.hpp>
     53
    5154#include <scconfig.h>
    5255#ifdef HAVE_SSTREAM
     
    178181}
    179182
    180 int
    181 try_main(int argc, char *argv[])
     183double EvaluateDensity(
     184    SCVector3 &r,
     185    Ref<Integral> &intgrl,
     186    GaussianBasisSet::ValueData &vdat,
     187    Ref<Wavefunction> &wfn);
     188
     189/** Places all known options into \a options and parses them from argc,argv.
     190 *
     191 * \param options options structure
     192 * \param argc argument count
     193 * \param argv argument array
     194 * \return return value by GetLongOpt::parse() function
     195 */
     196int ParseOptions(GetLongOpt &options, int argc, char **argv)
    182197{
    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 
    230198  options.usage("[options] [filename]");
    231199  options.enroll("f", GetLongOpt::MandatoryValue,
     
    254222  options.enroll("d", GetLongOpt::NoValue, "debug", 0);
    255223  options.enroll("h", GetLongOpt::NoValue, "print this message", 0);
    256   options.enroll("cca-path", GetLongOpt::OptionalValue, 
     224  options.enroll("cca-path", GetLongOpt::OptionalValue,
    257225                 "cca component path", "");
    258226  options.enroll("cca-load", GetLongOpt::OptionalValue,
     
    261229  int optind = options.parse(argc, argv);
    262230
    263   const char *output = options.retrieve("o");
    264   ostream *outstream = 0;
     231  return optind;
     232}
     233
     234/** Checks for each known option and acts accordingly.
     235 *
     236 * \param options option structure
     237 * \param *output name of outputfile on return
     238 * \param *outstream open output stream on return
     239 */
     240void ComputeOptions(GetLongOpt &options, const char *&output, ostream *&outstream)
     241{
     242  output = options.retrieve("o");
     243  outstream = 0;
    265244  if (output != 0) {
    266245    outstream = new ofstream(output);
     
    276255    exit(0);
    277256  }
    278  
     257
    279258  if (options.retrieve("v")) {
    280259    ExEnv::out0()
     
    284263    exit(0);
    285264  }
    286  
     265
    287266  if (options.retrieve("w")) {
    288267    ExEnv::out0()
     
    293272    exit(0);
    294273  }
    295  
     274
    296275  if (options.retrieve("L")) {
    297276    ExEnv::out0()
     
    305284  // set the working dir
    306285  if (strcmp(options.retrieve("W"),"."))
    307     chdir(options.retrieve("W"));
     286    int retval = chdir(options.retrieve("W"));
    308287
    309288  // check that n and f/o are not given at the same time
     
    311290    throw invalid_argument("-n must not be given with -f or -o");
    312291  }
     292}
     293
     294int
     295try_main(int argc, char *argv[])
     296{
     297  //trash_stack();
     298
     299  KeyValValueboolean truevalue(1), falsevalue(0);
     300  int i;
     301  const char *devnull = "/dev/null";
     302  atexit(clean_up);
     303
     304#ifdef HAVE_FEENABLEEXCEPT
     305  // this uses a glibc extension to trap on individual exceptions
     306# ifdef FE_DIVBYZERO
     307  feenableexcept(FE_DIVBYZERO);
     308# endif
     309# ifdef FE_INVALID
     310  feenableexcept(FE_INVALID);
     311# endif
     312# ifdef FE_OVERFLOW
     313  feenableexcept(FE_OVERFLOW);
     314# endif
     315#endif
     316
     317#ifdef HAVE_FEDISABLEEXCEPT
     318  // this uses a glibc extension to not trap on individual exceptions
     319# ifdef FE_UNDERFLOW
     320  fedisableexcept(FE_UNDERFLOW);
     321# endif
     322# ifdef FE_INEXACT
     323  fedisableexcept(FE_INEXACT);
     324# endif
     325#endif
     326
     327#if defined(HAVE_SETRLIMIT)
     328  struct rlimit rlim;
     329  rlim.rlim_cur = 0;
     330  rlim.rlim_max = 0;
     331  setrlimit(RLIMIT_CORE,&rlim);
     332#endif
     333
     334  ExEnv::init(argc, argv);
     335
     336  // parse commandline options
     337  GetLongOpt options;
     338  int optind = ParseOptions(options, argc, argv);
     339  const char *output = 0;
     340  ostream *outstream = 0;
     341  ComputeOptions(options, output, outstream);
    313342
    314343  // initialize keyval input
     
    327356
    328357  // get the message group.  first try the commandline and environment
     358  Ref<MessageGrp> grp;
     359#if defined(HAVE_MPI) && defined(ALWAYS_USE_MPI)
     360  grp = new MPIMessageGrp(&argc, &argv);
     361#endif
    329362  if (grp.null()) grp = MessageGrp::initial_messagegrp(argc, argv);
    330363  if (grp.nonnull())
     
    962995     ExEnv::out0() << "The AO density matrix is ";
    963996     wfn->ao_density()->print(ExEnv::out0());
     997     ExEnv::out0() << "The natural density matrix is ";
     998     wfn->natural_density()->print(ExEnv::out0());
    964999     ExEnv::out0() << "The Gaussian basis is " << wfn->basis()->name() << endl;
    9651000     ExEnv::out0() << "The Gaussians sit at the following centers: " << endl;
     
    9701005       ExEnv::out0() << endl;
    9711006     }
    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());
     1007     // GaussianShell is the actual orbital functions it seems ...
     1008     //ExEnv::out0() << "There are the following Gaussian Shells: " << endl;
     1009     SCVector3 r;
     1010     r.x() = r.y() = r.z() = 10;
     1011     ExEnv::out0() << "We get the following value at " << r << "." << endl;
     1012     Ref<Integral> intgrl = Integral::get_default_integral();
     1013     GaussianBasisSet::ValueData vdat(wfn->basis(), integral);
     1014     ExEnv::out0() << "Value at (10,10,10) is " << EvaluateDensity(r, intgrl, vdat, wfn) << endl;
     1015     boost::function<double (SCVector3 &r)> evaluator =
     1016         boost::bind(&EvaluateDensity, _1, boost::ref(intgrl), boost::ref(vdat), boost::ref(wfn));
     1017     ExEnv::out0() << "Check against values at " << r << "." << endl;
     1018     int nbasis = wfn->basis()->nbasis();
     1019     double *b_val = new double[nbasis];
     1020     wfn->basis()->values(r, &vdat, b_val);
     1021     for (int i=0; i<nbasis; i++) {
     1022       //ExEnv::out0() << "Shell nr. " << nr << ": ";
     1023       ExEnv::out0() << "Value at (10,10,10) is " << b_val[i] << endl;
    9771024     }
     1025     // perform test integration of density
     1026     double delta = 1.;
     1027     double sum = 0.;
     1028     for (r.x() = -10. ; r.x() < 10.; r.x() += delta)
     1029       for (r.y() = -10. ; r.y() < 10.; r.y() += delta)
     1030         for (r.z() = -10. ; r.z() < 10.; r.z() += delta) {
     1031           wfn->basis()->values(r, &vdat, b_val);
     1032           for (int i=0; i<nbasis; i++)
     1033             sum += wfn->ao_density()->get_element(i,i)*b_val[i];
     1034         }
     1035     sum /= pow(20/delta,3);
     1036     ExEnv::out0() << "Sum over domain [0:20]^3 with " << delta << " delta is " << sum << "." << endl;
     1037     delete[] b_val;
    9781038  }
    9791039
     
    10161076}
    10171077
     1078double EvaluateDensity(SCVector3 &r, Ref<Integral> &intgrl, GaussianBasisSet::ValueData &vdat, Ref<Wavefunction> &wfn)
     1079{
     1080  ExEnv::out0() << "We get the following values at " << r << "." << endl;
     1081  int nbasis = wfn->basis()->nbasis();
     1082  double *b_val = new double[nbasis];
     1083  wfn->basis()->values(r, &vdat, b_val);
     1084  double sum=0.;
     1085  for (int i=0; i<nbasis; i++)
     1086    sum += b_val[i];
     1087  delete[] b_val;
     1088  return sum;
     1089}
     1090
    10181091int
    10191092main(int argc, char *argv[])
Note: See TracChangeset for help on using the changeset viewer.