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

Kratos Master-Slave Constraints to Neighbours Process and Utility #11129

Merged
merged 34 commits into from
Sep 12, 2023

Conversation

SADPR
Copy link
Contributor

@SADPR SADPR commented May 15, 2023

This pull request addresses the feedback and comments received on my previous pull request (#11083).

Main changes:

  • Renamed the utility to "AssignMasterSlaveConstraintsToNeighboursUtility" to align with the Kratos Multiphysics Master-Slave naming convention.
  • Updated tests and file namings to reflect the changes in the utility.
  • Improved code structure by eliminating repetition for double and/or array variables. Instead, the process now generates a Python list containing the names of the variables. That is:
"variable_names" : ["DISPLACEMENT", "PRESSURE"]:

variable_names = ["DISPLACEMENT_X","DISPLACEMENT_Y","PRESSURE"] for 2D
or:
variable_names = ["DISPLACEMENT_X","DISPLACEMENT_Y","DISPLACEMENT_Z","PRESSURE"] for 3D

This approach allows the C++ code to receive the variable list and accommodates the 2D and 3D problem discussed in the previous pull request.

These changes enhance the clarity and consistency of the codebase and improve the usability of the utility. The updated tests ensure proper functionality and maintain code reliability.

I request the community to review this pull request and provide any further feedback or suggestions for improvement.

Thank you!

rCloudOfDofs.resize(rVariableList.size());

// Resize rCloudOfDofs vectors to match the size of the cloud of nodes
for (auto& dofs : rCloudOfDofs)
Copy link
Member

Choose a reason for hiding this comment

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

Usually we consider https://en.wikipedia.org/wiki/Indentation_style#K&R_style with Linux kernel variant

Copy link
Member

Choose a reason for hiding this comment

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

At least I consider that and I ask other to use that

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Will update this.

// Loop over the variable list
for (int j = 0; j < static_cast<int>(rVariableList.size()); j++)
{
const auto& rVariable = rVariableList[j];
Copy link
Member

Choose a reason for hiding this comment

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

Should be r_variable (snake case inside scope, cammel case for input)

NodesContainerType pSlaveNodes,
double const Radius,
ModelPart& rComputingModelPart,
const std::vector<std::reference_wrapper<const Kratos::Variable<double>>>& rVariableList,
Copy link
Member

Choose a reason for hiding this comment

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

I didn't know about std::reference_wrapper, until now we were using variables raw pointers...

@SADPR SADPR changed the title Kratos mp cs suggestions Kratos Master-Slave Constraints to Nieghbours Process and Utility May 15, 2023
@SADPR SADPR changed the title Kratos Master-Slave Constraints to Nieghbours Process and Utility Kratos Master-Slave Constraints to Neighbours Process and Utility May 15, 2023
@SADPR
Copy link
Contributor Author

SADPR commented May 16, 2023

@rubenzorrilla I think it is now ready to check. I hope I covered all your suggestions.

@SADPR SADPR requested a review from a team as a code owner June 16, 2023 14:15
Copy link
Member

@RiccardoRossi RiccardoRossi left a comment

Choose a reason for hiding this comment

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

minor comments

velocity_vector = MathUtils<double>::CrossProduct(angular_velocity_vector, position_vector);

// Setting the node's velocity
rNode.FastGetSolutionStepValue(VELOCITY_X) = velocity_vector[0];
Copy link
Member

Choose a reason for hiding this comment

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

this should be

noalias(rNode.FastGetSolutionStepValue(VELOCITY)) = velocity_vector

// Creating the angular velocity vector
array_1d<double, 3> angular_velocity_vector = rOmega * rAxisOfRotation;

thread_local array_1d<double, 3> position_vector;
Copy link
Member

Choose a reason for hiding this comment

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

please do not use the thread_local keyword.

in particular this is a stack array, so you can just define it there where you use it

rot_matrix(2, 1) = 2*(c*d+a*b);
rot_matrix(2, 2) = a*a+d*d-b*b-c*c;

thread_local array_1d<double, 3> rotated_point;
Copy link
Member

Choose a reason for hiding this comment

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

same

auto& r_point = rNode.GetInitialPosition().Coordinates();

// Shifting the rotation center to the origin
auto centered_point = r_point - rCenterOfRotation;
Copy link
Member

Choose a reason for hiding this comment

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

define it here locally


// Shifting the point back and updating the node coordinates
rotated_point += rCenterOfRotation;
rNode.FastGetSolutionStepValue(MESH_DISPLACEMENT_X) = rotated_point[0] - rNode.X0();
Copy link
Member

Choose a reason for hiding this comment

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

see the noalias comment above

noalias(rNode.FastGetSolutionStepValue(MESH_DISPLACEMENT)) = rotated_point - rNode.GetInitialPosition();


KRATOS_TRY;

ResultNodesContainerType local_results(mMaxNumberOfNodes);
Copy link
Member

Choose a reason for hiding this comment

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

it would be better if you pass the local_results in the argument, making it a TLS variable if it is in a parallel loop

@SADPR
Copy link
Contributor Author

SADPR commented Sep 10, 2023

@loumalouomega if you can, please also take a look to this one. I implemented all suggestions.


BuiltinTimer build_and_assign_mscs;

// Declare thread-local storage (TLS) variables
Copy link
Member

Choose a reason for hiding this comment

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

👀

loumalouomega
loumalouomega previously approved these changes Sep 11, 2023
Copy link
Member

@loumalouomega loumalouomega left a comment

Choose a reason for hiding this comment

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

My only concern would be to remove the old proceess, ia priori if in the Core is part of the API and in theory maybe someone else is using it

(I also have aestheic comments, but I am not going to be that "tocah****")

@SADPR
Copy link
Contributor Author

SADPR commented Sep 12, 2023

My only concern would be to remove the old proceess, ia priori if in the Core is part of the API and in theory maybe someone else is using it

(I also have aestheic comments, but I am not going to be that "tocah****")

We renamed "AssignMPCsToNeighboursUtility" to "AssignMasterSlaveConstraintsToNeighboursUtility" because while "MPC" is a general term, the master-slave constraint is a specific type of MPC used in Kratos. We believe this new name provides a more accurate description. To ensure backward compatibility, we can provide an alias for the old process name that points to the new one.

@SADPR
Copy link
Contributor Author

SADPR commented Sep 12, 2023

My only concern would be to remove the old proceess, ia priori if in the Core is part of the API and in theory maybe someone else is using it

(I also have aestheic comments, but I am not going to be that "tocah****")

Here's my proposal to ensure we don't disrupt users relying on the old process name or settings:

Process Name: I've retained the file assign_mpcs_to_neighbours_process.py. If someone uses the old AssignMPCsToNeighboursProcess, it'll still work but will throw a deprecation warning, nudging them towards the new name.

Settings: For those using the old setting assign_mpcs_every_time_step, it'll be automatically swapped with the new reform_constraints_at_each_step, accompanied by a deprecation warning.

I believe this approach ensures a smooth transition without breaking anyone's workflow.

What do you think?

Here's the code for the proposed changes (assign_mpcs_to_neighbours_process.py):

import KratosMultiphysics 
from KratosMultiphysics.assign_master_slave_constraints_to_neighbours_process import AssignMasterSlaveConstraintsToNeighboursProcess

def Factory(settings, model):
    KratosMultiphysics.Logger.PrintWarning("assign_mpcs_to_neighbours_process", "This process is deprecated. Please use 'assign_master_slave_constraints_to_neighbours_process' instead.")
    return AssignMPCsToNeighboursProcess(model, settings["Parameters"])

class AssignMPCsToNeighboursProcess(AssignMasterSlaveConstraintsToNeighboursProcess):
    def __init__(self, model, settings):
        # The setting 'assign_mpcs_every_time_step' was renamed to 'reform_constraints_at_each_step'
        # This block ensures backward compatibility by updating the old setting to the new one and issuing a warning.
        if settings.Has("assign_mpcs_every_time_step"):
            KratosMultiphysics.Logger.PrintWarning("AssignMPCsToNeighboursProcess", "'assign_mpcs_every_time_step' is deprecated. Please use 'reform_constraints_at_each_step' instead.")
            settings.AddValue("reform_constraints_at_each_step", settings["assign_mpcs_every_time_step"])
            settings.RemoveValue("assign_mpcs_every_time_step")
        
        super().__init__(model, settings)

@SADPR SADPR merged commit ee9c27b into master Sep 12, 2023
@SADPR SADPR deleted the Kratos_MPCs_suggestions branch September 12, 2023 20:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants