Rocstar  1.0
Rocstar multiphysics simulation application
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ModTools.F90
Go to the documentation of this file.
1 ! *********************************************************************
2 ! * Rocstar Simulation Suite *
3 ! * Copyright@2015, Illinois Rocstar LLC. All rights reserved. *
4 ! * *
5 ! * Illinois Rocstar LLC *
6 ! * Champaign, IL *
7 ! * www.illinoisrocstar.com *
8 ! * sales@illinoisrocstar.com *
9 ! * *
10 ! * License: See LICENSE file in top level of distribution package or *
11 ! * http://opensource.org/licenses/NCSA *
12 ! *********************************************************************
13 ! *********************************************************************
14 ! * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, *
15 ! * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES *
16 ! * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND *
17 ! * NONINFRINGEMENT. IN NO EVENT SHALL THE CONTRIBUTORS OR *
18 ! * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
19 ! * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
20 ! * Arising FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE *
21 ! * USE OR OTHER DEALINGS WITH THE SOFTWARE. *
22 ! *********************************************************************
23 !******************************************************************************
24 !
25 ! Purpose: Collection of utility functions.
26 !
27 ! Description: None
28 !
29 ! Notes:
30 ! 1. Named ModTools instead of ModUtilities because we already have a
31 ! utilities directory.
32 !
33 !******************************************************************************
34 !
35 ! $Id: ModTools.F90,v 1.11 2008/12/06 08:44:19 mtcampbe Exp $
36 !
37 ! Copyright: (c) 2002-2005 by the University of Illinois
38 !
39 !******************************************************************************
40 
41 MODULE modtools
42 
43  USE moddatatypes
44 
45  IMPLICIT NONE
46 
47  CONTAINS
48 
49 ! ------------------------------------------------------------------------------
50 ! Swap two integers
51 ! ------------------------------------------------------------------------------
52 
53  SUBROUTINE swapintegers(a,b)
54 
55  INTEGER, INTENT(INOUT) :: a,b
56 
57  INTEGER :: c
58 
59  c = b
60  b = a
61  a = c
62 
63  END SUBROUTINE swapintegers
64 
65 ! ------------------------------------------------------------------------------
66 ! Swap two floats
67 ! ------------------------------------------------------------------------------
68 
69  SUBROUTINE swaprfreals(a,b)
70 
71  REAL(RFREAL), INTENT(INOUT) :: a,b
72 
73  REAL(RFREAL) :: c
74 
75  c = b
76  b = a
77  a = c
78 
79  END SUBROUTINE swaprfreals
80 
81 ! ------------------------------------------------------------------------------
82 ! Prevent division by zero by blending of variable with machine precision
83 ! ------------------------------------------------------------------------------
84 
85  FUNCTION makenonzero(x)
86 
87  REAL(RFREAL) :: makenonzero
88 
89  REAL(RFREAL), INTENT(IN) :: x
90 
91  makenonzero = x + sign(1.0_rfreal,x)*epsilon(1.0_rfreal)
92 
93  END FUNCTION makenonzero
94 
95 ! ------------------------------------------------------------------------------
96 ! Comparison of floating-point numbers for equality
97 ! ------------------------------------------------------------------------------
98 
99  LOGICAL FUNCTION floatequal(a,b,tolIn)
100 
101  REAL(RFREAL), INTENT(IN) :: a,b
102  REAL(RFREAL), INTENT(IN), OPTIONAL :: tolin
103 
104  REAL(RFREAL) :: tol
105 
106  floatequal = .false.
107 
108  IF ( present(tolin) .EQV. .true. ) THEN
109  tol = tolin
110  ELSE
111  tol = 10.0_rfreal*epsilon(1.0_rfreal)
112  END IF ! PRESENT
113 
114  IF ( abs(a-b) <= (1.0_rfreal + 0.5_rfreal*abs(a+b))*tol ) THEN
115  floatequal = .true.
116  END IF ! ABS
117 
118  END FUNCTION floatequal
119 
120 ! ------------------------------------------------------------------------------
121 ! Comparison of floating-point numbers: greater than
122 ! ------------------------------------------------------------------------------
123 
124  LOGICAL FUNCTION floatgreater(a,b)
125 
126  REAL(RFREAL), INTENT(IN) :: a,b
127 
128  floatgreater = .false.
129 
130  IF ( a - b > b*epsilon(1.0_rfreal) ) THEN
131  floatgreater = .true.
132  END IF ! a
133 
134  END FUNCTION floatgreater
135 
136 ! ------------------------------------------------------------------------------
137 ! Comparison of floating-point numbers: less than
138 ! ------------------------------------------------------------------------------
139 
140  LOGICAL FUNCTION floatless(a,b)
141 
142  REAL(RFREAL), INTENT(IN) :: a,b
143 
144  floatless = .false.
145 
146  IF ( a - b < b*epsilon(1.0_rfreal) ) THEN
147  floatless = .true.
148  END IF ! a
149 
150  END FUNCTION floatless
151 
152 ! ------------------------------------------------------------------------------
153 ! Compute Factorial
154 ! ------------------------------------------------------------------------------
155 
156  INTEGER FUNCTION compfact(n)
157 
158  INTEGER, INTENT(IN) :: n
159 
160  INTEGER :: i
161 
162  compfact = 1
163 
164  DO i = 2,n
166  END DO ! i
167 
168  END FUNCTION compfact
169 
170 ! ------------------------------------------------------------------------------
171 ! Compute (a^2 + b^2)**(1/2) without under- or overflow
172 ! ------------------------------------------------------------------------------
173 
174  FUNCTION comppythag(a,b)
175 
176  REAL(RFREAL) :: comppythag
177 
178  REAL(RFREAL), INTENT(IN) :: a,b
179 
180  REAL(RFREAL) :: absa,absb
181 
182  absa = abs(a)
183  absb = abs(b)
184 
185  IF ( absa > absb ) THEN
186  comppythag = absa*sqrt(1.0_rfreal + (absb/absa)**2)
187  ELSE
188  IF ( absb == 0.0_rfreal ) THEN
189  comppythag = 0.0_rfreal
190  ELSE
191  comppythag = absb*sqrt(1.0_rfreal + (absa/absb)**2)
192  END IF ! absb
193  END IF ! absa
194 
195  END FUNCTION comppythag
196 
197 ! ------------------------------------------------------------------------------
198 ! Detect NaNs
199 ! ------------------------------------------------------------------------------
200 
201  FUNCTION isnan(x)
202 
203  LOGICAL :: isnan
204 
205  REAL(RFREAL), INTENT(IN) :: x
206 
207  isnan = .false.
208 
209  IF ( .NOT.(x > -huge(1.0) .AND. x < huge(1.0)) ) THEN
210  isnan = .true.
211  END IF ! NOT
212 
213  END FUNCTION isnan
214 
215 ! ******************************************************************************
216 ! End
217 ! ******************************************************************************
218 
219 END MODULE modtools
220 
221 !******************************************************************************
222 !
223 ! RCS Revision history:
224 !
225 ! $Log: ModTools.F90,v $
226 ! Revision 1.11 2008/12/06 08:44:19 mtcampbe
227 ! Updated license.
228 !
229 ! Revision 1.10 2008/11/19 22:17:30 mtcampbe
230 ! Added Illinois Open Source License/Copyright
231 !
232 ! Revision 1.9 2006/03/25 21:47:54 haselbac
233 ! Renamed SwapFloats to SwapRFREALS
234 !
235 ! Revision 1.8 2003/12/04 03:28:29 haselbac
236 ! Added function to detect NaNs
237 !
238 ! Revision 1.7 2003/02/06 19:30:59 haselbac
239 ! Added optional tolerance argument to FloatEqual
240 !
241 ! Revision 1.6 2003/01/28 16:49:01 haselbac
242 ! Changed FloatEqual to work better with very small floats
243 !
244 ! Revision 1.5 2002/11/27 20:24:42 haselbac
245 ! Changed tolerance in FloatEqual
246 !
247 ! Revision 1.4 2002/11/26 15:25:45 haselbac
248 ! Fixed bug in FloatEqual
249 !
250 ! Revision 1.3 2002/09/09 15:00:54 haselbac
251 ! Added robust calculation of CompPythag
252 !
253 ! Revision 1.2 2002/07/25 15:13:39 haselbac
254 ! Added factorial function
255 !
256 ! Revision 1.1 2002/05/04 16:51:58 haselbac
257 ! Initial revision
258 !
259 !******************************************************************************
260 
261 
262 
263 
264 
265 
static SURF_BEGIN_NAMESPACE double sign(double x)
INTEGER function compfact(n)
Definition: ModTools.F90:156
unsigned char b() const
Definition: Color.h:70
double sqrt(double d)
Definition: double.h:73
LOGICAL function floatgreater(a, b)
Definition: ModTools.F90:124
RT c() const
Definition: Line_2.h:150
LOGICAL function floatless(a, b)
Definition: ModTools.F90:140
logical function isnan(x)
Definition: ModTools.F90:201
blockLoc i
Definition: read.cpp:79
void int int REAL * x
Definition: read.cpp:74
const NT & n
subroutine swapintegers(a, b)
Definition: ModTools.F90:53
subroutine swaprfreals(a, b)
Definition: ModTools.F90:69
real(rfreal) function comppythag(a, b)
Definition: ModTools.F90:174
LOGICAL function floatequal(a, b, tolIn)
Definition: ModTools.F90:99
RT a() const
Definition: Line_2.h:140
real(rfreal) function makenonzero(x)
Definition: ModTools.F90:85