Neko  0.8.1
A portable framework for high-order spectral element flow simulations
utils.f90
Go to the documentation of this file.
1 ! Copyright (c) 2019-2021, 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 !
35 module utils
36  implicit none
37 
38  integer, parameter :: neko_fname_len = 1024
39 
40  interface neko_error
41  module procedure neko_error_plain, neko_error_msg
42  end interface neko_error
43 
44 contains
45 
47  pure function filename_suffix_pos(fname) result(suffix_pos)
48  character(len=*), intent(in) :: fname
49  integer :: suffix_pos
50  suffix_pos = scan(trim(fname), '.', back=.true.)
51  end function filename_suffix_pos
52 
54  pure function filename_tslash_pos(fname) result(tslash_pos)
55  character(len=*), intent(in) :: fname
56  integer :: tslash_pos
57  tslash_pos = scan(trim(fname), '/', back=.true.)
58  end function filename_tslash_pos
59 
61  subroutine filename_suffix(fname, suffix)
62  character(len=*) :: fname
63  character(len=*) :: suffix
64  suffix = trim(fname(filename_suffix_pos(fname) + 1:len_trim(fname)))
65  end subroutine filename_suffix
66 
68  subroutine filename_chsuffix(fname, new_fname, new_suffix)
69  character(len=*) :: fname
70  character(len=*) :: new_fname
71  character(len=*) :: new_suffix
72  integer :: suffix_pos
73 
74  suffix_pos = filename_suffix_pos(fname)
75  new_fname = trim(fname(1:suffix_pos))//new_suffix
76 
77  end subroutine filename_chsuffix
78 
81  function split_string(string, delimiter) result(split_str)
82  character(len=*) :: string
83  character(len=*) :: delimiter
84  character(len=100), allocatable :: split_str(:)
85  integer :: length, i, i2,offset, j
86  i = 0
87  offset = 1
88  length = 1
89  if (len(trim(string)) .eq. 0) then
90  allocate(split_str(1))
91  split_str(1) = trim(string)
92  return
93  end if
94  do while( .true.)
95  i = scan(string(offset:), delimiter, back=.false.)
96  if (i .eq. 0) exit
97  length = length + 1
98  offset = offset + i
99  end do
100 
101  allocate(split_str(length))
102  i = 0
103  j = 1
104  offset=1
105  do while( .true.)
106  i2 = scan(trim(string(offset:)), delimiter, back=.false.)
107  if (i2 .eq. 0) then
108  split_str(j) = trim(string(offset:))
109  exit
110  end if
111  split_str(j) = trim(string(offset:offset+i2-2))
112  offset = offset+i2
113  j = j + 1
114  end do
115  end function split_string
116 
117 
118 
119 
122  pure function linear_index(i,j,k,l,lx,ly,lz) result(index)
123  integer, intent(in) :: i, j, k, l, lx, ly, lz
124  integer :: index
125 
126  index = (i + lx * ((j - 1) + ly * ((k - 1) + lz * ((l - 1)))))
127  end function linear_index
128 
129  pure function index_is_on_facet(i, j, k, lx, ly, lz, facet) result(is_on)
130  integer, intent(in) :: i, j, k, lx, ly, lz, facet
131  logical :: is_on
132 
133  is_on = .false.
134  select case(facet)
135  case(1)
136  if (i .eq. 1) is_on = .true.
137  case(2)
138  if (i .eq. lx) is_on = .true.
139  case(3)
140  if (j .eq. 1) is_on = .true.
141  case(4)
142  if (j .eq. ly) is_on = .true.
143  case(5)
144  if (k .eq. 1) is_on = .true.
145  case(6)
146  if (k .eq. lz) is_on = .true.
147  end select
148 
149 
150  end function index_is_on_facet
151 
152 
155  pure function nonlinear_index(linear_index,lx,ly,lz) result(index)
156  integer, intent(in) :: linear_index, lx, ly, lz
157  integer :: index(4)
158  integer :: lin_idx
159  lin_idx = linear_index -1
160  index(4) = lin_idx/(lx*ly*lz)
161  index(3) = (lin_idx-(lx*ly*lz)*index(4))/(lx*ly)
162  index(2) = (lin_idx-(lx*ly*lz)*index(4)-(lx*ly)*index(3))/lx
163  index(1) = (lin_idx-(lx*ly*lz)*index(4)-(lx*ly)*index(3)-lx*index(2))
164  index(1) = index(1) + 1
165  index(2) = index(2) + 1
166  index(3) = index(3) + 1
167  index(4) = index(4) + 1
168 
169  end function nonlinear_index
170 
171  subroutine neko_error_plain(error_code)
172  integer, optional :: error_code
173 
174  if (present(error_code)) then
175  write(*,*) '*** ERROR ***', error_code
176  error stop
177  else
178  write(*,*) '*** ERROR ***'
179  error stop
180  end if
181 
182  end subroutine neko_error_plain
183 
184  subroutine neko_error_msg(error_msg)
185  character(len=*) :: error_msg
186  write(*,*) '*** ERROR: ', error_msg,' ***'
187  error stop
188  end subroutine neko_error_msg
189 
190  subroutine neko_warning(warning_msg)
191  character(len=*) :: warning_msg
192  write(*,*) '*** WARNING: ', warning_msg,' ***'
193  end subroutine neko_warning
194 
195 end module utils
__device__ void nonlinear_index(const int idx, const int lx, int *index)
Utilities.
Definition: utils.f90:35
subroutine filename_suffix(fname, suffix)
Extract a filename's suffix.
Definition: utils.f90:62
character(len=100) function, dimension(:), allocatable split_string(string, delimiter)
Split a string based on delimiter (tokenizer) OBS: very hacky, this should really be improved,...
Definition: utils.f90:82
pure integer function filename_tslash_pos(fname)
Find position (in the string) of a filename's trailing slash.
Definition: utils.f90:55
subroutine neko_warning(warning_msg)
Definition: utils.f90:191
subroutine neko_error_msg(error_msg)
Definition: utils.f90:185
pure integer function filename_suffix_pos(fname)
Find position (in the string) of a filename's suffix.
Definition: utils.f90:48
pure logical function index_is_on_facet(i, j, k, lx, ly, lz, facet)
Definition: utils.f90:130
integer, parameter neko_fname_len
Definition: utils.f90:38
subroutine filename_chsuffix(fname, new_fname, new_suffix)
Change a filename's suffix.
Definition: utils.f90:69
subroutine neko_error_plain(error_code)
Definition: utils.f90:172
pure integer function linear_index(i, j, k, l, lx, ly, lz)
Compute the address of a (i,j,k,l) array with sizes (1:lx, 1:ly, 1:lz, :)
Definition: utils.f90:123