Binding.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  @file Binding.hpp
34 
35  @author Jean-Christophe FABRE <jean-christophe.fabre@inra.fr>
36 */
37 
38 
39 #ifndef __OPENFLUID_UTILS_BINDING_HPP__
40 #define __OPENFLUID_UTILS_BINDING_HPP__
41 
42 
43 #include <clocale>
44 
45 #include <QCoreApplication>
46 
47 #include <openfluid/config.hpp>
48 #include <openfluid/base/Init.hpp>
67 
68 
69 // =====================================================================
70 // =====================================================================
71 
72 
73 #define STRING_TO_ALLOCATED_CARRAY(str,carray) \
74  char* carray = (char*)malloc((str.length()+1)*sizeof(char)); \
75  strcpy(carray,str.c_str());
76 
77 
78 /**
79  Macro to use for definition and initialization of the static part of the openfluid::utils::Binding class.
80  It must be called in the C or C++ code of the binding wrapper.
81  @param[in] erroutclass The class definition for the standard output and errors,
82  derived from the openfluid::utils::BindingAbstractOutErr class.
83 */
84 #define OPENFLUID_BINDING_DEFINE(erroutclass)\
85  bool openfluid::utils::Binding::m_Initialized = false; \
86  int openfluid::utils::Binding::m_qapp_argc = 1; \
87  char openfluid::utils::Binding::m_qapp_arg0[] = "OpenFLUID_Binding"; \
88  char* openfluid::utils::Binding::m_qapp_argv[] = { openfluid::utils::Binding::m_qapp_arg0 , NULL }; \
89  std::string openfluid::utils::Binding::m_LastErrorMsg = ""; \
90  const openfluid::utils::BindingAbstractOutErr* openfluid::utils::Binding::mp_OutErr = new erroutclass();
91 
92 
93 // =====================================================================
94 // =====================================================================
95 
96 
97 // OpenFLUID:stylecheck:!incs
98 
99 
100 namespace openfluid { namespace utils {
101 
102 /**
103  @brief Class for easier binding with other programming languages.
104 
105  Class for easier binding of OpenFLUID with other programming languages, such as Python, R, JavaScript, ...
106  This class provides common methods to open datasets, add wares paths, modify parameters and attributes,
107  run simulations and many more. All of these methods have parameters and return values in plain old types
108  for better interoperability with other languages, excepted for internal data usually handled as "external pointers"
109  in bindings.
110 
111  To use this header-only class
112  @li the header <openfluid/utils/Binding.hpp> must be included in the C or C++ wrapper code
113  specifically developped for the binded language,
114  @li a class derived from the openfluid::utils::BindingAbstractOutErr class must be defined in order to process
115  the standard and error console streams,
116  @li the #OPENFLUID_BINDING_DEFINE macro must be called in the C or C++ wrapper code,
117  with the previous derived class as a parameter.
118 
119  <i>Example</i>
120  @code
121  #include <openfluid/utils/Binding.hpp>
122 
123  class MyErrOut : public openfluid::utils::BindingAbstractOutErr
124  {
125  public:
126 
127  void printfOut(const char* fmt, ... ) const
128  {
129  va_list args;
130  va_start(args,fmt);
131  vprintf(fmt,args);
132  va_end(args);
133  }
134 
135  void printfErr(const char* fmt, ... ) const
136  {
137  va_list args;
138  va_start(args,fmt);
139  vfprintf(stderr,fmt,args);
140  va_end(args);
141  }
142  };
143 
144  OPENFLUID_BINDING_DEFINE(MyErrOut);
145 
146  // proceed here with the use of the binding class features
147  @endcode
148 */
149 class Binding
150 {
151  private:
152 
153  static bool m_Initialized;
154 
155  static int m_qapp_argc;
156  static char m_qapp_arg0[];
157  static char* m_qapp_argv[];
158 
159  static std::string m_LastErrorMsg;
160 
161  static const BindingAbstractOutErr* mp_OutErr;
162 
163  openfluid::base::IOListener m_FluidXListener;
164 
166 
167  bool m_IsProject = false;
168  bool m_IsDataset = false;
169 
170  std::string m_SourcePath = "";
171  std::string m_OutputDir = "";
172 
173  bool m_IsSimulationRun = false;
174 
175 
176  // =====================================================================
177  // =====================================================================
178 
179 
180  Binding() :
181  m_FluidXDesc(&m_FluidXListener)
182  {
183 
184  }
185 
186 
187  // =====================================================================
188  // =====================================================================
189 
190 
191  ~Binding()
192  {
193 
194  }
195 
196 
197  // =====================================================================
198  // =====================================================================
199 
200 
201  std::map<openfluid::core::UnitsClass_t,unsigned int> getUnitsCountByClasses()
202  {
203  std::map<openfluid::core::UnitsClass_t,unsigned int> RetMap;
204 
205  for (const auto& UnitsByClass : m_FluidXDesc.spatialDomain().spatialUnits())
206  {
207  RetMap[UnitsByClass.first] = UnitsByClass.second.size();
208  }
209 
210  return RetMap;
211  }
212 
213 
214  // =====================================================================
215  // =====================================================================
216 
217 
218  public:
219 
220  /**
221  Static initialization of the binding. Muste be called at least once.
222  */
223  static void init()
224  {
225  if (m_Initialized)
226  {
227  return;
228  }
229 
230  INIT_OPENFLUID_APPLICATION(m_qapp_argc,m_qapp_argv);
231 
232  // reset locale for "LC_NUMERIC" To "C"
233  // to prevent from Qt changing locale on init
234  std::setlocale(LC_NUMERIC,"C");
235 
236  m_Initialized = true;
237  }
238 
239 
240  // =====================================================================
241  // =====================================================================
242 
243 
244  /**
245  Returns a new instance
246  @return The newly created instance
247  */
248  static Binding* make()
249  {
250  return (new Binding());
251  }
252 
253 
254  // =====================================================================
255  // =====================================================================
256 
257 
258  /**
259  Deletes a given instance
260  @param[in] B The instance to delete
261  */
262  static void destroy(Binding* B)
263  {
264  delete B;
265  }
266 
267 
268  // =====================================================================
269  // =====================================================================
270 
271 
272  /**
273  Returns the OpenFLUID version
274  */
275  static const char* getVersion()
276  {
277  STRING_TO_ALLOCATED_CARRAY(openfluid::config::VERSION_FULL,CStr);
278  return CStr;
279  }
280 
281 
282  // =====================================================================
283  // =====================================================================
284 
285 
286  /**
287  Returns the last error message
288  */
289  static const char* getLastError()
290  {
291  return m_LastErrorMsg.c_str();
292  }
293 
294 
295  // =====================================================================
296  // =====================================================================
297 
298 
299  /**
300  Add supplementary paths to search for simulators when running simulations
301  @param[in] Paths The paths to add using the path separator used on the current system
302  @see openfluid::base::Environment::addExtraSimulatorsDirs
303  */
304  static void addExtraSimulatorsPaths(const char* Paths)
305  {
307  }
308 
309 
310  // =====================================================================
311  // =====================================================================
312 
313 
314  /**
315  Resets the list of supplementary paths to search for simulators
316  */
318  {
320  }
321 
322 
323  // =====================================================================
324  // =====================================================================
325 
326 
327  /**
328  Returns the number of default paths to search for simulators
329  @return the number of paths
330  */
331  static unsigned int getSimulatorsPathsCount()
332  {
334  }
335 
336 
337  // =====================================================================
338  // =====================================================================
339 
340 
341  /**
342  Returns the default paths to search for simulators
343  @return the paths list
344  */
345  static char** getSimulatorsPaths()
346  {
347  std::vector<std::string> SimsPaths = openfluid::base::Environment::getSimulatorsDirs();
348 
349  const unsigned int Count = SimsPaths.size();
350 
351  char** Paths = (char**)malloc(Count*sizeof(char*));
352 
353  for (unsigned int i=0;i<Count;i++)
354  {
355  Paths[i] = (char*)malloc(SimsPaths[i].size()+1);
356  std::copy(SimsPaths[i].begin(), SimsPaths[i].end(), Paths[i]);
357  Paths[i][SimsPaths[i].size()] = '\0';
358  }
359 
360  return Paths;
361  }
362 
363 
364  // =====================================================================
365  // =====================================================================
366 
367 
368  /**
369  Returns the number of supplementary paths to search for simulators
370  @return the number of paths
371  */
372  static unsigned int getExtraSimulatorsPathsCount()
373  {
375  }
376 
377 
378  // =====================================================================
379  // =====================================================================
380 
381 
382  /**
383  Returns the supplementary paths to search for simulators
384  @return the paths list
385  */
386  static char** getExtraSimulatorsPaths()
387  {
388  std::vector<std::string> ExtraSimsPaths = openfluid::base::Environment::getExtraSimulatorsDirs();
389 
390  const unsigned int Count = ExtraSimsPaths.size();
391 
392  char** Paths = (char**)malloc(Count*sizeof(char*));
393 
394  for (unsigned int i=0;i<Count;i++)
395  {
396  Paths[i] = (char*)malloc(ExtraSimsPaths[i].size()+1);
397  std::copy(ExtraSimsPaths[i].begin(), ExtraSimsPaths[i].end(), Paths[i]);
398  Paths[i][ExtraSimsPaths[i].size()] = '\0';
399  }
400 
401  return Paths;
402  }
403 
404 
405  // =====================================================================
406  // =====================================================================
407 
408 
409  /**
410  Add supplementary paths to search for observers when running simulations
411  @param[in] Paths the paths to add using the path separator used on the current system
412  @see openfluid::base::Environment::addExtraObserversDirs
413  */
414  static void addExtraObserversPaths(const char* Paths)
415  {
417  }
418 
419 
420  // =====================================================================
421  // =====================================================================
422 
423 
424  /**
425  Resets the list of supplementary paths to search for observers
426  */
428  {
430  }
431 
432 
433  // =====================================================================
434  // =====================================================================
435 
436 
437  /**
438  Returns the number of default paths to search for observers
439  @return The number of paths
440  */
441  static unsigned int getObserversPathsCount()
442  {
444  }
445 
446 
447  // =====================================================================
448  // =====================================================================
449 
450 
451  /**
452  Returns the default paths to search for observers
453  @return The paths list
454  */
455  static char** getObserversPaths()
456  {
457  std::vector<std::string> ObsPaths = openfluid::base::Environment::getObserversDirs();
458 
459  const unsigned int Count = ObsPaths.size();
460 
461  char** Paths = (char**)malloc(Count*sizeof(char*));
462 
463  for (unsigned int i=0;i<Count;i++)
464  {
465  Paths[i] = (char*)malloc(ObsPaths[i].size()+1);
466  std::copy(ObsPaths[i].begin(), ObsPaths[i].end(), Paths[i]);
467  Paths[i][ObsPaths[i].size()] = '\0';
468  }
469 
470  return Paths;
471  }
472 
473 
474  // =====================================================================
475  // =====================================================================
476 
477 
478  /**
479  Returns the number of supplementary paths to search for observers
480  @return The number of paths
481  */
482  static unsigned int getExtraObserversPathsCount()
483  {
485  }
486 
487 
488  // =====================================================================
489  // =====================================================================
490 
491 
492  /**
493  Returns the supplementary paths to search for observers
494  @return The paths list
495  */
496  static char** getExtraObserversPaths()
497  {
498  std::vector<std::string> ExtraObsPaths = openfluid::base::Environment::getExtraObserversDirs();
499 
500  const unsigned int Count = ExtraObsPaths.size();
501 
502  char** Paths = (char**)malloc(Count*sizeof(char*));
503 
504  for (unsigned int i=0;i<Count;i++)
505  {
506  Paths[i] = (char*)malloc(ExtraObsPaths[i].size()+1);
507  std::copy(ExtraObsPaths[i].begin(), ExtraObsPaths[i].end(), Paths[i]);
508  Paths[i][ExtraObsPaths[i].size()] = '\0';
509  }
510 
511  return Paths;
512  }
513 
514 
515  // =====================================================================
516  // =====================================================================
517 
518 
519  /**
520  Sets the current output path used by default
521  @param[in] Path The output path to use
522  */
523  static void setCurrentOutputDir(const char* Path)
524  {
525  openfluid::base::RunContextManager::instance()->setOutputDir(std::string(Path));
526  }
527 
528 
529  // =====================================================================
530  // =====================================================================
531 
532 
533  /**
534  Opens an OpenFLUID dataset given by its path
535  @param[in] Path The path to the dataset
536  @returns an instance of the Binding for the opened dataset, NULL if an error occured
537  */
538  static Binding* openDataset(const char* Path)
539  {
540  Binding* Data = new Binding();
541 
542  try
543  {
544  init();
545 
547 
548  openfluid::base::RunContextManager::instance()->setInputDir(std::string(Path));
549  Data->m_FluidXDesc.loadFromDirectory(openfluid::base::RunContextManager::instance()->getInputDir());
550 
551  Data->m_IsSimulationRun = false;
552 
553  if (!Data->m_IsProject)
554  {
555  Data->m_IsDataset = true;
556  Data->m_SourcePath = openfluid::base::RunContextManager::instance()->getInputDir();
557  }
558 
559 
560  return Data;
561  }
562  catch (openfluid::base::Exception& E)
563  {
564  m_LastErrorMsg = "OpenFLUID ERROR: " + std::string(E.what()) +"\n";
565  }
566  catch (std::bad_alloc& E)
567  {
568  m_LastErrorMsg = "MEMORY ALLOCATION ERROR: " + std::string(E.what()) +
569  ". Possibly not enough memory available\n";
570  }
571  catch (std::exception& E)
572  {
573  m_LastErrorMsg = "SYSTEM ERROR: " + std::string(E.what()) +"\n";
574  }
575  catch (...)
576  {
577  m_LastErrorMsg = "UNKNOWN ERROR\n";
578  }
579 
580  Data->m_IsProject = false;
581  Data->m_IsDataset = false;
582  Data->m_SourcePath = "";
583 
584  delete Data;
585 
586  return nullptr;
587  }
588 
589 
590  // =====================================================================
591  // =====================================================================
592 
593 
594  /**
595  Opens an OpenFLUID project given by its path
596  @param[in] Path The path to the project
597  @returns an instance of the Binding for the opened project, NULL if an error occured
598  */
599  static Binding* openProject(const char* Path)
600  {
601  try
602  {
603  init();
604 
605  Binding* Data = new Binding();
606 
607  if (!openfluid::base::RunContextManager::instance()->openProject(std::string(Path)))
608  {
611  std::string(Path) + " is not a correct project path");
612  }
613 
614  Data->m_IsProject = true;
615  Data->m_SourcePath = openfluid::base::RunContextManager::instance()->getProjectPath();
616 
617  return Binding::openDataset(openfluid::base::RunContextManager::instance()->getInputDir().c_str());
618  }
619  catch (openfluid::base::Exception& E)
620  {
621  m_LastErrorMsg = "OpenFLUID ERROR: " + std::string(E.what()) +"\n";
622  }
623  catch (std::bad_alloc& E)
624  {
625  m_LastErrorMsg = "MEMORY ALLOCATION ERROR: " + std::string(E.what()) +
626  ". Possibly not enough memory available\n";
627  }
628  catch (std::exception& E)
629  {
630  m_LastErrorMsg = "SYSTEM ERROR: " + std::string(E.what()) +"\n";
631  }
632  catch (...)
633  {
634  m_LastErrorMsg = "UNKNOWN ERROR\n";
635  }
636 
637  return nullptr;
638  }
639 
640 
641  // =====================================================================
642  // =====================================================================
643 
644 
645  /**
646  Runs a simulation based on the current configuration for simulation.
647  @param[in] IsVerbose Set to true to enable verbosity (false by default).
648  */
649  unsigned short int runSimulation(int IsVerbose = false)
650  {
651  try
652  {
653  init();
654 
656 
657 
660 
661  openfluid::machine::SimulatorPluginsManager::instance()->unloadAllWares();
662 
663 
664  std::unique_ptr<openfluid::machine::MachineListener> Listener;
665 
666  if (IsVerbose)
667  {
668  Listener = std::make_unique<BindingVerboseMachineListener>(mp_OutErr);
669  }
670  else
671  {
672  Listener = std::make_unique<openfluid::machine::MachineListener>();
673  }
674 
675 
676  // ===== spatial domain and run config
677 
678  if (IsVerbose)
679  {
680  mp_OutErr->printfOut("%s","Building spatial domain...");
681  }
682 
684 
685  if (IsVerbose)
686  {
687  static_cast<BindingVerboseMachineListener*>(Listener.get())->
689  }
690 
691 
692  // ===== model instance
693 
694  if (IsVerbose)
695  {
696  mp_OutErr->printfOut("%s","Building model instance...");
697  }
698 
699 
700  openfluid::machine::ModelInstance Model(SimBlob,Listener.get());
701 
703 
704  if (IsVerbose)
705  {
706  static_cast<BindingVerboseMachineListener*>(Listener.get())->
708  }
709 
710 
711  // ===== monitoring instance
712 
713  if (IsVerbose)
714  {
715  mp_OutErr->printfOut("%s","Building monitoring instance...");
716  }
717 
718  openfluid::machine::MonitoringInstance Monitoring(SimBlob);
719 
721  Monitoring);
722 
723  if (IsVerbose)
724  {
725  static_cast<BindingVerboseMachineListener*>(Listener.get())->
727  }
728 
729 
730  // ===== simulation
731 
732  m_OutputDir = openfluid::base::RunContextManager::instance()->getOutputDir();
733 
734 
735  Engine = new openfluid::machine::Engine(SimBlob, Model, Monitoring, Listener.get());
736 
737  Engine->initialize();
738 
739  Engine->initParams();
740  Engine->prepareData();
741  Engine->checkConsistency();
742  Engine->run();
743 
744  Engine->finalize();
745 
746  delete Engine;
747 
748  m_IsSimulationRun = true;
749 
750  return 1;
751  }
752  catch (openfluid::base::Exception& E)
753  {
754  m_LastErrorMsg = "OpenFLUID ERROR: " + std::string(E.what()) +"\n";
755  }
756  catch (std::bad_alloc& E)
757  {
758  m_LastErrorMsg = "MEMORY ALLOCATION ERROR: " + std::string(E.what()) +
759  ". Possibly not enough memory available\n";
760  }
761  catch (std::exception& E)
762  {
763  m_LastErrorMsg = "SYSTEM ERROR: " + std::string(E.what()) +"\n";
764  }
765  catch (...)
766  {
767  m_LastErrorMsg = "UNKNOWN ERROR\n";
768  }
769 
770  return 0;
771  }
772 
773 
774  // =====================================================================
775  // =====================================================================
776 
777 
778  /**
779  Prints informations about simulation configuration (spatial domain, processes model, ...)
780  */
782  {
783  // Spatial domain
784 
785  mp_OutErr->printfOut("Spatial domain is made of %i spatial units\n",
786  m_FluidXDesc.spatialDomain().getUnitsCount());
787 
788  for (const auto& UnitsClass : m_FluidXDesc.spatialDomain().spatialUnits())
789  {
790  mp_OutErr->printfOut(" - %i units of class %s\n",UnitsClass.second.size(),UnitsClass.first.c_str());
791  }
792 
793 
794  // Model
795 
796  mp_OutErr->printfOut("Model is made of %i simulation items\n",m_FluidXDesc.model().items().size());
797 
798  for (const auto& ModelInfos : m_FluidXDesc.model().items())
799  {
800  mp_OutErr->printfOut(" - ");
801 
802  if (ModelInfos->isType(openfluid::ware::WareType::SIMULATOR))
803  {
804  mp_OutErr->printfOut("%s simulator\n",
805  ((openfluid::fluidx::SimulatorDescriptor*)(ModelInfos))->getID().c_str());
806  }
807 
808  if (ModelInfos->isType(openfluid::ware::WareType::GENERATOR))
809  {
811 
813  {
814  mp_OutErr->printfOut("fixed");
815  }
816 
818  {
819  mp_OutErr->printfOut("random");
820  }
821 
823  {
824  mp_OutErr->printfOut("interp");
825  }
826 
828  {
829  mp_OutErr->printfOut("inject");
830  }
831 
832  mp_OutErr->printfOut(" generator for variable %s on units %s\n",
833  pGenDesc->getVariableName().c_str(),pGenDesc->getUnitsClass().c_str());
834  }
835  }
836 
837  // Time period
838 
839  mp_OutErr->printfOut("Simulation period from %s to %s\n",
840  m_FluidXDesc.runConfiguration().getBeginDate().getAsISOString().c_str(),
841  m_FluidXDesc.runConfiguration().getEndDate().getAsISOString().c_str());
842 
843  // Time step
844 
845  mp_OutErr->printfOut("Simulation time step : %i\n",m_FluidXDesc.runConfiguration().getDeltaT());
846 
847  }
848 
849 
850  // =====================================================================
851  // =====================================================================
852 
853 
854  /**
855  Returns the simulation output directory
856  @return The path to the output directory
857  */
859  {
860  STRING_TO_ALLOCATED_CARRAY(m_OutputDir,CStr);
861  return CStr;
862  }
863 
864 
865  // =====================================================================
866  // =====================================================================
867 
868 
869  /**
870  Returns the default delta T for the current simulation
871  @return The delta T
872  */
874  {
875  return m_FluidXDesc.runConfiguration().getDeltaT();
876  }
877 
878 
879  // =====================================================================
880  // =====================================================================
881 
882 
883  /**
884  Sets the default delta T for the current simulation
885  @param[in] DeltaT The delta T to set
886  */
887  void setDefaultDeltaT(int DeltaT)
888  {
889  m_FluidXDesc.runConfiguration().setDeltaT(DeltaT);
890  }
891 
892 
893  // =====================================================================
894  // =====================================================================
895 
896 
897  /**
898  Returns the begin date for the simulation period,
899  as an ISO date-time string (with a space instead of a T letter, e.g. : 2008-01-02 11:13:00)
900  @return The begin date as a string
901  */
902  const char* getPeriodBeginDate()
903  {
904  std::string DateStr = m_FluidXDesc.runConfiguration().getBeginDate().getAsString("%Y-%m-%d %H:%M:%S");
905  STRING_TO_ALLOCATED_CARRAY(DateStr,CStr);
906 
907  return CStr;
908  }
909 
910 
911  // =====================================================================
912  // =====================================================================
913 
914 
915  /**
916  Returns the end date for the simulation period,
917  as an ISO date-time string (with a space instead of a T letter, e.g. : 2011-08-06 15:00:00)
918  @return The end date as a string
919  */
920  const char* getPeriodEndDate()
921  {
922  std::string DateStr = m_FluidXDesc.runConfiguration().getEndDate().getAsString("%Y-%m-%d %H:%M:%S");
923  STRING_TO_ALLOCATED_CARRAY(DateStr,CStr);
924 
925  return CStr;
926  }
927 
928 
929  // =====================================================================
930  // =====================================================================
931 
932 
933  /**
934  Sets the simulation period with begin and end dates. The dates are given as ISO date-time strings
935  (with a space instead of a T letter, e.g. : 2008-01-02 11:13:00, 2011-08-06 15:00:00)
936  @param[in] BeginDate The begin date as a string
937  @param[in] EndDate The end date as a string
938  */
939  void setPeriod(const char* BeginDate, const char* EndDate)
940  {
941  std::string StrBeginDate(BeginDate);
942  std::string StrEndDate(EndDate);
943  openfluid::core::DateTime DateToSet;
944 
945  if (!StrBeginDate.empty() && DateToSet.setFromISOString(StrBeginDate))
946  {
947  m_FluidXDesc.runConfiguration().setBeginDate(DateToSet);
948  }
949 
950  if (!StrEndDate.empty() && DateToSet.setFromISOString(StrEndDate))
951  {
952  m_FluidXDesc.runConfiguration().setEndDate(DateToSet);
953  }
954  }
955 
956 
957  // =====================================================================
958  // =====================================================================
959 
960 
961  /**
962  Returns the IDs list of the simulators used in the current model, as a semicolon separated list.
963  @return The list of IDs
964  */
965  const char* getSimulatorsIDs()
966  {
967  std::string SimIDsStr("");
968  std::ostringstream ssSimIDs;
969  std::string sep = "";
970  openfluid::fluidx::CoupledModelDescriptor ModDesc(m_FluidXDesc.model());
971 
972  for (const auto& Item : ModDesc.items())
973  {
974  if (Item->isType(openfluid::ware::WareType::SIMULATOR))
975  {
976  ssSimIDs << sep << ModDesc.getID(Item);
977  sep = ";";
978  }
979  }
980 
981  SimIDsStr = ssSimIDs.str();
982  STRING_TO_ALLOCATED_CARRAY(SimIDsStr,CStr);
983  return CStr;
984  }
985 
986 
987  // =====================================================================
988  // =====================================================================
989 
990 
991  /**
992  Returns the list of the parameters names for a given simulator, as a semicolon separated list.
993  @param[in] SimID The ID of the simulator
994  @return The list of parameters names
995  */
996  const char* getSimulatorParamNames(const char* SimID)
997  {
998  std::string SimIDStr(SimID);
999  std::string ParamNamesStr("");
1000  std::ostringstream ssParamNames;
1001  std::string sep = "";
1002  openfluid::fluidx::CoupledModelDescriptor ModDesc(m_FluidXDesc.model());
1003  int ItemIndex = ModDesc.findFirstItem(SimIDStr);
1004 
1005  if (ItemIndex < 0)
1006  {
1007  return "";
1008  }
1009 
1010  openfluid::fluidx::ModelItemDescriptor Item = ModDesc.itemAt(ItemIndex);
1012  {
1013  return "";
1014  }
1015 
1016  for (const auto& Param : Item.parameters())
1017  {
1018  ssParamNames << sep << Param.first;
1019  sep = ";";
1020  }
1021 
1022  ParamNamesStr = ssParamNames.str();
1023  STRING_TO_ALLOCATED_CARRAY(ParamNamesStr,CStr);
1024  return CStr;
1025 
1026  }
1027 
1028 
1029  // =====================================================================
1030  // =====================================================================
1031 
1032 
1033  /**
1034  Returns the value of a given simulator parameter, an empty string if not found.
1035  @param[in] SimID The ID of the simulator
1036  @param[in] ParamName The name of the parameter
1037  @return The value of the parameter, as a string
1038  */
1039  const char* getSimulatorParam(const char* SimID, const char* ParamName)
1040  {
1041  std::string ParamValStr("");
1042  std::string SimIDStr(SimID);
1043  std::string ParamNameStr(ParamName);
1044 
1045  for (const auto& ModelInfos : m_FluidXDesc.model().items())
1046  {
1047  if (ModelInfos->isType(openfluid::ware::WareType::SIMULATOR) &&
1048  ((openfluid::fluidx::SimulatorDescriptor*)ModelInfos)->getID() == SimIDStr)
1049  {
1050  openfluid::ware::WareParams_t Params = ModelInfos->getParameters();
1051  openfluid::ware::WareParams_t::iterator ItParam = Params.find(ParamNameStr);
1052 
1053  if (ItParam != Params.end())
1054  {
1055  ParamValStr = (*ItParam).second;
1056  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1057  return CStr;
1058  }
1059  }
1060  }
1061 
1062  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1063  return CStr;
1064  }
1065 
1066 
1067  // =====================================================================
1068  // =====================================================================
1069 
1070 
1071  /**
1072  Sets the value of a given simulator parameter.
1073  @param[in] SimID The ID of the simulator
1074  @param[in] ParamName The name of the parameter
1075  @param[in] ParamVal The new value for the parameter
1076  */
1077  void setSimulatorParam(const char* SimID, const char* ParamName, const char* ParamVal)
1078  {
1079  std::string SimIDStr(SimID);
1080  std::string ParamNameStr(ParamName);
1081  std::string ParamValStr(ParamVal);
1082 
1083  for (auto& ModelInfos : m_FluidXDesc.model().items())
1084  {
1085  if (ModelInfos->isType(openfluid::ware::WareType::SIMULATOR) &&
1086  ((openfluid::fluidx::SimulatorDescriptor*)ModelInfos)->getID() == SimIDStr)
1087  ModelInfos->setParameter(ParamNameStr,ParamValStr);
1088  }
1089  }
1090 
1091 
1092  // =====================================================================
1093  // =====================================================================
1094 
1095 
1096  /**
1097  Removes a given simulator parameter.
1098  @param[in] SimID The ID of the simulator
1099  @param[in] ParamName The name of the parameter
1100  */
1101  void removeSimulatorParam(const char* SimID, const char* ParamName)
1102  {
1103  std::string SimIDStr(SimID);
1104  std::string ParamNameStr(ParamName);
1105 
1106  for (auto& ModelInfos : m_FluidXDesc.model().items())
1107  {
1108  if (ModelInfos->isType(openfluid::ware::WareType::SIMULATOR) &&
1109  ((openfluid::fluidx::SimulatorDescriptor*)ModelInfos)->getID() == SimIDStr)
1110  {
1111  ModelInfos->eraseParameter(ParamNameStr);
1112  }
1113  }
1114  }
1115 
1116 
1117  // =====================================================================
1118  // =====================================================================
1119 
1120 
1121  /**
1122  Returns the list of the parameters names for a given generator, as a semicolon separated list.
1123  @param[in] UnitsClass The name of the spatial units class
1124  @param[in] VarName The name of the generated variable
1125  @return The list of parameters names
1126  */
1127  const char* getGeneratorParamNames(const char* UnitsClass, const char* VarName)
1128  {
1129  std::string VarNameStr(VarName);
1130  std::string UnitsClassStr(UnitsClass);
1131  std::string ParamNamesStr("");
1132  std::ostringstream ssParamNames;
1133  std::string sep = "";
1134 
1135  for (const auto& Item : m_FluidXDesc.model().items())
1136  {
1137  if ( (Item->isType(openfluid::ware::WareType::GENERATOR))
1138  && (((openfluid::fluidx::GeneratorDescriptor*)Item)->getVariableName() == VarNameStr)
1139  && (((openfluid::fluidx::GeneratorDescriptor*)Item)->getUnitsClass() == UnitsClassStr) )
1140  {
1141  for (const auto& Param : Item->parameters())
1142  {
1143  ssParamNames << sep << Param.first;
1144  sep = ";";
1145  }
1146  break;
1147  }
1148  }
1149 
1150  ParamNamesStr = ssParamNames.str();
1151  STRING_TO_ALLOCATED_CARRAY(ParamNamesStr,CStr);
1152  return CStr;
1153 
1154  }
1155 
1156 
1157  // =====================================================================
1158  // =====================================================================
1159 
1160 
1161  /**
1162  Returns the value of a given generator parameter, an empty string if not found.
1163  @param[in] UnitsClass The name of the spatial units class
1164  @param[in] VarName The name of the generated variable
1165  @param[in] ParamName The name of the parameter
1166  @return The value of the parameter, as a string
1167  */
1168  const char* getGeneratorParam(const char* UnitsClass, const char* VarName, const char* ParamName)
1169  {
1170  std::string UnitsClassStr(UnitsClass);
1171  std::string VarNameStr(VarName);
1172  std::string ParamNameStr(ParamName);
1173  std::string ParamValStr("");
1174 
1175  for (const auto& ModelInfos : m_FluidXDesc.model().items())
1176  {
1177  if (ModelInfos->isType(openfluid::ware::WareType::GENERATOR) &&
1178  ((openfluid::fluidx::GeneratorDescriptor*)ModelInfos)->getUnitsClass() == UnitsClassStr &&
1179  ((openfluid::fluidx::GeneratorDescriptor*)ModelInfos)->getVariableName() == VarNameStr)
1180  {
1181  const openfluid::ware::WareParams_t& Params = ModelInfos->getParameters();
1182  openfluid::ware::WareParams_t::const_iterator ItParam = Params.find(ParamNameStr);
1183 
1184  if (ItParam != Params.end())
1185  {
1186  ParamValStr = (*ItParam).second;
1187  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1188  return CStr;
1189  }
1190 
1191  }
1192  }
1193 
1194  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1195  return CStr;
1196  }
1197 
1198 
1199  // =====================================================================
1200  // =====================================================================
1201 
1202 
1203  /**
1204  Sets the value for a given generator parameter, an empty string if not found.
1205  @param[in] UnitsClass The name of the spatial units class
1206  @param[in] VarName The name of the generated variable
1207  @param[in] ParamName The name of the parameter
1208  @param[in] ParamVal The new value for the parameter
1209  */
1210  void setGeneratorParam(const char* UnitsClass, const char* VarName, const char* ParamName, const char* ParamVal)
1211  {
1212  std::string UnitsClassStr(UnitsClass);
1213  std::string VarNameStr(VarName);
1214  std::string ParamNameStr(ParamName);
1215  std::string ParamValStr(ParamVal);
1216 
1217  for (auto& ItModelInfos : m_FluidXDesc.model().items())
1218  {
1219  if (ItModelInfos->isType(openfluid::ware::WareType::GENERATOR) &&
1220  ((openfluid::fluidx::GeneratorDescriptor*)ItModelInfos)->getUnitsClass() == UnitsClassStr &&
1221  ((openfluid::fluidx::GeneratorDescriptor*)ItModelInfos)->getVariableName() == VarNameStr)
1222  {
1223  ItModelInfos->setParameter(ParamNameStr,ParamValStr);
1224  }
1225  }
1226  }
1227 
1228 
1229  // =====================================================================
1230  // =====================================================================
1231 
1232 
1233  /**
1234  Returns the list of the variables produced by generators on a given spatial units class,
1235  as a semicolon separated list.
1236  @param[in] UnitsClass The spatial units class
1237  @return The list of produced variables
1238  */
1239  const char* getGeneratorsVarNames(const char* UnitsClass)
1240  {
1241  std::string VarNamesStr("");
1242  std::string UnitsClassStr(UnitsClass);
1243  std::ostringstream ssVarNames;
1244  std::string sep = "";
1245 
1246  for (const auto& Item : m_FluidXDesc.model().items())
1247  {
1248  if ( (Item->isType(openfluid::ware::WareType::GENERATOR))
1249  && (((openfluid::fluidx::GeneratorDescriptor*)Item)->getUnitsClass() == UnitsClassStr) )
1250  {
1251  ssVarNames << sep << ((openfluid::fluidx::GeneratorDescriptor*)Item)->getVariableName();
1252  sep = ";";
1253  }
1254  }
1255 
1256  VarNamesStr = ssVarNames.str();
1257  STRING_TO_ALLOCATED_CARRAY(VarNamesStr,CStr);
1258  return CStr;
1259 
1260  }
1261 
1262 
1263  // =====================================================================
1264  // =====================================================================
1265 
1266 
1267  /**
1268  Returns the list of the global parameters for the current model, as a semicolon separated list.
1269  @return The list of parameters names
1270  */
1272  {
1273  std::string ParamNamesStr("");
1274  std::ostringstream ssParamNames;
1275  std::string sep = "";
1276 
1277  for (const auto& Param : m_FluidXDesc.model().getGlobalParameters())
1278  {
1279  ssParamNames << sep << Param.first;
1280  sep = ";";
1281  }
1282 
1283  ParamNamesStr = ssParamNames.str();
1284  STRING_TO_ALLOCATED_CARRAY(ParamNamesStr,CStr);
1285  return CStr;
1286  }
1287 
1288 
1289  // =====================================================================
1290  // =====================================================================
1291 
1292 
1293  /**
1294  Returns the value of a given global parameter for the current model.
1295  @param[in] ParamName The name of the parameter
1296  @return The value of the parameter, as a string
1297  */
1298  const char* getModelGlobalParam(const char* ParamName)
1299  {
1300  std::string ParamNameStr(ParamName);
1301  std::string ParamValStr("");
1302 
1303  openfluid::ware::WareParams_t Params = m_FluidXDesc.model().getGlobalParameters();
1304  openfluid::ware::WareParams_t::const_iterator ItParam = Params.find(ParamNameStr);
1305 
1306  if (ItParam != Params.end())
1307  {
1308  ParamValStr = (*ItParam).second;
1309  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1310  return CStr;
1311  }
1312 
1313  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1314  return CStr;
1315  }
1316 
1317 
1318  // =====================================================================
1319  // =====================================================================
1320 
1321 
1322  /**
1323  Sets the value of a given global parameter for the current model.
1324  @param[in] ParamName The name of the parameter
1325  @param[in] ParamVal The new value for the parameter
1326  */
1327  void setModelGlobalParam(const char* ParamName, const char* ParamVal)
1328  {
1329  std::string ParamNameStr(ParamName);
1330  std::string ParamValStr(ParamVal);
1331 
1332  m_FluidXDesc.model().setGlobalParameter(ParamNameStr,ParamValStr);
1333  }
1334 
1335 
1336  // =====================================================================
1337  // =====================================================================
1338 
1339 
1340  /**
1341  Removes a given global parameter for the current model.
1342  @param[in] ParamName The name of the parameter
1343  */
1344  void removeModelGlobalParam(const char* ParamName)
1345  {
1346  std::string ParamNameStr(ParamName);
1347 
1348  m_FluidXDesc.model().eraseGlobalParameter(ParamNameStr);
1349  }
1350 
1351 
1352  // =====================================================================
1353  // =====================================================================
1354 
1355 
1356  /**
1357  Returns the IDs list of the observers used in the current model, as a semicolon separated list.
1358  @return The list of IDs
1359  */
1360  const char* getObserversIDs()
1361  {
1362  std::string ObsIDsStr("");
1363  std::ostringstream ssObsIDs;
1364  std::string sep = "";
1365  const openfluid::fluidx::MonitoringDescriptor& MonDesc = m_FluidXDesc.monitoring();
1366 
1367  for (auto& ItItem : MonDesc.items())
1368  {
1369  ssObsIDs << sep << MonDesc.getID(ItItem);
1370  sep = ";";
1371  }
1372 
1373  ObsIDsStr = ssObsIDs.str();
1374  STRING_TO_ALLOCATED_CARRAY(ObsIDsStr,CStr);
1375  return CStr;
1376  }
1377 
1378 
1379  // =====================================================================
1380  // =====================================================================
1381 
1382 
1383  /**
1384  Returns the list of the parameters names for a given observer, as a semicolon separated list.
1385  @param[in] ObsID The ID of the observer
1386  @return The list of parameters names
1387  */
1388  const char* getObserverParamNames(const char* ObsID)
1389  {
1390  std::string ObsIDStr(ObsID);
1391  std::string ParamNamesStr("");
1392  std::ostringstream ssParamNames;
1393  std::string sep = "";
1394  const openfluid::fluidx::MonitoringDescriptor& MonDesc = m_FluidXDesc.monitoring();
1395 
1396  int ItemIndex = MonDesc.findFirstItem(ObsIDStr);
1397  if (ItemIndex < 0)
1398  {
1399  return "";
1400  }
1401 
1402  openfluid::fluidx::ObserverDescriptor Item = MonDesc.itemAt(ItemIndex);
1404  {
1405  return "";
1406  }
1407 
1408  for (const auto& Param : Item.parameters())
1409  {
1410  ssParamNames << sep << Param.first;
1411  sep = ";";
1412  }
1413 
1414  ParamNamesStr = ssParamNames.str();
1415  STRING_TO_ALLOCATED_CARRAY(ParamNamesStr,CStr);
1416  return CStr;
1417 
1418  }
1419 
1420 
1421  // =====================================================================
1422  // =====================================================================
1423 
1424 
1425  /**
1426  Returns the value of a given observer parameter, an empty string if not found.
1427  @param[in] ObsID The ID of the observer
1428  @param[in] ParamName The name of the parameter
1429  @return The value of the parameter, as a string
1430  */
1431  const char* getObserverParam(const char* ObsID, const char* ParamName)
1432  {
1433  std::string ParamValStr("");
1434  std::string ObsIDStr(ObsID);
1435  std::string ParamNameStr(ParamName);
1436 
1437  for (const auto& ObsInfos : m_FluidXDesc.monitoring().items())
1438  {
1439  if (ObsInfos->isType(openfluid::ware::WareType::OBSERVER) &&
1440  ((openfluid::fluidx::ObserverDescriptor*)ObsInfos)->getID() == ObsIDStr)
1441  {
1442  openfluid::ware::WareParams_t Params = ObsInfos->getParameters();
1443  openfluid::ware::WareParams_t::const_iterator ItParam = Params.find(ParamNameStr);
1444 
1445  if (ItParam != Params.end())
1446  {
1447  ParamValStr = (*ItParam).second;
1448  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1449  return CStr;
1450  }
1451  }
1452  }
1453 
1454  STRING_TO_ALLOCATED_CARRAY(ParamValStr,CStr);
1455  return CStr;
1456  }
1457 
1458 
1459  // =====================================================================
1460  // =====================================================================
1461 
1462 
1463  /**
1464  Sets the value of a given observer parameter.
1465  @param[in] ObsID The ID of the observer
1466  @param[in] ParamName The name of the parameter
1467  @param[in] ParamVal The new value for the parameter
1468  */
1469  void setObserverParam(const char* ObsID, const char* ParamName, const char* ParamVal)
1470  {
1471  std::string ObsIDStr(ObsID);
1472  std::string ParamNameStr(ParamName);
1473  std::string ParamValStr(ParamVal);
1474 
1475  for (auto& ObsInfos : m_FluidXDesc.monitoring().items())
1476  {
1477  if (ObsInfos->isType(openfluid::ware::WareType::OBSERVER) &&
1478  ((openfluid::fluidx::ObserverDescriptor*)ObsInfos)->getID() == ObsIDStr)
1479  {
1480  ObsInfos->setParameter(ParamNameStr,ParamValStr);
1481  }
1482  }
1483  }
1484 
1485 
1486  // =====================================================================
1487  // =====================================================================
1488 
1489 
1490  /**
1491  Removes a given observer parameter.
1492  @param[in] ObsID The ID of the observer
1493  @param[in] ParamName The name of the parameter
1494  */
1495  void removeObserverParam(const char* ObsID, const char* ParamName)
1496  {
1497  std::string ObsIDStr(ObsID);
1498  std::string ParamNameStr(ParamName);
1499 
1500  for (auto& ObsInfos : m_FluidXDesc.monitoring().items())
1501  {
1502  if (ObsInfos->isType(openfluid::ware::WareType::OBSERVER) &&
1503  ((openfluid::fluidx::ObserverDescriptor*)ObsInfos)->getID() == ObsIDStr)
1504  ObsInfos->eraseParameter(ParamNameStr);
1505  }
1506  }
1507 
1508 
1509  // =====================================================================
1510  // =====================================================================
1511 
1512 
1513  /**
1514  Returns the spatial units classes names as a list of strings.
1515  @return The list of spatial units classes
1516  */
1518  {
1519  std::map<openfluid::core::UnitsClass_t,unsigned int> UnitsCountByClasses;
1520  UnitsCountByClasses = getUnitsCountByClasses();
1521 
1522  const unsigned int Count = UnitsCountByClasses.size();
1523 
1524  char** Classes = (char**)malloc(Count*sizeof(char*));
1525 
1526  unsigned int i=0;
1527  for (auto& ItUCC : UnitsCountByClasses)
1528  {
1529  Classes[i] = (char*)malloc(ItUCC.first.size()+1);
1530  std::copy(ItUCC.first.begin(), ItUCC.first.end(), Classes[i]);
1531  Classes[i][ItUCC.first.size()] = '\0';
1532  i++;
1533  }
1534 
1535  return Classes;
1536 
1537  }
1538 
1539 
1540  // =====================================================================
1541  // =====================================================================
1542 
1543 
1544  /**
1545  Returns the number of spatial units classes names.
1546  @return The number of spatial units classes
1547  */
1548  unsigned int getUnitsClassesCount()
1549  {
1550  return getUnitsCountByClasses().size();
1551  }
1552 
1553 
1554  // =====================================================================
1555  // =====================================================================
1556 
1557 
1558  /**
1559  Returns the spatial units IDs for a given spatial units class, as a list of integers.
1560  @return The list of spatial units IDs
1561  */
1562  int* getUnitsIDs(const char* UnitsClass)
1563  {
1564  int* IDs = NULL;
1565 
1566  if (m_FluidXDesc.spatialDomain().isClassNameExists(UnitsClass))
1567  {
1568  const auto IDsOfClass = m_FluidXDesc.spatialDomain().getIDsOfClass(UnitsClass);
1569 
1570  IDs = (int*)malloc(IDsOfClass.size()*sizeof(int));
1571  unsigned int i=0;
1572  for (const auto& ID : IDsOfClass)
1573  {
1574  IDs[i] = ID;
1575  i++;
1576  }
1577 
1578  }
1579 
1580  return IDs;
1581  }
1582 
1583 
1584  // =====================================================================
1585  // =====================================================================
1586 
1587 
1588  /**
1589  Returns the number of spatial units IDs for a given spatial units class.
1590  @return The number of spatial units IDs
1591  */
1592  unsigned int getUnitsIDsCount(const char* UnitsClass)
1593  {
1594  if (m_FluidXDesc.spatialDomain().isClassNameExists(UnitsClass))
1595  {
1596  return m_FluidXDesc.spatialDomain().getUnitsCount(UnitsClass);
1597  }
1598 
1599  return 0;
1600  }
1601 
1602 
1603  // =====================================================================
1604  // =====================================================================
1605 
1606 
1607  /**
1608  Returns the list of the attributes names for a given spatial units class, as a semicolon separated list.
1609  @param[in] UnitsClass The units class
1610  @return The list of attributes names
1611  */
1612  const char* getAttributesNames(const char* UnitsClass)
1613  {
1614  std::string UnitClassStr(UnitsClass);
1615  std::string AttrNamesStr("");
1616  std::ostringstream ssAttrNames;
1617  std::string sep = "";
1618 
1619  for (const auto& Name : m_FluidXDesc.spatialDomain().getAttributesNames(UnitsClass))
1620  {
1621  ssAttrNames << sep << Name;
1622  sep = ";";
1623  }
1624 
1625  AttrNamesStr = ssAttrNames.str();
1626  STRING_TO_ALLOCATED_CARRAY(AttrNamesStr,CStr);
1627  return CStr;
1628  }
1629 
1630 
1631  // =====================================================================
1632  // =====================================================================
1633 
1634 
1635  /**
1636  Creates an attribute for all spatial units of a given class, with a default value for this attribute.
1637  @param[in] UnitsClass The spatial units class
1638  @param[in] AttrName The name of the attribute
1639  @param[in] AttrVal The default value for this attribute
1640  */
1641  void createAttribute(const char* UnitsClass, const char* AttrName, const char* AttrVal)
1642  {
1643  std::string UnitsClassStr(UnitsClass);
1644  std::string AttrNameStr(AttrName);
1645  std::string AttrValStr(AttrVal);
1646 
1647  if (m_FluidXDesc.spatialDomain().isClassNameExists(UnitsClassStr))
1648  {
1649  m_FluidXDesc.spatialDomain().addAttribute(UnitsClass,AttrNameStr,AttrValStr);
1650  }
1651  }
1652 
1653 
1654  // =====================================================================
1655  // =====================================================================
1656 
1657 
1658  /**
1659  Returns the value of an attribute of a spatial unit.
1660  @param[in] UnitsClass The spatial units class
1661  @param[in] UnitID The spatial unit ID
1662  @param[in] AttrName The name of the attribute
1663  @return The value of the attribute, as a string
1664  */
1665  const char* getAttribute(const char* UnitsClass, int UnitID, const char* AttrName)
1666  {
1667  std::string UnitClassStr(UnitsClass);
1668  std::string AttrNameStr(AttrName);
1669  std::string AttrValStr("");
1670 
1671  try
1672  {
1673  AttrValStr = m_FluidXDesc.spatialDomain().getAttribute(UnitClassStr,UnitID,AttrNameStr);
1674  }
1676  {
1677  // do nothing
1678  }
1679 
1680  STRING_TO_ALLOCATED_CARRAY(AttrValStr,CStr);
1681  return CStr;
1682  }
1683 
1684 
1685  // =====================================================================
1686  // =====================================================================
1687 
1688 
1689  /**
1690  Sets an attribute value for a given spatial unit.
1691  @param[in] UnitsClass The spatial units class
1692  @param[in] UnitID The spatial unit ID
1693  @param[in] AttrName The name of the attribute
1694  @param[in] AttrVal The new value for this attribute
1695  */
1696  void setAttribute(const char* UnitsClass, int UnitID, const char* AttrName, const char* AttrVal)
1697  {
1698  std::string UnitClassStr(UnitsClass);
1699  std::string AttrNameStr(AttrName);
1700  std::string AttrValStr(AttrVal);
1701 
1702  try
1703  {
1704  m_FluidXDesc.spatialDomain().setAttribute(UnitClassStr,UnitID,AttrNameStr,AttrValStr);
1705  }
1707  {
1708  // do nothing
1709  }
1710  }
1711 
1712 
1713  // =====================================================================
1714  // =====================================================================
1715 
1716 
1717  /**
1718  Removes an attribute for all spatial units of a given class.
1719  @param[in] UnitsClass The spatial units class
1720  @param[in] AttrName The name of the attribute
1721  */
1722  void removeAttribute(const char* UnitsClass, const char* AttrName)
1723  {
1724  std::string UnitClassStr(UnitsClass);
1725  std::string AttrNameStr(AttrName);
1726 
1727  try
1728  {
1729  m_FluidXDesc.spatialDomain().deleteAttribute(UnitClassStr,AttrNameStr);
1730  }
1732  {
1733  // do nothing
1734  }
1735  }
1736 
1737 
1738  // =====================================================================
1739  // =====================================================================
1740 
1741 
1742  /**
1743  Adds simulation variable to be automatically exported using CSV format.
1744  @param[in] BindingName The name used as an identifier in output files names
1745  @param[in] UnitsClass The name of the spatial units class
1746  @param[in] UnitsIDs The semicolon separated list of spatial units IDs, can be '*' for all spatial units
1747  @param[in] VarName The name of the variable
1748  @param[in] Precision The floating precision to use for floating point representation,
1749  default precision is used if this parameter is less or equal to 0
1750  */
1751  void addVariablesExportAsCSV(const char* BindingName,
1752  const char* UnitsClass, const char* UnitsIDs,
1753  const char* VarName, int Precision)
1754  {
1755  std::string BindingNameStr(BindingName);
1756  std::string UnitsClassStr(UnitsClass);
1757  std::string UnitsIDsStr(UnitsIDs);
1758  std::string VarNameStr(VarName);
1759  openfluid::fluidx::MonitoringDescriptor& MonDesc = m_FluidXDesc.monitoring();
1760 
1761  // 1. add CSV observer if not present
1762 
1763  if (MonDesc.findFirstItem("export.vars.files.csv") < 0)
1764  {
1765  MonDesc.appendItem(new openfluid::fluidx::ObserverDescriptor("export.vars.files.csv"));
1766  }
1767 
1768  openfluid::fluidx::ObserverDescriptor& ObsDesc = MonDesc.itemAt(MonDesc.findFirstItem("export.vars.files.csv"));
1769 
1770  // 2. add format for binding
1771  ObsDesc.setParameter("format."+BindingNameStr+".header",openfluid::core::StringValue("colnames-as-data"));
1772  ObsDesc.setParameter("format."+BindingNameStr+".date",openfluid::core::StringValue("%Y%m%d-%H%M%S"));
1773  ObsDesc.setParameter("format."+BindingNameStr+".colsep",openfluid::core::StringValue(" "));
1774  if (Precision > 0)
1775  {
1776  std::ostringstream ssPrec;
1777  ssPrec << Precision;
1778  std::string PrecStr(ssPrec.str());
1779  ObsDesc.setParameter("format."+BindingNameStr+".precision",openfluid::core::StringValue(PrecStr));
1780  }
1781 
1782  // 3. add binding output set
1783  ObsDesc.setParameter("set."+BindingNameStr+UnitsClassStr+".unitsclass",
1784  openfluid::core::StringValue(UnitsClassStr));
1785  ObsDesc.setParameter("set."+BindingNameStr+UnitsClassStr+".unitsIDs",
1786  openfluid::core::StringValue(UnitsIDsStr));
1787  ObsDesc.setParameter("set."+BindingNameStr+UnitsClassStr+".vars",
1788  openfluid::core::StringValue(VarNameStr));
1789  ObsDesc.setParameter("set."+BindingNameStr+UnitsClassStr+".format",
1790  openfluid::core::StringValue(BindingNameStr));
1791  }
1792 
1793 };
1794 
1795 
1796 } } // namespaces
1797 
1798 
1799 #endif /* __OPENFLUID_UTILS_BINDING_HPP__ */
Definition: SimulationBlob.hpp:53
const char * getSimulatorParamNames(const char *SimID)
Definition: Binding.hpp:996
const char * getSimulatorsIDs()
Definition: Binding.hpp:965
void setAttribute(const openfluid::core::UnitsClass_t &ClassName, const openfluid::core::UnitID_t &ID, const openfluid::core::AttributeName_t &AttrName, const std::string &AttrValue)
Definition: ObserverDescriptor.hpp:50
Definition: BindingAbstractOutErr.hpp:46
Definition: GeneratorDescriptor.hpp:54
void setModelGlobalParam(const char *ParamName, const char *ParamVal)
Definition: Binding.hpp:1327
static Binding * make()
Definition: Binding.hpp:248
static const char * getLastError()
Definition: Binding.hpp:289
Definition: MonitoringInstance.hpp:58
static char ** getExtraObserversPaths()
Definition: Binding.hpp:496
void setDefaultDeltaT(int DeltaT)
Definition: Binding.hpp:887
openfluid::core::DateTime getBeginDate() const
Definition: RunConfigurationDescriptor.hpp:81
static void addExtraObserversDirs(const std::string &Paths)
Definition: GeneratorDescriptor.hpp:54
Definition: Engine.hpp:75
void setPeriod(const char *BeginDate, const char *EndDate)
Definition: Binding.hpp:939
static void destroy(Binding *B)
Definition: Binding.hpp:262
static const char * getVersion()
Definition: Binding.hpp:275
static void resetExtraObserversDirs()
void eraseGlobalParameter(const openfluid::ware::WareParamKey_t &Key)
Definition: WareSetDescriptor.hpp:146
Definition: GeneratorDescriptor.hpp:54
openfluid::ware::WareID_t getID(ModelItemDescriptor *Item) const
std::map< WareParamKey_t, WareParamValue_t > WareParams_t
Definition: TypeDefs.hpp:128
const char * getObserverParam(const char *ObsID, const char *ParamName)
Definition: Binding.hpp:1431
void setGeneratorParam(const char *UnitsClass, const char *VarName, const char *ParamName, const char *ParamVal)
Definition: Binding.hpp:1210
static char ** getObserversPaths()
Definition: Binding.hpp:455
Definition: GeneratorDescriptor.hpp:51
const char * getGeneratorParamNames(const char *UnitsClass, const char *VarName)
Definition: Binding.hpp:1127
const char * getGeneratorParam(const char *UnitsClass, const char *VarName, const char *ParamName)
Definition: Binding.hpp:1168
static void setCurrentOutputDir(const char *Path)
Definition: Binding.hpp:523
static std::vector< std::string > getSimulatorsDirs()
void appendItem(T *Item)
Definition: WareSetDescriptor.hpp:156
static void addExtraObserversPaths(const char *Paths)
Definition: Binding.hpp:414
static Binding * openDataset(const char *Path)
Definition: Binding.hpp:538
void removeAttribute(const char *UnitsClass, const char *AttrName)
Definition: Binding.hpp:1722
SpatialUnitsByIDByClass_t & spatialUnits()
Definition: SpatialDomainDescriptor.hpp:101
Definition: CoupledModelDescriptor.hpp:52
SetDescription_t & items()
Definition: WareSetDescriptor.hpp:90
static Binding * openProject(const char *Path)
Definition: Binding.hpp:599
void loadFromDirectory(const std::string &DirPath)
Definition: ApplicationException.hpp:50
int getDeltaT() const
Definition: RunConfigurationDescriptor.hpp:101
Definition: ModelInstance.hpp:65
Definition: SimulatorDescriptor.hpp:51
openfluid::fluidx::RunConfigurationDescriptor & runConfiguration()
Definition: FluidXDescriptor.hpp:194
unsigned short int runSimulation(int IsVerbose=false)
Definition: Binding.hpp:649
int findFirstItem(const openfluid::ware::WareID_t &ID) const
Definition: WareSetDescriptor.hpp:319
bool setFromISOString(const std::string &DateTimeStr)
Definition: MonitoringDescriptor.hpp:52
Class for easier binding with other programming languages.
Definition: Binding.hpp:149
static unsigned int getExtraObserversPathsCount()
Definition: Binding.hpp:482
Definition: ApplicationException.hpp:47
std::string getAttribute(const openfluid::core::UnitsClass_t &ClassName, const openfluid::core::UnitID_t &ID, const openfluid::core::AttributeName_t &AttrName) const
void setObserverParam(const char *ObsID, const char *ParamName, const char *ParamVal)
Definition: Binding.hpp:1469
static void buildMonitoringInstanceFromDescriptor(const openfluid::fluidx::MonitoringDescriptor &MonDesc, MonitoringInstance &MonInstance)
const char * getSimulationOutputDir()
Definition: Binding.hpp:858
std::string getAsString(std::string Format) const
std::set< std::string > getAttributesNames(const openfluid::core::UnitsClass_t &ClassName) const
unsigned int getUnitsClassesCount()
Definition: Binding.hpp:1548
unsigned int getUnitsIDsCount(const char *UnitsClass)
Definition: Binding.hpp:1592
std::set< int > getIDsOfClass(const openfluid::core::UnitsClass_t &ClassName) const
Definition: Listener.hpp:54
#define INIT_OPENFLUID_APPLICATION(ac, av)
Definition: Init.hpp:54
bool isClassNameExists(const openfluid::core::UnitsClass_t &ClassName) const
Definition: BindingVerboseMachineListener.hpp:50
void removeSimulatorParam(const char *SimID, const char *ParamName)
Definition: Binding.hpp:1101
bool isType(openfluid::ware::WareType MIType) const
virtual void printfOut(const char *fmt,...) const =0
const char * getObserversIDs()
Definition: Binding.hpp:1360
openfluid::ware::WareParams_t getGlobalParameters() const
Definition: WareSetDescriptor.hpp:136
void setDeltaT(const int DeltaT)
Definition: RunConfigurationDescriptor.hpp:106
const char * getPeriodEndDate()
Definition: Binding.hpp:920
Definition: FrameworkException.hpp:50
static unsigned int getObserversPathsCount()
Definition: Binding.hpp:441
const char * getModelGlobalParamNames()
Definition: Binding.hpp:1271
openfluid::ware::WareID_t getID(ObserverDescriptor *Item) const
static std::vector< std::string > getExtraSimulatorsDirs()
Definition: Environment.hpp:365
const char * getSimulatorParam(const char *SimID, const char *ParamName)
Definition: Binding.hpp:1039
const char * getModelGlobalParam(const char *ParamName)
Definition: Binding.hpp:1298
static char ** getExtraSimulatorsPaths()
Definition: Binding.hpp:386
void setBeginDate(const openfluid::core::DateTime BeginDate)
Definition: RunConfigurationDescriptor.hpp:86
static void init()
Definition: Binding.hpp:223
static void buildSimulationBlobFromDescriptors(const openfluid::fluidx::FluidXDescriptor &FluidXDesc, SimulationBlob &SimBlob)
void printSimulationInfo()
Definition: Binding.hpp:781
const char * getGeneratorsVarNames(const char *UnitsClass)
Definition: Binding.hpp:1239
static void resetExtraObserversPaths()
Definition: Binding.hpp:427
static void addExtraSimulatorsPaths(const char *Paths)
Definition: Binding.hpp:304
openfluid::core::UnitsClass_t getUnitsClass() const
GeneratorMethod getGeneratorMethod() const
void deleteAttribute(const openfluid::core::UnitsClass_t &ClassName, const std::string &AttrName)
void setAttribute(const char *UnitsClass, int UnitID, const char *AttrName, const char *AttrVal)
Definition: Binding.hpp:1696
#define STRING_TO_ALLOCATED_CARRAY(str, carray)
Definition: Binding.hpp:73
openfluid::core::VariableName_t getVariableName() const
void setEndDate(const openfluid::core::DateTime EndDate)
Definition: RunConfigurationDescriptor.hpp:96
void setParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
void setSimulatorParam(const char *SimID, const char *ParamName, const char *ParamVal)
Definition: Binding.hpp:1077
static unsigned int getSimulatorsPathsCount()
Definition: Binding.hpp:331
const char * getPeriodBeginDate()
Definition: Binding.hpp:902
int getDefaultDeltaT()
Definition: Binding.hpp:873
const char * getAttribute(const char *UnitsClass, int UnitID, const char *AttrName)
Definition: Binding.hpp:1665
static void buildModelInstanceFromDescriptor(const openfluid::fluidx::CoupledModelDescriptor &ModelDesc, ModelInstance &MInstance)
int * getUnitsIDs(const char *UnitsClass)
Definition: Binding.hpp:1562
static ExceptionContext computeContext(const std::string &AppName)
Definition: ApplicationException.hpp:76
openfluid::fluidx::SpatialDomainDescriptor & spatialDomain()
Definition: FluidXDescriptor.hpp:184
static std::vector< std::string > getExtraObserversDirs()
Definition: Environment.hpp:408
char ** getUnitsClasses()
Definition: Binding.hpp:1517
openfluid::fluidx::MonitoringDescriptor & monitoring()
Definition: FluidXDescriptor.hpp:214
const char * getObserverParamNames(const char *ObsID)
Definition: Binding.hpp:1388
Definition: GeneratorDescriptor.hpp:54
void setGlobalParameter(const openfluid::ware::WareParamKey_t &Key, const openfluid::ware::WareParamValue_t &Value)
Definition: WareSetDescriptor.hpp:110
openfluid::ware::WareParams_t & parameters()
const char * what() const
Definition: Exception.hpp:87
static void addExtraSimulatorsDirs(const std::string &Paths)
static unsigned int getExtraSimulatorsPathsCount()
Definition: Binding.hpp:372
void addAttribute(const openfluid::core::UnitsClass_t &ClassName, const std::string &AttrName, const std::string &DefaultValue, bool Check=true)
void removeModelGlobalParam(const char *ParamName)
Definition: Binding.hpp:1344
const char * getAttributesNames(const char *UnitsClass)
Definition: Binding.hpp:1612
static std::vector< std::string > getObserversDirs()
void createAttribute(const char *UnitsClass, const char *AttrName, const char *AttrVal)
Definition: Binding.hpp:1641
static void resetExtraSimulatorsPaths()
Definition: Binding.hpp:317
Definition: ModelItemDescriptor.hpp:51
static void resetExtraSimulatorsDirs()
Definition: IOListener.hpp:53
Definition: FluidXDescriptor.hpp:70
T & itemAt(unsigned int Index) const
Definition: WareSetDescriptor.hpp:198
Definition: Exception.hpp:52
void removeObserverParam(const char *ObsID, const char *ParamName)
Definition: Binding.hpp:1495
openfluid::core::DateTime getEndDate() const
Definition: RunConfigurationDescriptor.hpp:91
std::string getAsISOString() const
Definition: StringValue.hpp:88
void addVariablesExportAsCSV(const char *BindingName, const char *UnitsClass, const char *UnitsIDs, const char *VarName, int Precision)
Definition: Binding.hpp:1751
openfluid::fluidx::CoupledModelDescriptor & model()
Definition: FluidXDescriptor.hpp:174
Class for management of date and time information.
Definition: DateTime.hpp:116
static char ** getSimulatorsPaths()
Definition: Binding.hpp:345