Workbench利用Python驅(qū)動MAPDL執(zhí)行APDL命令

2017-09-15  by:CAE仿真在線  來源:互聯(lián)網(wǎng)

Workbench建模所能用的腳本js命令不夠豐富,因而使通過js建立比較完善的模型的可能性幾乎為0,同時Workbench的Mesh模塊采用的也是js腳本,同樣不夠豐富,無法比較好的自動完成模型的網(wǎng)格劃分。


基于以上原因,想要通過js實現(xiàn)模型的參數(shù)化創(chuàng)建與網(wǎng)格劃分的可能性非常小,難度相當(dāng)大。然而,ANSYS經(jīng)典的APDL命令卻非常的完備,幾乎能夠完成任何操作。故,可在Workbench中結(jié)合MAPDL模塊來完成相關(guān)工作,實現(xiàn)整個過程的參數(shù)化,提高自動化程度。

該篇文章是前面一篇《Workbench利用Python驅(qū)動DM執(zhí)行Js進(jìn)行建?!返逆⒚闷?。主要闡述兩個問題,一是如何在workbench中驅(qū)動APDL命令,二是如何實現(xiàn)Python腳本與APDL命令的數(shù)據(jù)傳遞與交換。

首先,如何在workbench中驅(qū)動APDL命令

# encoding: utf-8

# Release 16.0

SetScriptVersion(Version="16.0.361")

#在workbench主界面創(chuàng)建DM模塊

template1 = GetTemplate(TemplateName="Geometry")

system1 = template1.CreateSystem()

#在workbench主界面創(chuàng)建MAPDL模塊,并與DM模塊建立聯(lián)系

template2 = GetTemplate(TemplateName="Mechanical APDL")

system2 = template2.CreateSystem(

Position="Right",

RelativeTo=system1)

geometryComponent1 = system1.GetComponent(Name="Geometry")

setupComponent1 = system2.GetComponent(Name="Setup")

geometryComponent1.TransferData(TargetComponent=setupComponent1)

創(chuàng)建的結(jié)果如下。


Workbench利用Python驅(qū)動MAPDL執(zhí)行APDL命令ansys結(jié)構(gòu)分析圖片1

#啟動DM模塊


system1 = GetSystem(Name="Geom")

geometry1 = system1.GetContainer(ComponentName="Geometry")

geometry1.Edit()


#DM模塊執(zhí)行Jscript腳本命令,創(chuàng)建幾何模型

geometry1.SendCommand( Command = """

var ps1 = new Object();

//Plane

ps1.Plane = agb.GetActivePlane();

ps1.Origin = ps1.Plane.GetOrigin();

ps1.XAxis = ps1.Plane.GetXAxis();

ps1.YAxis = ps1.Plane.GetYAxis();


//Sketch

ps1.Sk1 = ps1.Plane.NewSketch();

ps1.Sk1.Name = "Sketch1";

//Edges

with (ps1.Sk1)

{

ps1.Ln7 = Line(0.50000000, 0.00000000, 0.52000000, 0.00000000);

ps1.Ln8 = Line(0.52000000, 0.00000000, 0.52000000, -1.00000000);

ps1.Ln9 = Line(0.52000000, -1.00000000, 0.50000000, -1.00000000);

ps1.Ln10 = Line(0.50000000, 0.00000000, 0.50000000, -1.00000000);

}

//Sketch

ps1.Sk2 = ps1.Plane.NewSketch();

ps1.Sk2.Name = "Sketch2";

//Edges

with (ps1.Sk2)

{

ps1.Ln11 = Line(0.10000000, -0.60000000, 1.00000000, -0.60000000);

ps1.Ln12 = Line(1.00000000, -0.60000000, 1.00000000, -0.62000000);

ps1.Ln13 = Line(1.00000000, -0.62000000, 0.10000000, -0.62000000);

ps1.Ln14 = Line(0.10000000, -0.62000000, 0.10000000, -0.60000000);

ps1.Ln15 = Line(0.00000000, -0.50000000, 1.36996958, -0.50000000);

}

var rev1=agb.Revolve(agc.Add,ps1.Sk1,ps1.YAxis,agc.DirReversed,180.0, 0.0, agc.No, 0.0, 0.0)

agb.Regen();

""")

上段py代碼通過SendCommand函數(shù)驅(qū)動DM執(zhí)行js腳本創(chuàng)建幾何模型,此處模型較為簡單,如下所示。

Workbench利用Python驅(qū)動MAPDL執(zhí)行APDL命令ansys結(jié)構(gòu)分析圖片2

##更新APDL模塊,打開MADPL

system1 = GetSystem(Name="APDL")

setupComponent1 = system1.GetComponent(Name="Setup")

setupComponent1.Update(AllDependencies=True)

setupComponent1.Refresh()

