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.
mmg_utilities.h
Go to the documentation of this file.
1 // KRATOS __ __ _____ ____ _ _ ___ _ _ ____
2 // | \/ | ____/ ___|| | | |_ _| \ | |/ ___|
3 // | |\/| | _| \___ \| |_| || || \| | | _
4 // | | | | |___ ___) | _ || || |\ | |_| |
5 // |_| |_|_____|____/|_| |_|___|_| \_|\____| APPLICATION
6 //
7 // License: BSD License
8 // license: MeshingApplication/license.txt
9 //
10 // Main authors: Vicente Mataix Ferrandiz
11 //
12 
13 #if !defined(KRATOS_MMG_UTILITIES)
14 #define KRATOS_MMG_UTILITIES
15 
16 // System includes
17 
18 // External includes
19 
20 // Project includes
21 #include "meshing_application.h"
22 #include "includes/key_hash.h"
23 #include "includes/model_part.h"
28 
29 #ifndef MMG_VERSION_GT
30  #define MMG_VERSION_GT(MAJOR,MINOR) false
31 #endif
32 #ifndef MMG_VERSION_GE
33  #define MMG_VERSION_GE(MAJOR,MINOR) false
34 #endif
35 #ifndef MMG_VERSION_LT
36  #define MMG_VERSION_LT(MAJOR,MINOR) false
37 #endif
38 #ifndef MMG_VERSION_LE
39  #define MMG_VERSION_LE(MAJOR,MINOR) false
40 #endif
41 #ifndef MMG_VERSION_MAJOR
42  #define MMG_VERSION_MAJOR 5
43 #endif
44 #ifndef MMG_VERSION_MINOR
45  #define MMG_VERSION_MINOR 5
46 #endif
47 #ifndef MMG_VERSION_PATCH
48  #define MMG_VERSION_PATCH 0
49 #endif
50 
51 #if MMG_VERSION_LT(5,5)
52  #error "ERROR:: The minimal MMG compatible version is 5.5.0. Please update your MMG library"
53 #endif
54 
55 // NOTE: The following contains the license of the MMG library
56 /* =============================================================================
57 ** Copyright (c) Bx INP/Inria/UBordeaux/UPMC, 2004- .
58 **
59 ** mmg is free software: you can redistribute it and/or modify it
60 ** under the terms of the GNU Lesser General Public License as published
61 ** by the Free Software Foundation, either version 3 of the License, or
62 ** (at your option) any later version.
63 **
64 ** mmg is distributed in the hope that it will be useful, but WITHOUT
65 ** ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
66 ** FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
67 ** License for more details.
68 **
69 ** You should have received a copy of the GNU Lesser General Public
70 ** License and of the GNU General Public License along with mmg (in
71 ** files COPYING.LESSER and COPYING). If not, see
72 ** <http://www.gnu.org/licenses/>. Please read their terms carefully and
73 ** use this copy of the mmg distribution only if you accept them.
74 ** =============================================================================
75 */
76 
77 namespace Kratos
78 {
81 
85 
87  typedef std::size_t IndexType;
88 
90  typedef std::size_t SizeType;
91 
93  typedef std::vector<IndexType> IndexVectorType;
94 
98 
102 
106 
113 template<MMGLibrary TMMGLibrary>
115 {
116  // Info stored
123 
128 
133 
138 
143 };
144 
151 template<MMGLibrary TMMGLibrary>
152 class KRATOS_API(MESHING_APPLICATION) MmgUtilities
153 {
154 public:
155 
158 
161 
163  typedef Node NodeType;
164  // Geometry definition
166 
168  static constexpr SizeType Dimension = (TMMGLibrary == MMGLibrary::MMG2D) ? 2 : 3;
169 
171  typedef typename std::conditional<Dimension == 2, array_1d<double, 3>, array_1d<double, 6>>::type TensorArrayType;
172 
174  typedef std::vector<double> DoubleVectorType;
175 
177  typedef std::unordered_map<DoubleVectorType, IndexType, KeyHasherRange<DoubleVectorType>, KeyComparorRange<DoubleVectorType> > DoubleVectorMapType;
178 
180  typedef std::unordered_map<IndexVectorType, IndexType, KeyHasherRange<IndexVectorType>, KeyComparorRange<IndexVectorType> > IndexVectorMapType;
181 
183  typedef std::unordered_map<IndexType,IndexType> ColorsMapType;
184 
186  typedef std::pair<IndexType,IndexType> IndexPairType;
187 
189  typedef std::pair<IndexType, std::vector<std::string>> IndexStringVectorPairType;
190 
192  static constexpr double ZeroTolerance = std::numeric_limits<double>::epsilon();
193 
197 
201 
205 
209 
213 
217 
221 
225 
230  std::string GetMmgVersion();
231 
236  void SetEchoLevel(const SizeType EchoLevel);
237 
242  SizeType GetEchoLevel();
243 
248  void SetDiscretization(const DiscretizationOption Discretization);
249 
254  DiscretizationOption GetDiscretization();
255 
260  void SetRemoveRegions(const bool RemoveRegions);
261 
266  bool GetRemoveRegions();
267 
272  void PrintAndGetMmgMeshInfo(MMGMeshInfo<TMMGLibrary>& rMMGMeshInfo);
273 
278  virtual IndexVectorType FindDuplicateNodeIds(const ModelPart& rModelPart);
279 
285 
291 
297 
303 
308  virtual void BlockNode(const IndexType iNode);
309 
314  virtual void BlockCondition(const IndexType iCondition);
315 
320  virtual void BlockElement(const IndexType iElement);
321 
331  virtual NodeType::Pointer CreateNode(
332  ModelPart& rModelPart,
333  IndexType iNode,
334  int& Ref,
335  int& IsRequired
336  );
337 
349  virtual Condition::Pointer CreateFirstTypeCondition(
350  ModelPart& rModelPart,
351  std::unordered_map<IndexType,Condition::Pointer>& rMapPointersRefCondition,
352  const IndexType CondId,
353  int& Ref,
354  int& IsRequired,
355  bool SkipCreation
356  );
357 
367  Condition::Pointer CreateSecondTypeCondition(
368  ModelPart& rModelPart,
369  std::unordered_map<IndexType,Condition::Pointer>& rMapPointersRefCondition,
370  const IndexType CondId,
371  int& Ref,
372  int& IsRequired,
373  bool SkipCreation
374  );
375 
385  virtual Element::Pointer CreateFirstTypeElement(
386  ModelPart& rModelPart,
387  std::unordered_map<IndexType,Element::Pointer>& rMapPointersRefElement,
388  const IndexType ElemId,
389  int& Ref,
390  int& IsRequired,
391  bool SkipCreation
392  );
393 
403  Element::Pointer CreateSecondTypeElement(
404  ModelPart& rModelPart,
405  std::unordered_map<IndexType,Element::Pointer>& rMapPointersRefElement,
406  const IndexType ElemId,
407  int& Ref,
408  int& IsRequired,
409  bool SkipCreation
410  );
411 
421  virtual void InitMesh();
422 
426  virtual void InitVerbosity();
427 
432  virtual void InitVerbosityParameter(const IndexType VerbosityMMG);
433 
439  virtual void SetMeshOptimizationModeParameter(const bool EnableMeshOptimization);
440 
446 
451  virtual void SetSolSizeScalar(const SizeType NumNodes);
452 
457  virtual void SetSolSizeVector(const SizeType NumNodes);
458 
463  virtual void SetSolSizeTensor(const SizeType NumNodes);
464 
469  virtual void SetDispSizeVector(const SizeType NumNodes);
470 
474  virtual void CheckMeshData();
475 
480  virtual void InputMesh(const std::string& rInputName);
481 
486  virtual void InputSol(const std::string& rInputName);
487 
492  virtual void OutputMesh(const std::string& rOutputName);
493 
498  virtual void OutputSol(const std::string& rOutputName);
499 
504  virtual void OutputDisplacement(const std::string& rOutputName);
505 
512  virtual void OutputReferenceEntitities(
513  const std::string& rOutputName,
514  const std::unordered_map<IndexType,Condition::Pointer>& rRefCondition,
515  const std::unordered_map<IndexType,Element::Pointer>& rRefElement
516  );
517 
521  virtual void FreeAll();
522 
526  void MMGLibCallMetric(Parameters ConfigurationParameters);
527 
531  void MMGLibCallIsoSurface(Parameters ConfigurationParameters);
532 
541  virtual void SetNodes(
542  const double X,
543  const double Y,
544  const double Z,
545  const IndexType Color,
546  const IndexType Index
547  );
548 
555  virtual void SetConditions(
556  GeometryType& rGeometry,
557  const IndexType Color,
558  const IndexType Index
559  );
560 
567  virtual void SetElements(
568  GeometryType& rGeometry,
569  const IndexType Color,
570  const IndexType Index
571  );
572 
578  virtual void SetMetricScalar(
579  const double Metric,
580  const IndexType NodeId
581  );
582 
588  virtual void SetMetricVector(
589  const array_1d<double, Dimension>& rMetric,
590  const IndexType NodeId
591  );
592 
598  virtual void SetMetricTensor(
599  const TensorArrayType& rMetric,
600  const IndexType NodeId
601  );
602 
608  virtual void SetDisplacementVector(
609  const array_1d<double, 3>& rDisplacement,
610  const IndexType NodeId
611  );
612 
617  virtual void GetMetricScalar(double& rMetric);
618 
624 
629  virtual void GetMetricTensor(TensorArrayType& rMetric);
630 
635  virtual void GetDisplacementVector(array_1d<double, 3>& rDisplacement);
636 
641  void ReorderAllIds(ModelPart& rModelPart);
642 
652  virtual void GenerateMeshDataFromModelPart(
653  ModelPart& rModelPart,
654  std::unordered_map<IndexType,std::vector<std::string>>& rColors,
655  ColorsMapType& rColorMapCondition,
656  ColorsMapType& rColorMapElement,
658  const bool CollapsePrismElements = false
659  );
660 
669  virtual void GenerateReferenceMaps(
670  ModelPart& rModelPart,
671  const ColorsMapType& rColorMapCondition,
672  const ColorsMapType& rColorMapElement,
673  std::unordered_map<IndexType,Condition::Pointer>& rRefCondition,
674  std::unordered_map<IndexType,Element::Pointer>& rRefElement
675  );
676 
681  virtual void GenerateSolDataFromModelPart(ModelPart& rModelPart);
682 
688  virtual void GenerateIsosurfaceMetricDataFromModelPart(ModelPart& rModelPart);
689 
694  virtual void GenerateDisplacementDataFromModelPart(ModelPart& rModelPart);
695 
705  void WriteMeshDataToModelPart(
706  ModelPart& rModelPart,
707  const std::unordered_map<IndexType,std::vector<std::string>>& rColors,
708  const NodeType::DofsContainerType& rDofs,
709  const MMGMeshInfo<TMMGLibrary>& rMMGMeshInfo,
710  std::unordered_map<IndexType,Condition::Pointer>& rMapPointersRefCondition,
711  std::unordered_map<IndexType,Element::Pointer>& rMapPointersRefElement
712  );
713 
718  void WriteSolDataToModelPart(ModelPart& rModelPart);
719 
727  virtual void WriteReferenceEntitities(
728  ModelPart& rModelPart,
729  const std::string& rFilename,
730  std::unordered_map<IndexType,Condition::Pointer>& rRefCondition,
731  std::unordered_map<IndexType,Element::Pointer>& rRefElement
732  );
733 
738  virtual void CreateAuxiliarSubModelPartForFlags(ModelPart& rModelPart);
739 
744  virtual void AssignAndClearAuxiliarSubModelPartForFlags(ModelPart& rModelPart);
745 
750  void SetNumberOfLocalParameters(IndexType NumberOfLocalParameter);
751 
756  void SetLocalParameter(IndexType rColor, double HMin, double HMax, double HausdorffValue);
757 
761 
762 
766 
767 
771 
773  std::string Info() const // override
774  {
775  return "MmgUtilities";
776  }
777 
779  void PrintInfo(std::ostream& rOStream) const // override
780  {
781  rOStream << "MmgUtilities";
782  }
783 
785  void PrintData(std::ostream& rOStream) const // override
786  {
787  }
788 
789 protected:
790 
793 
797 
801 
805 
809 
813 
817 
819 
820 private:
823 
827 
828  SizeType mEchoLevel = 0;
829  bool mRemoveRegions = false;
831  bool mUsingMetricTensor = true;
832 
836 
840 
846  void ResursivelyAssignFlagEntities(
847  ModelPart& rModelPart,
848  const Flags& rFlag,
849  const bool FlagValue
850  )
851  {
852  // We call it recursively
853  for (auto& r_sub_model_part : rModelPart.SubModelParts()) {
854  VariableUtils().SetFlag(rFlag, FlagValue, r_sub_model_part.Conditions());
855  VariableUtils().SetFlag(rFlag, FlagValue, r_sub_model_part.Elements());
856  ResursivelyAssignFlagEntities(r_sub_model_part, rFlag, FlagValue);
857  }
858  }
859 
863 
864 
868 
869 
873 
874 // /// Assignment operator.
875 // MmgUtilities& operator=(MmgUtilities const& rOther);
876 
877 // /// Copy constructor.
878 // MmgUtilities(MmgUtilities const& rOther);
879 
881 
882 };// class MmgUtilities
884 
887 
888 
892 
894 template<MMGLibrary TMMGLibrary>
895 inline std::istream& operator >> (std::istream& rIStream,
897 
899 template<MMGLibrary TMMGLibrary>
900 inline std::ostream& operator << (std::ostream& rOStream,
901  const MmgUtilities<TMMGLibrary>& rThis)
902 {
903  rThis.PrintInfo(rOStream);
904  rOStream << std::endl;
905  rThis.PrintData(rOStream);
906 
907  return rOStream;
908 }
909 
910 }// namespace Kratos.
911 #endif /* KRATOS_MMG_UTILITIES defined */
Definition: flags.h:58
Geometry base class.
Definition: geometry.h:71
Provides the Kratos interface to the MMG library API.
Definition: mmg_utilities.h:153
virtual void SetNodes(const double X, const double Y, const double Z, const IndexType Color, const IndexType Index)
This sets the nodes of the mesh.
virtual void InputSol(const std::string &rInputName)
This sets the output sol.
virtual void BlockNode(const IndexType iNode)
It blocks certain nodes before remesh the model.
std::pair< IndexType, IndexType > IndexPairType
Index pair.
Definition: mmg_utilities.h:186
virtual void OutputSol(const std::string &rOutputName)
This sets the output sol.
virtual void SetDisplacementVector(const array_1d< double, 3 > &rDisplacement, const IndexType NodeId)
This function is used to set the displacement vector (x, y, z)
virtual void SetMetricTensor(const TensorArrayType &rMetric, const IndexType NodeId)
This function is used to set the Hessian metric tensor, note that when using the Hessian,...
Condition::Pointer CreateSecondTypeCondition(ModelPart &rModelPart, std::unordered_map< IndexType, Condition::Pointer > &rMapPointersRefCondition, const IndexType CondId, int &Ref, int &IsRequired, bool SkipCreation)
It creates the new condition (second type, depends if the library work in 2D/3D/Surfaces)
KRATOS_CLASS_POINTER_DEFINITION(MmgUtilities)
Pointer definition of MmgUtilities.
virtual void SetMeshOptimizationModeParameter(const bool EnableMeshOptimization)
This sets the optimization mode using the API where edge lengths are preserved, ignoring the metric v...
virtual void SetElements(GeometryType &rGeometry, const IndexType Color, const IndexType Index)
This sets elements of the mesh.
virtual void SetSolSizeScalar(const SizeType NumNodes)
This sets the size of the solution for the scalar case.
virtual void SetDispSizeVector(const SizeType NumNodes)
This sets the size of the displacement for lagrangian movement.
void PrintData(std::ostream &rOStream) const
Print object's data.
Definition: mmg_utilities.h:785
virtual void SetConditions(GeometryType &rGeometry, const IndexType Color, const IndexType Index)
This sets the conditions of the mesh.
virtual void GetMetricTensor(TensorArrayType &rMetric)
This function is used to retrieve the Hessian metric tensor, note that when using the Hessian,...
void SetMeshSize(MMGMeshInfo< TMMGLibrary > &rMMGMeshInfo)
This sets the size of the mesh.
std::string Info() const
Turn back information as a string.
Definition: mmg_utilities.h:773
virtual void GetMetricScalar(double &rMetric)
This function is used to retrieve the metric scalar.
Geometry< NodeType > GeometryType
Definition: mmg_utilities.h:165
virtual void SetSolSizeVector(const SizeType NumNodes)
This sets the size of the solution for the vector case.
virtual void InitVerbosityParameter(const IndexType VerbosityMMG)
Here the verbosity is set using the API.
Node NodeType
Node definition.
Definition: mmg_utilities.h:163
virtual void InitMesh()
Initialisation of mesh and sol structures.
virtual NodeType::Pointer CreateNode(ModelPart &rModelPart, IndexType iNode, int &Ref, int &IsRequired)
It creates the new node.
Element::Pointer CreateSecondTypeElement(ModelPart &rModelPart, std::unordered_map< IndexType, Element::Pointer > &rMapPointersRefElement, const IndexType ElemId, int &Ref, int &IsRequired, bool SkipCreation)
It creates the new element (second type, depends if the library work in 2D/3D/Surfaces)
std::unordered_map< IndexVectorType, IndexType, KeyHasherRange< IndexVectorType >, KeyComparorRange< IndexVectorType > > IndexVectorMapType
Index vector map.
Definition: mmg_utilities.h:180
virtual void GetMetricVector(array_1d< double, Dimension > &rMetric)
This function is used to retrieve the metric vector (x, y, z)
virtual void OutputDisplacement(const std::string &rOutputName)
This sets the output displacement.
virtual Condition::Pointer CreateFirstTypeCondition(ModelPart &rModelPart, std::unordered_map< IndexType, Condition::Pointer > &rMapPointersRefCondition, const IndexType CondId, int &Ref, int &IsRequired, bool SkipCreation)
It creates the new condition (first type, depends if the library work in 2D/3D/Surfaces)
virtual void OutputMesh(const std::string &rOutputName)
This sets the output mesh.
virtual void BlockCondition(const IndexType iCondition)
It blocks certain conditions before remesh the model.
virtual void CheckMeshData()
This checks the mesh data and prints if it is OK.
virtual IndexVectorType CheckSecondTypeConditions()
Returns a vector of ids of repeated conditions.
virtual void SetMetricScalar(const double Metric, const IndexType NodeId)
This function is used to set the metric scalar.
void SetNumberOfLocalParameters(IndexType NumberOfLocalParameter)
Sets the number of parameters to be defined locally at each reference color. Required by the library.
void PrintInfo(std::ostream &rOStream) const
Print information about this object.
Definition: mmg_utilities.h:779
virtual void InputMesh(const std::string &rInputName)
This sets the output mesh.
virtual void SetSolSizeTensor(const SizeType NumNodes)
This sets the size of the solution for the tensor case.
std::unordered_map< IndexType, IndexType > ColorsMapType
Colors map.
Definition: mmg_utilities.h:183
virtual void SetMetricVector(const array_1d< double, Dimension > &rMetric, const IndexType NodeId)
This function is used to set the metric vector (x, y, z)
virtual IndexVectorType CheckFirstTypeConditions()
Returns a vector of ids of repeated conditions.
virtual void FreeAll()
This frees the MMG structures.
virtual void GetDisplacementVector(array_1d< double, 3 > &rDisplacement)
This function is used to retrieve the displacement vector (x, y, z)
virtual IndexVectorType CheckFirstTypeElements()
Returns a vector of ids of repeated elements.
void SetLocalParameter(IndexType rColor, double HMin, double HMax, double HausdorffValue)
Sets the local hmin, hmax and hausdorff value for all entities with the given color reference.
void MMGLibCallIsoSurface(Parameters ConfigurationParameters)
This loads the solution.
virtual Element::Pointer CreateFirstTypeElement(ModelPart &rModelPart, std::unordered_map< IndexType, Element::Pointer > &rMapPointersRefElement, const IndexType ElemId, int &Ref, int &IsRequired, bool SkipCreation)
It creates the new element (first type, depends if the library work in 2D/3D/Surfaces)
std::vector< double > DoubleVectorType
Double vector.
Definition: mmg_utilities.h:174
virtual void BlockElement(const IndexType iElement)
It blocks certain elements before remesh the model.
virtual IndexVectorType CheckSecondTypeElements()
Returns a vector of ids of repeated elements.
std::conditional< Dimension==2, array_1d< double, 3 >, array_1d< double, 6 > >::type TensorArrayType
The type of array considered for the tensor.
Definition: mmg_utilities.h:171
std::unordered_map< DoubleVectorType, IndexType, KeyHasherRange< DoubleVectorType >, KeyComparorRange< DoubleVectorType > > DoubleVectorMapType
Double vector map.
Definition: mmg_utilities.h:177
void MMGLibCallMetric(Parameters ConfigurationParameters)
This loads the solution.
std::pair< IndexType, std::vector< std::string > > IndexStringVectorPairType
Index and string vector pair.
Definition: mmg_utilities.h:189
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
SubModelPartsContainerType & SubModelParts()
Definition: model_part.h:1718
This class defines the node.
Definition: node.h:65
std::vector< std::unique_ptr< Dof< double > >> DofsContainerType
The DoF container type definition.
Definition: node.h:92
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetFlag(const Flags &rFlag, const bool FlagValue, TContainerType &rContainer)
Sets a flag according to a given status over a given container.
Definition: variable_utils.h:900
std::size_t IndexType
The definition of the index type.
Definition: key_hash.h:35
static int EchoLevel
Definition: co_sim_EMPIRE_API.h:42
void SetEchoLevel(const int EchoLevel)
Definition: add_custom_io_to_python.cpp:344
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
FrameworkEulerLagrange
This enums allows to differentiate the working framework.
Definition: meshing_application.h:66
DiscretizationOption
This enums allows to differentiate the discretization options.
Definition: meshing_application.h:71
std::size_t SizeType
The definition of the size type.
Definition: mortar_classes.h:43
std::istream & operator>>(std::istream &rIStream, LinearMasterSlaveConstraint &rThis)
input stream function
std::vector< IndexType > IndexVectorType
Index vector.
Definition: mmg_io.h:63
std::ostream & operator<<(std::ostream &rOStream, const LinearMasterSlaveConstraint &rThis)
output stream function
Definition: linear_master_slave_constraint.h:432
type
Definition: generate_gid_list_file.py:35
def Index()
Definition: hdf5_io_tools.py:38
tuple const
Definition: ode_solve.py:403
This is a key comparer of general pourpose between two classes.
Definition: key_hash.h:100
Stores the Mmg mesh information.
Definition: mmg_utilities.h:115
SizeType NumberOfTriangles
Definition: mmg_utilities.h:119
SizeType NumberFirstTypeConditions() const
It returns the number of the first type of conditions.
SizeType NumberOfTetrahedra
Definition: mmg_utilities.h:122
SizeType NumberSecondTypeConditions() const
It returns the number of the second type of conditions.
SizeType NumberOfLines
Definition: mmg_utilities.h:118
SizeType NumberOfPrism
Definition: mmg_utilities.h:121
SizeType NumberFirstTypeElements() const
It returns the number of the first type of elements.
SizeType NumberSecondTypeElements() const
It returns the number of the second type of elements.
SizeType NumberOfNodes
Definition: mmg_utilities.h:117
SizeType NumberOfQuadrilaterals
Definition: mmg_utilities.h:120