The MainKratos.py script generated by GiD contains the following code.
import sys
import time
import importlib
import KratosMultiphysics
def CreateAnalysisStageWithFlushInstance(cls, global_model, parameters):
class AnalysisStageWithFlush(cls):
def __init__(self, model,project_parameters, flush_frequency=10.0):
super().__init__(model,project_parameters)
self.flush_frequency = flush_frequency
self.last_flush = time.time()
sys.stdout.flush()
def Initialize(self):
super().Initialize()
sys.stdout.flush()
def FinalizeSolutionStep(self):
super().FinalizeSolutionStep()
if self.parallel_type == "OpenMP":
now = time.time()
if now - self.last_flush > self.flush_frequency:
sys.stdout.flush()
self.last_flush = now
return AnalysisStageWithFlush(global_model, parameters)
if __name__ == "__main__":
with open("ProjectParameters.json", 'r') as parameter_file:
parameters = KratosMultiphysics.Parameters(parameter_file.read())
analysis_stage_module_name = parameters["analysis_stage"].GetString()
analysis_stage_class_name = analysis_stage_module_name.split('.')[-1]
analysis_stage_class_name = ''.join(x.title() for x in analysis_stage_class_name.split('_'))
analysis_stage_module = importlib.import_module(analysis_stage_module_name)
analysis_stage_class = getattr(analysis_stage_module, analysis_stage_class_name)
global_model = KratosMultiphysics.Model()
simulation = CreateAnalysisStageWithFlushInstance(analysis_stage_class, global_model, parameters)
simulation.Run()
This first part of the script defines a custom AnalysisStageWithFlush class, which extends the analysis stage class specified in the ProjectParameters.json file.
In the case of the MPMApplication, the AnalysisStageWithFlush extends the MpmAnalysis class.
The second part of the script executes the following steps:
- reads the
ProjectParameters.jsonfile using theKratosMultiphysics.Parametersclass; - identifies and imports the analysis stage class (
MpmAnalysis) specified in the"analysis_stage"field of the parameters; - initializes an instance of
AnalysisStageWithFlush, which extends theMpmAnalysisclass; - run the simulation using the initialised analysis stage.
To execute the MainKratos.py script, open the terminal, navigate to the directory containing the script MainKratos.py and execute the command
python MainKratos.py