Defining Functions for FluentDemo1

The IronPython script main.py follows. This script defines all functions executed by the callbacks in the extension's XMLfile:

  • The function ImportMesh is invoked by the callback <onrefresh> in the step Analysis.

  • The function CreateAnalysis is invoked by the callback <onupdate> in the step Analysis.

  • The function CreateReport is invoked by the callback <onrefresh> in the step Results.

def ImportMesh(step):
    tui = ExtAPI.Application.ScriptByName("TUI")
    
    tui.SendCommand("""switch-to-meshing-mode""")
    tui.SendCommand("""/file/read-mesh "{0}" """.format(System.IO.Path.Combine(
ExtAPI.Extension.InstallDir,"final.msh")))
    tui.SendCommand("""switch-to-solution-mode yes yes""")
    tui.SendCommand("""/display/mesh ok""")
    tui.SendCommand("""/display/views/restore-view right""")
    
def CreateAnalysis(step):
    tui = ExtAPI.Application.ScriptByName("TUI")

    res = System.IO.Path.GetTempFileName()
    img = System.IO.Path.GetTempFileName()+".jpg"
    curve = System.IO.Path.GetTempFileName()
    tui.SendCommand("""/define/boundary-conditions/velocity-inlet inlet no no yes yes no {0} no 0""".format(step.Properties["velocity"].Value), True)
    tui.SendCommand("""/solve/initialize/hyb-initialization""", True)
    tui.SendCommand("""/solve/iterate 50""", True)
    tui.SendCommand("""/display/mesh ok""", True)
    tui.SendCommand("""/display/views/restore-view right""", True)
    tui.SendCommand("""/display/set/contours/filled-contours? yes""", True)
    tui.SendCommand("""/display/set/contours/surfaces wall inlet outlet ()""", True)
    tui.SendCommand("""/display/contour pressure 0 2""", True)
    tui.SendCommand("""/display/save-picture "{0}" """.format(img), True)
    tui.SendCommand("""/surface/rake-surface rake-5 0 0 0 0 0 1 50""", True)
    tui.SendCommand("""/plot/plot-direction 0 0 1""", True)
    tui.SendCommand("""/plot/plot yes "{0}" yes yes no no pressure yes 0 0 1 rake-5 ()""".format(curve), True)
    tui.SendCommand("""/report/surface-integrals/area-weighted-avg inlet () pressure yes "{0}" """.format(res), True)
    
    val = 0.
    n = 0;
    with System.IO.StreamReader(res) as reader:
        while n!=5:
            line = reader.ReadLine()
            if line!=None:
                n+=1
        str = line.Substring(32)
        val = System.Double.Parse(str, System.Globalization.CultureInfo.InvariantCulture)
        
    x=[]
    y=[]
    n = 0;
    with System.IO.StreamReader(curve) as reader:
        while n!=5:
            line = reader.ReadLine()
            if line!=None:
                n+=1
        while not line.StartsWith(")"):
            tab = line.Split("\t")
            x.Add(System.Double.Parse(tab[0], System.Globalization.CultureInfo.InvariantCulture))
            y.Add(System.Double.Parse(tab[1], System.Globalization.CultureInfo.InvariantCulture))
            line = reader.ReadLine()
    System.IO.File.Delete(res)
    System.IO.File.Delete(curve)
    
    step.NextStep.Properties["pressure"].Value = val
    step.NextStep.Attributes["img"] = img
    step.NextStep.Attributes["x"] = x
    step.NextStep.Attributes["y"] = y
    
def CreateReport(step):
    graph = step.UserInterface.GetComponent("Chart")
    graph.Title("Static Pressure")
    graph.ShowLegend(False)
    graph.Plot(step.Attributes["x"],  step.Attributes["y"], key="Static Pressure", color='g')

    help = step.UserInterface.GetComponent("Report")
    help.SetHtmlContent(ExtAPI.Extension.InstallDir,"""<center><img style="height:250px" src="{0}"></center>""".format(step.Attributes["img"]))