Main.cpp 22.4 KB
Newer Older
gsell's avatar
gsell committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// ------------------------------------------------------------------------
// $RCSfile: Main.cpp,v $
// ------------------------------------------------------------------------
// $Revision: 1.9.2.2 $
// ------------------------------------------------------------------------
// Copyright: see Copyright.readme
// ------------------------------------------------------------------------
//
// Main program for OPAL
//
// ------------------------------------------------------------------------
//
// $Date: 2004/11/12 20:10:10 $
// $Author: adelmann $
//
// ------------------------------------------------------------------------

18
#include "opal.h"
gsell's avatar
gsell committed
19

20 21 22
Ippl *ippl;
Inform *gmsg;

gsell's avatar
gsell committed
23 24
#include "H5hut.h"

gsell's avatar
gsell committed
25 26 27 28 29 30
#include "AbstractObjects/OpalData.h"
#include "OpalConfigure/Configure.h"
#include "OpalParser/OpalParser.h"
#include "Parser/FileStream.h"
#include "Parser/TerminalStream.h"
#include "Utilities/Timer.h"
31
#include "Fields/Fieldmap.h"
gsell's avatar
gsell committed
32 33
#include "FixedAlgebra/FTps.h"

34
#include "BasicActions/Option.h"
35
#include "Utilities/Options.h"
36 37
#include "Utilities/Options.h"
#include "Utilities/OpalException.h"
38
#include "Utilities/Util.h"
gsell's avatar
gsell committed
39

40
#include "OPALconfig.h"
gsell's avatar
gsell committed
41

42
#ifdef ENABLE_AMR
frey_m's avatar
frey_m committed
43
#include <AMReX_ParallelDescriptor.H>
44
#endif
adelmann's avatar
adelmann committed
45
/*
46
  Includes related to the optimizer
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
47
*/
adelmann's avatar
adelmann committed
48 49 50 51 52 53 54 55 56 57 58
#include "boost/smart_ptr.hpp"

#include "Pilot/Pilot.h"
#include "Util/CmdArguments.h"
#include "Util/OptPilotException.h"

#include "Optimizer/EA/FixedPisaNsga2.h"
#include "Optimizer/EA/BlendCrossover.h"
#include "Optimizer/EA/IndependentBitMutation.h"

#include "Util/OpalInputFileParser.h"
59
#include "Optimize/OpalSimulation.h"
adelmann's avatar
adelmann committed
60 61 62 63 64 65 66 67 68 69 70 71 72

#include "Comm/CommSplitter.h"
#include "Comm/Topology/NoCommTopology.h"
#include "Comm/Splitter/ManyMasterSplit.h"
#include "Comm/MasterGraph/SocialNetworkGraph.h"

#include "Expression/Parser/function.hpp"
#include "Expression/FromFile.h"
#include "Expression/SumErrSq.h"
#include "Expression/SDDSVariable.h"
#include "Expression/RadialPeak.h"
#include "Expression/SumErrSqRadialPeak.h"
#include "Expression/ProbeVariable.h"
kraus's avatar
kraus committed
73

74 75 76 77 78 79 80 81 82
#include <gsl/gsl_errno.h>

#include <boost/filesystem.hpp>
#include <boost/algorithm/string/predicate.hpp>

#include <cstring>
#include <set>
#include <algorithm>

gsell's avatar
gsell committed
83 84 85
//  DTA
#define NC 5

86
void printStringVector(const std::vector<std::string> &strings) {
gsell's avatar
gsell committed
87 88 89 90 91 92 93 94
    unsigned int iend = strings.size(), nc = 0;
    for(unsigned int i = 0; i < iend; ++i) {
        std::cout << "  " << strings[i];
        if(++nc == NC) { nc = 0; std::cout << std::endl; }
    }
    if(nc != 0) std::cout << std::endl;
    std::cout << std::endl;
}
95 96 97 98 99

void errorHandlerGSL(const char *reason,
                     const char *file,
                     int line,
                     int gsl_errno);
