Neko  0.8.1
A portable framework for high-order spectral element flow simulations
fluid_volflow.f90
Go to the documentation of this file.
1 ! Copyright (c) 2008-2020, UCHICAGO ARGONNE, LLC.
2 !
3 ! The UChicago Argonne, LLC as Operator of Argonne National
4 ! Laboratory holds copyright in the Software. The copyright holder
5 ! reserves all rights except those expressly granted to licensees,
6 ! and U.S. Government license rights.
7 !
8 ! Redistribution and use in source and binary forms, with or without
9 ! modification, are permitted provided that the following conditions
10 ! are met:
11 !
12 ! 1. Redistributions of source code must retain the above copyright
13 ! notice, this list of conditions and the disclaimer below.
14 !
15 ! 2. Redistributions in binary form must reproduce the above copyright
16 ! notice, this list of conditions and the disclaimer (as noted below)
17 ! in the documentation and/or other materials provided with the
18 ! distribution.
19 !
20 ! 3. Neither the name of ANL nor the names of its contributors
21 ! may be used to endorse or promote products derived from this software
22 ! without specific prior written permission.
23 !
24 ! THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 ! "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 ! LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
27 ! FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
28 ! UCHICAGO ARGONNE, LLC, THE U.S. DEPARTMENT OF
29 ! ENERGY OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ! SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
31 ! TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ! DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ! THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ! (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ! OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 !
37 ! Additional BSD Notice
38 ! ---------------------
39 ! 1. This notice is required to be provided under our contract with
40 ! the U.S. Department of Energy (DOE). This work was produced at
41 ! Argonne National Laboratory under Contract
42 ! No. DE-AC02-06CH11357 with the DOE.
43 !
44 ! 2. Neither the United States Government nor UCHICAGO ARGONNE,
45 ! LLC nor any of their employees, makes any warranty,
46 ! express or implied, or assumes any liability or responsibility for the
47 ! accuracy, completeness, or usefulness of any information, apparatus,
48 ! product, or process disclosed, or represents that its use would not
49 ! infringe privately-owned rights.
50 !
51 ! 3. Also, reference herein to any specific commercial products, process,
52 ! or services by trade name, trademark, manufacturer or otherwise does
53 ! not necessarily constitute or imply its endorsement, recommendation,
54 ! or favoring by the United States Government or UCHICAGO ARGONNE LLC.
55 ! The views and opinions of authors expressed
56 ! herein do not necessarily state or reflect those of the United States
57 ! Government or UCHICAGO ARGONNE, LLC, and shall
58 ! not be used for advertising or product endorsement purposes.
59 !
61  use operators
62  use num_types
63  use mathops
64  use krylov, only : ksp_t, ksp_monitor_t
65  use precon
66  use dofmap
67  use field
68  use coefs
70  use math
71  use comm
72  use neko_config
73  use device_math
74  use device_mathops
75  use gather_scatter, only : gs_t, gs_op_add
76  use json_module, only : json_file
77  use json_utils, only: json_get
80  use ax_product, only : ax_t
81  implicit none
82  private
83 
85  type, public :: fluid_volflow_t
86  integer :: flow_dir
87  logical :: avflow
88  real(kind=rp) :: flow_rate
89  real(kind=rp) :: dtlag = 0d0
90  real(kind=rp) :: bdlag = 0d0
91  type(field_t) :: u_vol, v_vol, w_vol, p_vol
92  real(kind=rp) :: domain_length, base_flow
94  type(scratch_registry_t) :: scratch
95  contains
96  procedure, pass(this) :: init => fluid_vol_flow_init
97  procedure, pass(this) :: free => fluid_vol_flow_free
98  procedure, pass(this) :: adjust => fluid_vol_flow
99  procedure, private, pass(this) :: compute => fluid_vol_flow_compute
100  end type fluid_volflow_t
101 
102 contains
103 
104  subroutine fluid_vol_flow_init(this, dm_Xh, params)
105  class(fluid_volflow_t), intent(inout) :: this
106  type(dofmap_t), intent(inout) :: dm_Xh
107  type(json_file), intent(inout) :: params
108  logical average, found
109  integer :: direction
110  real(kind=rp) :: rate
111 
112  call this%free()
113 
114  !Initialize vol_flow (if there is a forced volume flow)
115  call json_get(params, 'case.fluid.flow_rate_force.direction', direction)
116  call json_get(params, 'case.fluid.flow_rate_force.value', rate)
117  call json_get(params, 'case.fluid.flow_rate_force.use_averaged_flow',&
118  average)
119 
120  this%flow_dir = direction
121  this%avflow = average
122  this%flow_rate = rate
123 
124  if (this%flow_dir .ne. 0) then
125  call this%u_vol%init(dm_xh, 'u_vol')
126  call this%v_vol%init(dm_xh, 'v_vol')
127  call this%w_vol%init(dm_xh, 'w_vol')
128  call this%p_vol%init(dm_xh, 'p_vol')
129  end if
130 
131  this%scratch = scratch_registry_t(dm_xh, 3, 1)
132 
133  end subroutine fluid_vol_flow_init
134 
135  subroutine fluid_vol_flow_free(this)
136  class(fluid_volflow_t), intent(inout) :: this
137 
138  call this%u_vol%free()
139  call this%v_vol%free()
140  call this%w_vol%free()
141  call this%p_vol%free()
142 
143  call this%scratch%free()
144 
145  end subroutine fluid_vol_flow_free
146 
150  subroutine fluid_vol_flow_compute(this, u_res, v_res, w_res, p_res, &
151  ext_bdf, gs_Xh, c_Xh, rho, mu, bd, dt, &
152  bclst_dp, bclst_du, bclst_dv, bclst_dw, bclst_vel_res, &
153  Ax, ksp_prs, ksp_vel, pc_prs, pc_vel, prs_max_iter, vel_max_iter)
154  class(fluid_volflow_t), intent(inout) :: this
155  type(field_t), intent(inout) :: u_res, v_res, w_res, p_res
156  type(coef_t), intent(inout) :: c_Xh
157  type(gs_t), intent(inout) :: gs_Xh
158  type(time_scheme_controller_t), intent(inout) :: ext_bdf
159  type(bc_list_t), intent(inout) :: bclst_dp, bclst_du, bclst_dv, bclst_dw
160  type(bc_list_t), intent(inout) :: bclst_vel_res
161  class(ax_t), intent(inout) :: Ax
162  class(ksp_t), intent(inout) :: ksp_prs, ksp_vel
163  class(pc_t), intent(inout) :: pc_prs, pc_vel
164  real(kind=rp), intent(inout) :: bd
165  real(kind=rp), intent(in) :: rho, mu, dt
166  integer, intent(in) :: vel_max_iter, prs_max_iter
167  integer :: n, i
168  real(kind=rp) :: xlmin, xlmax
169  real(kind=rp) :: ylmin, ylmax
170  real(kind=rp) :: zlmin, zlmax
171  type(ksp_monitor_t) :: ksp_result
172  type(field_t), pointer :: ta1, ta2, ta3
173  integer :: temp_indices(3)
174 
175  call this%scratch%request_field(ta1, temp_indices(1))
176  call this%scratch%request_field(ta2, temp_indices(2))
177  call this%scratch%request_field(ta3, temp_indices(3))
178 
179 
180  associate(msh => c_xh%msh, p_vol => this%p_vol, &
181  u_vol => this%u_vol, v_vol => this%v_vol, w_vol => this%w_vol)
182 
183  n = c_xh%dof%size()
184  xlmin = glmin(c_xh%dof%x, n)
185  xlmax = glmax(c_xh%dof%x, n)
186  ylmin = glmin(c_xh%dof%y, n) ! for Y!
187  ylmax = glmax(c_xh%dof%y, n)
188  zlmin = glmin(c_xh%dof%z, n) ! for Z!
189  zlmax = glmax(c_xh%dof%z, n)
190  if (this%flow_dir.eq.1) this%domain_length = xlmax - xlmin
191  if (this%flow_dir.eq.2) this%domain_length = ylmax - ylmin
192  if (this%flow_dir.eq.3) this%domain_length = zlmax - zlmin
193 
194  if (neko_bcknd_device .eq. 1) then
195  call device_cfill(c_xh%h1_d, 1.0_rp/rho, n)
196  call device_rzero(c_xh%h2_d, n)
197  else
198  do i = 1, n
199  c_xh%h1(i,1,1,1) = 1.0_rp / rho
200  c_xh%h2(i,1,1,1) = 0.0_rp
201  end do
202  end if
203  c_xh%ifh2 = .false.
204 
205  ! Compute pressure
206 
207  if (this%flow_dir .eq. 1) then
208  call cdtp(p_res%x, c_xh%h1, c_xh%drdx, c_xh%dsdx, c_xh%dtdx, c_xh)
209  end if
210 
211  if (this%flow_dir .eq. 2) then
212  call cdtp(p_res%x, c_xh%h1, c_xh%drdy, c_xh%dsdy, c_xh%dtdy, c_xh)
213  end if
214 
215  if (this%flow_dir .eq. 3) then
216  call cdtp(p_res%x, c_xh%h1, c_xh%drdz, c_xh%dsdz, c_xh%dtdz, c_xh)
217  end if
218 
219  call gs_xh%op(p_res, gs_op_add)
220  call bc_list_apply_scalar(bclst_dp, p_res%x, n)
221  call pc_prs%update()
222  ksp_result = ksp_prs%solve(ax, p_vol, p_res%x, n, &
223  c_xh, bclst_dp, gs_xh, prs_max_iter)
224 
225  ! Compute velocity
226 
227  call opgrad(u_res%x, v_res%x, w_res%x, p_vol%x, c_xh)
228 
229  if ((neko_bcknd_hip .eq. 1) .or. (neko_bcknd_cuda .eq. 1) .or. &
230  (neko_bcknd_opencl .eq. 1)) then
231  call device_opchsign(u_res%x_d, v_res%x_d, w_res%x_d, msh%gdim, n)
232  call device_copy(ta1%x_d, c_xh%B_d, n)
233  call device_copy(ta2%x_d, c_xh%B_d, n)
234  call device_copy(ta3%x_d, c_xh%B_d, n)
235  else
236  call opchsign(u_res%x, v_res%x, w_res%x, msh%gdim, n)
237  call copy(ta1%x, c_xh%B, n)
238  call copy(ta2%x, c_xh%B, n)
239  call copy(ta3%x, c_xh%B, n)
240  end if
241  call bc_list_apply_vector(bclst_vel_res,&
242  ta1%x, ta2%x, ta3%x, n)
243 
244  ! add forcing
245 
246  if (neko_bcknd_device .eq. 1) then
247  if (this%flow_dir .eq. 1) then
248  call device_add2(u_res%x_d, ta1%x_d, n)
249  else if (this%flow_dir .eq. 2) then
250  call device_add2(v_res%x_d, ta2%x_d, n)
251  else if (this%flow_dir .eq. 3) then
252  call device_add2(w_res%x_d, ta3%x_d, n)
253  end if
254  else
255  if (this%flow_dir .eq. 1) then
256  call add2(u_res%x, ta1%x, n)
257  else if (this%flow_dir .eq. 2) then
258  call add2(v_res%x, ta2%x, n)
259  else if (this%flow_dir .eq. 3) then
260  call add2(w_res%x, ta3%x, n)
261  end if
262  end if
263 
264  if (neko_bcknd_device .eq. 1) then
265  call device_cfill(c_xh%h1_d, mu, n)
266  call device_cfill(c_xh%h2_d, rho * (bd / dt), n)
267  else
268  do i = 1, n
269  c_xh%h1(i,1,1,1) = mu
270  c_xh%h2(i,1,1,1) = rho * (bd / dt)
271  end do
272  end if
273  c_xh%ifh2 = .true.
274 
275  call gs_xh%op(u_res, gs_op_add)
276  call gs_xh%op(v_res, gs_op_add)
277  call gs_xh%op(w_res, gs_op_add)
278 
279  call bc_list_apply_vector(bclst_vel_res,&
280  u_res%x, v_res%x, w_res%x, n)
281  call pc_vel%update()
282 
283  ksp_result = ksp_vel%solve(ax, u_vol, u_res%x, n, &
284  c_xh, bclst_du, gs_xh, vel_max_iter)
285  ksp_result = ksp_vel%solve(ax, v_vol, v_res%x, n, &
286  c_xh, bclst_dv, gs_xh, vel_max_iter)
287  ksp_result = ksp_vel%solve(ax, w_vol, w_res%x, n, &
288  c_xh, bclst_dw, gs_xh, vel_max_iter)
289 
290  if (neko_bcknd_device .eq. 1) then
291  if (this%flow_dir .eq. 1) then
292  this%base_flow = &
293  device_glsc2(u_vol%x_d, c_xh%B_d, n) / this%domain_length
294  end if
295 
296  if (this%flow_dir .eq. 2) then
297  this%base_flow = &
298  device_glsc2(v_vol%x_d, c_xh%B_d, n) / this%domain_length
299  end if
300 
301  if (this%flow_dir .eq. 3) then
302  this%base_flow = &
303  device_glsc2(w_vol%x_d, c_xh%B_d, n) / this%domain_length
304  end if
305  else
306  if (this%flow_dir .eq. 1) then
307  this%base_flow = glsc2(u_vol%x, c_xh%B, n) / this%domain_length
308  end if
309 
310  if (this%flow_dir .eq. 2) then
311  this%base_flow = glsc2(v_vol%x, c_xh%B, n) / this%domain_length
312  end if
313 
314  if (this%flow_dir .eq. 3) then
315  this%base_flow = glsc2(w_vol%x, c_xh%B, n) / this%domain_length
316  end if
317  end if
318  end associate
319 
320  call this%scratch%relinquish_field(temp_indices)
321  end subroutine fluid_vol_flow_compute
322 
332  subroutine fluid_vol_flow(this, u, v, w, p, u_res, v_res, w_res, p_res, &
333  c_Xh, gs_Xh, ext_bdf, rho, mu, dt, &
334  bclst_dp, bclst_du, bclst_dv, bclst_dw, bclst_vel_res, &
335  Ax, ksp_prs, ksp_vel, pc_prs, pc_vel, prs_max_iter, vel_max_iter)
336 
337  class(fluid_volflow_t), intent(inout) :: this
338  type(field_t), intent(inout) :: u, v, w, p
339  type(field_t), intent(inout) :: u_res, v_res, w_res, p_res
340  type(coef_t), intent(inout) :: c_Xh
341  type(gs_t), intent(inout) :: gs_Xh
342  type(time_scheme_controller_t), intent(inout) :: ext_bdf
343  real(kind=rp), intent(in) :: rho, mu, dt
344  type(bc_list_t), intent(inout) :: bclst_dp, bclst_du, bclst_dv, bclst_dw
345  type(bc_list_t), intent(inout) :: bclst_vel_res
346  class(ax_t), intent(inout) :: Ax
347  class(ksp_t), intent(inout) :: ksp_prs, ksp_vel
348  class(pc_t), intent(inout) :: pc_prs, pc_vel
349  integer, intent(in) :: prs_max_iter, vel_max_iter
350  real(kind=rp) :: ifcomp, flow_rate, xsec
351  real(kind=rp) :: current_flow, delta_flow, base_flow, scale
352  integer :: n, ierr
353  type(field_t), pointer :: ta1, ta2, ta3
354  integer :: temp_indices(3)
355 
356  associate(u_vol => this%u_vol, v_vol => this%v_vol, &
357  w_vol => this%w_vol, p_vol => this%p_vol)
358 
359  n = c_xh%dof%size()
360 
361  ! If either dt or the backwards difference coefficient change,
362  ! then recompute base flow solution corresponding to unit forcing:
363 
364  ifcomp = 0.0_rp
365 
366  if (dt .ne. this%dtlag .or. ext_bdf%diffusion_coeffs(1) .ne. this%bdlag) then
367  ifcomp = 1.0_rp
368  end if
369 
370  this%dtlag = dt
371  this%bdlag = ext_bdf%diffusion_coeffs(1)
372 
373  call mpi_allreduce(mpi_in_place, ifcomp, 1, &
374  mpi_real_precision, mpi_sum, neko_comm, ierr)
375 
376  if (ifcomp .gt. 0d0) then
377  call this%compute(u_res, v_res, w_res, p_res, &
378  ext_bdf, gs_xh, c_xh, rho, mu, ext_bdf%diffusion_coeffs(1), dt, &
379  bclst_dp, bclst_du, bclst_dv, bclst_dw, bclst_vel_res, &
380  ax, ksp_prs, ksp_vel, pc_prs, pc_vel, prs_max_iter, vel_max_iter)
381  end if
382 
383  if (neko_bcknd_device .eq. 1) then
384  if (this%flow_dir .eq. 1) then
385  current_flow = &
386  device_glsc2(u%x_d, c_xh%B_d, n) / this%domain_length ! for X
387  else if (this%flow_dir .eq. 2) then
388  current_flow = &
389  device_glsc2(v%x_d, c_xh%B_d, n) / this%domain_length ! for Y
390  else if (this%flow_dir .eq. 3) then
391  current_flow = &
392  device_glsc2(w%x_d, c_xh%B_d, n) / this%domain_length ! for Z
393  end if
394  else
395  if (this%flow_dir .eq. 1) then
396  current_flow = glsc2(u%x, c_xh%B, n) / this%domain_length ! for X
397  else if (this%flow_dir .eq. 2) then
398  current_flow = glsc2(v%x, c_xh%B, n) / this%domain_length ! for Y
399  else if (this%flow_dir .eq. 3) then
400  current_flow = glsc2(w%x, c_xh%B, n) / this%domain_length ! for Z
401  end if
402  end if
403 
404  if (this%avflow) then
405  xsec = c_xh%volume / this%domain_length
406  flow_rate = this%flow_rate*xsec
407  else
408  flow_rate = this%flow_rate
409  endif
410 
411  delta_flow = flow_rate - current_flow
412  scale = delta_flow / this%base_flow
413 
414  if (neko_bcknd_device .eq. 1) then
415  call device_add2s2(u%x_d, u_vol%x_d, scale, n)
416  call device_add2s2(v%x_d, v_vol%x_d, scale, n)
417  call device_add2s2(w%x_d, w_vol%x_d, scale, n)
418  call device_add2s2(p%x_d, p_vol%x_d, scale, n)
419  else
420  call add2s2(u%x, u_vol%x, scale, n)
421  call add2s2(v%x, v_vol%x, scale, n)
422  call add2s2(w%x, w_vol%x, scale, n)
423  call add2s2(p%x, p_vol%x, scale, n)
424  end if
425  end associate
426 
427  end subroutine fluid_vol_flow
428 
429 
430 end module fluid_volflow
Retrieves a parameter by name or throws an error.
Definition: json_utils.f90:44
Defines a Matrix-vector product.
Definition: ax.f90:34
Defines a boundary condition.
Definition: bc.f90:34
subroutine, public bc_list_apply_vector(bclst, x, y, z, n, t, tstep)
Apply a list of boundary conditions to a vector field.
Definition: bc.f90:572
subroutine, public bc_list_apply_scalar(bclst, x, n, t, tstep)
Apply a list of boundary conditions to a scalar field.
Definition: bc.f90:515
Coefficients.
Definition: coef.f90:34
Definition: comm.F90:1
subroutine, public device_add2(a_d, b_d, n)
subroutine, public device_rzero(a_d, n)
real(kind=rp) function, public device_glsc2(a_d, b_d, n)
subroutine, public device_copy(a_d, b_d, n)
subroutine, public device_cfill(a_d, c, n)
subroutine, public device_opchsign(a1_d, a2_d, a3_d, gdim, n)
Defines a mapping of the degrees of freedom.
Definition: dofmap.f90:35
Defines a field.
Definition: field.f90:34
subroutine fluid_vol_flow_init(this, dm_Xh, params)
subroutine fluid_vol_flow_compute(this, u_res, v_res, w_res, p_res, ext_bdf, gs_Xh, c_Xh, rho, mu, bd, dt, bclst_dp, bclst_du, bclst_dv, bclst_dw, bclst_vel_res, Ax, ksp_prs, ksp_vel, pc_prs, pc_vel, prs_max_iter, vel_max_iter)
Compute flow adjustment.
subroutine fluid_vol_flow(this, u, v, w, p, u_res, v_res, w_res, p_res, c_Xh, gs_Xh, ext_bdf, rho, mu, dt, bclst_dp, bclst_du, bclst_dv, bclst_dw, bclst_vel_res, Ax, ksp_prs, ksp_vel, pc_prs, pc_vel, prs_max_iter, vel_max_iter)
Adjust flow volume.
subroutine fluid_vol_flow_free(this)
Gather-scatter.
Utilities for retrieving parameters from the case files.
Definition: json_utils.f90:34
Implements the base abstract type for Krylov solvers plus helper types.
Definition: krylov.f90:34
Definition: math.f90:60
real(kind=rp) function, public glsc2(a, b, n)
Weighted inner product .
Definition: math.f90:792
subroutine, public add2(a, b, n)
Vector addition .
Definition: math.f90:503
real(kind=rp) function, public glmax(a, n)
Max of a vector of length n.
Definition: math.f90:297
subroutine, public copy(a, b, n)
Copy a vector .
Definition: math.f90:211
real(kind=rp) function, public glmin(a, n)
Min of a vector of length n.
Definition: math.f90:325
Collection of vector field operations operating on and . Note that in general the indices and ....
Definition: mathops.f90:65
subroutine opchsign(a1, a2, a3, gdim, n)
for and .
Definition: mathops.f90:73
Build configurations.
Definition: neko_config.f90:34
integer, parameter neko_bcknd_hip
Definition: neko_config.f90:42
integer, parameter neko_bcknd_device
Definition: neko_config.f90:44
integer, parameter neko_bcknd_opencl
Definition: neko_config.f90:43
integer, parameter neko_bcknd_cuda
Definition: neko_config.f90:41
integer, parameter, public rp
Global precision used in computations.
Definition: num_types.f90:12
Operators.
Definition: operators.f90:34
subroutine, public cdtp(dtx, x, dr, ds, dt, coef)
Apply D^T to a scalar field, where D is the derivative matrix.
Definition: operators.f90:157
subroutine, public opgrad(ux, uy, uz, u, coef, es, ee)
Compute the gradient of a scalar field.
Definition: operators.f90:100
Krylov preconditioner.
Definition: precon.f90:34
Defines a registry for storing and requesting temporary fields This can be used when you have a funct...
Compound scheme for the advection and diffusion operators in a transport equation.
Base type for a matrix-vector product providing .
Definition: ax.f90:43
A list of boundary conditions.
Definition: bc.f90:102
Coefficients defined on a given (mesh, ) tuple. Arrays use indices (i,j,k,e): element e,...
Definition: coef.f90:54
Type for storing initial and final residuals in a Krylov solver.
Definition: krylov.f90:55
Base abstract type for a canonical Krylov method, solving .
Definition: krylov.f90:65
Defines a canonical Krylov preconditioner.
Definition: precon.f90:40
Implements the logic to compute the time coefficients for the advection and diffusion operators in a ...