[430] | 1 | <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN"> |
---|
| 2 | |
---|
| 3 | <!--Converted with LaTeX2HTML 99.1 release (March 30, 1999) |
---|
| 4 | original version by: Nikos Drakos, CBLU, University of Leeds |
---|
| 5 | * revised and updated by: Marcus Hennecke, Ross Moore, Herb Swan |
---|
| 6 | * with significant contributions from: |
---|
| 7 | Jens Lippmann, Marek Rouchal, Martin Wilck and others --> |
---|
| 8 | <HTML> |
---|
| 9 | <HEAD> |
---|
| 10 | <TITLE>fortran-rules</TITLE> |
---|
| 11 | <!-- Changed by: Hans Grote, 10-Jun-2002 --> |
---|
| 12 | <META NAME="description" CONTENT="fortran-rules"> |
---|
| 13 | <META NAME="keywords" CONTENT="fortran-rules"> |
---|
| 14 | <META NAME="resource-type" CONTENT="document"> |
---|
| 15 | <META NAME="distribution" CONTENT="global"> |
---|
| 16 | |
---|
| 17 | <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1"> |
---|
| 18 | <META NAME="Generator" CONTENT="LaTeX2HTML v99.1 release"> |
---|
| 19 | <META HTTP-EQUIV="Content-Style-Type" CONTENT="text/css"> |
---|
| 20 | </HEAD> |
---|
| 21 | |
---|
| 22 | <BODY > |
---|
| 23 | <!--Navigation Panel--> |
---|
| 24 | <BR> |
---|
| 25 | <!--End of Navigation Panel--> |
---|
| 26 | |
---|
| 27 | <center> |
---|
| 28 | EUROPEAN ORGANIZATION FOR NUCLEAR RESEARCH |
---|
| 29 | <IMG SRC="http://cern.ch/madx/icons/mx7_25.gif" align=right> |
---|
| 30 | <h2> Rules for Fortran routines for MAD-X</B></h2> |
---|
| 31 | </center> |
---|
| 32 | |
---|
| 33 | <UL> |
---|
| 34 | <LI><B> Code</B> |
---|
| 35 | |
---|
| 36 | <OL> |
---|
| 37 | <LI>lower case throughout</LI> |
---|
| 38 | <LI>all routines start with ``implicit none''</LI> |
---|
| 39 | <LI>the following types should be used exclusively: |
---|
| 40 | |
---|
| 41 | <UL> |
---|
| 42 | <LI>integer - use integer as well instead of logical, 0 = false, |
---|
| 43 | else true</LI> |
---|
| 44 | <LI>double precision (not real*8)</LI> |
---|
| 45 | <LI>complex*16 - only internally, and in cases where it really helps</LI> |
---|
| 46 | <LI>character - if this routine can be called from outside |
---|
| 47 | (i.e. as well from C routines), always foresee a length input |
---|
| 48 | variable (see example). When calling a C routine with character strings, |
---|
| 49 | make sure they end with a blank (e.g. 'abc '). |
---|
| 50 | </LI> |
---|
| 51 | </UL></LI> |
---|
| 52 | <LI>use only generic names for functions, i.e. sin, cos, abs, exp |
---|
| 53 | etc. and <B> not</B> dsin, dcos, dabs, dexp etc.; nota bene: ANSI |
---|
| 54 | for arcus sinus and arcus cosinus is asin and acos (not arsin and arcos) |
---|
| 55 | </LI> |
---|
| 56 | <LI>do not use statement functions, arithmetic IF (3-branch), or IMPLICIT GOTO. |
---|
| 57 | </LI> |
---|
| 58 | <LI>in DO loops: use always do...enddo constructs. |
---|
| 59 | </LI> |
---|
| 60 | <LI>IMPORTANT: when transferring strings to C routines, always terminate |
---|
| 61 | the string with a blank, e.g. |
---|
| 62 | <PRE> |
---|
| 63 | call double_to_table('twiss ', 'betx ', betx) |
---|
| 64 | </PRE> |
---|
| 65 | </LI> |
---|
| 66 | <LI>When transferring integer or double to C routines, always use |
---|
| 67 | specific variables (no values or expressions), e.g. to receive the |
---|
| 68 | current element name |
---|
| 69 | <PRE> |
---|
| 70 | integer i |
---|
| 71 | character * 16 name |
---|
| 72 | i = 16 |
---|
| 73 | call element_name(name, i) |
---|
| 74 | </PRE> |
---|
| 75 | </LI> |
---|
| 76 | </OL></LI> |
---|
| 77 | <LI><B> Style</B> |
---|
| 78 | |
---|
| 79 | <OL> |
---|
| 80 | <LI>all modules and stand-alone routines perform all input/output |
---|
| 81 | through symbolic variables in the call (no ``common''); |
---|
| 82 | common blocks can be used inside modules, however.</LI> |
---|
| 83 | <LI>functions return only one (the function-) value and do not |
---|
| 84 | modify any of the input variables</LI> |
---|
| 85 | <LI>all input/output parameters are described, and the routine |
---|
| 86 | purpose stated</LI> |
---|
| 87 | <LI>avoid ``goto'' as far as reasonable</LI> |
---|
| 88 | <LI>indent do ... enddo and if ... then ... else constructs</LI> |
---|
| 89 | <LI>avoid using ``return''; |
---|
| 90 | only one exit of the routine, at the end (this makes |
---|
| 91 | debugging easier) |
---|
| 92 | </LI> |
---|
| 93 | <LI>no I/O (read, write, print to files) in a Fortran routine, except |
---|
| 94 | for printing to output (error messages, summary data etc.); |
---|
| 95 | most of the data will be put into tables via C routine calls.</LI> |
---|
| 96 | <LI> Error flags, or calls to special exception routines |
---|
| 97 | can be foreseen as need arises.</LI> |
---|
| 98 | </OL></LI> |
---|
| 99 | <LI><B> General</B> |
---|
| 100 | |
---|
| 101 | <OL> |
---|
| 102 | <LI>for local buffers of moderate size use local arrays; for bulk |
---|
| 103 | storage use blank common; blank common may as well be used to |
---|
| 104 | pass data inside modules</LI> |
---|
| 105 | <LI>for matrix manipulation use the existing m66 package</LI> |
---|
| 106 | <LI>do not use any external library (e.g. cernlib), rather extract |
---|
| 107 | and include the code needed |
---|
| 108 | </LI> |
---|
| 109 | </OL></LI> |
---|
| 110 | <LI><B> Checks are essential!!</B> |
---|
| 111 | <OL> |
---|
| 112 | <LI> Run your code through f2c to check that the Fortran is compatible |
---|
| 113 | with standard Fortran 77. Recent testing of the MAD-X Fortran77 code has |
---|
| 114 | revealed some problematic programming techniques. |
---|
| 115 | Example: f2c twiss.F |
---|
| 116 | <LI> Use the NAG f95 compiler with very strict checking by using |
---|
| 117 | "make -f Makefile_develop (madxp)" in brackets if you test the MAD-X |
---|
| 118 | PTC version.</LI> |
---|
| 119 | <LI> Then run your example with that executable. The "-nan" compile |
---|
| 120 | flags will demonstrate at run time if variables have not been |
---|
| 121 | properly initialized so as to allow for fixing this problem. |
---|
| 122 | </LI> |
---|
| 123 | <LI> At last the MAD-X custodian (presently FS) will check the |
---|
| 124 | compatibility with Fortran90. Thereby performing certain |
---|
| 125 | "beautification" procedures. Moreover, the NAG f95 compiler |
---|
| 126 | reveals more inconsistencies in the Fortran90 mode. |
---|
| 127 | </LI> |
---|
| 128 | </OL></LI> |
---|
| 129 | </UL> |
---|
| 130 | |
---|
| 131 | <B> Example</B> |
---|
| 132 | <PRE> |
---|
| 133 | |
---|
| 134 | integer function type(s_length, s_type) |
---|
| 135 | *++ Purpose: returns an integer type code for certain elements |
---|
| 136 | *-- input: |
---|
| 137 | * s_length length of s_type |
---|
| 138 | * s_type string containing type |
---|
| 139 | *-- return values: |
---|
| 140 | * 1 for s_type == 'drift' |
---|
| 141 | * 2 for s_type == 'sbend' |
---|
| 142 | * 3 for s_type == 'rbend' |
---|
| 143 | * 5 for s-type == 'quadrupole' |
---|
| 144 | * 0 else |
---|
| 145 | *++ |
---|
| 146 | implicit none |
---|
| 147 | integer s_length |
---|
| 148 | character *(*) s_type |
---|
| 149 | |
---|
| 150 | if (s_type(:s_length) .eq. 'drift') then |
---|
| 151 | type = 1 |
---|
| 152 | elseif (s_type(:s_length) .eq. 'sbend') then |
---|
| 153 | type = 2 |
---|
| 154 | elseif (s_type(:s_length) .eq. 'rbend') then |
---|
| 155 | type = 3 |
---|
| 156 | elseif (s_type(:s_length) .eq. 'quadrupole') then |
---|
| 157 | type = 5 |
---|
| 158 | else |
---|
| 159 | type = 0 |
---|
| 160 | endif |
---|
| 161 | end |
---|
| 162 | </PRE> |
---|
| 163 | <BR><HR> |
---|
| 164 | <aDDRESS> |
---|
| 165 | <I>Hans Grote</I> |
---|
| 166 | <BR><I>2001-01-22</I> |
---|
| 167 | </ADDRESS> |
---|
| 168 | </BODY> |
---|
| 169 | </HTML> |
---|