New URL for NEMO forge!   http://forge.nemo-ocean.eu

Since March 2022 along with NEMO 4.2 release, the code development moved to a self-hosted GitLab.
This present forge is now archived and remained online for history.
NEMO_coding.conv.tex in trunk/DOC – NEMO

source: trunk/DOC/NEMO_coding.conv.tex @ 2691

Last change on this file since 2691 was 2691, checked in by clevy, 13 years ago

add Coding Conventions tex and pdf files in DOC

File size: 28.1 KB
Line 
1\documentclass[a4paper]{article}
2\usepackage{type1cm} 
3\usepackage{times} 
4\usepackage{color} 
5\usepackage{rotating} 
6\usepackage{color} 
7\usepackage{framed} 
8\usepackage{makeidx} 
9
10
11%%%%%%%
12\pagestyle{empty} 
13\setlength{\leftmargin}{1 cm} 
14\setlength{\rightmargin}{1 cm} 
15\setlength{\oddsidemargin}{0 cm} 
16\setlength{\evensidemargin}{0 cm} 
17\setlength{\topmargin}{-1cm} 
18\setlength{\textwidth}{16 cm} 
19\setlength{\textheight}{25cm} 
20
21%%%%%%%%%essai plus jolis from NEMO book
22\usepackage{fancyhdr}
23
24\pagestyle{fancy}
25%\usepackage[colorlinks=true]{hyperref} %%create link
26 
27\makeindex %% run first makeindex NEMO_coding.conv.idx NEMO_coding.conv.ist
28     
29\begin{document}
30
31
32\title{ 
33\includegraphics[width=0.3\textwidth]{./TexFiles/FIgures/NEMO_logo_Black.pdf} \\
34\vspace{1.0cm}
35\rule{345pt}{1.5pt} \\
36\vspace{0.45cm}
37 {\Huge NEMO coding conventions} 
38\rule{345pt}{1.5pt} \\ 
39{\small  -- version 2 --}   }
40%\title{NEMO coding conventions}
41\author{NEMO System Team - C. L\'evy}
42\date{July 2010}
43
44
45\maketitle
46
47\newpage
48
49\tableofcontents   
50         
51\newpage
52%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
53\section{Introduction}
54This document describes conventions\index{conventions} used in NEMO coding and suggested for its development. The objectives are to offer a guide to all readers of the NEMO code, and to facilitate the work of all the developers, including the validation of their developments, and eventually the implementation of these developments within the NEMO platform. \\
55A first approach of these rules can be found in the code in $NEMO/OPA\_SRC/module\_example$ where all the basics coding conventions are illustrated. More details can be found below.\\
56This work is based on the coding conventions is use for the Community Climate System Model \footnote { http://www.cesm.ucar.edu/working\_groups/Software/dev\_guide/dev\_guide/node7.html }
57 the previous version of this document (ÒFORTRAN coding standard in the OPA SystemÓ) and the expertise of the NEMO System Team which can be contacted for further information ($nemo\_st@locean-ipsl.upmc.fr$)
58After a general overview below, this document will describe :
59\begin{itemize} 
60\item The style rules, i.e. the syntax, appearance and naming conventions chosen to improve readability of the code;
61\item The content rules, i.e. the conventions to improve the reliability of the different parts of the code;
62\item The package rules to go a step further by improving the reliability of the whole and interfaces between routines and modules.
63\end{itemize}
64
65%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
66\section{Overview and general conventions}
67NEMO has different component: ocean dynamics ($OPA\_SRC$), sea-ice ($LIM\_SRC$), ocean biogeochemistry\- ($TOP\_SRC$), linear-tangent and adjoint of the dynamics ($TAM$)É each of them corresponding to a directory.
68In each directory, one will find some FORTRAN files and/or subdirectories, one per functionality of the code: $BDY$ (boundaries), $DIA$ (diagnostics), $DOM$ (domain), $DYN$ (dynamics), $LDF$ (lateral diffusion), etc...\\
69All name are chosen to be as self-explanatory as possible, in English, all prefixes are 3 digits.\\
70English is used for all variables names, comments, and documentation. \\
71Physical units are MKS. Only exception for the temperature, which is expressed in degree Celsius, except in bulk formulae and part of LIM sea-ice model where it is in Kelvin. See $DOM/phycst.F90$ files for conversions.
72%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73\section{Architecture}
74Within each directory, organisation of files is driven by ÒorthogonalityÓ\index{orthogonality}, i.e. one functionality of the code is intented to be in one and only one directory, and one module and all its related routines are in one file.
75The functional modules\index{module} are:
76\begin{itemize} 
77\item SBC      surface module
78\item IOM      management of the I/O
79\item NST      interface to AGRIF (nesting model) for dynamics and biogeochemistry
80\item OBC, BDY management of structured and unstructured open boundaries
81\item C1D      1D (vertical) configuration for dynamics, sea-ice and biogeochemistry
82\item OFF      off-line module: passive tracer or biogeochemistry alone
83\item CFG      tutorial and reference configurations
84\item DOC      documentation
85\end{itemize}
86
87For example, the file $domain.F90$ contains the module $domain$ and all the subroutines related to this module ($ dom\_init, dom\_nam, dom\_ctl$).
88%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
89\section{Style rules}
90%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
91\subsection{Argument list format}
92Routines argument lists will contain a maximum 5 variables\index{variable} per line, whilst continuation lines can be used.
93This applies both to the calling routine and the dummy argument list in the routine being called. The purpose is to simplify matching up the arguments between caller and callee.
94
95\begin{verbatim}
96SUBROUTINE tra_adv_eiv( kt, pun, pvn, pwn )
97
98      CALL tra_adv_eiv( kt, zun, zvn, zwn )
99\end{verbatim}
100
101%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
102\subsection{Array syntax}
103Except for long loops (see below), array notation should be used if possible. To improve readability the array shape must be shown in brackets, e.g.:
104\begin{verbatim}
105onedarraya(:) = onedarrayb(:) + onedarrayc(:)
106twodarray (:,:) = scalar * anothertwodarray(:,:)
107\end{verbatim}
108When accessing sections of arrays, for example in finite difference equations, do so by using the triplet notation on the full array, e.g.:
109\begin{verbatim}
110twodarray(:,2:len2) = scalar                      & 
111   &                * ( twodarray2(:,1:len2-1 )   & 
112   &                -   twodarray2(:,2:len2 ) )
113\end{verbatim}   
114For long, complicated loops, explicitly indexed loops should be preferred. In general when using this syntax, the order of the loops indices should reflect the following scheme: (best usage of data locality):
115\begin{verbatim}
116DO jk = 1, jpk
117   DO jj = 1, jpj
118      DO ji = 1, jpi
119         array(ji,jj,jk) = ...
120      END DO
121   END DO
122END DO
123\end{verbatim}
124%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
125\subsection{Case}
126All FORTRAN keywords are in capital : \begin {verbatim} DIMENSION, WRITE, DO É END DO, NAMELIST \end{verbatim} 
127All other parts of the NEMO code will be written in lower case.   
128
129%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
130\subsection{Comments}
131Comments in the code are useful when reading the code and changing or developing it. \\
132The full documentation and detailed explanations are to be added in the reference manual (TeX files, aside from the code itself). \\
133In the code, the comments should explain variable content and describe each computational step.\\
134Comments in the header start with Ò!!Ó. For more details on the content of the headers, see ÒContent rules/HeadersÓ in this document.\\
135Comments in the code start with "!".\\
136All comments are indented (3, 6, or 9 É blank spaces).\\
137Short comments may be included on the same line as executable code, and an additional line can be used with proper alignment. For example:
138\begin{verbatim}
139      zx = zx *zzy   ! Describe what is going on and if it is
140      !              ! too long use another Ô!Õ for proper
141      !              ! alignment with automatic indentation
142\end{verbatim}     
143More in-depth comments should be written in the form:
144\begin{verbatim}
145!   Check of some namelist values
146\end{verbatim}         
147or
148\begin{verbatim}   
149!
150!         !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
151!         !  Bottom boundary condition on tke
152!         !<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
153!
154\end{verbatim}   
155Key features of this style are 1) it starts with a "!" in the column required for proper indentation, 2) the text is offset above and below by a blank line or a content line built for underlying.
156%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
157\subsection{Continuation lines}
158Continuation lines can be used with precise alignment for readability. For example:
159\begin{verbatim}   
160avmu(ji,jj,jk) = avmu(ji,jj,jk) * ( un(ji,jj,jk-1) - un(ji,jj,jk) )   &
161   &                            * ( ub(ji,jj,jk-1) - ub(ji,jj,jk) )   &
162   &           / (  fse3uw_n(ji,jj,jk)         &
163   &              * fse3uw_b(ji,jj,jk) )
164\end{verbatim}   
165Code lines, which are continuation lines of assignment statements, must begin to the right of the column of the assignment operator. Due to the possibility of automatic indentation in some editor (emacs for example), use a Ô\&Õ as first character of the continuing lines to maintain the alignment.
166%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
167\subsection{Declaration of arguments and local variables}
168
169In a routine, input arguments and local variables are declared 1 per line, with a comment field on the same line as the declaration. Multiple comment lines describing a single variable are acceptable if needed. For example:
170\begin{verbatim} 
171INTEGER             ::   kstp   ! ocean time-step index
172\end{verbatim}
173%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
174\subsection{F90 Standard}
175NEMO software adheres to the FORTRAN 95 language standard and does not rely on any specific language or vendor extension.
176
177
178%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
179\subsection{Free-Form Source}
180Free-form source will be used. The F90/95 standard allows up to 132 characters, but a self-imposed limit of 80 should enhance readability, or print source files with two columns per page. Multi-line comments that extend to column 100 would be unacceptable.
181
182%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
183\subsection{Indentation}
184Code as well as comment lines within loops, if-blocks, continuation lines, MODULE or SUBROUTINE statements will be indented 3 characters for readability. (except for CONTAINS that remains at first column)
185\begin{verbatim} 
186MODULE mod1
187   REAL(wp) xx
188CONTAINS
189   SUBROUTINE sub76( px, py, pz, pw, pa,   & 
190      &              pb, pc, pd, pe      )
191      <instruction>
192   END SUBROUTINE sub76
193END MODULE mod1
194\end{verbatim}
195%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
196\subsection{Loops}
197Loops, if explicit, should be structured with the do-end do construct as opposed to numbered loops. Nevertheless non-number label can be used for a big iterative loop of recursive algorithm. In case of long loop, a self-descriptive label can be used (i.e. not just a number).
198
199%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
200\subsection{Naming Conventions: files}
201A file containing a module will have the same name as the inside module.
202
203%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
204\subsection{Naming Conventions: modules}
205Use meaningful English name and the Ò3 lettersÓ naming convention: first 3 metters for the code section, and last 3 to describe the module. For example, zdftke, where ÒzdfÓ stands for vertical diffusion, and ÒtkeÓ for turbulent kinetic energy.
206Modules must be called with the same name as the file in which they reside, because dependency rules used by "make" programs are based on file names
207\footnote{For example, if routine A "USE"s module B, then "make" must be told of the dependency relation which requires B to be compiled before A. If one can assume that module B resides in file B.o, building a tool to generate this dependency rule (e.g. A.o: B.o) is quite simple. Put another way, it is difficult (to say nothing of CPU-intensive) to search an entire source tree to find the file in which module B resides for each routine or module which "USE"s B.}
208 .
209\\
210Note that by implication multiple modules are not allowed in a single file.
211The use of common blocks is deprecated in Fortran 90 and their use in NEMO is strongly discouraged. Modules are a better way to declare static data. Among the advantages of modules is the ability to freely mix data of various types, and to limit access to contained variables through use of the ONLY and PRIVATE attributes.
212
213%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
214\subsection{Naming Conventions: variables}
215All variable should be named as explicit as possible in English. The naming convention concerns prefix letters of these name, in  order to identify the variable type and status.\\
216Never use a FORTRAN keyword as a routine or variable name. \\
217Table below lists the stating letter(s) to be used for variable naming, depending on their type and status:
218%--------------------------------------------------TABLE--------------------------------------------------
219\begin{table}[htbp]
220\begin{center}
221\begin{tabular}{|p{50pt}|p{50pt}|p{50pt}|p{50pt}|p{50pt}|p{50pt}|p{50pt}|}
222\hline  Type \par / Status &   integer&   real&   logical &   character&   double \par precision&   complex \\ 
223\hline
224public  \par or  \par module variable& 
225\textbf{m n} \par \textit{but not } \par \textbf{nn\_}& 
226\textbf{a b e f g h o} \textbf{q} \textit{to} \textbf{x} \par but not \par \textbf{fs rn\_}& 
227\textbf{l} \par \textit{but not} \par \textbf{lp ld ll ln\_}& 
228\textbf{c} \par \textit{but not} \par \textbf{cp cd cl cn\_}& 
229\textbf{d} \par \textit{but not} \par \textbf{dp dd dl dn\_}& 
230\textbf{y} \par \textit{but not} \par \textbf{yp yd yl} \\
231\hline
232dummy \par argument& 
233\textbf{k} \par \textit{but not} \par \textbf{kf}& 
234\textbf{p} \par \textit{but not}  \par \textbf{pp pf}& 
235\textbf{ld}& 
236\textbf{cd}& 
237\textbf{dd}& 
238\textbf{yd} \\
239\hline
240local \par variable& 
241\textbf{i}& 
242\textbf{z}& 
243\textbf{ll}& 
244\textbf{cl}& 
245\textbf{cd}& 
246\textbf{yl} \\
247\hline
248loop \par control& 
249\textbf{j} \par \textit{but not } \par \textbf{jp}& 
250& 
251& 
252& 
253& 
254 \\
255\hline
256parameter& 
257\textbf{jp}& 
258\textbf{pp}& 
259\textbf{lp}& 
260\textbf{cp}& 
261\textbf{dp}& 
262\textbf{yp} \\
263\hline
264
265namelist&
266\textbf{nn\_}& 
267\textbf{rn\_}& 
268\textbf{ln\_}& 
269\textbf{cn\_}& 
270\textbf{dn\_}& 
271\\
272\hline
273CPP \par macro& 
274\textbf{kf}& 
275\textbf{sf} \par & 
276& 
277& 
278& 
279 \\
280\hline
281\end{tabular}
282\label{tab1}
283\end{center}
284\end{table}
285%--------------------------------------------------------------------------------------------------------------
286
287%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
288\subsection{Operators}
289Use of the operators $<, >, <=, >=, ==, /= $ is strongly recommended instead of their deprecated counterparts, $lt., .gt., .le., .ge., .eq., and .ne. $ The motivation is readability. In general use the notation: \\
290$<Blank><Operator><Blank>$
291%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
292\subsection{Pre processor}
293Where the use of a language pre-processor is required, it will be the C pre-processor (cpp).\\
294The cpp key is the main feature used, allowing to ignore some useless parts of the code at compilation step. \\
295The advantage is to reduce the memory use; the drawback is that compilation of this part of the code isnÕt checked. \\
296The cpp key feature should only be used for a few limited options, if is reduces the memory usage. In all cases, a logical variable and a FORTRAN $IF$ should be preferred.
297When using a cpp key $key\_optionname$, a corresponding logical variable $lk\_optionname$ should be declared to allow FORTRAN $IF$ tests in the code and  a FORTRAN module with the same name (i.e. $optionname.F90$) should
298 to be defined. This module is the only place where a \#if defined command appears, selecting either the whole FORTRAN code or a dummy module. For example, the TKE vertical physics, the module name is $zdftke.F90$, the CPP key is $key\_zdftke$ and the associated logical is $lk\_zdftke$.
299
300The following syntax:
301\begin{verbatim} 
302#if defined key_optionname
303!! Part of code conditionally compiled if cpp key key_optionname is active
304#endif
305\end{verbatim} 
306Is to be used rather than the \#ifdef abbreviate form since it may have conflicts with some Unix scripts.
307
308Tests on cpp keys included in NEMO at compilation step:
309\begin{itemize}
310\item The CPP keys used are compared to the previous list of cpp keys (the compilation will stop if trying to specify a Ònon-existing keyÓ)
311\item If a change occurs in the CPP keys used for a given experiment, the whole compilation phase is done again.
312\end{itemize}
313%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
314\section{Content rules}
315
316%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317\subsection{Configurations}
318The configuration defines the domain and the grid on which NEMO is running. It may be usefull to associate a cpp key and some variables to a given configuration, although the part of the code changed under each of those keys should be minimized. As an example, the "ORCA2" configuration (global ocean, 2 degrees grid size) is associated with the cpp key $key\_orca2$ for which
319\begin{verbatim} 
320cp_cfg = "orca"
321jp_cfg = 2
322\end{verbatim} 
323%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324\subsection{Constants}
325Physical 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, in the setup routine for the package\index{package}. We realize than many parameterizations 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.
326
327%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
328\subsection{Declaration for variables and constants}
329
330\subsubsection{Rules :}
331Variables used as constants should be declared with attribute PARAMETER and used always without copying to local variables, inorder to prevent from using different values for the same constant or changing it accidentally.
332\begin{itemize}
333\item Usage of the DIMENSION statement or attribute is required in declaration statements
334\item Attribute SAVE is banned. This simplifies the use of AGRIF software. Since all the subroutine are embedded into a module, the variables which value have to be preserved between two calls can be declared in the module interface.
335The Ò::Ó 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>:<three blanks> to improve readability.
336\item Declare the length of a character variable using the CHARACTER (len=xxx) syntax
337\footnote { 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). }
338
339\item For all global data (in contrast to module data, that is all data that can be access by other module) must be accompanied with a comment field  on the same line.
340\footnote {This allows a easy research of where and how a variable is declared using the unix command: Ògrep var *90 |grep !:Ó. }
341\\
342For example:
343\begin{verbatim}
344REAL(wp), DIMENSION(jpi,jpj,jpk) ::  ua  &  !: i-horizontal velocity (m/s)
345\end{verbatim}
346\end{itemize}
347
348\subsubsection{Implicit None:}
349 All subroutines and functions will include an IMPLICIT NONE statement.
350Thus all variables must be explicitly typed. It also allows the compiler to detect typographical errors in variable names.
351For modules, one IMPLICIT NONE statement in the modules definition section is needed.
352Improper data initialisation is another common source of errors.
353\footnote{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 initialise the variable pointed to.} 
354To avoid problems, initialise variables as close as possible to where they are first used.
355
356\subsubsection{Attributes:}
357$PRIVATE / PUBLIC$ :
358All resources of a module are $PUBLIC$ by default.
359A reason to store multiple routines and their data in a single module is that the scope of the data defined in the module can be limited to the routines which are in the same module. This is accomplished with the $PRIVATE$ attribute.\\
360$INTENT$ :
361All dummy arguments of a routine must include the $INTENT$ clause in their declaration in order to improve control of variables in routine calls.
362
363%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364\subsection{Headers}
365Prologues are not used in NEMO for now, although it may become an interesting tool in combination with ProTeX auto documentation script in the future.
366Rules to code the headers and layout of a module or a routine are illustrated in the example module available with the code : $NEMO/OPA\_SRC/module\_example$
367%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
368\subsection{Interface blocks}
369Explicit 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 95 compilers can automatically provide explicit interface blocks  for routines contained in a module.
370%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
371\subsection{I/O Error Conditions}
372I/O statements which need to check an error condition will use the $iostat=<integer variable>$ construct instead of the outmoded end= and err=. \\
373Note that a 0 value means success, a positive value means an error has occurred, and a negative value means the end of record or end of file was encountered.
374 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
375\subsection{PRINT - ASCII output files}
376Output listing and errors are directed to $numout$ logical unit =6 and produces a file called $ocean.output$  (use ln\_prt to have one output per process in  MPP). Logical $lwp$ variable allows for less verbose outputs.
377To output an error from a routine, one can use the following template:
378\begin{verbatim}
379      IF( nstop /= 0 .AND. lwp ) THEN   ! error print
380         WRITE(numout,cform_err)
381         WRITE(numout,*) nstop, ' error have been found'
382      ENDIF
383\end{verbatim}
384
385%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
386\subsection{Precision}
387Parameterizations should not rely on vendor-supplied flags to supply a default floating point precision or integer size. The f95$ KIND$ feature should be used instead. In order to improve portability between 32 and 64 bit platforms, it is necessary to make use of kinds by using a specific module ($OPA\_SRC/par\_kind.F90$)  declaring the "kind definitions" to obtain the required numerical precision and range as well as size of INTEGER. It should be noted that constants need to have attached a \_kindvalue to have the according size. \\
388Thus wp being the "working precision" as declared in $OPA\_SRC/par\_kind.F90$, declaring real array $zpc$ will take the form:
389\begin{verbatim}
390      REAL(wp), DIMENSION(jpi,jpj,jpk) ::  zpc      ! power consumption
391       \end{verbatim}
392%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
393\subsection{Structures}
394The TYPE structure allowing to declare some variables is more often used in NEMO, especially in the modules dealing with reading fields, or interfaces.For example
395\begin{verbatim}
396 ! Definition of a tracer as a structure
397      TYPE PTRACER
398         CHARACTER(len = 20)  :: sname  !: short name
399         CHARACTER(len = 80 ) :: lname  !: long name
400         CHARACTER(len = 20 ) :: unit   !: unit
401         LOGICAL              :: lini   !: read in a file or not
402         LOGICAL              :: lsav   !: ouput the tracer or not
403      END TYPE PTRACER
404
405      TYPE(PTRACER) , DIMENSION(jptra) :: tracer
406 \end{verbatim}
407 
408 Missing rule on structure name??
409 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
410\section{Packages coding rules}
411 
412%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
413\subsection{Bounds checking}
414NEMO is able to run when an array bounds checking option is enabled. \\
415Thus, constructs of the  following form are disallowed:
416\begin{verbatim}
417REAL(wp) :: arr(1)
418 \end{verbatim}
419where "arr" is an input argument into which the user wishes to index beyond 1. Use of the (*) construct in array dimensioning  is forbidden also because it effectively disables array bounds checking.
420%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
421\subsection{Communication}
422A package should refer only to its own modules and subprograms and to those intrinsic functions included in the Fortran standard.\\
423All communication with the package will be through the argument list or namelist input.
424\footnote { The point behind this rule is that packages should not have to know details of the surrounding model data structures, or the names of variables outside of the package. A notable exception to this rule is model resolution parameters. The reason for the exception is to allow compile-time array sizing inside the package. This is often important for efficiency.}
425%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
426\subsection{Error conditions}
427When an error condition occurs inside a package, a message describing what went wrong will be printed (see PRINT - ASCII output files). The name of the routine in which the error occurred must be included. It is acceptable to terminate execution within a package, but the developer may instead wish to return an error flag through the argument list, see $stpctl.F90$.
428%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
429\subsection{Memory management}
430
431The main action is to identify and declare which arrays are PUBLIC and which are PRIVATE.\\
432Dynamic memory allocation should be avoided, unless necessary. Indeed, it may be desirable in some occasions. However, this type of memory allocation can reduce performance on some machines, and some debuggers may get confused when trying to diagnose the contents of such variables.  \\
433
434The preferable mechanism for dynamic memory allocation is automatic arrays, as opposed to $ALLOCATABLE$ or $POINTER$ arrays for which memory must be explicitly allocated and de-allocated.
435\footnote {$POINTER$ and $ALLOCATE$ are widely used in AGRIF, and in the routine reading input files.}
436An example of an automatic array is:
437\begin{verbatim}
438SUBROUTINE sub(n)
439   REAL :: a(n)
440   ...
441END SUBROUTINE sub
442\end{verbatim}
443
444Whereas the same example with $ALLOCATE$ is:
445\begin{verbatim}
446SUBROUTINE sub(n)
447   REAL, ALLOCATABLE :: a(:)
448   ALLOCATE(a(n))
449   ...
450   DEALLOCATE(a) ...
451END SUBROUTINE sub
452\end{verbatim}
453
454 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
455\subsection{Optimisation}
456
457Considering the new computer architecture, optimisation cannot be considered independently from the computer type.
458In NEMO, portability is a priority, before any too specific optimisation.
459Some tools are available to help: \\
460For vector computers:
461\begin{itemize} 
462\item  using $key\_vectopt\_loop$ allows to unroll a loop
463\end{itemize}
464%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
465\subsection{Package attribute: $PRIVATE, PUBLIC, USE, ONLY$}
466Modules 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.
467%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
468\subsection {Parallelism: using MPI}
469NEMO is written in order to be able to run on one processor, or on one or more using MPI (i.e. activating the cpp key $key_mpp\_mpi$, and defining the number of subdomains in latitude and longitude. The domain decomposition divides the global domain in cubes (see NEMO reference manual). Whilst coding a new development, the MPI compatibility has to be taken in account (see $lib\_mpp.F90$) and should be tested.
470
471%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
472\section{Features to be avoided}
473
474The code must follow the current standards of FORTRAN and ANSI C.  In particular, the code should not produce any WARNING at compiling phase, so that user can be easily alerted of potential bugs when some appear in their new developments. ).
475Below is a list of features to avoid:
476\begin{itemize}
477\item COMMON blocks (use the declaration part of MODULEs instead)
478\item EQUIVALENCE (use POINTERs or derived data types instead to form data structures)
479\item Assigned and computed GOTOs (use the CASE construct instead)
480\item Arithmetic IF statements ( use the block IF, ELSE, ELSEIF, ENDIF or SELECT CASE construct instead)
481\item Labeled DO constructs  (use unlabeled END DO instead)
482\item FORMAT statements (use character parameters or explicit format- specifiers inside the READ or WRITE statement instead)
483\item GOTO and CONTINUE statement (use IF, CASE, DO WHILE, EXIT or CYCLE statements or a contained
484\item PAUSE
485\item ENTRY statements: a subprogram must only have one entry point.
486\item RETURN Ð it is obsolete and so not necessary at the end of program units
487\item STATEMENT FUNCTION
488 \item Avoid functions with side effects.
489\footnote{ First, the code is easier to understand, if you can rely on the rule that functions don't change their arguments, second, some compilers generate more efficient code for PURE (in FORTRAN 95 there are the attributes PURE and ELEMENTAL) functions, because they can store the arguments in different places. This is especially important on massive parallel and as well on vector machines. }
490\item DATA and BLOCK DATA - (use initialisers)
491\end{itemize}
492
493%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
494
495% \printindex
496% \input NEMO_coding.conv.ind
497
498\end{document}
Note: See TracBrowser for help on using the repository browser.