gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-252-gf9a3f78
a Global And Modular Bsm Inference Tool
model_macros.hpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
22 
23 #ifndef __model_macros_hpp__
24 #define __model_macros_hpp__
25 
26 #include "gambit/Models/orphan.hpp"
31 
32 #include <boost/preprocessor/seq/for_each.hpp>
33 
34 #ifdef __model_rollcall_hpp__
36  #ifndef STANDALONE
38  #endif
39  #define START_MODEL CORE_START_MODEL
40  #define DEFINEPARS(...) CORE_DEFINEPARS(__VA_ARGS__)
41  #define MAP_TO_CAPABILITY(PARAMETER,CAPABILITY) CORE_MAP_TO_CAPABILITY(PARAMETER,CAPABILITY)
42  #define INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC) CORE_INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC)
43  #define INTERPRET_AS_PARENT_FUNCTION(FUNC) CORE_INTERPRET_AS_PARENT_FUNCTION(FUNC)
44  #define INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE) CORE_INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE)
45  // "Traditional" module macros
46  // These are just copy/pasted from the module versions with small adjustments, like MODULE->MODEL and NOT_MODEL->IS_MODEL
47  // (Note: MODULE left as MODULE where it is just a function argument)
48  #define START_CAPABILITY CORE_START_CAPABILITY(MODEL, CAPABILITY, IS_MODEL)
49  #define LONG_START_CAPABILITY(MODULE, CAPABILITY) CORE_START_CAPABILITY(MODULE, CAPABILITY, IS_MODEL)
50  #define DECLARE_FUNCTION(TYPE, FLAG) CORE_DECLARE_FUNCTION(MODEL, CAPABILITY, FUNCTION, TYPE, FLAG, IS_MODEL)
51  #define LONG_DECLARE_FUNCTION(MODULE, CAPABILITY, FUNCTION, TYPE, FLAG) CORE_DECLARE_FUNCTION(MODULE, CAPABILITY, FUNCTION, TYPE, FLAG, IS_MODEL)
52  #define NEEDS_MANAGER(...) CORE_NEEDS_MANAGER(__VA_ARGS__)
53  #define DEPENDENCY(DEP, TYPE) CORE_DEPENDENCY(DEP, TYPE, MODEL, FUNCTION, IS_MODEL)
54  #define LONG_DEPENDENCY(MODULE, FUNCTION, DEP, TYPE) CORE_DEPENDENCY(DEP, TYPE, MODULE, FUNCTION, IS_MODEL)
55  #define ALLOW_MODELS(...) ALLOW_MODELS_AB(MODEL, FUNCTION, __VA_ARGS__)
56  #define ALLOWED_MODEL(MODULE,FUNCTION,MODEL) CORE_ALLOWED_MODEL(MODULE,FUNCTION,MODEL,IS_MODEL)
57  #define ALLOWED_MODEL_DEPENDENCE(MODULE,FUNCTION,MODEL) CORE_ALLOW_MODEL_DEPENDENCE(MODULE,FUNCTION,MODEL,IS_MODEL)
58  #define ALLOW_MODEL_COMBINATION(...) CORE_ALLOW_MODEL_COMBINATION(MODEL,FUNCTION,IS_MODEL,(__VA_ARGS__))
59  #define MODEL_GROUP(GROUPNAME,GROUP) CORE_MODEL_GROUP(MODEL,FUNCTION,GROUPNAME,GROUP,IS_MODEL)
60  #define DECLARE_BACKEND_REQ(GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE) \
61  CORE_BACKEND_REQ(MODEL, CAPABILITY, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE, IS_MODEL)
62  #define LONG_DECLARE_BACKEND_REQ(MODULE, CAPABILITY, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE) \
63  CORE_BACKEND_REQ(MODULE, CAPABILITY, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE, IS_MODEL)
64  #define BE_GROUP(GROUP) CORE_BE_GROUP(GROUP,IS_MODEL)
65  #define ACTIVATE_BACKEND_REQ_FOR_MODELS(MODELS,TAGS) CORE_BE_MODEL_RULE(MODELS,TAGS,IS_MODEL)
66  #define BACKEND_OPTION(BACKEND_AND_VERSIONS,TAGS) LONG_BACKEND_OPTION(MODEL, CAPABILITY, FUNCTION, BACKEND_AND_VERSIONS,TAGS)
67  #define LONG_BACKEND_OPTION(MODULE, CAPABILITY, FUNCTION, BACKEND_AND_VERSIONS,TAGS) \
68  CORE_BACKEND_OPTION(MODULE, CAPABILITY, FUNCTION, BACKEND_AND_VERSIONS,TAGS, IS_MODEL)
69  #define FORCE_SAME_BACKEND(...) CORE_FORCE_SAME_BACKEND(IS_MODEL,__VA_ARGS__)
70  #define START_CONDITIONAL_DEPENDENCY(TYPE) CORE_START_CONDITIONAL_DEPENDENCY(MODEL, CAPABILITY, \
71  FUNCTION, CONDITIONAL_DEPENDENCY, TYPE, IS_MODEL)
72  #define ACTIVATE_DEP_BE(BACKEND_REQ, BACKEND, VERSTRING) CORE_ACTIVATE_DEP_BE(BACKEND_REQ, BACKEND, VERSTRING, IS_MODEL)
73  #define ACTIVATE_FOR_MODELS(...) ACTIVATE_DEP_MODEL(MODEL, CAPABILITY, FUNCTION, CONDITIONAL_DEPENDENCY, IS_MODEL, #__VA_ARGS__)
74  #define MODEL_CONDITIONAL_DEPENDENCY(DEP, TYPE, ...) CORE_START_CONDITIONAL_DEPENDENCY(MODEL, CAPABILITY, FUNCTION, DEP, TYPE, IS_MODEL) \
75  ACTIVATE_DEP_MODEL(MODEL, CAPABILITY, FUNCTION, DEP, IS_MODEL #__VA_ARGS__)
76  #define CLASSLOAD_NEEDED(BACKEND, VERSION) CORE_CLASSLOAD_NEEDED(BACKEND, VERSION, IS_MODEL)
77 #else
79  #define START_MODEL MODULE_START_MODEL
80  #define DEFINEPARS(...) /* Do nothing */
81  #define MAP_TO_CAPABILITY(PARAMETER,CAPABILITY) /* Do nothing */
82  #define INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC) MODULE_INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC)
83  #define INTERPRET_AS_PARENT_FUNCTION(FUNC) MODULE_INTERPRET_AS_X_FUNCTION(PARENT,FUNC)
84  #define INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE) MODULE_INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE)
85  // "Traditional" module macros
86  #define START_CAPABILITY MODULE_START_CAPABILITY(MODEL)
87  #define LONG_START_CAPABILITY(MODULE, C) MODULE_START_CAPABILITY(MODULE)
88  #define DECLARE_FUNCTION(TYPE, CAN_MANAGE) MODULE_DECLARE_FUNCTION(MODEL, FUNCTION, TYPE, CAN_MANAGE, IS_MODEL)
89  #define LONG_DECLARE_FUNCTION(MODULE, C, FUNCTION, TYPE, CAN_MANAGE) \
90  MODULE_DECLARE_FUNCTION(MODULE, FUNCTION, TYPE, CAN_MANAGE, IS_MODEL)
91  #define DEPENDENCY(DEP, TYPE) MODULE_DEPENDENCY(DEP, TYPE, MODEL, FUNCTION, IS_MODEL)
92  #define LONG_DEPENDENCY(MODULE, FUNCTION, DEP, TYPE) MODULE_DEPENDENCY(DEP, TYPE, MODEL, FUNCTION, IS_MODEL)
93  #define NEEDS_MANAGER(...) MODULE_NEEDS_MANAGER_REDIRECT(__VA_ARGS__)
94  #define ALLOWED_MODEL(MODULE,FUNCTION,MODEL) MODULE_ALLOWED_MODEL(MODULE,FUNCTION,MODEL,IS_MODEL)
95  #define ALLOWED_MODEL_DEPENDENCE(MODULE,FUNCTION,MODEL) MODULE_ALLOWED_MODEL(MODULE,FUNCTION,MODEL,IS_MODEL)
96  #define ALLOW_MODEL_COMBINATION(...) DUMMYARG(__VA_ARGS__)
97  #define MODEL_GROUP(GROUPNAME, GROUP) DUMMYARG(GROUPNAME, GROUP)
98 
99  #define BE_GROUP(GROUP) MODULE_BE_GROUP(GROUP,IS_MODEL)
100  #define DECLARE_BACKEND_REQ(GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE) \
101  MODULE_BACKEND_REQ(MODEL, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE, IS_MODEL)
102  #define LONG_DECLARE_BACKEND_REQ(MODULE, C, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE) \
103  MODULE_BACKEND_REQ(MODULE, FUNCTION, GROUP, REQUIREMENT, TAGS, TYPE, ARGS, IS_VARIABLE, IS_MODEL)
104  #define ACTIVATE_BACKEND_REQ_FOR_MODELS(MODELS,TAGS) DUMMYARG(MODELS,TAGS)
105  #define START_CONDITIONAL_DEPENDENCY(TYPE) MODULE_DEPENDENCY(CONDITIONAL_DEPENDENCY, TYPE, MODEL, FUNCTION, IS_MODEL)
106  #define ACTIVATE_DEP_BE(BACKEND_REQ, BACKEND, VERSTRING) DUMMYARG(BACKEND_REQ, BACKEND, VERSTRING)
107  #define ACTIVATE_FOR_MODELS(...) DUMMYARG(__VA_ARGS__)
108  #define MODEL_CONDITIONAL_DEPENDENCY(DEP, TYPE, ...) MODULE_DEPENDENCY(DEP, TYPE, MODEL, FUNCTION, IS_MODEL)
109  #define BACKEND_OPTION(BACKEND_AND_VERSIONS,TAGS) DUMMYARG(BACKEND_AND_VERSIONS,TAGS)
110  #define LONG_BACKEND_OPTION(MODULE, CAPABILITY, FUNCTION, BACKEND_AND_VERSIONS,TAGS) \
111  DUMMYARG(BACKEND_AND_VERSIONS,TAGS)
112  #define FORCE_SAME_BACKEND(...) DUMMYARG(__VA_ARGS__)
113  #define CLASSLOAD_NEEDED(...) DUMMYARG(__VA_ARGS__)
114 
115 #endif
116 #define ALLOW_MODELS(...) ALLOW_MODELS_AB(MODEL, FUNCTION, __VA_ARGS__)
117 
118 #ifndef STANDALONE
119  #define MAKE_PRIMARY_MODEL_FUNCTOR(FUNCTION,CAPABILITY,ORIGIN) MAKE_PRIMARY_MODEL_FUNCTOR_MAIN(FUNCTION,CAPABILITY,ORIGIN) \
120  MAKE_PRIMARY_MODEL_FUNCTOR_SUPP(FUNCTION)
121 #else
122  #define MAKE_PRIMARY_MODEL_FUNCTOR(FUNCTION,CAPABILITY,ORIGIN) MAKE_PRIMARY_MODEL_FUNCTOR_MAIN(FUNCTION,CAPABILITY,ORIGIN)
123 #endif
124 
125 // MACRO DEFINITIONS.
126 
127 // ****************************************************************************
130 
132 #define MODULE_START_MODEL \
133  IF_TOKEN_UNDEFINED(MODEL,FAIL("You must define MODEL before calling " \
134  "START_MODEL.")) \
135  /*_Pragma("message declaring model...") \
136  _Pragma( STRINGIFY(CAT("message Forward declaring model: ",MODEL)) ) */ \
137  namespace Gambit \
138  { \
139  namespace Models \
140  { \
141  namespace MODEL \
142  { \
143  /* Module errors */ \
144  error& CAT(MODEL,_error)(); \
145  /* Module warnings */ \
146  warning& CAT(MODEL,_warning)(); \
147  } \
148  } \
149  } \
150 
151 #define MODULE_INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC) \
153  namespace Gambit \
154  { \
155  namespace Models \
156  { \
157  namespace MODEL \
158  { \
159  /* Declare the user-defined function as defined elsewhere */ \
160  extern void FUNC (const ModelParameters&, ModelParameters&); \
161  \
162  /* Let the module source know that this functor is declared*/ \
163  namespace Functown { extern module_functor<ModelParameters> \
164  CAT(MODEL_X,_parameters); } \
165  \
166  namespace Pipes \
167  { \
168  namespace CAT(MODEL_X,_parameters) \
169  { \
170  /* Declare the parameters safe-pointer map as external. */ \
171  extern std::map<str, safe_ptr<double> > Param; \
172  /* Declare the safe-pointer to the models vector as external. */ \
173  extern safe_ptr< std::vector<str> > Models; \
174  /* Declare the safe pointer to the run options as external. */ \
175  extern safe_ptr<Options> runOptions; \
176  } \
177  } \
178  } \
179  } \
180  } \
181 
182 #define MODULE_INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE) \
184  MODULE_DEPENDENCY(DEP, TYPE, MODEL, CAT(MODEL_X,_parameters), IS_MODEL)
185 
186 
189 #define CORE_START_MODEL \
190  \
191  namespace Gambit \
192  { \
193  ADD_TAG_IN_CURRENT_NAMESPACE(primary_parameters) \
194  ADD_TAG_IN_CURRENT_NAMESPACE(CAT(MODEL,_parameters)) \
195  namespace Models \
196  { \
197  ADD_MODEL_TAG_IN_CURRENT_NAMESPACE(MODEL) \
198  \
199  namespace MODEL \
200  { \
201  \
202  /* Basic machinery, same as for modules \
203  (macro from module_macros_incore.hpp) */ \
204  CORE_START_MODULE_COMMON_MAIN(MODEL) \
205  \
206  /* Add the model to GAMBIT model database */ \
207  int model_rego = add_model(STRINGIFY(MODEL), STRINGIFY(PARENT)); \
208  \
209  /* Functor's actual "calculate" function. Doesn't do anything. */ \
210  void primary_parameters (ModelParameters&) {} \
211  \
212  /* Wrap it up in a primary_model_functor */ \
213  MAKE_PRIMARY_MODEL_FUNCTOR(primary_parameters, CAT(MODEL,_parameters), \
214  MODEL) \
215  \
216  /* Ini-function to set the name of the model hosted by the
217  ModelParameters object */ \
218  int added_model_name = \
219  set_model_name(Functown::primary_parameters,STRINGIFY(MODEL)); \
220  } \
221  \
222  /* Make the functor exclusive to this model and its descendants */ \
223  CORE_ALLOW_MODEL(MODEL,primary_parameters,MODEL) \
224  \
225  } \
226  } \
227 
228 
229 
237 #define CORE_MAP_TO_CAPABILITY(PARAMETER,CAPABILITY) \
238  \
239  namespace Gambit \
240  { \
241  \
242  /* Add CAPABILITY to global set of tags of known module capabilities/deps*/\
243  ADD_TAG_IN_CURRENT_NAMESPACE(CAPABILITY) \
244  \
245  namespace Models \
246  { \
247  \
248  namespace MODEL \
249  { \
250  \
251  /* Add PARAMETER to set of tags of known module functions.*/ \
252  ADD_TAG_IN_CURRENT_NAMESPACE(PARAMETER) \
253  \
254  /* The wrapper function which extracts the value of PARAMETER from \
255  the parameter object. This is the analogue of a module function, \
256  and is what will be wrapped in a functor for processing by the \
257  core */ \
258  /* Just the prototype here: defined a bit later on */ \
259  void PARAMETER (double &); \
260  \
261  /* Wrap it up in a functor (macro from module_macros_incore.hpp) */ \
262  MAKE_FUNCTOR(PARAMETER,double,CAPABILITY,MODEL,0) \
263  \
264  } \
265  \
266  /* Make the functor exclusive to this model and its descendants */ \
267  CORE_ALLOW_MODEL(MODEL,PARAMETER,MODEL) \
268  \
269  } \
270  \
271  } \
272  \
273  /* Create dependency of PARAMETER functor on host model parameters object */ \
274  CORE_DEPENDENCY(CAT(MODEL,_parameters),ModelParameters,MODEL,PARAMETER, \
275  IS_MODEL) \
276  \
277  /* Define the actual parameter setting function, now that we have the
278  functor and its dependency */ \
279  namespace Gambit \
280  { \
281  \
282  namespace Models \
283  { \
284  \
285  namespace MODEL \
286  { \
287  \
288  /* The wrapper function which extracts the value of PARAMETER from
289  the parameter object. This is the analogue of a module function,
290  and is what will be wrapped in a functor for processing by the
291  core */ \
292  void PARAMETER (double &result) \
293  { \
294  safe_ptr<ModelParameters> model_safe_ptr = \
295  Pipes::PARAMETER::Dep::CAT(MODEL,_parameters).safe_pointer(); \
296  result = model_safe_ptr->getValue(STRINGIFY(PARAMETER)); \
297  } \
298  \
299  } \
300  \
301  } \
302  \
303  } \
304 
305 
308 #define DEFINEPAR(PARAMETER) \
309  namespace Gambit \
310  { \
311  namespace Models \
312  { \
313  namespace MODEL \
314  { \
315  /* Tell the functor holding the ModelParameters object to add the new \
316  parameter to its [parameter name, value] map. */ \
317  int CAT(added_,PARAMETER) = \
318  add_parameter(Functown::primary_parameters,STRINGIFY(PARAMETER)); \
319  } \
320  } \
321  } \
322 
323 #define CORE_DEFINEPARS(...) \
326  BOOST_PP_SEQ_FOR_EACH(DO_LINK, _, BOOST_PP_TUPLE_TO_SEQ((__VA_ARGS__)))
327 #define DO_LINK(r,data,elem) DEFINEPAR(elem)
328 
331 #define CORE_INTERPRET_AS_X_FUNCTION(MODEL_X,FUNC) \
332  INTERPRET_AS_X_FUNCTION_FULL(MODEL_X,FUNC,1) \
333 
334 #define INTERPRET_AS_X_FUNCTION_FULL(MODEL_X,FUNC,ADD_FRIEND) \
336  namespace Gambit \
337  { \
338  \
339  /* Add tags which specify that MODEL_X_parameters is a known capability/dep\
340  in GAMBIT. */ \
341  ADD_TAG_IN_CURRENT_NAMESPACE(CAT(MODEL_X,_parameters)) \
342  \
343  namespace Models \
344  { \
345  \
346  namespace MODEL \
347  { \
348  \
349  /* Add MODEL_X_parameters to the set of tags of known functions \
350  provided by this model. */ \
351  ADD_TAG_IN_CURRENT_NAMESPACE(CAT(MODEL_X,_parameters)) \
352  ADD_TAG_IN_CURRENT_NAMESPACE(FUNC) \
353  \
354  /* The function which computes the MODEL_X_parameters object. This \
355  is the analogue of a module function, and is what will be wrapped \
356  in a functor for processing by the core \
357  Note: CODE must be enclosed in braces. */ \
358  /* Register (prototype) the function */ \
359  void CAT(MODEL_X,_parameters) (ModelParameters &); \
360  \
361  /* Wrap it up in a functor (macro from module_macros_incore.hpp) */ \
362  MAKE_FUNCTOR(CAT(MODEL_X,_parameters),ModelParameters, \
363  CAT(MODEL_X,_parameters),MODEL,0) \
364  \
365  /* Call a function that tells the functor to take its parameter \
366  definition from MODEL_X's primary_parameters functor, and \
367  adds MODEL_X as a friend of MODEL if it is not a parent. */ \
368  const int CAT(pars_for_,MODEL_X) = \
369  copy_parameters(MODEL_X::Functown::primary_parameters, \
370  Functown::CAT(MODEL_X,_parameters), \
371  BOOST_PP_IIF(ADD_FRIEND,true,false), \
372  STRINGIFY(MODEL), STRINGIFY(MODEL_X)); \
373  \
374  } \
375  \
376  } \
377  \
378  } \
379  \
380  /* Automatically add a dependency on the host model's parameters */ \
381  INTERPRET_AS_X_DEPENDENCY(MODEL_X,CAT(MODEL,_parameters),ModelParameters) \
382  \
383  namespace Gambit \
384  { \
385  \
386  namespace Models \
387  { \
388  \
389  namespace MODEL \
390  { \
391  \
392  /* Prototype the user-defined function */ \
393  void FUNC (const ModelParameters&, ModelParameters&); \
394  \
395  /* The actual definition of the interpret_as_X "module" function */ \
396  void CAT(MODEL_X,_parameters) (ModelParameters &model_x_params) \
397  { \
398  /* Collect MODEL's parameters via dependency system */ \
399  using namespace Pipes::CAT(MODEL_X,_parameters); \
400  const ModelParameters& model_params = *Dep::CAT(MODEL,_parameters); \
401  \
402  /* Run user-supplied code (which must take result as an
403  argument, and set the parameters it contains as desired) */ \
404  FUNC (model_params,model_x_params); \
405  } \
406  \
407  } \
408  \
409  /* Make the functor exclusive to this model and its descendants */ \
410  CORE_ALLOW_MODEL(MODEL,CAT(MODEL_X,_parameters),MODEL) \
411  \
412  } \
413  \
414  } \
415 
416 
418 #define CORE_INTERPRET_AS_X_DEPENDENCY(MODEL_X, DEP, TYPE) \
419  CORE_DEPENDENCY(DEP, TYPE, MODEL, CAT(MODEL_X,_parameters), IS_MODEL) \
420 
421 #define INTERPRET_AS_PARENT_DEPENDENCY(DEP, TYPE) \
424  INTERPRET_AS_X_DEPENDENCY(PARENT, DEP, TYPE)
425 #define CORE_INTERPRET_AS_PARENT_FUNCTION(FUNC) \
426  INTERPRET_AS_X_FUNCTION_FULL(PARENT,FUNC,0)
427 
430 #define MODEL_NAMESPACE Gambit::Models::MODEL
431 
434 #define USE_MODEL_PIPE(MODEL_X) \
435  using namespace MODEL_NAMESPACE::Pipes::CAT(MODEL_X,_parameters); \
436 
437 
443 
445 #define MAKE_PRIMARY_MODEL_FUNCTOR_MAIN(FUNCTION,CAPABILITY,ORIGIN) \
446  /* Create the function wrapper object (functor) */ \
447  namespace Functown \
448  { \
449  primary_model_functor FUNCTION \
450  (&ORIGIN::FUNCTION, STRINGIFY(FUNCTION), STRINGIFY(CAPABILITY), \
451  "ModelParameters", STRINGIFY(ORIGIN), ModelDB()); \
452  } \
453 
454 #define MAKE_PRIMARY_MODEL_FUNCTOR_SUPP(FUNCTION) \
456  /* Register the functor with the Core. */ \
457  int CAT(coreg_,FUNCTION) = register_model_functor_core(Functown::FUNCTION); \
458 
459 
461 #endif /* defined(__model_macros_hpp__) */
462 
General small utility macros.
Redirection macros for generic observable and likelihood function macro definitions, for inclusion from the Core.
Core-only functions for triggering initialisation code.
Functions for triggering initialisation code.
Generic observable and likelihood function macro definitions, for inclusion from macro redirection he...
Copies of boost headers that are required but that may be too recent to be present on the user&#39;s mach...
Base lineage for starting orphan models.
GAMBIT model claw object accessor function.