DAMASK with grid solvers  Revision: v2.0.3-2204-gdb1f2151
The Düsseldorf Advanced Material Simulation Kit with Grid Solvers
prec.f90
Go to the documentation of this file.
1 # 1 "/home/damask_user/GitLabCI_Pipeline_4301/DAMASK/src/prec.f90"
2 # 1 "<built-in>"
3 # 1 "<command-line>"
4 # 1 "/home/damask_user/GitLabCI_Pipeline_4301/DAMASK/src/prec.f90"
5 !--------------------------------------------------------------------------------------------------
12 !--------------------------------------------------------------------------------------------------
13 module prec
14  use, intrinsic :: ieee_arithmetic
15 
16  implicit none
17  public
18 
19  ! https://software.intel.com/en-us/blogs/2017/03/27/doctor-fortran-in-it-takes-all-kinds
20  integer, parameter :: preal = ieee_selected_real_kind(15,307)
21 
22 
23 
24  integer, parameter :: pint = selected_int_kind(9)
25 
26  integer, parameter :: plongint = selected_int_kind(18)
27  integer, parameter :: pstringlen = 256
28  integer, parameter :: ppathlen = 4096
29 
30  real(preal), parameter :: tol_math_check = 1.0e-8_preal
31 
32 
33  type :: group_float
34  real(preal), dimension(:), pointer :: p
35  end type group_float
36 
37  type :: group_int
38  integer, dimension(:), pointer :: p
39  end type group_int
40 
41  ! http://stackoverflow.com/questions/3948210/can-i-have-a-pointer-to-an-item-in-an-allocatable-array
42  type :: tstate
43  integer :: &
44  sizestate = 0, & !< size of state
45  sizedotstate = 0, &
46  offsetdeltastate = 0, &
47  sizedeltastate = 0
48  real(preal), pointer, dimension(:), contiguous :: &
49  atol
50  real(preal), pointer, dimension(:,:), contiguous :: & ! a pointer is needed here because we might point to state/doState. However, they will never point to something, but are rather allocated and, hence, contiguous
51  state0, &
52  state, & !< state
53  dotstate, & !< rate of state change
54  deltastate
55  real(preal), allocatable, dimension(:,:) :: &
56  partionedstate0, &
57  substate0, &
58  previousdotstate, &
59  previousdotstate2
60  real(preal), allocatable, dimension(:,:,:) :: &
61  rk4dotstate, &
62  rkck45dotstate
63  end type
64 
65  type, extends(tstate) :: tplasticstate
66  logical :: &
67  nonlocal = .false.
68  real(preal), pointer, dimension(:,:) :: &
69  sliprate, & !< slip rate
70  accumulatedslip
71  end type
72 
73  type :: tsourcestate
74  type(tstate), dimension(:), allocatable :: p
75  end type
76 
77  type :: thomogmapping
78  integer, pointer, dimension(:,:) :: p
79  end type
80 
81  real(preal), private, parameter :: preal_epsilon = epsilon(0.0_preal)
82  real(preal), private, parameter :: preal_min = tiny(0.0_preal)
83 
84  integer, dimension(0), parameter :: &
85  emptyintarray = [integer::]
86  real(preal), dimension(0), parameter :: &
87  emptyrealarray = [real(preal)::]
88  character(len=pStringLen), dimension(0), parameter :: &
89  emptystringarray = [character(len=pstringlen)::]
90 
91  private :: &
92  unittest
93 
94 contains
95 
96 
97 !--------------------------------------------------------------------------------------------------
99 !--------------------------------------------------------------------------------------------------
100 subroutine prec_init
101 
102  write(6,'(/,a)') ' <<<+- prec init -+>>>'
103 
104  write(6,'(a,i3)') ' Size of integer in bit: ',bit_size(0)
105  write(6,'(a,i19)') ' Maximum value: ',huge(0)
106  write(6,'(/,a,i3)') ' Size of float in bit: ',storage_size(0.0_preal)
107  write(6,'(a,e10.3)') ' Maximum value: ',huge(0.0_preal)
108  write(6,'(a,e10.3)') ' Minimum value: ',tiny(0.0_preal)
109  write(6,'(a,i3)') ' Decimal precision: ',precision(0.0_preal)
110 
111  call unittest
112 
113 end subroutine prec_init
114 
115 
116 !--------------------------------------------------------------------------------------------------
118 ! replaces "==" but for certain (relative) tolerance. Counterpart to dNeq
119 ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
120 ! AlmostEqualRelative
121 !--------------------------------------------------------------------------------------------------
122 logical elemental pure function dEq(a,b,tol)
123 
124  real(preal), intent(in) :: a,b
125  real(preal), intent(in), optional :: tol
126  real(preal) :: eps
127 
128  if (present(tol)) then
129  eps = tol
130  else
131  eps = preal_epsilon * maxval(abs([a,b]))
132  endif
133 
134  deq = merge(.true.,.false.,abs(a-b) <= eps)
135 
136 end function deq
137 
138 
139 !--------------------------------------------------------------------------------------------------
141 ! replaces "!=" but for certain (relative) tolerance. Counterpart to dEq
142 ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
143 ! AlmostEqualRelative NOT
144 !--------------------------------------------------------------------------------------------------
145 logical elemental pure function dneq(a,b,tol)
146 
147  real(preal), intent(in) :: a,b
148  real(preal), intent(in), optional :: tol
149 
150  if (present(tol)) then
151  dneq = .not. deq(a,b,tol)
152  else
153  dneq = .not. deq(a,b)
154  endif
155 
156 end function dneq
157 
158 
159 !--------------------------------------------------------------------------------------------------
161 ! replaces "==0" but everything not representable as a normal number is treated as 0. Counterpart to dNeq0
162 ! https://de.mathworks.com/help/matlab/ref/realmin.html
163 ! https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html
164 !--------------------------------------------------------------------------------------------------
165 logical elemental pure function deq0(a,tol)
166 
167  real(preal), intent(in) :: a
168  real(preal), intent(in), optional :: tol
169  real(preal) :: eps
170 
171  if (present(tol)) then
172  eps = tol
173  else
174  eps = preal_min * 10.0_preal
175  endif
176 
177  deq0 = merge(.true.,.false.,abs(a) <= eps)
178 
179 end function deq0
180 
181 
182 !--------------------------------------------------------------------------------------------------
184 ! replaces "!=0" but everything not representable as a normal number is treated as 0. Counterpart to dEq0
185 ! https://de.mathworks.com/help/matlab/ref/realmin.html
186 ! https://docs.oracle.com/cd/E19957-01/806-3568/ncg_math.html
187 !--------------------------------------------------------------------------------------------------
188 logical elemental pure function dneq0(a,tol)
189 
190  real(preal), intent(in) :: a
191  real(preal), intent(in), optional :: tol
192 
193  if (present(tol)) then
194  dneq0 = .not. deq0(a,tol)
195  else
196  dneq0 = .not. deq0(a)
197  endif
198 
199 end function dneq0
200 
201 
202 !--------------------------------------------------------------------------------------------------
204 ! replaces "==" but for certain (relative) tolerance. Counterpart to cNeq
205 ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
206 ! probably a component wise comparison would be more accurate than the comparsion of the absolute
207 ! value
208 !--------------------------------------------------------------------------------------------------
209 logical elemental pure function ceq(a,b,tol)
210 
211  complex(pReal), intent(in) :: a,b
212  real(preal), intent(in), optional :: tol
213  real(preal) :: eps
214 
215  if (present(tol)) then
216  eps = tol
217  else
218  eps = preal_epsilon * maxval(abs([a,b]))
219  endif
220 
221  ceq = merge(.true.,.false.,abs(a-b) <= eps)
222 
223 end function ceq
224 
225 
226 !--------------------------------------------------------------------------------------------------
228 ! replaces "!=" but for certain (relative) tolerance. Counterpart to cEq
229 ! https://randomascii.wordpress.com/2012/02/25/comparing-floating-point-numbers-2012-edition/
230 ! probably a component wise comparison would be more accurate than the comparsion of the absolute
231 ! value
232 !--------------------------------------------------------------------------------------------------
233 logical elemental pure function cneq(a,b,tol)
234 
235  complex(pReal), intent(in) :: a,b
236  real(preal), intent(in), optional :: tol
237 
238  if (present(tol)) then
239  cneq = .not. ceq(a,b,tol)
240  else
241  cneq = .not. ceq(a,b)
242  endif
243 
244 end function cneq
245 
246 
247 !--------------------------------------------------------------------------------------------------
249 !--------------------------------------------------------------------------------------------------
250 subroutine unittest
251 
252  integer, allocatable, dimension(:) :: realloc_lhs_test
253  real(preal), dimension(2) :: r
254  external :: &
255  quit
256 
257  call random_number(r)
258  r = r/minval(r)
259  if(.not. all(deq(r,r+preal_epsilon))) call quit(9000)
260  if(deq(r(1),r(2)) .and. dneq(r(1),r(2))) call quit(9000)
261  if(.not. all(deq0(r-(r+preal_min)))) call quit(9000)
262 
263  realloc_lhs_test = [1,2]
264  if (any(realloc_lhs_test/=[1,2])) call quit(9000)
265 
266 end subroutine unittest
267 
268 end module prec
prec::unittest
subroutine, private unittest
check correctness of some prec functions
Definition: prec.f90:251
prec::deq
logical elemental pure function deq(a, b, tol)
equality comparison for float with double precision
Definition: prec.f90:123
prec::emptyrealarray
real(preal), dimension(0), parameter emptyrealarray
Definition: prec.f90:86
prec::tol_math_check
real(preal), parameter tol_math_check
tolerance for internal math self-checks (rotation)
Definition: prec.f90:30
prec::plongint
integer, parameter plongint
number with at least up to +-1e18 (typically 64 bit)
Definition: prec.f90:26
prec::ppathlen
integer, parameter ppathlen
maximum length of a path name on linux
Definition: prec.f90:28
prec::thomogmapping
Definition: prec.f90:77
prec::emptystringarray
character(len=pstringlen), dimension(0), parameter emptystringarray
Definition: prec.f90:88
prec::preal_epsilon
real(preal), parameter, private preal_epsilon
minimum positive number such that 1.0 + EPSILON /= 1.0.
Definition: prec.f90:81
prec
setting precision for real and int type
Definition: prec.f90:13
prec::pstringlen
integer, parameter pstringlen
default string length
Definition: prec.f90:27
prec::prec_init
subroutine prec_init
reporting precision
Definition: prec.f90:101
prec::dneq0
logical elemental pure function dneq0(a, tol)
inequality to 0 comparison for float with double precision
Definition: prec.f90:189
prec::preal
integer, parameter preal
number with 15 significant digits, up to 1e+-307 (typically 64 bit)
Definition: prec.f90:20
quit
subroutine quit(stop_id)
quit subroutine
Definition: quit.f90:12
prec::ceq
logical elemental pure function ceq(a, b, tol)
equality comparison for complex with double precision
Definition: prec.f90:210
prec::cneq
logical elemental pure function cneq(a, b, tol)
inequality comparison for complex with double precision
Definition: prec.f90:234
prec::dneq
logical elemental pure function dneq(a, b, tol)
inequality comparison for float with double precision
Definition: prec.f90:146
prec::group_float
variable length datatype used for storage of state
Definition: prec.f90:33
prec::emptyintarray
integer, dimension(0), parameter emptyintarray
Definition: prec.f90:84
prec::tstate
Definition: prec.f90:42
prec::pint
integer, parameter pint
number with at least up to +-1e9 (typically 32 bit)
Definition: prec.f90:24
prec::group_int
Definition: prec.f90:37
prec::tplasticstate
Definition: prec.f90:65
prec::tsourcestate
Definition: prec.f90:73
prec::deq0
logical elemental pure function deq0(a, tol)
equality to 0 comparison for float with double precision
Definition: prec.f90:166
prec::preal_min
real(preal), parameter, private preal_min
smallest normalized floating point number
Definition: prec.f90:82