Octopus
algorithm.F90
Go to the documentation of this file.
1!! Copyright (C) 2020 M. Oliveira
2!!
3!! This Source Code Form is subject to the terms of the Mozilla Public
4!! License, v. 2.0. If a copy of the MPL was not distributed with this
5!! file, You can obtain one at https://mozilla.org/MPL/2.0/.
6!!
7
8#include "global.h"
9
23! The following pseudocode shows how multiple algorithms, one per system, are executed:
24! @code
25! repeat
26! for all systems do
27! algo_op ← next algorithmic operation
28! break ← f alse
29! while not break do
30! if algo_op != update interactions then
31! execute algorithmic operation
32! algo_op ← next algorithmic operation
33! else
34! try updating interactions
35! if interactions updated then
36! algo_op ← next algorithmic operation
37! end if
38! break ← true
39! end if
40! end while
41! end for
42! until all algorithms finished
43! @endcode
44!
45! Note: The above codeblock is not enabled for doxygen, as the formatting is broken in doxygen.
46!
47!TODO: the concept of granularity also needs to be explained more in detail.
48module algorithm_oct_m
49 use debug_oct_m
50 use global_oct_m
53 use loct_oct_m
55
56 implicit none
57
58 private
59 public :: &
63
64 integer, parameter, public :: ALGO_LABEL_LEN = 50
65
71 character(len=ALGO_LABEL_LEN) :: id
73 character(len=ALGO_LABEL_LEN) :: label
75
76 !# doc_start generic_algorithmic_operations
79 character(len=ALGO_LABEL_LEN), public, parameter :: &
80 SKIP = 'SKIP', &
81 update_couplings = 'UPDATE_COUPLINGS', &
82 update_interactions = 'UPDATE_INTERACTIONS', &
83 iteration_done = 'ITERATION_DONE', &
84 rewind_algorithm = 'REWIND_ALGORITHM'
85
86 type(algorithmic_operation_t), public, parameter :: &
87 OP_SKIP = algorithmic_operation_t(skip, 'Skipping algorithmic operation'), &
88 op_update_couplings = algorithmic_operation_t(update_couplings, 'Algorithmic operation - Updating couplings'), &
89 op_update_interactions = algorithmic_operation_t(update_interactions, 'Algorithmic operation - Updating interactions'), &
92 !# doc_end
93
94
97 private
98 contains
99 procedure :: get_next => algorithm_iterator_get_next
100 end type algorithm_iterator_t
101
102
106 !
107 type, extends(linked_list_t), abstract :: algorithm_t
108 private
109 type(algorithm_iterator_t), public :: iter
110 type(algorithmic_operation_t) :: current_ops
111
112 type(algorithmic_operation_t), public :: start_operation
115
116 type(algorithmic_operation_t), public :: final_operation
119
120 integer, public :: algo_steps
127
128 logical :: iteration_done
129
130 type(iteration_counter_t), public :: iteration
131 float :: start_time = m_zero
132 float, public :: elapsed_time = m_zero
133
134 contains
135 procedure :: add_operation => algorithm_add_operation
136 procedure :: do_operation => algorithm_do_operation
137 procedure :: update_elapsed_time => algorithm_update_elapsed_time
138 procedure :: rewind => algorithm_rewind
139 procedure :: next => algorithm_next
140 procedure :: get_current_operation => algorithm_get_current_operation
141 procedure(algorithm_finished), deferred :: finished
142 procedure(algorithm_init_iteration_counters), deferred :: init_iteration_counters
143 end type algorithm_t
144
145 abstract interface
146
147 !
148 logical function algorithm_finished(this)
149 import :: algorithm_t
150 class(algorithm_t), intent(in) :: this
151 end function algorithm_finished
152
156 import :: algorithm_t
157 class(algorithm_t), intent(inout) :: this
159 end interface
160
161contains
162
163 ! ---------------------------------------------------------
165 !
166 subroutine algorithm_add_operation(this, operation)
167 class(algorithm_t), intent(inout) :: this
168 type(algorithmic_operation_t), intent(in) :: operation
169
171
172 call this%add_copy(operation)
173
175 end subroutine algorithm_add_operation
176
177 ! ---------------------------------------------------------
179 !
180 logical function algorithm_do_operation(this, operation) result(done)
181 class(algorithm_t), intent(inout) :: this
182 type(algorithmic_operation_t), intent(in) :: operation
184 ! By default no algorithm specific operation is implemented in the algorithm
185 ! class. Child classes that wish to change this behaviour should override
186 ! this method.
187 done = .false.
188
189 end function algorithm_do_operation
190
191 ! ---------------------------------------------------------
195 class(algorithm_t), intent(inout) :: this
198
199 this%elapsed_time = loct_clock() - this%start_time
202 end subroutine algorithm_update_elapsed_time
203
204 ! ---------------------------------------------------------
206 !
207 subroutine algorithm_rewind(this)
208 class(algorithm_t), intent(inout) :: this
209
210 push_sub(algorithm_rewind)
211
212 ! Note that both lines are necessary to get the first element of the list.
213 ! For more information, see the linked_list_t implementation.
214 call this%iter%start(this)
215 call this%next()
216 this%start_time = loct_clock()
217
218 pop_sub(algorithm_rewind)
219 end subroutine algorithm_rewind
221 ! ---------------------------------------------------------
224 subroutine algorithm_next(this)
225 class(algorithm_t), intent(inout) :: this
227 push_sub(algorithm_next)
228
229 this%current_ops = this%iter%get_next()
230
231 pop_sub(algorithm_next)
232 end subroutine algorithm_next
233
234 ! ---------------------------------------------------------
236 !
237 type(algorithmic_operation_t) function algorithm_get_current_operation(this) result(operation)
238 class(algorithm_t), intent(in) :: this
241
242 operation = this%current_ops
243
246
247 ! ---------------------------------------------------------
251 !
252 function algorithm_iterator_get_next(this) result(operation)
253 class(algorithm_iterator_t), intent(inout) :: this
254 type(algorithmic_operation_t) :: operation
255
257
258 select type (ptr => this%get_next_ptr())
259 class is (algorithmic_operation_t)
260 operation = ptr
261 class default
262 assert(.false.)
263 end select
266 end function algorithm_iterator_get_next
267
268end module algorithm_oct_m
269
270!! Local Variables:
271!! mode: f90
272!! coding: utf-8
273!! End:
indicate whether the algorithm has finished one time step
Definition: algorithm.F90:232
initializes the algorithm and system iteration counters
Definition: algorithm.F90:239
This module implements the basic elements defining algorithms.
Definition: algorithm.F90:132
character(len=algo_label_len), parameter, public update_interactions
Definition: algorithm.F90:163
character(len=algo_label_len), parameter, public rewind_algorithm
Definition: algorithm.F90:163
type(algorithmic_operation_t), parameter, public op_iteration_done
Definition: algorithm.F90:170
character(len=algo_label_len), parameter, public update_couplings
Definition: algorithm.F90:163
type(algorithmic_operation_t), parameter, public op_rewind_algorithm
Definition: algorithm.F90:170
type(algorithmic_operation_t) function algorithm_get_current_operation(this)
return the current algorithmic operation.
Definition: algorithm.F90:322
subroutine algorithm_rewind(this)
Reset the algorithm to the first operation.
Definition: algorithm.F90:292
type(algorithmic_operation_t) function algorithm_iterator_get_next(this)
Get the next algorithmic operation from the iterator.
Definition: algorithm.F90:337
type(algorithmic_operation_t), parameter, public op_update_couplings
Definition: algorithm.F90:170
character(len=algo_label_len), parameter, public iteration_done
Definition: algorithm.F90:163
subroutine algorithm_add_operation(this, operation)
add an algorithmic operation to the list
Definition: algorithm.F90:251
subroutine algorithm_update_elapsed_time(this)
The elapsed time is used for the output of run time information.
Definition: algorithm.F90:279
subroutine algorithm_next(this)
move to the next algorithmic operation
Definition: algorithm.F90:309
logical function algorithm_do_operation(this, operation)
try to perform one operation of the algorithm. If successfull return .true.
Definition: algorithm.F90:265
type(algorithmic_operation_t), parameter, public op_update_interactions
Definition: algorithm.F90:170
real(8), parameter, public m_zero
Definition: global.F90:167
This module implements fully polymorphic linked lists, and some specializations thereof.
Definition: linked_list.F90:96
Iterator to loop over the algorithmic operations of an algorithm.
Definition: algorithm.F90:180
An algorithm is a list of algorithmic operations executed sequentially.
Definition: algorithm.F90:191
Descriptor of one algorithmic operation.
Definition: algorithm.F90:154
This class implements an iterator for the polymorphic linked list.
This class implements a linked list of unlimited polymorphic values.