MoDeNa  1.0
Software framework facilitating sequential multi-scale modelling
modenaModel.H
Go to the documentation of this file.
1 
40 #ifndef __MODENAMODEL_H__
41 #define __MODENAMODEL_H__
42 
43 #include <exception>
44 #include <string>
45 #include <vector>
46 
47 #include "modena.h"
48 
49 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
50 
51 namespace Modena
52 {
53 
54 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
55 
57 :
58  public std::exception
59 {
60  // Private data
61 
62  int errorCode_;
63 
64 public:
65 
66  modenaException(int errorCode)
67  :
68  errorCode_(errorCode)
69  {}
70 
71  int errorCode() const
72  {
73  return errorCode_;
74  }
75 
76  virtual const char* what() const throw()
77  {
78  return "Modena Exception";
79  }
80 };
81 
82 
84 {
85  // Private data
86 
87  // Pointer to MoDeNa model
88  modena_model_t *model_;
89 
90  modena_inputs_t *inputs_;
91  modena_outputs_t *outputs_;
92 
93 public:
94 
95  // Constructors
96 
97  //- Construct from model name
99  (
100  const std::string& name
101  )
102  {
103  // Instantiate a model
104  model_ = modena_model_new(name.c_str());
105 
106  if(modena_error_occurred())
107  {
108  throw modenaException(modena_error());
109  }
110 
111  // Allocate memory and fetch arg positions
112  inputs_ = modena_inputs_new(model_);
113  outputs_ = modena_outputs_new(model_);
114  }
115 
116  // Destructor
117 
118  ~modenaModel()
119  {
120  modena_inputs_destroy(inputs_);
121  modena_outputs_destroy(outputs_);
122  modena_model_destroy(model_);
123  }
124 
125  // Member Functions
126 
127  inline size_t inputs_size() const
128  {
129  return modena_model_inputs_size(model_);
130  }
131 
132  inline size_t outputs_size() const
133  {
134  return modena_model_outputs_size(model_);
135  }
136 
137  inline size_t parameters_size() const
138  {
139  return modena_model_parameters_size(model_);
140  }
141 
142  inline size_t inputs_argPos(const std::string& name) const
143  {
144  return modena_model_inputs_argPos(model_, name.c_str());
145  }
146 
147  inline size_t outputs_argPos(const std::string& name) const
148  {
149  return modena_model_outputs_argPos(model_, name.c_str());
150  }
151 
152  inline void inputs_set(const size_t i, double x) const
153  {
154  modena_inputs_set(inputs_, i, x);
155  }
156 
157  inline double outputs_get(const size_t i) const
158  {
159  return modena_outputs_get(outputs_, i);
160  }
161 
162  inline void argPos_check() const
163  {
165  }
166 
167  inline std::vector<std::string> inputs_names() const
168  {
169  std::vector<std::string> v;
170 
171  const char** iNames = modena_model_inputs_names(model_);
172  for(int i=0; i<modena_model_inputs_size(model_); i++)
173  {
174  v.push_back(std::string(iNames[i]));
175  }
176 
177  return v;
178  }
179 
180  inline std::vector<std::string> outputs_names() const
181  {
182  std::vector<std::string> v;
183 
184  const char** oNames = modena_model_outputs_names(model_);
185  for(int i=0; i<modena_model_outputs_size(model_); i++)
186  {
187  v.push_back(std::string(oNames[i]));
188  }
189 
190  return v;
191  }
192 
193  inline std::vector<std::string> parameters_names() const
194  {
195  std::vector<std::string> v;
196 
197  const char** pNames = modena_model_parameters_names(model_);
198  for(int i=0; i<modena_model_parameters_size(model_); i++)
199  {
200  v.push_back(std::string(pNames[i]));
201  }
202 
203  return v;
204  }
205 
206  inline void call() const
207  {
208  modena_model_call(model_, inputs_, outputs_);
209 
210  if(modena_error_occurred())
211  {
212  throw modenaException(modena_error());
213  }
214  }
215 
216 };
217 
218 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
219 
220 } // End namespace Modena
221 
222 #endif /* __MODENAMODEL_H__ */
223 
224 // ************************************************************************* //
char ** modena_model_parameters_names(const modena_model_t *self)
Function returning the names of the parameters.
Definition: model.c:442
char ** modena_model_inputs_names(const modena_model_t *self)
Function returning the names of the inputs.
Definition: model.c:432
size_t modena_model_inputs_argPos(const modena_model_t *self, const char *name)
Function determining position of an argument in the input vector.
Definition: model.c:366
size_t modena_model_inputs_size(const modena_model_t *self)
Function returning the size of the input vector.
Definition: model.c:447
size_t modena_model_outputs_size(const modena_model_t *self)
Function returning the size of the output vector.
Definition: model.c:452
modena_model_t * modena_model_new(const char *modelId)
Function fetching a surrogate model from MongoDB.
Definition: model.c:281
int modena_model_call(modena_model_t *self, modena_inputs_t *inputs, modena_outputs_t *outputs)
Function calling the surrogate model and checking for errors.
Definition: model.c:553
stores a surrogate model
Definition: model.h:94
void modena_model_destroy(modena_model_t *self)
Function deallocating the memory allocated for the surrogate model.
Definition: model.c:665
size_t modena_model_parameters_size(const modena_model_t *self)
Function returning the size of the parameter vector.
Definition: model.c:457
size_t modena_model_outputs_argPos(const modena_model_t *self, const char *name)
Function determining position of a result in the output vector.
Definition: model.c:392
void modena_model_argPos_check(const modena_model_t *self)
Function checking that the user has queried all input positions.
Definition: model.c:408
char ** modena_model_outputs_names(const modena_model_t *self)
Function returning the names of the outputs.
Definition: model.c:437