CFD Online Discussion Forums

CFD Online Discussion Forums (https://www.cfd-online.com/Forums/)
-   Pointwise & Gridgen (https://www.cfd-online.com/Forums/pointwise/)
-   -   Another scripting Problem (https://www.cfd-online.com/Forums/pointwise/137666-another-scripting-problem.html)

v_herw June 20, 2014 07:22

Another scripting Problem
 
Hi,

i need a bit of help. I scripted a mesh and now i wanted to divide the script into procedures to have a nicer overview over the script. the problem now is that i need in several procedures different Cons/Doms. I wanted to create a procedure where i define all of these parameters to get them later in the procedures.

My script:
Code:

# PROCEDURE TO SET UP ALL PARAMETER
# -----------------------------------------------
proc PARAMETER {} {
       
# SET THE CONS/DOMS OF THE LAST SECTION
        global CN(1)
        set CN(1) [pw::GridEntity getByName "TopUpperCon"]
        global CN(2)
        set CN(2) [pw::GridEntity getByName "TopLowerCon"]
        global CN(3)
       
        set CN(3) [pw::GridEntity getByName "TopTeCon"]       
        global DM(2)
        set DM(2) [pw::GridEntity getByName "TopBlLowerDom"]
        global DM(3)
        set DM(3) [pw::GridEntity getByName "TopBlUpperDom"]
        global DM(4)
        set DM(4) [pw::GridEntity getByName "BottomBlUpperDom"]
        global DM(5)
        set DM(5) [pw::GridEntity getByName "UpperBlDom"]

}

But this won't work - i get the following fault::confused:

Code:

Script: ----- TCL TRACE -----
Script: bad variable name "CN(1)": upvar won't create a scalar variable that looks like an array element
Script:    while executing
Script: "global CN(1)"
Script:    (procedure "PARAMETER" line 22)
Script:    invoked from within
Script: "PARAMETER"
Script:    (file "C:\xxxxxxxxxxxx.glf" line 161)

is there any opportunity to make the parameters "global"??

Thank you very much for your help :)

jchawner June 22, 2014 14:18

Variable scope is a feature of Tcl, not Glyph. Therefore, you can find many online resources such as this one: https://www.tcl.tk/man/tcl8.5/tutorial/Tcl13.html

This page includes the ability to make variables global in scope. However, in general, global variables are bad practice. I would recommend passing variables to a proc via its parameter list instead.

I hope this helps.

dgarlisch June 23, 2014 11:49

To answer your question directly, the error is caused by using the array subscript in the global statement.

I did not test this, but your proc should look like:

Code:

proc PARAMETER {} {
  global CN
  set CN(1) [pw::GridEntity getByName "TopUpperCon"]
  set CN(2) [pw::GridEntity getByName "TopLowerCon"]
  set CN(3) [pw::GridEntity getByName "TopTeCon"]       
  set DM(2) [pw::GridEntity getByName "TopBlLowerDom"]
  set DM(3) [pw::GridEntity getByName "TopBlUpperDom"]
  set DM(4) [pw::GridEntity getByName "BottomBlUpperDom"]
  set DM(5) [pw::GridEntity getByName "UpperBlDom"]
}

HOWEVER, I agree wholeheartedly with John.

Using global variables is a bad* programming practice in general. However, for "quick and dirty" scripts that will be used once and then thrown away, globals may be the easiest way to get things done quickly.


* There are always exceptions to the rules! (bad != NEVER)


All times are GMT -4. The time now is 11:00.