【问题标题】:How to modify a set of Ansys Fluent variables with Scheme file i/o?如何使用 Scheme 文件 i/o 修改一组 Ansys Fluent 变量?
【发布时间】:2016-06-29 13:56:53
【问题描述】:

问题

我和我的同事一直在尝试让一些 Scheme 代码工作,这些代码将从 TSV 文件中获取输入并将这些值分配给已在 Ansys 软件中定义的变量。


文件

TSV 文件的组织方式如下:

1   0.7  0.8  0.7  1.0  0.5  [...]
2   0.9  0.9  0.3  0.5  0.5  [...]
3   0.5  0.8  0.3  0.3  0.7  [...]
4   0.6  0.2  1.0  0.5  0.5  [...]
5   0.9  0.0  0.0  0.8  0.3  [...]
6   0.5  0.6  0.3  0.4  1.0  [...]

[ ... ]

1998  0.5  0.0  0.2  0.9  0.0  [...]
1999  0.8  0.2  0.2  0.7  0.2  [...]
2000  0.7  0.2  0.8  0.4  0.5  [...]

第一列是需要将特定输入集分配给软件中的变量的流动时间;其他 96 列代表需要设置的 96 个变量。


我目前的方案代码是这样的:

;Set variables
(define (setFunc)
    (lambda(row-position outflow-number flow-wt-1)
        (let outflow-string ((string-append("def bc outflow " outflow-number " ~a"))) 
            (if (< row-position 180)
                (ti-menu-load-string (format #f (outflow-string) flow-wt-1))
                (ti-menu-load-string (format #f (outflow-string) 0.0))
            )
        )
    )
)

;Read a file to a list of chars
(define (file->char_list)
    (call-with-input-file "doorstates.txt"
        (lambda(input-port)
            (let loop ((x (read-char input-port)))
                (if (not (eof-object? x))
                    (#t (begin (cons x (loop (read-char input-port)))))
                )
            )
        )
    )
)

;Main
(begin
    (let inFile ((apply string (file->char_list)))
        (let inFileList ((string-split (inFile #\newline)))
            (let index ((0))
                ; Row position = flow-time - 1
                (define rowPosition ((- flow-time 1)))
                (let loop ((index))
                    (cond ((>= index (length inFileList)) 
                            '()
                        )
                        ((= (- index (* (/ index 97) 97)) 0)
                            (+ index 1)
                            (loop (index))
                        )
                        ((= (/ index 97) rowPosition)
                            ; Column position = index - (rowPosition * rowLength)
                            (define colPosition ((- index (* rowPosition 97))))
                            (setFunc (rowPosition colPosition list-ref inFileList index))
                            (+ index 1)
                            (loop (index))
                        )
                        (else
                            (+ index 1)
                            (loop (index))
                        )
                    )
                )
            )
        )
    )
)

当上面的代码使用 Ansys 软件运行时,会产生以下错误:

Error: eval: unbound variable

Error Object: string

我读过的所有资料都说,未绑定变量的最可能原因是在不是函数的参数(如文字)周围放置了额外的括号;但是我所看到的所有内容都在 let 参数周围加上了双括号。


要点

在参与此项目之前,我从未使用过 Scheme 或任何其他 LISP 变体,因此如果有任何错误或我遗漏的常见做法,请告诉我。另外,如果上面的方案代码完全是废话,并且没有做我需要做的事情,请告诉我并建议下一步!

【问题讨论】:

    标签: file-io scheme


    【解决方案1】:

    您误解了错误消息。您的 begin 缺少右括号。因此,在以(begin ... 开头的“列表”中会遇到文件结尾。此外,您错误地使用了let。您需要附上使用变量的代码,例如:

    (let ((a 1))
      (+ a 7))
    

    我建议使用理解 s 表达式的编辑器。

    【讨论】:

    • 谢谢迈克尔!我遇到了另一个错误。如果您有时间,请查看上面的修改并权衡一下!
    • 您仍在错误地使用let。特别是,(let inFile ((apply string ... 试图定义一个命名循环,在这个过程中重新定义了apply
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 1970-01-01
    • 2020-04-12
    • 1970-01-01
    • 2010-10-04
    • 2021-10-15
    • 1970-01-01
    相关资源
    最近更新 更多