Python Script Tutorial: Writing a JSon configuration file
As complexity increases one needs more and more flexibility in “configuring” a simulation. This is typically achieved by defining a "configuration file" which organizes all of the "user instructions" to be used in the configuration of the solver.
Some requirements for this configuration are:
- Must allow nesting of parameters (tree-like structure)
- Should allow using default values
- Must allow some form of type checking
- Should permit to echo all of the defaults in a single place
In order to meet such requirements Kratos introduces a "Parameters" object, which provides a thin wrapper to JSon strings.
The typical structure of a JSon file is like the following:
{ "problem_data" : { "problem_name" : "minimal_structure", "model_part_name" : "Structure", "domain_size" : 2, "time_step" : 1.1, "start_time" : 0.0, "end_time" : 1.0, "echo_level" : 0 }, "solver_settings" : { "solver_type" : "solid_mechanics_static_solver", "echo_level" : 0, "solution_type" : "Static", "analysis_type" : "Linear", "model_import_settings" : { "input_type" : "mdpa", "input_filename" : "minimal_structure" }, "line_search" : false, "convergence_criterion" : "Residual_criterion", "displacement_relative_tolerance" : 0.0001, "displacement_absolute_tolerance" : 1e-9, "residual_relative_tolerance" : 0.0001, "residual_absolute_tolerance" : 1e-9, "max_iteration" : 10, "linear_solver_settings" : { "solver_type" : "Super_LU", "scaling" : false, "verbosity" : 0 }, "problem_domain_sub_model_part_list" : ["Parts_Parts_Auto2"], "processes_sub_model_part_list" : ["DISPLACEMENT_Displacement_Auto1","SelfWeight2D_Self_weight_Auto1"], "rotation_dofs" : false }, "constraints_process_list" : [{ "implemented_in_file" : "impose_vector_value_by_components_process", "implemented_in_module" : "KratosMultiphysics", "help" : "This process fixes the selected components of a given vector variable", "process_name" : "ImposeVectorValueByComponentsProcess", "Parameters" : { "mesh_id" : 0, "model_part_name" : "DISPLACEMENT_Displacement_Auto1", "variable_name" : "DISPLACEMENT", "is_fixed_x" : true, "is_fixed_y" : true, "is_fixed_z" : true, "value" : [0.0,0.0,0.0] } }], "loads_process_list" : [{ "implemented_in_file" : "process_factory", "implemented_in_module" : "KratosMultiphysics", "check" : "DirectorVectorNonZero direction", "help" : "This process ", "process_name" : "ApplyConstantVectorValueProcess", "Parameters" : { "mesh_id" : 0, "model_part_name" : "SelfWeight2D_Self_weight_Auto1", "variable_name" : "VOLUME_ACCELERATION", "factor" : 9.8, "direction" : [10.0,0.0,0.0] } }], "output_configuration" : { "result_file_configuration" : { "gidpost_flags" : { "GiDPostMode" : "GiD_PostBinary", "WriteDeformedMeshFlag" : "WriteDeformed", "WriteConditionsFlag" : "WriteConditions", "MultiFileFlag" : "SingleFile" }, "file_label" : "step", "output_control_type" : "step", "output_frequency" : 1.0, "body_output" : true, "node_output" : false, "skin_output" : false, "plane_output" : [], "nodal_results" : ["DISPLACEMENT","REACTION"], "gauss_point_results" : ["VON_MISES_STRESS"] }, "point_data_configuration" : [] }, "restart_options" : { "SaveRestart" : false, "RestartFrequency" : 0, "LoadRestart" : false, "Restart_Step" : 0 }, "constraints_data" : { "incremental_load" : false, "incremental_displacement" : false } }
Such text shall be read to an "input_string" which is then parsed and stored within a "Parameters" object
this is done in python as
input_string = open("ProjectParameters.json",'r').read() settings = Parameters( input_string )
the settings object now behaves effectively as a combination of lists and dictionaries, for example one can obtain the name of the problem to be solved as
name = settings["problem_data"]["problem_name"].GetString()
an important aspect of this is that the user is expected to ask specifically for the type to be provided For the case at hand (the name) one is asking for the item "settings["problem_data"]["problem_name"]" to be provided as a string by calling the "GetString()" method.
similarly one can call
- GetInt()
- GetDouble()
- GetBool()
- GetString()
the type can be queried by the functions
- IsInt()
- IsDouble()
- IsBool()
- IsString()