Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[GeoMechanicsApplication] Add process factory to settlement workflow #11591

Merged
merged 23 commits into from
Sep 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f82d240
Merge branch 'master' into maas
rfaasse Sep 14, 2023
a752a92
Initial addition of processfactory to settlement workflow
rfaasse Sep 14, 2023
d4aae3b
Added version of a UT that checks if initialization throws
rfaasse Sep 14, 2023
e47ebf8
Progress on dgeosettlement unit test
rfaasse Sep 15, 2023
4107576
added first version of ProcessNameParser
rfaasse Sep 19, 2023
e45aaa0
Added functionality + UTs for retrieving process info
rfaasse Sep 20, 2023
1a388a9
Fix build
rfaasse Sep 20, 2023
9fc6f8a
Merge branch 'master' into geo/add-process-factory-to-settlement
rfaasse Sep 20, 2023
816ed51
Added ProjectInfoParser to GeoSettlement workflow + rename/interface …
rfaasse Sep 20, 2023
3a0d520
Revert rename
rfaasse Sep 20, 2023
b721234
Retrieved processes from json string and prepared vector of refs for …
rfaasse Sep 21, 2023
347214c
added stub functionality to stub process info parser
rfaasse Sep 21, 2023
206307e
Minor renaming and formatting changes
rfaasse Sep 21, 2023
fd46be1
Clean up + fixing SonarQube issues
rfaasse Sep 22, 2023
6f33fb6
SonarQube Fix
rfaasse Sep 22, 2023
c1bef92
Fix ubuntu build, which fails on comparison between different signedness
rfaasse Sep 25, 2023
e083848
Processing review comments
rfaasse Sep 26, 2023
f5dda6c
Made function const
rfaasse Sep 26, 2023
9c6849b
Added exception when process is unknown and added nullptr check
rfaasse Sep 26, 2023
995bb56
Added custom callback as per review comments
rfaasse Sep 26, 2023
4d808db
Review comments
rfaasse Sep 26, 2023
048177b
Fix Ubuntu build
rfaasse Sep 26, 2023
0274861
Minor SQ fixes
rfaasse Sep 27, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// KRATOS___
// // ) )
// // ___ ___
// // ____ //___) ) // ) )
// // / / // // / /
// ((____/ / ((____ ((___/ / MECHANICS
//
// License: geo_mechanics_application/license.txt
//
// Main authors: Richard Faasse
//
#include "json_process_info_parser.h"

