[f4d063] | 1 | /*
|
---|
| 2 | * Project: MoleCuilder
|
---|
| 3 | * Description: creates and alters molecular systems
|
---|
| 4 | * Copyright (C) 2010 University of Bonn. All rights reserved.
|
---|
| 5 | * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
|
---|
| 6 | */
|
---|
| 7 |
|
---|
| 8 | /*
|
---|
| 9 | * builder_init.cpp
|
---|
| 10 | *
|
---|
| 11 | * Created on: Dec 15, 2010
|
---|
| 12 | * Author: heber
|
---|
| 13 | */
|
---|
| 14 |
|
---|
| 15 | // include config.h
|
---|
| 16 | #ifdef HAVE_CONFIG_H
|
---|
| 17 | #include <config.h>
|
---|
| 18 | #endif
|
---|
| 19 |
|
---|
[ad011c] | 20 | #include "CodePatterns/MemDebug.hpp"
|
---|
[f4d063] | 21 |
|
---|
[862b6a] | 22 | #include <iomanip>
|
---|
[4e855e] | 23 | #include <iostream>
|
---|
| 24 |
|
---|
[f4d063] | 25 | #include "config.hpp"
|
---|
| 26 | #include "molecule.hpp"
|
---|
| 27 | #include "World.hpp"
|
---|
| 28 |
|
---|
[e69c87] | 29 | #include "Actions/ActionExceptions.hpp"
|
---|
[f4d063] | 30 | #include "Actions/ActionHistory.hpp"
|
---|
[e69c87] | 31 | #include "Actions/ActionRegistry.hpp"
|
---|
[f4d063] | 32 |
|
---|
[862b6a] | 33 | #include "CodePatterns/Chronos.hpp"
|
---|
| 34 | #include "CodePatterns/Log.hpp"
|
---|
| 35 | #include "Element/periodentafel.hpp"
|
---|
| 36 | #include "Graph/BondGraph.hpp"
|
---|
| 37 | #include "Parser/ChangeTracker.hpp"
|
---|
| 38 | #include "Parser/FormatParserStorage.hpp"
|
---|
[1d5a871] | 39 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp"
|
---|
| 40 | #include "RandomNumbers/RandomNumberEngineFactory.hpp"
|
---|
[3f9eba] | 41 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
[862b6a] | 42 | #include "Tesselation/tesselationhelpers.hpp"
|
---|
[f4d063] | 43 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
[862b6a] | 44 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
| 45 | #include "UIElements/CommandLineUI/CommandLineParser.hpp"
|
---|
[f4d063] | 46 | #include "UIElements/Dialog.hpp"
|
---|
[862b6a] | 47 | #include "UIElements/MainWindow.hpp"
|
---|
| 48 | #include "UIElements/Menu/MenuDescription.hpp"
|
---|
| 49 | #ifdef USE_GUI_QT
|
---|
| 50 | #include "UIElements/Qt4/QtUIFactory.hpp"
|
---|
| 51 | #endif
|
---|
| 52 | #include "UIElements/TextUI/TextUIFactory.hpp"
|
---|
| 53 | #include "UIElements/UIFactory.hpp"
|
---|
[f4d063] | 54 |
|
---|
| 55 | #include "version.h"
|
---|
| 56 |
|
---|
| 57 | #include "builder_init.hpp"
|
---|
| 58 |
|
---|
| 59 | /** Print some initial information output the program.
|
---|
| 60 | *
|
---|
| 61 | */
|
---|
| 62 | void ProgramHeader()
|
---|
| 63 | {
|
---|
| 64 | // print version check and copyright notice
|
---|
| 65 | cout << MOLECUILDERVERSION << endl;
|
---|
| 66 | cout << "MoleCuilder comes with ABSOLUTELY NO WARRANTY; for details type" << endl;
|
---|
| 67 | cout << "`molecuilder --warranty'." << endl;
|
---|
| 68 | cout << "`MoleCuilder - to create and alter molecular systems." << endl;
|
---|
| 69 | cout << "Copyright (C) 2010 University Bonn. All rights reserved." << endl;
|
---|
[ad7270] | 70 | cout << endl;
|
---|
[f4d063] | 71 | }
|
---|
| 72 |
|
---|
| 73 | /** General stuff to intialize before UI.
|
---|
| 74 | *
|
---|
| 75 | */
|
---|
| 76 | void initGeneral()
|
---|
| 77 | {
|
---|
| 78 | // while we are non interactive, we want to abort from asserts
|
---|
| 79 | ASSERT_DO(Assert::Abort);
|
---|
| 80 | ASSERT_HOOK(dumpMemory);
|
---|
| 81 |
|
---|
| 82 | ProgramHeader();
|
---|
| 83 |
|
---|
[ad7270] | 84 | setVerbosity(1);
|
---|
[f4d063] | 85 | // need to init the history before any action is created
|
---|
[ce7fdc] | 86 | MoleCuilder::ActionHistory::init();
|
---|
[f4d063] | 87 |
|
---|
| 88 | // from this moment on, we need to be sure to deeinitialize in the correct order
|
---|
| 89 | // this is handled by the cleanup function
|
---|
| 90 | atexit(cleanUp);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
| 93 | /** Initialize specific UIFactory.
|
---|
| 94 | *
|
---|
| 95 | * @param argc argument count
|
---|
| 96 | * @param argv argument array
|
---|
| 97 | */
|
---|
| 98 | void initUI(int argc, char **argv)
|
---|
| 99 | {
|
---|
| 100 | std::string BondGraphFileName("\n");
|
---|
| 101 | // Parse command line options and if present create respective UI
|
---|
| 102 | // construct bond graph
|
---|
[f71baf] | 103 | if (boost::filesystem::exists(BondGraphFileName)) {
|
---|
| 104 | std::ifstream input(BondGraphFileName.c_str());
|
---|
| 105 | if ((input.good()) && (World::getInstance().getBondGraph()->LoadBondLengthTable(input))) {
|
---|
[47d041] | 106 | LOG(0, "Bond length table loaded successfully.");
|
---|
[f71baf] | 107 | } else {
|
---|
[47d041] | 108 | ELOG(1, "Bond length table loading failed.");
|
---|
[f4d063] | 109 | }
|
---|
[f71baf] | 110 | input.close();
|
---|
[f4d063] | 111 | }
|
---|
| 112 | // handle remaining arguments by CommandLineParser
|
---|
| 113 | if (argc>1) {
|
---|
[47d041] | 114 | LOG(0, "Setting UI to CommandLine.");
|
---|
[f4d063] | 115 | CommandLineParser::getInstance().InitializeCommandArguments();
|
---|
| 116 | CommandLineParser::getInstance().Run(argc,argv);
|
---|
| 117 | UIFactory::registerFactory(new CommandLineUIFactory::description());
|
---|
| 118 | UIFactory::makeUserInterface("CommandLine");
|
---|
| 119 | } else {
|
---|
| 120 | // In the interactive mode, we can leave the user the choice in case of error
|
---|
| 121 | ASSERT_DO(Assert::Ask);
|
---|
| 122 | #ifdef USE_GUI_QT
|
---|
[47d041] | 123 | LOG(0, "Setting UI to Qt4.");
|
---|
[f4d063] | 124 | UIFactory::registerFactory(new QtUIFactory::description());
|
---|
| 125 | UIFactory::makeUserInterface("Qt4");
|
---|
| 126 | #else
|
---|
[47d041] | 127 | LOG(0, "Setting UI to Text.");
|
---|
[f4d063] | 128 | cout << MOLECUILDERVERSION << endl;
|
---|
| 129 | UIFactory::registerFactory(new TextUIFactory::description());
|
---|
| 130 | UIFactory::makeUserInterface("Text");
|
---|
| 131 | #endif
|
---|
| 132 | }
|
---|
| 133 | }
|
---|
| 134 |
|
---|
| 135 | /** Create MainWindow and displays.
|
---|
| 136 | * I.e. here all the Actions are parsed and done.
|
---|
| 137 | */
|
---|
| 138 | void doUI()
|
---|
| 139 | {
|
---|
| 140 | MainWindow *mainWindow = UIFactory::getInstance().makeMainWindow();
|
---|
[e69c87] | 141 | try {
|
---|
| 142 | mainWindow->display();
|
---|
| 143 | } catch(ActionFailureException &e) {
|
---|
| 144 | std::cerr << "Action " << *boost::get_error_info<ActionNameString>(e) << " has failed." << std::endl;
|
---|
| 145 | World::getInstance().setExitFlag(5);
|
---|
| 146 | }
|
---|
[f4d063] | 147 | delete mainWindow;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 |
|
---|
| 151 | /** In this function all dynamicly allocated member variables to static/global
|
---|
| 152 | * variables are added to the ignore list of Memory/MemDebug.
|
---|
| 153 | *
|
---|
| 154 | * Use this to prevent their listing in the Memory::getState() at the end of the
|
---|
| 155 | * program. Check with valgrind that truely no memory leak occurs!
|
---|
| 156 | */
|
---|
| 157 | void AddStaticEntitiestoIgnoreList()
|
---|
| 158 | {
|
---|
| 159 | // zeroVec and unitVec are global variables (on the stack) but vectorContent
|
---|
| 160 | // within is situated on the heap and has to be ignored
|
---|
| 161 | Memory::ignore(zeroVec.get());
|
---|
| 162 | Memory::ignore(unitVec[0].get());
|
---|
| 163 | Memory::ignore(unitVec[1].get());
|
---|
| 164 | Memory::ignore(unitVec[2].get());
|
---|
| 165 | }
|
---|
| 166 |
|
---|
[862b6a] | 167 | /** We give a list of all times per action and a total time.
|
---|
| 168 | *
|
---|
| 169 | */
|
---|
| 170 | void printTimings()
|
---|
| 171 | {
|
---|
| 172 | const MoleCuilder::ActionRegistry &AR = MoleCuilder::ActionRegistry::getInstance();
|
---|
| 173 | const Chronos &Chron = Chronos::getInstance();
|
---|
| 174 | std::cout << "(Non-zero) Times used per Action [seconds]:" << std::endl;
|
---|
| 175 | for (MoleCuilder::ActionRegistry::const_iterator iter = AR.getBeginIter(); iter != AR.getEndIter(); ++iter)
|
---|
| 176 | if (Chron.getTime(iter->first) != 0.) { // dont give if action has not been used
|
---|
| 177 | std::cout << " " << setiosflags(ios::left) << setw(24) << setfill('.') << iter->first;
|
---|
| 178 | std::cout << setiosflags(ios::left) << setprecision(6) << fixed << Chron.getTime(iter->first) << std::endl;
|
---|
| 179 | }
|
---|
| 180 | std::cout << "Total Time: " << Chron.SumUpTotalTime() << " seconds" << std::endl;
|
---|
| 181 | std::cout << "Total Actions called: " << Chron.SumUpTotalFunctions() << std::endl;
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[f4d063] | 184 | /** Cleans all singleton instances in an orderly fashion.
|
---|
| 185 | * C++ does not guarantee any specific sequence of removal of single instances
|
---|
| 186 | * which have static/global variables. Some singletons depend on others hence we
|
---|
| 187 | * acertain a specific ordering here, which is is used via the atexit() hook.
|
---|
| 188 | */
|
---|
| 189 | void cleanUp()
|
---|
| 190 | {
|
---|
[862b6a] | 191 | printTimings();
|
---|
| 192 | Chronos::purgeInstance();
|
---|
| 193 |
|
---|
[c9bc2b7] | 194 | RandomNumberDistributionFactory::purgeInstance();
|
---|
| 195 | RandomNumberEngineFactory::purgeInstance();
|
---|
[3f9eba] | 196 | RandomNumberGeneratorFactory::purgeInstance();
|
---|
[f4d063] | 197 | FormatParserStorage::purgeInstance();
|
---|
| 198 | ChangeTracker::purgeInstance();
|
---|
| 199 | World::purgeInstance();
|
---|
| 200 | MenuDescription::purgeInstance();
|
---|
| 201 | UIFactory::purgeInstance();
|
---|
| 202 | ValueStorage::purgeInstance();
|
---|
| 203 | CommandLineParser::purgeInstance();
|
---|
[ce7fdc] | 204 | MoleCuilder::ActionRegistry::purgeInstance();
|
---|
| 205 | MoleCuilder::OptionRegistry::purgeInstance();
|
---|
| 206 | MoleCuilder::ActionHistory::purgeInstance();
|
---|
[f4d063] | 207 | // we have to remove these two static as otherwise their boost::shared_ptrs are still present
|
---|
[ce7fdc] | 208 | MoleCuilder::Action::removeStaticStateEntities();
|
---|
[f4d063] | 209 | // put some static variables' dynamic contents on the Memory::ignore map to avoid their
|
---|
| 210 | // admonishing lateron
|
---|
| 211 | AddStaticEntitiestoIgnoreList();
|
---|
| 212 | logger::purgeInstance();
|
---|
| 213 | errorLogger::purgeInstance();
|
---|
| 214 | #ifdef LOG_OBSERVER
|
---|
| 215 | cout << observerLog().getLog();
|
---|
| 216 | #endif
|
---|
| 217 | Memory::getState();
|
---|
| 218 | }
|
---|
| 219 |
|
---|
| 220 | /** Dump current memory chunks.
|
---|
| 221 | *
|
---|
| 222 | */
|
---|
| 223 | void dumpMemory()
|
---|
| 224 | {
|
---|
| 225 | ofstream ost("molecuilder.memdump");
|
---|
| 226 | Memory::dumpMemory(ost);
|
---|
| 227 | }
|
---|
| 228 |
|
---|
| 229 | /** Save the current World to output files and exit.
|
---|
| 230 | *
|
---|
| 231 | * @return retrieved from World::getExitFlag()
|
---|
| 232 | */
|
---|
| 233 | int saveAll()
|
---|
| 234 | {
|
---|
| 235 | FormatParserStorage::getInstance().SaveAll();
|
---|
| 236 | ChangeTracker::getInstance().saveStatus();
|
---|
| 237 |
|
---|
| 238 | int ExitFlag = World::getInstance().getExitFlag();
|
---|
| 239 | return (ExitFlag == 1 ? 0 : ExitFlag);
|
---|
| 240 | }
|
---|