Line data Source code
1 : /*
2 : * Copyright (c) 2013-2015: G-CSC, Goethe University Frankfurt
3 : * Author: Martin Rupp
4 : *
5 : * This file is part of UG4.
6 : *
7 : * UG4 is free software: you can redistribute it and/or modify it under the
8 : * terms of the GNU Lesser General Public License version 3 (as published by the
9 : * Free Software Foundation) with the following additional attribution
10 : * requirements (according to LGPL/GPL v3 §7):
11 : *
12 : * (1) The following notice must be displayed in the Appropriate Legal Notices
13 : * of covered and combined works: "Based on UG4 (www.ug4.org/license)".
14 : *
15 : * (2) The following notice must be displayed at a prominent place in the
16 : * terminal output of covered works: "Based on UG4 (www.ug4.org/license)".
17 : *
18 : * (3) The following bibliography is recommended for citation and must be
19 : * preserved in all covered files:
20 : * "Reiter, S., Vogel, A., Heppner, I., Rupp, M., and Wittum, G. A massively
21 : * parallel geometric multigrid solver on hierarchically distributed grids.
22 : * Computing and visualization in science 16, 4 (2013), 151-164"
23 : * "Vogel, A., Reiter, S., Rupp, M., Nägel, A., and Wittum, G. UG4 -- a novel
24 : * flexible software system for simulating pde based models on high performance
25 : * computers. Computing and visualization in science 16, 4 (2013), 165-179"
26 : *
27 : * This program is distributed in the hope that it will be useful,
28 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30 : * GNU Lesser General Public License for more details.
31 : */
32 :
33 : #ifndef NUMBER_UTIL_H_
34 : #define NUMBER_UTIL_H_
35 :
36 : #include "common/math/ugmath.h"
37 : namespace ug{
38 : extern bool g_bNoNANCheck;
39 : inline bool IsFiniteAndNotTooBig(double d)
40 : {
41 : const double tooBigValue = 1e30;
42 0 : if(d > tooBigValue || d < -tooBigValue || std::isfinite(d) == false)
43 : // if(std::isfinite(d) == false)
44 : return false;
45 : return true;
46 : }
47 :
48 : inline bool CloseToZero(double d)
49 : {
50 0 : if(d > -1e-100 && d < +1e-100)
51 : return true;
52 : else
53 : return false;
54 : }
55 :
56 : //inline bool IsFiniteAndNotTooBig(float d)
57 : //{
58 : // return IsFiniteAndNotTooBig((double) d);
59 : //}
60 :
61 :
62 : template <std::size_t N, std::size_t M, typename T>
63 : inline bool IsFiniteAndNotTooBig(const MathMatrix<N, M, T> &m)
64 : {
65 0 : for(size_t r=0; r<m.num_rows(); r++)
66 0 : for(size_t c=0; c<m.num_cols(); c++)
67 0 : if(IsFiniteAndNotTooBig(m(r,c)) == false) return false;
68 : return true;
69 : }
70 :
71 : template <std::size_t N, typename T>
72 : inline bool IsFiniteAndNotTooBig(const MathVector<N, T> &m)
73 : {
74 0 : for(size_t r=0; r<m.size(); r++)
75 0 : if(IsFiniteAndNotTooBig(m[r]) == false) return false;
76 : return true;
77 : }
78 :
79 :
80 :
81 : template <size_t TRank, size_t N, typename T>
82 : inline bool IsFiniteAndNotTooBig(const MathTensor<TRank, N, T> &t)
83 : {
84 : for(size_t i=0; i<t.size(); i++)
85 : if(IsFiniteAndNotTooBig(t[i]) == false) return false;
86 :
87 : return true;
88 : }
89 :
90 :
91 : template<typename TData, size_t N>
92 : inline bool IsFiniteAndNotTooBig(const MathTensorX<TData, N> &t)
93 : {
94 : for(size_t i=0; i<t.size(); i++)
95 : if(IsFiniteAndNotTooBig(t[i]) == false) return false;
96 :
97 : return true;
98 : }
99 :
100 : template<typename TData>
101 : inline bool IsFiniteAndNotTooBig(const std::vector<TData> &t)
102 : {
103 : for(size_t i=0; i<t.size(); i++)
104 : if(IsFiniteAndNotTooBig(t[i]) == false) return false;
105 :
106 : return true;
107 : }
108 :
109 : template<typename T>
110 : inline bool IsVectorFiniteAndNotTooBig(const T &t)
111 : {
112 : for(size_t i=0; i<t.size(); i++)
113 : if(IsFiniteAndNotTooBig(t[i]) == false) return false;
114 :
115 : return true;
116 : }
117 :
118 :
119 : template<typename T>
120 : inline bool WarnIfIsVectorNanOrTooBig(const T &t, const char *callerName)
121 : {
122 : if(g_bNoNANCheck) return true;
123 : size_t iFirst, iCount=0;
124 : for(size_t i=0; i<t.size(); i++)
125 : if(IsFiniteAndNotTooBig(t[i]) == false)
126 : {
127 : if(iCount == 0) iFirst = i;
128 : iCount++;
129 : }
130 :
131 : if(iCount > 0)
132 : {
133 : UG_LOG("WARNING in " << callerName << ": " << iFirst << " nan or too big (" << t[iFirst] << "). ");
134 : if(iCount > 1)
135 : { UG_LOG(iCount-1 << " more.");}
136 : UG_LOG("\n");
137 : return false;
138 : }
139 : return true;
140 : }
141 :
142 :
143 : template<typename T>
144 : inline bool ThrowIfIsVectorNanOrTooBig(const T &t, const char *callerName)
145 : {
146 : if(g_bNoNANCheck) return true;
147 : size_t iFirst, iCount=0;
148 : for(size_t i=0; i<t.size(); i++)
149 : if(IsFiniteAndNotTooBig(t[i]) == false)
150 : {
151 : if(iCount == 0) iFirst = i;
152 : iCount++;
153 : }
154 :
155 : if(iCount > 0)
156 : {
157 : UG_THROW("In " << callerName << ": " << iFirst << " nan or too big (" << t[iFirst] << "). "
158 : << iCount-1 << " more.");
159 : return false;
160 : }
161 :
162 : return true;
163 : }
164 :
165 : }
166 :
167 :
168 : #endif /* NUMBER_UTIL_H_ */
|