Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2010 Sandia National Laboratories. Developed at the
5 : : University of Wisconsin--Madison under SNL contract number
6 : : 624796. The U.S. Government and the University of Wisconsin
7 : : retain certain rights to this software.
8 : :
9 : : This library is free software; you can redistribute it and/or
10 : : modify it under the terms of the GNU Lesser General Public
11 : : License as published by the Free Software Foundation; either
12 : : version 2.1 of the License, or (at your option) any later version.
13 : :
14 : : This library is distributed in the hope that it will be useful,
15 : : but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : Lesser General Public License for more details.
18 : :
19 : : You should have received a copy of the GNU Lesser General Public License
20 : : (lgpl.txt) along with this library; if not, write to the Free Software
21 : : Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : :
23 : : (2010) [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : /** \file TMPCommon.hpp
28 : : * \brief Common utility stuff for implementing target metrics
29 : : * \author Jason Kraftcheck
30 : : */
31 : :
32 : : #ifndef MSQ_TMP_COMMON_HPP
33 : : #define MSQ_TMP_COMMON_HPP
34 : :
35 : : #include "Mesquite.hpp"
36 : :
37 : : namespace MBMesquite
38 : : {
39 : :
40 : : /**\def TMP_T_TEMPL_IMPL_COMMON(N)
41 : : * \brief A macro that declares virtual evaluation functions for a \c TMetric subclass \c N
42 : : *
43 : : * IF a \c TMetric class provides template functions named \c eval, \c grad,
44 : : * and \c hess, then this macro can be used to provide the trivial implementations
45 : : * of the 2D and 3D \c evaluate, \c evaluate_with_grad, and \c evaluate_with_hess virtual
46 : : * member functions in terms of those macros.
47 : : */
48 : :
49 : : #define TMP_T_TEMPL_IMPL_DIM( N, D ) \
50 : : bool N::evaluate( const MsqMatrix< D, D >& T, double& r, MsqError& ) \
51 : : { \
52 : : return eval( T, r ); \
53 : : } \
54 : : bool N::evaluate_with_grad( const MsqMatrix< D, D >& T, double& r, MsqMatrix< D, D >& d1, MsqError& ) \
55 : : { \
56 : : return grad( T, r, d1 ); \
57 : : } \
58 : : bool N::evaluate_with_hess( const MsqMatrix< D, D >& T, double& r, MsqMatrix< D, D >& d1, MsqMatrix< D, D >* d2, \
59 : : MsqError& ) \
60 : : { \
61 : : return hess( T, r, d1, d2 ); \
62 : : }
63 : :
64 : : #define TMP_T_TEMPL_IMPL_COMMON( N ) \
65 : : TMP_T_TEMPL_IMPL_DIM( N, 2 ) \
66 : : TMP_T_TEMPL_IMPL_DIM( N, 3 )
67 : :
68 : : /**\def TMP_T_TEMPL_IMPL_COMMON_ERR(N)
69 : : * \brief A macro that declares virtual evaluation functions for a \c TMetric subclass \c N
70 : : *
71 : : * IF a \c TMetric class provides template functions named \c eval, \c grad,
72 : : * and \c hess, then this macro can be used to provide the trivial implementations
73 : : * of the 2D and 3D \c evaluate, \c evaluate_with_grad, and \c evaluate_with_hess virtual
74 : : * member functions in terms of those macros.
75 : : *
76 : : * The difference between this macro and \c TMP_AW_TEMPL_IMPL_COMMON is that this
77 : : * variation expects the \c eval, \c grad, and \c hess to accept an \c MsqError
78 : : * as their last argument. This variation is typically used for metrics that are
79 : : * a function of some other metric.
80 : : */
81 : :
82 : : #define TMP_T_TEMPL_IMPL_ERR_DIM( N, D ) \
83 : : bool N::evaluate( const MsqMatrix< D, D >& T, double& r, MsqError& err ) \
84 : : { \
85 : : return eval( T, r, err ); \
86 : : } \
87 : : bool N::evaluate_with_grad( const MsqMatrix< D, D >& T, double& r, MsqMatrix< D, D >& d1, MsqError& err ) \
88 : : { \
89 : : return grad( T, r, d1, err ); \
90 : : } \
91 : : bool N::evaluate_with_hess( const MsqMatrix< D, D >& T, double& r, MsqMatrix< D, D >& d1, MsqMatrix< D, D >* d2, \
92 : : MsqError& err ) \
93 : : { \
94 : : return hess( T, r, d1, d2, err ); \
95 : : }
96 : :
97 : : #define TMP_T_TEMPL_IMPL_COMMON_ERR( N ) \
98 : : TMP_T_TEMPL_IMPL_ERR_DIM( N, 2 ) \
99 : : TMP_T_TEMPL_IMPL_ERR_DIM( N, 3 )
100 : :
101 : : /**\def TMP_AW_TEMPL_IMPL_COMMON(N)
102 : : * \brief A macro that declares virtual evaluation functions for a \c AWMetric subclass \c N
103 : : *
104 : : * IF a \c AWMetric class provides template functions named \c eval, \c grad,
105 : : * and \c hess, then this macro can be used to provide the trivial implementations
106 : : * of the 2D and 3D \c evaluate, \c evaluate_with_grad, and \c evaluate_with_hess virtual
107 : : * member functions in terms of those macros.
108 : : */
109 : :
110 : : #define TMP_AW_TEMPL_IMPL_DIM( N, D ) \
111 : : bool N::evaluate( const MsqMatrix< D, D >& A, const MsqMatrix< D, D >& W, double& r, MsqError& ) \
112 : : { \
113 : : return eval( A, W, r ); \
114 : : } \
115 : : bool N::evaluate_with_grad( const MsqMatrix< D, D >& A, const MsqMatrix< D, D >& W, double& r, \
116 : : MsqMatrix< D, D >& d1, MsqError& ) \
117 : : { \
118 : : return grad( A, W, r, d1 ); \
119 : : } \
120 : : bool N::evaluate_with_hess( const MsqMatrix< D, D >& A, const MsqMatrix< D, D >& W, double& r, \
121 : : MsqMatrix< D, D >& d1, MsqMatrix< D, D >* d2, MsqError& ) \
122 : : { \
123 : : return hess( A, W, r, d1, d2 ); \
124 : : }
125 : :
126 : : #define TMP_AW_TEMPL_IMPL_COMMON( N ) \
127 : : TMP_AW_TEMPL_IMPL_DIM( N, 2 ) \
128 : : TMP_AW_TEMPL_IMPL_DIM( N, 3 )
129 : :
130 : : /**\def TMP_AW_TEMPL_IMPL_COMMON(N)
131 : : * \brief Like TMP_AW_TEMPL_IMPL_COMMON, except no implementation of 2nd derivs
132 : : */
133 : :
134 : : #define TMP_AW_TEMPL_IMPL_NO2ND_DIM( N, D ) \
135 : : bool N::evaluate( const MsqMatrix< D, D >& A, const MsqMatrix< D, D >& W, double& r, MsqError& ) \
136 : : { \
137 : : return eval( A, W, r ); \
138 : : } \
139 : : bool N::evaluate_with_grad( const MsqMatrix< D, D >& A, const MsqMatrix< D, D >& W, double& r, \
140 : : MsqMatrix< D, D >& d1, MsqError& ) \
141 : : { \
142 : : return grad( A, W, r, d1 ); \
143 : : }
144 : :
145 : : #define TMP_AW_TEMPL_IMPL_COMMON_NO2ND( N ) \
146 : : TMP_AW_TEMPL_IMPL_NO2ND_DIM( N, 2 ) \
147 : : TMP_AW_TEMPL_IMPL_NO2ND_DIM( N, 3 )
148 : :
149 : : /**\brief Dimension-specific constants
150 : : *
151 : : * Provide constants that depend on a template dimension parameter.
152 : : * For use in implementing targe metrics for which the target metric
153 : : * implementation is templatized on the dimension of of the matrix.
154 : : *
155 : : * In optimized code these should reduce to literal constants.
156 : : */
157 : : template < unsigned D >
158 : : struct DimConst
159 : : {
160 : : };
161 : : template <>
162 : : struct DimConst< 2 >
163 : : {
164 : 217709 : static inline double sqrt()
165 : : {
166 : 217709 : return MSQ_SQRT_TWO;
167 : : }
168 : 0 : static inline double inv()
169 : : {
170 : 0 : return 0.5;
171 : : }
172 : : };
173 : : template <>
174 : : struct DimConst< 3 >
175 : : {
176 : 0 : static inline double sqrt()
177 : : {
178 : 0 : return MSQ_SQRT_THREE;
179 : : }
180 : 0 : static inline double inv()
181 : : {
182 : 0 : return MSQ_ONE_THIRD;
183 : : }
184 : : };
185 : :
186 : : } // namespace MBMesquite
187 : :
188 : : #endif
|