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.
time_loop_executor.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 // Anne van de Graaf
12 // Wijtze Pieter Kikstra
13 //
14 
15 #pragma once
16 
17 #include <memory>
18 #include <vector>
19 
21 #include "strategy_wrapper.hpp"
22 #include "time_incrementor.h"
24 #include "time_step_end_state.hpp"
25 #include "time_step_executor.h"
26 
27 namespace Kratos
28 {
29 
30 class Process;
31 
33 {
34 public:
35  void SetCancelDelegate(const std::function<bool()>& rCancelDelegate) override
36  {
37  mCancelDelegate = rCancelDelegate;
38  }
39 
40  void SetProgressDelegate(const std::function<void(double)>& rProgressDelegate) override
41  {
42  mProgressDelegate = rProgressDelegate;
43  }
44 
45  void SetProcessObservables(const std::vector<std::weak_ptr<Process>>& rProcessObservables) override
46  {
47  mTimeStepExecutor->SetProcessObservables(rProcessObservables);
48  }
49 
50  void SetTimeIncrementor(std::unique_ptr<TimeIncrementor> pTimeIncrementor) override
51  {
52  mTimeIncrementor = std::move(pTimeIncrementor);
53  }
54 
55  void SetSolverStrategyWrapper(std::shared_ptr<StrategyWrapper> pStrategyWrapper) override
56  {
57  mStrategyWrapper = std::move(pStrategyWrapper);
58  mTimeStepExecutor->SetSolverStrategy(mStrategyWrapper);
59  }
60 
61  std::vector<TimeStepEndState> Run(const TimeStepEndState& EndState) override
62  {
63  mStrategyWrapper->Initialize();
64  mStrategyWrapper->SaveTotalDisplacementFieldAtStartOfTimeLoop();
65  std::vector<TimeStepEndState> result;
66  TimeStepEndState NewEndState = EndState;
67  ScopedOutputFileAccess limit_output_file_access_to_this_scope{*mStrategyWrapper};
68  while (mTimeIncrementor->WantNextStep(NewEndState) && !IsCancelled()) {
69  mStrategyWrapper->IncrementStepNumber();
70  // clone without end time, the end time is overwritten anyway
71  mStrategyWrapper->CloneTimeStep();
72  NewEndState = RunCycleLoop(NewEndState);
73  mStrategyWrapper->AccumulateTotalDisplacementField();
74  mStrategyWrapper->FinalizeSolutionStep();
75  mStrategyWrapper->OutputProcess();
76  result.emplace_back(NewEndState);
77  }
78 
79  return result;
80  }
81 
82 private:
83  TimeStepEndState RunCycle(double PreviousTime)
84  {
85  // Setting the time and time increment may be needed for the processes
86  const auto time_increment = mTimeIncrementor->GetIncrement();
87  mStrategyWrapper->SetTimeIncrement(time_increment);
88  const auto end_time = PreviousTime + time_increment;
89  mStrategyWrapper->SetEndTime(end_time);
90 
91  auto end_state = mTimeStepExecutor->Run(end_time);
92  mTimeIncrementor->PostTimeStepExecution(end_state);
93  UpdateProgress(end_time);
94  return end_state;
95  }
96 
97  TimeStepEndState RunCycleLoop(const TimeStepEndState& previous_state)
98  {
99  auto cycle_number = 0;
100  auto end_state = previous_state;
101  while (mTimeIncrementor->WantRetryStep(cycle_number, end_state) && !IsCancelled()) {
102  if (cycle_number > 0) mStrategyWrapper->RestorePositionsAndDOFVectorToStartOfStep();
103  end_state = RunCycle(previous_state.time);
104  ++cycle_number;
105  }
106 
107  end_state.num_of_cycles = cycle_number;
108  return end_state;
109  }
110 
111  bool IsCancelled() const { return mCancelDelegate && mCancelDelegate(); }
112 
113  void UpdateProgress(double Time) const
114  {
115  if (mProgressDelegate) {
116  mProgressDelegate(Time);
117  }
118  }
119 
120  std::unique_ptr<TimeIncrementor> mTimeIncrementor;
121  std::function<bool()> mCancelDelegate;
122  std::function<void(double)> mProgressDelegate;
123  std::unique_ptr<TimeStepExecutor> mTimeStepExecutor = std::make_unique<TimeStepExecutor>();
124  std::shared_ptr<StrategyWrapper> mStrategyWrapper;
125 };
126 
127 } // namespace Kratos
Definition: scoped_output_file_access.h:21
Definition: time_loop_executor.hpp:33
std::vector< TimeStepEndState > Run(const TimeStepEndState &EndState) override
Definition: time_loop_executor.hpp:61
void SetProgressDelegate(const std::function< void(double)> &rProgressDelegate) override
Definition: time_loop_executor.hpp:40
void SetSolverStrategyWrapper(std::shared_ptr< StrategyWrapper > pStrategyWrapper) override
Definition: time_loop_executor.hpp:55
void SetProcessObservables(const std::vector< std::weak_ptr< Process >> &rProcessObservables) override
Definition: time_loop_executor.hpp:45
void SetCancelDelegate(const std::function< bool()> &rCancelDelegate) override
Definition: time_loop_executor.hpp:35
void SetTimeIncrementor(std::unique_ptr< TimeIncrementor > pTimeIncrementor) override
Definition: time_loop_executor.hpp:50
Definition: time_loop_executor_interface.h:31
end_time
Definition: DEM_benchmarks.py:174
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
Definition: time_step_end_state.hpp:23