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.
vtk_output.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: Aditya Ghantasala
11 // Philipp Bucher (https://github.com/philbucher)
12 // Collaborator: Vicente Mataix Ferrandiz
13 //
14 //
15 
16 #pragma once
17 
18 // System includes
19 #include <unordered_map>
20 
21 // External includes
22 
23 // Project includes
25 #include "includes/io.h"
27 
28 namespace Kratos
29 {
34 class KRATOS_API(KRATOS_CORE) VtkOutput : public IO
35 {
36 public:
37 
39  using SizeType = std::size_t;
40 
42  using IndexType = std::size_t;
43 
45  using NodeType = Node;
46 
49 
52 
55 
61  explicit VtkOutput(
62  ModelPart& rModelPart,
63  Parameters ThisParameters = Parameters(R"({})" )
64  );
65 
67  virtual ~VtkOutput() = default;
68 
72 
76  static Parameters GetDefaultParameters();
77 
81  void PrintOutput(const std::string& rOutputFilename = "");
82 
84 
86  std::string Info() const override
87  {
88  return " VtkOutput object ";
89  }
90 
95  void PrintInfo(std::ostream& rOStream) const override
96  {
97  rOStream << " VtkOutput object " << std::endl;
98  }
99 
101  void PrintData(std::ostream& rOStream) const override
102  {
103  }
104 
105  enum class FileFormat {
106  VTK_ASCII,
107  VTK_BINARY
108  };
109 
110 protected:
113 
114  enum class EntityType {
115  ELEMENT,
116  CONDITION,
117  AUTOMATIC,
118  NONE
119  // GEOMETRY // TODO PBucher
120  };
121 
123 
126 
129 
131  unsigned int mDefaultPrecision;
132  std::unordered_map<int, int> mKratosIdToVtkId;
133  bool mShouldSwap = false;
134 
135  // pointer to object of the extrapolation from gauss point to nodes process
136  IntegrationValuesExtrapolationToNodesProcess::UniquePointer mpGaussToNodesProcess;
137 
141 
146  EntityType GetEntityType(const ModelPart& rModelPart) const;
147 
151  void PrepareGaussPointResults();
152 
159  void WriteModelPartToFile(const ModelPart& rModelPart, const bool IsSubModelPart, const std::string& rOutputFilename);
160 
165  std::string GetOutputFileName(const ModelPart& rModelPart, const bool IsSubModelPart, const std::string& rOutputFilename);
166 
171  void Initialize(const ModelPart& rModelPart);
172 
177  void CreateMapFromKratosIdToVTKId(const ModelPart& rModelPart);
178 
184  void WriteHeaderToFile(const ModelPart& rModelPart, std::ofstream& rFileStream) const;
185 
191  void WriteMeshToFile(const ModelPart& rModelPart, std::ofstream& rFileStream) const;
192 
198  virtual void WriteNodesToFile(const ModelPart& rModelPart, std::ofstream& rFileStream) const;
199 
206  virtual void WriteConditionsAndElementsToFile(const ModelPart& rModelPart, std::ofstream& rFileStream) const;
207 
214  template<typename TContainerType>
215  std::size_t DetermineVtkCellListSize(const TContainerType& rContainer) const;
216 
223  template <typename TContainerType>
224  void WriteConnectivity(const TContainerType& rContainer, std::ofstream& rFileStream) const;
225 
232  template <typename TContainerType>
233  void WriteCellType(const TContainerType& rContainer, std::ofstream& rFileStream) const;
234 
239  bool IsCompatibleVariable(const std::string& rVariableName) const;
240 
246  virtual void WriteNodalResultsToFile(const ModelPart& rModelPart, std::ofstream& rFileStream);
247 
253  void WriteElementResultsToFile(const ModelPart& rModelPart, std::ofstream& rFileStream);
254 
260  void WriteConditionResultsToFile(const ModelPart& rModelPart, std::ofstream& rFileStream);
261 
270  void WriteNodalContainerResults(
271  const std::string& rVariableName,
272  const ModelPart::NodesContainerType& rNodes,
273  const bool IsHistoricalValue,
274  std::ofstream& rFileStream) const;
275 
283  template<typename TContainerType>
284  void WriteGeometricalContainerResults(const std::string& rVariableName,
285  const TContainerType& rContainer,
286  std::ofstream& rFileStream) const;
287 
295  template<typename TContainerType>
296  void WriteGeometricalContainerIntegrationResults(const std::string& rVariableName,
297  const TContainerType& rContainer,
298  std::ofstream& rFileStream) const;
299 
307  template<class TVarType>
308  void WriteNodalScalarValues(
309  const ModelPart::NodesContainerType& rNodes,
310  const TVarType& rVariable,
311  const bool IsHistoricalValue,
312  std::ofstream& rFileStream) const;
313 
321  template<class TVarType>
322  void WriteNodalVectorValues(
323  const ModelPart::NodesContainerType& rNodes,
324  const TVarType& rVariable,
325  const bool IsHistoricalValue,
326  std::ofstream& rFileStream) const;
327 
336  template<typename TContainerType, class TVarType>
337  void WriteScalarSolutionStepVariable(
338  const TContainerType& rContainer,
339  const TVarType& rVariable,
340  std::ofstream& rFileStream) const;
341 
350  template<typename TContainerType, class TVarType>
351  void WriteVectorSolutionStepVariable(
352  const TContainerType& rContainer,
353  const TVarType& rVariable,
354  std::ofstream& rFileStream) const;
355 
364  template<typename TContainerType>
365  void WriteFlagContainerVariable(
366  const TContainerType& rContainer,
367  const Flags Flag,
368  const std::string& rFlagName,
369  std::ofstream& rFileStream) const;
370 
379  template<typename TContainerType, class TVarType>
380  void WriteScalarContainerVariable(
381  const TContainerType& rContainer,
382  const TVarType& rVariable,
383  std::ofstream& rFileStream) const;
384 
393  template<typename TContainerType, class TVarType>
394  void WriteIntegrationScalarContainerVariable(
395  const TContainerType& rContainer,
396  const Variable<TVarType>& rVariable,
397  std::ofstream& rFileStream) const;
398 
408  template<typename TContainerType, class TVarType>
409  void WriteVectorContainerVariable(
410  const TContainerType& rContainer,
411  const TVarType& rVariable,
412  std::ofstream& rFileStream) const;
413 
423  template<typename TContainerType, class TVarType>
424  void WriteIntegrationVectorContainerVariable(
425  const TContainerType& rContainer,
426  const Variable<TVarType>& rVariable,
427  std::ofstream& rFileStream) const;
428 
435  template <typename TData>
436  void WriteScalarDataToFile(const TData& rData, std::ofstream& rFileStream) const
437  {
438  if (mFileFormat == VtkOutput::FileFormat::VTK_ASCII) {
439  rFileStream << rData;
440  } else if (mFileFormat == VtkOutput::FileFormat::VTK_BINARY) {
441  TData data = rData;
442  ForceBigEndian(reinterpret_cast<unsigned char *>(&data));
443  rFileStream.write(reinterpret_cast<char *>(&data), sizeof(TData));
444  }
445  }
446 
453  template <typename TData>
454  void WriteVectorDataToFile(const TData& rData, std::ofstream& rFileStream) const
455  {
456  if (mFileFormat == VtkOutput::FileFormat::VTK_ASCII) {
457  for (const auto& r_data_comp : rData) {
458  rFileStream << float(r_data_comp) << " ";
459  }
460  } else if (mFileFormat == VtkOutput::FileFormat::VTK_BINARY) {
461  for (const auto& r_data_comp : rData ) {
462  float data_comp_local = (float)r_data_comp; // should not be const or a reference for enforcing big endian
463  ForceBigEndian(reinterpret_cast<unsigned char *>(&data_comp_local));
464  rFileStream.write(reinterpret_cast<char *>(&data_comp_local), sizeof(float));
465  }
466  }
467  }
468 
474  void ForceBigEndian(unsigned char* pBytes) const;
475 
477 
478 private:
481  VtkOutput::EntityType mEntityType;
482 
485 
491  GeometryType::Pointer ReorderConnectivity(GeometryType::Pointer& pGeometry) const;
492 
499  template<typename TContainerType>
500  void WritePropertiesIdsToFile(
501  const TContainerType& rContainer,
502  std::ofstream& rFileStream) const;
503 
511  template<typename TContainerType>
512  void WriteIdsToFile(
513  const TContainerType& rContainer,
514  const std::string& DataName,
515  std::ofstream& rFileStream) const;
516 
517 
522  void WriteModelPartWithoutNodesToFile(ModelPart& rModelPart, const std::string& rOutputFilename);
523 
525 };
526 
527 } // namespace Kratos
Definition: flags.h:58
Geometry base class.
Definition: geometry.h:71
IO provides different implementation of input output procedures which can be used to read and write w...
Definition: io.h:58
std::size_t SizeType
Definition: io.h:97
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
MeshType::NodesContainerType NodesContainerType
Nodes container. Which is a vector set of nodes with their Id's as key.
Definition: model_part.h:128
This class defines the node.
Definition: node.h:65
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
VtkOutput A simple class that has functionality to write vtk output.
Definition: vtk_output.h:35
EntityType
Definition: vtk_output.h:114
void WriteScalarDataToFile(const TData &rData, std::ofstream &rFileStream) const
Write the scalar value to the file provided, takes care of binary and ascii formats.
Definition: vtk_output.h:436
ModelPart & mrModelPart
Definition: vtk_output.h:127
std::unordered_map< int, int > mKratosIdToVtkId
The default precision.
Definition: vtk_output.h:132
IntegrationValuesExtrapolationToNodesProcess::UniquePointer mpGaussToNodesProcess
If the bytes need to be swapped (endianness)
Definition: vtk_output.h:136
VtkOutput::FileFormat mFileFormat
The main model part to post process.
Definition: vtk_output.h:128
unsigned int mDefaultPrecision
The configuration parameters.
Definition: vtk_output.h:131
virtual ~VtkOutput()=default
Destructor.
std::string Info() const override
Turn back information as a string.
Definition: vtk_output.h:86
void WriteVectorDataToFile(const TData &rData, std::ofstream &rFileStream) const
Write the vector values to the file provided, takes care of binary and ascii formats.
Definition: vtk_output.h:454
std::size_t IndexType
Definition of the index type.
Definition: vtk_output.h:42
Parameters mOutputSettings
The file format considered.
Definition: vtk_output.h:130
FileFormat
Definition: vtk_output.h:105
void PrintData(std::ostream &rOStream) const override
Print object's data.
Definition: vtk_output.h:101
KRATOS_CLASS_POINTER_DEFINITION(VtkOutput)
Pointer definition of VtkOutput.
void PrintInfo(std::ostream &rOStream) const override
Prints information about the class.
Definition: vtk_output.h:95
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
data
Definition: mesh_to_mdpa_converter.py:59