Skip to content

Commit

Permalink
[GeoMechanicsApplication] Add method to time loop executor to cancel …
Browse files Browse the repository at this point in the history
…the loop (#11727)

* Add method to time loop executor to cancel the loop

* Dont check cancel delegate if its not defined

* Dont check cancel delegate if its not defined

* Add IsCancelled method and use in both loops

* Add

* Remove mCancelDelegateAvailable
  • Loading branch information
Carlos Lubbers authored Nov 14, 2023
1 parent be6e483 commit e6eb1f8
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ int KratosGeoSettlement::RunStage(const std::filesystem::path& rWorki
const std::function<void(const char*)>& rLogCallback,
const std::function<void(double)>& ,
const std::function<void(const char*)>& ,
const std::function<bool()>& )
const std::function<bool()>& rShouldCancel)
{
std::stringstream kratos_log_buffer;
LoggerOutput::Pointer logger_output = CreateLoggingOutput(kratos_log_buffer);
Expand Down Expand Up @@ -197,6 +197,7 @@ int KratosGeoSettlement::RunStage(const std::filesystem::path& rWorki

if (mpTimeLoopExecutor)
{
mpTimeLoopExecutor->SetCancelDelegate(rShouldCancel);
mpTimeLoopExecutor->SetProcessObservables(process_observables);
mpTimeLoopExecutor->SetTimeIncrementor(MakeTimeIncrementor(project_parameters));
mpTimeLoopExecutor->SetSolverStrategyWrapper(MakeStrategyWrapper(project_parameters,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ namespace Kratos

class Process;

class TimeLoopExecutor : public TimeLoopExecutorInterface{
class TimeLoopExecutor : public TimeLoopExecutorInterface {
public :
void SetCancelDelegate(const std::function<bool()>& rCancelDelegate) override
{
mCancelDelegate = rCancelDelegate;
}

void SetProcessObservables(const std::vector<std::weak_ptr<Process>>& rProcessObservables) override
{
mTimeStepExecutor->SetProcessObservables(rProcessObservables);
Expand All @@ -52,7 +57,7 @@ public :
mStrategyWrapper->SaveTotalDisplacementFieldAtStartOfTimeLoop();
std::vector<TimeStepEndState> result;
TimeStepEndState NewEndState = EndState;
while (mTimeIncrementor->WantNextStep(NewEndState)) {
while (mTimeIncrementor->WantNextStep(NewEndState) && !IsCancelled()) {
mStrategyWrapper->IncrementStepNumber();
// clone without end time, the end time is overwritten anyway
mStrategyWrapper->CloneTimeStep();
Expand Down Expand Up @@ -86,7 +91,7 @@ public :
{
auto cycle_number = 0;
auto end_state = previous_state;
while (mTimeIncrementor->WantRetryStep(cycle_number, end_state)) {
while (mTimeIncrementor->WantRetryStep(cycle_number, end_state) && !IsCancelled()) {
if (cycle_number > 0) mStrategyWrapper->RestorePositionsAndDOFVectorToStartOfStep();
end_state = RunCycle(previous_state.time);
++cycle_number;
Expand All @@ -96,7 +101,13 @@ public :
return end_state;
}

bool IsCancelled() const
{
return mCancelDelegate && mCancelDelegate();
}

std::unique_ptr<TimeIncrementor> mTimeIncrementor;
std::function<bool()> mCancelDelegate;
std::unique_ptr<TimeStepExecutor> mTimeStepExecutor = std::make_unique<TimeStepExecutor>();
std::shared_ptr<StrategyWrapper> mStrategyWrapper;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include <vector>
#include <memory>
#include <functional>

#include "time_step_end_state.hpp"
#include "time_incrementor.h"
Expand All @@ -29,6 +30,7 @@ class Process;
class TimeLoopExecutorInterface{
public :
virtual ~TimeLoopExecutorInterface() = default;
virtual void SetCancelDelegate(const std::function<bool()>& rCancelDelegate) = 0;
virtual void SetProcessObservables(const std::vector<std::weak_ptr<Process>>& rProcessObservables) = 0;
virtual void SetTimeIncrementor(std::unique_ptr<TimeIncrementor> pTimeIncrementor) = 0;
virtual void SetSolverStrategyWrapper(std::shared_ptr<StrategyWrapper> pStrategyWrapper) = 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,10 @@ void StubTimeLoopExecutor::SetProcessObservables(const std::vector<std::weak_ptr
}
}

void StubTimeLoopExecutor::SetCancelDelegate(const std::function<bool()>& rCancelDelegate) {
// intentionally empty
}

void StubTimeLoopExecutor::SetTimeIncrementor(std::unique_ptr<TimeIncrementor> pTimeIncrementor) {
// intentionally empty
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class StubTimeLoopExecutor : public TimeLoopExecutorInterface
{
public:
explicit StubTimeLoopExecutor(size_t NumberOfExpectedProcesses = 0);

void SetCancelDelegate(const std::function<bool()>& rCancelDelegate) override;

void SetProcessObservables(const std::vector<std::weak_ptr<Process>>& rProcessObservables) override;

void SetTimeIncrementor(std::unique_ptr<TimeIncrementor> pTimeIncrementor) override;
Expand Down

0 comments on commit e6eb1f8

Please sign in to comment.