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.
solving_strategy_wrapper.hpp
Go to the documentation of this file.
1 // KRATOS___
2 // // ) )
3 // // ___ ___
4 // // ____ //___) ) // ) )
5 // // / / // // / /
6 // ((____/ / ((____ ((___/ / MECHANICS
7 //
8 // License: geo_mechanics_application/license.txt
9 //
10 // Main authors: Richard Faasse
11 //
12 
13 #pragma once
14 
15 #include "strategy_wrapper.hpp"
16 #include <memory>
17 
19 #include "geo_output_writer.h"
20 #include "includes/variables.h"
22 
23 namespace Kratos
24 {
25 
26 template <class TSparseSpace, class TDenseSpace>
28 {
29 public:
31  bool ResetDisplacements = false,
32  const std::filesystem::path& rWorkingDirectory = "",
33  const Parameters& rProjectParameters = {})
34  : mpStrategy(std::move(strategy)),
35  mrModelPart(mpStrategy->GetModelPart()),
36  mResetDisplacements{ResetDisplacements},
37  mProjectParameters{rProjectParameters},
38  mWorkingDirectory{rWorkingDirectory}
39  {
40  }
41 
42  ~SolvingStrategyWrapper() override = default;
43 
44  size_t GetNumberOfIterations() const override
45  {
46  return mrModelPart.GetProcessInfo()[NL_ITERATION_NUMBER];
47  }
48 
49  double GetEndTime() const override { return mrModelPart.GetProcessInfo()[TIME]; }
50 
51  void Initialize() override { mpStrategy->Initialize(); }
52 
53  void InitializeOutput() override
54  {
55  const auto gid_output_settings =
56  mProjectParameters["output_processes"]["gid_output"][0]["Parameters"];
57  mWriter = std::make_unique<GeoOutputWriter>(
58  gid_output_settings, mWorkingDirectory.generic_string(), mrModelPart);
59  }
60 
61  void InitializeSolutionStep() override { mpStrategy->InitializeSolutionStep(); }
62 
63  void Predict() override { mpStrategy->Predict(); }
64 
65  void SetEndTime(double EndTime) override { mrModelPart.GetProcessInfo()[TIME] = EndTime; }
66 
67  double GetTimeIncrement() const override { return mrModelPart.GetProcessInfo()[DELTA_TIME]; }
68 
69  void SetTimeIncrement(double TimeIncrement) override
70  {
71  mrModelPart.GetProcessInfo()[DELTA_TIME] = TimeIncrement;
72  }
73 
74  size_t GetStepNumber() const override
75  {
76  return static_cast<std::size_t>(mrModelPart.GetProcessInfo()[STEP]);
77  }
78 
79  void IncrementStepNumber() override { mrModelPart.GetProcessInfo()[STEP] += 1; }
80 
81  void CloneTimeStep() override
82  {
83  auto& root_model_part = mrModelPart.IsSubModelPart() ? mrModelPart.GetRootModelPart() : mrModelPart;
84  root_model_part.CloneTimeStep();
85  }
86 
88  {
89  VariableUtils().UpdateCurrentPosition(mrModelPart.Nodes(), DISPLACEMENT, 1);
90 
91  const auto index_of_old_value = Node::IndexType{1};
92  const auto index_of_new_value = Node::IndexType{0};
93  CopyNodalSolutionStepValues(DISPLACEMENT, index_of_old_value, index_of_new_value);
94  CopyNodalSolutionStepValues(WATER_PRESSURE, index_of_old_value, index_of_new_value);
95  CopyNodalSolutionStepValues(ROTATION, index_of_old_value, index_of_new_value);
96  }
97 
99  {
100  if (mResetDisplacements) {
101  mOldTotalDisplacements.clear();
102  for (const auto& node : mrModelPart.Nodes()) {
103  mOldTotalDisplacements.emplace_back(node.GetSolutionStepValue(TOTAL_DISPLACEMENT));
104  }
105  }
106  }
107 
109  {
110  if (mResetDisplacements) {
111  KRATOS_ERROR_IF_NOT(mrModelPart.Nodes().size() == mOldTotalDisplacements.size())
112  << "The number of old displacements (" << mOldTotalDisplacements.size()
113  << ") does not match the current number of nodes (" << mrModelPart.Nodes().size() << ").";
114  std::size_t count = 0;
115  for (auto& node : mrModelPart.Nodes()) {
116  node.GetSolutionStepValue(TOTAL_DISPLACEMENT) =
117  mOldTotalDisplacements[count] + node.GetSolutionStepValue(DISPLACEMENT);
118  ++count;
119  }
120  }
121  }
122 
123  void OutputProcess() override
124  {
125  if (mWriter) {
126  const auto write_hydraulic_head_to_nodes = false;
127  const auto gid_output_settings =
128  mProjectParameters["output_processes"]["gid_output"][0]["Parameters"];
129  mWriter->WriteGiDOutput(mrModelPart, gid_output_settings, write_hydraulic_head_to_nodes);
130  }
131  }
132 
134  {
135  return mpStrategy->SolveSolutionStep() ? TimeStepEndState::ConvergenceState::converged
137  ;
138  }
139 
140  void FinalizeSolutionStep() override { return mpStrategy->FinalizeSolutionStep(); }
141 
142  void FinalizeOutput() override
143  {
144  if (mWriter) {
145  mWriter->FinalizeResults();
146  }
147  }
148 
149 private:
150  template <typename TVariableType>
151  void CopyNodalSolutionStepValues(const TVariableType& rVariable, Node::IndexType SourceIndex, Node::IndexType DestinationIndex)
152  {
153  if (!mrModelPart.HasNodalSolutionStepVariable(rVariable)) return;
154 
155  for (auto& node : mrModelPart.Nodes()) {
156  node.GetSolutionStepValue(rVariable, DestinationIndex) =
157  node.GetSolutionStepValue(rVariable, SourceIndex);
158  }
159  }
160 
161  std::unique_ptr<SolvingStrategy<TSparseSpace, TDenseSpace>> mpStrategy;
162  ModelPart& mrModelPart;
163  bool mResetDisplacements;
164  std::vector<array_1d<double, 3>> mOldTotalDisplacements;
165  Parameters mProjectParameters;
166  std::filesystem::path mWorkingDirectory;
167  std::unique_ptr<GeoOutputWriter> mWriter;
168 };
169 
170 } // namespace Kratos
IndexType CloneTimeStep()
Definition: model_part.cpp:143
bool IsSubModelPart() const
Definition: model_part.h:1893
bool HasNodalSolutionStepVariable(VariableData const &ThisVariable) const
Definition: model_part.h:544
ProcessInfo & GetProcessInfo()
Definition: model_part.h:1746
ModelPart & GetRootModelPart()
Definition: model_part.cpp:510
NodesContainerType & Nodes(IndexType ThisIndex=0)
Definition: model_part.h:507
std::size_t IndexType
The index type.
Definition: node.h:86
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
Solving strategy base class This is the base class from which we will derive all the strategies (impl...
Definition: solving_strategy.h:64
Definition: solving_strategy_wrapper.hpp:28
void AccumulateTotalDisplacementField() override
Definition: solving_strategy_wrapper.hpp:108
void FinalizeSolutionStep() override
Definition: solving_strategy_wrapper.hpp:140
void SaveTotalDisplacementFieldAtStartOfTimeLoop() override
Definition: solving_strategy_wrapper.hpp:98
~SolvingStrategyWrapper() override=default
size_t GetNumberOfIterations() const override
Definition: solving_strategy_wrapper.hpp:44
void InitializeOutput() override
Definition: solving_strategy_wrapper.hpp:53
size_t GetStepNumber() const override
Definition: solving_strategy_wrapper.hpp:74
void CloneTimeStep() override
Definition: solving_strategy_wrapper.hpp:81
void Initialize() override
Definition: solving_strategy_wrapper.hpp:51
SolvingStrategyWrapper(std::unique_ptr< SolvingStrategy< TSparseSpace, TDenseSpace >> strategy, bool ResetDisplacements=false, const std::filesystem::path &rWorkingDirectory="", const Parameters &rProjectParameters={})
Definition: solving_strategy_wrapper.hpp:30
void OutputProcess() override
Definition: solving_strategy_wrapper.hpp:123
double GetTimeIncrement() const override
Definition: solving_strategy_wrapper.hpp:67
void RestorePositionsAndDOFVectorToStartOfStep() override
Definition: solving_strategy_wrapper.hpp:87
void InitializeSolutionStep() override
Definition: solving_strategy_wrapper.hpp:61
void Predict() override
Definition: solving_strategy_wrapper.hpp:63
void SetTimeIncrement(double TimeIncrement) override
Definition: solving_strategy_wrapper.hpp:69
void IncrementStepNumber() override
Definition: solving_strategy_wrapper.hpp:79
TimeStepEndState::ConvergenceState SolveSolutionStep() override
Definition: solving_strategy_wrapper.hpp:133
void FinalizeOutput() override
Definition: solving_strategy_wrapper.hpp:142
double GetEndTime() const override
Definition: solving_strategy_wrapper.hpp:49
void SetEndTime(double EndTime) override
Definition: solving_strategy_wrapper.hpp:65
Definition: strategy_wrapper.hpp:23
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void UpdateCurrentPosition(const ModelPart::NodesContainerType &rNodes, const ArrayVarType &rUpdateVariable=DISPLACEMENT, const IndexType BufferPosition=0)
This method updates the current coordinates For each node, this method takes the value of the provide...
Definition: variable_utils.cpp:273
#define KRATOS_ERROR_IF_NOT(conditional)
Definition: exception.h:163
Kratos::ModelPart ModelPart
Definition: kratos_wrapper.h:31
string path
Definition: DEM_run_all_benchmarks_analysis.py:10
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
std::unique_ptr< T > unique_ptr
Definition: smart_pointers.h:33
int count
Definition: generate_frictional_mortar_condition.py:212
ConvergenceState
Definition: time_step_end_state.hpp:24
Definition: mesh_converter.cpp:38