def test_ApplyFunctionToNodesUtility(self): parameters = KM.Parameters ("""{ "origin" : [0,0,0], "axes" : [[0,1,0],[1,0,0],[0,0,1]] }""") function = KM.PythonGenericFunctionUtility("x+2*y", parameters) self.assertTrue(function.DependsOnSpace()) self.assertTrue(function.UseLocalSystem()) self.assertEqual(function.FunctionBody(), "x+2*y") self.assertEqual(function.CallFunction(4.0,3.0,0.0,0.0,0.0,0.0,0.0), 10) self.assertEqual(function.RotateAndCallFunction(4.0,3.0,0.0,0.0,0.0,0.0,0.0), 11) this_model = KM.Model() model_part = this_model.CreateModelPart("Main", 2) current_process_info = model_part.ProcessInfo current_process_info[KM.DOMAIN_SIZE] = 2 model_part.AddNodalSolutionStepVariable(KM.DISPLACEMENT) model_part.AddNodalSolutionStepVariable(KM.VISCOSITY) model_part_io = KM.ModelPartIO(GetFilePath("auxiliar_files_for_python_unittest/mdpa_files/test_model_part_io_read")) model_part_io.ReadModelPart(model_part) utility = KM.ApplyFunctionToNodesUtility(model_part.Nodes, function) utility.ApplyFunction(KM.VISCOSITY, 1.0) for node in model_part.Nodes: self.assertEqual(node.GetSolutionStepValue(KM.VISCOSITY) - (node.Y + 2.0 * node.X), 0.0)
def __init__(self, Model, settings ): """ The default constructor of the class Keyword arguments: self -- It signifies an instance of a class. Model -- the container of the different model parts. settings -- Kratos parameters containing solver settings. """ KratosMultiphysics.Process.__init__(self) #The value can be a double or a string (function) default_settings = KratosMultiphysics.Parameters(""" { "help" : "This process sets a given scalar value for a certain variable in all the nodes of a submodelpart", "mesh_id" : 0, "model_part_name" : "please_specify_model_part_name", "variable_name" : "SPECIFY_VARIABLE_NAME", "interval" : [0.0, 1e30], "constrained" : true, "value" : 0.0, "local_axes" : {} } """ ) # Assign this here since it will change the "interval" prior to validation self.interval = KratosMultiphysics.IntervalUtility(settings) # Here i do a trick, since i want to allow "value" to be a string or a double value if settings.Has("value"): if settings["value"].IsString(): default_settings["value"].SetString("0.0") settings.ValidateAndAssignDefaults(default_settings) self.variable = KratosMultiphysics.KratosGlobals.GetVariable(settings["variable_name"].GetString()) if not isinstance(self.variable, KratosMultiphysics.DoubleVariable) and not isinstance(self.variable, KratosMultiphysics.VectorVariable): msg = "Error in AssignScalarToNodesProcess. Variable type of variable : " + settings["variable_name"].GetString() + " is incorrect . Must be a scalar or a component" raise Exception(msg) self.model_part = Model[settings["model_part_name"].GetString()] self.mesh = self.model_part.GetMesh(settings["mesh_id"].GetInt()) self.is_fixed = settings["constrained"].GetBool() self.value_is_numeric = False if settings["value"].IsNumber(): self.value_is_numeric = True self.value = settings["value"].GetDouble() else: self.function_string = settings["value"].GetString() self.aux_function = KratosMultiphysics.PythonGenericFunctionUtility(self.function_string, settings["local_axes"]) if self.aux_function.DependsOnSpace(): self.cpp_apply_function_utility = KratosMultiphysics.ApplyFunctionToNodesUtility(self.mesh.Nodes, self.aux_function ) # Construct a variable_utils object to speedup fixing self.variable_utils = KratosMultiphysics.VariableUtils() self.step_is_active = False
def __init__(self, Model, settings): KratosMultiphysics.Process.__init__(self) default_settings = KratosMultiphysics.Parameters(""" { "mesh_id" : 0, "model_part_name" : "please_specify_model_part_name", "variable_name" : "SPECIFY_VARIABLE_NAME", "interval" : [0.0, 1e30], "constrained" : true, "value" : 0.0, "local_axes" : {} } """) #assign this here since it will change the "interval" prior to validation self.interval = KratosMultiphysics.IntervalUtility(settings) #here i do a trick, since i want to allow "value" to be a string or a double value if (settings.Has("value")): if (settings["value"].IsString()): default_settings["value"].SetString("0.0") settings.ValidateAndAssignDefaults(default_settings) self.variable = KratosMultiphysics.KratosGlobals.GetVariable( settings["variable_name"].GetString()) if (type(self.variable) != KratosMultiphysics.Array1DComponentVariable and type(self.variable) != KratosMultiphysics.DoubleVariable and type(self.variable) != KratosMultiphysics.VectorVariable): msg = "Error in AssignScalarToNodesProcess. Variable type of variable : " + settings[ "variable_name"].GetString( ) + " is incorrect . Must be a scalar or a component" raise Exception(msg) self.model_part = Model[settings["model_part_name"].GetString()] self.mesh = self.model_part.GetMesh(settings["mesh_id"].GetInt()) self.is_fixed = settings["constrained"].GetBool() self.value_is_numeric = False if settings["value"].IsNumber(): self.value_is_numeric = True self.value = settings["value"].GetDouble() else: self.function_string = settings["value"].GetString() self.aux_function = KratosMultiphysics.PythonGenericFunctionUtility( self.function_string, settings["local_axes"]) if (self.aux_function.DependsOnSpace()): self.cpp_apply_function_utility = KratosMultiphysics.ApplyFunctionToNodesUtility( self.mesh.Nodes, self.aux_function) #construct a variable_utils object to speedup fixing self.variable_utils = KratosMultiphysics.VariableUtils() self.step_is_active = False
def __init__(self, Model, settings): default_settings = KratosMultiphysics.Parameters(""" { "mesh_id" : 0, "model_part_name" : "Outlet2D_Outlet_pressure_Auto1", "variable_name" : "TEMPERATURE", "interval" : [0.0, 10.0], "is_fixed" : true, "free_outside_of_interval" : false, "f(x,y,z,t)" : "x+y" } """) settings.ValidateAndAssignDefaults(default_settings) KratosMultiphysics.Process.__init__(self) self.model_part = Model[settings["model_part_name"].GetString()] self.variable = getattr(KratosMultiphysics, settings["variable_name"].GetString()) self.mesh = self.model_part.GetMesh(settings["mesh_id"].GetInt()) #self.table = model_part.GetTable(table_id) self.interval = settings[ "interval"] #expected to be a vector of size 2 self.is_fixed = settings["is_fixed"].GetBool() self.free_outside_of_interval = settings[ "free_outside_of_interval"].GetBool() self.function_string = settings["f(x,y,z,t)"].GetString() ##compile function function_string if (sys.version_info > (3, 0)): self.compiled_function = compile( self.function_string, '', 'eval', optimize=2 ) #here we precompile the expression so that then it is much faster to execute it else: self.compiled_function = compile(self.function_string, '', 'eval') #construct a variable_utils object to speedup fixing self.variable_utils = KratosMultiphysics.VariableUtils() self.tmp = KratosMultiphysics.PythonGenericFunctionUtility( aux_object_cpp_callback(self.compiled_function)) self.cpp_apply_function_utility = KratosMultiphysics.ApplyFunctionToNodesUtility( self.mesh.Nodes, self.tmp) print("finished construction of ApplyCustomFunctionProcess Process")