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