s using the setstate command and running and continuing the simulation using run and cont-this is normally done interactively with the interpreter. The
source command reads interpreter commands from the named file, until the end of the file or a syntax error occurs. The "#" character indicates that the rest of the line is a comment. By convention, files meant to be read by the load command end in ".pt". Example:
source "testfile.pt"The tilde notation for users' home directories is allowed; for example, if your installation of Ptolemy was made by creating a user
ptolemy (see
"Setup" on page 2-1), try
source "$PTOLEMY/demo/ptcl/sdf/basic/butterfly.pt"
It is also possible to specify a file to be loaded by the interpreter on the command line. If, when you start the interpreter you typeptcl myCommands.pt
the interpreter will load the named file, execute its commands, and then quit. No command prompt will appear. The source command is actually built into Tcl itself, but it is described here nevertheless, for convenience.3.9.2 Changing the seed of random number generation
The seed command changes the seed of the random number generation. The default value is 1. The syntax isseed n
where n is an unsigned integer.3.9.3 Changing the current directory
The cd command changes the current directory. For example,cd "$PTOLEMY/demo/ptcl/sdf/basic"
source "butterfly.pt"
will load the same file as the example in the previous section. Again, we have assumed that your installation contains a user ptolemy (see
"Setup" on page 2-1). To see what the interpreter's current directory is, you can type
pwd
3.9.4 Dynamically linking new stars
The interpreter has the ability to extend itself by linking in outside object files; the object files in question must define single stars (they will have the right format if they are produced from preprocessor input). Unlike pigi, the graphical interface, the interpreter will not automatically run the preprocessor and compiler; it expects to be given object files that have already been compiled. The syntax islink object-file-name
Any star object files that are linked in this way must only call routines that are already statically or permanently linked into the interpreter. For that reason, it is possible that a star that can be linked into pigi might not be linkable into the interpreter, although this is rare. Specifically, pigi contains Tk, an X window toolkit based on Tcl, while ptcl does not. Hence, any star that uses Tk is excluded from ptcl.$PTOLEMY/mk/userstars.mk includes rules to build the proper object file for a star.
See "Dynamic linking fails" on page A-30. for hints about fixing incremental linking problems.
It is also possible to link in several object files at once, or pull in functions from libraries by use of the
multilink command. The syntax ismultilink opt1 opt2 opt3 ...
where the options may be the names of object files, linker options such as "-L" or "-l" switches, etc. These arguments are supplied to the Unix linker along with whatever options are needed to completely specify the incremental link. link or multilink command could be repeated. There is an alternative linking command that specifies that the new code is to be considered "permanent"; it causes a new symbol table to be produced for use in future links (See the ptlang derivedFrom item in the Ptolemy Programmers Manual for more information). Such code cannot be replaced, but it can be linked against by future incremental link commands. The syntax ispermlink opt1 opt2 opt3 ...
where the options are the same as for the multilink command.3.9.5 Top-level blocks
The commandtopblocks
returns the list of top-level blocks in the current galaxy or universe. With an argument,topblocks block
it returns the list of top-level blocks in the named block.3.9.6 Examining states
The statevalue command takes the formstatevalue block state
and returns the current value of the state state within the block block. The command takes an optional third argument, which may be either "current" to specify that the current value should be returned (the default), or "initial" to specify that the initial value (the parameter value) should be returned.3.9.7 Giving up
The exit command exits the interpreter. The syntax isexit
3.9.8 Getting help
The help command implements a simple help system describing the commands available and their syntax. It does not provide help with the standard Tcl functions. The syntax ishelp topic
orhelp ?
for a list of topics. If the argument is omitted, a short "help on help" is printed. 3.9.9 Registering actions
It is possible to associate a Tcl action with the firing of any star. The registerAction command does this. The syntax isregisterAction pre tcl_command
registerAction post tcl_command
The first argument specifies whether the action should occur before or after the firing of a star. The second argument is a string giving the first part of a tcl command. Before this command is invoked, the name of the star that triggered the action will be appended as an argument. For example:registerAction pre puts
will result in the name of a star being printed on the standard output before it is fired. A typical "action" resulting from this command would beputs universe_name.galaxy_name.star_name
The value returned by registerAction is an "action_handle", which must be used to cancel the action using cancelAction. The syntax isset action_handle [registerAction pre tcl_command]
cancelAction action_handle
3.9.10 The Interface to Matlab and Mathematica
Ptcl can control Matlab [Han96] and Mathematica [Wol92] processes by means of the matlab and mathematica commands. The commands have a similar syntax:matlab command ?arg1? ?arg2?
The
mathematica command ?arg1? ?arg2?matlab command controls the interaction with a shared Matlab process. The possible commands and arguments are:
To initiate a connection to a Matlab and Mathematica process, use
matlab start
mathematica start
To generate a simple plot of a straight line in Matlab and Mathematica, usematlab send { plot([0 1 2 3])}
mathematica send { Plot[x, {x, 0, 3} ] }
The send command suppresses the output normally returned by interacting with the program using the command interface. The eval command, on the other hand, returns the dialog with the console interface:mathematica eval { Plot[x, {x, 0, 3}] }
-Graphics-
To terminate the connection, usematlab end
mathematica end
One can work with matrices as Tcl lists or in Matlab format. To create a new Matlab matrix x that has two rows and three columns:matlab set x 2 3 "1 2 3 4 5 6" "1 1 1 1 1 1"
We can retrieve this Matlab matrix in the same format:matlab get x
We can also retrieve the matrix elements as a Tcl list of complex numbers in an ordered-pair format:
2 3 {1.0 2.0 3.0 4.0 5.0 6.0} {1.0 1.0 1.0 1.0 1.0 1.0}matlab getpairs x
Now, matrices can be manipulated in both Tcl and Matlab.
(1.0,1.0) (2.0,1.0) (3.0,1.0) (4.0,1.0) (5.0,1.0) (6.0,1.0)ptcl> matlab start
For other examples of the use of the matlab and mathematica Ptcl commands, see
"Using Matlab and Mathematica to Compute Parameters" on page 2-18. These commands support the Matlab and Mathematica consoles in Tycho.
ptcl> set a 1
1
ptcl> set b 2
2
ptcl> set c 3
3
ptcl> set d 4
4
ptcl> set e [expr "{$a $b $c $d}"]
1 2 3 4
ptcl> set f [expr "{$a $b $c $d}"]
1 2 3 4
ptcl> matlab set matrix $b $b $e $f
ptcl> matlab eval {matrix(1,1)}
>>
ans =
1.0000 + 1.0000i
ptcl> set inv_matrix [matlab get inverse {inverse = inv(matrix)}]
2 2 {-1.0 0.5 0.75 -0.25} {1.0 -0.5 -0.75 0.25}
ptcl> set inv_matrix [matlab getpairs inverse {inverse = inv(matrix)}]
(-1.0,1.0) (0.5,-0.5) (0.75,-0.75) (-0.25,0.25)
ptcl> set new $inv_matrix
(-1.0,1.0) (0.5,-0.5) (0.75,-0.75) (-0.25,0.25)
ptcl> lindex $new 0
(-1.0,1.0)
ptcl> matlab unset matrix
ptcl> matlab eval {matrix(1,1)}
ptcl> matlab end