source: src/cleanUp.cpp@ 43552e

Candidate_v1.6.1
Last change on this file since 43552e was d8c6c7, checked in by Frederik Heber <frederik.heber@…>, 8 years ago

Added GeometryObject and GeometryRegistry.

  • Property mode set to 100644
File size: 7.5 KB
RevLine 
[949953]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[949953]22 */
23
24/*
25 * cleanUp.cpp
26 *
27 * Created on: Oct 28, 2011
28 * Author: heber
29 */
30
31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
36#include <iostream>
37#include <iomanip>
38
[9eb71b3]39//#include "CodePatterns/MemDebug.hpp"
[949953]40
41#include "cleanUp.hpp"
42
[c9c3c9]43#include "Atom/AtomObserver.hpp"
44
[949953]45#include "CodePatterns/Chronos.hpp"
46#include "CodePatterns/errorlogger.hpp"
47#include "CodePatterns/logger.hpp"
[856d05]48#include "CodePatterns/Observer/ObserverLog.hpp"
[574d377]49#include "CodePatterns/Observer/GlobalObservableInfo.hpp"
[949953]50
[628577]51#include "Actions/ActionQueue.hpp"
[949953]52#include "Actions/OptionRegistry.hpp"
53
[7f8c9a]54#include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
55
[d8c6c7]56#include "Geometry/GeometryRegistry.hpp"
57
[5d8f4f]58#include "MoleculeObserver.hpp"
59
[96c7d2]60#include "Potentials/PotentialFactory.hpp"
61#include "Potentials/PotentialRegistry.hpp"
62
[949953]63#include "RandomNumbers/RandomNumberDistributionFactory.hpp"
64#include "RandomNumbers/RandomNumberEngineFactory.hpp"
65#include "RandomNumbers/RandomNumberGeneratorFactory.hpp"
66
67#include "Parser/ChangeTracker.hpp"
68#include "Parser/FormatParserStorage.hpp"
[856d05]69#include "Parser/XmlParser.hpp"
[949953]70
[ff9e5a]71#include "Shapes/ShapeFactory.hpp"
72#include "Shapes/ShapeRegistry.hpp"
73
[949953]74#include "UIElements/CommandLineUI/CommandLineParser.hpp"
75#include "UIElements/Menu/MenuDescription.hpp"
76#include "UIElements/UIFactory.hpp"
77#include "World.hpp"
[856d05]78#include "WorldTime.hpp"
[949953]79
80/** In this function all dynamicly allocated member variables to static/global
81 * variables are added to the ignore list of Memory/MemDebug.
82 *
83 * Use this to prevent their listing in the Memory::getState() at the end of the
84 * program. Check with valgrind that truely no memory leak occurs!
85 */
86void AddStaticEntitiestoIgnoreList()
87{
[856d05]88 // XmlParser has static instance that uses vectorContent as well
[9eb71b3]89 //Memory::ignore(FormatParser< xml >::defaultAtomInfo.field.get());
[949953]90}
91
[567357]92/** purges all static (singleton) instances in correct order of dependency.
93 *
[949953]94 */
[567357]95void purgeStaticInstances()
[949953]96{
[415ddd]97 // make sure that ActionQueue is already purged!
[7f8c9a]98 FragmentationResultContainer::purgeInstance();
[d8c6c7]99 GeometryRegistry::purgeInstance();
[949953]100 Chronos::purgeInstance();
[96c7d2]101 PotentialFactory::purgeInstance();
102 PotentialRegistry::purgeInstance();
[949953]103 RandomNumberDistributionFactory::purgeInstance();
104 RandomNumberEngineFactory::purgeInstance();
105 RandomNumberGeneratorFactory::purgeInstance();
[ff9e5a]106 ShapeFactory::purgeInstance();
107 ShapeRegistry::purgeInstance();
[949953]108 FormatParserStorage::purgeInstance();
109 ChangeTracker::purgeInstance();
110 World::purgeInstance();
[c9c3c9]111 AtomObserver::purgeInstance();
[5d8f4f]112 MoleculeObserver::purgeInstance();
[949953]113 MenuDescription::purgeInstance();
114 UIFactory::purgeInstance();
115 CommandLineParser::purgeInstance();
[567357]116 logger::purgeInstance();
117 errorLogger::purgeInstance();
[856d05]118 WorldTime::purgeInstance();
[567357]119}
120
[415ddd]121void stopAndPurgeQueue()
122{
[74459a]123#ifdef HAVE_ACTION_THREAD
[415ddd]124 // adding a wait here such that ActionQueue's run_thread is done any may stop
125 waitQueue();
[74459a]126#endif
[415ddd]127 MoleCuilder::ActionQueue::purgeInstance();
128 MoleCuilder::OptionRegistry::purgeInstance();
129}
130
131void printTimings(const MoleCuilder::ActionQueue::ActionTokens_t &tokens);
132
[567357]133/** Cleans all singleton instances in an orderly fashion.
134 * C++ does not guarantee any specific sequence of removal of single instances
135 * which have static/global variables. Some singletons depend on others hence we
[415ddd]136 * ascertain a specific ordering here, which is is used via the atexit() hook.
[567357]137 */
138void cleanUp()
139{
[415ddd]140 // stop queue
141 const MoleCuilder::ActionQueue::ActionTokens_t tokens = MoleCuilder::ActionQueue::getInstance().getListOfActions();
142 stopAndPurgeQueue();
143 // give timings per Action, requires still present Chronos
144 printTimings(tokens);
145
[567357]146 // purge static instances from memory
147 purgeStaticInstances();
[949953]148 // put some static variables' dynamic contents on the Memory::ignore map to avoid their
149 // admonishing lateron
150 AddStaticEntitiestoIgnoreList();
151#ifdef LOG_OBSERVER
152 cout << observerLog().getLog();
[856d05]153#endif
154#ifndef NDEBUG
[708277]155 // CodePatterns always initializes observerLog except with NDEBUG
[856d05]156 ObserverLog::purgeInstance();
[574d377]157 GlobalObservableInfo::purgeInstance();
[949953]158#endif
[9eb71b3]159 //Memory::getState();
[949953]160}
161
162/** We give a list of all times per action and a total time.
163 *
[415ddd]164 * We require a list of Action tokens such that we do not access the ActionQueue.
165 * Timings are printed when Actions have all been performed, hence the queue should
166 * already be purged and we cannot access it anymore.
167 *
168 * \param tokens
[949953]169 */
[415ddd]170void printTimings(const MoleCuilder::ActionQueue::ActionTokens_t &tokens)
[949953]171{
[415ddd]172// const MoleCuilder::ActionQueue::ActionTokens_t tokens = MoleCuilder::ActionQueue::getInstance().getListOfActions();
[949953]173 const Chronos &Chron = Chronos::getInstance();
[3b0d2a]174 if (!DoLog(2)) {
175 std::cout << "(Non-zero) Times used per Action [seconds]:" << std::endl;
[690741]176 for (MoleCuilder::ActionQueue::ActionTokens_t::const_iterator iter = tokens.begin();
177 iter != tokens.end(); ++iter)
178 if (Chron.getTime(*iter) != 0.) { // dont give if action has not been used
179 std::cout << " " << setiosflags(ios::left) << setw(24) << setfill('.') << *iter;
[3b0d2a]180 std::cout << setiosflags(ios::left)
181#ifdef HAVE_SYS_TIMES_H
182 << setprecision(3)
183#else
184 << setprecision(9)
185#endif
[690741]186 << fixed << Chron.getTime(*iter) << std::endl;
[3b0d2a]187 }
188 } else {
189 std::cout << "Measured times [seconds]:" << std::endl;
190 const Chronos::TimekeepingMap &timemap = Chron.getTimekeepingMap();
191 for (Chronos::TimekeepingMap::const_iterator iter = timemap.begin();
192 iter != timemap.end();
193 ++iter) {
[949953]194 std::cout << " " << setiosflags(ios::left) << setw(24) << setfill('.') << iter->first;
[ddeed2]195 std::cout << setiosflags(ios::left)
196#ifdef HAVE_SYS_TIMES_H
197 << setprecision(3)
198#else
199 << setprecision(9)
200#endif
[3b0d2a]201 << fixed << iter->second << std::endl;
[949953]202 }
[3b0d2a]203 }
[949953]204 std::cout << "Total Time: " << Chron.SumUpTotalTime() << " seconds" << std::endl;
205 std::cout << "Total Actions called: " << Chron.SumUpTotalFunctions() << std::endl;
206}
[b5d38d]207
208/** Dump current memory chunks.
209 *
210 */
211void dumpMemory()
212{
[9eb71b3]213 //ofstream ost("molecuilder.memdump");
214 //Memory::dumpMemory(ost);
[b5d38d]215}
216
217/** Save the current World to output files and exit.
218 *
219 * @return retrieved from World::getExitFlag()
220 */
221int saveAll()
222{
223 FormatParserStorage::getInstance().SaveAll();
224 ChangeTracker::getInstance().saveStatus();
225
226 int ExitFlag = World::getInstance().getExitFlag();
227 return (ExitFlag == 1 ? 0 : ExitFlag);
228}
[415ddd]229
[74459a]230#ifdef HAVE_ACTION_THREAD
[415ddd]231/** Stops the queue such that all Actions are done.
232 *
233 */
234void stopQueue()
235{
236 MoleCuilder::ActionQueue::getInstance().stop();
237}
238
239/** Waits for the queue to idle.
240 *
241 */
242void waitQueue()
243{
244 MoleCuilder::ActionQueue::getInstance().wait();
245}
[74459a]246#endif
Note: See TracBrowser for help on using the repository browser.