Difference between revisions of "Guide for good coding"
From Planets
Romain.vande (talk | contribs) |
Romain.vande (talk | contribs) |
||
Line 1: | Line 1: | ||
== How to create a new module/subroutine == | == How to create a new module/subroutine == | ||
+ | |||
+ | <syntaxhighlight lang="Fortran" line> | ||
+ | MODULE aquarium_mod | ||
+ | |||
+ | IMPLICIT NONE | ||
+ | |||
+ | INTEGER :: water ! Define what is the variable | ||
+ | |||
+ | CONTAINS | ||
+ | |||
+ | SUBROUTINE aquarium(kre, grass, other_things | ||
+ | & really_anything) | ||
+ | |||
+ | use another_module_mod, only: function, variable | ||
+ | |||
+ | c======================================================================= | ||
+ | c subject: | ||
+ | c -------- | ||
+ | c What is the subroutine doing | ||
+ | c | ||
+ | c author: F.Forget | ||
+ | c ------ | ||
+ | c update E. Millour, march 2012: | ||
+ | c - I change something here | ||
+ | c | ||
+ | c======================================================================= | ||
+ | |||
+ | |||
+ | IMPLICIT NONE | ||
+ | |||
+ | c----------------------------------------------------------------------- | ||
+ | c | ||
+ | c Declarations : | ||
+ | c -------------- | ||
+ | c | ||
+ | c Input/Output | ||
+ | c ------------ | ||
+ | |||
+ | REAL, INTENT(IN) :: something(ngrid,nlayer) ! 2D tabular used as an input only | ||
+ | REAL, INTENT(OUT) :: kre(ngrid) ! 1D tabular output by the subroutine | ||
+ | REAL, INTENT(INOUT) :: grass(ngrid,nlayer) ! Variable that is modified by the subroutine | ||
+ | ! it is an input and output | ||
+ | LOGICAL,INTENT(IN) :: really_anything ! Boolean variable | ||
+ | |||
+ | c Local saved variables : | ||
+ | c ----------------- | ||
+ | INTEGER, PARAMETER :: nb_kre = 6 ! Number of kre | ||
+ | INTEGER, SAVE :: rock ! Antother variable | ||
+ | LOGICAL,SAVE :: firstcall=.true. ! Another variable | ||
+ | |||
+ | c Local variables : | ||
+ | c ----------------- | ||
+ | REAL tabular(ngrid) ! Local var | ||
+ | INTEGER i ! Loop var | ||
+ | |||
+ | c Beginning of the code : | ||
+ | c ----------------- | ||
+ | |||
+ | IF (firstcall) THEN | ||
+ | ! identify where are the kre | ||
+ | DO i=1,nb_kre | ||
+ | kre(i)=i | ||
+ | write(*,*) "the Kre ", i, "is behind the rock" | ||
+ | ENDDO !end of the loop nb_kre | ||
+ | ENDIF !end firstcall | ||
+ | |||
+ | END SUBROUTINE aeropacity | ||
+ | |||
+ | END MODULE aeropacity_mod | ||
+ | |||
+ | </syntaxhighlight> | ||
== How to pass variable to subroutine (by argument or by module) == | == How to pass variable to subroutine (by argument or by module) == |
Revision as of 11:47, 5 January 2023
Contents
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: function, variable
13
14 c=======================================================================
15 c subject:
16 c --------
17 c What is the subroutine doing
18 c
19 c author: F.Forget
20 c ------
21 c update E. Millour, march 2012:
22 c - I change something here
23 c
24 c=======================================================================
25
26
27 IMPLICIT NONE
28
29 c-----------------------------------------------------------------------
30 c
31 c Declarations :
32 c --------------
33 c
34 c Input/Output
35 c ------------
36
37 REAL, INTENT(IN) :: something(ngrid,nlayer) ! 2D tabular used as an input only
38 REAL, INTENT(OUT) :: kre(ngrid) ! 1D tabular output by the subroutine
39 REAL, INTENT(INOUT) :: grass(ngrid,nlayer) ! Variable that is modified by the subroutine
40 ! it is an input and output
41 LOGICAL,INTENT(IN) :: really_anything ! Boolean variable
42
43 c Local saved variables :
44 c -----------------
45 INTEGER, PARAMETER :: nb_kre = 6 ! Number of kre
46 INTEGER, SAVE :: rock ! Antother variable
47 LOGICAL,SAVE :: firstcall=.true. ! Another variable
48
49 c Local variables :
50 c -----------------
51 REAL tabular(ngrid) ! Local var
52 INTEGER i ! Loop var
53
54 c Beginning of the code :
55 c -----------------
56
57 IF (firstcall) THEN
58 ! identify where are the kre
59 DO i=1,nb_kre
60 kre(i)=i
61 write(*,*) "the Kre ", i, "is behind the rock"
62 ENDDO !end of the loop nb_kre
63 ENDIF !end firstcall
64
65 END SUBROUTINE aeropacity
66
67 END MODULE aeropacity_mod