Guide for good coding

From Planets
Revision as of 10:50, 5 January 2023 by Romain.vande (talk | contribs) (How to create a new module/subroutine)

Jump to: navigation, search

How to create a new module/subroutine

 1       MODULE aquarium_mod
 2 
 3       IMPLICIT NONE
 4 
 5       INTEGER :: water   ! Define what is the variable
 6 
 7       CONTAINS
 8 
 9       SUBROUTINE aquarium(kre, grass, other_things
10      &   really_anything)
11                                                          
12       use another_module_mod, only: fct, variable
13       
14 c=======================================================================
15 c   subject:
16 c   --------
17 c   What is the subroutine doing
18 c
19 c   author: Someone smart 
20 c   ------
21 c   update: Someone smarter, march 2012:
22 c         - I change something here
23 c   
24 c=======================================================================
25       
26        IMPLICIT NONE
27 
28 c-----------------------------------------------------------------------
29 c
30 c    Declarations :
31 c    --------------
32 c
33 c    Input/Output
34 c    ------------
35 
36       REAL, INTENT(IN)    ::  something(ngrid,nlayer) ! 2D tabular used as an input only
37       REAL, INTENT(OUT)   ::  kre(ngrid)              ! 1D tabular output by the subroutine  
38       REAL, INTENT(INOUT) ::  grass(ngrid,nlayer)     ! Variable that is modified by the subroutine
39                                                       ! it is an input and output
40       LOGICAL,INTENT(IN)  ::  really_anything         ! Boolean variable
41 
42 c    Local saved variables :
43 c    -----------------
44       INTEGER, PARAMETER :: nb_kre = 6                ! Number of kre
45       INTEGER, SAVE      :: rock                      ! Another variable
46       LOGICAL,SAVE :: firstcall=.true.                ! Another variable
47 
48 c    Local variables :
49 c    -----------------
50       REAL tabular(ngrid)                             ! Local var
51       INTEGER i                                       ! Loop var
52 
53 c    Beginning of the code :
54 c    ----------------- 
55 
56       IF (firstcall) THEN
57         ! identify where are the kre
58         DO i=1,nb_kre
59           kre(i)=i
60           write(*,*) "the Kre ", i, "is behind the rock"
61         ENDDO !end of the loop nb_kre
62       ENDIF   !end firstcall
63 
64       END SUBROUTINE aeropacity
65       
66       END MODULE aeropacity_mod

How to pass variable to subroutine (by argument or by module)

Naming variables convention

Loop index convention

Efficient loop coding

How to use the Intent

How to comment code

General guide