-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmo_template.f90
321 lines (267 loc) · 11.6 KB
/
mo_template.f90
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
!> \file mo_template.f90
!> \brief Template for future module development.
!> \details This module serves as a template for future model developments.
!> It shows the module structure, coding style, and documentation.
!> \authors Matthias Cuntz, Juliane Mai, Christoph Schneider
!> \date Nov 2011
MODULE mo_template
! This module is a template for the JAMS Fortran library.
! The module provides an example procedures and demonstrates the agreed coding standard:
! - Use mo_kind, only: i4, sp, dp, lgt, etc.
! - Always make procedures for single and double precision (sp, dp), i.e. include interface to module procedures.
! - Include optional mask argument, if possible.
! - Make the module private by default.
! - Make routines available explicitly, i.e. public.
! - Break lines at column 130 at most, including comments.
! - Do not use tabs in files.
! - Give 1-line descriptions after the public definition.
! - Documentation:
! * document before the individual routines;
! * but document before the module interface not the separate routines,
! i.e. only one documentation per interface, no separate docu for sp and dp;
! * follow the documentation structure before the interface of mean in mo_template.f90;
! * break comment lines at column 130 at most as well.
! - Sort routines alphabetically in the file and in the public definitions.
! - The modules should be tested with at least two different compilers (of different vendors)
! - Use a subdirectory test_mo_xxx for testing where you simply link your modules:
! This means you do on the command prompt in the test directory:
! ln -s ../mo_kind.f90
! ln -s ../mo_xxx.f90
! - Always include the license terms of the JAMS Fortran library (below)
! - Add the "Note on Numerical Recipes License" if you use Numerical Recipes code in you module
! - Add a Copyright with the developping years and all names of developpers (see License below)
! Written Matthias Cuntz, Nov 2011
! Modified Matthias Cuntz, Nov 2011 - add private
! Nov 2011 - add public
! Matthias Cuntz & Christoph Schneider, Nov 2011 - doxygen
! License
! -------
! This file is part of the JAMS Fortran package, distributed under the MIT License.
!
! Copyright (c) 2011-2016 Matthias Cuntz, Juliane Mai, Christoph Schneider - mc (at) macu (dot) de
!
! Permission is hereby granted, free of charge, to any person obtaining a copy
! of this software and associated documentation files (the "Software"), to deal
! in the Software without restriction, including without limitation the rights
! to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
! copies of the Software, and to permit persons to whom the Software is
! furnished to do so, subject to the following conditions:
!
! The above copyright notice and this permission notice shall be included in all
! copies or substantial portions of the Software.
!
! THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
! IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
! FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
! AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
! LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
! OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
! SOFTWARE.
! Note on Numerical Recipes License
! ---------------------------------
! Be aware that some code is under the Numerical Recipes License 3rd
! edition <http://www.nr.com/aboutNR3license.html>
! The Numerical Recipes Personal Single-User License lets you personally
! use Numerical Recipes code ("the code") on any number of computers,
! but only one computer at a time. You are not permitted to allow anyone
! else to access or use the code. You may, under this license, transfer
! precompiled, executable applications incorporating the code to other,
! unlicensed, persons, providing that (i) the application is
! noncommercial (i.e., does not involve the selling or licensing of the
! application for a fee), and (ii) the application was first developed,
! compiled, and successfully run by you, and (iii) the code is bound
! into the application in such a manner that it cannot be accessed as
! individual routines and cannot practicably be unbound and used in
! other programs. That is, under this license, your application user
! must not be able to use Numerical Recipes code as part of a program
! library or "mix and match" workbench.
! Businesses and organizations that purchase the disk or code download,
! and that thus acquire one or more Numerical Recipes Personal
! Single-User Licenses, may permanently assign those licenses, in the
! number acquired, to individual employees. Such an assignment must be
! made before the code is first used and, once made, it is irrevocable
! and can not be transferred.
! If you do not hold a Numerical Recipes License, this code is only for
! informational and educational purposes but cannot be used.
! Always use the number precisions of mo_kind
USE mo_kind, ONLY: i4, sp, dp
! Of course
IMPLICIT NONE
! Explicitly make public only the routines, parameters, etc. that shall be provided
! Sort alphabetically and give 1-line descriptions.
PUBLIC :: circum ! Circumference of circle
PUBLIC :: mean ! Average
PUBLIC :: PI_dp ! Constant Pi in double precision
PUBLIC :: PI_sp ! Constant Pi in single precision
! Documentation of routines is in front of the routines.
! But documentation of module interface routines must be in front of the interface definition.
! Interfaces for single and double precision routines; sort alphabetically
! Interfaces have to be in a public section for doxygen
! ------------------------------------------------------------------
! NAME
! mean
! PURPOSE
! Calculates the average value of a vector, i.e. the first moment of a series of numbers:
! mean = sum(x)/n
!
!> \brief The average.
!
!> \details Calculates the average value of a vector, i.e. the first moment of a series of numbers:
!> \f[ \bar{x} = \frac{1}{N} \sum_{i=1}^N x_i \f]
!
!> If an optinal mask is given, the mean is only over those locations that correspond
!> to true values in the mask.\n
!> x can be single or double precision. The result will have the same numerical precision.
!
! INTENT(IN)
!> \param[in] "real(sp/dp) :: dat(:)" \f$ x_i \f$ 1D-array with input numbers
!
! INTENT(INOUT)
! None
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
!> \param[in] "logical, optional :: mask(:)" 1D-array with input mask\n
!> If present, only those locations in dat corresponding
!> to the true values in mask are used.
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURN
!> \return real(sp/dp) :: mean — \f$ \bar{x} \f$ average of all elements in vec
!
! RESTRICTIONS
!> \note Input values must be floating points.
!
! EXAMPLE
! vec = (/ 1., 2, 3., -999., 5., 6. /)
! m = mean(vec, mask=(vec >= 0.))
! -> see also example in test directory
! LITERATURE
! Sokal RR & Rohlf FJ - Biometry: the principle and practice of statistics in biological research,
! Freeman & Co., ISBN 0-7167-2411-1
! Press WH, Teukolsky SA, Vetterling WT, & Flannery BP - Numerical Recipes in Fortran 90 -
! The Art of Parallel Scientific Computing, 2nd Edition, Volume 2 of Fortran Numerical Recipes,
! Cambridge University Press, UK, 1996
! HISTORY
!> \author Matthias Cuntz
!> \date Nov 2011
! Modified, Matthias Cuntz, Nov 2011 - include mask
! Matthias Cuntz, Nov 2011 - test size(mask) == size(dat)
INTERFACE mean
MODULE PROCEDURE mean_sp, mean_dp
END INTERFACE mean
! ------------------------------------------------------------------
! Make everything private by default.
! Do it after public interface definitions (for doxygen)
PRIVATE
! ------------------------------------------------------------------
! Public parameters
!> Constant Pi in double precision
REAL(dp), PARAMETER :: PI_dp = 3.141592653589793238462643383279502884197_dp
!> Constant Pi in single precision
REAL(sp), PARAMETER :: PI_sp = 3.141592653589793238462643383279502884197_sp
! Private global parameters (not used, only for demonstration)
INTEGER(i4), PARAMETER :: iTest=1
! ------------------------------------------------------------------
CONTAINS
! ------------------------------------------------------------------
!
! NAME
! circum
!
! PURPOSE
!> \brief Circumference of a circle
!
!> \details Calculates the circumference of a circle
!> \f[ c = 2 \pi r \f]
!
! CALLING SEQUENCE
! out = circum(radius)
!
! INTENT(IN)
!> \param[in] "real(dp) :: radius" Radius
!
! INTENT(INOUT)
! None
!
! INTENT(OUT)
! None
!
! INTENT(IN), OPTIONAL
! None
!
! INTENT(INOUT), OPTIONAL
! None
!
! INTENT(OUT), OPTIONAL
! None
!
! RETURN
!> \return real(dp) :: circum — circumference of circle.
!
! RESTRICTIONS
! None
!
! EXAMPLE
! r = (/ 1., 2, 3., 5., 6. /)
! c = circum(r)
!
! LITERATURE
! None
!
! HISTORY
!> \author Matthias Cuntz
!> \date Dec 2012
elemental pure function circum(radius)
implicit none
real(dp), intent(in) :: radius
real(dp) :: circum
circum = 2.0_dp * pi_dp * radius
end function circum
! ------------------------------------------------------------------
FUNCTION mean_dp(dat, mask)
IMPLICIT NONE
REAL(dp), DIMENSION(:), INTENT(IN) :: dat
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(dp) :: mean_dp
REAL(dp) :: n
LOGICAL, DIMENSION(size(dat)) :: maske
if (present(mask)) then
if (size(mask) /= size(dat)) stop 'Error mean_dp: size(mask) /= size(dat)'
maske = mask
n = real(count(maske),dp)
else
maske(:) = .true.
n = real(size(dat),dp)
endif
if (n <= (1.0_dp+tiny(1.0_dp))) stop 'mean_dp: n must be at least 2'
! Mean
mean_dp = sum(dat(:), mask=maske)/n
END FUNCTION mean_dp
FUNCTION mean_sp(dat, mask)
IMPLICIT NONE
REAL(sp), DIMENSION(:), INTENT(IN) :: dat
LOGICAL, DIMENSION(:), OPTIONAL, INTENT(IN) :: mask
REAL(sp) :: mean_sp
REAL(sp) :: n
LOGICAL, DIMENSION(size(dat)) :: maske
if (present(mask)) then
if (size(mask) /= size(dat)) stop 'Error mean_sp: size(mask) /= size(dat)'
maske = mask
n = real(count(maske),sp)
else
maske(:) = .true.
n = real(size(dat),sp)
endif
if (n <= (1.0_sp+tiny(1.0_sp))) stop 'mean_sp: n must be at least 2'
! Mean
mean_sp = sum(dat(:), mask=maske)/n
END FUNCTION mean_sp
! ------------------------------------------------------------------
END MODULE mo_template