def CalculateValue(self): Logger.PrintInfo("MassResponse", "Starting primal analysis for response", self.identifier) startTime = timer.time() value = self.response_function_utility.CalculateValue() self.model_part.ProcessInfo[StructuralMechanicsApplication.RESPONSE_VALUE] = value Logger.PrintInfo("MassResponse", "Time needed for calculating the response value = ",round(timer.time() - startTime,2),"s")
def ImportApplication(application, application_name, application_folder, caller, mod_path=None): KratosGlobals = KratosMultiphysics.KratosGlobals Kernel = KratosGlobals.Kernel applications_root = KratosGlobals.ApplicationsRoot Logger.PrintInfo("", "Importing " + application_name) # Add python scrips folder to path application_path = os.path.join(applications_root, application_folder) python_path = os.path.join(application_path, 'python_scripts') sys.path.append(python_path) # Add constitutive laws python scrips folder to path constitutive_laws_path = os.path.join(python_path, 'constitutive_laws') sys.path.append(constitutive_laws_path) warn_msg = '\nThe python-import-mechanism used for application "' + application_name warn_msg += '" is DEPRECATED!\n' warn_msg += 'Please check the following website for instuctions on how to update it:\n' warn_msg += 'https://github.com/KratosMultiphysics/Kratos/wiki/Applications-as-python-modules\n' warn_msg += 'The old mechanism will be removed on 01.10.2019!\n' Logger.PrintWarning('\n\x1b[1;31mDEPRECATION-WARNING\x1b[0m', warn_msg) # adding the scripts in "APP_NAME/python_scripts" such that they are treated as a regular python-module if mod_path is not None: # optional for backwards compatibility mod_path.append(python_path) # Add application to kernel Kernel.ImportApplication(application)
def _SynchronizeAdjointFromPrimal(self): Logger.PrintInfo( self._GetLabel(), "Synchronize primal and adjoint modelpart for response:", self.identifier) if len(self.primal_model_part.Nodes) != len( self.adjoint_model_part.Nodes): raise RuntimeError( "_SynchronizeAdjointFromPrimal: Model parts have a different number of nodes!" ) for primal_node, adjoint_node in zip(self.primal_model_part.Nodes, self.adjoint_model_part.Nodes): adjoint_node.X0 = primal_node.X0 adjoint_node.Y0 = primal_node.Y0 adjoint_node.Z0 = primal_node.Z0 adjoint_node.X = primal_node.X adjoint_node.Y = primal_node.Y adjoint_node.Z = primal_node.Z # Put primal solution on adjoint model Logger.PrintInfo(self._GetLabel(), "Transfer primal state to adjoint model part.") variable_utils = KM.VariableUtils() for variable in self.primal_state_variables: variable_utils.CopyModelPartNodalVar(variable, self.primal_model_part, self.adjoint_model_part, 0)
def _SynchronizeAdjointFromPrimal(self): Logger.PrintInfo( self._GetLabel(), "Synchronize primal and adjoint modelpart for response:", self.identifier) if len(self.primal_model_part.Nodes) != len( self.adjoint_model_part.Nodes): raise RuntimeError( "_SynchronizeAdjointFromPrimal: Model parts have a different number of nodes!" ) # TODO this should happen automatically for primal_node, adjoint_node in zip(self.primal_model_part.Nodes, self.adjoint_model_part.Nodes): adjoint_node.X0 = primal_node.X0 adjoint_node.Y0 = primal_node.Y0 adjoint_node.Z0 = primal_node.Z0 adjoint_node.X = primal_node.X adjoint_node.Y = primal_node.Y adjoint_node.Z = primal_node.Z # Put primal solution on adjoint model - for "auto" setting, else it has to be done by the user e.g. using hdf5 process if self.response_settings["adjoint_settings"].GetString() == "auto": Logger.PrintInfo(self._GetLabel(), "Transfer primal state to adjoint model part.") variable_utils = KratosMultiphysics.VariableUtils() for variable in self.primal_state_variables: variable_utils.CopyModelPartNodalVar(variable, self.primal_model_part, self.adjoint_model_part, 0)
def RenumberNodesIdsToAvoidRepeating(fluid_model_part, dem_model_part, rigid_faces_model_part): max_fluid_id = FindMaxNodeId(fluid_model_part) must_renumber = True in (node.Id < max_fluid_id for node in list(dem_model_part.Nodes) + list(rigid_faces_model_part.Nodes)) if must_renumber: Logger.PrintWarning( "SwimmingDEM", "WARNING!, the DEM model part and the fluid model part have some ID values in common. Renumbering..." ) for node in dem_model_part.Nodes: node.Id += max_fluid_id for node in rigid_faces_model_part.Nodes: node.Id += max_fluid_id Logger.PrintWarning( "SwimmingDEM", "The DEM model part and the fem-DEM model parts Ids have been renumbered" )
def _GetAdjointParameters(self): with open(self.response_settings["adjoint_settings"].GetString(), 'r') as parameter_file: adjoint_parameters = Parameters(parameter_file.read()) if self.response_settings["transfer_response_parameters"].GetBool(): # sensitivity settings if adjoint_parameters["solver_settings"].Has( "sensitivity_settings"): Logger.PrintWarning( self._GetLabel(), "Adjoint settings already have 'sensitivity_settings' - Will be overwritten!" ) adjoint_parameters["solver_settings"].RemoveValue( "sensitivity_settings") adjoint_parameters["solver_settings"].AddValue( "sensitivity_settings", self.response_settings["sensitivity_settings"]) # response settings if adjoint_parameters["solver_settings"].Has( "response_function_settings"): Logger.PrintWarning( self._GetLabel(), "Adjoint settings already have 'response_function_settings' - Will be overwritten!" ) adjoint_parameters["solver_settings"].RemoveValue( "response_function_settings") adjoint_parameters["solver_settings"].AddValue( "response_function_settings", self.response_settings) return adjoint_parameters
def _SynchronizeAdjointFromPrimal(self): Logger.PrintInfo( self._GetLabel(), "Synchronize primal and adjoint modelpart for response:", self.identifier) if len(self.primal_model_part.Nodes) != len( self.adjoint_model_part.Nodes): raise RuntimeError( "_SynchronizeAdjointFromPrimal: Model parts have a different number of nodes!" ) # TODO this should happen automatically for primal_node, adjoint_node in zip(self.primal_model_part.Nodes, self.adjoint_model_part.Nodes): adjoint_node.X0 = primal_node.X0 adjoint_node.Y0 = primal_node.Y0 adjoint_node.Z0 = primal_node.Z0 adjoint_node.X = primal_node.X adjoint_node.Y = primal_node.Y adjoint_node.Z = primal_node.Z # Put primal solution on adjoint model if self.primal_data_transfer_with_python: Logger.PrintInfo(self._GetLabel(), "Transfer primal state to adjoint model part.") variable_utils = KratosMultiphysics.VariableUtils() for variable in self.primal_state_variables: variable_utils.CopyModelPartNodalVar(variable, self.primal_model_part, self.adjoint_model_part, 0)
def CalculateGradient(self): Logger.PrintInfo("\n> Starting gradient calculation for response", self.identifier) startTime = timer.time() self.response_function_utility.CalculateGradient() Logger.PrintInfo("> Time needed for calculating gradients", round(timer.time() - startTime, 2), "s")
def CalculateGradient(self): Logger.PrintInfo("\n> Starting adjoint analysis for response:", self.identifier) startTime = timer.time() self.adjoint_analysis._GetSolver().Predict() self.adjoint_analysis._GetSolver().SolveSolutionStep() Logger.PrintInfo("> Time needed for solving the adjoint analysis = ", round(timer.time() - startTime, 2), "s")
def InitializeSolutionStep(self): # Run the primal analysis. # TODO if primal_analysis.status==solved: return Logger.PrintInfo(self._GetLabel(), "Starting primal analysis for response:", self.identifier) startTime = timer.time() if not self.primal_analysis.time < self.primal_analysis.end_time: self.primal_analysis.end_time += 1 self.primal_analysis.RunSolutionLoop() Logger.PrintInfo(self._GetLabel(), "Time needed for solving the primal analysis = ",round(timer.time() - startTime,2),"s")
def CalculateGradient(self): # synchronize the modelparts self._SynchronizeAdjointFromPrimal() startTime = timer.time() Logger.PrintInfo(self._GetLabel(), "Starting adjoint analysis for response:", self.identifier) if not self.adjoint_analysis.time < self.adjoint_analysis.end_time: self.adjoint_analysis.end_time += 1 self.adjoint_analysis.RunSolutionLoop() Logger.PrintInfo(self._GetLabel(), "Time needed for solving the adjoint analysis = ",round(timer.time() - startTime,2),"s")
def CalculateGradient(self): Logger.PrintInfo("\n> Starting gradient calculation for response", self.identifier) startTime = timer.time() for node in self.model_part.Nodes: normalized_normal = node.GetSolutionStepValue(KSO.NORMALIZED_SURFACE_NORMAL) self.gradient[node.Id] = normalized_normal Logger.PrintInfo("> Time needed for calculating gradients = ", round(timer.time() - startTime,2), "s")
def CalculateValue(self): Logger.PrintInfo("FaceAngleResponse", "Starting calculation of response value:", self.identifier) startTime = timer.time() self.value = self.response_function_utility.CalculateValue() Logger.PrintInfo("FaceAngleResponse", "Time needed for calculating the response value = ", round(timer.time() - startTime, 2), "s")
def Writeresults(self, time): # We reorder the Id of the model parts femdem_util = FEMDEM.FEMDEMCouplingUtilities() reorder_util_elem = FEMDEM.RenumberingNodesUtility( self.solid_model_part, self.fluid_model_part) reorder_util_elem.RenumberElements() Logger.PrintInfo("", "") Logger.PrintInfo( "", "***************** PRINTING RESULTS FOR GID *************************" ) Logger.PrintInfo("", "") Logger.Flush() number_pfem_nodes = femdem_util.GetNumberOfNodes(self.fluid_model_part) for node in self.balls_model_part.Nodes: node.Id = node.Id + number_pfem_nodes if self.GiDMultiFileFlag == "Multiples": self.mixed_solid_fluid_model_part.Elements.clear() self.mixed_solid_fluid_model_part.Nodes.clear() self.mixed_solid_balls_model_part.Elements.clear() self.mixed_solid_balls_model_part.Nodes.clear() self.mixed_solid_balls_fluid_model_part.Elements.clear() self.mixed_solid_balls_fluid_model_part.Nodes.clear() # Now we fill the mixed MDPA in order to print post_utils = DEMApplication.PostUtilities() post_utils.AddModelPartToModelPart( self.mixed_solid_fluid_model_part, self.fluid_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_model_part, self.balls_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_model_part, self.rigid_faces_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_fluid_model_part, self.balls_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_fluid_model_part, self.rigid_faces_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_fluid_model_part, self.solid_model_part) post_utils.AddModelPartToModelPart( self.mixed_solid_balls_fluid_model_part, self.fluid_model_part) FEMDEM.FEMDEMCouplingUtilities().RemoveDuplicates( self.mixed_solid_fluid_model_part) self.write_dem_fem_results(time) reorder_util_elem.UndoRenumberElements()
def CalculateValue(self): Logger.PrintInfo("StrainEnergyResponse", "Starting primal analysis for response", self.identifier) startTime = timer.time() self.primal_analysis._GetSolver().Predict() self.primal_analysis._GetSolver().SolveSolutionStep() Logger.PrintInfo("StrainEnergyResponse", "Time needed for solving the primal analysis",round(timer.time() - startTime,2),"s") startTime = timer.time() value = self.response_function_utility.CalculateValue() self.primal_model_part.ProcessInfo[StructuralMechanicsApplication.RESPONSE_VALUE] = value Logger.PrintInfo("StrainEnergyResponse", "Time needed for calculating the response value",round(timer.time() - startTime,2),"s")
def Assess( self, model_part ): # in the first time step the 'old' pressure vector is created and filled stationarity = self.tool.AssessStationarity(model_part, self.tol) if stationarity: Logger.PrintInfo("SwimmingDEM", "\nThe fluid has reached a stationary state.") Logger.PrintInfo("Its calculation will be omitted from here on.\n") Logger.Flush() return stationarity
def CalculateGradient(self): Logger.PrintInfo("FaceAngleResponse", "Starting gradient calculation for response", self.identifier) for node in self.model_part.Nodes: node.SetSolutionStepValue(KM.SHAPE_SENSITIVITY, [0.0, 0.0, 0.0]) startTime = timer.time() self.response_function_utility.CalculateGradient() Logger.PrintInfo("FaceAngleResponse", "Time needed for calculating gradients", round(timer.time() - startTime, 2), "s")
def Initialize(self): if self.response_settings["model_import_settings"][ "input_type"].GetString() == "mdpa": file_name = self.response_settings["model_import_settings"][ "input_filename"].GetString() model_part_io = KM.ModelPartIO(file_name) model_part_io.ReadModelPart(self.model_part) else: self.model_part = self.model.GetModelPart(self._model_part_name) only = self.response_settings["only"].GetString() if only != "": only_part = self.model.GetModelPart(only) if only_part.NumberOfConditions() == 0: _AddConditionsFromParent(self.model_part, only_part) Logger.PrintWarning( "FaceAngleResponse", "Automatically added {} conditions to model_part '{}'.". format(only_part.NumberOfConditions(), only_part.Name)) else: only_part = self.model_part if only_part.NumberOfConditions() == 0: raise RuntimeError( "The model_part '{}' does not have any surface conditions!". format(only_part.Name)) self.response_function_utility = KSO.FaceAngleResponseFunctionUtility( only_part, self.response_settings) self.response_function_utility.Initialize()
def CalculateGradient(self): Logger.PrintInfo("\n> Starting gradient calculation for response", self.identifier) startTime = timer.time() if not self.directions or not self.signed_distances: self._CalculateDistances() for i, node in enumerate(self.model_part.Nodes): gradient = self._CalculateNodalGradient( self.signed_distances[i], self.directions[i * 3:i * 3 + 3]) self.gradient[node.Id] = gradient Logger.PrintInfo("> Time needed for calculating gradients = ", round(timer.time() - startTime, 2), "s")
def CalculateValue(self): Logger.PrintInfo("\n> Starting primal analysis for response", self.identifier) startTime = timer.time() value = 0.0 for node in self.model_part.Nodes: shape_update = node.GetSolutionStepValue(KSO.SHAPE_UPDATE) normalized_normal = node.GetSolutionStepValue(KSO.NORMALIZED_SURFACE_NORMAL) value += shape_update[0] * normalized_normal[0] value += shape_update[1] * normalized_normal[1] value += shape_update[2] * normalized_normal[2] self.value = value + self.previous_value Logger.PrintInfo("> Time needed for calculating the response value = ", round(timer.time() - startTime,2), "s")
def ExecuteInitialize(self): self.empirical_spring_element_process.ExecuteInitialize() ## add new element in the computing MP for element_i in self.custom_model_part.Elements: self.computing_model_part.AddElement(element_i, 0) Logger.PrintInfo("Initialized", "EmpiricalSpringElementProcess")
def ExecuteInitializeSolutionStep(self): self.UpdateInletPosition() Logger.PrintInfo('ImposeWindInletProcess', 'inlet position = %e' % self.inlet_position) if len(self.inlet_nodes) > 0: self.AssignVelocity() self.ApplyRamp()
def CalculateValue(self): startTime = timer.time() self.value = self._GetResponseFunctionUtility().CalculateValue( self.primal_model_part) Logger.PrintInfo(self._GetLabel(), "Time needed for calculating the response value = ", round(timer.time() - startTime, 2), "s")
def ExecuteInitialize(self): self.edge_cable_element_process.ExecuteInitialize() ## add new element in the computing MP for element_i in self.edge_model_part.Elements: self.computing_model_part.AddElement(element_i, 0) Logger.PrintInfo("Initialized","EdgeCableElementProcess")
def _CheckParameters(self, parameters): if not parameters["solver_settings"].Has( "reform_dofs_at_each_step") or not parameters[ "solver_settings"]["reform_dofs_at_each_step"].GetBool(): if not parameters["solver_settings"].Has( "reform_dofs_at_each_step"): parameters["solver_settings"].AddEmptyValue( "reform_dofs_at_each_step") parameters["solver_settings"]["reform_dofs_at_each_step"].SetBool( True) wrn_msg = 'This solver requires the setting reform the dofs at each step in optimization.' wrn_msg += 'The solver setting has been set to True' Logger.PrintWarning(self._GetLabel(), wrn_msg) for subproc_keys, subproc_values in parameters["processes"].items(): for process in subproc_values: if "wake" in process["python_module"].GetString(): if not process["Parameters"].Has( "compute_wake_at_each_step" ) or not process["Parameters"][ "compute_wake_at_each_step"].GetBool(): if not process["Parameters"].Has( "compute_wake_at_each_step"): process["Parameters"].AddEmptyValue( "compute_wake_at_each_step") process["Parameters"]["compute_wake_at_each_step"].SetBool( True) return parameters
def ImportApplication(application, application_name, application_folder, caller): Globals = KratosMultiphysics.KratosGlobals Kernel = Globals.Kernel main_caller = Globals.AuthorizedCaller applications_root = Globals.ApplicationsRoot # caller and main_caller are generated from the output of inspect.stack(). # In particular position [1] is the name of the file containing the call. # Note that position [0] (a frame instance) could also be used for the check, # but can return false if both calls are made from the python interpreter if main_caller[1] != caller[1]: msg = "\n***\n* Python file " + str(caller[1]) + "\n* requires " + str(application_name) + "\n* Please import it from your main Python script, " +str(main_caller[1]+'\n***') # print caller # print main_caller raise RuntimeError(msg) elif application_name not in Globals.RequestedApplications: # This check is possibly redundant, as Python won't import the same module twice Logger.PrintInfo("", "Importing " + application_name) # Add application to dictionary of registered applications Globals.RequestedApplications[application_name] = application # Add python scrips folder to path application_path = os.path.join(applications_root, application_folder) python_path = os.path.join(application_path, 'python_scripts') sys.path.append(python_path) # Add constitutive laws python scrips folder to path constitutive_laws_path = os.path.join(python_path, 'constitutive_laws') sys.path.append(constitutive_laws_path) # Add application to kernel Kernel.ImportApplication(application) # Dynamic renumbering of variables to ensure consistency Kernel.Initialize() for iterName, iterApplication in list(Globals.RequestedApplications.items()): # print("Initializing",iterName) Kernel.InitializeApplication(iterApplication)
def FinalizeSolutionStep(self): super().FinalizeSolutionStep() tolerance = 1.001 for node in self.spheres_model_part.Nodes: node_vel = node.GetSolutionStepValue(KratosMultiphysics.VELOCITY_Y) node_force = node.GetSolutionStepValue( KratosMultiphysics.TOTAL_FORCES_Y) if node.Id == 6: if self.time >= 1.15: Logger.PrintInfo(node_vel) Logger.PrintInfo(node_force) self.assertAlmostEqual(node_vel, 0.380489240, delta=tolerance) self.assertAlmostEqual(node_force, -120983.1002, delta=tolerance)
def CalculateValue(self): Logger.PrintInfo("\n> Starting primal analysis for response", self.identifier) startTime = timer.time() if not self.directions or not self.signed_distances: self._CalculateDistances() value = 0.0 for i in range(len(self.signed_distances)): value += self._CalculateNodalValue(self.signed_distances[i]) self.value = value Logger.PrintInfo("> Time needed for calculating the response value = ", round(timer.time() - startTime, 2), "s")
def CalculateValue(self): startTime = timer.time() value = self._GetResponseFunctionUtility().CalculateValue( self.primal_model_part) Logger.PrintInfo("> Time needed for calculating the response value = ", round(timer.time() - startTime, 2), "s") self.primal_model_part.ProcessInfo[ StructuralMechanicsApplication.RESPONSE_VALUE] = value
def Writeresults(self, time): Logger.PrintInfo( "SwimmingDEM", "******************* PRINTING RESULTS FOR GID ***************************" ) Logger.Flush() gid_output_options = self.project_parameters["sdem_output_processes"][ "gid_output"][0]["Parameters"] result_file_configuration = gid_output_options[ "postprocess_parameters"]["result_file_configuration"] multiple_files_option_key = result_file_configuration["gidpost_flags"][ "MultiFileFlag"].GetString() if multiple_files_option_key == "MultipleFiles": renumbering_utility = SDEM.RenumberingNodesUtility( self.fluid_model_part, self.rigid_faces_model_part, self.balls_model_part) renumbering_utility.Renumber() self.mixed_model_part.Elements.clear() self.mixed_model_part.Nodes.clear() # here order is important! self.post_utilities.AddModelPartToModelPart( self.mixed_model_part, self.balls_model_part) self.post_utilities.AddModelPartToModelPart( self.mixed_model_part, self.rigid_faces_model_part) self.post_utilities.AddModelPartToModelPart( self.mixed_model_part, self.fluid_model_part) self.gid_io.write_swimming_DEM_results( time, self.fluid_model_part, self.balls_model_part, self.clusters_model_part, self.rigid_faces_model_part, self.mixed_model_part, self.vars_man.nodal_results, self.vars_man.dem_nodal_results, self.vars_man.clusters_nodal_results, self.vars_man.rigid_faces_nodal_results, self.vars_man.mixed_nodal_results, self.vars_man.gauss_points_results) if multiple_files_option_key == "MultipleFiles": renumbering_utility.UndoRenumber()