Neko 1.99.6
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!
37 use num_types, only : rp
43 use vector, only : vector_t
44 use, intrinsic :: iso_c_binding
45 implicit none
46 private
74 type(ext_time_scheme_t) :: ext
75 type(ab_time_scheme_t) :: ab
76 type(bdf_time_scheme_t) :: bdf
77
79 type(vector_t) :: advection_coeffs
81 type(vector_t) :: diffusion_coeffs
84 integer :: ndiff = 0
87 integer :: nadv = 0
89 integer :: advection_time_order = 3
91 integer :: diffusion_time_order
93 type(c_ptr) :: advection_coeffs_d = c_null_ptr
95 type(c_ptr) :: diffusion_coeffs_d = c_null_ptr
96
97 contains
99 procedure, pass(this) :: init => time_scheme_controller_init
101 procedure, pass(this) :: free => time_scheme_controller_free
103 procedure, pass(this) :: set_coeffs => &
106
107contains
108
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
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
143 implicit none
144 class(time_scheme_controller_t) :: this
145 real(kind=rp), intent(inout), 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:243
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:12
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 ...