MONC
test_terminationcheck.F90
Go to the documentation of this file.
1 ! Tests the termination checking and determination functionality
3  use fruit, only : assert_equals
4  use terminationcheck_mod, only : timestep_callback, consolidation_callback, modeldump_callback, options_add
5  use state_mod, only : model_state_type
6  implicit none
7 
8  integer, parameter :: max_tests = 1000 ! Number of tests to run for each case, some will require termination some not
9 
10 contains
11 
12  ! Tests the timestep termination checking functionality
14  integer :: i, nn_timesteps
15  type(model_state_type) :: current_state
16  real :: r
17 
18  call init_random_seed
19 
20  do i=1,max_tests
21  call random_number(r)
22  current_state%timestep = int(r*10000)+1
23  call random_number(r)
24  nn_timesteps= int(r*9)+1
25 
26  current_state%last_timestep_column = .true. ! Forces the check (only once per timestep)
27  current_state%continue_timestep=.true.
28  call options_add(current_state%options_database, "nn_timesteps", nn_timesteps)
29  call timestep_callback(current_state)
30  call assert_equals(mod(current_state%timestep, nn_timesteps) /= 0, current_state%continue_timestep,&
31  "Timestep completion consistent with expectations")
32  end do
33  end subroutine test_timestep_callback
34 
35  ! Tests the consolidation stage termination checking functionality
37  integer :: i, nn_timesteps, nn_consolidation, multiplier
38  type(model_state_type) :: current_state
39  real :: r
40 
41  call init_random_seed
42 
43  do i=1,max_tests
44  call random_number(r)
45  nn_timesteps= int(r*9)+1
46  call random_number(r)
47  multiplier = int(r*199)+1
48  call random_number(r)
49  nn_consolidation = int(r*9)+1
50 
51  current_state%timestep = nn_timesteps * multiplier ! timestep already terminated hence multiplication
52  current_state%continue_consolidation=.true.
53  call options_add(current_state%options_database, "nn_timesteps", nn_timesteps)
54  call options_add(current_state%options_database, "nn_consolidation", nn_consolidation)
55  call consolidation_callback(current_state)
56  call assert_equals(mod(current_state%timestep / nn_timesteps, nn_consolidation) /= 0, current_state%continue_consolidation,&
57  "Consolidation completion consistent with expectations")
58  end do
59  end subroutine test_consolidation_callback
60 
61  ! Tests the model dump termination checking functionality
63  integer :: i, nn_timesteps, nn_consolidation, nn_modeldump, multiplier
64  type(model_state_type) :: current_state
65  real :: r
66 
67  call init_random_seed
68 
69  do i=1,max_tests
70  call random_number(r)
71  nn_timesteps= int(r*9)+1
72  call random_number(r)
73  multiplier = int(r*39)+1
74  call random_number(r)
75  nn_consolidation = int(r*9)+1
76  call random_number(r)
77  nn_modeldump = int(r*9)+1
78 
79  current_state%timestep = nn_timesteps * nn_consolidation * multiplier ! timestep and consolidation already terminated hence multiplication
80  current_state%continue_modeldump=.true.
81  call options_add(current_state%options_database, "nn_timesteps", nn_timesteps)
82  call options_add(current_state%options_database, "nn_consolidation", nn_consolidation)
83  call options_add(current_state%options_database, "nn_modeldump", nn_modeldump)
84  call modeldump_callback(current_state)
85  call assert_equals(mod(current_state%timestep / nn_timesteps / nn_consolidation, nn_modeldump) /= 0,&
86  current_state%continue_modeldump, "Model dump completion consistent with expectations")
87  end do
88  end subroutine test_modeldump_callback
89 
90  ! Helper subroutine to initialise the random seed (based on the clock)
91  subroutine init_random_seed()
92  integer :: i, n, clock
93  integer, dimension(:), allocatable :: seed
94 
95  call random_seed(size = n)
96  allocate(seed(n))
97 
98  call system_clock(count=clock)
99 
100  seed = clock + 37 * (/ (i - 1, i = 1, n) /)
101  call random_seed(put=seed)
102 
103  deallocate(seed)
104  end subroutine init_random_seed
105 end module test_terminationcheck_mod
106 
107 ! Driver for termination checking tests
109  use fruit, only : init_fruit, run_test_case, fruit_summary
111 
112  implicit none
113 
114  call init_fruit
115  call run_test_case(test_timestep_callback, "Test time-stepping completion")
116  call run_test_case(test_consolidation_callback, "Test consolidation completion")
117  call run_test_case(test_modeldump_callback, "Test model dump completion")
118  call fruit_summary
test_terminationcheck_mod::test_modeldump_callback
subroutine test_modeldump_callback()
Definition: test_terminationcheck.F90:63
test_terminationcheck_mod
Definition: test_terminationcheck.F90:2
test_terminationcheck_mod::test_consolidation_callback
subroutine test_consolidation_callback()
Definition: test_terminationcheck.F90:37
test_terminationcheck_mod::max_tests
integer, parameter max_tests
Definition: test_terminationcheck.F90:8
test_terminationcheck_mod::init_random_seed
subroutine init_random_seed()
Definition: test_terminationcheck.F90:92
terminationcheck_mod
This component will check for termination conditions at stages of the model run and terminate that sp...
Definition: terminationcheck.F90:3
state_mod::model_state_type
The ModelState which represents the current state of a run.
Definition: state.F90:39
test_terminationcheck_mod::test_timestep_callback
subroutine test_timestep_callback()
Definition: test_terminationcheck.F90:14
test_terminationcheck_driver
program test_terminationcheck_driver
Definition: test_terminationcheck.F90:108
terminationcheck_mod::timestep_callback
subroutine timestep_callback(current_state)
Timestep hook which is called at each timestep to determine whether or not to terminate timestep iter...
Definition: terminationcheck.F90:92
state_mod
The model state which represents the current state of a run.
Definition: state.F90:2