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