gsell's avatar
gsell committed
100 101


adelmann's avatar
adelmann committed
102
bool haveOptimiseRun(int argc, char *argv[]) {
gsell's avatar
gsell committed
103

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
104
    namespace fs = boost::filesystem;
gsell's avatar
gsell committed
105

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
106 107
    std::string so("--initialPopulation");
    bool foundOptArg = false;
108

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
109 110
    int arg = -1;
    std::string fname;
111

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
112 113
    for(int ii = 1; ii < argc; ++ ii) {
        std::string argStr = std::string(argv[ii]);
114

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
115 116 117
        if (argStr.find(so) != std::string::npos) {
            foundOptArg = true;
        }
118

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
        if (argStr == std::string("--input")) {
            ++ ii;
            arg = ii;
            INFOMSG(argv[ii] << endl);
            continue;
        } else if (argStr == std::string("-restart") ||
                   argStr == std::string("--restart")) {
            return false;
        } else if (argStr == std::string("-restartfn") ||
                   argStr == std::string("--restartfn")) {
            return false;
        } else if (argStr == std::string("-version") ||
                   argStr == std::string("--version")) {
            return false;
        } else if (argStr == std::string("-help") ||
                   argStr == std::string("--help")) {
            return false;
        }
        else {
            if (arg == -1 &&
                (ii == 1 || ii + 1 == argc) &&
                argv[ii][0] != '-') {
                arg = ii;
                continue;
            } else {
                continue;
            }
        }
147 148
    }

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
149 150 151 152
    if (arg == -1) {
        INFOMSG("No input file provided!" << endl);
        exit(1);
    }
gsell's avatar
gsell committed
153

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
154 155 156 157 158
    fname = std::string(argv[arg]);
    if (!fs::exists(fname)) {
        INFOMSG("Input file \"" << fname << "\" doesn't exist!" << endl);
        exit(1);
    }
gsell's avatar
gsell committed
159

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
160 161
    std::ifstream inFile;
    inFile.open(fname);
162

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
163 164 165
    std::stringstream strStream;
    strStream << inFile.rdbuf();
    std::string str = strStream.str();
166

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
167
    std::transform(str.begin(), str.end(),str.begin(), ::toupper);
168

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
169 170 171
    const std::string s1("OPTIMIZE");
    const std::string s2("OBJECTIVE");
    const std::string s3("DVAR");
iff's avatar
iff committed
172

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
173 174 175
    bool res = (boost::algorithm::contains(str, s1) &&
                boost::algorithm::contains(str, s2) &&
                boost::algorithm::contains(str, s3));
176

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
177
    inFile.close();
178

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
179
    return res && foundOptArg;
180 181 182 183
}

int mainOPALOptimiser(int argc, char *argv[]) {

184
    MPI_Init(&argc, &argv);
adelmann's avatar
adelmann committed
185 186 187 188 189 190 191 192 193 194 195

    // Setup/Configuration
    //////////////////////////////////////////////////////////////////////////
    typedef OpalInputFileParser Input_t;
    typedef OpalSimulation Sim_t;

    typedef FixedPisaNsga2< BlendCrossover, IndependentBitMutation > Opt_t;

    typedef CommSplitter< ManyMasterSplit< NoCommTopology > > Comm_t;
    typedef SocialNetworkGraph< NoCommTopology > SolPropagationGraph_t;

196
    typedef Pilot<Input_t, Opt_t, Sim_t, SolPropagationGraph_t, Comm_t> pilot_t;
adelmann's avatar
adelmann committed
197 198 199 200 201 202 203

    // prepare function dictionary and add all available functions in
    // expressions
    functionDictionary_t funcs;
    client::function::type ff;
    ff = FromFile();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
204
                 ("fromFile", ff));
adelmann's avatar
adelmann committed
205 206
    ff = SumErrSq();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
207
                 ("sumErrSq", ff));
adelmann's avatar
adelmann committed
208 209
    ff = SDDSVariable();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
210
                 ("sddsVariableAt", ff));
