Neko 1.99.11
A portable framework for high-order spectral element flow simulations
Loading...
Searching...
No Matches
interpolation.f90
Go to the documentation of this file.
1! Copyright (c) 2021-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!> Routines to interpolate between different spaces
36 use num_types, only : rp
38 use fast3d, only : setup_intp
39 use tensor, only : tnsr3d
40 use tensor_cpu, only : tnsr3d_cpu
41 use space, only : space_t, operator(.eq.), gl, gll
42 use utils, only : neko_error
43 use, intrinsic :: iso_c_binding
44 implicit none
45 private
46
47 !> Interpolation between two \ref space::space_t.
48 !! @details
49 !! This type implements functionality to interpolate between a pair of spaces.
50 !! Simply put, given some data of form (lx1, lx1, lx1, nelem) we can map it to
51 !! (lx2, lx2, lx2, nelem), corresponding to a different polynomial order in
52 !! each element.
53 type, public :: interpolator_t
54 !> First space.
55 type(space_t), pointer :: xh
56 !> Second space.
57 type(space_t), pointer :: yh
58 !> Interpolation weights from Xh to Yh.
59 real(kind=rp), allocatable :: xh_to_yh(:,:), xh_to_yht(:,:)
60 !> Interpolation weights from Yh to Xh.
61 real(kind=rp), allocatable :: yh_to_xh(:,:), yh_to_xht(:,:)
62 !> Device pointer for Xh_to_Yh.
63 type(c_ptr) :: xh_yh_d = c_null_ptr
64 !> Device pointer for Xh_to_YhT.
65 type(c_ptr) :: xh_yht_d = c_null_ptr
66 !> Device pointer for Yh_to_Xh.
67 type(c_ptr) :: yh_xh_d = c_null_ptr
68 !> Device pointer for Yh_to_XhT.
69 type(c_ptr) :: yh_xht_d = c_null_ptr
70 contains
71
72 !> Constructor.
73 procedure, pass(this) :: init => interpolator_init
74 !> Destructor.
75 procedure, pass(this) :: free => interpolator_free
76 !> Interpolate an array to one of Xh or Yh.
77 procedure, pass(this) :: map => interpolator_map
78 !> Interpolate an array to one of Xh or Yh on the host.
79 procedure, pass(this) :: map_host => interpolator_map_host
80
81 end type interpolator_t
82
83contains
84
85 !> Constructor to initialize with two different spaces.
86 !> @param Xh The first space.
87 !> @param Xh The second space.
88 subroutine interpolator_init(this, Xh, Yh)
89 class(interpolator_t), intent(inout), target :: this
90 type(space_t), intent(inout), target :: Xh
91 type(space_t), intent(inout), target :: Yh
92 integer :: deg_derivate
93
94 call this%free()
95
96 allocate(this%Xh_to_Yh(yh%lx, xh%lx))
97 allocate(this%Xh_to_YhT(xh%lx, yh%lx))
98 allocate(this%Yh_to_Xh(xh%lx, yh%lx))
99 allocate(this%Yh_to_XhT(yh%lx, xh%lx))
100
101 if (xh%t .eq. gll .and. yh%t .eq. gll) then
102 else if ((xh%t .eq. gl .and. yh%t .eq. gll) .or. &
103 (yh%t .eq. gl .and. xh%t .eq. gll)) then
104 else
105 call neko_error('Unsupported interpolation')
106 end if
107 deg_derivate = 0
108 call setup_intp(this%Xh_to_Yh, this%Xh_to_YhT, &
109 yh%zg, xh%zg, yh%lx, xh%lx, deg_derivate)
110 call setup_intp(this%Yh_to_Xh, this%Yh_to_XhT, &
111 xh%zg, yh%zg, xh%lx, yh%lx, deg_derivate)
112
113 this%Xh => xh
114 this%Yh => yh
115 if (neko_bcknd_device .eq. 1) then
116 call device_map(this%Xh_to_Yh, this%Xh_Yh_d, yh%lx*xh%lx)
117 call device_map(this%Xh_to_YhT, this%Xh_YhT_d, yh%lx*xh%lx)
118 call device_map(this%Yh_to_Xh, this%Yh_Xh_d, yh%lx*xh%lx)
119 call device_map(this%Yh_to_XhT, this%Yh_XhT_d, yh%lx*xh%lx)
120 call device_memcpy(this%Xh_to_Yh, this%Xh_Yh_d, yh%lx*xh%lx, &
121 host_to_device, sync = .false.)
122 call device_memcpy(this%Xh_to_YhT, this%Xh_YhT_d, yh%lx*xh%lx, &
123 host_to_device, sync = .false.)
124 call device_memcpy(this%Yh_to_Xh, this%Yh_Xh_d, yh%lx*xh%lx, &
125 host_to_device, sync = .false.)
126 call device_memcpy(this%Yh_to_XhT, this%Yh_XhT_d, yh%lx*xh%lx, &
127 host_to_device, sync = .true.)
128 end if
129
130 end subroutine interpolator_init
131
132 subroutine interpolator_free(this)
133 class(interpolator_t), intent(inout) :: this
134
135 if (allocated(this%Xh_to_Yh)) then
136 if (neko_bcknd_device .eq. 1) then
137 call device_unmap(this%Xh_to_Yh, this%Xh_Yh_d)
138 end if
139 deallocate(this%Xh_to_Yh)
140 end if
141 if (allocated(this%Xh_to_YhT)) then
142 if (neko_bcknd_device .eq. 1) then
143 call device_unmap(this%Xh_to_YhT, this%Xh_YhT_d)
144 end if
145 deallocate(this%Xh_to_YhT)
146 end if
147 if (allocated(this%Yh_to_Xh)) then
148 if (neko_bcknd_device .eq. 1) then
149 call device_unmap(this%Yh_to_Xh, this%Yh_Xh_d)
150 end if
151 deallocate(this%Yh_to_Xh)
152 end if
153 if (allocated(this%Yh_to_XhT)) then
154 if (neko_bcknd_device .eq. 1) then
155 call device_unmap(this%Yh_to_XhT, this%Yh_XhT_d)
156 end if
157 deallocate(this%Yh_to_XhT)
158 end if
159
160 nullify(this%Xh)
161 nullify(this%Yh)
162
163 end subroutine interpolator_free
164
165 !> Interpolates an array to one of Xh or Yh.
166 !! @param x Original array.
167 !! @param y Interpolated array.
168 !! @param nel Number of elements in the mesh.
169 !! @param to_space The space to interpolate to, must be either Xh or Yh.
170 subroutine interpolator_map(this, y, x, nel, to_space)
171 class(interpolator_t), intent(inout) :: this
172 integer :: nel
173 type(space_t) :: to_space
174 real(kind=rp), intent(in) :: x(this%Xh%lx, this%Xh%lx, this%Xh%lx, nel)
175 real(kind=rp), intent(inout) :: y(this%Yh%lx, this%Yh%lx, this%Yh%lx, nel)
176 if (to_space .eq. this%Yh) then
177 call tnsr3d(y, this%Yh%lx, x, &
178 this%Xh%lx, this%Yh_to_XhT, &
179 this%Yh_to_Xh, this%Yh_to_Xh, nel)
180 else if (to_space .eq. this%Xh) then
181 call tnsr3d(y, this%Xh%lx, x, &
182 this%Yh%lx, this%Yh_to_Xh, &
183 this%Yh_to_XhT, this%Yh_to_XhT, nel)
184 else
185 call neko_error('Invalid interpolation')
186 end if
187 end subroutine interpolator_map
188
189
190 !> Interpolates an array to one of Xh or Yh on host.
191 !! @param x Original array.
192 !! @param y Interpolated array.
193 !! @param nel Number of elements in the mesh.
194 !! @param to_space The space to interpolate to, must be either Xh or Yh.
195 !! @note Not optimized for performance, should only be used during init.
196 subroutine interpolator_map_host(this, y, x, nel, to_space)
197 class(interpolator_t), intent(inout) :: this
198 integer :: nel
199 type(space_t) :: to_space
200 real(kind=rp), intent(inout) :: x(this%Xh%lx, this%Xh%lx, this%Xh%lx, nel)
201 real(kind=rp), intent(inout) :: y(this%Yh%lx, this%Yh%lx, this%Yh%lx, nel)
202 if (to_space .eq. this%Yh) then
203 call tnsr3d_cpu(y, this%Yh%lx, x, &
204 this%Xh%lx, this%Yh_to_XhT, &
205 this%Yh_to_Xh, this%Yh_to_Xh, nel)
206 else if (to_space .eq. this%Xh) then
207 call tnsr3d_cpu(y, this%Xh%lx, x, &
208 this%Yh%lx, this%Yh_to_Xh, &
209 this%Yh_to_XhT, this%Yh_to_XhT, nel)
210 else
211 call neko_error('Invalid interpolation')
212 end if
213 end subroutine interpolator_map_host
214
215
216end module interpolation
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
Device abstraction, common interface for various accelerators.
Definition device.F90:34
integer, parameter, public host_to_device
Definition device.F90:48
Fast diagonalization methods from NEKTON.
Definition fast3d.f90:61
subroutine, public setup_intp(jh, jht, z_to, z_from, n_to, n_from, derivative)
Compute interpolation weights for points z_to using values at points z_from.
Definition fast3d.f90:246
Routines to interpolate between different spaces.
subroutine interpolator_map_host(this, y, x, nel, to_space)
Interpolates an array to one of Xh or Yh on host.
subroutine interpolator_map(this, y, x, nel, to_space)
Interpolates an array to one of Xh or Yh.
subroutine interpolator_init(this, xh, yh)
Constructor to initialize with two different spaces.
subroutine interpolator_free(this)
NEKTON map.
Definition map.f90:3
Build configurations.
integer, parameter neko_bcknd_device
integer, parameter, public rp
Global precision used in computations.
Definition num_types.f90:14
Defines a function space.
Definition space.f90:34
integer, parameter, public gll
Definition space.f90:50
integer, parameter, public gl
Definition space.f90:50
subroutine, public tnsr3d_cpu(v, nv, u, nu, a, bt, ct, nelv)
Three-dimensional tensor product over a list of elements.
Tensor operations.
Definition tensor.f90:61
subroutine, public tnsr3d(v, nv, u, nu, a, bt, ct, nelv)
Tensor product performed on nelv elements.
Definition tensor.f90:234
Utilities.
Definition utils.f90:35
Interpolation between two space::space_t.
The function space for the SEM solution fields.
Definition space.f90:64