namespace Kratos
{

std::vector<ProcessParameters> JsonProcessInfoParser::GetProcessList(const Parameters& rProcessParameters) const
{
std::vector<ProcessParameters> result;
const std::vector<std::string> process_list_names = {"constraints_process_list",
"loads_process_list",
"auxiliar_process_list"};

for (const auto& process_list_name : process_list_names)
{
const auto processes_for_list = AddProcessesForList(process_list_name, rProcessParameters);
result.insert(result.end(), processes_for_list.begin(), processes_for_list.end());
}

return result;
}

std::vector<ProcessParameters> JsonProcessInfoParser::AddProcessesForList(const std::string& rProcessListName, const Parameters& rProcessParameters) const {
if (!rProcessParameters.Has(rProcessListName))
{
return {};
}

std::vector<ProcessParameters> result;
for (Parameters process : rProcessParameters[rProcessListName])
{
const std::string process_name_entry = "process_name";
if (process.Has(process_name_entry))
{
result.emplace_back(process[process_name_entry].GetString(), process["Parameters"]);
}
}

return result;
}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// KRATOS___
// // ) )
// // ___ ___
// // ____ //___) ) // ) )
// // / / // // / /
// ((____/ / ((____ ((___/ / MECHANICS
//
// License: geo_mechanics_application/license.txt
//
// Main authors: Richard Faasse
//

#pragma once

#include "process_info_parser.h"
#include "process_parameters.h"

#include <vector>

namespace Kratos {

class JsonProcessInfoParser : public ProcessInfoParser
{
public:
std::vector<ProcessParameters> GetProcessList(const Parameters& rProcessParameters) const override;

private:
std::vector<ProcessParameters> AddProcessesForList(const std::string& rProcessListName, const Parameters& rProcessParameters) const;
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,20 @@
#include "custom_utilities/process_factory.hpp"
#include "includes/kratos_parameters.h"


namespace Kratos
{

ProcessFactory::ProductType ProcessFactory::Create(const std::string& rProcessClassName,
const Parameters& rProcessSettings) const
{
auto pos = mCreatorMap.find(rProcessClassName);
if (pos == mCreatorMap.end()) {
if (pos == mCreatorMap.end())
{
if (mCallBackIfProcessIsUnknown != nullptr)
{
mCallBackIfProcessIsUnknown(rProcessClassName);
}

return nullptr;
}

Expand All @@ -35,4 +40,9 @@ void ProcessFactory::AddCreator(const std::string& rP
mCreatorMap[rProcessClassName] = std::move(Creator);
}

void ProcessFactory::SetCallBackWhenProcessIsUnknown(const std::function<void(const std::string&)>& function)
{
mCallBackIfProcessIsUnknown = function;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include <map>
#include <memory>
#include <string>

#include <functional>

namespace Kratos
{
Expand All @@ -35,8 +35,11 @@ class ProcessFactory
void AddCreator(const std::string& rProcessClassName,
std::function<ProductType(const Parameters&)> Creator);

void SetCallBackWhenProcessIsUnknown(const std::function<void(const std::string&)>& function);

private:
std::map<std::string, std::function<ProductType(const Parameters&)>, std::less<>> mCreatorMap;
std::function<void(const std::string&)> mCallBackIfProcessIsUnknown;
};

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// KRATOS___
// // ) )
// // ___ ___
// // ____ //___) ) // ) )
// // / / // // / /
// ((____/ / ((____ ((___/ / MECHANICS
//
// License: geo_mechanics_application/license.txt
//
// Main authors: Richard Faasse
//

#pragma once

#include "process_parameters.h"

#include <vector>

namespace Kratos {

class ProcessInfoParser {
public:
virtual ~ProcessInfoParser() = default;
virtual std::vector<ProcessParameters> GetProcessList(const Parameters& rProcessParameters) const = 0;
};

} // Kratos
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
// KRATOS___
// // ) )
// // ___ ___
// // ____ //___) ) // ) )
// // / / // // / /
// ((____/ / ((____ ((___/ / MECHANICS
//
// License: geo_mechanics_application/license.txt
//
// Main authors: Richard Faasse
//
#pragma once

#include "includes/kratos_parameters.h"

#include <string>

namespace Kratos
{

struct ProcessParameters
avdg81 marked this conversation as resolved.
Show resolved Hide resolved
{
std::string name;
Parameters parameters;

ProcessParameters(const std::string &rName, const Parameters &rParameters) :
name{rName},
parameters{rParameters}
{}

bool operator==(const ProcessParameters &rhs) const
{
return name == rhs.name && parameters.WriteJsonString() == rhs.parameters.WriteJsonString();
}
};

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,19 @@

#include "custom_workflow_factory.h"
#include "dgeosettlement.h"
#include "time_loop_executor.h"

#include "custom_utilities/file_input_utility.h"
#include "custom_utilities/json_process_info_parser.h"

#include <memory>

namespace Kratos
{

KratosGeoSettlement* CustomWorkflowFactory::CreateKratosGeoSettlement()
{
return new KratosGeoSettlement{std::make_unique<FileInputUtility>()};
return new KratosGeoSettlement{std::make_unique<FileInputUtility>(), std::make_unique<JsonProcessInfoParser>(), {}};
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,28 @@
//
#include "dgeosettlement.h"
#include "input_output/logger.h"
#include "time_loop_executor.h"

#include "utilities/variable_utils.h"

#include "custom_processes/apply_vector_constraints_table_process.hpp"
#include "custom_processes/set_parameter_field_process.hpp"
#include "custom_processes/apply_k0_procedure_process.hpp"
#include "custom_processes/apply_excavation_process.hpp"

#include "custom_utilities/input_utility.h"
#include "custom_utilities/process_factory.hpp"
#include "custom_utilities/process_info_parser.h"

namespace Kratos
{

KratosGeoSettlement::KratosGeoSettlement(std::unique_ptr<InputUtility> pInputUtility) :
mpInputUtility{std::move(pInputUtility)}
KratosGeoSettlement::KratosGeoSettlement(std::unique_ptr<InputUtility> pInputUtility,
std::unique_ptr<ProcessInfoParser> pProcessInfoParser,
std::unique_ptr<TimeLoopExecutor> pTimeLoopExecutor) :
mpInputUtility{std::move(pInputUtility)},
mpProcessInfoParser{std::move(pProcessInfoParser)},
mpTimeLoopExecutor{std::move(pTimeLoopExecutor)}
{
KRATOS_INFO("KratosGeoSettlement") << "Setting up Kratos" << std::endl;
KRATOS_ERROR_IF_NOT(mpInputUtility) << "Invalid Input Utility";
Expand All @@ -29,6 +43,43 @@ KratosGeoSettlement::KratosGeoSettlement(std::unique_ptr<InputUtility> pInputUti
mpGeoApp = Kratos::make_shared<KratosGeoMechanicsApplication>();
mKernel.ImportApplication(mpGeoApp);
}

InitializeProcessFactory();
}

void KratosGeoSettlement::InitializeProcessFactory() {
mProcessFactory->AddCreator("ApplyVectorConstraintsTableProcess",
[this](const Parameters& rParameters)
{
return std::make_unique<ApplyVectorConstraintsTableProcess>(mModel.GetModelPart(mModelPartName),
rParameters);
});

mProcessFactory->AddCreator("SetParameterFieldProcess",
[this](const Parameters& rParameters)
{
return std::make_unique<SetParameterFieldProcess>(mModel.GetModelPart(mModelPartName),
rParameters);
});

mProcessFactory->AddCreator("ApplyExcavationProcess",
[this](const Parameters& rParameters)
{
return std::make_unique<ApplyExcavationProcess>(mModel.GetModelPart(mModelPartName),
rParameters);
});

mProcessFactory->AddCreator("ApplyK0ProcedureProcess",
[this](const Parameters& rParameters)
{
return std::make_unique<ApplyK0ProcedureProcess>(mModel.GetModelPart(mModelPartName),
rParameters);
});

mProcessFactory->SetCallBackWhenProcessIsUnknown([](const std::string& rProcessName)
{
KRATOS_ERROR << "Unexpected process (" << rProcessName << "), calculation is aborted";
});
}

int KratosGeoSettlement::RunStage(const std::filesystem::path& rWorkingDirectory,
Expand All @@ -45,6 +96,7 @@ int KratosGeoSettlement::RunStage(const std::filesystem::path& rWorki
project_parameters_file_path.generic_string());
KRATOS_INFO("KratosGeoSettlement") << "Parsed project parameters file " << project_parameters_file_path << std::endl;

mModelPartName = project_parameters["solver_settings"]["model_part_name"].GetString();
if (const auto model_part_name = project_parameters["solver_settings"]["model_part_name"].GetString();
!mModel.HasModelPart(model_part_name)) {
auto& model_part = AddNewModelPart(model_part_name);
Expand All @@ -59,6 +111,14 @@ int KratosGeoSettlement::RunStage(const std::filesystem::path& rWorki
KRATOS_INFO("KratosGeoSettlement") << "Read the materials from " << material_file_path << std::endl;
}

std::vector<std::shared_ptr<Process>> processes = GetProcesses(project_parameters);
std::vector<std::weak_ptr<Process>> process_observables(processes.begin(), processes.end());

if (mpTimeLoopExecutor)
{
mpTimeLoopExecutor->SetProcessReferences(process_observables);
}

return 0;
}

Expand Down Expand Up @@ -119,6 +179,20 @@ const InputUtility* KratosGeoSettlement::GetInterfaceInputUtility() const
return mpInputUtility.get();
}

std::vector<std::shared_ptr<Process>> KratosGeoSettlement::GetProcesses(const Parameters& project_parameters) const
{
std::vector<std::shared_ptr<Process>> result;
if (project_parameters.Has("processes")) {
const auto processes = mpProcessInfoParser->GetProcessList(project_parameters["processes"]);
for (const auto &process: processes) {
result.emplace_back(mProcessFactory->Create(process.name, process.parameters));
}

}

return result;
}

// This default destructor is added in the cpp to be able to forward member variables in a unique_ptr
KratosGeoSettlement::~KratosGeoSettlement() = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
namespace Kratos
{

class ProcessFactory;
class InputUtility;
class ProcessInfoParser;
class TimeLoopExecutor;
class Process;

class KRATOS_API(GEO_MECHANICS_APPLICATION) KratosGeoSettlement
{
public:
explicit KratosGeoSettlement(std::unique_ptr<InputUtility> pInputUtility);
KratosGeoSettlement(std::unique_ptr<InputUtility> pInputUtility,
std::unique_ptr<ProcessInfoParser> pProcessInfoParser,
std::unique_ptr<TimeLoopExecutor> pTimeLoopExecutor);

~KratosGeoSettlement();

int RunStage(const std::filesystem::path& rWorkingDirectory,
Expand All @@ -45,11 +52,17 @@ class KRATOS_API(GEO_MECHANICS_APPLICATION) KratosGeoSettlement
ModelPart& AddNewModelPart(const std::string& rModelPartName);
static void AddNodalSolutionStepVariablesTo(ModelPart& rModelPart);
static void AddDegreesOfFreedomTo(ModelPart& rModelPart);
void InitializeProcessFactory();
std::vector<std::shared_ptr<Process>> GetProcesses(const Parameters& project_parameters) const;

Kernel mKernel;
Model mModel;
std::string mModelPartName;
KratosGeoMechanicsApplication::Pointer mpGeoApp;
std::unique_ptr<ProcessFactory> mProcessFactory = std::make_unique<ProcessFactory>();
std::unique_ptr<InputUtility> mpInputUtility;
std::unique_ptr<ProcessInfoParser> mpProcessInfoParser;
std::unique_ptr<TimeLoopExecutor> mpTimeLoopExecutor;
};

}
Loading