Exemplo n.º 1
0
 def SerializeModelParameters(self):
     with open(self.project_parameters_path,'r') as parameter_file:
         parameters = KratosMultiphysics.Parameters(parameter_file.read())
     # create wrapper instance to modify current project parameters
     self.wrapper = ParametersWrapper(parameters)
     # save problem name
     self.problem_name = parameters["problem_data"]["problem_name"].GetString()
     # serialize parmeters (to avoid adding new data dependent on the application)
     parameters = self.wrapper.SetModelImportSettingsInputType("use_input_model_part")
     serialized_project_parameters = KratosMultiphysics.StreamSerializer()
     serialized_project_parameters.Save("ParametersSerialization",parameters)
     self.serialized_project_parameters = serialized_project_parameters
     # reset to read the model part
     parameters = self.wrapper.SetModelImportSettingsInputType("mdpa")
     # prepare the model to serialize
     model = KratosMultiphysics.Model()
     fake_sample = generator.GenerateSample(self.problem_name) # only used to serialize
     simulation = self.analysis(model,parameters,fake_sample)
     simulation.Initialize()
     # reset general flags
     main_model_part_name = self.wrapper.GetModelPartName()
     simulation.model.GetModelPart(main_model_part_name).ProcessInfo.SetValue(KratosMultiphysics.IS_RESTARTED,True)
     # serialize model
     serialized_model = KratosMultiphysics.StreamSerializer()
     serialized_model.Save("ModelSerialization",simulation.model)
     self.serialized_model = serialized_model
     # pickle model and parameters
     pickled_model = pickle.dumps(serialized_model, 2) # second argument is the protocol and is NECESSARY (according to pybind11 docs)
     pickled_project_parameters = pickle.dumps(serialized_project_parameters, 2)
     self.pickled_model = pickled_model
     self.pickled_project_parameters = pickled_project_parameters
     self.is_project_parameters_pickled = True
     self.is_model_pickled = True
     print("\n","#"*50," SERIALIZATION COMPLETED ","#"*50,"\n")
Exemplo n.º 2
0
 def ExecuteInstance(self):
     # ensure working level is level 0
     current_level = self.current_level
     sample = generator.GenerateSample(self.problem_name)
     if (current_level != 0):
         raise Exception ("current work level must be = 0 in the Monte Carlo algorithm")
     return (ExecuteInstanceAux_Task(self.pickled_model,self.pickled_project_parameters,sample,self.GetAnalysis(),self.current_level),current_level)
Exemplo n.º 3
0
 def SerializeModelParameters(self):
     with open(self.project_parameters_path, 'r') as parameter_file:
         parameters = KratosMultiphysics.Parameters(parameter_file.read())
     model = KratosMultiphysics.Model()
     fake_sample = generator.GenerateSample()  # only used to serialize
     simulation = self.analysis(model, parameters, fake_sample)
     simulation.Initialize()
     serialized_model = KratosMultiphysics.StreamSerializer()
     serialized_model.Save("ModelSerialization", simulation.model)
     serialized_project_parameters = KratosMultiphysics.StreamSerializer()
     serialized_project_parameters.Save("ParametersSerialization",
                                        simulation.project_parameters)
     # pickle dataserialized_data
     pickled_model = pickle.dumps(
         serialized_model, 2
     )  # second argument is the protocol and is NECESSARY (according to pybind11 docs)
     pickled_project_parameters = pickle.dumps(
         serialized_project_parameters, 2)
     self.pickled_model = pickled_model
     self.pickled_project_parameters = pickled_project_parameters
     self.is_project_parameters_pickled = True
     self.is_model_pickled = True
     print("\n", "#" * 50, " SERIALIZATION COMPLETED ", "#" * 50, "\n")
Exemplo n.º 4
0
def ExecuteInstanceAux_Task(pickled_model, pickled_project_parameters,
                            current_analysis_stage, current_level):
    # overwrite the old model serializer with the unpickled one
    model_serializer = pickle.loads(pickled_model)
    current_model = KratosMultiphysics.Model()
    model_serializer.Load("ModelSerialization", current_model)
    del (model_serializer)
    # overwrite the old parameters serializer with the unpickled one
    serialized_project_parameters = pickle.loads(pickled_project_parameters)
    current_project_parameters = KratosMultiphysics.Parameters()
    serialized_project_parameters.Load("ParametersSerialization",
                                       current_project_parameters)
    del (serialized_project_parameters)
    # initialize the MonteCarloResults class
    mc_results_class = MonteCarloResults(current_level)
    sample = generator.GenerateSample()
    simulation = current_analysis_stage(current_model,
                                        current_project_parameters, sample)
    simulation.Run()
    QoI = simulation.EvaluateQuantityOfInterest()
    mc_results_class.QoI[current_level].append(
        QoI
    )  # saving results in the corresponding list, for MC only list of level 0
    return mc_results_class