Neko  0.8.99
A portable framework for high-order spectral element flow simulations
simulation_component.f90
Go to the documentation of this file.
1 ! Copyright (c) 2023, The Neko Authors
2 ! All rights reserved.
3 !
4 ! Redistribution and use in source and binary forms, with or without
5 ! modification, are permitted provided that the following conditions
6 ! are met:
7 !
8 ! * Redistributions of source code must retain the above copyright
9 ! notice, this list of conditions and the following disclaimer.
10 !
11 ! * Redistributions in binary form must reproduce the above
12 ! copyright notice, this list of conditions and the following
13 ! disclaimer in the documentation and/or other materials provided
14 ! with the distribution.
15 !
16 ! * Neither the name of the authors nor the names of its
17 ! contributors may be used to endorse or promote products derived
18 ! from this software without specific prior written permission.
19 !
20 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
24 ! COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25 ! INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26 ! BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27 ! LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28 ! CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 ! LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
30 ! ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 ! POSSIBILITY OF SUCH DAMAGE.
32 !
33 !
39  use num_types, only : rp
40  use json_module, only : json_file
41  use case, only : case_t
44  implicit none
45  private
46 
48  type, abstract, public :: simulation_component_t
50  type(case_t), pointer :: case
52  type(time_based_controller_t) :: preprocess_controller
54  type(time_based_controller_t) :: compute_controller
56  type(time_based_controller_t) :: output_controller
58  integer :: order
59  contains
61  procedure, pass(this) :: init_base => simulation_component_init_base
63  procedure, pass(this) :: free_base => simulation_component_free_base
66  procedure, pass(this) :: restart => simulation_component_restart_wrapper
69  procedure, pass(this) :: preprocess => &
73  procedure, pass(this) :: compute => simulation_component_compute_wrapper
75  procedure(simulation_component_init), pass(this), deferred :: init
77  procedure(simulation_component_free), pass(this), deferred :: free
79  procedure, pass(this) :: preprocess_
81  procedure, pass(this) :: compute_
83  procedure, pass(this) :: restart_
84  end type simulation_component_t
85 
88  class(simulation_component_t), allocatable :: simcomp
90 
91 
92  abstract interface
93 
96  subroutine simulation_component_init(this, json, case)
97  import simulation_component_t, json_file, case_t
98  class(simulation_component_t), intent(inout) :: this
99  type(json_file), intent(inout) :: json
100  class(case_t), intent(inout), target :: case
101  end subroutine simulation_component_init
102  end interface
103 
104  abstract interface
105 
106  subroutine simulation_component_free(this)
108  class(simulation_component_t), intent(inout) :: this
109  end subroutine simulation_component_free
110  end interface
111 
112  interface
113 
118  module subroutine simulation_component_factory(object, json, case)
119  class(simulation_component_t), allocatable, intent(inout) :: object
120  type(json_file), intent(inout) :: json
121  class(case_t), intent(inout), target :: case
122  end subroutine simulation_component_factory
123  end interface
124 
125  public :: simulation_component_factory
126 
127 contains
129  subroutine simulation_component_init_base(this, json, case)
130  class(simulation_component_t), intent(inout) :: this
131  type(json_file), intent(inout) :: json
132  class(case_t), intent(inout), target :: case
133  character(len=:), allocatable :: preprocess_control, compute_control, output_control
134  real(kind=rp) :: preprocess_value, compute_value, output_value
135  integer :: order
136 
137  this%case => case
138 
139  ! We default to preprocess every time-step
140  call json_get_or_default(json, "preprocess_control", preprocess_control, &
141  "tsteps")
142  call json_get_or_default(json, "preprocess_value", preprocess_value, 1.0_rp)
143 
144  ! We default to compute every time-step
145  call json_get_or_default(json, "compute_control", compute_control, &
146  "tsteps")
147  call json_get_or_default(json, "compute_value", compute_value, 1.0_rp)
148 
149  ! We default to output whenever we execute
150  call json_get_or_default(json, "output_control", output_control, &
151  compute_control)
152  call json_get_or_default(json, "output_value", output_value, &
153  compute_value)
154 
155 
156  if (output_control == "global") then
157  call json_get(this%case%params, 'case.fluid.output_control', &
158  output_control)
159  call json_get(this%case%params, 'case.fluid.output_value', &
160  output_value)
161  end if
162 
163  call json_get_or_default(json, "order", order, -1)
164  this%order = order
165 
166  call this%preprocess_controller%init(case%end_time, preprocess_control, &
167  preprocess_value)
168  call this%compute_controller%init(case%end_time, compute_control, &
169  compute_value)
170  call this%output_controller%init(case%end_time, output_control, &
171  output_value)
172 
173  end subroutine simulation_component_init_base
174 
176  subroutine simulation_component_free_base(this)
177  class(simulation_component_t), intent(inout) :: this
178 
179  nullify(this%case)
180  end subroutine simulation_component_free_base
181 
186  subroutine simulation_component_preprocess_wrapper(this, t, tstep)
187  class(simulation_component_t), intent(inout) :: this
188  real(kind=rp), intent(in) :: t
189  integer, intent(in) :: tstep
190 
191  if (this%preprocess_controller%check(t, tstep)) then
192  call this%preprocess_(t, tstep)
193  call this%preprocess_controller%register_execution()
194  end if
195  end subroutine simulation_component_preprocess_wrapper
196 
201  subroutine simulation_component_compute_wrapper(this, t, tstep)
202  class(simulation_component_t), intent(inout) :: this
203  real(kind=rp), intent(in) :: t
204  integer, intent(in) :: tstep
205 
206  if (this%compute_controller%check(t, tstep)) then
207  call this%compute_(t, tstep)
208  call this%compute_controller%register_execution()
209  end if
210  end subroutine simulation_component_compute_wrapper
211 
215  subroutine simulation_component_restart_wrapper(this, t)
216  class(simulation_component_t), intent(inout) :: this
217  real(kind=rp), intent(in) :: t
218 
219  call this%compute_controller%set_counter(t)
220  call this%output_controller%set_counter(t)
221  call this%restart_(t)
222 
223  end subroutine simulation_component_restart_wrapper
224 
227  subroutine restart_(this, t)
228  class(simulation_component_t), intent(inout) :: this
229  real(kind=rp), intent(in) :: t
230 
231  ! Do nothing
232  end subroutine restart_
233 
237  subroutine preprocess_(this, t, tstep)
238  class(simulation_component_t), intent(inout) :: this
239  real(kind=rp), intent(in) :: t
240  integer, intent(in) :: tstep
241 
242  ! Do nothing
243  end subroutine preprocess_
244 
248  subroutine compute_(this, t, tstep)
249  class(simulation_component_t), intent(inout) :: this
250  real(kind=rp), intent(in) :: t
251  integer, intent(in) :: tstep
252 
253  ! Do nothing
254  end subroutine compute_
255 end module simulation_component
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Definition: json_utils.f90:53
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:44
The common constructor using a JSON dictionary.
Defines a simulation case.
Definition: case.f90:34
Utilities for retrieving parameters from the case files.
Definition: json_utils.f90:34
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Simulation components are objects that encapsulate functionality that can be fit to a particular comp...
subroutine preprocess_(this, t, tstep)
Dummy preprocessing function.
subroutine simulation_component_free_base(this)
Destructor for the simulation_component_t (base) class.
subroutine simulation_component_restart_wrapper(this, t)
Wrapper for calling set_counter_ based for the controllers. Serves as the public interface.
subroutine simulation_component_preprocess_wrapper(this, t, tstep)
Wrapper for calling preprocess_ based on the preprocess_controller. Serves as the public interface.
subroutine compute_(this, t, tstep)
Dummy compute function.
subroutine simulation_component_init_base(this, json, case)
Simulation component factory. Both constructs and initializes the object.
subroutine restart_(this, t)
Dummy restart function.
subroutine simulation_component_compute_wrapper(this, t, tstep)
Wrapper for calling compute_ based on the compute_controller. Serves as the public interface.
Contains the time_based_controller_t type.
Base abstract class for simulation components.
A helper type that is needed to have an array of polymorphic objects.
A utility type for determening whether an action should be executed based on the current time value....