Neko-TOP
A portable framework for high-order spectral element flow toplogy optimization.
Loading...
Searching...
No Matches
RAMP_mapping.f90
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!
36 use num_types, only: rp
37 use mapping, only: mapping_t
38 use json_module, only: json_file
39 use field, only: field_t
40 use coefs, only: coef_t
41 use neko_config, only: neko_bcknd_device
42 use device_ramp_mapping, only: device_convex_down_ramp_mapping_apply, &
43 device_convex_down_ramp_mapping_apply_backward, &
44 device_convex_up_ramp_mapping_apply, &
45 device_convex_up_ramp_mapping_apply_backward
46 use json_utils, only: json_get, json_get_or_default
47 implicit none
48 private
49
77
78 type, public, extends(mapping_t) :: ramp_mapping_t
80 real(kind=rp) :: f_min
82 real(kind=rp) :: f_max
84 real(kind=rp) :: q
87 logical :: convex_up
88
89 contains
91 procedure, pass(this) :: init => ramp_mapping_init_from_json
93 procedure, pass(this) :: init_from_attributes => &
94 ramp_mapping_init_from_attributes
96 procedure, pass(this) :: free => ramp_mapping_free
98 procedure, pass(this) :: forward_mapping => ramp_forward_mapping
100 procedure, pass(this) :: backward_mapping => ramp_backward_mapping
101 end type ramp_mapping_t
102
103contains
104
106 subroutine ramp_mapping_init_from_json(this, json, coef)
107 class(ramp_mapping_t), intent(inout) :: this
108 type(json_file), intent(inout) :: json
109 type(coef_t), intent(inout) :: coef
110 real(kind=rp) :: f_min, f_max, q
111 logical :: convex_up
112
113 call json_get_or_default(json, 'f_min', f_min, 0.0_rp)
114 call json_get(json, 'f_max', f_max)
115 call json_get_or_default(json, 'q', q, 1.0_rp)
116 call json_get_or_default(json, 'convex_up', convex_up, .false.)
117
118 call this%init_base(json, coef)
119 call this%init_from_attributes(coef, f_min, f_max, q, &
120 convex_up)
121
122 end subroutine ramp_mapping_init_from_json
123
125 subroutine ramp_mapping_init_from_attributes(this, coef, f_min, f_max, q, &
126 convex_up)
127 class(ramp_mapping_t), intent(inout) :: this
128 type(coef_t), intent(inout) :: coef
129 real(kind=rp), intent(in) :: f_min, f_max, q
130 logical, intent(in) :: convex_up
131
132 this%f_min = f_min
133 this%f_max = f_max
134 this%q = q
135 this%convex_up = convex_up
136
137 end subroutine ramp_mapping_init_from_attributes
138
140 subroutine ramp_mapping_free(this)
141 class(ramp_mapping_t), intent(inout) :: this
142
143 call this%free_base()
144
145 end subroutine ramp_mapping_free
146
151 subroutine ramp_forward_mapping(this, X_out, X_in)
152 class(ramp_mapping_t), intent(inout) :: this
153 type(field_t), intent(in) :: X_in
154 type(field_t), intent(inout) :: X_out
155
156 if (this%convex_up .eqv. .true.) then
157 call convex_up_ramp_mapping_apply(this%f_min, this%f_max, &
158 this%q, x_out, x_in)
159 else
160 call convex_down_ramp_mapping_apply(this%f_min, this%f_max, &
161 this%q, x_out, x_in)
162 end if
163
164 end subroutine ramp_forward_mapping
165
166
172 subroutine ramp_backward_mapping(this, sens_out, sens_in, X_in)
173 class(ramp_mapping_t), intent(inout) :: this
174 type(field_t), intent(in) :: X_in
175 type(field_t), intent(in) :: sens_in
176 type(field_t), intent(inout) :: sens_out
177
178 if (this%convex_up .eqv. .true.) then
179 call convex_up_ramp_mapping_apply_backward(this%f_min, this%f_max, &
180 this%q, sens_out, sens_in, x_in)
181 else
182 call convex_down_ramp_mapping_apply_backward(this%f_min, this%f_max, &
183 this%q, sens_out, sens_in, x_in)
184 end if
185
186 end subroutine ramp_backward_mapping
187
194 subroutine convex_down_ramp_mapping_apply(f_min, f_max, q, X_out, X_in)
195 real(kind=rp), intent(in) :: q, f_min, f_max
196 type(field_t), intent(in) :: x_in
197 type(field_t), intent(inout) :: X_out
198 integer :: n, i
199
200 ! x_out = f_min + (f_max - f_min) * x_in / (1 + q * (1 - x_in) )
201
202 n = x_in%dof%size()
203 if (neko_bcknd_device .eq. 1) then
204 call device_convex_down_ramp_mapping_apply(f_min, f_max, q, &
205 x_out%x_d, x_in%x_d, n)
206 else
207 do i = 1, n
208 x_out%x(i,1,1,1) = f_min + (f_max - f_min) * &
209 x_in%x(i,1,1,1) / (1.0_rp + q * (1.0_rp - x_in%x(i,1,1,1) ) )
210 end do
211 end if
212
213 end subroutine convex_down_ramp_mapping_apply
214
215
223 subroutine convex_down_ramp_mapping_apply_backward(f_min, f_max, q, &
224 sens_out, sens_in, X_in)
225 real(kind=rp), intent(in) :: f_min, f_max, q
226 type(field_t), intent(in) :: x_in
227 type(field_t), intent(in) :: sens_in
228 type(field_t), intent(inout) :: sens_out
229 integer :: n, i
230
231 ! df/dx_in = df/dx_out * dx_out/dx_in
232
233 ! dx_out/dx_in = (f_min - f_max) * (q + 1) / (1 - q*(x - 1))**2
234
235 n = x_in%dof%size()
236
237 if (neko_bcknd_device .eq. 1) then
238 call device_convex_down_ramp_mapping_apply_backward(f_min, f_max, q, &
239 sens_out%x_d, sens_in%x_d, x_in%x_d, n)
240 else
241 do i = 1, n
242 sens_out%x(i,1,1,1) = (f_max - f_min) * (q + 1.0_rp) / &
243 ((1.0_rp - q * (x_in%x(i,1,1,1) - 1.0_rp))**2) * &
244 sens_in%x(i,1,1,1)
245 end do
246 end if
247
248 end subroutine convex_down_ramp_mapping_apply_backward
249
256 subroutine convex_up_ramp_mapping_apply(f_min, f_max, q, X_out, X_in)
257 real(kind=rp), intent(in) :: f_min, f_max, q
258 type(field_t), intent(in) :: x_in
259 type(field_t), intent(inout) :: X_out
260 integer :: n, i
261
262 ! x_out = f_min + (f_max - f_min) * x_in * (q + 1) / (x_in + q)
263
264 n = x_in%dof%size()
265
266 if (neko_bcknd_device .eq. 1) then
267 call device_convex_up_ramp_mapping_apply(f_min, f_max, q, &
268 x_out%x_d, x_in%x_d, n)
269 else
270 do i = 1, n
271 x_out%x(i,1,1,1) = f_min + (f_max - f_min) * &
272 x_in%x(i,1,1,1) * (1.0_rp + q ) / (x_in%x(i,1,1,1) + q)
273 end do
274 end if
275
276
277 end subroutine convex_up_ramp_mapping_apply
278
279
287 subroutine convex_up_ramp_mapping_apply_backward(f_min, f_max, q, &
288 sens_out, sens_in, X_in)
289 real(kind=rp), intent(in) :: f_min, f_max, q
290 type(field_t), intent(in) :: x_in
291 type(field_t), intent(in) :: sens_in
292 type(field_t), intent(inout) :: sens_out
293 integer :: n, i
294
295 ! df/dx_in = df/dx_out * dx_out/dx_in
296
297 ! dx_out/dx_in = (f_min - f_max) * (q + 1) / (q + x)**2
298
299 n = x_in%dof%size()
300
301 if (neko_bcknd_device .eq. 1) then
302 call device_convex_up_ramp_mapping_apply_backward(f_min, f_max, q, &
303 sens_out%x_d, sens_in%x_d, x_in%x_d, n)
304 else
305 do i = 1, n
306 sens_out%x(i,1,1,1) = (f_max - f_min) * (q + 1.0_rp) / &
307 ( (x_in%x(i,1,1,1) + q)**2) * &
308 sens_in%x(i,1,1,1)
309 end do
310 end if
311
312 end subroutine convex_up_ramp_mapping_apply_backward
313end module ramp_mapping
Mappings to be applied to a scalar field.
Definition mapping.f90:35
A RAMP mapping of coefficients.
Base abstract class for mapping.
Definition mapping.f90:45
A RAMP mapping of coefficients This is the standard RAMP described in https://doi....