Thank you, Nico, for releasing Jmol 11.3.21.

Jmol 11.3.21 represents a quantum leap in scripting and a couple of bug 
fixes for problems introduced in recent versions:

# bug fix: isosurface of MOs not properly displaying color in saved state
# bug fix: state saving of @ definitions has extra = sign.


new feature: fully functional user-defined functions
----------------------------------------------------

This is really something. There are three new scripting capabilities in 
Jmol that as far as I know were not possible at all in Chime scripting 
or RasMol. (Correct me if I am wrong.) Basically, this enhancement turns 
Jmol math into a fledging computing language. The three features include:

macros -- you can now create single-word commands that can be called to 
execute sequences of commands, much like "script somefile.spt" but 
without the file.

subroutines -- you can pass variables to a script in the form of a 
subroutine.

functions -- you can create scripts that have return values that can be 
used in Jmol math calculations and expressions.

All three of these features use the same new mechanism -- a pair of 
statements ("function" and "end function") with statements in between. 
Variables introduced with the "var" keyword, as in JavaScript, are local 
to the function in which they are contained. Localization with "var" 
also works for file-based scripts, which are in all ways identical to 
script-based macros.

The advantage of using functions over individual script files is obvious 
-- faster loading and faster execution, cleaner coding, and more 
extensible "library"-like capabilities.

Functions may or may not have parameters and may or may not have return 
values.
So, for example:


MACROS (no parameters)

 function mymacro
     background red
     color atoms blue
 end function
 
 function mymacro2
     background black
     color atoms cpk
 end function
 
 background white
 delay 2
 mymacro
 delay 2
 mymacro2

 

SUBROUTINES (parameters, but no return)

 function drawline(id,a,b)
   var x = script("draw line"+ id + " {atomno=" + a + "} {atomno=" + b + 
"}")
 end function

 drawline(1,3,5)
 drawline(2,10,12)

Note that if x is defined outside the function, it is unchanged by this 
call, because we have "var x". If the "var" were not there, then x is a 
global variable that is saved in the state. Variables preceeded by "var" 
are not saved in the state.


FUNCTIONS (parameters and returns)

 function d(a,b)
    return a.distance(b)
 end function
 
 x = d({atomno=3},{atomno=4})
 print x


Functions of all types may be nested -- macros can call other macros; 
functions can call subroutines, subroutines can call functions, etc. The 
overall depth, including script file levels is arbitrarily set at 10 for 
now. But there is no particular reason it has to be that small.


SHOW, WRITE, and RESET
----------------------
Along with these capabilities are extensions of the three commands SHOW, 
WRITE, and RESET to allow for easier development.


 show functions  # list functions in the console window
 
 write functions macros.spt # dump functions to a file

 reset x          # "undefine" a variable
 reset all        # delete all user-defined variables
 reset functions  # delete all functions

Of course, there will be a few things to shake out here; I'm hoping some 
users will be interested enough to give this a try and let us know what 
problems arise and what sort of uses this functionality might have.

I suppose the next thing we need is "for" and "while" just to make 
looping a bit easier. That's coming -- it's really a very simple 
addition at this point, though maybe not this week.


Bob Hanson

-- 
Robert M. Hanson
Professor of Chemistry
St. Olaf College
Northfield, MN
http://www.stolaf.edu/people/hansonr


If nature does not answer first what we want,
it is better to take what answer we get. 

-- Josiah Willard Gibbs, Lecture XXX, Monday, February 5, 1900



-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2005.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
Jmol-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/jmol-users

Reply via email to