source: src/RandomNumbers/RandomNumberGeneratorFactory.cpp@ c9bc2b7

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 Candidate_v1.7.0 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since c9bc2b7 was c9bc2b7, checked in by Frederik Heber <heber@…>, 15 years ago

Bigger rewrite of RandomNumbers ... due to cloning of prototypes.

  • in order to copy the prototypes and not use the original instance, we have implemented a new pattern called "Creator" that wraps a templated class in a creation interface class such that this abstract class can instaniate the particular types (see here http://stackoverflow.com/questions/3506026/c-clone-abstract-base-class-without-meddling-with-derived). This is also wrapped up in ..._Creator.hpp files.
  • meanwhile we also tried to have factories for Engine and Distribution but this failed due to boost::variate_operator<> having to know the particular types of engine and distribution. One could go forth with some cast's but this is very unclean and not what we want. ... Downside is that we can't set global options for engines or distributions as of now.
  • For the moment Engine- and DistributionFactory both have a prototype table, i.e. the same wrapped ones as above, but they are not used for the GeneratorFactory.
  • Property mode set to 100644
File size: 5.6 KB
Line 
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 * RandomNumberGeneratorFactory.cpp
10 *
11 * Created on: Dec 31, 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 <utility>
23#include "CodePatterns/Singleton_impl.hpp"
24#include "CodePatterns/Assert.hpp"
25
26#include "TemplatePowerSetGenerator.hpp"
27#include "EmptyPrototypeTable.hpp"
28
29#include "RandomNumberEngineFactory.hpp"
30#include "RandomNumberDistributionFactory.hpp"
31#include "RandomNumberGenerator_Encapsulation.hpp"
32
33#include <boost/preprocessor/facilities/empty.hpp>
34#include <boost/preprocessor/punctuation/paren.hpp>
35#include <boost/preprocessor/seq/for_each_product.hpp>
36#include <boost/preprocessor/facilities/identity.hpp>
37#include <boost/preprocessor/facilities/expand.hpp>
38#include <boost/preprocessor/seq/seq.hpp>
39
40
41#include "RandomNumberGeneratorFactory.hpp"
42#include "RandomNumberDistributionFactory.def"
43#include "RandomNumberEngineFactory.def"
44
45enum RandomNumberDistributionFactory::Distribution RandomNumberGeneratorFactory::distribution = (enum RandomNumberDistributionFactory::Distribution) 0;
46enum RandomNumberEngineFactory::Engine RandomNumberGeneratorFactory::engine = (enum RandomNumberEngineFactory::Engine) 0;
47RandomNumberGeneratorFactory::EngineDistributionTable RandomNumberGeneratorFactory::GeneratorPrototypeTable;
48
49RandomNumberGeneratorFactory::RandomNumberGeneratorFactory()
50{
51 FillPrototypeTable();
52}
53
54RandomNumberGeneratorFactory::~RandomNumberGeneratorFactory()
55{
56 // clear out factories map to allow boost::shared_ptr to do their work (i.e. to release mem)
57 // this is necessary as factories is a object
58 for (EngineDistributionTable::iterator iter = GeneratorPrototypeTable.begin();
59 !GeneratorPrototypeTable.empty();
60 iter = GeneratorPrototypeTable.begin()) {
61 EmptyPrototypeTable< std::map<enum RandomNumberDistributionFactory::Distribution,IRandomNumberGenerator_Creator *> > (iter->second);
62 GeneratorPrototypeTable.erase(iter);
63 }
64 GeneratorPrototypeTable.clear();
65}
66
67void RandomNumberGeneratorFactory::FillPrototypeTable()
68{
69 // fill GeneratorPrototypeTable
70#define SequenceElementizer(z,data) (data)
71
72#define distributionengine_seqseq \
73 BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq)(distribution_seq))
74#define distributionengine_seqseq_a \
75 BOOST_PP_SEQ_FOR_EACH_PRODUCT(SequenceElementizer, (engine_seq_a)(distribution_seq))
76
77#define suffixseq ()(<> > )
78#define tableseq (*EnginePrototypeTable)(*DistributionPrototypeTable)
79#define name_spaces_seq (RandomNumberEngineFactory::)(RandomNumberDistributionFactory::)
80
81#define size_tupels BOOST_PP_SEQ_SIZE(tableseq)
82
83#define TableItemPrinter(z,n,sequence) \
84 [ \
85 BOOST_PP_SEQ_ELEM(n,name_spaces_seq) \
86 BOOST_PP_SEQ_ELEM(n,sequence) \
87 ]
88
89#define TemplateItemPrinter(z,n,sequence) \
90 BOOST_PP_COMMA_IF(n) \
91 boost:: \
92 BOOST_PP_SEQ_ELEM(n,sequence) \
93 BOOST_PP_SEQ_ELEM(n,suffixseq)
94
95#define ParamItemPrinter(z,n,sequence)
96
97#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Creator< RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
98#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq)-1 )
99#include BOOST_PP_LOCAL_ITERATE()
100
101#define BOOST_PP_LOCAL_MACRO(n) seqitems_as_enum_key_multidimmap(~, n, size_tupels, distributionengine_seqseq_a, GeneratorPrototypeTable, TableItemPrinter, new RandomNumberGenerator_Creator< RandomNumberGenerator_Encapsulation , TemplateItemPrinter, ParamItemPrinter)
102#define BOOST_PP_LOCAL_LIMITS (0, BOOST_PP_SEQ_SIZE(distributionengine_seqseq_a)-1 )
103#include BOOST_PP_LOCAL_ITERATE()
104}
105
106RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator() const
107{
108 // Instantiate and return (implicitly creates a copy of the stored prototype)
109 return (GeneratorPrototypeTable[engine][distribution]->create());
110}
111
112RandomNumberGenerator* RandomNumberGeneratorFactory::makeRandomNumberGenerator(
113 std::string engine_type,
114 std::string distribution_type
115 ) const
116{
117 enum RandomNumberEngineFactory::Engine eng_type;
118 enum RandomNumberDistributionFactory::Distribution dis_type;
119
120 // Instantiate and return (implicitly creates a copy of the stored prototype)
121 if (!engine_type.empty()) {
122 eng_type = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
123 } else
124 eng_type = engine;
125 if (!distribution_type.empty()) {
126 dis_type = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
127 } else
128 dis_type = distribution;
129 return (GeneratorPrototypeTable[ eng_type ][ dis_type ]->create());
130}
131
132void RandomNumberGeneratorFactory::setEngine(std::string engine_type)
133{
134 engine = RandomNumberEngineFactory::getInstance().getEnum(engine_type);
135}
136
137const std::string &RandomNumberGeneratorFactory::getEngineName() const
138{
139 return RandomNumberEngineFactory::getInstance().getName(engine);
140}
141
142void RandomNumberGeneratorFactory::setDistribution(std::string distribution_type)
143{
144 distribution = RandomNumberDistributionFactory::getInstance().getEnum(distribution_type);
145}
146
147const std::string &RandomNumberGeneratorFactory::getDistributionName() const
148{
149 return RandomNumberDistributionFactory::getInstance().getName(distribution);
150}
151
152CONSTRUCT_SINGLETON(RandomNumberGeneratorFactory)
153
154#include "RandomNumberDistributionFactory.undef"
155#include "RandomNumberEngineFactory.undef"
156
Note: See TracBrowser for help on using the repository browser.