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] GetVariableBasedOnString is now emitting a warning rather than an error in set_parameter_field_process #13011

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
@@ -1,5 +1,7 @@
import json
import importlib
import warnings
from typing import Optional

import KratosMultiphysics
import KratosMultiphysics.GeoMechanicsApplication as KratosGeo
Expand Down Expand Up @@ -48,15 +50,12 @@ def __init__(self, Model, settings ):
self.params.AddValue("dataset_file_name", settings["dataset_file_name"])
self.process = KratosGeo.SetParameterFieldProcess(self.model_part, self.params)

def GetVariableBasedOnString(self):
def GetVariableBasedOnString(self) -> Optional[KratosMultiphysics.VariableData]:
aronnoordam marked this conversation as resolved.
Show resolved Hide resolved
"""
This function returns the variable based on the variable name string.


Returns
-------
variable : KratosMultiphysics.Variable

Returns:
- Optional[KratosMultiphysics.VariableData]: the kratos variable object
"""

# Get variable object
Expand All @@ -67,8 +66,10 @@ def GetVariableBasedOnString(self):
variable = getattr(kratos_module, self.params["variable_name"].GetString())
return variable

raise AttributeError(f'The variable: {self.params["variable_name"].GetString()} is not present within '
f'the imported modules')
# add warning if variable is not found
warnings.warn(f'The variable: {self.params["variable_name"].GetString()} is not present within '
f'the imported modules')
return None


def ExecuteInitialize(self):
Expand All @@ -94,6 +95,9 @@ def ExecuteInitialize(self):
all_coordinates = []

variable = self.GetVariableBasedOnString()
if variable is None:
raise AttributeError(f'The variable: {self.params["variable_name"].GetString()} is not present within '
f'the imported modules')

for element in self.model_part.Elements:

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
import os
import shutil
import stat
import warnings

import KratosMultiphysics as Kratos
import KratosMultiphysics.GeoMechanicsApplication as KratosGeo
import KratosMultiphysics.KratosUnittest as KratosUnittest
from KratosMultiphysics.GeoMechanicsApplication.set_parameter_field_process import SetParameterFieldProcess

import test_helper


Expand Down Expand Up @@ -203,6 +205,61 @@ def test_parameter_field_with_invalid_json(self):
self.assertTrue(r'Error: The parameter field does not have the same size as '
r'the amount of elements within the model part!' in str(cm.exception))

def test_GetVariableBasedOnString(self):
"""
Test to check if the variable is correctly retrieved from the imported modules
"""

# dummy variables with YOUNG_MODULUS, which is a variable which is exported to the python module
settings = Kratos.Parameters("""{
"model_part_name": "test",
"variable_name": "YOUNG_MODULUS",
"dataset": "dummy",
"func_type": "json_file",
"function": "dummy",
"dataset_file_name": "test_file"
}""")

# initialize the set parameter field process
model = Kratos.Model()
model.CreateModelPart("test")
process = SetParameterFieldProcess(model, settings)

variable = process.GetVariableBasedOnString()

assert variable == Kratos.YOUNG_MODULUS

def test_GetVariableBasedOnString_non_existing_variable_in_python(self):
"""
Test to check if a warning is raised when a variable is not present in the imported modules
"""

# dummy variables with DENSITY_SOLID_dummy, which is a variable which is not exported to the python module
settings = Kratos.Parameters("""{
"model_part_name": "test",
"variable_name": "DENSITY_SOLID_dummy",
"dataset": "dummy",
"func_type": "json_file",
"function": "dummy",
"dataset_file_name": "test_file"
}""")

# initialize the set parameter field process
model = Kratos.Model()
model.CreateModelPart("test")
process = SetParameterFieldProcess(model, settings)

# catch the warnings for the test
with warnings.catch_warnings(record=True) as w:
warnings.simplefilter("always") # Ensure warnings are triggered

assert process.GetVariableBasedOnString() is None

# Check that a warning was raised
assert len(w) == 1
assert issubclass(w[-1].category, UserWarning)
Copy link
Contributor

Choose a reason for hiding this comment

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

We've had some issues in the past that sonarcloud doesn't recognize the assert and therefore could flag the indexging w[-1] as a 'reliability' issue. Would it be possible to rename this branch to geo/ such that the sonarcloud analysis as well as other pipelines will be run? E.g. github doesn't run the validation tests (only small/nightly)

assert str(w[-1].message) == "The variable: DENSITY_SOLID_dummy is not present within the imported modules"


if __name__ == '__main__':
KratosUnittest.main()
Loading