[GeoMechanicsApplication] Clean up process for applying constant scalar values#13676
[GeoMechanicsApplication] Clean up process for applying constant scalar values#13676
Conversation
smell
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_X", | ||
| "value" : 0.0, | ||
| "is_fixed" : true | ||
| })"})); | ||
|
|
||
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_Y", | ||
| "value" : -9.81, | ||
| "is_fixed" : true | ||
| })"})); | ||
|
|
||
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_Z", | ||
| "value" : 0.0, | ||
| "is_fixed" : true | ||
| })"})); |
There was a problem hiding this comment.
I think we should look at this (later), it seems strange to me that we hard-code it, but for now I kept it as it is, just using the 'parameters' version of the constructor
There was a problem hiding this comment.
Agreed. In this way, we are also hard-coding the gravity direction and the value of the gravity acceleration (assuming all input is given in SI).
avdg81
left a comment
There was a problem hiding this comment.
Hi Richard, thanks a lot for cleaning up this process and getting rid of several code smells. I see that understanding the process has become simpler due to your changes. And having a few more unit tests is also a very nice extension.
I have several minor suggestions and a few questions. Hope this helps.
| GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart, Parameters ThisParameters) | ||
| : Process(Flags()), mrModelPart(rModelPart) | ||
| GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart, | ||
| const Parameters& ThisParameters) |
There was a problem hiding this comment.
Just to comply with the Kratos Style Guide:
| const Parameters& ThisParameters) | |
| const Parameters& rParameters) |
|
|
||
| mVariableName = ThisParameters["variable_name"].GetString(); | ||
| this->Set(VARIABLE_IS_FIXED, ThisParameters["is_fixed"].GetBool()); | ||
| mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false; |
There was a problem hiding this comment.
Does this change mean that parameter "is_fixed" is no longer required, but optional (having a default value of false if not provided)? If yes, could you please help me understand why this change is needed?
Also, if this functional change is needed, we are repeating the default value when "is_fixed" is not provided (the default value of mIsFixed is also set in the header file, which is great). So perhaps we can rewrite this statement to:
| mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false; | |
| if (ThisParameters.Has("is_fixed")) mIsFixed = ThisParameters["is_fixed"].GetBool(); |
There was a problem hiding this comment.
As discussed, it was handled by using the default parameters before, so the behavior hasn't changed. I implemented your suggestion, because indeed the default is already there in the header.
| << "' which is not of type Variable<double>.\n"; | ||
|
|
||
| KRATOS_ERROR_IF_NOT(rModelPart.GetNodalSolutionStepVariablesList().Has(KratosComponents<VariableData>::Get(mVariableName))) | ||
| << "Trying to fix a variable that is not in the rModelPart - variable name is " |
There was a problem hiding this comment.
Perhaps we can make the error message a little bit more specific by reporting the name of the model part rather than rModelPart?
| if (mIsFixed && KratosComponents<Variable<double>>::Has(mVariableName)) { | ||
| VariableUtils().ApplyFixity(KratosComponents<Variable<double>>::Get(mVariableName), true, | ||
| mrModelPart.Nodes()); | ||
| } |
There was a problem hiding this comment.
I don't understand why we shouldn't need to call ApplyFixity when mIsFixed equals false. In other words, why wouldn't we write this:
if (KratosComponents<Variable<double>>::Has(mVariableName)) {
VariableUtils().ApplyFixity(KratosComponents<Variable<double>>::Get(mVariableName), mIsFixed,
mrModelPart.Nodes());
}Perhaps you can help to shed some light here?
There was a problem hiding this comment.
As discussed, to keep the functionality the same (and keep the initialize and finalize symmetric), we only touch the fixity of the nodes if mFixed is true (otherwise, we keep the fixity as is)
| Parameters{R"( | ||
| { | ||
| "variable_name" : "WATER_PRESSURE", | ||
| "is_fixed" : true | ||
| })"}; |
There was a problem hiding this comment.
This unnamed object is destroyed immediately after creation. That's probably not what you intended.
There was a problem hiding this comment.
Very good catch, thanks!
| Parameters{R"( | ||
| { | ||
| "variable_name" : "WATER_PRESSURE", | ||
| "is_fixed" : true |
There was a problem hiding this comment.
I'm not sure whether always setting this value to true is equivalent to what the code did previously. Do you know?
There was a problem hiding this comment.
I think it's equivalent to setting the flag
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_X", | ||
| "value" : 0.0, | ||
| "is_fixed" : true | ||
| })"})); | ||
|
|
||
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_Y", | ||
| "value" : -9.81, | ||
| "is_fixed" : true | ||
| })"})); | ||
|
|
||
| mProcesses.push_back(make_shared<GeoApplyConstantScalarValueProcess>(part, Parameters{R"( | ||
| { | ||
| "variable_name" : "VOLUME_ACCELERATION_Z", | ||
| "value" : 0.0, | ||
| "is_fixed" : true | ||
| })"})); |
There was a problem hiding this comment.
Agreed. In this way, we are also hard-coding the gravity direction and the value of the gravity acceleration (assuming all input is given in SI).
| KratosGeoMechanicsFastSuiteWithoutKernel) | ||
| { | ||
| Model current_model; | ||
| ModelPart& r_model_part = current_model.CreateModelPart("Main", 2); |
There was a problem hiding this comment.
| ModelPart& r_model_part = current_model.CreateModelPart("Main", 2); | |
| auto& r_model_part = current_model.CreateModelPart("Main", 2); |
This suggestion also applies to the other new test cases.
| const Geo::ConstVariableRefs nodal_variables = {}; | ||
| ModelPart& r_model_part = | ||
| ModelSetupUtilities::CreateModelPartWithASingle2D3NElement(current_model, nodal_variables); |
There was a problem hiding this comment.
I'm just wondering whether this isn't more cumbersome than needed. Wouldn't it be sufficient to have an empty model part rather than a model part with nodes and an element?
This suggestion also applies to the next unit test.
There was a problem hiding this comment.
You're right, simplified!
rfaasse
left a comment
There was a problem hiding this comment.
Processed the review comments
| GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart, Parameters ThisParameters) | ||
| : Process(Flags()), mrModelPart(rModelPart) | ||
| GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart, | ||
| const Parameters& ThisParameters) |
|
|
||
| mVariableName = ThisParameters["variable_name"].GetString(); | ||
| this->Set(VARIABLE_IS_FIXED, ThisParameters["is_fixed"].GetBool()); | ||
| mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false; |
There was a problem hiding this comment.
As discussed, it was handled by using the default parameters before, so the behavior hasn't changed. I implemented your suggestion, because indeed the default is already there in the header.
| << "' which is not of type Variable<double>.\n"; | ||
|
|
||
| KRATOS_ERROR_IF_NOT(rModelPart.GetNodalSolutionStepVariablesList().Has(KratosComponents<VariableData>::Get(mVariableName))) | ||
| << "Trying to fix a variable that is not in the rModelPart - variable name is " |
| if (mIsFixed && KratosComponents<Variable<double>>::Has(mVariableName)) { | ||
| VariableUtils().ApplyFixity(KratosComponents<Variable<double>>::Get(mVariableName), true, | ||
| mrModelPart.Nodes()); | ||
| } |
There was a problem hiding this comment.
As discussed, to keep the functionality the same (and keep the initialize and finalize symmetric), we only touch the fixity of the nodes if mFixed is true (otherwise, we keep the fixity as is)
| Parameters{R"( | ||
| { | ||
| "variable_name" : "WATER_PRESSURE", | ||
| "is_fixed" : true | ||
| })"}; |
There was a problem hiding this comment.
Very good catch, thanks!
| Parameters{R"( | ||
| { | ||
| "variable_name" : "WATER_PRESSURE", | ||
| "is_fixed" : true |
There was a problem hiding this comment.
I think it's equivalent to setting the flag
| KratosGeoMechanicsFastSuiteWithoutKernel) | ||
| { | ||
| Model current_model; | ||
| ModelPart& r_model_part = current_model.CreateModelPart("Main", 2); |
| const Geo::ConstVariableRefs nodal_variables = {}; | ||
| ModelPart& r_model_part = | ||
| ModelSetupUtilities::CreateModelPartWithASingle2D3NElement(current_model, nodal_variables); |
There was a problem hiding this comment.
You're right, simplified!
avdg81
left a comment
There was a problem hiding this comment.
Thank you for processing the review suggestions. I feel this PR is ready to go.
📝 Description
This PR reduces code smells and simplifies the code