AMDiS.cc 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
 *
 * AMDiS - Adaptive multidimensional simulations
 *
 * Copyright (C) 2013 Dresden University of Technology. All Rights Reserved.
 * Web: https://fusionforge.zih.tu-dresden.de/projects/amdis
 *
 * Authors: 
 * Simon Vey, Thomas Witkowski, Andreas Naumann, Simon Praetorius, et al.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 *
 *
 * This file is part of AMDiS
 *
 * See also license.opensource.txt in the distribution.
 * 
 ******************************************************************************/
20 21 22 23 24 25 26


#include "AMDiS.h"
#include <string>
#ifdef HAVE_ZOLTAN
#include <zoltan_cpp.h>
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
27
#include "boost/program_options.hpp"
28 29 30 31 32

namespace AMDiS {

  using namespace std;

33
#if defined(HAVE_PARALLEL_MTL4)
34
  mtl::par::environment* mtl_environment = NULL;
35 36
#endif

Thomas Witkowski's avatar
Thomas Witkowski committed
37
  void init(int argc, char **argv, std::string initFileName)
38
  {
39 40
#if defined(HAVE_PARALLEL_DOMAIN_AMDIS) || defined(HAVE_PETSC)
  #ifdef HAVE_PARALLEL_MTL4
41
    mtl_environment = new mtl::par::environment(argc, argv);
42
  #else
43
    PetscInitialize(&argc, &argv, NULL, NULL);
44 45 46 47 48 49 50 51
    #if defined(HAVE_PARALLEL_DOMAIN_AMDIS)
    Parallel::mpi::startRand();
    #else
    srand(time(NULL));
    #endif
  #endif

  #ifdef HAVE_ZOLTAN
52 53
    float zoltanVersion = 0.0;
    Zoltan_Initialize(argc, argv, &zoltanVersion);
54
  #endif
55
#endif
Praetorius, Simon's avatar
Praetorius, Simon committed
56 57
    
    Parameters::clearData();
Praetorius, Simon's avatar
Praetorius, Simon committed
58 59 60 61 62 63 64 65 66 67 68
    
    // read commandline arguments
    namespace po = boost::program_options;
    
    // Declare the supported options.
    po::options_description desc("Usage: " + std::string(argv[0]) + " init-file [options]\nAllowed options");
    desc.add_options()
	("help", "produce help message")
	("init-file", po::value<std::string>(), "set init file")
	("rs", po::value<int>(), "set restart parameter")
	("parameters", po::value<std::string>(), "set parameter in init file\nsyntax: \"key1: value1; key2: value2...\"");
69 70 71 72 73 74 75

    po::options_description hidden("Hidden options");
    hidden.add_options()
	("unknown", po::value<std::vector<std::string> >(), "unknown options");
	
    po::options_description cmdline_options;
    cmdline_options.add(desc).add(hidden);
Praetorius, Simon's avatar
Praetorius, Simon committed
76 77 78 79
	
    // first argument is init-filename
    po::positional_options_description p;
    p.add("init-file", 1);
80
    p.add("unknown", -1);
Praetorius, Simon's avatar
Praetorius, Simon committed
81 82 83

    // parse comandline
    po::variables_map vm;
84
    po::store(po::command_line_parser(argc, argv).options(cmdline_options).positional(p).allow_unregistered().run(), vm);
Praetorius, Simon's avatar
Praetorius, Simon committed
85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    po::notify(vm);    

    // print help message
    if (vm.count("help")) {
      cout << desc << "\n";
      exit(1);
    }
    
    // add parameter for restart simulations
    if (vm.count("rs"))
      Parameters::add("argv->rs", vm["rs"].as<int>(), 0);
      
    // set parameters before reading the initfile
    if (vm.count("parameters"))
      Parameters::readArgv(vm["parameters"].as<std::string>());
Praetorius, Simon's avatar
Praetorius, Simon committed
100
    
Thomas Witkowski's avatar
Thomas Witkowski committed
101
    if (initFileName == "") {
Praetorius, Simon's avatar
Praetorius, Simon committed
102 103 104 105
      if (vm.count("init-file"))
	Parameters::init(vm["init-file"].as<std::string>());
      else
	throw(std::runtime_error("No init file specified!"));
Thomas Witkowski's avatar
Thomas Witkowski committed
106 107 108
    } else {
      Parameters::init(initFileName);
    }
109

110
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
111
    Parameters::get("parallel->log main rank", Msg::outputMainRank);
112
#endif
113

Praetorius, Simon's avatar
Praetorius, Simon committed
114 115 116 117 118 119
    // reset parameters from command line
    bool ignoreCommandline = false;
    Parameters::get("ignore commandline options", ignoreCommandline);
    if (vm.count("parameters") && !ignoreCommandline)
      Parameters::readArgv(vm["parameters"].as<std::string>(),0);
    
120
    
Praetorius, Simon's avatar
Praetorius, Simon committed
121 122
    // initialize global strcutures using parameters
    Global::init();
123 124 125
  }


126 127 128
  void init(std::string initFileName)
  {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
129
    FUNCNAME("AMDiS::init()");
130 131 132 133 134 135 136
    ERROR_EXIT("Does not work in parallel!\n");
#endif

    Parameters::init(initFileName);
  }


137 138 139
  void finalize()
  {
#ifdef HAVE_PARALLEL_DOMAIN_AMDIS
140 141 142
    Parallel::MeshDistributor::globalMeshDistributor->exitParallelization();
    delete Parallel::MeshDistributor::globalMeshDistributor;
  #ifdef HAVE_PARALLEL_MTL4
Thomas Witkowski's avatar
Thomas Witkowski committed
143
    if (mtl_environment)
144
      delete mtl_environment;
145
  #else
146
    PetscFinalize();
147
  #endif
148
#endif
149 150 151
    
#ifdef HAVE_PETSC
    PetscFinalize();
152
#endif
153
    
154 155 156
  }

}