def setUp(self): self.model = KM.Model() self.model_part = self.model.CreateModelPart("default") self.model_part.AddNodalSolutionStepVariable(KM.PRESSURE) self.model_part.AddNodalSolutionStepVariable(KM.PARTITION_INDEX) self.dimension = 3 self.model_part.ProcessInfo[KM.DOMAIN_SIZE] = self.dimension self.my_pid = KM.DataCommunicator.GetDefault().Rank() self.num_nodes = self.my_pid % 5 + 3 # num_nodes in range (3 ... 7) if self.my_pid == 4: self.num_nodes = 0 # in order to emulate one partition not having local nodes for i in range(self.num_nodes): node = self.model_part.CreateNewNode( i, 0.1 * i, 0.0, 0.0 ) # this creates the same coords in different ranks, which does not matter for this test node.SetSolutionStepValue(KM.PARTITION_INDEX, self.my_pid) node.SetSolutionStepValue(KM.PRESSURE, uniform(-10, 50)) if KM.IsDistributedRun(): KratosMPI.ParallelFillCommunicator(self.model_part).Execute() data_settings = KM.Parameters("""{ "model_part_name" : "default", "variable_name" : "PRESSURE" }""") self.interface_data = CouplingInterfaceData(data_settings, self.model) self.interface_data.Initialize() self.dummy_solver_wrapper = DummySolverWrapper( {"data_4_testing": self.interface_data})
def AllocateHistoricalVariablesFromCouplingDataSettings( data_settings_list, model, solver_name): '''This function allocates historical variables for the Modelparts It retrieves the historical variables that are needed for the ModelParts from the specified CouplingInterfaceData-settings and allocates them on the ModelParts Note that it can only be called after the (Main-)ModelParts are created ''' data_settings_list = data_settings_list.Clone( ) # clone to not mess with the following validation for data_settings in data_settings_list.values(): CouplingInterfaceData.GetDefaultParameters() data_settings.ValidateAndAssignDefaults( CouplingInterfaceData.GetDefaultParameters()) if data_settings["location"].GetString() == "node_historical": variable = KM.KratosGlobals.GetVariable( data_settings["variable_name"].GetString()) main_model_part_name = data_settings["model_part_name"].GetString( ).split(".")[0] if not model.HasModelPart(main_model_part_name): raise Exception( 'ModelPart "{}" does not exist in solver "{}"!'.format( main_model_part_name, solver_name)) main_model_part = model[main_model_part_name] if not main_model_part.HasNodalSolutionStepVariable(variable): cs_print_info( "CoSimTools", 'Allocating historical variable "{}" in ModelPart "{}" for solver "{}"' .format(variable.Name(), main_model_part_name, solver_name)) main_model_part.AddNodalSolutionStepVariable(variable)
def test_wrong_input_no_dim_vector(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "DISPLACEMENT" }""") coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, '"dimension" has to be specifed for vector variables!'): coupling_data.Initialize()
def test_printing(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "DISPLACEMENT", "dimension" : 2 }""") coupling_data = CouplingInterfaceData(settings, self.model) coupling_data.Initialize() self.assertMultiLineEqual(str(coupling_data), coupling_interface_data_str)
def test_non_existing_model_part(self): settings = KM.Parameters("""{ "model_part_name" : "something", "variable_name" : "PRESSURE", "location" : "node_non_historical" }""") coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, "The specified ModelPart is not in the Model, only the following ModelParts are available:"): coupling_data.Initialize()
def test_wrong_input_dim_scalar(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE", "dimension" : 2 }""") coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, '"dimension" cannot be specifed for scalar variables!'): coupling_data.Initialize()
def test_wrong_input_set_data(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE" }""") settings_model_part = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE", "location" : "model_part" }""") coupling_data = CouplingInterfaceData(settings, self.model) coupling_data.Initialize() coupling_data_mp = CouplingInterfaceData(settings_model_part, self.model) coupling_data_mp.Initialize() wrong_data = [1,2,3] correct_data = [1,2,3,4,5] with self.assertRaisesRegex(Exception, "The sizes of the data are not matching, got: 3, expected: 5"): coupling_data.SetData(wrong_data) with self.assertRaisesRegex(Exception, "The buffer-size is not large enough: current buffer size: 2 | requested solution_step_index: 3"): coupling_data.SetData(correct_data, 2) with self.assertRaisesRegex(Exception, "accessing data from previous steps is only possible with historical nodal data!"): coupling_data_mp.SetData(correct_data, 2)
def test_without_initialization(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "DISPLACEMENT", "dimension" : 2 }""") coupling_data = CouplingInterfaceData(settings, self.model) # coupling_data.Initialize() # intentially commented to raise error with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): self.assertMultiLineEqual(str(coupling_data), coupling_interface_data_str) with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.PrintInfo() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.GetModelPart() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.IsDistributed() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.Size() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.GetBufferSize() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.GetData() with self.assertRaisesRegex(Exception, ' can onyl be called after initializing the CouplingInterfaceData!'): coupling_data.SetData([])
def test_wrong_input_missing_solutionstepvar_double(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "TEMPERATURE" }""") exp_error = '"TEMPERATURE" is missing as SolutionStepVariable in ModelPart "mp_4_test"' coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, exp_error): coupling_data.Initialize()
def test_wrong_input_variable_type(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "EXTERNAL_FORCES_VECTOR" }""") exp_error = 'The input for "variable" "EXTERNAL_FORCES_VECTOR" is of variable type "Vector" which is not allowed, only the following variable types are allowed:\nBool, Integer, Unsigned Integer, Double, Component, Array' coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, exp_error): coupling_data.Initialize()
def test_GetSetNodalHistoricalData(self): settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "DISPLACEMENT", "dimension" : 2 }""") coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values exp_data_scal_cur = [ NodeScalarHistValueCurrent(node.Id) for node in self.mp.Nodes ] exp_data_scal_prev = [ NodeScalarHistValuePrevious(node.Id) for node in self.mp.Nodes ] exp_data_vec_cur = GetVectorValues(self.mp.Nodes, NodeVectorHistValueCurrent, 2) exp_data_vec_prev = GetVectorValues(self.mp.Nodes, NodeVectorHistValuePrevious, 2) self.__CheckData(exp_data_scal_cur, coupling_data_scal.GetData()) self.__CheckData(exp_data_vec_cur, coupling_data_vec.GetData()) self.__CheckData(exp_data_scal_prev, coupling_data_scal.GetData(1)) self.__CheckData(exp_data_vec_prev, coupling_data_vec.GetData(1)) # 2. check setting and getting works set_data_scal_cur = [ ElementScalarValue(node.Id) for node in self.mp.Nodes ] set_data_scal_prev = [ ConditionScalarValue(node.Id) for node in self.mp.Nodes ] set_data_vec_cur = GetVectorValues(self.mp.Nodes, ElementVectorValue, 2) set_data_vec_prev = GetVectorValues(self.mp.Nodes, ConditionVectorValue, 2) self.__CheckSetGetData(set_data_scal_cur, coupling_data_scal) self.__CheckSetGetData(set_data_vec_cur, coupling_data_vec) self.__CheckSetGetData(set_data_scal_prev, coupling_data_scal, 1) self.__CheckSetGetData(set_data_vec_prev, coupling_data_vec, 1)
def test_wrong_input_dim_array(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "DISPLACEMENT", "dimension" : 4 }""") exp_error = '"dimension" can only be 1,2,3 when using variables of type "Array"' coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, exp_error): coupling_data.Initialize()
def test_wrong_input_location(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE", "location" : "dummy" }""") exp_error = '"dummy" is not allowed as "location", only the following options are possible:\nnode_historical, node_non_historical, element, condition, process_info, model_part' coupling_data = CouplingInterfaceData(settings, self.model) with self.assertRaisesRegex(Exception, exp_error): coupling_data.Initialize()
def test_GetSetElementalData(self): settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "location" : "element", "variable_name" : "DENSITY" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "FORCE", "location" : "element", "dimension" : 2 }""") coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values exp_data_scal = [ElementScalarValue(elem.Id) for elem in self.mp.Elements] exp_data_vec = GetVectorValues(self.mp.Elements, ElementVectorValue, 2) self.__CheckData(exp_data_scal, coupling_data_scal.GetData()) self.__CheckData(exp_data_vec, coupling_data_vec.GetData()) # 2. check setting and getting works set_data_scal = [NodeScalarNonHistValue(elem.Id) for elem in self.mp.Elements] set_data_vec = GetVectorValues(self.mp.Elements, NodeVectorNonHistValue, 2) self.__CheckSetGetData(set_data_scal, coupling_data_scal) self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetNodalNonHistoricalData(self): settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "location" : "node_non_historical", "variable_name" : "TEMPERATURE" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "VELOCITY", "location" : "node_non_historical", "dimension" : 3 }""") self.mp.ProcessInfo[KM.DOMAIN_SIZE] = 3 coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values exp_data_scal = [NodeScalarNonHistValue(node.Id) for node in self.mp.Nodes] exp_data_vec = GetVectorValues(self.mp.Nodes, NodeVectorNonHistValue, 3) self.__CheckData(exp_data_scal, coupling_data_scal.GetData()) self.__CheckData(exp_data_vec, coupling_data_vec.GetData()) # 2. check setting and getting works set_data_scal = [ConditionScalarValue(node.Id) for node in self.mp.Nodes] set_data_vec = GetVectorValues(self.mp.Nodes, ConditionVectorValue, 3) self.__CheckSetGetData(set_data_scal, coupling_data_scal) self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetModelPartData(self): settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "location" : "model_part", "variable_name" : "NODAL_MASS" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "TORQUE", "location" : "model_part", "dimension" : 1 }""") self.mp.ProcessInfo[KM.DOMAIN_SIZE] = 1 coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values self.__CheckData([model_part_scalar_value], coupling_data_scal.GetData()) self.__CheckData([model_part_vector_value[0]], coupling_data_vec.GetData()) # 2. check setting and getting works set_data_scal = [model_part_scalar_value_2] set_data_vec = [model_part_vector_value_2[0]] self.__CheckSetGetData(set_data_scal, coupling_data_scal) self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetProcessInfoData(self): settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "location" : "process_info", "variable_name" : "NODAL_ERROR" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "NORMAL", "location" : "process_info", "dimension" : 2 }""") coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values self.__CheckData([process_info_scalar_value], coupling_data_scal.GetData()) self.__CheckData( [process_info_vector_value[0], process_info_vector_value[1]], coupling_data_vec.GetData()) # 2. check setting and getting works set_data_scal = [model_part_scalar_value] set_data_vec = [model_part_vector_value[0], model_part_vector_value[1]] self.__CheckSetGetData(set_data_scal, coupling_data_scal) self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_GetSetConditionalData(self): if using_pykratos: self.skipTest("This test cannot be run with pyKratos!") settings_scal = KM.Parameters("""{ "model_part_name" : "mp_4_test", "location" : "condition", "variable_name" : "YOUNG_MODULUS" }""") settings_vec = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "ROTATION", "location" : "condition", "dimension" : 2 }""") coupling_data_scal = CouplingInterfaceData(settings_scal, self.model) coupling_data_vec = CouplingInterfaceData(settings_vec, self.model) coupling_data_scal.Initialize() coupling_data_vec.Initialize() # 1. check the initial values exp_data_scal = [ConditionScalarValue(cond.Id) for cond in self.mp.Conditions] exp_data_vec = GetVectorValues(self.mp.Conditions, ConditionVectorValue, 2) self.__CheckData(exp_data_scal, coupling_data_scal.GetData()) self.__CheckData(exp_data_vec, coupling_data_vec.GetData()) # 2. check setting and getting works set_data_scal = [NodeScalarHistValuePrevious(cond.Id) for cond in self.mp.Conditions] set_data_vec = GetVectorValues(self.mp.Conditions, NodeVectorHistValuePrevious, 2) self.__CheckSetGetData(set_data_scal, coupling_data_scal) self.__CheckSetGetData(set_data_vec, coupling_data_vec)
def test_unallowed_names(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE" }""") with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'): CouplingInterfaceData(settings, self.model, "") with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'): CouplingInterfaceData(settings, self.model, "aaa.bbbb") with self.assertRaisesRegex(Exception, 'The name cannot be empty, contain whitespaces or "."!'): CouplingInterfaceData(settings, self.model, "aaa bbb")
def __init__(self, settings, name): """Constructor of the Base Solver Wrapper The derived classes should do the following things in their constructors: 1. call the base-class constructor (i.e. the constructor of this class => CoSimulationSolverWrapper) 2. create the ModelParts required for the CoSimulation 3. Optional: call "_AllocateHistoricalVariablesFromCouplingData" to allocate the nodal historical variables on the previously created ModelParts (this should not be necessary for Kratos) => this has to be done before the meshes/coupling-interfaces are read/received/imported (due to how the memory allocation of Kratos works for historical nodal values) """ # Every SolverWrapper has its own model, because: # - the names can be easily overlapping (e.g. "Structure.Interface") # - Solvers should not be able to access the data of other solvers directly! self.model = KM.Model() self.settings = settings self.settings.ValidateAndAssignDefaults(self._GetDefaultSettings()) self.name = name self.echo_level = self.settings["echo_level"].GetInt() self.data_dict = { data_name: CouplingInterfaceData(data_config, self.model, data_name, self.name) for (data_name, data_config) in self.settings["data"].items() } # The IO is only used if the corresponding solver is used in coupling and it initialized from the "higher instance, i.e. the coupling-solver self.__io = None
def __CreateInterfaceDataDict(self): data_dict = dict() for data_name, data_config in self.settings["data"].items(): data_dict[data_name] = CouplingInterfaceData( data_config, self.model) return data_dict
def test_var_does_not_exist(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "var_that_hopefully_none_will_ever_create_otherwise_this_test_will_be_wrong" }""") with self.assertRaisesRegex(Exception, 'does not exist!'): CouplingInterfaceData(settings, self.model)
def setUp(self): self.model = KM.Model() self.model_part = self.model.CreateModelPart("default") self.model_part.AddNodalSolutionStepVariable(KM.PRESSURE) for i in range(10): # using 10 nodes gives suitable values in the tests node = self.model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # position of nodes does not matter for this test node.SetSolutionStepValue(KM.PRESSURE, 1.0) data_settings = KM.Parameters("""{ "model_part_name" : "default", "variable_name" : "PRESSURE" }""") self.interface_data = CouplingInterfaceData(data_settings, self.model) self.interface_data.Initialize() self.dummy_solver_wrapper = DummySolverWrapper({"data_4_testing" : self.interface_data})
def test_non_existing_model_part(self): settings = KM.Parameters("""{ "model_part_name" : "something", "variable_name" : "PRESSURE", "location" : "node_non_historical" }""") with self.assertRaisesRegex(Exception, 'The ModelPart named : "something" was not found either as root-ModelPart or as a flat name. The total input string was "something'): CouplingInterfaceData(settings, self.model)
def Initialize(self): self.data_dict = { data_name: CouplingInterfaceData(data_config, self.model, data_name, self.name) for (data_name, data_config) in self.settings["data"].items() } if self.__HasIO(): self.__GetIO().Initialize()
def CreateMainModelPartsFromCouplingDataSettings(data_settings_list, model, solver_name): '''This function creates the Main-ModelParts that are used in the specified CouplingInterfaceData-settings''' data_settings_list = data_settings_list.Clone( ) # clone to not mess with the following validation for data_settings in data_settings_list.values(): CouplingInterfaceData.GetDefaultParameters() data_settings.ValidateAndAssignDefaults( CouplingInterfaceData.GetDefaultParameters()) main_model_part_name = data_settings["model_part_name"].GetString( ).split(".")[0] if not model.HasModelPart(main_model_part_name): model.CreateModelPart(main_model_part_name) cs_print_info( "CoSimTools", 'Created ModelPart "{}" for solver "{}"'.format( main_model_part_name, solver_name))
def test_wrong_input_missing_solutionstepvar_component(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "FORCE_X" }""") exp_error = '"FORCE_X" is missing as SolutionStepVariable in ModelPart "mp_4_test"' with self.assertRaisesRegex(Exception, exp_error): CouplingInterfaceData(settings, self.model)
def test_Export_ImportCouplingData(self): p = self.__RunPythonInSubProcess("import_export_data") model = KM.Model() io_settings = KM.Parameters("""{ "connect_to" : "impExp" }""") solver_name = "ExpImp" # aka "my_name" for the CoSimIO kratos_co_sim_io = CreateKratosCoSimIO(io_settings, model, solver_name) # this connects kratos_co_sim_io.Initialize() model_part = model.CreateModelPart("for_test") model_part.AddNodalSolutionStepVariable(KM.PRESSURE) model_part.AddNodalSolutionStepVariable(KM.TEMPERATURE) for i in range(5): node = model_part.CreateNewNode(i+1, 0.0, 0.0, 0.0) # using same coord, doesn't matter for this test node.SetSolutionStepValue(KM.PRESSURE, i*1.7) settings_pres = KM.Parameters("""{ "model_part_name" : "for_test", "variable_name" : "PRESSURE" }""") settings_temp = KM.Parameters("""{ "model_part_name" : "for_test", "variable_name" : "TEMPERATURE" }""") interface_data_pres = CouplingInterfaceData(settings_pres, model, "data_exchange_1") interface_data_temp = CouplingInterfaceData(settings_temp, model, "data_exchange_2") data_configuration_export = {"type" : "coupling_interface_data", "interface_data" : interface_data_pres} kratos_co_sim_io.ExportData(data_configuration_export) data_configuration_import = {"type" : "coupling_interface_data", "interface_data" : interface_data_temp} kratos_co_sim_io.ImportData(data_configuration_import) kratos_co_sim_io.Finalize() # this disconnects # checking the values after disconnecting to avoid deadlock for i, node in enumerate(model_part.Nodes): self.assertAlmostEqual(node.GetSolutionStepValue(KM.TEMPERATURE), i*1.7) self.__CheckSubProcess(p)
def setUp(self): self.model = KM.Model() self.model_part = self.model.CreateModelPart("default") self.model_part.AddNodalSolutionStepVariable(KM.PRESSURE) self.model_part.ProcessInfo[KM.TIME] = 0.0 self.model_part.ProcessInfo[KM.STEP] = 0 for i in range(5): new_node = self.model_part.CreateNewNode(i+1, i*0.1, 0.0, 0.0) new_node.SetSolutionStepValue(KM.PRESSURE, 0, i+1.3) data_settings = KM.Parameters("""{ "model_part_name" : "default", "variable_name" : "PRESSURE" }""") self.interface_data = CouplingInterfaceData(data_settings, self.model) self.interface_data.Initialize() self.solver_wrappers = {"dummy_solver" : DummySolverWrapper({"data_4_testing" : self.interface_data})}
def test_GetHistoricalVariableDict(self): settings = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE" }""") coupling_data = CouplingInterfaceData(settings, self.model) exp_dict = {"mp_4_test": KM.PRESSURE} dict_res = coupling_data.GetHistoricalVariableDict() self.assertEqual(len(exp_dict), len(dict_res)) self.assertEqual(exp_dict["mp_4_test"].Name(), dict_res["mp_4_test"].Name()) # this should not give anything since there are no historical variables in this case settings_2 = KM.Parameters("""{ "model_part_name" : "mp_4_test", "variable_name" : "PRESSURE", "location" : "element" }""") coupling_data_2 = CouplingInterfaceData(settings_2, self.model) exp_dict_2 = {} dict_res_2 = coupling_data_2.GetHistoricalVariableDict() self.assertEqual(len(exp_dict_2), len(dict_res_2))