FLUENT Scheme 脚本调用及其案例

2016-08-19  by:CAE仿真在线  来源:互联网


FLUENT 中使用 Scheme,如果好好利用,能够大大提高工作效率。这是自己编写的第一个 journal 文件,用来设置一些选项。由于经常使用 User-defined Materials Database,在 journal 中添加曾经遭遇错误,后来才发现是 Material 的名称出了问题。

[python] 
  1. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2. ; Author: huys
  3. ; Description: Batch setup for segment one
  4. ; Date: 2008/06/03
  5. ; Revision:
  6. ; 1.1 Add materials from user-defined material database
  7. ; Please make sure material's name is right defiend
  8. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  9. ;Read in the mesh file.
  10. file read-case seg1_m.msh
  11. ;Scale it. mm -> m.
  12. grid/scale
  13. ;x scale factor [1]
  14. 0.001
  15. ;y scale factor [1]
  16. 0.001
  17. ;z scale factor [1]
  18. 0.001
  19. ;Turn on the energy equation
  20. define/models energy?
  21. ;Enable energy model? [no]
  22. yes
  23. ;Compute viscous energy dissipation? [no]
  24. no
  25. ;include pressure work in energy equation? [no]
  26. no
  27. ;include kinetic energy in energy equation? [no]
  28. no
  29. ;Include diffusion at inlets? [yes]
  30. yes
  31. ;Specify turbulence model.
  32. define/models/viscous/ke-standard
  33. ;Enable the standard k-epsilon turbulence model? [no]
  34. yes
  35. ;Select equations to solve.
  36. solve/set/equations flow
  37. ;Solve Flow equation(s)? [yes]
  38. yes
  39. solve/set/equations temperature
  40. ;Solve Energy equation(s)? [yes]
  41. yes
  42. solve/set/equations ke
  43. ;Solve Turbulence equation(s)? [yes]
  44. yes
  45. ;Select material database
  46. define/materials/data-base/database-type
  47. ;Available Types: (fluent-database user-defined)
  48. ;Database Type [fluent-database]
  49. user-defined
  50. ;Database File [""]
  51. custom.scm
  52. ;Copy your materials
  53. define/materials/copy
  54. ;Material of type []>
  55. fluid
  56. ;database-material-name>
  57. water_var
  58. ;
  59. define/materials/copy
  60. ;Material of type []>
  61. solid
  62. ;database-material-name>
  63. stainless_cnt
  64. ;Define boundary conditions
  65. ; Wall
  66. define/boundary-conditions/wall
  67. ; ...
  68. inner_wall_b
  69. ;Wall Thickness (m) [0]
  70. 0
  71. ;Use Profile for Heat Generation Rate? [no]
  72. no
  73. ;Heat Generation Rate (w/m3) [0]
  74. 0
  75. ;material-name [aluminum]: Change current value? [no]
  76. no
  77. ;Thermal BC Type [heat-flux]: Change current value? [no]
  78. yes
  79. ;Thermal BC Type [heat-flux]
  80. ;; convection
  81. ;; coupled
  82. ;; heat-flux
  83. ;; mixed
  84. ;; network
  85. ;; radiation
  86. ;; temperature
  87. temperature
  88. ;Use Profile for Temperature? [no]
  89. no
  90. ;Temperature (k) [300]
  91. 1000
  92. ;Enable shell conduction? [no]
  93. no
  94. ; Inlet
  95. define/boundary-conditions/velocity-inlet
  96. ;
  97. inlet_channel_b
  98. ;Velocity Specification Method: Magnitude and Direction [no]
  99. no
  100. ;Velocity Specification Method: Components [no]
  101. no
  102. ;Velocity Specification Method: Magnitude, Normal to Boundary [yes]
  103. yes
  104. ;Reference Frame: Absolute [yes]
  105. yes
  106. ;Use Profile for Velocity Magnitude? [no]
  107. no
  108. ;Velocity Magnitude (m/s) [0]
  109. 10
  110. ;Coordinate System: Cartesian (X, Y, Z) [yes]
  111. yes
  112. ;Use Profile for Temperature? [no]
  113. no
  114. ;Temperature (k) [300]
  115. 300
  116. ;Turbulent Specification Method: K and Epsilon [yes]
  117. no
  118. ;Turbulent Specification Method: Intensity and Length Scale [no]
  119. no
  120. ;Turbulent Specification Method: Intensity and Viscosity Ratio [no]
  121. yes
  122. ;Turbulent Intensity (%) [10]
  123. 10
  124. ;Turbulent Viscosity Ratio [10]
  125. 10
  126. ; channel
  127. define/boundary-conditions/fluid
  128. ;(channel)
  129. ;zone id/name [channel]
  130. channel
  131. ;material-name [air]: Change current value? [no]
  132. yes
  133. ;material-name [air]>
  134. water_var
  135. ;Specify source terms? [no]
  136. no
  137. ;Specify fixed values? [no]
  138. no
  139. ;Motion Type: Stationary [yes]
  140. yes
  141. ;X-Origin of Rotation-Axis (m) [0]
  142. 0
  143. ;Y-Origin of Rotation-Axis (m) [0]
  144. 0
  145. ;Z-Origin of Rotation-Axis (m) [0]
  146. 0
  147. ;X-Component of Rotation-Axis [0]
  148. 0
  149. ;Y-Component of Rotation-Axis [0]
  150. 0
  151. ;Z-Component of Rotation-Axis [1]
  152. 1
  153. ;Deactivated Thread [no]
  154. no
  155. ;Laminar zone? [no]
  156. no
  157. ;Porous zone? [no]
  158. no
  159. ; soild(wall)
  160. define/boundary-conditions/solid
  161. ;(wall)
  162. ;zone id/name [wall]
  163. wall
  164. ;material-name [aluminum]: Change current value? [no]
  165. yes
  166. ;material-name [aluminum]>
  167. stainless_cnt
  168. ;Specify source terms? [no]
  169. no
  170. ;Specify fixed values? [no]
  171. no
  172. ;Motion Type: Stationary [yes]
  173. yes
  174. ;X-Origin of Rotation-Axis (m) [0]
  175. 0
  176. ;Y-Origin of Rotation-Axis (m) [0]
  177. 0
  178. ;Z-Origin of Rotation-Axis (m) [0]
  179. 0
  180. ;X-Component of Rotation-Axis [0]
  181. 0
  182. ;Y-Component of Rotation-Axis [0]
  183. 0
  184. ;Z-Component of Rotation-Axis [1]
  185. 1
  186. ;Deactivated Thread [no]
  187. no
  188. ;Set Pressure Velocity Coupling Scheme to SIMPLEC
  189. solve/set/p-v-coupling
  190. ;Pressure Velocity Coupling Scheme [20] ?
  191. ;; 20 for SIMPLE
  192. ;; 21 for SIMPLEC
  193. ;; 22 for PISO
  194. ;; 24 for Coupled
  195. 21
  196. ;Set the discretization schemes./
  197. solve/set/discretization-scheme pressure
  198. ;Convective discretization scheme for Pressure [10]
  199. ;; 10 for Standard
  200. ;; 14 for PRESTO!
  201. ;; 11 for Linear
  202. ;; 12 for Second Order
  203. ;; 13 for Body Force Weighted
  204. 10
  205. solve/set/discretization-scheme mom
  206. ;Convective discretization scheme for Momentum [0]
  207. ;; 0 for First Order Upwind
  208. ;; 1 for Second Order Upwind
  209. ;; 2 for Power Law
  210. ;; 4 for QUICK
  211. ;; 6 for Third-Order MUSCL
  212. 1
  213. solve/set/discretization-scheme temperature
  214. ;Convective discretization scheme for Energy [0]
  215. ;; 0 for First Order Upwind
  216. ;; 1 for Second Order Upwind
  217. ;; 2 for Power Law
  218. ;; 4 for QUICK
  219. ;; 6 for Third-Order MUSCL
  220. 1
  221. solve/set/discretization-scheme k
  222. ;Convective discretization scheme for Turbulence Kinetic Energy [0]
  223. ;; 0 for First Order Upwind
  224. ;; 1 for Second Order Upwind
  225. ;; 2 for Power Law
  226. ;; 4 for QUICK
  227. ;; 6 for Third-Order MUSCL
  228. 1
  229. solve/set/discretization-scheme epsilon
  230. ;Convective discretization scheme for Turbulence Dissipation Rate [0]
  231. ;; 0 for First Order Upwind
  232. ;; 1 for Second Order Upwind
  233. ;; 2 for Power Law
  234. ;; 4 for QUICK
  235. ;; 6 for Third-Order MUSCL
  236. 1
  237. ;Set underrelaxation factors.
  238. solve/set/under-relaxation pressure
  239. ;Underrelaxation factor for Pressure [0.3]
  240. 0.9
  241. solve/set/under-relaxation mom
  242. ;Underrelaxation factor for Momentum [0.7]
  243. 0.9
  244. solve/set/under-relaxation temperature
  245. ;Underrelaxation factor for Energy [1]
  246. 0.9
  247. solve/set/under-relaxation k
  248. ;Underrelaxation factor for Turbulence Kinetic Energy [0.8]
  249. 0.9
  250. solve/set/under-relaxation epsilon
  251. ;Underrelaxation factor for Turbulence Dissipation Rate [0.8]
  252. 0.9
  253. solve/set/under-relaxation turb-viscosity
  254. ;Underrelaxation factor for Viscosity [1]
  255. 0.9
  256. solve/set/under-relaxation density
  257. ;Underrelaxation factor for Density [1]
  258. 0.9
  259. solve/set/under-relaxation body-force
  260. ;Underrelaxation factor for Body Forces [1]
  261. 0.9
  262. ;Set multigrid.
  263. solve/set/multi-grid-controls pressure
  264. ;Multigrid cycle type for Pressure [1]
  265. 1
  266. ;Termination criterion [0.1]
  267. 1e-30
  268. ;Multigrid method for Pressure [0]
  269. 0
  270. ;Multigrid stabilization for Pressure [0]
  271. 0
  272. solve/set/multi-grid-controls x-mom
  273. ;Multigrid cycle type for X-Momentum [0]
  274. 0
  275. ;Termination criterion [0.1]
  276. 1e-30
  277. ;Residual reduction tolerance for [0.7]
  278. 0.999
  279. ;Multigrid method for X-Momentum [0]
  280. 0
  281. solve/set/multi-grid-controls y-mom
  282. ;Multigrid cycle type for X-Momentum [0]
  283. 0
  284. ;Termination criterion [0.1]
  285. 1e-30
  286. ;Residual reduction tolerance for [0.7]
  287. 0.999
  288. ;Multigrid method for X-Momentum [0]
  289. 0
  290. solve/set/multi-grid-controls z-mom
  291. ;Multigrid cycle type for X-Momentum [0]
  292. 0
  293. ;Termination criterion [0.1]
  294. 1e-30
  295. ;Residual reduction tolerance for [0.7]
  296. 0.999
  297. ;Multigrid method for X-Momentum [0]
  298. 0
  299. solve/set/multi-grid-controls k
  300. ;Multigrid cycle type for Turbulent Kinetic Energy [0]
  301. 0
  302. ;Termination criterion [0.1]
  303. 1e-30
  304. ;Residual reduction tolerance for [0.7]
  305. 0.999
  306. ;Multigrid method for Turbulent Kinetic Energy [0]
  307. 0
  308. solve/set/multi-grid-controls epsilon
  309. ;Multigrid cycle type for Turbulent Dissipation Rate [0]
  310. 0
  311. ;Termination criterion [0.1]
  312. 1e-30
  313. ;Residual reduction tolerance for [0.7]
  314. 0.999
  315. ;Multigrid method for Turbulent Dissipation Rate [0]
  316. 0
  317. solve/set/multi-grid-controls temperature
  318. ;Multigrid cycle type for Energy [0]
  319. 0
  320. ;Termination criterion [0.1]
  321. 1e-30
  322. ;Residual reduction tolerance for [0.7]
  323. 0.999
  324. ;Multigrid method for Energy [0]
  325. 0
  326. ;Set the convergence criteria.
  327. solve/monitors/residual convergence-criteria
  328. ;continuity residual convergence criterion [0.001]
  329. 1e-16
  330. ;x-velocity residual convergence criterion [0.001]
  331. 1e-16
  332. ;y-velocity residual convergence criterion [0.001]
  333. 1e-16
  334. ;z-velocity residual convergence criterion [0.001]
  335. 1e-16
  336. ;energy residual convergence criterion [1e-06]
  337. 1e-16
  338. ;k residual convergence criterion [0.001]
  339. 1e-16
  340. ;epsilon residual convergence criterion [0.001]
  341. 1e-16
  342. ;Write settings to a case file.
  343. file/write-case
  344. ;case file name [""]
  345. "seg1_xx.cas"


