Changes between Version 17 and Version 18 of doc/tec/developerrules/palmstandard


Ignore:
Timestamp:
Nov 21, 2018 1:26:55 PM (6 years ago)
Author:
kanani
Comment:

--

Legend:

Unmodified
Added
Removed
Modified
  • doc/tec/developerrules/palmstandard

    v17 v18  
    66\\\\
    77Formulated rules are based on the specifications given for the ocean dynamics model [https://www.nemo-ocean.eu/ NEMO].
    8 Other work that influenced the development of this standard are [http://www.cesm.ucar.edu/models/ccsm4.0/ Community Climate System Model] [http://www.cesm.ucar.edu//working_groups/Software/dev_guide/dev_guide/ Software Developer's Guide], and [attachment:european_standards-1.pdf European Standards For Writing and Documenting Exchangeable FORTRAN 90 Code].
     8Other work that influenced the development of this standard are [http://www.cesm.ucar.edu/models/ccsm4.0/ Community Climate System Model] [http://www.cesm.ucar.edu//working_groups/Software/dev_guide/dev_guide/ Software Developer's Guide], and [attachment:european_standards-1.pdf European Standards For Writing and Documenting Exchangeable Fortran 90 Code].
    99
    1010\\
    1111= (1) General hints & requirements =
    1212== (1.1) Language ==
    13 * '''FORTRAN-2003''' standard (all FORTRAN compilers can handle this, FORTRAN-2008 standard not yet supported by all compilers)
     13* '''Fortran-2003''' standard (all Fortran compilers can handle this, Fortran-2008 standard not yet supported by all compilers)
    1414* Use '''English''' language only
    1515* Use '''ASCII''' characters only
     
    5555* '''PARAMETER declaration''' | Variables used as constants should be declared with attribute PARAMETER and used always without copying to local variables. This prevents from using different values for the same constant or changing them accidentally.
    5656* '''DIMENSION statement''' | or attribute is required in declaration statements.
    57 * ''' :: notation''' | is quite useful to show that this program unit declaration part is written in standard FORTRAN syntax, even if there are no attributes to clarify the declaration section. Always use the notation <blank>::<two blanks> to improve readability.
     57* ''' :: notation''' | is quite useful to show that this program unit declaration part is written in standard Fortran syntax, even if there are no attributes to clarify the declaration section. Always use the notation <blank>::<two blanks> to improve readability.
    5858* '''LEN specifier''' | Declare the length of a character variable using the CHARACTER (LEN=xxx) syntax - the LEN specifier is important because it is possible to have several kinds for characters (e.g. Unicode using two bytes per character, or there might be a different kind for Japanese e.g. NEC).
    5959* '''Precision''' | Parameters and variables should not rely on vendor-supplied flags to supply a default floating point precision or integer size. The F2003 KIND feature should be used instead. Always use the default REAL- (wp) and INTEGER- (iwp) working precision in the respective declaration statements:
     
    6969  REAL(sp), DIMENSION(:,:,:), ALLOCATABLE ::  lpf  !< array for NetCDF output
    7070  }}}
    71 * '''INTENT clause''' | All dummy arguments must include the INTENT clause in their declaration. This is extremely valuable to someone reading the code, and can be checked by compilers. A common mistake is to put the wrong type of variable in a routine call. So, develop the habit of checking types of arguments in parameter lists. Many modern compilers, especially for FORTRAN 2003, check for consistent use within a file or across files using inter-procedural analysis. Compilers for FORTRAN 2003 will also flag up errors at link time if there are explicit or implicit interfaces.
     71* '''INTENT clause''' | All dummy arguments must include the INTENT clause in their declaration. This is extremely valuable to someone reading the code, and can be checked by compilers. A common mistake is to put the wrong type of variable in a routine call. So, develop the habit of checking types of arguments in parameter lists. Many modern compilers, especially for Fortran 2003, check for consistent use within a file or across files using inter-procedural analysis. Compilers for Fortran 2003 will also flag up errors at link time if there are explicit or implicit interfaces.
    7272* '''PRIVATE attribute''' | Modules variables and routines should be encapsulated by using the PRIVATE attribute. What shall be used outside the module can be declared PUBLIC instead. Use USE with the ONLY attribute to specify which of the variables, type definitions etc. defined in a module are to be made available to the using routine. Of course you do not need to add the ONLY attribute if you include all or nearly all public declarations of a module.
    7373* '''Data initialization''' | Improper data initialization is another common source of errors. A variable could contain an initial value you did not expect. This can happen for several reasons, e.g. the variable has never been assigned a value, its value is outdated, memory has been allocated for a pointer but you have forgotten to initialize the variable pointed to. Some compilers initialize variables to zero but when you port your code to another computer that does not do this previously working code will no longer work this can take some time to diagnose and longer to resolve. To avoid such mishaps, '''initialize''' variables as close as possible to where they are first used. If possible, give a default initial value in the declaration statement.
    74 * '''Constants and magic numbers''' | Magic numbers should be avoided. Physical constants (e.g. pi, gas constants) must never be hardwired into the executable portion of a code. Instead, a mnemonically named variable or parameter should be set to the appropriate value, probably in the setup routine for the package. We realize that many parametrizations rely on empirically derived constants or fudge factors, which are not easy to name. In these cases it is not forbidden to leave such factors coded as magic numbers buried in executable code, but comments should be included referring to the source of the empirical formula. Hard-coded numbers should never be passed through argument lists. One good reason for this rule is that a compiler flag, which defines a default precision for constants, cannot be guaranteed. FORTRAN 2003 allows specification of the precision of constants through the "_" compile-time operator (e.g. 3.14_dp or 365_i8). So if you insist on passing a constant through an argument list, you must also include a precision specification in the calling routine. If this is not done, a called routine that declares the resulting dummy argument as, say, real(dp) or 8 bytes, will produce erroneous results if the default floating point precision is 4 byte.
    75 * '''INTERFACE blocks''' | Explicit interface blocks are required between routines if optional or keyword arguments are to be used. They also allow the compiler to check that the type, shape and number of arguments specified in the CALL are the same as those specified in the subprogram itself. FORTRAN 2003 compilers can automatically provide explicit INTERFACE blocks for routines contained in a MODULE.
     74* '''Constants and magic numbers''' | Magic numbers should be avoided. Physical constants (e.g. pi, gas constants) must never be hardwired into the executable portion of a code. Instead, a mnemonically named variable or parameter should be set to the appropriate value, probably in the setup routine for the package. We realize that many parametrizations rely on empirically derived constants or fudge factors, which are not easy to name. In these cases it is not forbidden to leave such factors coded as magic numbers buried in executable code, but comments should be included referring to the source of the empirical formula. Hard-coded numbers should never be passed through argument lists. One good reason for this rule is that a compiler flag, which defines a default precision for constants, cannot be guaranteed. Fortran 2003 allows specification of the precision of constants through the "_" compile-time operator (e.g. 3.14_dp or 365_i8). So if you insist on passing a constant through an argument list, you must also include a precision specification in the calling routine. If this is not done, a called routine that declares the resulting dummy argument as, say, real(dp) or 8 bytes, will produce erroneous results if the default floating point precision is 4 byte.
     75* '''INTERFACE blocks''' | Explicit interface blocks are required between routines if optional or keyword arguments are to be used. They also allow the compiler to check that the type, shape and number of arguments specified in the CALL are the same as those specified in the subprogram itself. Fortran 2003 compilers can automatically provide explicit INTERFACE blocks for routines contained in a MODULE.
    7676
    7777== (2.3) Pre-processor directives ==
    78 PALM works with the C Pre-Processor (CPP), available on any UNIX platform, and covered by most FORTRAN compilers. The use of pre-processor directives inside the code allow to specifically exclude or include parts of the code for compilation.
     78PALM works with the C Pre-Processor (CPP), available on any UNIX platform, and covered by most Fortran compilers. The use of pre-processor directives inside the code allow to specifically exclude or include parts of the code for compilation.
    7979Only few pre-processor directives are used in PALM ([wiki:doc/app/cpp_options list of directives]), and activated by the {{{%cpp_options}}} variable in the .palm.config.<configuration_identifier> file.
    8080
     
    118118'''File header section'''
    119119(see Fig. 1)
    120 a. Doxygen command for FORTRAN file name ('''!>''' starts a doxygen command line)
     120a. Doxygen command for Fortran file name ('''!>''' starts a doxygen command line)
    121121b. PALM license section (you may have to add other licenses if allowedly implementing code from other models)
    122122c. Revision comments (see [wiki:doc/tec/developerrules/svnbranch working with svn branches] and [wiki:doc/tec/developerrules committing to trunk])
     
    176176= (4) Naming conventions =
    177177== (4.1) Use of lower & upper case letters ==
    178 * '''Upper case:''' FORTRAN keywords and intrinsic functions or routines, e.g.
     178* '''Upper case:''' Fortran keywords and intrinsic functions or routines, e.g.
    179179  * SUBROUTINE, MODULE, etc.
    180180  * INTEGER, REAL, PARAMETER, etc.
     
    260260== (5.3) Alphabetical sorting ==
    261261* Members in {{{ONLY}}} lists of USE statements (see e.g. Fig. 4)
    262 * Parameters in {{{NAMELISTS}}} (see e.g. initialization_parameters NAMELIST in parin.f90)
     262* Parameters in Fortran {{{NAMELIST}}} lists (see e.g. initialization_parameters namelist in parin.f90)
    263263* Declaration types (first {{{CHARACTER}}}, then {{{INTEGER}}}, etc., see Fig. 4)
    264264* Variables in declaration statements (see Fig. 4)