forked from onetep-devel/documentation
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemplate_mod.F90
More file actions
579 lines (486 loc) · 28.5 KB
/
template_mod.F90
File metadata and controls
579 lines (486 loc) · 28.5 KB
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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
!==============================================================================!
! !
! <Module name> !
! !
! <Author name(s)>, <Date of creation> !
! !
!------------------------------------------------------------------------------!
! # MODULE DESCRIPTION !
! Write a general description of the functionality offered by the module here. !
!------------------------------------------------------------------------------!
! # BACKGROUND !
! Optionally provide a more detailed description of any theoretical or !
! implementation details which may help developers working with the module. !
! This is useful, for example, if the module implements complicated or subtle !
! theory, the implementation is complex or difficult to follow, or there are !
! important data structures which require further explanation. !
!------------------------------------------------------------------------------!
! # NOTES !
! Write notes about the module which may be relevant to other developers, e.g. !
! known issues or constraints. !
! ## TODO LIST !
! * List of known tasks that remain to be completed !
!==============================================================================!
!------------------ DELETE THIS WHEN CREATING A NEW MODULE --------------------!
! # USING THIS TEMPLATE !
! ## General guidelines !
! (These are adapted from `CONTRIBUTING.markdown`. See this document for more !
! guidance on ONETEP coding conventions and guidelines.) !
! !
! Before creating a new module, you should consider carefully whether your new !
! functionality fits within the framework of an existing module, or is generic !
! enough to be part of a multi-purpose module, such as `utils` or `services`. !
! If a new module is needed to encapsulate some new functionality, then you !
! should follow the following guidelines: !
! !
! * Give your module a name which indicates the functionality it contains. !
! * The filename for the module should have the form `<module_name>_mod.F90` !
! where `<module_name>` is the name you have given the module. !
! * By default, variables and procedures in your module should be private !
! (i.e. they should have the `private` attribute). !
! * Global variables (private or public variables declared at the level of !
! the module itself rather than within its routines) constitute "hidden !
! state". Think carefully before declaring any module-level global variables !
! and avoid doing so where it is reasonably possible. !
! * Variables and procedures which do have to be public (accessible outside !
! the module) should be explicitly specified (i.e. they should have the !
! `public` attribute). !
! * In general, public variable and procedure names should be prepended by a !
! standard prefix (typically the module name, or a shortened version of the !
! name). !
! !
! ## Optional elements !
! This template contains some elements which may not be relevant to all !
! modules. For example, the functionality in the module may not lend itself to !
! unit testing or the use of initialization or de-initialization routines. !
! Please remove elements that are not suited for your module. !
! !
! ## Markdown !
! It is recommended that long-form comment text (e.g. in the module or !
! procedure headers is written with Markdown formatting. This enables text to !
! be clearly formatted into sections and bullet-point lists, but should also !
! facilitate automated extraction and formatting of documentation in the !
! future. !
! !
! Markdown is simple to learn and easy to apply, see e.g. !
! [CommonMark help page](https://commonmark.org/help/) !
! !
! Note that, for clarity, Markdown conventions are not fully followed in the !
! template. For example in line with usual man-page syntax, we use angle !
! brackets to denote text which should be replaced in the actual module (e.g. !
! <Module name>). This conflicts with the interpretation of text between angle !
! brackets as HTML tags in Markdown. Since these are to be replaced when the !
! module is written, this should not be an issue. !
! !
! In general, it is recommended to use Markdown syntax on a "best effort" !
! basis, to provide documentation with a readable and clear structure. Where !
! using Markdown conflicts with clarity (as with the case of text within angle !
! brackets), priority should be placed on clarity. !
! !
! ## AUTHOR(S) & CHANGELOG !
! Author(s): James C. Womack !
! Date of creation: 09/2018 !
! List of major changes: !
! 11/09/18, Initial creation of template module, J. C. Womack !
!------------------------------------------------------------------------------!
module template
! # MODULE-WIDE INCLUDES
use comms, only : pub_on_root
use constants, only: DP, stdout, stderr
implicit none
private
! # PUBLIC PROCEDURES
! Initialize module
public :: template_initialise
! De-initialize module (reset module state to default)
public :: template_exit
! Example public procedure
public :: template_pub_proc
! Unit tests for module procedures
public :: template_run_unit_tests
! # PUBLIC MODULE VARIABLES AND TYPES
! Example public variable
logical, public :: template_example_pub_var
! ^-- Module global variables should be avoided where possible to avoid
! introducing 'hidden state'.
! Example public type
public :: EXAMPLE_TYPE
type EXAMPLE_TYPE
integer :: a
integer :: b
integer :: c
end type EXAMPLE_TYPE
! # PRIVATE MODULE VARIABLES AND TYPES
! Example private variable
logical, save :: example_priv_var
! ^-- Module global variables should be avoided where possible to avoid
! introducing 'hidden state'.
! Short code for reporting (useful for grepping output)
! This is also the block name for devel codes
character(len=*), parameter :: module_short_code = "TMPLT"
! # INTERFACE BLOCKS
! Interface blocks may be included here, if needed (e.g. for overloading
! procedure interfaces)
contains
! ## PUBLIC PROCEDURES
subroutine template_initialise()
!==========================================================================!
! # DESCRIPTION !
! Initialize the <template> module: !
! * List of steps involved in module initialization !
! * ... !
!--------------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! None !
!--------------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!==========================================================================!
use constants, only: NORMAL
use rundat, only: pub_devel_code, pub_output_detail
use utils, only: utils_trace_in, utils_trace_out, &
utils_abort, utils_devel_code
implicit none
! Parameters
character(len=*), parameter :: myself = "template_initialise"
! Local variables
logical :: run_unit_tests
integer :: ierror
call utils_trace_in(myself)
! Insert initialization code here. If initialization is not required, this
! routine can be deleted.
!------- Example code for calling unit tests (delete if not needed) -------!
! This is a good place to run unit tests, if this module has any.
! It is recommended to activate these with a devel code value. In this
! case, the devel_code `TMPLT:UNIT_TEST=[T|F]:TMPLT` is checked, where
! TMPLT is the module parameter `module_short_code`.
run_unit_tests = &
utils_devel_code(.false.,module_short_code,"UNIT_TEST",pub_devel_code)
if (run_unit_tests) then
if (pub_on_root) then
write(stdout,"(a)") module_short_code//": Running unit tests."
end if
call template_run_unit_tests(ierror)
if (ierror /= 0) then
call utils_abort("Error in "//myself//": unit tests failed.")
else
write(stdout,"(a)") module_short_code//": Unit tests passed."
end if
end if
!--------------------------------------------------------------------------!
call utils_trace_out(myself)
end subroutine template_initialise
subroutine template_exit()
!==========================================================================!
! # DESCRIPTION !
! De-initialize / clean up the <template> module: !
! * List of steps involved in module initialization !
! * ... !
!--------------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! None !
!--------------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!==========================================================================!
use utils, only: utils_trace_in, utils_trace_out
implicit none
! Parameters
character(len=*), parameter :: myself = "template_exit"
! Local variables
call utils_trace_in(myself)
! Insert de-initialization code here. If initialization is not required,
! this routine can be deleted.
call utils_trace_out(myself)
end subroutine template_exit
subroutine template_pub_proc(example_arg1,example_arg2,example_arg3)
!==========================================================================!
! # DESCRIPTION !
! Write a description of the purpose and functionality of this procedure !
! here. !
!--------------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! example_arg1 out An example argument with intent out. !
! example_arg1 inout An example argument with intent in. !
! example_arg2 in An example argument with intent in (optional) !
!--------------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!==========================================================================!
use utils, only: utils_trace_in, utils_trace_out
implicit none
! Parameters
character(len=*), parameter :: myself = "template_pub_proc"
! Arguments
real(kind=DP), intent(out) :: example_arg1(:)
integer, intent(inout) :: example_arg2
logical, intent(in), optional :: example_arg3
! Local variables
logical :: example_arg3_loc
call utils_trace_in(myself)
! Insert procedure code here
!------ Example code for copying optional arg (delete if not needed) ------!
if (present(example_arg3)) then
example_arg3_loc = example_arg3
else
example_arg3_loc = .false.
end if
!--------------------------------------------------------------------------!
call utils_trace_out(myself)
end subroutine template_pub_proc
subroutine template_run_unit_tests(ierror)
!==========================================================================!
! # DESCRIPTION !
! Run unit tests for <template> module and output results to stdout. !
! !
! An error code is returned. If this is zero, all tests passed and no !
! discrepancies in outcomes were detected between MPI ranks. A non-zero !
! error code indicates test failure and/or discrepancies between ranks. !
!--------------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! ierror out error code (== 0 success, != 0 failure) !
!--------------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!==========================================================================!
use comms, only: pub_total_num_nodes, comms_allgather
use utils, only: utils_trace_in, utils_trace_out, &
utils_alloc_check, utils_dealloc_check
implicit none
! Parameters
character(len=*), parameter :: myself = "template_run_unit_tests"
integer, parameter :: IFAIL = 1
integer, parameter :: IPASS = 2
integer, parameter :: ISKIP = 3
integer, parameter :: TEST_UNDEFINED = 1
integer, parameter :: TEST_PASSED = 0
integer, parameter :: TEST_FAILED = -1
integer, parameter :: TEST_SKIPPED = -2
integer, parameter :: ERROR_FAILED_TEST = 1000
! Arguments
integer, intent(out) :: ierror ! 0 for success
! Local variables
! - General
integer :: itest ! count total number of tests
integer :: outcome ! integer representation of test outcome
integer :: my_results(3) ! test results
! results(IFAIL) counts fails
! results(IPASS) counts passes
! results(ISKIP) counts skips
call utils_trace_in(myself)
!-- Example implementation of unit test framework (delete if not needed) --!
! This is a template which provides a suggested structure for implementing
! unit tests for a module.
!
! In this example, the individual unit tests are implemented as internal
! procedures within the `template_run_unit_tests` subroutine. Each test
! returns the integer `outcome` (which has the value of one of the
! parameters TEST_UNDEFINED, TEST_PASSED, TEST_FAILED, TEST_SKIPPED), and
! overall results (fails, passes, skips) are counted in the array
! `my_results`.
!
! Once all the tests are completed, the results are reported to `stdout`
! and the `template_run_unit_tests` routine returns a single integer error
! code indicating which indicates the outcome of the overall tests.
!
! This is a serial unit testing framework. If you need tests to make use
! of MPI or OpenMP parallelism, you will need to take care to ensure the
! results are correctly communicated between multiple ranks.
! Assume the best, unless otherwise informed
ierror = 0
! Initialize counters
itest = 0
my_results(1:3) = 0
! START OF UNIT TESTS
call internal_test_example_priv_proc(outcome)
call internal_update_results(itest,my_results,outcome)
! END OF UNIT TESTS
! REPORT RESULTS
if (pub_on_root) then
write(stdout,"(a)") module_short_code//": [Unit test report]"
write(stdout,"(a,i0,a)") module_short_code//": ", itest, " tests run"
write(stdout,"(a,i0,a)") module_short_code//": * ", &
my_results(IPASS), " passed"
write(stdout,"(a,i0,a)") module_short_code//": * ", &
my_results(IFAIL), " failed"
write(stdout,"(a,i0,a)") module_short_code//": * ", &
my_results(ISKIP), " skipped"
end if
! Non-zero error code in event of failed tests
if (my_results(IFAIL) > 0) ierror = ierror + ERROR_FAILED_TEST
!--------------------------------------------------------------------------!
call utils_trace_out(myself)
contains
subroutine internal_update_results(itest,my_results,outcome)
!======================================================================!
! # INTERNAL PROCEDURE DESCRIPTION !
! Host: template_run_unit_tests !
! Function: Update test and results counters based on outcome of !
! a unit test !
!----------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! itest inout test counter (increment by 1) !
! my_results inout array counting of passes, fails and skips !
! outcome inout integer outcome code of preceding test !
! set to TEST_UNDEFINED on exit !
!----------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!======================================================================!
use utils, only: utils_abort
implicit none
! Parameters
character(len=*), parameter :: myself = &
"template_run_unit_tests::internal_update_results"
! Arguments
integer,intent(inout) :: itest
integer,intent(inout) :: my_results(3)
integer,intent(inout) :: outcome
! Increment test counter
itest = itest + 1
select case (outcome)
case (TEST_PASSED)
my_results(IPASS) = my_results(IPASS) + 1
case (TEST_FAILED)
my_results(IFAIL) = my_results(IFAIL) + 1
case (TEST_SKIPPED)
my_results(ISKIP) = my_results(ISKIP) + 1
case default
call utils_abort("Error in "//myself//": &
&outcome code not recognized.")
end select
! Reset outcome to undefined state
outcome = TEST_UNDEFINED
end subroutine internal_update_results
subroutine internal_test_example_priv_proc(outcome)
!======================================================================!
! # INTERNAL PROCEDURE DESCRIPTION !
! Host: template_run_unit_tests !
! Function: Unit test for <example_priv_proc> routine !
!----------------------------------------------------------------------!
! ## TESTS !
! * List of tests being performed in this procedure !
! * ... !
! !
!----------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! outcome out integer outcome code for this unit test !
!----------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!======================================================================!
implicit none
! Parameters
character(len=*), parameter :: unit_test_routine = &
"template_run_unit_tests::internal_test_example_priv_proc"
! Arguments
integer,intent(out) :: outcome
! Local variables
! ...
! Assume everything is fine, unless it's not
outcome = TEST_PASSED
! Insert unit test code here for the routine being tested. The value of
! `outcome` should be set to TEST_PASSED, TEST_FAILED, or TEST_SKIPPED
! based on the outcome of your tests
end subroutine internal_test_example_priv_proc
subroutine internal_test_template_pub_proc(outcome)
!======================================================================!
! # INTERNAL PROCEDURE DESCRIPTION !
! Host: template_run_unit_tests !
! Function: Unit test for <template_pub_proc> routine !
!----------------------------------------------------------------------!
! ## TESTS !
! * List of tests being performed in this procedure !
! * ... !
! !
!----------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! outcome out integer outcome code for this unit test !
!----------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!======================================================================!
implicit none
! Parameters
character(len=*), parameter :: unit_test_routine = &
"template_run_unit_tests::internal_test_template_pub_proc"
! Arguments
integer,intent(out) :: outcome
! Local variables
! ...
! Assume everything is fine, unless it's not
outcome = TEST_PASSED
! Insert unit test code here for the routine being tested. The value of
! `outcome` should be set to TEST_PASSED, TEST_FAILED, or TEST_SKIPPED
! based on the outcome of your tests
end subroutine internal_test_template_pub_proc
end subroutine template_run_unit_tests
! ## PRIVATE PROCEDURES
subroutine example_priv_proc(example_arg1,example_arg2,example_arg3)
!==========================================================================!
! # DESCRIPTION !
! Write a description of the purpose and functionality of this procedure !
! here. !
!--------------------------------------------------------------------------!
! # ARGUMENTS !
! <name> <in/out/inout> <arg descrption> !
! example_arg1 out An example argument with intent out. !
! example_arg1 inout An example argument with intent in. !
! example_arg2 in An example argument with intent in (optional) !
!--------------------------------------------------------------------------!
! # AUTHORS & CHANGELOG !
! Author(s): <Author name(s)> !
! Date of creation: <Date of creation> !
! List of major changes: !
! <date> <change description> <author> !
!==========================================================================!
use utils, only: utils_trace_in, utils_trace_out
implicit none
! Parameters
character(len=*), parameter :: myself = "example_priv_proc"
! Arguments
real(kind=DP), intent(out) :: example_arg1(:)
integer, intent(inout) :: example_arg2
logical, intent(in), optional :: example_arg3
! Local variables
logical :: example_arg3_loc
call utils_trace_in(myself)
! Insert procedure code here
!------ Example code for copying optional arg (delete if not needed) ------!
if (present(example_arg3)) then
example_arg3_loc = example_arg3
else
example_arg3_loc = .false.
end if
!--------------------------------------------------------------------------!
call utils_trace_out(myself)
end subroutine example_priv_proc
end module template