Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
spectral_vanishing_viscosity.f90
Go to the documentation of this file.
1! Copyright (c) 2026, 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 disclaimer
13! in the documentation and/or other materials provided with the
14! distribution.
15!
16! * Neither the name of the authors nor the names of its contributors may
17! be used to endorse or promote products derived from this software
18! without specific prior written permission.
19!
20! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21! AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22! IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23! ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24! LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25! CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26! SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27! INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28! CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29! ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30! POSSIBILITY OF SUCH DAMAGE.
31!
32!> Data and filter construction for spectral vanishing viscosity.
34 use num_types, only : rp
36 use registry, only : neko_registry
37 use field, only : field_t
39 use json_module, only : json_file
41 use coefs, only : coef_t
42 use math, only : cfill, copy, rzero, col2
45 use, intrinsic :: iso_c_binding, only : c_ptr, c_null_ptr
47 implicit none
48 private
49
50 character(len=3), parameter :: known_directions(7) = [character(len=3) :: &
51 "rst", "rs", "rt", "st", "r", "s", "t"]
52 character(len=10), parameter :: known_formulations(2) = &
53 [character(len=10) :: &
54 "one-sided", "factorized"]
55 character(len=5), parameter :: known_nu_types(2) = [character(len=5) :: &
56 "value", "field"]
57 character(len=5), parameter :: known_kernel_types(1) = [character(len=5) :: &
58 "power"]
59
60
61 !> Spectral vanishing viscosity configuration and coefficients.
62 type, public :: svv_t
63 !> Element-local modal low-pass filter.
65 !> Reference directions in which the filter is applied.
66 character(len=:), allocatable :: direction
67 !> SVV operator formulation.
68 character(len=:), allocatable :: formulation
69 !> Kernel type defining the modal transfer function.
70 character(len=:), allocatable :: kernel_type
71 !> SEM coefficients associated with this SVV object.
72 type(coef_t), pointer :: coef => null()
73 !> Density-weighted SVV viscosity.
74 real(kind=rp), allocatable :: h1(:,:,:,:)
75 type(c_ptr) :: h1_d = c_null_ptr
76 !> Identity matrix used to disable filtering in selected directions.
77 real(kind=rp), allocatable :: ident(:,:)
78 type(c_ptr) :: ident_d = c_null_ptr
79 !> Complementary derivatives, (I - F) D, for factorized SVV.
80 real(kind=rp), allocatable :: br(:,:), bs(:,:), bt(:,:)
81 type(c_ptr) :: br_d = c_null_ptr
82 type(c_ptr) :: bs_d = c_null_ptr
83 type(c_ptr) :: bt_d = c_null_ptr
84 !> Name and pointer for a field-valued SVV viscosity.
85 character(len=:), allocatable :: nue_field_name
86 type(field_t), pointer :: nue => null()
87 !> Whether the viscosity field is updated every time step.
88 logical :: tvar_h1 = .false.
89 contains
90 procedure, pass(this) :: init => svv_init_from_json
91 procedure, pass(this) :: free => svv_free
92 procedure, pass(this) :: update => svv_update_h1
93 end type svv_t
94
95contains
96
97 !> Construct an SVV object from case parameters.
98 !! @param this SVV object.
99 !! @param json JSON object containing an `svv` dictionary.
100 !! @param coef SEM coefficients.
101 !! @param rho Density field.
102 subroutine svv_init_from_json(this, json, coef, rho)
103 class(svv_t), intent(inout) :: this
104 type(json_file), intent(inout) :: json
105 type(coef_t), intent(in), target :: coef
106 type(field_t), intent(in) :: rho
107 real(kind=rp), allocatable :: transfer(:)
108 real(kind=rp) :: nu_val, power_coef, exponent_factor
109 character(len=:), allocatable :: nu_type, direction, formulation
110 integer :: i, lx
111
112
113 call this%free()
114 this%coef => coef
115 lx = coef%Xh%lx
116
117 call json_get_or_default(json, "svv.formulation", formulation, &
118 "factorized")
119 this%formulation = trim(formulation)
120 if (all(trim(this%formulation) .ne. known_formulations)) then
121 call neko_type_error("The SVV formulation", this%formulation, &
123 end if
124
125 call json_get_or_default(json, "svv.direction", direction, "rst")
126 this%direction = trim(direction)
127 if (all(trim(this%direction) .ne. known_directions)) then
128 call neko_type_error("The SVV direction", this%direction, &
130 end if
131
132 allocate(transfer(lx))
133
134 call json_get(json, "svv.kernel.type", this%kernel_type)
135 select case (trim(this%kernel_type))
136 case ("power")
137 call json_get_or_lookup(json, "svv.kernel.power_coefficient", power_coef)
138 if (power_coef .eq. 0.0_rp) then
139 transfer = 0.0_rp
140 else
141 if (this%formulation .eq. "factorized") then
142 exponent_factor = 0.5_rp
143 else
144 exponent_factor = 1.0_rp
145 end if
146 do i = 1, lx
147 transfer(i) = 1.0_rp - ((i - 1.0_rp) / (lx - 1.0_rp)) ** &
148 (exponent_factor * (lx - 1.0_rp) * power_coef)
149 end do
150 end if
151 case default
152 call neko_type_error("The SVV kernel type `", &
153 trim(this%kernel_type), known_kernel_types)
154 end select
155
156 call this%filter%init_from_components(coef, "nonBoyd", transfer)
157 deallocate(transfer)
158
159 allocate(this%ident(lx, lx))
160 this%ident = 0.0_rp
161 do i = 1, lx
162 this%ident(i, i) = 1.0_rp
163 end do
164
165 if (this%formulation .eq. "factorized") then
167 end if
168
169 allocate(this%h1(lx, lx, lx, coef%msh%nelv))
170 if (neko_bcknd_device .eq. 1) then
171 call device_map(this%ident, this%ident_d, lx * lx)
172 call device_memcpy(this%ident, this%ident_d, lx * lx, &
173 host_to_device, sync = .false.)
174 call device_map(this%h1, this%h1_d, coef%dof%size())
175 call device_rzero(this%h1_d, coef%dof%size())
176 else
177 call rzero(this%h1, coef%dof%size())
178 end if
179
180 call json_get(json, "svv.nu.type", nu_type)
181 select case (trim(nu_type))
182 case ("value")
183 call json_get_or_lookup(json, "svv.nu.value", nu_val)
184 if (neko_bcknd_device .eq. 1) then
185 call device_cfill(this%h1_d, nu_val, coef%dof%size())
186 call device_col2(this%h1_d, rho%x_d, coef%dof%size())
187 else
188 call cfill(this%h1, nu_val, coef%dof%size())
189 call col2(this%h1, rho%x, coef%dof%size())
190 end if
191 case ("field")
192 call json_get_or_default(json, "svv.nu.time_variable", &
193 this%tvar_h1, .true.)
194 call json_get(json, "svv.nu.field_name", this%nue_field_name)
195 if (neko_registry%field_exists(this%nue_field_name)) then
196 this%nue => neko_registry%get_field(this%nue_field_name)
197 call svv_copy_viscosity(this, rho)
198 else if (.not. this%tvar_h1) then
199 call neko_error("The static SVV viscosity field `" // &
200 this%nue_field_name // "` does not exist")
201 end if
202 case default
203 call neko_error("Invalid svv.nu.type: " // trim(nu_type))
204 end select
205
206 if (allocated(nu_type)) deallocate(nu_type)
207 if (allocated(direction)) deallocate(direction)
208 if (allocated(formulation)) deallocate(formulation)
209 end subroutine svv_init_from_json
210
211 !> Build the complementary derivative matrices used by factorized SVV.
212 !! @param this SVV object.
214 class(svv_t), intent(inout) :: this
215 real(kind=rp), allocatable :: complementary_filter(:,:)
216 integer :: lx
217
218 lx = this%coef%Xh%lx
219 allocate(complementary_filter(lx, lx))
220 allocate(this%Br(lx, lx), this%Bs(lx, lx), this%Bt(lx, lx))
221
222 complementary_filter = this%ident - this%filter%fh
223 this%Br = 0.0_rp
224 this%Bs = 0.0_rp
225 this%Bt = 0.0_rp
226 if (index(this%direction, "r") > 0) then
227 this%Br = matmul(complementary_filter, this%coef%Xh%dx)
228 end if
229 if (index(this%direction, "s") > 0) then
230 this%Bs = matmul(complementary_filter, this%coef%Xh%dy)
231 end if
232 if (index(this%direction, "t") > 0) then
233 this%Bt = matmul(complementary_filter, this%coef%Xh%dz)
234 end if
235
236 if (neko_bcknd_device .eq. 1) then
237 call device_map(this%Br, this%Br_d, lx * lx)
238 call device_map(this%Bs, this%Bs_d, lx * lx)
239 call device_map(this%Bt, this%Bt_d, lx * lx)
240 call device_memcpy(this%Br, this%Br_d, lx * lx, host_to_device, &
241 sync = .false.)
242 call device_memcpy(this%Bs, this%Bs_d, lx * lx, host_to_device, &
243 sync = .false.)
244 call device_memcpy(this%Bt, this%Bt_d, lx * lx, host_to_device, &
245 sync = .true.)
246 end if
247
248 deallocate(complementary_filter)
250
251 !> Update a time-varying, field-valued SVV viscosity.
252 !! @param this SVV object.
253 !! @param rho Density field.
254 !! @param tstep Current time-step index.
255 subroutine svv_update_h1(this, rho, tstep)
256 class(svv_t), intent(inout) :: this
257 type(field_t), intent(in) :: rho
258 integer, intent(in) :: tstep
259
260 if (.not. this%tvar_h1) return
261 if (.not. associated(this%nue)) then
262 if (.not. neko_registry%field_exists(this%nue_field_name)) then
263 call neko_error("The SVV viscosity field `" // &
264 this%nue_field_name // "` does not exist at time step " // &
265 trim(adjustl(to_string(tstep))))
266 end if
267 this%nue => neko_registry%get_field(this%nue_field_name)
268 end if
269 call svv_copy_viscosity(this, rho)
270 end subroutine svv_update_h1
271
272 !> Copy the configured viscosity field and multiply it by density.
273 !! @param this SVV object.
274 !! @param rho Density field.
275 subroutine svv_copy_viscosity(this, rho)
276 class(svv_t), intent(inout) :: this
277 type(field_t), intent(in) :: rho
278
279 if (neko_bcknd_device .eq. 1) then
280 call device_copy(this%h1_d, this%nue%x_d, this%coef%dof%size())
281 call device_col2(this%h1_d, rho%x_d, this%coef%dof%size())
282 else
283 call copy(this%h1, this%nue%x, this%coef%dof%size())
284 call col2(this%h1, rho%x, this%coef%dof%size())
285 end if
286 end subroutine svv_copy_viscosity
287
288 !> Release all resources owned by an SVV object.
289 !! @param this SVV object.
290 subroutine svv_free(this)
291 class(svv_t), intent(inout) :: this
292
293 call this%filter%free()
294 if (allocated(this%h1)) then
295 if (neko_bcknd_device .eq. 1) then
296 call device_unmap(this%h1, this%h1_d)
297 end if
298 deallocate(this%h1)
299 end if
300 if (allocated(this%ident)) then
301 if (neko_bcknd_device .eq. 1) then
302 call device_unmap(this%ident, this%ident_d)
303 end if
304 deallocate(this%ident)
305 end if
306 if (allocated(this%Br)) then
307 if (neko_bcknd_device .eq. 1) call device_unmap(this%Br, this%Br_d)
308 deallocate(this%Br)
309 end if
310 if (allocated(this%Bs)) then
311 if (neko_bcknd_device .eq. 1) call device_unmap(this%Bs, this%Bs_d)
312 deallocate(this%Bs)
313 end if
314 if (allocated(this%Bt)) then
315 if (neko_bcknd_device .eq. 1) call device_unmap(this%Bt, this%Bt_d)
316 deallocate(this%Bt)
317 end if
318 if (allocated(this%direction)) deallocate(this%direction)
319 if (allocated(this%formulation)) deallocate(this%formulation)
320 if (allocated(this%kernel_type)) deallocate(this%kernel_type)
321 if (allocated(this%nue_field_name)) deallocate(this%nue_field_name)
322 nullify(this%coef)
323 nullify(this%nue)
324 this%h1_d = c_null_ptr
325 this%ident_d = c_null_ptr
326 this%Br_d = c_null_ptr
327 this%Bs_d = c_null_ptr
328 this%Bt_d = c_null_ptr
329 this%tvar_h1 = .false.
330 end subroutine svv_free
331
332 !> Convert an integer to a compact string.
333 !! @param value Integer value to convert.
334 function to_string(value) result(string)
335 integer, intent(in) :: value
336 character(len=32) :: string
337 write(string, '(I0)') value
338 end function to_string
339
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
Unmap a Fortran array from a device (deassociate and free).
Definition device.F90:89
Retrieves a parameter by name or assigns a provided default value. In the latter case also adds the m...
Retrieves a parameter by name or throws an error.
Coefficients.
Definition coef.f90:34
subroutine, public device_rzero(a_d, n, strm)
Zero a real vector.
subroutine, public device_copy(a_d, b_d, n, strm)
Copy a vector .
subroutine, public device_col2(a_d, b_d, n, strm)
Vector multiplication .
subroutine, public device_cfill(a_d, c, n, strm)
Set all elements to a constant c .
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
Implements elementwise_filter_t.
Defines a field.
Definition field.f90:34
Filter to be applied to a scalar field.
Definition filter.f90:38
Utilities for retrieving parameters from the case files.
Definition math.F90:60
subroutine, public cfill(a, c, n)
Set all elements to a constant c .
Definition math.F90:611
subroutine, public col2(a, b, n)
Vector multiplication .
Definition math.F90:1095
subroutine, public copy(a, b, n)
Copy a vector .
Definition math.F90:305
subroutine, public rzero(a, n)
Zero a real vector.
Definition math.F90:249
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Defines a registry for storing solution fields.
Definition registry.f90:34
type(registry_t), target, public neko_registry
Global field registry.
Definition registry.f90:158
Data and filter construction for spectral vanishing viscosity.
subroutine svv_free(this)
Release all resources owned by an SVV object.
subroutine svv_update_h1(this, rho, tstep)
Update a time-varying, field-valued SVV viscosity.
subroutine svv_copy_viscosity(this, rho)
Copy the configured viscosity field and multiply it by density.
subroutine svv_build_complementary_derivatives(this)
Build the complementary derivative matrices used by factorized SVV.
subroutine svv_init_from_json(this, json, coef, rho)
Construct an SVV object from case parameters.
character(len=10), dimension(2), parameter known_formulations
character(len=5), dimension(1), parameter known_kernel_types
character(len=5), dimension(2), parameter known_nu_types
character(len=3), dimension(7), parameter known_directions
character(len=32) function to_string(value)
Convert an integer to a compact string.
Utilities.
Definition utils.f90:35
subroutine, public neko_type_error(base_type, wrong_type, known_types)
Reports an error allocating a type for a particular base pointer class.
Definition utils.f90:415
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition coef.f90:136
Implements the elementwise filter for SEM.
Spectral vanishing viscosity configuration and coefficients.