Fluent Scheme 脚本收集

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 "")


 

 

Fluent Scheme 脚本收集_第1张图片

 

 

 

 

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, 

;;(modify by LiuGuoqing ,[[email protected]][email protected][/email])

;;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)

      )

    )

  )

)


转载自:http://blog.csdn.net/fox000002/article/details/4510730


你可能感兴趣的:(list,Scheme,脚本,lambda,compression,newline)