Difference between revisions of "Guide for good coding"

From Planets
Jump to: navigation, search
(How to create a new module/subroutine)
(How to create a new module/subroutine)
Line 32: Line 32:
 
c  subject:
 
c  subject:
 
c  --------
 
c  --------
c  Initialise module's variable
+
c  Initialise module s variable
 
c
 
c
 
c  author: Someone   
 
c  author: Someone   
Line 47: Line 47:
 
c  subject:
 
c  subject:
 
c  --------
 
c  --------
Initialise module's variable
+
Deallocate module s variable
 
c
 
c
 
c  author: Someone   
 
c  author: Someone   

Revision as of 11:06, 5 January 2023

How to create a new module/subroutine

Subroutines should be integrated inside a module. A good writing habit is to add the line "IMPLICIT NONE", in the beginning of the module and subroutine.

First you can define all the variable that belongs to the module and could be used at another place. A good thing is also to create a subroutine that allocate and initialise module variables. If the variable is only modified inside the module it should be tag as protected. If the variable belongs only to the module it can be specified to be private.

You can now start the definition of the subroutine. Start by variable and function from other module if necessary. Comment the purpose of the subroutine directly. Declare the variables, first the one pass in arguments. Use the intent to specify the status of the variable inside the subroutine. Continue with the local and saved variables. Then you can define local variables. Remember to comment all of them, you can specify the dimension if the variable is an allocatable variable.

 1       MODULE aquarium_mod
 2 
 3       IMPLICIT NONE
 4 
 5       INTEGER :: water     ! Define what is the variable, the dimension, units etc..
 6       REAL,SAVE,ALLOCATABLE,DIMENSION(:) :: kre1      ! Another comment (°)
 7 
 8       CONTAINS
 9 
10       SUBROUTINE ini_aquarium(ngrid)
11 c=======================================================================
12 c   subject:
13 c   --------
14 c   Initialise module s variable
15 c
16 c   author: Someone  
17 c=======================================================================
18       IMPLICIT NONE
19       INTEGER, INTENT(IN) :: ngrid
20 
21       allocate(kre1(ngrid+1))
22 
23       END SUBROUTINE ini_aquarium
24 
25       SUBROUTINE end_aquarium
26 c=======================================================================
27 c   subject:
28 c   --------
29 c   Deallocate module s variable
30 c
31 c   author: Someone  
32 c=======================================================================
33 
34       IMPLICIT NONE
35 
36       IF (allocated(kre1)) deallocate(kre1)
37 
38       END SUBROUTINE end_aquarium
39 
40       SUBROUTINE aquarium(kre, grass, other_things
41      &   really_anything)
42                                                          
43       use another_module_mod, only: fct, variable
44       
45 c=======================================================================
46 c   subject:
47 c   --------
48 c   What is the subroutine doing
49 c
50 c   author: Someone smart 
51 c   ------
52 c   update: Someone smarter, march 2012:
53 c         - I change something here
54 c   
55 c=======================================================================
56       
57        IMPLICIT NONE
58 
59 c-----------------------------------------------------------------------
60 c
61 c    Declarations :
62 c    --------------
63 c
64 c    Input/Output
65 c    ------------
66 
67       REAL, INTENT(IN)    ::  something(ngrid,nlayer) ! 2D tabular used as an input only  (W/m^2)
68       REAL, INTENT(OUT)   ::  kre(ngrid)              ! 1D tabular output by the subroutine  
69       REAL, INTENT(INOUT) ::  grass(ngrid,nlayer)     ! Variable that is modified by the subroutine
70                                                       ! it is an input and output
71       LOGICAL,INTENT(IN)  ::  really_anything         ! Boolean variable  
72 
73 c    Local saved variables :
74 c    -----------------
75       INTEGER, PARAMETER :: nb_kre = 6                ! Number of kre
76       INTEGER, SAVE      :: rock                      ! Another variable
77       LOGICAL,SAVE :: firstcall=.true.                ! Another variable
78 
79 c    Local variables :
80 c    -----------------
81       REAL tabular(ngrid)                             ! Local var
82       INTEGER i                                       ! Loop var
83 
84 c    Beginning of the code :
85 c    ----------------- 
86 
87       IF (firstcall) THEN
88         ! identify where are the kre
89         DO i=1,nb_kre
90           kre(i)=i
91           write(*,*) "the Kre ", i, "is behind the rock"
92         ENDDO !end of the loop nb_kre
93       ENDIF   !end firstcall
94 
95       END SUBROUTINE aeropacity
96       
97       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