Table of Contents

Scripting in Computational Science by H. P. Langtangen.
1Introduction1
1.1Scripting: Higher­Level Programming for the 21st Century1
1.1.1A Productive Pair: C and the Unix Shell1
1.1.2System Programming Versus Scripting2
1.1.3GUI Programming6
1.1.4When to Choose a Scripting Language7
1.1.5Why Scripting is Useful in Computational Science9
1.1.6Script or Program?9
1.1.7Examples on Scripting Applications10
1.2Preparations for Studying the Book11
1.2.1Tools and Example Codes11
1.2.2Setting Environment Variables12
2Basic Scripting14
2.1The First Python Encounter15
2.1.1A Scientific Hello World Script15
2.1.2Reading and Writing Data Files19
2.1.3Automating Simulation and Visualization26
2.1.4Running Series of Simulations36
2.1.5File Format Conversion43
2.2A List of Common Tasks46
2.2.1File Reading and Writing47
2.2.2Output Formatting48
2.2.3Running an Application50
2.2.4List Operations51
2.2.5Tuples54
2.2.6Dictionary Operations54
2.2.7Writing and Reading Python Data Structures56
2.2.8Splitting and Joining Text57
2.2.9Text Processing58
2.2.10String Operations60
2.2.11Numerical Expressions61
2.2.12Environment Variables62
2.2.13Functions63
2.2.14File Globbing67
2.2.15Testing File Types67
2.2.16Copying and Renaming Files68
2.2.17Creating and Moving to Directories69
2.2.18Removing Files and Directories70
2.2.19Splitting a Pathname Into Directory, Filename, Etc70
2.2.20Platform­Dependent Operations71
2.2.21Traversing Directory Trees71
2.2.22Downloading Internet Files74
2.2.23CPU­Time Measurements74
2.2.24Binary Input/Output76
2.2.25Parsing Command­Line Arguments78
2.2.26The Python Profiler81
2.2.27Programming with Classes82
2.2.28Pickling Objects Into Files84
2.2.29Shelving Objects85
2.2.30Exception Handling86
2.2.31Determining a Variable's Type87
2.2.32Exercises89
2.3Some Useful Python Utilities104
2.3.1Building and Using Modules104
2.3.2IDLE: an Integrated Development Environment107
2.3.3Python Tools for Documenting Source Code109
2.3.4A List of Some Useful Python Modules111
2.3.5About Headers in Python Scripts111
2.4Numerical Computing in Python113
2.4.1A Quick NumPy Primer114
2.4.2File I/O with NumPy Arrays117
2.4.3Random Numbers118
2.4.4Linear Algebra121
2.4.5Reading and Writing Tables With NumPy Arrays121
2.4.6The ScientificPython Package124
2.4.7The Gnuplot Module124
2.4.8Example: Fitting a Straight Line to Data Points126
2.4.9The Python--Matlab Interface127
2.5Regular Expressions and Text Processing129
2.5.1Motivation129
2.5.2Special Characters132
2.5.3Combining Regular Expressions134
2.5.4Groups137
2.5.5Extracting Multiple Matches139
2.5.6Splitting Text141
2.5.7Pattern­Matching Modifiers142
2.5.8Substitution and Backreferences144
2.5.9A General Substitution Script148
2.5.10Debugging Regular Expressions149
2.5.11Non­Greedy Versus Greedy Regular Expressions150
2.5.12The Nature of Regular Expressions154
2.5.13Exercises154
3Building Graphical User Interfaces169
3.1Introduction to GUI Programming in Python/Tkinter169
3.1.1Introductory Remarks170
3.1.2GUI Versions of the Scientific Hello World Script172
3.1.3The Hello World GUI Implemented as a Class182
3.1.4Learning About the Pack Command185
3.1.5An Introduction to the Grid Geometry Manager187
3.1.6The Simulation and Visualization Script with a GUI189
3.1.7Adapting Widgets to the User's Resize Actions195
3.2A List of Common Widget Operations197
3.2.1Frame202
3.2.2Label202
3.2.3Button203
3.2.4Entry203
3.2.5Slider204
3.2.6Checkbutton204
3.2.7Radiobutton205
3.2.8Menu and Menubutton205
3.2.9Balloon Help206
3.2.10Option Menu207
3.2.11Listbox207
3.2.12Message Box209
3.2.13User­Defined Dialogs210
3.2.14Color­Picker Dialogs212
3.2.15File Selection Dialogs213
3.2.16Toplevel214
3.2.17Combo Box215
3.2.18Other Types of Widgets215
3.3More GUI Programming220
3.3.1Customizing Fonts and Colors220
3.3.2A Simple Graphical Function Evaluator222
3.3.3A Text Widget with Tailored Keyboard Bindings226
3.3.4A Fancy List Widget228
3.3.5Making Reuseable Pmw­Compatible Widgets232
3.4Animated Graphics with Canvas Widgets232
3.4.1The First Canvas Encounter232
3.4.2Coordinate Systems233
3.4.3The Mathematical Model Class237
3.4.4The Planet Class238
3.4.5Drawing and Moving Planets240
3.4.6Dragging Planets to New Positions241
3.4.7Using Pmw's Scrolled Canvas Widget245
3.4.8Interactive Grid Generation with a Canvas Widget248
3.5Web Interfaces; CGI Programming248
3.5.1Web Versions of the Scientific Hello World Program249
3.5.2Debugging CGI Scripts253
3.5.3HTMLgen for Generating HTML Code254
3.5.4A General Shell Script Wrapper for CGI Scripts256
3.5.5Making a Web Interface to a Script258
3.5.6Getting a CGI Script to Work262
3.5.7Security Issues265
3.5.8Troubleshooting266
4A Collection of Examples269
4.1Experimenting With Compiler Flags269
4.2A Database for NumPy Arrays280
4.2.1The Structure of the Database280
4.2.2Implementation Based on Pickling283
4.2.3Implementation Based on Formatted ASCII Storage284
4.2.4Implementation Based on Shelving284
4.2.5Comparing the Various Techniques286
4.3Estimating Convergence Rates286
4.4Converting Data File Formats287
4.4.1Converting Simple ASCII Formats287
4.4.2Converting Binary Files290
4.5Automating Remote Login and File Transfer291
4.5.1Using Secure Shell Commands in Scripts291
4.5.2Copying Documents from Internet Locations294
4.5.3Transferring Directory Trees in Email295
4.6Running Sets of Computer Experiments295
4.6.1Handling Multiple Values of Input Parameters
4.7Graphics and Image Manipulation295
4.7.1Making MPEG Movies From PostScript Files295
4.8Exchanging File Collections via Email300
4.8.1Specifying and Packing File Collections302
4.9Automating Web Questionnaires306
4.9.1Specification of Questions and Answers307
4.9.2Parsing the Input File and Generating a Web Form309
4.9.3Processing the Form in a CGI Script.313
4.9.4Statistics of the Questionnaire315
4.9.5Making the Webform Language Specific316
4.9.6Links to Individual Answers317
5Scripting and High­Performance Computing319
5.1Timing Array Operations320
5.1.1Timing Results in Python320
5.1.2Timing Results in Other Scripting Languages322
5.1.3Timing Results in C++/C and Fortran 77324
5.1.4Using NumPy Arrays for Speeding up Python Code324
5.2Case Study: Stochastic Beam Simulation325
5.2.1The Computational Problem: Stochastic Simulation325
5.2.2A Pure Python Implementation326
5.2.3Adding Histogram and Graphics329
5.2.4Doing Graphics with Threads331
5.2.5A Pure C/C++ Implementation332
5.2.6A NumPy Implementation334
5.2.7Finding Bottlenecks in the Python Script; Profiling337
5.3About Wrapper Code338
5.3.1Example on Calling C From Python338
5.3.2Automatic Generation of Wrapper Code339
5.3.3Alternatives to using F2Py, Pyfort, and SWIG340
5.4Combining Python and Fortran 77341
5.4.1Wrapping Fortran 77 Code with F2py342
5.4.2Wrapping Fortran 77 Code with Pyfort345
5.5Computing with NumPy Arrays in Fortran347
5.5.1Wrapping the Functions with F2py349
5.5.2Wrapping the Functions with Pyfort351
5.5.3Handling of In/Out Arguments352
5.5.4Migrating the Stochastic Beam Example to F77358
5.6Combining Python and C Code Using SWIG360
5.6.1Defining the Interface and Generating Wrapper Code360
5.6.2Compiling and Linking362
5.7Combining Python and C++ Code Using SWIG367
5.7.1Defining the Interface and Generating Wrapper Code367
5.7.2Compiling and Linking369
5.7.3The Combined Python--C/C++ Application369
5.8Computing with NumPy Arrays in C and C++371
5.8.1Working with the C Part of a NumPy Array Directly371
5.8.2Using SWIG to Simplify NumPy Operations in C374
5.9A Simple Computational Steering Example375
5.9.1Writing a Time­Loop for Repeated Simulations376
5.9.2Creating a Python Interface377
5.9.3The Steering Python Script379
5.9.4Computational Steering via a GUI382
6Creating Efficient Working Environments383
6.1Methods and Software for Wave Simulation383
6.1.1Modeling of Wave Phenomena384
6.1.2The Computational Algorithm388
6.1.3The Need­to­Know Information390
6.1.4Visualization392
6.2Building a Collection of Simple Scripts396
6.2.1Enhanced Browsing of Mapfiles397
6.2.2Searching for Curvenames in a Mapfile398
6.2.3Creating Gnuplot Animations399
6.2.4Automating Simulation401
6.2.5Running Series of Experiments402
6.3A PSE Based on a Flexible GUI405
6.3.1Using Notebooks for Specifying Functions405
6.3.2The BLT Graph Widget409
6.3.3Time and Grid Parameters421
6.3.4User­Drawn Bottom Functions421
6.3.5Collecting the Pieces in a Full GUI425
6.4Adding Computational Steering to the PSE426
6.5Scripting Interface to an Advanced Simulation Code427
ASetting up the Required Software Environment431
A.1Installing Software Packages; Unix Systems431
A.1.1A Suggested Directory Structure431
A.1.2Installing Tcl/Tk and Additional Modules432
A.1.3Installing Python and Additional Modules433
A.1.4Installing Perl and Additional Modules436
A.1.5Installing Gnuplot439
A.1.6Installing SWIG439
A.1.7Installing SDF and Related Tools440
A.1.8Testing the Installation of Scripting Utilities440
A.2Installing Software Packages; Windows Systems441
BElements of Software Engineering442
B.1Verification of Scripts (Regression Testing)442
B.1.1Automating Regression Tests442
B.1.2Implementing a Tool for Regression Tests446
B.1.3Writing a Test Script450
B.1.4Verifying Output from Numerical Computations451
B.1.5Making a Regression Test for the Wave Simulator455
B.2Version Control Management457
B.2.1Getting Started with CVS457
B.2.2Building Scripts to Simplify the Use of CVS461
B.3Documenting Scripts463
B.3.1Writing Clean and Readable Code464
B.3.2Using SDF for Embedded Documentation464
B.3.3The Basic Structure of SDF­Documented Codes465
B.3.4Examples on Some Basic SDF Commands466
B.3.5Translating SDF to Common Formats468
B.4Exercises470
CIntroduction to Perl474
C.1The First Perl Encounter474
C.1.1A Scientific Hello World Script475
C.1.2Reading and Writing Data Files476
C.1.3Automating Simulation and Visualization481
C.1.4There Is More Than One Way to Do It485
C.2A List of Common Tasks489
C.2.1File Reading and Writing489
C.2.2Running an Application490
C.2.3One­Line Perl Scripts491
C.2.4Array and List Operations492
C.2.5Hash Operations494
C.2.6Splitting and Joining Text495
C.2.7Text Processing496
C.2.8String Operations496
C.2.9Numerical Expressions497
C.2.10Environment Variables497
C.2.11Subroutines498
C.2.12File Globbing501
C.2.13Testing File Types502
C.2.14Copying and Renaming Files503
C.2.15Creating and Moving to Directories503
C.2.16Removing Files and Directories503
C.2.17Splitting a Pathname Into Directory, Filename, Etc504
C.2.18Traversing Directory Trees504
C.2.19Downloading a URL505
C.2.20CPU­Time Measurements506
C.2.21Programming with Classes in Perl507
C.2.22Debugging Perl Scripts507
C.2.23Regular Expressions509
C.2.24Exercises513
C.2.25Building and Using Modules516
C.2.26A Quick Intro to POD517
C.2.27A Listing of Some Useful Perl Modules519
C.2.28Binary Input/Output519
C.3Perl Versus Python520
C.3.1Python's Advantages520
C.3.2Perl's Advantages522
C.4GUI Programming with Perl/Tk523
C.4.1GUI Versions of the Scientific Hello World Script523
C.4.2Web Interfaces; CGI Programming527