Ftcl: Calling Tcl procedures
The interface: Fortran calling Tcl
Fortran routines may use the following routines to interact with the Tcl
interpreter (cf. Technical details):
- Set the value of a Tcl variable:
call ftcl_put( variable, value )
The argument variable is the name of the Tcl variable
to be set and value is an argument of type integer,
real, logical, double precision or a
character string.
Note:
If the variable does not yet exist, it will be created.
- Retrieve the current value of a Tcl variable:
call ftcl_get( variable, value )
The argument variable is the name of the Tcl variable
to be retrieved and value is an argument of type
integer, real, logical, double precision or a
character string.
Notes:
- If the variable does not exist, then a default value is
returned.
- If the value to be returned is a character string, the longest
string that will be returned is determined by the actual Fortran
string argument. For instance:
In Tcl:
set string "1234567890"
In Fortran:
character*2 str
...
call ftcl_get( 'string' , str )
write(*,*) 'Result: >', str, '<'
would print:
Result: >12<
- Set the contents of an array:
call ftcl_put_array( listname, array )
The argument listname is the name of the Tcl list
to be set and array is a one- or two-dimensional array of
type integer or real.
Note:
On the Tcl side this becomes a list or a list of lists, whose length
corresponds to the dimension of the Fortran array argument.
- Retrieve the contents of an array (a Tcl list):
call ftcl_get_array( listname, array )
The argument listname is the name of the Tcl list
to be retrieved and array is a one- or two-dimensional
array of type integer or real.
Note:
On the Tcl side this should a list or a list of lists, whose length
corresponds to the dimension of the Fortran array argument.
- Execute a Tcl script:
call ftcl_script( string )
The argument string must be a valid Tcl command stored
in a single character string.
- Set the configuration parameters:
call ftcl_config( option, value )
The argument option must be one of the following:
to be implemented.
- Set the result string:
call ftcl_set_result( value )
The argument value is either an integer, real, double
precision real, logical or string.
- Set the error code:
call ftcl_set_error( error )
The argument error is a logical value, .TRUE.
indicating some error was discovered (the result string should
explain what happened), .FALSE. indicating everything was fine.
Note:
The routines to manipulate Fortran arrays have not been implemented yet.
Creating a graphical user-interface with Tk
The Ftcl library makes it possible to create user-interfaces using the
Tk extension. Here is a small example (see also the lander
sources in the distribution):
PROGRAM LANDER
USE FTCL
CHARACTER(80) astring
IMPULSE = 200
FHEIGHT = 10000.0
SPEED = 100.0
FUEL = 1000.0
GROSS = 900.0
CALL ftcl_start('config.tcl')
CALL ftcl_script('package require Tk')
CALL ftcl_script('inputInitialValues')
CALL ftcl_script('update')
CALL ftcl_script('update idle')
CALL ftcl_get_string('burn', astring)
CALL ftcl_get_int('ready', irdy)
DO WHILE (irdy == 0)
CALL ftcl_script('update')
CALL ftcl_script('update idle')
CALL ftcl_get_int('ready', irdy)
ENDDO
CALL ftcl_get_real('burn', burn)
I = 0
DO WHILE (FHEIGHT .GT. 0)
CALL ftcl_get_real('burn', burn)
CALL CALCSPEED (SPEED, FUEL, GROSS, BURN, IMPULSE, SPEED)
I = I + 1
FUEL = FUEL - BURN
IF (FUEL .LE. 0) THEN
FUEL = 0
BURN = 0
CALL ftcl_put_real('burn', burn)
ENDIF
FHEIGHT = FHEIGHT - SPEED
CALL ftcl_put_int('time', I)
CALL ftcl_put_real('speed', speed)
CALL ftcl_put_real('fuel', fuel)
CALL ftcl_put_real('ht', FHEIGHT)
CALL ftcl_script('showState')
CALL ftcl_script('update idle')
ENDDO
DO
CALL ftcl_script('update idle')
CALL ftcl_script('update')
ENDDO
END
SUBROUTINE CALCSPEED (finit, fuel, gross, burn, impulse, speed)
MASS = gross + fuel
SPEED = SPEED + 9.8 * (1 - impulse * log(mass/(mass-burn)))
END
This program consists of the following steps:
- It loads the Tk extension (via "package require Tk")
- It sets up the user-interface elements (defined via the file
"config.tcl")
- It runs the commands "update" and "update idletasks" to make sure
the user-interface can be shown on screen
- It then enters a loop to see if the user wants to run the program
(irdy is set by the Go button)
- If the button was pressed, it enters a second loop to do the
computation (the height and speed and so on of a lunar lander are
computed) and updates the user-interface.
- Finally, when the computation is done, it runs an idle loop to
allow the user to view the result.
Because the Fortran program stays in control, it is necessary to use the
update command, so that window and other events can be
handled properly.
An alternative is to use the ftcl_make_command() routine to
register a new Tcl command (implemented in Fortran) and to enter the
event loop, as normally happens in applications using Tk (such as wish),
via ftcl_main_loop().