Manual for OpenFLUID 2.1.10

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