Skip to content

Manipulating solution values

Aditya Ghantasala edited this page Mar 23, 2019 · 25 revisions

Manipulating solution values

1. Introduction

This tutorial session is intended to illustrate how the Kratos' data structure, when accessed form Python, can be used to manipulate and set custom boundary conditions, solutions over the computational domain in a given problem. The following explanation builds upon the previous sessions of Data management and Solving strategies.

Here the CFD problem described in the first tutorial of Running an example from GiD is used as the working example. The setup files (mdpa, MainKratos.py and the JSON) of this problem can be downloaded from here. Please extract the zip file and make it ready for the next task.

2. The CFD example

As the first step of this tutorial, we understand the CFD example problem setup and run the simulation to observe the initial result. The CFD problem setup is illustrated below

As illustrated, a no slip condition is applied on the top and bottom walls of the 2D fluid domain. A uniform and constant velocity inlet is applied on the left hand side and a pressure outlet (0 pressure) is applied on the right hand side of the domain. These boundary condition are defined in the ProjectParameters.json. The following lines from the ProjectPrameters.json reflect the same

       "boundary_conditions_process_list" : [{
            "python_module" : "apply_inlet_process",
            "kratos_module" : "KratosMultiphysics.FluidDynamicsApplication",
            "Parameters"    : {
                "model_part_name" : "FluidModelPart.AutomaticInlet2D_Inlet",
                "variable_name"   : "VELOCITY",
                "modulus"         : "25.0",
                "direction"       : "automatic_inwards_normal",
                "interval"        : [0,10.0]
            }
        },{
            "python_module" : "apply_inlet_process",
            "kratos_module" : "KratosMultiphysics.FluidDynamicsApplication",
            "Parameters"    : {
                "model_part_name" : "FluidModelPart.AutomaticInlet2D_Inlet",
                "variable_name"   : "VELOCITY",
                "modulus"         : 25.0,
                "direction"       : "automatic_inwards_normal",
                "interval"        : [10.0,"End"]
            }
        },{
            "python_module" : "apply_outlet_process",
            "kratos_module" : "KratosMultiphysics.FluidDynamicsApplication",
            "Parameters"    : {
                "model_part_name"    : "FluidModelPart.Outlet2D_Outlet",
                "variable_name"      : "PRESSURE",
                "constrained"        : true,
                "value"              : 0.0,
                "hydrostatic_outlet" : false,
                "h_top"              : 0.0
            }
        },{
            "python_module" : "apply_noslip_process",
            "kratos_module" : "KratosMultiphysics.FluidDynamicsApplication",
            "Parameters"    : {
                "model_part_name" : "FluidModelPart.NoSlip2D_InterfaceFluid"
            }
        },{
            "python_module" : "apply_slip_process",
            "kratos_module" : "KratosMultiphysics.FluidDynamicsApplication",
            "process_name"  : "ApplySlipProcess",
            "Parameters"    : {
                "model_part_name" : "FluidModelPart.Slip2D"
            }
        }]

Please run the example by using the method described in the previous tutorials. That is by using the runkratos executable. Once the simulation is finished (should take about 4 minutes) we load the result (.bin) file into GiD to view the following result

3. The MainKratos.py Script

This is the script that instantiates the fluid dynamics analysis and calls the Run function of the solver class to solve the CFD problem with applied boundary conditions.

from __future__ import print_function, absolute_import, division #makes KratosMultiphysics backward compatible with python 2.6 and 2.7

import KratosMultiphysics
from KratosMultiphysics.FluidDynamicsApplication.fluid_dynamics_analysis import FluidDynamicsAnalysis

import sys
import time

class CustomFluidDynamicsAnalysis(FluidDynamicsAnalysis):

    def __init__(self,model,project_parameters,flush_frequency=10.0):
        super(CustomFluidDynamicsAnalysis,self).__init__(model,project_parameters)
        self.flush_frequency = flush_frequency
        self.last_flush = time.time()

    def FinalizeSolutionStep(self):
        super(CustomFluidDynamicsAnalysis,self).FinalizeSolutionStep()

        if self.parallel_type == "OpenMP":
            now = time.time()
            if now - self.last_flush > self.flush_frequency:
                sys.stdout.flush()
                self.last_flush = now

if __name__ == "__main__":

    with open("ProjectParameters.json",'r') as parameter_file:
        parameters = KratosMultiphysics.Parameters(parameter_file.read())

    model = KratosMultiphysics.Model()
    simulation = CustomFluidDynamicsAnalysis(model,parameters)
    simulation.Run()

As we see here, a custom fluid dynamics analysis is derived from the actual FluidDynamicsAnalysis to make a CustomFluidDynamicsAnalysis class and uses it for the simulation.

This CustomFluidDynamicsAnalysis can be used to modify the properties of the simulation, even those specified in the JSON file.

4. Accessing the sub modelparts of the boundaries

Project information

Getting Started

Tutorials

Developers

Kratos structure

Conventions

Solvers

Debugging, profiling and testing

HOW TOs

Utilities

Kratos API

Kratos Structural Mechanics API

Clone this wiki locally