PluggableSimulator.hpp
Go to the documentation of this file.
1 /*
2 
3  This file is part of OpenFLUID software
4  Copyright(c) 2007, INRA - Montpellier SupAgro
5 
6 
7  == GNU General Public License Usage ==
8 
9  OpenFLUID is free software: you can redistribute it and/or modify
10  it under the terms of the GNU General Public License as published by
11  the Free Software Foundation, either version 3 of the License, or
12  (at your option) any later version.
13 
14  OpenFLUID is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  GNU General Public License for more details.
18 
19  You should have received a copy of the GNU General Public License
20  along with OpenFLUID. If not, see <http://www.gnu.org/licenses/>.
21 
22 
23  == Other Usage ==
24 
25  Other Usage means a use of OpenFLUID that is inconsistent with the GPL
26  license, and requires a written agreement between You and INRA.
27  Licensees for Other Usage of OpenFLUID may use this file in accordance
28  with the terms contained in the written agreement between You and INRA.
29 
30 */
31 
32 
33 /**
34  @file PluggableSimulator.hpp
35 
36  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
37  */
38 
39 
40 #ifndef __OPENFLUID_WARE_PLUGGABLESIMULATOR_HPP__
41 #define __OPENFLUID_WARE_PLUGGABLESIMULATOR_HPP__
42 
43 
44 #include <string>
45 
46 #include <openfluid/dllexport.hpp>
49 #include <openfluid/core/TypeDefs.hpp>
53 #include <openfluid/core/Event.hpp>
58 
59 
60 // =====================================================================
61 // =====================================================================
62 
63 
64 /**
65  @deprecated This declaration is not needed anymore since version 2.1.0
66 */
67 #define DECLARE_SIMULATOR_PLUGIN
68 
69 
70 /**
71  Macro for definition of simulator hook
72  @param[in] pluginclassname The name of the class to instantiate
73 */
74 #define DEFINE_SIMULATOR_CLASS(pluginclassname) \
75  extern "C" \
76  { \
77  OPENFLUID_PLUGIN std::string WAREABIVERSION_PROC_DECL() \
78  { \
79  return std::string(openfluid::config::VERSION_FULL); \
80  } \
81  \
82  OPENFLUID_PLUGIN openfluid::ware::PluggableSimulator* WAREBODY_PROC_DECL() \
83  { \
84  return new pluginclassname(); \
85  } \
86  }
87 
88 
89 // =====================================================================
90 // =====================================================================
91 
92 
93 namespace openfluid { namespace ware {
94 
95 
96 /**
97  @brief Abstract class for simulator plugin
98 
99  Abstract class for simulator plugin interface,
100  defining the minimal structure for a simulator. \n
101  All simulators must inherit from this class.
102 
103  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
104 */
106 {
107 
108  private:
109 
110  /**
111  Pointer to the spatial graph, for internal use only
112  */
113  // TODO check here
114  // openfluid::core::SpatialGraph* mp_InternalSpatialGraph;
115 
116 
117  int m_MaxThreads;
118 
119 
120  protected:
121 
122  /**
123  Returns true if the parameter exists
124  @param[in] Params the parameters set for the simulator
125  @param[in] ParamName the name of the requested parameter
126  @return true if the parameter exists, false otherwise
127 
128  @cond OpenFLUID:completion
129  {
130  "contexts" : ["SIMULATOR"],
131  "menupath" : ["Compute code", "Parameters"],
132  "title" : "Test if a simulator parameter exists",
133  "text" : "OPENFLUID_IsSimulatorParameterExist(Params,%%SEL_START%%ParamName%%SEL_END%%)"
134  }
135  @endcond
136  */
137  bool OPENFLUID_IsSimulatorParameterExist(const openfluid::ware::WareParams_t& Params,
138  const openfluid::ware::WareParamKey_t& ParamName) const;
139 
140  /**
141  Returns a simulator parameter from the parameters set, as a StringValue
142  @param[in] Params the parameters set for the simulator
143  @param[in] ParamName the name of the requested parameter
144  @return the value of the requested parameter
145 
146  @cond OpenFLUID:completion
147  {
148  "contexts" : ["SIMULATOR"],
149  "menupath" : ["Compute code", "Parameters"],
150  "title" : "Get simulator parameter (by return)",
151  "text" : "OPENFLUID_GetSimulatorParameter(Params,%%SEL_START%%ParamName%%SEL_END%%)"
152  }
153  @endcond
154  */
155  openfluid::core::StringValue OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
156  const openfluid::ware::WareParamKey_t& ParamName)
157  const;
158 
159  /**
160  Gets a simulator parameter from the parameters set, as a StringValue
161  @param[in] Params the parameters set for the simulator
162  @param[in] ParamName the name of the requested parameter
163  @param[out] Val the value of the requested parameter
164  @return true if the parameter exists, false otherwise
165 
166  @cond OpenFLUID:completion
167  {
168  "contexts" : ["SIMULATOR"],
169  "menupath" : ["Compute code", "Parameters"],
170  "title" : "Get simulator parameter",
171  "text" : "OPENFLUID_GetSimulatorParameter(Params,%%SEL_START%%ParamName%%SEL_END%%,Val)"
172  }
173  @endcond
174  */
175  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
176  const openfluid::ware::WareParamKey_t& ParamName,
177  openfluid::core::StringValue& Val) const;
178 
179  /**
180  Gets a simulator parameter from the parameters set, as a DoubleValue
181  @param[in] Params the parameters set for the simulator
182  @param[in] ParamName the name of the requested parameter
183  @param[out] Val the value of the requested parameter
184  @return true if the parameter exists, false otherwise
185  */
186  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
187  const openfluid::ware::WareParamKey_t& ParamName,
188  openfluid::core::DoubleValue& Val) const;
189 
190  /**
191  Gets a simulator parameter from the parameters set, as a VectorValue
192  @param[in] Params the parameters set for the simulator
193  @param[in] ParamName the name of the requested parameter
194  @param[out] Val the value of the requested parameter
195  @return true if the parameter exists, false otherwise
196  */
197  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
198  const openfluid::ware::WareParamKey_t& ParamName,
199  openfluid::core::VectorValue& Val) const;
200 
201  /**
202  Gets a simulator parameter from the parameters set, as a MatrixValue
203  @param[in] Params the parameters set for the simulator
204  @param[in] ParamName the name of the requested parameter
205  @param[out] Val the value of the requested parameter
206  @return true if the parameter exists, false otherwise
207  */
208  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
209  const openfluid::ware::WareParamKey_t& ParamName,
210  openfluid::core::MatrixValue& Val) const;
211 
212  /**
213  Gets a simulator parameter from the parameters set, as a double
214  @param[in] Params the parameters set for the simulator
215  @param[in] ParamName the name of the requested parameter
216  @param[out] Val the value of the requested parameter
217  @return true if the parameter exists, false otherwise
218  */
219  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
220  const openfluid::ware::WareParamKey_t& ParamName,
221  double& Val) const;
222 
223  /**
224  Gets a simulator parameter value from the parameters set, as a long int
225  @param[in] Params the parameters set for the simulator
226  @param[in] ParamName the name of the requested parameter
227  @param[out] Val the value of the requested parameter
228  @return true if the parameter exists, false otherwise
229  */
230  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
231  const openfluid::ware::WareParamKey_t& ParamName,
232  long& Val) const;
233 
234  /**
235  Gets a simulator parameter from the parameters set, as a float
236  @param[in] Params the parameters set for the simulator
237  @param[in] ParamName the name of the requested parameter
238  @param[out] Val the value of the requested parameter
239  @return true if the parameter exists, false otherwise
240  */
241  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
242  const openfluid::ware::WareParamKey_t& ParamName,
243  float& Val) const;
244 
245  /**
246  Gets a simulator parameter value from the parameters set, as an int
247  @param[in] Params the parameters set for the simulator
248  @param[in] ParamName the name of the requested parameter
249  @param[out] Val the value of the requested parameter
250  @return true if the parameter exists, false otherwise
251  */
252  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
253  const openfluid::ware::WareParamKey_t& ParamName,
254  int& Val) const;
255 
256  /**
257  Gets a simulator parameter value from the parameters set, as a string
258  @param[in] Params the parameters set for the simulator
259  @param[in] ParamName the name of the requested parameter
260  @param[out] Val the value of the requested parameter
261  @return true if the parameter exists, false otherwise
262  */
263  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
264  const openfluid::ware::WareParamKey_t& ParamName,
265  std::string& Val) const;
266 
267  /**
268  Gets a simulator parameter vector of values from the parameters set, as a vector of strings
269  @param[in] Params the parameters set for the simulator
270  @param[in] ParamName the name of the requested parameter
271  @param[out] Vals the vector of values of the requested parameter
272  @return true if the parameter exists, false otherwise
273  */
274  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
275  const openfluid::ware::WareParamKey_t& ParamName,
276  std::vector<std::string>& Vals) const;
277 
278  /**
279  Gets a simulator parameter vector of values from the parameters set, as a vector of doubles
280  @param[in] Params the parameters set for the simulator
281  @param[in] ParamName the name of the requested parameter
282  @param[out] Vals the vector of values of the requested parameter
283  @return true if the parameter exists, false otherwise
284  */
285  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
286  const openfluid::ware::WareParamKey_t& ParamName,
287  std::vector<double>& Vals) const;
288 
289  /**
290  Gets a simulator parameter vector of values from the parameters set, as a vector of long ints
291  @param[in] Params the parameters set for the simulator
292  @param[in] ParamName the name of the requested parameter
293  @param[out] Vals the vector of values of the requested parameter
294  @return true if the parameter exists, false otherwise
295  */
296  bool OPENFLUID_GetSimulatorParameter(const openfluid::ware::WareParams_t& Params,
297  const openfluid::ware::WareParamKey_t& ParamName,
298  std::vector<long>& Vals) const;
299 
300  /**
301  Returns the maximum number of threads that can be concurrently run in threaded spatial loops
302  @return the number of threads
303 
304  @cond OpenFLUID:completion
305  {
306  "contexts" : ["SIMULATOR"],
307  "menupath" : ["Compute code", "Threading"],
308  "title" : "Get maximum number of threads",
309  "text" : "OPENFLUID_GetSimulatorMaxThreads()"
310  }
311  @endcond
312  */
314  {
315  return m_MaxThreads;
316  }
317 
318  /**
319  Sets the maximum number of threads that can be concurrently run in threaded spatial loops
320  @param[in] MaxNumThreads the number of threads
321 
322  @cond OpenFLUID:completion
323  {
324  "contexts" : ["SIMULATOR"],
325  "menupath" : ["Compute code", "Threading"],
326  "title" : "Set maximum number of threads",
327  "text" : "OPENFLUID_SetSimulatorMaxThreads(%%SEL_START%%Number%%SEL_END%%)"
328  }
329  @endcond
330  */
331  void OPENFLUID_SetSimulatorMaxThreads(const int& MaxNumThreads);
332 
333  /**
334  Returns a scheduling request to a single scheduling at the end
335  Return the corresponding scheduling request
336 
337  @cond OpenFLUID:completion
338  {
339  "contexts" : ["SIMULATOR"],
340  "menupath" : ["Compute code", "Sheduling"],
341  "title" : "Run once at the end",
342  "text" : "AtTheEnd()"
343  }
344  @endcond
345  */
347  {
349  }
350 
351  /**
352  Returns a scheduling request to never be scheduled again
353  Return the corresponding scheduling request
354 
355  @cond OpenFLUID:completion
356  {
357  "contexts" : ["SIMULATOR"],
358  "menupath" : ["Compute code", "Sheduling"],
359  "title" : "Never run again",
360  "text" : "Never()"
361  }
362  @endcond
363  */
365  {
367  }
368 
369  /**
370  Returns a scheduling request for the default DeltaT value
371  Return the corresponding scheduling request
372 
373  @cond OpenFLUID:completion
374  {
375  "contexts" : ["SIMULATOR"],
376  "menupath" : ["Compute code", "Sheduling"],
377  "title" : "Run in default deltaT",
378  "text" : "DefaultDeltaT()"
379  }
380  @endcond
381  */
383  {
384  return openfluid::base::SchedulingRequest(OPENFLUID_GetDefaultDeltaT());
385  }
386 
387  /**
388  Returns a scheduling request for the default DeltaT value multiplied by a factor
389  @param[in] Mult the multiplication factor
390  Return the corresponding scheduling request
391 
392  @cond OpenFLUID:completion
393  {
394  "contexts" : ["SIMULATOR"],
395  "menupath" : ["Compute code", "Sheduling"],
396  "title" : "Run in X default deltaT",
397  "text" : "MultipliedDefaultDeltaT(%%SEL_START%%X%%SEL_END%%)"
398  }
399  @endcond
400  */
402  {
403  return openfluid::base::SchedulingRequest(Mult * OPENFLUID_GetDefaultDeltaT());
404  }
405 
406  /**
407  Returns a scheduling request for a given duration
408  @param[in] D a duration in seconds
409  Return the corresponding scheduling request
410 
411  @cond OpenFLUID:completion
412  {
413  "contexts" : ["SIMULATOR"],
414  "menupath" : ["Compute code", "Sheduling"],
415  "title" : "Run in a duration in seconds",
416  "text" : "Duration(%%SEL_START%%Seconds%%SEL_END%%)"
417  }
418  @endcond
419  */
421  {
423  }
424 
425 
426  public:
427  /**
428  Constructor
429  */
431 
432  /**
433  Virtual destructor
434  */
435  virtual ~PluggableSimulator();
436 
437  /**
438  Internally called by the framework.
439  */
440  void initializeWare(const WareID_t& SimID,const unsigned int& MaxThreads);
441 
442  /**
443  Initializes simulator parameters of the simulator, given as a hash map. Internally called by the framework.
444  */
445  virtual void initParams(const openfluid::ware::WareParams_t& Params)=0;
446 
447  /**
448  Prepares data. Internally called by the framework.
449  */
450  virtual void prepareData()=0;
451 
452  /**
453  Checks the simulator consistency. Internally called by the framework.
454  */
455  virtual void checkConsistency()=0;
456 
457  /**
458  Internally called by the framework.
459  */
460  virtual openfluid::base::SchedulingRequest initializeRun()=0;
461 
462  /**
463  Internally called by the framework.
464  */
465  virtual openfluid::base::SchedulingRequest runStep()=0;
466 
467  /**
468  Internally called by the framework.
469  */
470  virtual void finalizeRun()=0;
471 
472 };
473 
474 
475 typedef PluggableSimulator* (*GetPluggableSimulatorBodyProc)();
476 
477 typedef SimulatorSignature* (*GetPluggableSimulatorSignatureProc)();
478 
479 
480 } } // namespaces
481 
482 
483 #endif /* __OPENFLUID_WARE_PLUGGABLESIMULATOR_HPP__ */
openfluid::base::SchedulingRequest Never() const
Definition: PluggableSimulator.hpp:364
openfluid::base::SchedulingRequest DefaultDeltaT() const
Definition: PluggableSimulator.hpp:382
std::string WareID_t
Definition: TypeDefs.hpp:49
std::string WareParamKey_t
Definition: TypeDefs.hpp:124
Definition: SimulatorSignature.hpp:306
Abstract class for simulator plugin.
Definition: PluggableSimulator.hpp:105
Definition: StringValue.hpp:76
std::uint64_t Duration_t
Definition: TypeDefs.hpp:312
Definition: VectorValue.hpp:84
openfluid::base::SchedulingRequest Duration(const openfluid::core::Duration_t &D) const
Definition: PluggableSimulator.hpp:420
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:128
Definition: SchedulingRequest.hpp:57
Definition: SchedulingRequest.hpp:53
Definition: DoubleValue.hpp:80
Definition: SimulationContributorWare.hpp:51
Definition: ApplicationException.hpp:47
int OPENFLUID_GetSimulatorMaxThreads() const
Definition: PluggableSimulator.hpp:313
Definition: MatrixValue.hpp:84
Definition: SchedulingRequest.hpp:57
openfluid::base::SchedulingRequest MultipliedDefaultDeltaT(const double &Mult) const
Definition: PluggableSimulator.hpp:401
#define OPENFLUID_API
Definition: dllexport.hpp:86
openfluid::base::SchedulingRequest AtTheEnd() const
Definition: PluggableSimulator.hpp:346