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.
formfinding_strategy.hpp
Go to the documentation of this file.
1 // KRATOS ___| | | |
2 // \___ \ __| __| | | __| __| | | __| _` | |
3 // | | | | | ( | | | | ( | |
4 // _____/ \__|_| \__,_|\___|\__|\__,_|_| \__,_|_| MECHANICS
5 //
6 // License: BSD License
7 // license: StructuralMechanicsApplication/license.txt
8 //
9 // Main authors: Iñigo López Canalejo
10 // Klaus B. Sautter
11 //
12 
13 #pragma once
14 
15 // System includes
16 #include <filesystem>
17 
18 // External includes
19 
20 // Project includes
23 #include "includes/model_part_io.h"
25 #include "includes/gid_io.h"
27 
28 namespace Kratos
29 {
30 template<class TSparseSpace,
31 class TDenseSpace, // = DenseSpace<double>,
32 class TLinearSolver //= LinearSolver<TSparseSpace,TDenseSpace>
33 >
35 : public ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>
36 {
37 public:
41 
42  // Counted pointer of ClassName
44 
51  //typedef IterationIOType::Pointer IterationIOPointerType;
53 
56 
58 
63  // constructor with Builder and Solver
66  typename TSchemeType::Pointer pScheme,
67  typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria,
68  typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver,
69  ModelPart& rFormFindingModelPart,
70  bool WriteFormFoundGeometryFile,
71  const std::string& rPrintingFormat,
72  Parameters ProjectionSetting,
73  int MaxIterations = 30,
74  bool CalculateReactions = false,
75  bool ReformDofSetAtEachStep = false,
76  bool MoveMeshFlag = false
77  )
78  : ResidualBasedNewtonRaphsonStrategy<TSparseSpace, TDenseSpace, TLinearSolver>(model_part, pScheme,
79  pNewConvergenceCriteria,pNewBuilderAndSolver,MaxIterations,CalculateReactions,ReformDofSetAtEachStep,
80  MoveMeshFlag),
81  mProjectionSettings(ProjectionSetting),
82  mrFormFindingModelPart(rFormFindingModelPart),
83  mPrintingFormat(rPrintingFormat),
84  mWriteFormFoundGeometryFile(WriteFormFoundGeometryFile)
85  {
86  InitializeIterationIO();
87  }
88 
89  // Destructor
90  ~FormfindingStrategy() = default;
91 
92  static void WriteFormFoundMdpa(ModelPart& rModelPart)
93  {
94  Matrix output_matrix;
95  const ProcessInfo& r_process_info = rModelPart.GetProcessInfo();
96  for(auto& r_element : rModelPart.Elements()){
97  r_element.Calculate(MEMBRANE_PRESTRESS,output_matrix,r_process_info);
98  r_element.GetData().Clear();
99  r_element.SetValue(MEMBRANE_PRESTRESS,output_matrix);
100  }
101 
102  rModelPart.Conditions().clear();
103  rModelPart.rProperties().clear();
105 
106  ModelPartIO model_part_io("formfinding_result_model", IO::WRITE);
107  model_part_io.WriteModelPart(rModelPart);
108  }
109 
110 private:
111  void UpdateDatabase(
113  TSystemVectorType& Dx,
115  const bool MoveMesh) override
116  {
118  for(auto& r_node : mrFormFindingModelPart.Nodes()){
119  // Updating reference
120  const array_1d<double, 3>& disp = r_node.FastGetSolutionStepValue(DISPLACEMENT);
121  array_1d<double, 3>& disp_non_historical = r_node.GetValue(DISPLACEMENT);
122 
123  disp_non_historical = disp_non_historical + disp;
124  r_node.GetInitialPosition() += disp;
125 
126  r_node.FastGetSolutionStepValue(DISPLACEMENT) = ZeroVector(3);
127  }
128  ProjectVectorOnSurfaceUtility::Execute(mrFormFindingModelPart, mProjectionSettings);
129 
130  PrintResults();
131  }
132 
133  void EchoInfo(const unsigned int IterationNumber) override
134  {
135  BaseType::EchoInfo(IterationNumber);
136  mIterationNumber = IterationNumber;
137  }
138 
139  void PrintResults()
140  {
141  if (mPrintingFormat=="all"){
142  PrintVtkFiles(mIterationNumber);
143  PrintGiDFiles(mIterationNumber);
144  }
145  else if (mPrintingFormat=="vtk") PrintVtkFiles(mIterationNumber);
146  else if (mPrintingFormat=="gid") PrintGiDFiles(mIterationNumber);
147  else;
148  }
149 
150  void FinalizeSolutionStep() override
151  {
153  if (mPrintingFormat=="all" || mPrintingFormat=="gid") mpIterationIO->FinalizeResults();
154  }
155 
156 
157 
158  void PrintVtkFiles(const int rIterationNumber)
159  {
160  Parameters vtk_params( R"({
161  "file_format" : "binary",
162  "output_precision" : 7,
163  "output_control_type" : "step",
164  "save_output_files_in_folder" : true,
165  "output_path" : "formfinding_results_vtk",
166  "nodal_data_value_variables" : ["DISPLACEMENT"]
167  })");
168 
169  const int max_prefix = int(std::floor(std::log10(BaseType::mMaxIterationNumber)))+1;
170  std::stringstream postfix;
171  postfix << std::setw(max_prefix) << std::setfill('0') << rIterationNumber;
172  VtkOutput(BaseType::GetModelPart(), vtk_params).PrintOutput("formfinding_"+postfix.str());
173  }
174 
175  void PrintGiDFiles(const int rIterationNumber)
176  {
177  double solution_tag = rIterationNumber;
178  mpIterationIO->WriteNodalResultsNonHistorical(DISPLACEMENT,BaseType::GetModelPart().Nodes(),solution_tag);
179  }
180 
181  void InitializeIterationIO()
182  {
183  // check user input for visualization of results
184  std::vector<std::string> printing_possibilities {"all","vtk","gid","none"};
185  if (std::find(printing_possibilities.begin(), printing_possibilities.end(), mPrintingFormat)==printing_possibilities.end()){
186  KRATOS_ERROR << "Chosen printing format :" << mPrintingFormat << " is not available. Please use: " << printing_possibilities << std::endl;
187  }
188 
189  // initialize i/o
190  if (mPrintingFormat=="all" || mPrintingFormat=="vtk"){
191  if (std::filesystem::exists("formfinding_results_vtk")){
192  std::filesystem::remove_all("formfinding_results_vtk");
193  }
194  std::filesystem::create_directory("formfinding_results_vtk");
195  PrintVtkFiles(mIterationNumber);
196  }
197 
198  if (mPrintingFormat=="all" || mPrintingFormat=="gid"){
199  mpIterationIO = Kratos::make_unique<IterationIOType>(
200  "formfinding_iterations",
201  GiD_PostBinary,
205 
206  mpIterationIO->InitializeMesh(0.0);
207  mpIterationIO->WriteMesh(BaseType::GetModelPart().GetMesh());
208  mpIterationIO->WriteNodeMesh(BaseType::GetModelPart().GetMesh());
209  mpIterationIO->FinalizeMesh();
210  mpIterationIO->InitializeResults(0.0, BaseType::GetModelPart().GetMesh());
211  }
212  }
213 
214 
215  IterationIOPointerType mpIterationIO;
216  Parameters mProjectionSettings;
217  ModelPart& mrFormFindingModelPart;
218  std::string mPrintingFormat;
219  int mIterationNumber = 0;
220  bool mWriteFormFoundGeometryFile = true;
221 
222 }; /* Class FormfindingStrategy */
223 } /* namespace Kratos. */
This is the base class to define the different convergence criterion considered.
Definition: convergence_criteria.h:58
void Clear()
Clears the entire data container.
Definition: data_value_container.h:352
Definition: formfinding_strategy.hpp:36
BaseType::TSchemeType TSchemeType
Definition: formfinding_strategy.hpp:47
BaseType::TSystemVectorType TSystemVectorType
Definition: formfinding_strategy.hpp:49
KRATOS_CLASS_POINTER_DEFINITION(FormfindingStrategy)
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: formfinding_strategy.hpp:46
ResidualBasedNewtonRaphsonStrategy< TSparseSpace, TDenseSpace, TLinearSolver > BaseType
Definition: formfinding_strategy.hpp:45
static void WriteFormFoundMdpa(ModelPart &rModelPart)
Definition: formfinding_strategy.hpp:92
ConvergenceCriteria< TSparseSpace, TDenseSpace > TConvergenceCriteriaType
Definition: formfinding_strategy.hpp:40
Kratos::unique_ptr< IterationIOType > IterationIOPointerType
Definition: formfinding_strategy.hpp:52
BaseType::TSystemMatrixType TSystemMatrixType
Definition: formfinding_strategy.hpp:48
GidIO IterationIOType
Definition: formfinding_strategy.hpp:50
FormfindingStrategy(ModelPart &model_part, typename TSchemeType::Pointer pScheme, typename TConvergenceCriteriaType::Pointer pNewConvergenceCriteria, typename TBuilderAndSolverType::Pointer pNewBuilderAndSolver, ModelPart &rFormFindingModelPart, bool WriteFormFoundGeometryFile, const std::string &rPrintingFormat, Parameters ProjectionSetting, int MaxIterations=30, bool CalculateReactions=false, bool ReformDofSetAtEachStep=false, bool MoveMeshFlag=false)
Definition: formfinding_strategy.hpp:64
This class defines an interface to the GiDPost library in order to provide GiD compliant I/O function...
Definition: gid_io.h:112
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
ConditionsContainerType & Conditions(IndexType ThisIndex=0)
Definition: model_part.h:1381
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
PropertiesContainerType & rProperties(IndexType ThisIndex=0)
Definition: model_part.h:1003
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
VariablesList & GetNodalSolutionStepVariablesList()
Definition: model_part.h:549
An IO class for reading and writing a modelpart.
Definition: model_part_io.h:56
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
void clear()
Clear the set, removing all elements.
Definition: pointer_vector_set.h:663
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
static void Execute(ModelPart &rModelPart, Parameters ThisParameters)
Definition: project_vector_on_surface_utility.cpp:84
This is the base Newton Raphson strategy.
Definition: residualbased_newton_raphson_strategy.h:66
virtual void UpdateDatabase(TSystemMatrixType &rA, TSystemVectorType &rDx, TSystemVectorType &rb, const bool MoveMesh)
Here the database is updated.
Definition: residualbased_newton_raphson_strategy.h:1278
unsigned int mMaxIterationNumber
Definition: residualbased_newton_raphson_strategy.h:1261
BaseType::TSchemeType TSchemeType
Definition: residualbased_newton_raphson_strategy.h:87
BaseType::TBuilderAndSolverType TBuilderAndSolverType
Definition: residualbased_newton_raphson_strategy.h:81
virtual void EchoInfo(const unsigned int IterationNumber)
This method returns the components of the system of equations depending of the echo level.
Definition: residualbased_newton_raphson_strategy.h:1298
void FinalizeSolutionStep() override
Performs all the required operations that should be done (for each step) after solving the solution s...
Definition: residualbased_newton_raphson_strategy.h:848
ModelPart & GetModelPart()
Operations to get the pointer to the model.
Definition: solving_strategy.h:350
TSparseSpace::MatrixType TSystemMatrixType
Definition: solving_strategy.h:71
bool MoveMeshFlag()
This function returns the flag that says if the mesh is moved.
Definition: solving_strategy.h:290
TSparseSpace::VectorType TSystemVectorType
Definition: solving_strategy.h:73
virtual void MoveMesh()
This function is designed to move the mesh.
Definition: solving_strategy.h:330
void clear()
Definition: variables_list.h:249
#define KRATOS_ERROR
Definition: exception.h:161
Kratos::ModelPart ModelPart
Definition: kratos_wrapper.h:31
ModelPart::MeshType & GetMesh(ModelPart &rModelPart)
bool create_directory(const std::string &rPath)
Definition: kratos_filesystem.cpp:45
bool exists(const std::string &rPath)
Definition: kratos_filesystem.cpp:27
std::uintmax_t remove_all(const std::string &rPath)
Definition: kratos_filesystem.cpp:63
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
@ WriteUndeformed
Definition: gid_io.h:52
@ SingleFile
Definition: gid_io.h:54
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
@ WriteConditions
Definition: gid_io.h:53
REACTION_CHECK_STIFFNESS_FACTOR int
Definition: contact_structural_mechanics_application_variables.h:75
model_part_io
Definition: PecletTest.py:50
model_part
Definition: face_heat.py:14
b
Definition: generate_total_lagrangian_mixed_volumetric_strain_element.py:31
A
Definition: sensitivityMatrix.py:70