现在的计算平台有很多是 Linux Cluster,常用的作业管理系统采用 PBS。如果要采用 FLUENT 进行计算就需要解决在 Text Mode 下调用 FLUENT 进行求解的问题,而且多数时候

需要执行并行版本的 FLUENT。


Linux 调用 FLUENT 的文本界面其实很简单,比如

  1. fluent 3d -g

出现了 FLUENT 的 console,这样就能输入命令并执行了。


不想手工输入命令的话,写一个 journal 或 scheme 脚本文件,让 FLUENT 启动后自动执行。

  1. fluent 3d -g -i script.scm

最好加上 I/O 重定向。

 

fluent 3d -g -i script.scm > out.log


远程连接时后台执行就比较重要了。

 

fluent 3d -g -i script.scm > out.log &


以上是之前在 PC 上常用的模式,还会利用 screen 之类的进行监视。


那么并行版本又如何呢?


本地并行的话,基本上就是


fluent 3d -t8 -g -i script.scm > out.log &


利用多个节点的话,需要做些处理


fluent 3d -pethernet -cnf=myhosts -t8 -ssh -g -i script.scm > out.log &


多了几个选项,其中


-pethernet 表示各节点利用局域网连接进行通信,发现用 default 选项的话会报错的,至少 fluent12 是这个样子。


