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 |
|
---|
20 | #include "CodePatterns/MemDebug.hpp"
|
---|
21 |
|
---|
22 | #include <iomanip>
|
---|
23 | #include <iostream>
|
---|
24 |
|
---|
25 | #include "config.hpp"
|
---|
26 | #include "molecule.hpp"
|
---|
27 | #include "World.hpp"
|
---|
28 |
|
---|
29 | #include "Actions/ActionExceptions.hpp"
|
---|
30 | #include "Actions/ActionHistory.hpp"
|
---|
31 | #include "Actions/ActionRegistry.hpp"
|
---|
32 |
|
---|
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"
|
---|
39 | #include "RandomNumbers/RandomNumberDistributionFactory.hpp"
|
---|
40 | #include "RandomNumbers/RandomNumberEngineFactory.hpp"
|
---|
41 | #include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
|
---|
42 | #include "Tesselation/tesselationhelpers.hpp"
|
---|
43 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
44 | #include "UIElements/CommandLineUI/CommandLineUIFactory.hpp"
|
---|
45 | #include "UIElements/CommandLineUI/CommandLineParser.hpp"
|
---|
46 | #include "UIElements/Dialog.hpp"
|
---|
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"
|
---|
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;
|
---|
70 | cout << endl;
|
---|
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 |
|
---|
84 | setVerbosity(1);
|
---|
85 | // need to init the history before any action is created
|
---|
86 | MoleCuilder::ActionHistory::init();
|
---|
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
|
---|
103 | if (boost::filesystem::exists(BondGraphFileName)) {
|
---|
104 | std::ifstream input(BondGraphFileName.c_str());
|
---|
105 | if ((input.good()) && (World::getInstance().getBondGraph()->LoadBondLengthTable(input))) {
|
---|
106 | DoLog(0) && (Log() << Verbose(0) << "Bond length table loaded successfully." << endl);
|
---|
107 | } else {
|
---|
108 | DoeLog(1) && (eLog()<< Verbose(1) << "Bond length table loading failed." << endl);
|
---|
109 | }
|
---|
110 | input.close();
|
---|
111 | }
|
---|
112 | // handle remaining arguments by CommandLineParser
|
---|
113 | if (argc>1) {
|
---|
114 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to CommandLine." << endl);
|
---|
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
|
---|
123 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to Qt4." << endl);
|
---|
124 | UIFactory::registerFactory(new QtUIFactory::description());
|
---|
125 | UIFactory::makeUserInterface("Qt4");
|
---|
126 | #else
|
---|
127 | DoLog(0) && (Log() << Verbose(0) << "Setting UI to Text." << endl);
|
---|
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();
|
---|
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 | }
|
---|
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 |
|
---|
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 |
|
---|
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 | {
|
---|
191 | printTimings();
|
---|
192 | Chronos::purgeInstance();
|
---|
193 |
|
---|
194 | RandomNumberDistributionFactory::purgeInstance();
|
---|
195 | RandomNumberEngineFactory::purgeInstance();
|
---|
196 | RandomNumberGeneratorFactory::purgeInstance();
|
---|
197 | FormatParserStorage::purgeInstance();
|
---|
198 | ChangeTracker::purgeInstance();
|
---|
199 | World::purgeInstance();
|
---|
200 | MenuDescription::purgeInstance();
|
---|
201 | UIFactory::purgeInstance();
|
---|
202 | ValueStorage::purgeInstance();
|
---|
203 | CommandLineParser::purgeInstance();
|
---|
204 | MoleCuilder::ActionRegistry::purgeInstance();
|
---|
205 | MoleCuilder::OptionRegistry::purgeInstance();
|
---|
206 | MoleCuilder::ActionHistory::purgeInstance();
|
---|
207 | // we have to remove these two static as otherwise their boost::shared_ptrs are still present
|
---|
208 | MoleCuilder::Action::removeStaticStateEntities();
|
---|
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 | }
|
---|