Skip to content

[GeoMechanicsApplication] Clean up process for applying constant scalar values#13676

Merged
rfaasse merged 18 commits intomasterfrom
geo/clean-up-process
Jul 30, 2025
Merged

[GeoMechanicsApplication] Clean up process for applying constant scalar values#13676
rfaasse merged 18 commits intomasterfrom
geo/clean-up-process

Conversation

@rfaasse
Copy link
Contributor

@rfaasse rfaasse commented Jul 25, 2025

📝 Description
This PR reduces code smells and simplifies the code

@rfaasse rfaasse requested a review from avdg81 July 25, 2025 13:37
@rfaasse rfaasse requested a review from a team as a code owner July 25, 2025 13:37
Comment on lines +178 to +197
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
})"}));
Copy link
Contributor Author

@rfaasse rfaasse Jul 25, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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).

@rfaasse rfaasse self-assigned this Jul 29, 2025
Copy link
Contributor

@avdg81 avdg81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to comply with the Kratos Style Guide:

Suggested change
const Parameters& ThisParameters)
const Parameters& rParameters)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


mVariableName = ThisParameters["variable_name"].GetString();
this->Set(VARIABLE_IS_FIXED, ThisParameters["is_fixed"].GetBool());
mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:

Suggested change
mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false;
if (ThisParameters.Has("is_fixed")) mIsFixed = ThisParameters["is_fixed"].GetBool();

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 "
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we can make the error message a little bit more specific by reporting the name of the model part rather than rModelPart?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +59 to +62
if (mIsFixed && KratosComponents<Variable<double>>::Has(mVariableName)) {
VariableUtils().ApplyFixity(KratosComponents<Variable<double>>::Get(mVariableName), true,
mrModelPart.Nodes());
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines +160 to +164
Parameters{R"(
{
"variable_name" : "WATER_PRESSURE",
"is_fixed" : true
})"};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This unnamed object is destroyed immediately after creation. That's probably not what you intended.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good catch, thanks!

Parameters{R"(
{
"variable_name" : "WATER_PRESSURE",
"is_fixed" : true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whether always setting this value to true is equivalent to what the code did previously. Do you know?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's equivalent to setting the flag

Comment on lines +178 to +197
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
})"}));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +107 to +109
const Geo::ConstVariableRefs nodal_variables = {};
ModelPart& r_model_part =
ModelSetupUtilities::CreateModelPartWithASingle2D3NElement(current_model, nodal_variables);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, simplified!

Copy link
Contributor Author

@rfaasse rfaasse left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Processed the review comments

GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart, Parameters ThisParameters)
: Process(Flags()), mrModelPart(rModelPart)
GeoApplyConstantScalarValueProcess::GeoApplyConstantScalarValueProcess(ModelPart& rModelPart,
const Parameters& ThisParameters)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done


mVariableName = ThisParameters["variable_name"].GetString();
this->Set(VARIABLE_IS_FIXED, ThisParameters["is_fixed"].GetBool());
mIsFixed = ThisParameters.Has("is_fixed") ? ThisParameters["is_fixed"].GetBool() : false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 "
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +59 to +62
if (mIsFixed && KratosComponents<Variable<double>>::Has(mVariableName)) {
VariableUtils().ApplyFixity(KratosComponents<Variable<double>>::Get(mVariableName), true,
mrModelPart.Nodes());
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)

Comment on lines +160 to +164
Parameters{R"(
{
"variable_name" : "WATER_PRESSURE",
"is_fixed" : true
})"};
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very good catch, thanks!

Parameters{R"(
{
"variable_name" : "WATER_PRESSURE",
"is_fixed" : true
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's equivalent to setting the flag

KratosGeoMechanicsFastSuiteWithoutKernel)
{
Model current_model;
ModelPart& r_model_part = current_model.CreateModelPart("Main", 2);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment on lines +107 to +109
const Geo::ConstVariableRefs nodal_variables = {};
ModelPart& r_model_part =
ModelSetupUtilities::CreateModelPartWithASingle2D3NElement(current_model, nodal_variables);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right, simplified!

@rfaasse rfaasse requested a review from avdg81 July 30, 2025 12:37
@rfaasse rfaasse enabled auto-merge (squash) July 30, 2025 12:37
Copy link
Contributor

@avdg81 avdg81 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for processing the review suggestions. I feel this PR is ready to go.

@rfaasse rfaasse merged commit d6c901a into master Jul 30, 2025
10 checks passed
@rfaasse rfaasse deleted the geo/clean-up-process branch July 30, 2025 15:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[GeoMechanicsApplication] Degrees of freedom follow up issue: clean-up

2 participants