-cnf=myhosts 表示运行的节点信息


-ssh 表示各节点采用 ssh 进行登录,默认的话采用 rsh,如果没有启用 rsh 服务的话就崩溃了。


到这里基本可以了。


如果要在 PBS 中进行调用,启动命令可以不加 i/O 重定向和后台运行的符号



fluent 3d -pethernet -cnf=myhosts -t8 -ssh -g -i script.scm



 


1、添加菜单项 (Text Mode)

(define ti-flab-iterate
(lambda ()
(display "Hello World!")
)
)
(define flowlab-menu
(make-menu
"flowlab"
("iterate" #t ti-flab-iterate "Iterate.")
)
)
(define add-menu
(lambda (name menu test value help)
(ti-menu-insert-item!
menu
(make-menu-item name test value help)
)
#t
)
)
(add-menu "flowlab/" main-menu #t flowlab-menu "")



addmenu


2 输出当前的迭代次数和参数

(define (export-niters fname parameter)

(let

(

(niter (%iterate 0))

(out-port (open-file fname "w"))

)

(if (not out-port)

(cx-error-dialog "unable to open output file for exporting niters")

)

(format out-port "$~a = ~a~%" parameter niter)

(flush-output-port out-port)

)

)


3 字符串处理

;takes any string as argument 'str', breaks it on the basis of blank-space or
;newline character into multiple strings. These multiple strings are returned
;as a list. 
(define (tokenizer str)
(let
(
(l (string->list str))
(result '())
(delimitters (list #/space #/newline #/) #/())
(started? #f)
(temp '())
)
(for-each
(lambda (c)
(if started?
(if (not (memv c delimitters))
(set! temp (append temp (list c)))
(begin
(set! result (append result (list (list->string temp))))
(set! temp '())
(set! started? #f)
)
)
(if (not (memv c delimitters))
(begin
(set! temp (append temp (list c)))
(set! started? #t)
)
)
)
)
l
)
(if started?
(set! result
(append result (list (list->string temp)))
)
)
result
)
)


4 自动保存,记录输出信息的迭代脚本

;;
;; iterate.scm
;;
(define casename "coal_inj_00")
(define iternum 500)
;
(define hy-start-transcript
(lambda (filename)
(if (file-exists? filename)
(remove-file filename)
#t
)
(ti-start-transcript filename #t)
#t
)
)
;;
(define (hy-stop-transcript)
(if transcript-open?
(stop-transcript #t)
#t
)
#t
)
;;
(define (log-exec-proc exec-proc trn-file)
(begin
(hy-start-transcript trn-file)
(exec-proc)
(newline)
(hy-stop-transcript)
)
)
;;
(define my-iterate
(lambda (num)
(if (number? num)
(begin
(read-case-data casename)
(iterate num)
(write-case-data (format #f "~a-i~a" casename num))
#t
)
#f
)
)
)
;;
(log-exec-proc (lambda () (my-iterate iternum)) (string-append casename ".trn"))

5 读取 report 文件中的数值


;;

;;

;;
(define (hy-read-rp-file filename)
(let ((p (open-input-file (format #f "~a" filename))))
(do
((x (read p) (read p)))
(
(or (number? x) (eof-object? x))
(close-input-port p)
(if (number? x) x #f)
)
)
)
)


6 读取数据到 list 中

(define hy-read-data-to-list
(lambda (fname)
(let ((p (open-input-file (format #f "~a" fname))))
(let f ((x (read p)) (l '()))
(if (or (eof? p) (eof-object? x))
(begin
(close-input-port p)
l
)
(begin
(display x)
(newline)
(if (number? x)
(set! l (append l (list x)))
)
(f (read p) l)
)
)
)
)
)
)


7 导出 bc 信息

(define (hy-export-bc-names)
(for-each
(lambda (name)
(display
(format #f "{~a, /"~a/", /"~a/"},/n"
(zone-name->id name)
name
(zone-type (get-zone name))
)
)
)
(inquire-zone-names)
)
)


8 显示 list 内容

;;;
(define hy-display-list
(lambda (l)
(do ((i 0 (+ i 1)))
((>= i (length l)))
(display (format #f "~a --- ~a/n" i (list-ref l i)))
)
)
)


9 非稳态计算时自动保存脚本

;; Scheme file to autosave files at a particular time interval,

 

;;original: FLUENT Online Technical Support "Solution 656 :Autosave at specified time intervals "


;; Usage:

;; To use the scheme file to save files at a given time interval,

;; 0. Read case and data files, then read autosave.scm through File > Read > Scheme

;; 1. Open the Execute Commands panel in the GUI (Solve>Execute Commands)

;; 2. Increase the number of Defined Commands by

;; 3. Click ON next to the most recent command and select Time Step

;; next to the drop down arrow under When

;; 4. Enter the following text in the field under Command:

;; (autowrite "wcd" "autoname-" 0.2)


;; 5. Enter the compression status below. Change only the text between the quotation marks.

;; For compressed files use ".gz"

;; For uncompressed files use ""

(define g_zip "")

;;

;; The value of 0.2 should be replaced with the desired interval for the current case

;; The value of "wcd" should be replaced with

;; "wd " for data files only

;; "wcd " for case and data files

;; The value of "autoname-" should be replaced with the actual pathname+filename :

;; if it is written in current directory,pathname can be omitted.

;;default setting

(define g_filename "auto-")

(define g_wcom "wd")


(define (writefiles current-time)

(let* ((time-string) (writecommand))

(rpsetvar 'autowrite/last-file-time current-time)

(set! writecommand (string-append g_wcom " " g_filename g_zip))

(ti-menu-load-string writecommand)

)

)


(define (autowrite wcom filename interval)

(let* ( (current-time) (overlap) (last-write) (file-write-time) )

(if (not (rp-var-object 'autowrite/last-file-time))

(rp-var-define 'autowrite/last-file-time (rpgetvar 'flow-time) 'real #f)

)

(set! current-time (rpgetvar 'flow-time))

(set! last-write (rpgetvar 'autowrite/last-file-time))

(set! overlap (- last-write (* interval (truncate (/ last-write interval)))))

(set! file-write-time (+ interval (- last-write overlap)))

(if (> current-time file-write-time)

(begin

(cond

((truncate (/ last-write interval)<? 10)

(set! g_filename (string-append filename "000" (number->string (inexact->exact (truncate(/ last-write interval))))))

)

((truncate (/ last-write interval)<? 100)

(set! g_filename (string-append filename "00" (number->string (inexact->exact (truncate(/ last-write interval))))))

)

((truncate (/ last-write interval)<? 1000)

(set! g_filename (string-append filename "0" (number->string (inexact->exact (truncate(/ last-write interval))))))

)

(else

(set! g_filename (string-append filename (number->string (inexact->exact (truncate(/ last-write interval))))))

)

)

(set! g_wcom wcom)

(writefiles current-time)

)

)

)

)


开放分享:优质有限元技术文章,助你自学成才

相关标签搜索:FLUENT Scheme 脚本调用及其案例 FLUENT Scheme 脚本 fluent Fluent培训 Fluent流体培训 Fluent软件培训 fluent技术教程 fluent在线视频教程 fluent资料下载 fluent分析理论 fluent化学反应 fluent软件下载 UDF编程代做 

编辑
在线报名:
  • 客服在线请直接联系我们的客服,您也可以通过下面的方式进行在线报名,我们会及时给您回复电话,谢谢!
验证码

全国服务热线

1358-032-9919

广州公司:
广州市环市中路306号金鹰大厦3800
电话:13580329919
          135-8032-9919
培训QQ咨询:点击咨询 点击咨询
项目QQ咨询:点击咨询
email:kf@1cae.com