adelmann's avatar
adelmann committed
211 212 213

    ff = RadialPeak();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
214
                 ("radialPeak", ff));
adelmann's avatar
adelmann committed
215 216
    ff = SumErrSqRadialPeak();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
217
                 ("sumErrSqRadialPeak", ff));
adelmann's avatar
adelmann committed
218 219 220

    ff = ProbeVariable();
    funcs.insert(std::pair<std::string, client::function::type>
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
221
                 ("probVariableWithID", ff));
adelmann's avatar
adelmann committed
222 223 224

    //////////////////////////////////////////////////////////////////////////

iff's avatar
iff committed
225 226
    try {
        CmdArguments_t args(new CmdArguments(argc, argv));
adelmann's avatar
adelmann committed
227

iff's avatar
iff committed
228 229 230 231
        std::string fname = args->getArg<std::string>("inputfile", true);
        ff = sameSDDSVariable(fname);
        funcs.insert(std::pair<std::string, client::function::type>
                     ("sameSDDSVariableAt", ff));
adelmann's avatar
adelmann committed
232

iff's avatar
iff committed
233 234
        boost::shared_ptr<Comm_t>  comm(new Comm_t(args, MPI_COMM_WORLD));
        boost::scoped_ptr<pilot_t> pi(new pilot_t(args, comm, funcs));
adelmann's avatar
adelmann committed
235

iff's avatar
iff committed
236 237 238 239
    } catch (OptPilotException &e) {
        std::cout << "Exception caught: " << e.what() << std::endl;
        MPI_Abort(MPI_COMM_WORLD, -100);
    }
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
240

241 242 243 244 245 246 247 248 249
    Ippl::Comm->barrier();
    Fieldmap::clearDictionary();
    OpalData::deleteInstance();
    delete gmsg;
    delete ippl;
    delete Ippl::Info;
    delete Ippl::Warn;
    delete Ippl::Error;
    delete Ippl::Debug;
iff's avatar
iff committed
250
    return 0;
251 252 253 254
}


