Neko  0.9.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
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  if (compute_control .eq. "fluid_output") then
150  call json_get(this%case%params, 'case.fluid.output_control', &
151  compute_control)
152  call json_get(this%case%params, 'case.fluid.output_value', &
153  compute_value)
154  end if
155 
156  ! We default to output whenever we execute
157  call json_get_or_default(json, "output_control", output_control, &
158  compute_control)
159  call json_get_or_default(json, "output_value", output_value, &
160  compute_value)
161 
162  if (output_control == "global") then
163  call json_get(this%case%params, 'case.fluid.output_control', &
164  output_control)
165  call json_get(this%case%params, 'case.fluid.output_value', &
166  output_value)
167  end if
168 
169  call json_get_or_default(json, "order", order, -1)
170  this%order = order
171 
172  call this%preprocess_controller%init(case%end_time, preprocess_control, &
173  preprocess_value)
174  call this%compute_controller%init(case%end_time, compute_control, &
175  compute_value)
176  call this%output_controller%init(case%end_time, output_control, &
177  output_value)
178 
179  end subroutine simulation_component_init_base
180 
182  subroutine simulation_component_free_base(this)
183  class(simulation_component_t), intent(inout) :: this
184 
185  nullify(this%case)
186  end subroutine simulation_component_free_base
187 
192  subroutine simulation_component_preprocess_wrapper(this, t, tstep)
193  class(simulation_component_t), intent(inout) :: this
194  real(kind=rp), intent(in) :: t
195  integer, intent(in) :: tstep
196 
197  if (this%preprocess_controller%check(t, tstep)) then
198  call this%preprocess_(t, tstep)
199  call this%preprocess_controller%register_execution()
200  end if
201  end subroutine simulation_component_preprocess_wrapper
202 
207  subroutine simulation_component_compute_wrapper(this, t, tstep)
208  class(simulation_component_t), intent(inout) :: this
209  real(kind=rp), intent(in) :: t
210  integer, intent(in) :: tstep
211 
212  if (this%compute_controller%check(t, tstep)) then
213  call this%compute_(t, tstep)
214  call this%compute_controller%register_execution()
215  end if
216  end subroutine simulation_component_compute_wrapper
217 
221  subroutine simulation_component_restart_wrapper(this, t)
222  class(simulation_component_t), intent(inout) :: this
223  real(kind=rp), intent(in) :: t
224 
225  call this%compute_controller%set_counter(t)
226  call this%output_controller%set_counter(t)
227  call this%restart_(t)
228 
229  end subroutine simulation_component_restart_wrapper
230 
233  subroutine restart_(this, t)
234  class(simulation_component_t), intent(inout) :: this
235  real(kind=rp), intent(in) :: t
236 
237  ! Do nothing
238  end subroutine restart_
239 
243  subroutine preprocess_(this, t, tstep)
244  class(simulation_component_t), intent(inout) :: this
245  real(kind=rp), intent(in) :: t
246  integer, intent(in) :: tstep
247 
248  ! Do nothing
249  end subroutine preprocess_
250 
254  subroutine compute_(this, t, tstep)
255  class(simulation_component_t), intent(inout) :: this
256  real(kind=rp), intent(in) :: t
257  integer, intent(in) :: tstep
258 
259  ! Do nothing
260  end subroutine compute_
261 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:54
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:45
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
Implements output_controller_t
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....