Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
time_scheme_controller.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!> Compound scheme for the advection and diffusion operators in a transport
34!! equation.
37 use num_types, only : rp
43 use vector, only : vector_t
44 use, intrinsic :: iso_c_binding
45 implicit none
46 private
47 !> Implements the logic to compute the time coefficients for the advection and
48 !! diffusion operators in a transport equation.
49 !! @details
50 !! Uses the BDF scheme for the diffusion, where as the term for advection
51 !! the scheme depends on the orders of the BDF and advection schemes.
52 !! - Order 1 advection
53 !! - BDF1 for diffusion -> Adam-Bashforth scheme.
54 !! - Order 2 advection
55 !! - BDF1 for diffusion -> Adam-Bashforth scheme.
56 !! - BDF2 for diffusion -> Explcit extrapolation scheme.
57 !! - Order 3 advection
58 !! - BDF1 for diffusion -> Adam-Bashforth scheme.
59 !! - BDF2 for diffusion -> Modified explcit extrapolation scheme.
60 !! - BDF3 for diffusion -> Explciit extrapolation scheme.
61 !! The order of the BDF scheme in the above logic is set by the user, whereas
62 !! the advection scheme is set to forward Euler when BDF is order 1,
63 !! and otherwise to a 3rd order scheme (excluding the first 2 timesteps).
64 !! This means that some of the options in the above list never get realized,
65 !! particularly order 2 and 3 advection for 1st order BDF. They remain in the
66 !! code so as to have the orinigal Nek5000 logic in place for possible
67 !! adoption in the future.
68 !! An important detail here is the handling of the first timesteps where a
69 !! high-order scheme cannot be constructed. The parameters `nadv` and `ndiff`,
70 !! which are initialized to 0, hold the current order of the respective
71 !! scheme.
72 !! @note the advection scheme also applies to source terms.
74 type(ext_time_scheme_t) :: ext
75 type(ab_time_scheme_t) :: ab
76 type(bdf_time_scheme_t) :: bdf
77
78 !> Time coefficients for the advection operator
79 type(vector_t) :: advection_coeffs
80 !> Time coefficients for the diffusion operator
81 type(vector_t) :: diffusion_coeffs
82 !> Controls the actual order of the diffusion scheme,
83 !! e.g. 1 at the first time-step
84 integer :: ndiff = 0
85 !> Controls the actual order of the advection scheme,
86 !! e.g. 1 at the first time-step
87 integer :: nadv = 0
88 !> Order of the advection scheme
89 integer :: advection_time_order = 3
90 !> Order of the diffusion scheme
91 integer :: diffusion_time_order
92 !> Device pointer for `advection_coeffs`
93 type(c_ptr) :: advection_coeffs_d = c_null_ptr
94 !> Device pointer for `diffusion_coeffs`
95 type(c_ptr) :: diffusion_coeffs_d = c_null_ptr
96
97 contains
98 !> Constructor
99 procedure, pass(this) :: init => time_scheme_controller_init
100 !> Destructor
101 procedure, pass(this) :: free => time_scheme_controller_free
102 !> Set the time coefficients
103 procedure, pass(this) :: set_coeffs => &
106
107contains
108
109 !> Contructor
110 !! @param torder Desired order of the scheme: 1, 2, 3.
111 !! This sets the order for the diffusion scheme only.
112 subroutine time_scheme_controller_init(this, torder)
113 implicit none
114 class(time_scheme_controller_t) :: this
115 integer :: torder
116
117 call this%free()
118
119 this%diffusion_time_order = torder
120
121 ! Force 1st order advection when diffusion is 1st order
122 if (torder .eq. 1) then
123 this%advection_time_order = 1
124 end if
125
126 call this%advection_coeffs%init(4)
127 call this%diffusion_coeffs%init(4)
128 end subroutine time_scheme_controller_init
129
130 !> Destructor
132 implicit none
133 class(time_scheme_controller_t) :: this
134
135 call this%advection_coeffs%free()
136 call this%diffusion_coeffs%free()
137 end subroutine time_scheme_controller_free
138
139 !> Set the time coefficients
140 !! @details Implements all necessary logic to handle
141 !! @param dt Timestep values, first element is the current timestep.
143 implicit none
144 class(time_scheme_controller_t) :: this
145 real(kind=rp), intent(in), dimension(10) :: dt
146 real(kind=rp), dimension(4) :: adv_coeffs_old
147 real(kind=rp), dimension(4) :: diff_coeffs_old
148
149 associate( &
150 nadv => this%nadv, &
151 ndiff => this%ndiff, &
152 adv_coeffs => this%advection_coeffs, &
153 adv_coeffs_d => this%advection_coeffs_d, &
154 diff_coeffs => this%diffusion_coeffs, &
155 diff_coeffs_d => this%diffusion_coeffs_d)
156
157 adv_coeffs_old = adv_coeffs%x
158 diff_coeffs_old = diff_coeffs%x
159
160 ! On unified memory backends the coefficient arrays may alias
161 ! their device pointers, and the host-side rewrite below races
162 ! in-flight device work reading them. Rewriting identical values
163 ! is benign, so only drain the device when the coefficients can
164 ! actually change (scheme order still ramping up, or a varying
165 ! time step)
166 if (neko_bcknd_device .eq. 1) then
167 if ((ndiff .lt. this%diffusion_time_order) .or. &
168 (nadv .lt. this%advection_time_order) .or. &
169 (dt(1) .ne. dt(2))) then
170 call device_sync()
171 end if
172 end if
173
174 ! Increment the order of the scheme if below time_order
175 ndiff = ndiff + 1
176 ndiff = min(ndiff, this%diffusion_time_order)
177 nadv = nadv + 1
178 nadv = min(nadv, this%advection_time_order)
179
180 call this%bdf%compute_coeffs(diff_coeffs%x, dt, ndiff)
181
182 if (nadv .eq. 1) then
183 ! Forward euler
184 call this%ext%compute_coeffs(adv_coeffs%x, dt, nadv)
185 else if (nadv .eq. 2) then
186 if (ndiff .eq. 1) then
187 ! 2nd order Adam-Bashforth, currently never used
188 call this%ab%compute_coeffs(adv_coeffs%x, dt, nadv)
189 else
190 ! Linear extrapolation
191 call this%ext%compute_coeffs(adv_coeffs%x, dt, nadv)
192 end if
193 else if (nadv .eq. 3) then
194 if (ndiff .eq. 1) then
195 ! 3rd order Adam-Bashforth, currently never used
196 call this%ab%compute_coeffs(adv_coeffs%x, dt, nadv)
197 else if (ndiff .eq. 2) then
198 ! The modified EXT scheme
199 call this%ext%compute_modified_coeffs(adv_coeffs%x, dt)
200 else
201 ! Quadratic extrapolation
202 call this%ext%compute_coeffs(adv_coeffs%x, dt, nadv)
203 end if
204 end if
205
206 if (neko_bcknd_device .eq. 1) then
207 if (maxval(abs(adv_coeffs%x - adv_coeffs_old)) .gt. 1e-10_rp) then
208 call adv_coeffs%copy_from(host_to_device, sync = .false.)
209 end if
210
211 if (maxval(abs(diff_coeffs%x - diff_coeffs_old)) .gt. 1e-10_rp) then
212 call diff_coeffs%copy_from(host_to_device, sync = .false.)
213 end if
214 end if
215 end associate
216
218end module time_scheme_controller
Map a Fortran array to a device (allocate and associate).
Definition device.F90:83
Copy data between host and device (or device and device).
Definition device.F90:72
Synchronize a device or stream.
Definition device.F90:119
Adam-Bashforth scheme for time integration.
Backward-differencing scheme for time integration.
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
subroutine, public device_free(x_d)
Deallocate memory on the device.
Definition device.F90:281
Explicit extrapolation scheme for time integration.
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Compound scheme for the advection and diffusion operators in a transport equation.
subroutine time_scheme_controller_free(this)
Destructor.
subroutine time_scheme_controller_init(this, torder)
Contructor.
subroutine time_scheme_controller_set_coeffs(this, dt)
Set the time coefficients.
Defines a vector.
Definition vector.f90:34
Explicit Adam-Bashforth scheme for time integration.
Implicit backward-differencing scheme for time integration.
Explicit extrapolation scheme for time integration.
Implements the logic to compute the time coefficients for the advection and diffusion operators in a ...