KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
kratos_parameters.h
Go to the documentation of this file.
1 // | / |
2 // ' / __| _` | __| _ \ __|
3 // . \ | ( | | ( |\__ `
4 // _|\_\_| \__,_|\__|\___/ ____/
5 // Multi-Physics
6 //
7 // License: BSD License
8 // Kratos default license: kratos/license.txt
9 //
10 // Main authors: Riccardo Rossi
11 // Vicente Mataix Ferrandiz
12 //
13 
14 #pragma once
15 
16 // System includes
17 #include <filesystem>
18 
19 // External includes
20 #include "json/json_fwd.hpp" // Import forward declaration nlohmann json library
21 
22 // Project includes
23 #include "includes/serializer.h"
25 
26 namespace Kratos
27 {
30 
33 
37 
41 
45 
49 
58 class KRATOS_API(KRATOS_CORE) Parameters
59 {
60 private:
63 
69  class KRATOS_API(KRATOS_CORE) iterator_adaptor
70  {
71  public:
74 
75  using iterator_category = std::forward_iterator_tag;
76  using difference_type = std::ptrdiff_t;
77  using value_type = Parameters;
78  using pointer = Parameters*;
79  using reference = Parameters&;
80 
81  using value_iterator = nlohmann::detail::iter_impl<nlohmann::json>;
82 
86 
92  iterator_adaptor(value_iterator itValue, nlohmann::json* pValue, Kratos::shared_ptr<nlohmann::json> pRoot);
93 
98  iterator_adaptor(const iterator_adaptor& itValue);
99 
103 
109  iterator_adaptor& operator++();
110 
117  iterator_adaptor operator++(int);
118 
124  bool operator==(const iterator_adaptor& rhs) const;
125 
131  bool operator!=(const iterator_adaptor& rhs) const;
132 
138  Parameters& operator*() const;
139 
145  Parameters* operator->() const;
146 
150 
155  inline value_iterator GetCurrentIterator() const;
156 
161  const std::string name();
162 
164 
165  private:
168 
169  std::size_t mDistance = 0;
170  nlohmann::json& mrValue;
171  std::unique_ptr<Parameters> mpParameters;
172 
174  };
175 
182  class KRATOS_API(KRATOS_CORE) const_iterator_adaptor
183  {
184  public:
187 
188  using iterator_category = std::forward_iterator_tag;
189  using difference_type = std::ptrdiff_t;
190  using value_type = Parameters;
191  using pointer = const Parameters*;
192  using reference = const Parameters&;
193 
194  using value_iterator = nlohmann::detail::iter_impl<const nlohmann::json>;
195 
199 
205  const_iterator_adaptor(value_iterator itValue, nlohmann::json* pValue, Kratos::shared_ptr<nlohmann::json> pRoot);
206 
212  const_iterator_adaptor(const const_iterator_adaptor& itValue);
213 
217 
223  const_iterator_adaptor& operator++();
224 
231  const_iterator_adaptor operator++(int);
232 
238  bool operator==(const const_iterator_adaptor& rhs) const;
239 
245  bool operator!=(const const_iterator_adaptor& rhs) const;
246 
252  const Parameters& operator*() const;
253 
259  const Parameters* operator->() const;
260 
264 
269  inline value_iterator GetCurrentIterator() const;
270 
275  const std::string name();
276 
278  private:
281 
282  std::size_t mDistance = 0;
283  nlohmann::json& mrValue;
284  std::unique_ptr<Parameters> mpParameters;
285 
287  };
288 
290 
291 public:
294 
296  typedef std::size_t IndexType;
297 
299  typedef std::size_t SizeType;
300 
303 
305  using iterator = iterator_adaptor;
306  using const_iterator = const_iterator_adaptor;
307 
309  typedef nlohmann::detail::iter_impl<nlohmann::json> json_iterator;
310  typedef nlohmann::detail::iter_impl<const nlohmann::json> json_const_iterator;
311  typedef nlohmann::detail::iteration_proxy<json_iterator> json_iteration_proxy;
312  typedef nlohmann::detail::iteration_proxy<json_const_iterator> json_const_iteration_proxy;
313 
317 
322  Parameters();
323 
328  Parameters(const std::string& rJsonString);
329 
334  Parameters(std::ifstream& rStringStream);
335 
337  Parameters(Parameters const& rOther);
338 
340  Parameters(Parameters&& rOther);
341 
343  virtual ~Parameters()
344  {
345 // delete[] mpValue;
346 // mpRoot = nullptr;
347  }
348 
352 
354  Parameters& operator=(Parameters const& rOther);
355 
361  Parameters operator[](const std::string& rEntry);
362 
368  Parameters operator[](const std::string& rEntry) const;
369 
375  Parameters operator[](const IndexType Index);
376 
382  Parameters operator[](const IndexType Index) const;
383 
388  Parameters& operator=(Parameters&& rOther);
389 
393 
398  Parameters Clone() const;
399 
404  const std::string WriteJsonString() const;
405 
410  const std::string PrettyPrintJsonString() const;
411 
417  Parameters GetValue(const std::string& rEntry);
418 
424  Parameters GetValue(const std::string& rEntry) const;
425 
431  void SetValue(
432  const std::string& rEntry,
433  const Parameters& rOtherValue
434  );
435 
441  void AddValue(
442  const std::string& rEntry,
443  const Parameters& rOtherValue
444  );
445 
450  Parameters AddEmptyValue(const std::string& rEntry);
451 
457  bool RemoveValue(const std::string& rEntry);
458 
464  bool RemoveValues(const std::vector<std::string>& rEntries);
465 
470  json_iteration_proxy items() noexcept;
471 
476  json_const_iteration_proxy items() const noexcept;
477 
483  bool Has(const std::string& rEntry) const;
484 
489  bool IsNull() const;
490 
495  bool IsNumber() const;
496 
501  bool IsDouble() const;
502 
507  bool IsInt() const;
508 
513  bool IsBool() const;
514 
519  bool IsString() const;
520 
525  bool IsArray() const;
526 
531  bool IsStringArray() const;
532 
537  bool IsVector() const;
538 
543  bool IsMatrix() const;
544 
550  template <class TValue>
551  bool Is() const;
552 
557  bool IsSubParameter() const;
558 
563  double GetDouble() const;
564 
569  int GetInt() const;
570 
575  bool GetBool() const;
576 
581  std::string GetString() const;
582 
587  std::vector<std::string> GetStringArray() const;
588 
593  Vector GetVector() const;
594 
599  Matrix GetMatrix() const;
600 
606  template <class TValue>
607  TValue Get() const;
608 
613  void SetDouble(const double Value);
614 
619  void SetInt(const int Value);
620 
625  void SetBool(const bool Value);
626 
631  void SetString(const std::string& rValue);
632 
637  void SetStringArray(const std::vector<std::string>& rValue);
638 
643  void SetVector(const Vector& rValue);
644 
649  void SetMatrix(const Matrix& rValue);
650 
657  template <class TValue>
658  void Set(const TValue& rValue);
659 
665  void AddDouble(
666  const std::string& rEntry,
667  const double Value
668  );
669 
675  void AddInt(
676  const std::string& rEntry,
677  const int Value
678  );
679 
685  void AddBool(
686  const std::string& rEntry,
687  const bool Value
688  );
689 
695  void AddString(
696  const std::string& rEntry,
697  const std::string& rValue
698  );
699 
705  void AddStringArray(
706  const std::string& rEntry,
707  const std::vector<std::string>& rValue
708  );
709 
715  void AddVector(
716  const std::string& rEntry,
717  const Vector& rValue
718  );
719 
725  void AddMatrix(
726  const std::string& rEntry,
727  const Matrix& rValue
728  );
729 
734  iterator begin();
735 
740  iterator end();
741 
746  const_iterator begin() const;
747 
753 
758  SizeType size() const;
759 
764  void swap(Parameters& rOther) noexcept;
765 
769  void Reset() noexcept;
770 
777 
784 
790  void SetArrayItem(
792  const Parameters& rOtherArrayItem
793  );
794 
799  void AddEmptyArray(const std::string& rEntry);
800 
805  void Append(const double Value);
806 
811  void Append(const int Value);
812 
817  void Append(const bool Value);
818 
823  void Append(const std::string& rValue);
824 
829  void Append(const Vector& rValue);
830 
835  void Append(const Matrix& rValue);
836 
841  void Append(const Parameters& rValue);
842 
848  void CopyValuesFromExistingParameters(
849  const Parameters OriginParameters,
850  const std::vector<std::string>& rListParametersToCopy
851  );
852 
858  void RecursivelyFindValue(
859  const nlohmann::json& rBaseValue,
860  const nlohmann::json& rValueToFind
861  ) const;
862 
869  bool IsEquivalentTo(Parameters& rParameters);
870 
877  bool HasSameKeysAndTypeOfValuesAs(Parameters& rParameters);
878 
885  void ValidateAndAssignDefaults(const Parameters& rDefaultParameters);
886 
893  void RecursivelyValidateAndAssignDefaults(const Parameters& rDefaultParameters);
894 
901  void AddMissingParameters(const Parameters& rDefaultParameters);
902 
909  void RecursivelyAddMissingParameters(const Parameters& rDefaultParameters);
910 
917  void ValidateDefaults(const Parameters& rDefaultParameters) const;
918 
925  void RecursivelyValidateDefaults(const Parameters& rDefaultParameters) const;
926 
930 
934 
935 
939 
941  virtual std::string Info() const
942  {
943  return this->PrettyPrintJsonString();
944  }
945 
947  virtual void PrintInfo(std::ostream& rOStream) const
948  {
949  rOStream << "Parameters Object " << Info();
950  }
951 
953  virtual void PrintData(std::ostream& rOStream) const
954  {
955 // rOStream << "Parameters Object " << Info();
956  };
957 
958 private:
961 
962  friend class Serializer;
963 
964  void save(Serializer& rSerializer) const;
965 
966  void load(Serializer& rSerializer);
967 
971 
972  nlohmann::json* mpValue;
974 
978 
982 
989  Parameters(nlohmann::json* pValue, Kratos::shared_ptr<nlohmann::json> pRoot);
990 
997  Parameters(json_iterator itValue, nlohmann::json* pValue, Kratos::shared_ptr<nlohmann::json> pRoot);
998 
1005  Parameters(json_const_iterator itValue, nlohmann::json* pValue, Kratos::shared_ptr<nlohmann::json> pRoot);
1006 
1012  nlohmann::json* GetUnderlyingStorage();
1013 
1019  nlohmann::json* GetUnderlyingStorage() const;
1020 
1026  void SetUnderlyingSotrage(nlohmann::json* pNewValue);
1027 
1033  Kratos::shared_ptr<nlohmann::json> GetUnderlyingRootStorage();
1034 
1040  Kratos::shared_ptr<nlohmann::json> GetUnderlyingRootStorage() const;
1041 
1047  void SetUnderlyingRootStorage(Kratos::shared_ptr<nlohmann::json> pNewValue);
1053  void InternalSetValue(const Parameters& rOtherValue);
1054 
1062  void SolveIncludes(nlohmann::json& rJson, const std::filesystem::path& rFileName, std::vector<std::filesystem::path>& rIncludeSequence);
1063 
1069  nlohmann::json ReadFile(const std::filesystem::path& rFileName);
1070 
1071 }; // Parameters class
1072 
1074 
1077 
1078 
1082 
1084 inline std::istream& operator >> (std::istream& rIStream,
1085  Parameters& rThis)
1086 {
1087  return rIStream;
1088 }
1089 
1091 inline std::ostream& operator << (std::ostream& rOStream,
1092  const Parameters& rThis)
1093 {
1094  rThis.PrintInfo(rOStream);
1095  rOStream << std::endl;
1096  rThis.PrintData(rOStream);
1097 
1098  return rOStream;
1099 }
1101 
1103 
1104 } // namespace Kratos.
PeriodicInterfaceProcess & operator=(const PeriodicInterfaceProcess &)=delete
std::string Info() const override
Turn back information as a string.
Definition: periodic_interface_process.hpp:93
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
nlohmann::detail::iteration_proxy< json_const_iterator > json_const_iteration_proxy
Definition: kratos_parameters.h:312
virtual void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: kratos_parameters.h:947
KRATOS_CLASS_POINTER_DEFINITION(Parameters)
Pointer definition of MmgProcess.
nlohmann::detail::iter_impl< const nlohmann::json > json_const_iterator
Definition: kratos_parameters.h:310
nlohmann::detail::iter_impl< nlohmann::json > json_iterator
Iterators from nlohmann::json.
Definition: kratos_parameters.h:309
std::size_t SizeType
Size definition.
Definition: kratos_parameters.h:299
std::size_t IndexType
Index definition.
Definition: kratos_parameters.h:296
virtual void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: kratos_parameters.h:953
virtual ~Parameters()
Destructor.
Definition: kratos_parameters.h:343
const_iterator_adaptor const_iterator
Definition: kratos_parameters.h:306
iterator_adaptor iterator
Definition of the iterators.
Definition: kratos_parameters.h:305
nlohmann::detail::iteration_proxy< json_iterator > json_iteration_proxy
Definition: kratos_parameters.h:311
The serialization consists in storing the state of an object into a storage format like data file or ...
Definition: serializer.h:123
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
end
Definition: DEM_benchmarks.py:180
string path
Definition: DEM_run_all_benchmarks_analysis.py:10
bool Has(const std::string &ModelerName)
Checks if the modeler is registered.
Definition: modeler_factory.cpp:24
void AddValue(GeometryType &rThisGeometry, const TVarType &rThisVariable, const Matrix &rThisValue)
This method adds the value.
pybind11::list items(Parameters const &self)
Definition: add_kratos_parameters_to_python.cpp:24
Parameters GetArrayItem(Parameters &rParameters, const std::size_t Index)
Definition: add_kratos_parameters_to_python.cpp:57
void Append(Parameters &rParameters, const T &obj)
Definition: add_kratos_parameters_to_python.cpp:49
Parameters GetValue(Parameters &rParameters, const std::string &rEntry)
Definition: add_kratos_parameters_to_python.cpp:53
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
Expression::Pointer operator*(const Expression::ConstPointer &rpLeft, const double Right)
std::shared_ptr< T > shared_ptr
Definition: smart_pointers.h:27
bool operator==(const Flags &Left, const Flags &Right)
Definition: flags.cpp:45
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
bool operator!=(const Flags &Left, const Flags &Right)
Definition: flags.cpp:50
def SetValue(entity, variable, value)
Definition: coupling_interface_data.py:256
rhs
Definition: generate_frictional_mortar_condition.py:297
def Index()
Definition: hdf5_io_tools.py:38
def load(f)
Definition: ode_solve.py:307
tuple const
Definition: ode_solve.py:403
def RecursivelyValidateAndAssignDefaults(defaults, settings)
Definition: sdof_solver.py:283
def ValidateAndAssignDefaults(defaults, settings, recursive=False)
Definition: sdof_solver.py:252
namespace
Definition: array_1d.h:793
def IsVector(_X)
Definition: custom_math.py:26