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.
file_name_data_collector.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: Suneth Warnakulasuriya (https://github.com/sunethwarna)
11 //
12 
13 #if !defined(KRATOS_FILE_NAME_DATA_COLLECTOR)
14 #define KRATOS_FILE_NAME_DATA_COLLECTOR
15 
16 // System includes
17 #include <string>
18 #include <unordered_map>
19 #include <vector>
20 
21 // External includes
22 
23 // Project includes
24 #include "includes/define.h"
25 #include "includes/model_part.h"
26 
27 namespace Kratos
28 {
31 
32 class KRATOS_API(KRATOS_CORE) FileNameDataCollector
33 {
34  // private forward declarations
35  class PatternFlag;
36 
37 public:
40 
43 
47 
49  {
50  public:
53 
56 
60 
61  // Default constructor
63  {}
64 
66  const std::string& rFileName,
67  const int Rank,
68  const int Step,
69  const double Time)
70  : mFileName(rFileName),
71  mRank(Rank),
72  mStep(Step),
73  mTime(Time)
74  {}
75 
79 
80  void SetFileName(const std::string& rFileName) { mFileName = rFileName; }
81 
82  std::string GetFileName() const { return mFileName; }
83 
84  void SetRank(const int Rank) { mRank = Rank; }
85 
86  int GetRank() const { return mRank; }
87 
88  void SetStep(const int Step) { mStep = Step; }
89 
90  int GetStep() const { return mStep; }
91 
92  void SetTime(const double Time) { mTime = Time; }
93 
94  double GetTime() const { return mTime; }
95 
99 
100  void Clear()
101  {
102  mFileName = "";
103  mRank = -1;
104  mStep = -1;
105  mTime = -1.0;
106  }
107 
111 
112  bool operator==(const FileNameData& rRHS)
113  {
114  return (mFileName == rRHS.mFileName && mRank == rRHS.mRank &&
115  mStep == rRHS.mStep && mTime == rRHS.mTime);
116  }
117 
119 
120  private:
123 
124  std::string mFileName = "";
125  int mRank = -1;
126  int mStep = -1;
127  double mTime = -1.0;
128 
129  friend class PatternFlag;
130 
132  };
133 
137 
181  const ModelPart& rModelPart,
182  const std::string& rFileNamePattern,
183  const std::unordered_map<std::string, std::string>& rFlagFormatMap);
184 
188 
197  std::string GetFileName() const;
198 
207  std::string GetPath() const;
208 
223  std::vector<std::string> GetSortedFileNamesList(
224  const std::vector<std::string>& rFlagsSortingOrder) const;
225 
245  bool RetrieveFileNameData(
246  FileNameData& rFileNameData,
247  const std::string& rFileNameWithoutPath) const;
248 
257  std::vector<FileNameData> GetFileNameDataList() const;
258 
262 
277  static void SortListOfFileNameData(
278  std::vector<FileNameData>& rFileNameDataList,
279  const std::vector<std::string>& rFlagsSortingOrder);
280 
294  static std::pair<bool, std::string> ExtractFileNamePattern(
295  const std::string& rFileName,
296  const std::vector<std::string>& rFlagsList);
297 
299 
300 private:
303 
310  class KRATOS_API(KRATOS_CORE) PatternFlag
311  {
312  public:
315 
316  // Constructor
326  PatternFlag(
327  const std::string& rPatternFlag,
328  const std::string& rPatternValueFormat = "");
329 
333 
346  bool UpdateFileNameData(
347  FileNameData& rFileNameData,
348  std::size_t& rCurrentPosition,
349  const std::string& rData) const
350  {
351  return (this->*mUpdateData)(rFileNameData, rCurrentPosition, rData);
352  }
353 
360  std::string GetValueString(
361  const ModelPart& rModelPart) const
362  {
363  return (this->*mGetValueString)(rModelPart);
364  }
365 
366 
373  bool IsFlag() const
374  {
375  return (mPatternFlag.front() == '<' && mPatternFlag.back() == '>');
376  }
377 
383  std::string GetPatternFlagString() const
384  {
385  return mPatternFlag;
386  }
387 
391 
408  static bool RetrieveIntegerValue(
409  int& rValue,
410  std::size_t& rCurrentPosition,
411  const std::string& rData);
412 
434  static bool RetrieveFloatingPointValue(
435  double& rValue,
436  std::size_t& rCurrentPosition,
437  const std::string& rData);
438 
440 
441  private:
444 
445  const std::string mPatternFlag;
446  const std::string mPatternValueFormat;
447  bool (PatternFlag::*mUpdateData)(FileNameData& rFileNameData, std::size_t& rCurrentPosition, const std::string& rData) const;
448  std::string (PatternFlag::*mGetValueString)(const ModelPart& rModelPart) const;
449 
453 
454  bool UpdateRank(FileNameData& rFileNameData, std::size_t& rCurrentPosition, const std::string& rData) const;
455  bool UpdateStep(FileNameData& rFileNameData, std::size_t& rCurrentPosition, const std::string& rData) const;
456  bool UpdateTime(FileNameData& rFileNameData, std::size_t& rCurrentPosition, const std::string& rData) const;
457  bool UpdateCustomString(FileNameData& rFileNameData, std::size_t& rCurrentPosition, const std::string& rData) const;
458 
459  std::string GetRankString(const ModelPart& rModelPart) const;
460  std::string GetStepString(const ModelPart& rModelPart) const;
461  std::string GetTimeString(const ModelPart& rModelPart) const;
462  std::string GetCustomString(const ModelPart& rModelPart) const;
463 
471  template <class TDataType>
472  std::string GetFormattedValueString(
473  const TDataType& rData) const
474  {
475  if (mPatternValueFormat == "") {
476  return std::to_string(rData);
477  } else {
478  int length = std::snprintf(nullptr, 0, mPatternValueFormat.c_str(), rData);
479  assert(length >= 0);
480 
481  char* buf = new char[length + 1];
482  std::snprintf(buf, length + 1, mPatternValueFormat.c_str(), rData);
483 
484  std::string data_str(buf);
485  delete[] buf;
486 
487  return data_str;
488  }
489  }
490 
492  };
493 
497 
498  const ModelPart& mrModelPart;
499  std::string mFilePath;
500  std::vector<PatternFlag> mFileNamePatternFlags;
501 
505 
512  static std::vector<std::string> GetPatternFlagStrings(
513  const std::string& rPattern);
514 
522  static void FindAndReplace(
523  std::string& rInputString,
524  const std::string& rSearchString,
525  const std::string& rReplaceString);
526 
528 };
529 
531 
532 } // namespace Kratos
533 
534 #endif // KRATOS_FILE_NAME_DATA_COLLECTOR
Definition: file_name_data_collector.h:49
double GetTime() const
Definition: file_name_data_collector.h:94
void SetFileName(const std::string &rFileName)
Definition: file_name_data_collector.h:80
void SetStep(const int Step)
Definition: file_name_data_collector.h:88
int GetStep() const
Definition: file_name_data_collector.h:90
FileNameData()
Definition: file_name_data_collector.h:62
int GetRank() const
Definition: file_name_data_collector.h:86
void Clear()
Definition: file_name_data_collector.h:100
bool operator==(const FileNameData &rRHS)
Definition: file_name_data_collector.h:112
std::string GetFileName() const
Definition: file_name_data_collector.h:82
void SetRank(const int Rank)
Definition: file_name_data_collector.h:84
KRATOS_CLASS_POINTER_DEFINITION(FileNameData)
Pointer definition of FileNameData.
void SetTime(const double Time)
Definition: file_name_data_collector.h:92
FileNameData(const std::string &rFileName, const int Rank, const int Step, const double Time)
Definition: file_name_data_collector.h:65
Definition: file_name_data_collector.h:33
KRATOS_CLASS_POINTER_DEFINITION(FileNameDataCollector)
Pointer definition of FileNameDataCollector.
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Kratos::ModelPart ModelPart
Definition: kratos_wrapper.h:31
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
REACTION_CHECK_STIFFNESS_FACTOR INNER_LOOP_ITERATION DISTANCE_THRESHOLD ACTIVE_CHECK_FACTOR AUXILIAR_COORDINATES NORMAL_GAP WEIGHTED_GAP WEIGHTED_SCALAR_RESIDUAL bool
Definition: contact_structural_mechanics_application_variables.h:93