int mainOPAL(int argc, char *argv[]) {
255
    Ippl *ippl = new Ippl(argc, argv);
256
    gmsg = new  Inform("OPAL");
gsell's avatar
gsell committed
257

kraus's avatar
kraus committed
258 259
    namespace fs = boost::filesystem;

260
#ifdef ENABLE_AMR
frey_m's avatar
frey_m committed
261
    // false: build no parmparse, we use the OPAL parser instead.
frey_m's avatar
frey_m committed
262
    amrex::Initialize(argc, argv, false, Ippl::getComm());
263 264
#endif

gsell's avatar
gsell committed
265 266
    OPALTimer::Timer simtimer;

267 268
    std::string dateStr(simtimer.date());
    std::string timeStr(simtimer.time());
gsell's avatar
gsell committed
269

270
    H5SetVerbosityLevel(0); //65535);
gsell's avatar
gsell committed
271

272 273
    gsl_set_error_handler(&errorHandlerGSL);

gsell's avatar
gsell committed
274 275 276 277
    static IpplTimings::TimerRef mainTimer = IpplTimings::getTimer("mainTimer");
    IpplTimings::startTimer(mainTimer);

    Inform hmsg("");
278
    std::string mySpace("            ");
gsell's avatar
gsell committed
279 280 281 282 283 284 285 286 287 288 289

    if(Ippl::myNode() == 0) remove("errormsg.txt");

    hmsg << mySpace <<  "   ____  _____       ___ " << endl;
    hmsg << mySpace <<  "  / __ \\|  __ \\ /\\   | | " << endl;
    hmsg << mySpace <<  " | |  | | |__) /  \\  | |" << endl;
    hmsg << mySpace <<  " | |  | |  ___/ /\\ \\ | |" << endl ;
    hmsg << mySpace <<  " | |__| | |  / ____ \\| |____" << endl;
    hmsg << mySpace <<  "  \\____/|_| /_/    \\_\\______|" << endl;


kraus's avatar
kraus committed
290
    *gmsg << endl
291
          << "This is OPAL (Object Oriented Parallel Accelerator Library) Version " << OPAL_VERSION_STR << "\n\n"
kraus's avatar
kraus committed
292
          << "                (c) PSI, http://amas.web.psi.ch" << endl
adelmann's avatar
cleanup  
adelmann committed
293
          << "The optimiser (former opt-Pilot) is integrated " << endl
gsell's avatar
gsell committed
294
          << endl;
295 296

#ifdef OPAL_DKS
297
    *gmsg << "OPAL compiled with DKS (Dynamic Kernel Scheduler) Version "
adelmann's avatar
adelmann committed
298 299 300 301 302
	  << DKS_VERSION;
    if (IpplInfo::DKSEnabled)
      *gmsg << " GPU present" << endl << endl;
    else
      *gmsg << " GPU not present" << endl << endl;
303 304
#endif

kraus's avatar
kraus committed
305 306
    *gmsg << "Please send cookies, goodies or other motivations (wine and beer ... ) \nto the OPAL developers " << PACKAGE_BUGREPORT << "\n" << endl;
    *gmsg << "Time: " << timeStr << " date: " << dateStr << "\n" << endl;
gsell's avatar
gsell committed
307 308 309 310 311 312


    /*
      Make a directory data for some of the output
    */
    if(Ippl::myNode() == 0) {
kraus's avatar
kraus committed
313 314
        if (!fs::exists("data")) {
            boost::system::error_code error_code;
315 316 317 318
            if (!fs::create_directory("data", error_code)) {
                std::cerr << error_code.message() << std::endl;
                // use error code to prevent create_directory from throwing an exception
            }
gsell's avatar
gsell committed
319 320
        }
    }
kraus's avatar
kraus committed
321 322 323 324 325
    Ippl::Comm->barrier();
    if (!fs::is_directory("data")) {
        std::cerr << "unable to create directory; aborting" << std::endl;
        abort();
    }
gsell's avatar
gsell committed
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340

    const OpalParser parser;

    //  DTA
    std::cout.precision(16);
    std::cout.setf(std::ios::scientific, std::ios::floatfield);
    std::cerr.precision(16);
    std::cerr.setf(std::ios::scientific, std::ios::floatfield);
    // /DTA

    // Set global truncation orders.
    FTps<double, 2>::setGlobalTruncOrder(20);
    FTps<double, 4>::setGlobalTruncOrder(15);
    FTps<double, 6>::setGlobalTruncOrder(10);

341
    OpalData *opal = OpalData::getInstance();
342
    opal->storeArguments(argc, argv);
gsell's avatar
gsell committed
343 344 345 346
    try {
        Configure::configure();

        // Read startup file.
kraus's avatar
kraus committed
347
        FileStream::setEcho(Options::echo);
gsell's avatar
gsell committed
348

kraus's avatar
kraus committed
349
        char *startup = getenv("HOME");
Andreas Adelmann's avatar
Andreas Adelmann committed
350 351
	boost::filesystem::path p = strncat(startup, "/init.opal", 20);
	if (startup != NULL && is_regular_file(p)) {
kraus's avatar
kraus committed
352 353 354 355 356 357 358 359 360 361 362

            FileStream::setEcho(false);
            FileStream *is;

            try {
                is = new FileStream(startup);
            } catch(...) {
                is = 0;
                ERRORMSG("Could not open startup file \"" << startup << "\".\n"
                         << "Note: this is not mandatory for an OPAL simulation!\n");
            }
gsell's avatar
gsell committed
363

kraus's avatar
kraus committed
364 365 366 367 368
            if(is) {
                *gmsg << "Reading startup file \"" << startup << "\"." << endl;
                parser.run(is);
                *gmsg << "Finished reading startup file." << endl;
            }
kraus's avatar
kraus committed
369
            FileStream::setEcho(Options::echo);
kraus's avatar
kraus committed
370 371 372
        } else {
            *gmsg << "Couldn't find startup file \"" << startup << "\".\n"
                  << "Note: this is not mandatory for an OPAL simulation!\n" << endl;
gsell's avatar
gsell committed
373 374
        }

375 376 377 378 379 380
        if(argc <= 1) {
            // Run commands from standard input
            parser.run(new TerminalStream("OPAL"));
        } else {
            int arg = -1;
            std::string fname;
381
            std::string restartFileName;
382 383 384 385 386 387
            // if(argc > 3) {
            //     if(argc > 5) {
            //         // will write dumping date into a new h5 file
            for(int ii = 1; ii < argc; ++ ii) {
                std::string argStr = std::string(argv[ii]);
                // The sequence of the two arguments is free
388
                if (argStr == std::string("--input")) {
389 390 391 392
                    ++ ii;
                    arg = ii;
                    INFOMSG(argv[ii] << endl);
                    continue;
393 394
                } else if (argStr == std::string("-restart") ||
                           argStr == std::string("--restart")) {
395 396 397 398
                    opal->setRestartRun();
                    opal->setRestartStep(atoi(argv[++ ii]));
                    opal->setRestartFileName(argv[1]);
                    continue;
399 400
                } else if (argStr == std::string("-restartfn") ||
                           argStr == std::string("--restartfn")) {
401
                    restartFileName = std::string(argv[++ ii]);
402
                    continue;
403 404 405
                } else if (argStr == std::string("-version") ||
                           argStr == std::string("--version")) {
                    INFOMSG("OPAL Version " << OPAL_VERSION_STR << ", git rev. " << Util::getGitRevision() << endl);
406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
                    IpplInfo::printVersion(true);
                    std::string options = (IpplInfo::compileOptions() +
                                           std::string(" ") +
                                           std::string(PACKAGE_COMPILE_OPTIONS) +
                                           std::string(" "));
                    std::set<std::string> uniqOptions;
                    while (options.length() > 0) {
                        size_t n = options.find_first_of(' ');
                        while (n == 0) {
                            options = options.substr(n + 1);
                            n = options.find_first_of(' ');
                        }

                        uniqOptions.insert(options.substr(0, n));
                        options = options.substr(n + 1);
                    }
                    for (auto it: uniqOptions) {
                        options += it + " ";
                    }

                    std::string header("Compile-time options: ");
                    while (options.length() > 58) {
                        std::string line = options.substr(0, 58);
                        size_t n = line.find_last_of(' ');
                        INFOMSG(header << line.substr(0, n) << "\n");
gsell's avatar
gsell committed
431

432 433 434 435 436
                        header = std::string(22, ' ');
                        options = options.substr(n + 1);
                    }
                    INFOMSG(header << options << endl);
                    exit(0);
437 438
                } else if (argStr == std::string("-help") ||
                           argStr == std::string("--help")) {
439
                    IpplInfo::printHelp(argv);
440 441 442 443 444
                    INFOMSG("   --version            : Print a brief version summary.\n");
                    INFOMSG("   --input <fname>      : Specifies the input file <fname>.\n");
                    INFOMSG("   --restart <n>        : Performes a restart from step <n>.\n");
                    INFOMSG("   --restartfn <fname>  : Uses the file <fname> to restart from.\n");
                    INFOMSG("   --help               : Display this command-line summary.\n");
445 446 447 448 449 450 451 452 453 454 455
                    INFOMSG(endl);
                    exit(0);
                } else {
                    if (arg == -1 &&
                        (ii == 1 || ii + 1 == argc) &&
                        argv[ii][0] != '-') {
                        arg = ii;
                        continue;
                    } else {
                        INFOMSG("Unknown argument \"" << argStr << "\"" << endl);
                        IpplInfo::printHelp(argv);
456 457 458 459 460
                        INFOMSG("   --version            : Print a brief version summary.\n");
                        INFOMSG("   --input <fname>      : Specifies the input file <fname>.\n");
                        INFOMSG("   --restart <n>        : Performes a restart from step <n>.\n");
                        INFOMSG("   --restartfn <fname>  : Uses the file <fname> to restart from.\n");
                        INFOMSG("   --help               : Display this command-line summary.\n");
461 462
                        INFOMSG(endl);
                        exit(1);
gsell's avatar
gsell committed
463 464 465 466
                    }
                }
            }

467 468 469 470 471 472 473 474 475 476 477 478 479
            if (arg == -1) {
                INFOMSG("No input file provided!" << endl);
                exit(1);
            }

            fname = std::string(argv[arg]);
            if (!fs::exists(fname)) {
                INFOMSG("Input file \"" << fname << "\" doesn't exist!" << endl);
                exit(1);
            }

            opal->storeInputFn(fname);

480 481 482 483 484 485 486 487 488
            if (opal->inRestartRun()) {
                if (restartFileName == "")
                    restartFileName = opal->getInputBasename() + std::string(".h5");
                if (!fs::exists(restartFileName)) {
                    INFOMSG("Restart file \"" << restartFileName << "\" doesn't exist!" << endl);
                    exit(1);
                }
                opal->setRestartFileName(restartFileName);
            }
489

gsell's avatar
gsell committed
490 491 492
            FileStream *is;

            try {
493
                is = new FileStream(fname);
gsell's avatar
gsell committed
494 495
            } catch(...) {
                is = 0;
496
                *gmsg << "Input file \"" << fname << "\" not found." << endl;
gsell's avatar
gsell committed
497 498 499
            }

            if(is) {
500
                *gmsg << "* Reading input stream \"" << fname << "\"." << endl;
gsell's avatar
gsell committed
501
                parser.run(is);
502
                *gmsg << "* End of input stream \"" << fname << "\"." << endl;
503 504
            }
        }
505

gsell's avatar
gsell committed
506
        IpplTimings::stopTimer(mainTimer);
507

kraus's avatar
kraus committed
508
        IpplTimings::print();
gsell's avatar
gsell committed
509

adelmann's avatar
adelmann committed
510 511
	IpplTimings::print(std::string("timing.dat"),
                           OpalData::getInstance()->getProblemCharacteristicValues());
512

gsell's avatar
gsell committed
513
        if(Ippl::myNode() == 0) {
514
            std::ifstream errormsg("errormsg.txt");
gsell's avatar
gsell committed
515 516
            if(errormsg.good()) {
                char buffer[256];
517
                std::string closure("                                                                                 *\n");
kraus's avatar
kraus committed
518 519 520 521 522
                ERRORMSG("\n"
                         << "* **********************************************************************************\n"
                         << "* ************** W A R N I N G / E R R O R * * M E S S A G E S *********************\n"
                         << "* **********************************************************************************"
                         << endl);
gsell's avatar
gsell committed
523 524
                errormsg.getline(buffer, 256);
                while(errormsg.good()) {
kraus's avatar
kraus committed
525
                    ERRORMSG("* ");
gsell's avatar
gsell committed
526
                    if(errormsg.gcount() == 1) {
kraus's avatar
kraus committed
527
                        ERRORMSG(closure);
528
                    } else if ((size_t)errormsg.gcount() <= closure.size()) {
kraus's avatar
kraus committed
529
                        ERRORMSG(buffer << closure.substr(errormsg.gcount() - 1));
530
                    } else {
kraus's avatar
kraus committed
531
                        ERRORMSG(buffer << endl);
gsell's avatar
gsell committed
532 533 534
                    }
                    errormsg.getline(buffer, 256);
                }
kraus's avatar
kraus committed
535 536 537 538
                ERRORMSG("* " << closure
                         << "* **********************************************************************************\n"
                         << "* **********************************************************************************"
                         << endl);
gsell's avatar
gsell committed
539 540 541 542 543
            }
            errormsg.close();
        }

        Ippl::Comm->barrier();
adelmann's avatar
adelmann committed
544
	Fieldmap::clearDictionary();
gsell's avatar
gsell committed
545 546 547
        OpalData::deleteInstance();
        delete gmsg;
        delete ippl;
548 549 550 551
        delete Ippl::Info;
        delete Ippl::Warn;
        delete Ippl::Error;
        delete Ippl::Debug;
gsell's avatar
gsell committed
552 553
        return 0;

554 555 556 557 558 559 560 561 562 563 564 565 566 567 568
    } catch(OpalException &ex) {
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n*** User error detected by function \""
                 << ex.where() << "\"\n";
        // stat->printWhere(errorMsg, true);
        std::string what = ex.what();
        size_t pos = what.find_first_of('\n');
        do {
            errorMsg << "    " << what.substr(0, pos) << endl;
            what = what.substr(pos + 1, std::string::npos);
            pos = what.find_first_of('\n');
        } while (pos != std::string::npos);
        errorMsg << "    " << what << endl;

        MPI_Abort(MPI_COMM_WORLD, -100);
gsell's avatar
gsell committed
569
    } catch(ClassicException &ex) {
570 571 572 573 574 575 576 577 578 579 580 581
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n*** User error detected by function \""
                 << ex.where() << "\"\n";
        // stat->printWhere(errorMsg, true);
        std::string what = ex.what();
        size_t pos = what.find_first_of('\n');
        do {
            errorMsg << "    " << what.substr(0, pos) << endl;
            what = what.substr(pos + 1, std::string::npos);
            pos = what.find_first_of('\n');
        } while (pos != std::string::npos);
        errorMsg << "    " << what << endl;
582

583 584 585 586 587 588 589
        MPI_Abort(MPI_COMM_WORLD, -100);
    } catch(std::bad_alloc &ex) {
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n*** Error:\n";
        errorMsg << "    Sorry, virtual memory exhausted.\n"
                 << ex.what()
                 << endl;
adelmann's avatar
adelmann committed
590

591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
        MPI_Abort(MPI_COMM_WORLD, -100);
    } catch(assertion &ex) {
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n*** Runtime-error ******************\n";
        std::string what = ex.what();
        size_t pos = what.find_first_of('\n');
        do {
            errorMsg << "    " << what.substr(0, pos) << endl;
            what = what.substr(pos + 1, std::string::npos);
            pos = what.find_first_of('\n');
        } while (pos != std::string::npos);
        errorMsg << "    " << what << endl;

        errorMsg << "\n************************************\n" << endl;
        throw std::runtime_error("in Parser");
    } catch(std::exception &ex) {
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n"
                 << "*** Error:\n"
                 << "    Internal OPAL error: \n";
        std::string what = ex.what();
        size_t pos = what.find_first_of('\n');
        do {
            errorMsg << "    " << what.substr(0, pos) << endl;
            what = what.substr(pos + 1, std::string::npos);
            pos = what.find_first_of('\n');
        } while (pos != std::string::npos);
        errorMsg << "    " << what << endl;
619

620
        MPI_Abort(MPI_COMM_WORLD, -100);
gsell's avatar
gsell committed
621
    } catch(...) {
622 623 624 625 626
        Inform errorMsg("Error", std::cerr, INFORM_ALL_NODES);
        errorMsg << "\n*** Error:\n"
                 << "    Unexpected exception caught.\n" << endl;

        MPI_Abort(MPI_COMM_WORLD, -100);
gsell's avatar
gsell committed
627
    }
628 629

    return 1;
gsell's avatar
gsell committed
630
}
631 632 633 634 635 636

void errorHandlerGSL(const char *reason,
                     const char *file,
                     int,
                     int) {
    throw OpalException(file, reason);
637 638 639 640 641 642 643 644
}



// The OPAL main program.

int main(int argc, char *argv[]) {

Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
645
    int res;
646

647
    // if ((argc <= 1) || !haveOptimiseRun(argc, argv))
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
648
        res = mainOPAL(argc, argv);
649 650
    // else
    //     res = mainOPALOptimiser(argc, argv);
Christof Metzger-Kraus's avatar
Christof Metzger-Kraus committed
651
    return res;
652

653
}