gambit is hosted by Hepforge, IPPP Durham
GAMBIT  v1.5.0-2191-ga4742ac
a Global And Modular Bsm Inference Tool
models.cpp
Go to the documentation of this file.
1 // GAMBIT: Global and Modular BSM Inference Tool
2 // *********************************************
30 
31 #include <algorithm>
33 #include "gambit/Models/models.hpp"
34 
35 namespace Gambit
36 {
37 
38  namespace Models
39  {
40 
41  typedef std::map<std::string, primary_model_functor *>::const_iterator activemodel_it;
42 
47 
48  // Public functions and data members
49 
52  primodel_vec ModelFunctorClaw::getPrimaryModelFunctorsToActivate (std::set<str> selectedmodels, const primodel_vec& primaryModelFunctors)
53  {
54  // Holder for result
55  primodel_vec result;
56  // Iterator to elements of 'selectedmodels'
57  std::set<str>::iterator el;
58 
59  // Loop through functor list and activate functor if it matches a member of 'selectedmodels'.
60  for (primodel_vec::const_iterator
61  it = primaryModelFunctors.begin();
62  it != primaryModelFunctors.end();
63  ++it)
64  {
65  // Check if this functor originates from one of the selected models
66  el = std::find(selectedmodels.begin(), selectedmodels.end(), (*it)->origin());
67  if(el != selectedmodels.end())
68  {
69  // If yes, activate this functor. Default is inactivated.
70  (*it)->setStatus(1); // 1 means "available".
71  (*it)->setPrintRequirement(true); // Tell printer to output this functor
72  // Initialise ModelParameters object it contains
73  (*it)->calculate();
74  // Add it to the vector of primary model functors to make active (to be returned)
75  result.push_back(*it);
76  // Add it to the internal list of active models
77  activemodels.insert(*el);
78  // Remove it from the input 'selectedmodels' list
79  selectedmodels.erase(el);
80  }
81  }
82 
83  // Check that all requested models have been activated
84  if( selectedmodels.size() != 0 )
85  {
86  // Report error
87  str errmsg = "Error! Some of the requested models could not be activated for \n";
88  errmsg += "scanning! Probably they have not been defined, or you spelled \n";
89  errmsg += "their name wrong in the ini file. \n";
90  errmsg += "Models that cannot be activated: \n";
91  for (std::set<str>::iterator m = selectedmodels.begin(); m != selectedmodels.end(); ++m)
92  {
93  errmsg += (" " + *m + "\n");
94  }
95  model_error().raise(LOCAL_INFO,errmsg);
96  }
97 
98  return result;
99 
100  }
101 
103  const std::set<str>& ModelFunctorClaw::get_allmodels() const { return allmodelnames; }
104 
106  std::set<str> ModelFunctorClaw::get_activemodels() const { return activemodels; }
107 
114  {
115  std::vector<std::string> unusedmodels;
116  std::string modelname;
117  primary_model_functor* functorPtr;
118  // Loop through functor list and check that their statuses have all been
119  // set to 2 ("active"). If not, it means that some of them were not
120  // activated by the dependency resolver and thus are not used for
121  // computing anything.
122  for(activemodel_it it = activeModelFunctors.begin();
123  it != activeModelFunctors.end(); it++)
124  {
125  modelname = it->first;
126  functorPtr = it->second;
127 
128  if ( functorPtr->status()!=2 )
129  {
130  unusedmodels.push_back( modelname );
131  }
132  }
133 
134  // If we found unused models throw an error
135  if ( unusedmodels.size() > 0 )
136  {
137  str errmsg = "Some models selected for scanning are not required by any of\n";
138  errmsg += "the requested observables/likelihoods! Please switch these \n";
139  errmsg += "off in the inifile or add a target that actually uses them. \n";
140  errmsg += "List of unused models: \n";
141  for (std::vector<std::string>::iterator it = unusedmodels.begin(); it != unusedmodels.end(); ++it)
142  {
143  errmsg += (*it + "\n");
144  }
145  model_error().raise(LOCAL_INFO,errmsg);
146  }
147 
148  }
149 
151  void ModelFunctorClaw::declare_model (const str &model, const str &parent)
152  {
153  // Register the new model.
154  allmodelnames.insert(model);
155  if (parent != "PARENT")
156  {
157  // If the parent actually doesn't exist yet, die.
158  if (not model_exists(parent))
159  {
160  str errmsg = "Requested parent model \""+parent+ "\" for model \""+model;
161  errmsg += "\nis not in the GAMBIT database. Recognised models are:\n" + list_models();
162  model_error().raise(LOCAL_INFO,errmsg);
163  }
164  // Add the parent to the parents database.
165  myParentsDB[model] = parent;
166  // Inherit friends from the model's parent.
167  myFriendsDB[model] = myFriendsDB[parent];
168  // Inherit lineage from the model's parent.
169  myLineageDB[model] = myLineageDB[parent];
170  }
171  else
172  {
173  // Seed empty friend sets and inheritance vector.
174  myFriendsDB[model] = std::set<str>();
175  myBestFriendsDB[model] = std::set<str>();
176  myLineageDB[model] = std::vector<str>();
177  }
178  // Add the new model to its own lineage vector.
179  myLineageDB[model].push_back(model);
180  // Register the model in each of its parents' descendents vectors.
181  for (std::set<str>::iterator parent = allmodelnames.begin(); parent != allmodelnames.end(); ++parent)
182  {
183  // If this model descends from parent, add it to the parent's descendents vector
184  if (descended_from(model,*parent)) myDescendantsDB[*parent].push_back(model);
185  }
186  }
187 
189  void ModelFunctorClaw::add_friend (const str &model, const str &newfriend)
190  {
191  // If the new friend actually doesn't exist yet, die.
192  if (not model_exists(newfriend))
193  {
194  str errmsg = "Requested friend model \""+newfriend + "\" for model \""+model;
195  errmsg += "\nis not in the GAMBIT database. Recognised models are:\n" + list_models();
196  model_error().raise(LOCAL_INFO,errmsg);
197  }
198  // Add the new friend as a best friend.
199  myBestFriendsDB[model].insert(newfriend);
200  // Add the new friend's whole lineage vector as regular friends.
201  std::set<str> temp1, temp2, lineage(myLineageDB[newfriend].begin(), myLineageDB[newfriend].end());
202  set_union(myFriendsDB[model].begin(), myFriendsDB[model].end(), lineage.begin(), lineage.end(), inserter(temp1, temp1.begin()));
203  // Also inherit the new friend's friends as regular friends.
204  set_union(temp1.begin(), temp1.end(), myFriendsDB[newfriend].begin(), myFriendsDB[newfriend].end(), inserter(temp2, temp2.begin()));
205  myFriendsDB[model] = temp2;
206  }
207 
209  bool ModelFunctorClaw::model_exists (const str &model) const
210  {
211  return allmodelnames.find(model) != allmodelnames.end();
212  }
213 
216  {
217  str temp = "";
218  for (std::set<str>::iterator it = allmodelnames.begin(); it != allmodelnames.end(); ++it)
219  {
220  temp += "\n" + *it;
221  }
222  return temp;
223  }
224 
226  void ModelFunctorClaw::verify_model(const str &model) const
227  {
228  if (not model_exists(model))
229  {
230  str errmsg = "Model \"";
231  errmsg += model + "\" is not in the GAMBIT database.";
232  errmsg += "\nRecognised models are:" + list_models();
233  model_error().raise(LOCAL_INFO,errmsg);
234  }
235  }
236 
238  std::vector<str> ModelFunctorClaw::get_descendants (const str &model) const
239  {
240  return myDescendantsDB.find(model) == myDescendantsDB.end() ? std::vector<str>() : myDescendantsDB.at(model);
241  }
242 
244  str ModelFunctorClaw::get_parent (const str &model) const
245  {
246  return myParentsDB.find(model) == myParentsDB.end() ? "none" : myParentsDB.at(model);
247  }
248 
250  std::vector<str> ModelFunctorClaw::get_lineage (const str &model) const
251  {
252  return myLineageDB.find(model) == myLineageDB.end() ? std::vector<str>() : myLineageDB.at(model);
253  }
254 
256  std::set<str> ModelFunctorClaw::get_friends (const str &model) const
257  {
258  return myFriendsDB.find(model) == myFriendsDB.end() ? std::set<str>() : myFriendsDB.at(model);
259  }
260 
262  std::set<str> ModelFunctorClaw::get_best_friends (const str &model) const
263  {
264  return myBestFriendsDB.find(model) == myBestFriendsDB.end() ? std::set<str>() : myBestFriendsDB.at(model);
265  }
266 
268  bool ModelFunctorClaw::descended_from (const str &model1, const str &model2) const
269  {
270  verify_model(model1);
271  auto lineage = myLineageDB.at(model1);
272  for (std::vector<str>::const_iterator it = lineage.begin(); it != lineage.end(); ++it)
273  {
274  if (model2==*it) return true;
275  }
276  return false;
277  }
278 
280  bool ModelFunctorClaw::ancestor_of (const str &model1, const str &model2) const
281  {
282  return descended_from(model2, model1);
283  }
284 
286  bool ModelFunctorClaw::downstream_of (const str &model1, const str &model2) const
287  {
288  if (descended_from(model1, model2)) return true;
289  if (myFriendsDB.find(model1) != myFriendsDB.end())
290  {
291  std::set<str> friends = myFriendsDB.at(model1);
292  return friends.find(model2) != friends.end();
293  }
294  return false;
295  }
296 
298  bool ModelFunctorClaw::upstream_of (const str &model1, const str &model2) const
299  {
300  return downstream_of(model2, model1);
301  }
302 
304 
305  }
306 
307 }
std::map< str, std::set< str > > myBestFriendsDB
Definition: models.hpp:74
bool upstream_of(const str &, const str &) const
Check if model 1 exists somewhere upstream of model 2, allowing model 2 to be interpreted as model 1...
Definition: models.cpp:298
std::vector< primary_model_functor * > primodel_vec
Definition: models.hpp:50
Model helper declarations.
std::map< str, std::set< str > > myFriendsDB
Definition: models.hpp:73
str list_models() const
List all the models recognised by GAMBIT.
Definition: models.cpp:215
void checkPrimaryModelFunctorUsage(const activemodel_map &) const
Active model functor "usefulness" checker.
Definition: models.cpp:113
Functors specific to primary ModelParameters objects.
Definition: functors.hpp:1001
std::map< str, str > myParentsDB
Definition: models.hpp:69
#define LOCAL_INFO
Definition: local_info.hpp:34
void verify_model(const str &) const
Verify that a string matches a model recognised by GAMBIT, crash otherwise.
Definition: models.cpp:226
std::set< str > allmodelnames
Definition: models.hpp:68
std::set< str > get_friends(const str &model) const
Retrieve the friends for a given model.
Definition: models.cpp:256
str get_parent(const str &) const
Retrieve the parent model for a given model.
Definition: models.cpp:244
bool downstream_of(const str &, const str &) const
Check if model 1 exists somewhere downstream of (and can be therefore be interpreted as a) model 2...
Definition: models.cpp:286
primodel_vec getPrimaryModelFunctorsToActivate(std::set< str >, const primodel_vec &)
Model activation function.
Definition: models.cpp:52
std::set< str > activemodels
Internal record of the active models.
Definition: models.hpp:64
const std::set< str > & get_allmodels() const
Return set of all models recognised by GAMBIT.
Definition: models.cpp:103
const std::vector< std::string > lineage
Definition: orphan.hpp:35
std::string str
Definition: models.hpp:49
std::map< std::string, primary_model_functor * >::const_iterator activemodel_it
Definition: models.cpp:41
Functor class definitions.
std::set< str > get_activemodels() const
Return the set of active models;.
Definition: models.cpp:106
void add_friend(const str &model, const str &newfriend)
Add a friend, and all its friends and ancestors, to a model&#39;s list of friends.
Definition: models.cpp:189
std::map< str, std::vector< str > > myLineageDB
Definition: models.hpp:70
bool ancestor_of(const str &, const str &) const
Check if model 1 is an ancestor of model 2.
Definition: models.cpp:280
std::map< str, primary_model_functor * > activemodel_map
Definition: models.hpp:51
bool model_exists(const str &) const
Indicate whether a model is recognised by GAMBIT or not.
Definition: models.cpp:209
bool descended_from(const str &, const str &) const
Check if model 1 is descended from model 2.
Definition: models.cpp:268
std::set< str > get_best_friends(const str &model) const
Retrieve the best friends for a given model.
Definition: models.cpp:262
std::map< str, std::vector< str > > myDescendantsDB
Definition: models.hpp:71
std::vector< str > get_lineage(const str &) const
Retrieve the lineage for a given model.
Definition: models.cpp:250
void declare_model(const str &, const str &)
Add a new model to the model database.
Definition: models.cpp:151
std::vector< str > get_descendants(const str &) const
Retrieve the descendants for a given model.
Definition: models.cpp:238
int status() const
Getter for the wrapped function current status: -4 = required backend absent (backend ini functions) ...
Definition: functors.cpp:134
error & model_error()
Model errors.
TODO: see if we can use this one:
Definition: Analysis.hpp:33