setup1 = system1.GetContainer(ComponentName="Setup")

setup1.Edit(

Interactive=True,

LoadInputFiles=True)

下面便是關(guān)鍵的部分,通過上面的幾句py命令,打開了MAPDL模塊。

setup1.SendCommand( Command = """

/prep7

et,1,185

vplot

vsweep,all

eplot


CDOPT,IGES

CDWRITE,ALL,'file','cdb',,'file','iges'

save

! /exit

""")

##關(guān)閉APDL窗口

setup1.Exit()


##更新APDL模塊

system1 = GetSystem(Name="APDL")

setupComponent1 = system1.GetComponent(Name="Setup")

setupComponent1.Update(AllDependencies=True)

可以看到,同樣是通過SendCommand命令,將APDL命令傳遞給MAPDL模塊。幾句APDL命令完成的是單元定義、網(wǎng)格劃分、結(jié)果保存。

/prep7

et,1,185

vplot

vsweep,all

eplot

CDOPT,IGES

CDWRITE,ALL,'file','cdb',,'file','iges'

Save

完成后在MAPDL界面可以看到如下結(jié)果。

Workbench利用Python驅(qū)動MAPDL執(zhí)行APDL命令ansys結(jié)構(gòu)分析圖片3

其次,如何實現(xiàn)Python與APDL的數(shù)據(jù)傳遞

實現(xiàn)Python和APDL數(shù)據(jù)傳遞的目的是為了更好的實現(xiàn)參數(shù)化過程,因為APDL命令在主腳本里全部都是字符串的形式,與Python本身其實是無法直接進(jìn)行數(shù)據(jù)傳遞的。

但是,卻可以通過替換字符串的形式來進(jìn)行數(shù)據(jù)傳遞。

同時,與DM模塊的js腳本的傳遞不一樣,js腳本是屬于面向?qū)ο?而APDL命令卻是面向過程的,在利用SendCommand的時候,可以逐句完成。

下面是一個例子。

# encoding: utf-8

# Release 16.0

SetScriptVersion(Version="16.0.361")

template1 = GetTemplate(TemplateName="Mechanical APDL")

system1 = template1.CreateSystem()

setupComponent1 = system1.GetComponent(Name="Setup")

setupComponent1.Refresh()

setup1 = system1.GetContainer(ComponentName="Setup")

setup1.Edit(

Interactive=True,

LoadInputFiles=True)


Para_A = 1.0

Para_B = 2.0

Para_C = 3.0

cmd_A="A=%f" % Para_A

cmd_B="B=%f" % Para_B

cmd_C="C=%f" % Para_C


setup1.SendCommand(Command=cmd_A)

setup1.SendCommand(Command=cmd_B)

setup1.SendCommand(Command=cmd_C)


setup1.SendCommand(Command="""

/prep7

block,,A,,B,,C

""")

例子中,有A、B、C三個數(shù)據(jù),這三個變量的值在Python里面定義,但是想要傳導(dǎo)到MAPDL中,需要間接定義三個APDL語句,cmd_A、cmd_B和cmd_C。

原始的APDL命令應(yīng)該是:

cmd_A=A

cmd_B=B

cmd_C=C

利用SendCommand傳遞時,通過cmd_A="A=%f" % Para_A,將參數(shù)A的值替換到語句A=1.0中,于是得到一句APDL命令cmd_A=1.0。

注意到上面代碼多次用了SendCommand命令。這也是Python能夠?qū)崿F(xiàn)APDL的數(shù)據(jù)傳遞的重要原因,得益于APDL的命令能夠逐條導(dǎo)入。


姊妹篇:

《Workbench利用Python驅(qū)動DM執(zhí)行Js進(jìn)行參數(shù)化建?!?


開放分享:優(yōu)質(zhì)有限元技術(shù)文章,助你自學(xué)成才

相關(guān)標(biāo)簽搜索:Workbench利用Python驅(qū)動MAPDL執(zhí)行APDL命令 Ansys有限元培訓(xùn) Ansys workbench培訓(xùn) ansys視頻教程 ansys workbench教程 ansys APDL經(jīng)典教程 ansys資料下載 ansys技術(shù)咨詢 ansys基礎(chǔ)知識 ansys代做 Fluent、CFX流體分析 HFSS電磁分析 Abaqus培訓(xùn) 

編輯
在線報名:
  • 客服在線請直接聯(lián)系我們的客服,您也可以通過下面的方式進(jìn)行在線報名,我們會及時給您回復(fù)電話,謝謝!
驗證碼

全國服務(wù)熱線

1358-032-9919

廣州公司:
廣州市環(huán)市中路306號金鷹大廈3800
電話:13580329919
          135-8032-9919
培訓(xùn)QQ咨詢:點擊咨詢 點擊咨詢
項目QQ咨詢:點擊咨詢
email:kf@1cae.com