home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / fortran / 3320 < prev    next >
Encoding:
Text File  |  1992-08-31  |  12.9 KB  |  349 lines

  1. Newsgroups: comp.lang.fortran
  2. Path: sparky!uunet!noc.near.net!wpi.WPI.EDU!phillies
  3. From: phillies@wpi.WPI.EDU (George D. Phillies)
  4. Subject: Physical formatting issues
  5. Message-ID: <BtvqHH.3MH@wpi.WPI.EDU>
  6. Sender: news@wpi.WPI.EDU (USENET News System)
  7. Nntp-Posting-Host: wpi.wpi.edu
  8. Organization: Worcester Polytechnic Institute
  9. Distribution: na
  10. Date: Tue, 1 Sep 1992 03:17:41 GMT
  11. Lines: 336
  12.  
  13. We have had a variety of somewhat airy discussions of programming style.
  14. Ignoring the theological fundamentalists who wish to convert us to
  15. another language (COBOL, C, etc.) eliminate GOTO's and other well-defined
  16. language constructs, et tedious cetera, there is the interesting question 
  17. as to how one might physically format a program for documentability
  18. and readability.
  19.  
  20. I follow, for general discussion, a section of local code, taken from a
  21. Brownian Dynamics program we wrote here.  Large parts of the code have 
  22. been deleted, but there is a main, several subroutines, slices of the
  23. variables and procedures documentation, and the included common block.
  24. (The rationale for COMMON blocks via INCLUDE is explained before the
  25. COMMON block.)  The subroutines (there are lots of these in the
  26. original) are carefully kept alphabetical, without which your chances of
  27. finding anything on a printout go downhill. Very far.  Very fast.)
  28.  
  29. The subject of discussion (other people have other approaches) is the
  30. issue of physical formatting of code for legibility.  I don't mind 
  31. being chopped on the readability (the code is not meant to be used by
  32. third parties, but should be readable in a few years), but I think most 
  33. of the group is acutely disinterested in being converted to some other
  34. language.  (By the way, does anyone have a useful C to Fortran converter?)
  35.  
  36. George Phillies                   * C Users Anonymous *
  37. phillies@wpi.wpi.edu              * Salvation Through Fortran*
  38.  
  39. C *************************************************
  40. C * Program DIFFUSION     Version 1.01            *
  41. C * Monte Carlo Diffusion through a random        *
  42. C * Lennard-Jones potential background.           *
  43. C * Program by (names of guilty parties deleted)  *
  44. C *                  1991                         *
  45. C *                                               *
  46. C * Variables are listed and discussed in the     *
  47. C * VARIABLES section at the end of the program   * 
  48. C * Test was Microway NDP FORTRAN, which allows   *
  49. C * 'INCLUDE' and names up to 31 characters long  *
  50. C *************************************************
  51.  
  52. C *************************************************
  53. C * Required control files are *.dat, namely      *
  54. C * RANDOM.DAT, REMOTE.DAT, COMMON01.DAT,         *
  55. C * CORRTIME.DAT.                                 *
  56. C * Files continuing output are *.OUT, namely     *
  57. C * RUN.OUT and RESULT.OUT.                       *
  58. C *************************************************
  59.  
  60.       program diffusion
  61.       include 'common01.dat'
  62.  
  63. C variables for testing program
  64.       integer*4 ilocal
  65.  
  66. c...opening steps that are the same for all runs
  67.       call start1
  68.       write(6,1000) ' start1'
  69. 1000  format(A)
  70.  
  71. C a few lines of code deleted here
  72.  
  73. c...stable termination of the run
  74.       call shutdown
  75.       write(6,1000) ' shutdown'
  76. C********NEXT LINE MUST BE LAST LINE*************************
  77.       end
  78. C********PREVIOUS LINE MUST BE LAST LINE*********************
  79.  
  80. C ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  81. C * END OF PROGRAM diffusion        *
  82. C * End of Main Program Body.       *
  83. C * Subroutines follow.             *
  84. C * GDJP August 1991                *
  85. C ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  86.  
  87.  
  88. C ***********************************************
  89. C * subroutine RANDOM                           *
  90. C * Random generator of linear variates on 0,1  *
  91. C * based on Wichman and Hill Byte article      *
  92. C * GDJP 1/9/90                                 *
  93. C ***********************************************
  94.       subroutine random
  95.  
  96.       include 'common01.dat'
  97.  
  98.       xrandom=171*(xrandom-177*INT(xrandom/177))-2*INT(xrandom/177)
  99.       IF (xrandom .le. 0) xrandom = xrandom + 30269
  100.       yrandom=172*(yrandom-176*INT(yrandom/176))-35*INT(yrandom/176)
  101.       IF (yrandom .le. 0) yrandom = yrandom + 30307
  102.       zrandom=170*(zrandom-178*INT(zrandom/178))-63*INT(zrandom/178)
  103.       IF (zrandom .le. 0) zrandom = zrandom + 30323
  104.       TEMPRAND = xrandom/30269.0+yrandom/30307.0+zrandom/30323.0
  105.       rand = TEMPRAND - INT(TEMPRAND)
  106.       return
  107.       end
  108. C ^^^^^^^^^^^^^^^^^^^^^^^^^^^^END OF RANDOM^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  109.  
  110.  
  111. C ***********************************************
  112. C * subroutine SHUTDOWN 1.01                    *
  113. C * Closes files and turns everything off       *
  114. C * GDJP 1/18/90                                *
  115. C ***********************************************
  116.  
  117.       subroutine shutdown
  118.       include 'common01.dat'
  119.  
  120. C when did we finish
  121.  
  122.       call dostim(hrs,mins,secs,hundrs)
  123.       time2 = 3600.0*hrs+60.0*mins+1.0*secs+0.01*hundrs
  124.       write(50,1000) 'timestamp at calculation end'
  125.       write(50,1030) hrs, mins, secs, hundrs
  126.       write(50,1010) ' Elapsed time was', time2-time 1, 'seconds'
  127.  
  128. 1000  format (A)
  129. 1010  format (A, E14.7, A)
  130. 1030  format (4I8)
  131.  
  132. C commands to shut down stably
  133.       open(63, file = 'random.dat', access = 
  134.      Q 'SEQUENTIAL', status = 'UNKNOWN', form = 'UNFORMATTED')
  135.       write(63,*) xrandom,yrandom,zrandom,iset3g
  136.       close (63, status = 'KEEP')
  137.  
  138. C close the file that describes the run
  139.       close (50, status = 'KEEP')
  140.  
  141. C close the file that records the results
  142.       close (60, status = 'KEEP')
  143.       return
  144.       end
  145. C ^^^^^^^^^^^^^^^^^^^^^^^^^^^^END OF SHUTDOWN^^^^^^^^^^^^^^^^^^^^^^^^^^^
  146.  
  147.  
  148. C ***********************************************
  149. C * subroutine START1 Version 2.0               *
  150. C * Initialize variables, zero matrices,        *
  151. C * Activate random number generator            *
  152. C * Set default values                          *
  153. C * GDJP 4/4/90  and 8/21/91                    *
  154. C ***********************************************
  155.  
  156.       subroutine start1
  157.       include 'common01.dat'
  158.  
  159. C zero matrices ---  or get an unpleasant surprise when you port
  160. C reduced from full program
  161.  
  162.       do 98 i = 1,idof
  163.          wrap(i)  =0
  164.          xbox(i ) =0
  165.          xcutoff(i) =0
  166.          xinit(i) =0
  167.          xpos(i) =0
  168.          xtrial(i) =0
  169. 98    end do
  170.  
  171.  
  172. C Set fixed variables
  173.       iflagcreate = 0
  174.       ncorr = 0
  175.  
  176. C open file for recording run parameters
  177.       open(50, file = 'run.out', access = 
  178.      Q 'SEQUENTIAL', status = 'UNKNOWN', form = 'UNFORMATTED')
  179.  
  180. C open file for recording results of work
  181.       open(60, file = 'result.out', access = 
  182.      Q 'SEQUENTIAL', status = 'UNKNOWN', form = 'UNFORMATTED')
  183.  
  184. C document program in use
  185.  
  186.       write(50,1000) '                                     '
  187.       write(50,1000) '                                     ' 
  188.       write(50,1000) '                                     '
  189.       write(50,1000) '****************************         '
  190.       write(50,1000) '****************************         '
  191.       write(50,1000) '            RESULT                   '
  192.       write(50,1000) '            RESULT                   '
  193.       write(50,1000) '            RESULT                   '
  194.       write(50,1000) '****************************         '
  195.       write(50,1000) '****************************         '
  196.       write(50,1000) 'SUBROUTINES: analysis1,arraypos,batch,compare'
  197.       write(50,1000) 'change,compare,create   '
  198.       write(50,1000) 'makelist,newpos,potcheck,potential   '
  199.       write(50,1000) 'propagator,random,randomg,start1,shutdown       '
  200.       write(50,1000) 'thermal                              '
  201.  
  202.       call dostim(hrs,mins,secs,hundrs)
  203.       time1 = 3600.0*hrs+60.0*mins+1.0*secs+0.01*hundrs
  204.       write(50,1000) 'timestamp at calculation start'
  205.       write(50,1030) hrs, mins, secs, hundrs
  206.       write(60,1030) hrs, mins, secs, hundrs
  207.  
  208.       write(50,1000) 'idof, lmax, nmol'
  209.       write(50,1020) idof,lmax, nmol
  210.  
  211. 1000  format (A)
  212. 1010  format (I4, 3I12 ,I4)
  213. 1020  format(3I8)
  214. 1030  format(4I8)
  215.  
  216.       return
  217.       end
  218.  
  219. C ^^^^^^^^^^^^^^^^^^^^^^^^^^^^END OF START1^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  220.  
  221.  
  222.  
  223. C **********************************************************************
  224. C **********************************************************************
  225. C * No active code beyond this point.                *
  226. C * Following are lists of variables and subroutines *
  227. C ****************************************************
  228.  
  229. C **************************************************
  230. C * Section VARIABLES     Version 1.01             *
  231. C * This section contains no active program state- *
  232. C * ments.  It contains only comment lines listing *
  233. C * program variables.                             *  
  234. C * CS, DR, GDJP August 1991                       *
  235. C **************************************************
  236.  
  237. C **********************************************************************
  238. c...  BINCORR stores <x^2>, <y^2> against time interval
  239. c...  BOLT is the boltzmann weight exp(delener/temp)
  240. c         with temperature in units such that k_B = 1
  241. c...  DBARE =<x^2>/2 for one time step, no potential is a bare diffusion
  242. c...       coefficient.
  243.  
  244. C...  Many lines deleted here
  245.  
  246. c...  XRANDOM used in random # generator
  247. c...  XSTORE stores random # generator variable for probe starts
  248. c...  XTEMP stores random # generator variable for non-probe starts
  249. c...  XTRIAL() is particle position after jump (could reject this)
  250. c...  YRANDOM used in random # generator
  251.  
  252. C... More deleted lines here
  253.  
  254. C **************************************************************************
  255.  
  256. C **************************************************
  257. C * Section PROCEDURES    Version 1.01             *
  258. C * This section contains no active program state- *
  259. C * ments.  It contains only comment lines listing *
  260. C * program subroutines.                           *  
  261. C * GDJP August 1991                               *
  262. C **************************************************
  263.  
  264. C *************************************************************************
  265.  
  266. C...  Unlisted routines are deleted here
  267.  
  268. C...  RANDOM: random number generator (linear variate RAND 0.0-1.0)
  269. C        based on Wichman and Hill
  270. C...  SHUTDOWN: stores RANDOM status and closes other files
  271. C...  START1: zeros matrices, initializes RANDOM, opens files, other 
  272. C        processes that are always the same
  273. C *************************************************************************
  274.  
  275.  
  276. C The following is the common block, which in a working program needs
  277. C its own file. Why common blocks? Because experience shows that the
  278. C largest waste of time, especially with student programmers who are
  279. C not FORTRAN adepts, is failure to pass variables correctly between
  280. C programs and subroutines, coupled with a student reluctance to debug by
  281. C printing values for all variables on each line during a single pass 
  282. C through each subroutine.  With common blocks (IMPLICIT NONE helps, too)
  283. C the error rate in this category of mistake is depressed.
  284.  
  285. C ************************************************* 
  286. C * COMMON01.DAT      Version 1.01                * 
  287. C * Specifies all common blocks and variables for * 
  288. C * glass simulations.  Includes all common       * 
  289. C * blocks, variable type identifications, dimen- * 
  290. C * sion statements, and parameter assignments.   * 
  291. C * Must appear as separate file in order to      * 
  292. C * operate.                                      * 
  293. C * GDJP 8/19/1991                                * 
  294. C ************************************************* 
  295.  
  296. C parameter declaration
  297.       integer*4 idof, lmax, nmol
  298.  
  299. C idof is the dimensionality of the simulation
  300.       parameter (idof = 2)
  301. C lmax is maximum number of glass particles in interaction box
  302.       parameter (lmax = 300)
  303. C nmol is the maximum possible number of glass molecules
  304.       parameter (nmol = 9000)
  305. C CAUTION! Parameters can't be passed inside common blocks!
  306. C We know this, but some users may forget.
  307.  
  308. C variable declaration
  309.       integer*4 dbug, flag, glassmoltype, hundrs, hrs,
  310.      Q i, iflagcreate, isample, iseed, 
  311. C long deletion here
  312.      Q xrandom,xstore,xtemp,yrandom,ystore,ytemp,zrandom, 
  313.      Q zstore,ztemp,itest
  314.  
  315. C note that the declaration and common blocks statements match
  316. C line by line
  317.       common/integr4/ dbug, flag, glassmoltype, hundrs, hrs, 
  318.      Q i, iflagcreate, isample, iseed, 
  319. C long deletion here
  320.      Q xrandom,xstore,xtemp,yrandom,ystore,ytemp,zrandom, 
  321.      Q zstore,ztemp,itest
  322.  
  323.       dimension  glassmoltype(nmol), list(lmax), wrap(idof),
  324.      Q wraptest(idof)  
  325.  
  326.       real*4 bolt, d, daverage, dbare, delener, density, 
  327. C long deletion here
  328.      Q xglass, xinit, xold, xpos, xtrial 
  329.  
  330.       dimension dist(idof), sig(4), sigavg(4), sigavg2(4), xbox(idof), 
  331.      Q  xcutoff(idof), xglass(3,nmol), 
  332.      Q  xinit(idof), xold(idof), xpos(idof), 
  333.      Q  xtrial(idof) 
  334.                     
  335. C again the real*4 declaration and the common blocks match
  336.       common/reel4/ bolt, d, daverage, dbare, delener, density, 
  337. C long deletion here
  338.      Q xglass, xinit, xpos, xtrial 
  339.  
  340.       character string(3)*10, glassname*72,test1
  341.  
  342.       common/caracter/string, glassname
  343.  
  344. C remainder of file deleted
  345.  
  346. ********************
  347.  
  348.  
  349.