Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
source_term.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!> Implements the `source_term_t` type and a wrapper `source_term_wrapper_t`.
35 use num_types, only : rp, dp
36 use utils, only : neko_typename_len
37 use coefs, only : coef_t
38 use field_list, only : field_list_t
39 use json_module, only : json_file
40 use time_state, only : time_state_t
41 implicit none
42 private
43
44 !> Base abstract type for source terms.
45 type, abstract, public :: source_term_t
46 !> The fields to be updated with the source term values
47 type(field_list_t) :: fields
48 !> Coefficients for the SEM.
49 type(coef_t), pointer :: coef => null()
50 !> Start time for adding the source term.
51 real(kind=rp) :: start_time = 0.0_rp
52 !> End time for adding the source term.
53 real(kind=rp) :: end_time = huge(0.0_rp)
54 !> Whether the owning scheme may extrapolate this term in time together
55 !! with its other explicit terms.
56 logical :: extrapolate = .true.
57 contains
58 !> Constructor for the source_term_t (base) type.
59 procedure, pass(this) :: init_base => source_term_init_base
60 !> Destructor for the source_term_t (base) type.
61 procedure, pass(this) :: free_base => source_term_free_base
62 !> Executes `compute_` based on time conditions.
63 procedure, pass(this) :: compute => source_term_compute_wrapper
64 !> The common constructor using a JSON object.
65 procedure(source_term_init), pass(this), deferred :: init
66 !> Destructor.
67 procedure(source_term_free), pass(this), deferred :: free
68 !> Computes the source term and adds the result to `fields`.
69 procedure(source_term_compute), pass(this), deferred :: compute_
70 end type source_term_t
71
72
73 !> A helper type that is needed to have an array of polymorphic objects
74 type, public :: source_term_wrapper_t
75 !> Wrapped polymorphic source term.
76 class(source_term_t), allocatable :: source_term
77 contains
78 !> Destructor.
79 procedure, pass(this) :: free => source_term_wrapper_free
81
82 abstract interface
83 !> The common constructor using a JSON object.
84 !! @param json The JSON object for the source.
85 !! @param fields A list of fields for adding the source values.
86 !! @param coef The SEM coeffs.
87 !! @param variable_name The name of the variable where the source term
88 !! acts.
89 subroutine source_term_init(this, json, fields, coef, variable_name)
90 import source_term_t, json_file, field_list_t, coef_t
91 class(source_term_t), intent(inout) :: this
92 type(json_file), intent(inout) :: json
93 type(field_list_t), intent(in), target :: fields
94 type(coef_t), intent(in), target :: coef
95 character(len=*), intent(in) :: variable_name
96 end subroutine source_term_init
97 end interface
98
99 abstract interface
100 !> Destructor.
101 subroutine source_term_free(this)
102 import source_term_t
103 class(source_term_t), intent(inout) :: this
104 end subroutine source_term_free
105 end interface
106
107 abstract interface
108 !> Computes the source term and adds the result to `fields`.
109 !! @param t The time value.
110 !! @param tstep The current time-step.
111 subroutine source_term_compute(this, time)
113 class(source_term_t), intent(inout) :: this
114 type(time_state_t), intent(in) :: time
115 end subroutine source_term_compute
116 end interface
117
118 interface
119 !> Source term factory. Both constructs and initializes the object.
120 !! @param object The object to be initialized.
121 !! @param json JSON object initializing the source term.
122 !! @param fields The list of fields updated by the source term.
123 !! @param coef The SEM coefficients.
124 !! @param variable_name The name of the variable
125 !! where the source term acts.
126 module subroutine source_term_factory(object, json, fields, coef, &
127 variable_name)
128 class(source_term_t), allocatable, intent(inout) :: object
129 type(json_file), intent(inout) :: json
130 type(field_list_t), intent(inout) :: fields
131 type(coef_t), intent(inout) :: coef
132 character(len=*), intent(in) :: variable_name
133 end subroutine source_term_factory
134 end interface
135
136 interface
137 !> Source term allocator.
138 !! @param object The object to be allocated.
139 !! @param type_name The name of the type to allocate.
140 module subroutine source_term_allocator(object, type_name)
141 class(source_term_t), allocatable, intent(inout) :: object
142 character(len=:), allocatable, intent(in) :: type_name
143 end subroutine source_term_allocator
144 end interface
145
146 !
147 ! Machinery for injecting user-defined types
148 !
149
150 !> Interface for an object allocator.
151 !! Implemented in the user modules, should allocate the `obj` to the custom
152 !! user type.
153 abstract interface
154 subroutine source_term_allocate(obj)
155 import source_term_t
156 class(source_term_t), allocatable, intent(inout) :: obj
157 end subroutine source_term_allocate
158 end interface
159
160 interface
161 !> Called in user modules to add an allocator for custom types.
162 module subroutine register_source_term(type_name, allocator)
163 character(len=*), intent(in) :: type_name
164 procedure(source_term_allocate), pointer, intent(in) :: allocator
165 end subroutine register_source_term
166 end interface
167
168 ! A name-allocator pair for user-defined types. A helper type to define a
169 ! registry of custom allocators.
170 type allocator_entry
171 character(len=NEKO_TYPENAME_LEN) :: type_name
172 procedure(source_term_allocate), pointer, nopass :: allocator
173 end type allocator_entry
174
175 !> Registry of source term allocators for user-defined types
176 type(allocator_entry), allocatable :: source_term_registry(:)
177
178 !> The size of the `source_term_registry`
179 integer :: source_term_registry_size = 0
180
181 public :: source_term_factory, source_term_allocator, register_source_term, &
182 source_term_allocate
183
184contains
185
186 !> Constructor for the `source_term_t` (base) type.
187 !> @param fields A list of pointers to fields to be updated by the source
188 !! term.
189 !> @param coef SEM coefs.
190 !! @param start_time When to start adding the source term.
191 !! @param end_time When to stop adding the source term.
192 subroutine source_term_init_base(this, fields, coef, start_time, end_time)
193 class(source_term_t), intent(inout) :: this
194 type(field_list_t) :: fields
195 type(coef_t), intent(in), target :: coef
196 real(kind=rp), intent(in) :: start_time
197 real(kind=rp), intent(in) :: end_time
198 integer :: n_fields, i
199
200 this%coef => coef
201 this%start_time = start_time
202 this%end_time = end_time
203 n_fields = fields%size()
204
205 call this%fields%init(n_fields)
206
207 ! A lot of attribute nesting here due to Fortran needing wrapper types
208 ! but this is just pointer assignement for the fields.
209 do i = 1, n_fields
210 call this%fields%assign(i, fields%get(i))
211 end do
212 end subroutine source_term_init_base
213
214 !> Destructor for the `source_term_t` (base) type.
215 subroutine source_term_free_base(this)
216 class(source_term_t), intent(inout) :: this
217
218 call this%fields%free()
219 nullify(this%coef)
220 this%extrapolate = .true.
221 end subroutine source_term_free_base
222
223 !> Destructor for the `source_term_wrapper_t` type.
224 subroutine source_term_wrapper_free(this)
225 class(source_term_wrapper_t), intent(inout) :: this
226 integer :: n_fields, i
227
228 if (allocated(this%source_term)) then
229 call this%source_term%free()
230 deallocate(this%source_term)
231 end if
232 end subroutine source_term_wrapper_free
233
234 !> Executes `compute_` based on time conditions.
235 !! @param time Time state.
236 subroutine source_term_compute_wrapper(this, time)
237 class(source_term_t), intent(inout) :: this
238 type(time_state_t), intent(in) :: time
239 real(kind=dp) :: tol
240
241 tol = time%tolerance()
242 if (time%t + tol .ge. this%start_time .and. &
243 time%t - tol .le. this%end_time) then
244 call this%compute_(time)
245 end if
246
247 end subroutine source_term_compute_wrapper
248end module source_term
Computes the source term and adds the result to fields.
The common constructor using a JSON object.
Coefficients.
Definition coef.f90:34
integer, parameter, public dp
Definition num_types.f90:10
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Implements the source_term_t type and a wrapper source_term_wrapper_t.
subroutine source_term_wrapper_free(this)
Destructor for the source_term_wrapper_t type.
subroutine source_term_compute_wrapper(this, time)
Executes compute_ based on time conditions.
subroutine source_term_free_base(this)
Destructor for the source_term_t (base) type.
subroutine source_term_init_base(this, fields, coef, start_time, end_time)
Constructor for the source_term_t (base) type.
Module with things related to the simulation time.
Utilities.
Definition utils.f90:35
integer, parameter, public neko_typename_len
Definition utils.f90:44
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition coef.f90:136
field_list_t, To be able to group fields together
Base abstract type for source terms.
A helper type that is needed to have an array of polymorphic objects.
A struct that contains all info about the time, expand as needed.