libMesh
petsc_macro.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2019 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 #ifndef LIBMESH_PETSC_MACRO_H
19 #define LIBMESH_PETSC_MACRO_H
20 
21 // Local includes
22 #include "libmesh/libmesh_config.h"
23 
24 #ifdef LIBMESH_HAVE_PETSC
25 
26 // A convenient macro for comparing PETSc versions. Returns 1 if the
27 // current PETSc version is < major.minor.subminor and zero otherwise.
28 //
29 // This macro does not require petscversion.h to be included for it to work correctly.
30 // It instead relies on the PETSc version numbers detected during configure. Note that if
31 // LIBMESH_HAVE_PETSC is not defined, none of the LIBMESH_DETECTED_PETSC_VERSION_* variables will
32 // be defined either.
33 #define PETSC_VERSION_LESS_THAN(major,minor,subminor) \
34  ((LIBMESH_DETECTED_PETSC_VERSION_MAJOR < (major) || \
35  (LIBMESH_DETECTED_PETSC_VERSION_MAJOR == (major) && (LIBMESH_DETECTED_PETSC_VERSION_MINOR < (minor) || \
36  (LIBMESH_DETECTED_PETSC_VERSION_MINOR == (minor) && \
37  LIBMESH_DETECTED_PETSC_VERSION_SUBMINOR < (subminor))))) ? 1 : 0)
38 
39 // The PETSC_VERSION_RELEASE constant was introduced just prior to 2.3.0 (ca. Apr 22 2005),
40 // so fall back to using PETSC_VERSION_LESS_THAN in case it doesn't exist.
41 #ifdef LIBMESH_DETECTED_PETSC_VERSION_RELEASE
42 
43 #define PETSC_RELEASE_LESS_THAN(major,minor,subminor) \
44  (PETSC_VERSION_LESS_THAN(major,minor,subminor) && LIBMESH_DETECTED_PETSC_VERSION_RELEASE)
45 
46 #else
47 
48 #define PETSC_RELEASE_LESS_THAN(major,minor,subminor) \
49  (PETSC_VERSION_LESS_THAN(major,minor,subminor))
50 
51 #endif
52 
53 // We used to have workarounds for missing extern "C" in old PETSc
54 // versions. We no longer support PETSc versions so old, but we do
55 // still support libMesh applications old enough to have used these
56 // macros.
57 #define EXTERN_C_FOR_PETSC_BEGIN
58 #define EXTERN_C_FOR_PETSC_END
59 
60 // Petsc include files
61 // Wrapped to avoid triggering our more paranoid warnings
62 #include <libmesh/ignore_warnings.h>
63 #ifdef I
64 # define LIBMESH_SAW_I
65 #endif
66 #include <petsc.h>
67 #ifndef LIBMESH_SAW_I
68 # undef I // Avoid complex.h contamination
69 #endif
70 #include <libmesh/restore_warnings.h>
71 
72 #if PETSC_RELEASE_LESS_THAN(3,1,1)
73 typedef PetscTruth PetscBool;
74 #endif
75 
76 #if PETSC_RELEASE_LESS_THAN(3,1,1)
77 # define LibMeshVecDestroy(x) VecDestroy(*(x))
78 # define LibMeshVecScatterDestroy(x) VecScatterDestroy(*(x))
79 # define LibMeshMatDestroy(x) MatDestroy(*(x))
80 # define LibMeshISDestroy(x) ISDestroy(*(x))
81 # define LibMeshKSPDestroy(x) KSPDestroy(*(x))
82 # define LibMeshSNESDestroy(x) SNESDestroy(*(x))
83 # define LibMeshPetscViewerDestroy(x) PetscViewerDestroy(*(x))
84 # define LibMeshPCDestroy(x) PCDestroy(*(x))
85 #else
86 # define LibMeshVecDestroy(x) VecDestroy(x)
87 # define LibMeshVecScatterDestroy(x) VecScatterDestroy(x)
88 # define LibMeshMatDestroy(x) MatDestroy(x)
89 # define LibMeshISDestroy(x) ISDestroy(x)
90 # define LibMeshKSPDestroy(x) KSPDestroy(x)
91 # define LibMeshSNESDestroy(x) SNESDestroy(x)
92 # define LibMeshPetscViewerDestroy(x) PetscViewerDestroy(x)
93 # define LibMeshPCDestroy(x) PCDestroy(x)
94 #endif
95 
96 // PETSc devs temporarily considered adding VecScatterCreateWithData, but
97 // it was dropped in PETSc-130e142e39 and never made it into any release.
98 // We will keep the ifdef for backwards compatibility in case anyone wrote
99 // code directly using it, but that should be pretty unlikely.
100 #define LibMeshVecScatterCreate(xin,ix,yin,iy,newctx) VecScatterCreate(xin,ix,yin,iy,newctx)
101 
102 #if PETSC_RELEASE_LESS_THAN(3,1,1)
104 # define ISCreateLibMesh(comm,n,idx,mode,is) \
105  ((mode) == PETSC_USE_POINTER \
106  ? ISCreateGeneralWithArray((comm),(n),(idx),(is)) \
107  : ((mode) == PETSC_OWN_POINTER \
108  ? ISCreateGeneralNC((comm),(n),(idx),(is)) \
109  : ISCreateGeneral((comm),(n),(idx),(is))))
110 #else
111 # define ISCreateLibMesh(comm,n,idx,mode,is) ISCreateGeneral((comm),(n),(idx),(mode),(is))
112 #endif
113 
114 // As of release 3.8.0, MatGetSubMatrix was renamed to MatCreateSubMatrix.
115 #if PETSC_RELEASE_LESS_THAN(3,8,0)
116 # define LibMeshCreateSubMatrix MatGetSubMatrix
117 #else
118 # define LibMeshCreateSubMatrix MatCreateSubMatrix
119 #endif
120 
121 // If we're using quad precision, we need to disambiguate std
122 // operations on PetscScalar
123 
124 #if LIBMESH_DEFAULT_QUADRUPLE_PRECISION
125 # include <boost/multiprecision/float128.hpp>
126 
127 namespace std
128 {
129 inline
130 std::ostream & operator<< (std::ostream & os, const PetscScalar in)
131 {
132  os << (boost::multiprecision::float128(in));
133  return os;
134 }
135 
136 #define LIBMESH_PETSCSCALAR_UNARY(funcname) \
137 inline PetscScalar funcname \
138  (const PetscScalar in) \
139 { \
140  return boost::multiprecision::funcname \
141  (boost::multiprecision::float128(in)).backend().value(); \
142 }
143 
144 LIBMESH_PETSCSCALAR_UNARY(sqrt)
145 LIBMESH_PETSCSCALAR_UNARY(exp)
146 LIBMESH_PETSCSCALAR_UNARY(log)
147 LIBMESH_PETSCSCALAR_UNARY(log10)
148 LIBMESH_PETSCSCALAR_UNARY(sin)
149 LIBMESH_PETSCSCALAR_UNARY(cos)
150 LIBMESH_PETSCSCALAR_UNARY(tan)
151 LIBMESH_PETSCSCALAR_UNARY(asin)
152 LIBMESH_PETSCSCALAR_UNARY(acos)
153 LIBMESH_PETSCSCALAR_UNARY(atan)
154 LIBMESH_PETSCSCALAR_UNARY(sinh)
155 LIBMESH_PETSCSCALAR_UNARY(cosh)
156 LIBMESH_PETSCSCALAR_UNARY(tanh)
157 LIBMESH_PETSCSCALAR_UNARY(abs)
158 LIBMESH_PETSCSCALAR_UNARY(fabs)
159 LIBMESH_PETSCSCALAR_UNARY(ceil)
160 LIBMESH_PETSCSCALAR_UNARY(floor)
161 
162 } // namespace std
163 
164 // Helper functions for boost float128 compatibility
165 namespace libMesh
166 {
167 template <typename T>
168 PetscScalar PS(T val)
169 {
170  return val.backend().value();
171 }
172 
173 template <typename T>
174 PetscScalar * pPS(T * ptr)
175 {
176  return &(ptr->backend().value());
177 }
178 
179 template <typename T>
180 const PetscScalar * pPS(const T * ptr)
181 {
182  return &(ptr->backend().value());
183 }
184 
185 template <typename T>
186 PetscReal * pPR(T * ptr)
187 {
188  return &(ptr->backend().value());
189 }
190 
191 template <typename T>
192 const PetscReal * pPR(const T * ptr)
193 {
194  return &(ptr->backend().value());
195 }
196 } // namespace libMesh
197 
198 #else
199 
200 namespace libMesh
201 {
202 template <typename T>
203 PetscScalar PS(T val)
204 {
205  return val;
206 }
207 
208 template <typename T>
209 PetscScalar * pPS(T * ptr)
210 {
211  return ptr;
212 }
213 
214 template <typename T>
215 const PetscScalar * pPS(const T * ptr)
216 {
217  return ptr;
218 }
219 
220 template <typename T>
221 PetscReal * pPR(T * ptr)
222 {
223  return ptr;
224 }
225 
226 template <typename T>
227 const PetscReal * pPR(const T * ptr)
228 {
229  return ptr;
230 }
231 } // namespace libMesh
232 
233 #endif // LIBMESH_ENABLE_QUADRUPLE_PRECISION
234 
235 #else // LIBMESH_HAVE_PETSC
236 
237 #define PETSC_VERSION_LESS_THAN(major,minor,subminor) 1
238 #define PETSC_RELEASE_LESS_THAN(major,minor,subminor) 1
239 
240 #endif // LIBMESH_HAVE_PETSC
241 
242 #endif // LIBMESH_PETSC_MACRO_H
libMesh
The libMesh namespace provides an interface to certain functionality in the library.
Definition: factoryfunction.C:55
libMesh::pPR
PetscReal * pPR(T *ptr)
Definition: petsc_macro.h:186
libMesh::pPS
PetscScalar * pPS(T *ptr)
Definition: petsc_macro.h:174
std::sqrt
MetaPhysicL::DualNumber< T, D > sqrt(const MetaPhysicL::DualNumber< T, D > &in)
PetscBool
PetscTruth PetscBool
Definition: petsc_macro.h:73
PETSC_USE_POINTER
Definition: petsc_macro.h:103
std::abs
MetaPhysicL::DualNumber< T, D > abs(const MetaPhysicL::DualNumber< T, D > &in)
libMesh::PS
PetscScalar PS(T val)
Definition: petsc_macro.h:168
PETSC_COPY_VALUES
Definition: petsc_macro.h:103
PetscCopyMode
PetscCopyMode
Definition: petsc_macro.h:103
PETSC_OWN_POINTER
Definition: petsc_macro.h:103
std
Definition: float128_shims.h:27
std::operator<<
std::ostream & operator<<(std::ostream &os, const PetscScalar in)
Definition: petsc_macro.h:130