Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2006 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 : : (2006) [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : /** \file TShapeSizeB3.cpp
28 : : * \brief
29 : : * \author Jason Kraftcheck
30 : : */
31 : :
32 : : #include "Mesquite.hpp"
33 : : #include "TShapeSizeB3.hpp"
34 : : #include "TMPDerivs.hpp"
35 : : #include "MsqError.hpp"
36 : :
37 : : #include <iostream>
38 : :
39 : : namespace MBMesquite
40 : : {
41 : :
42 : 0 : std::string TShapeSizeB3::get_name() const
43 : : {
44 [ # # ]: 0 : return "TShapeSizeB3";
45 : : }
46 : :
47 [ # # ]: 0 : TShapeSizeB3::~TShapeSizeB3() {}
48 : :
49 : 0 : bool TShapeSizeB3::evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
50 : : {
51 : 0 : const double tau = det( T );
52 [ # # ]: 0 : if( invalid_determinant( tau ) )
53 : : { // barrier
54 [ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
55 : 0 : return false;
56 : : }
57 : :
58 : 0 : result = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
59 : 0 : return true;
60 : : }
61 : :
62 : 0 : bool TShapeSizeB3::evaluate_with_grad( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
63 : : MsqError& err )
64 : : {
65 : 0 : const double tau = det( T );
66 [ # # ]: 0 : if( invalid_determinant( tau ) )
67 : : { // barrier
68 [ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
69 : 0 : return false;
70 : : }
71 : :
72 : 0 : result = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
73 : 0 : deriv_wrt_T = T;
74 [ # # ][ # # ]: 0 : deriv_wrt_T -= 1 / tau * transpose_adj( T );
75 : 0 : deriv_wrt_T *= 2;
76 : :
77 : 0 : return true;
78 : : }
79 : :
80 : 0 : bool TShapeSizeB3::evaluate_with_hess( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
81 : : MsqMatrix< 2, 2 > second_wrt_T[3], MsqError& err )
82 : : {
83 [ # # ]: 0 : const double tau = det( T );
84 [ # # ][ # # ]: 0 : if( invalid_determinant( tau ) )
85 : : { // barrier
86 [ # # ][ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
87 : 0 : return false;
88 : : }
89 : :
90 [ # # ]: 0 : result = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
91 : :
92 [ # # ]: 0 : const MsqMatrix< 2, 2 > adjt = transpose_adj( T );
93 : 0 : const double it = 1 / tau;
94 : 0 : deriv_wrt_T = T;
95 [ # # ][ # # ]: 0 : deriv_wrt_T -= it * adjt;
96 [ # # ]: 0 : deriv_wrt_T *= 2;
97 : :
98 [ # # ]: 0 : set_scaled_outer_product( second_wrt_T, 2 * it * it, adjt );
99 [ # # ]: 0 : pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -2 * it );
100 [ # # ]: 0 : pluseq_scaled_I( second_wrt_T, 2.0 );
101 : :
102 : 0 : return true;
103 : : }
104 : :
105 : 0 : bool TShapeSizeB3::evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& err )
106 : : {
107 : 0 : const double tau = det( T );
108 [ # # ]: 0 : if( invalid_determinant( tau ) )
109 : : { // barrier
110 [ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
111 : 0 : return false;
112 : : }
113 : :
114 : 0 : double n = Frobenius( T );
115 : 0 : result = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
116 : 0 : return true;
117 : : }
118 : :
119 : 0 : bool TShapeSizeB3::evaluate_with_grad( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv_wrt_T,
120 : : MsqError& err )
121 : : {
122 [ # # ]: 0 : const double tau = det( T );
123 [ # # ][ # # ]: 0 : if( invalid_determinant( tau ) )
124 : : { // barrier
125 [ # # ][ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
126 : 0 : return false;
127 : : }
128 : :
129 [ # # ]: 0 : double n = Frobenius( T );
130 : 0 : result = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
131 : :
132 [ # # ]: 0 : const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
133 : 0 : deriv_wrt_T = T;
134 [ # # ]: 0 : deriv_wrt_T *= 3 * n;
135 [ # # ][ # # ]: 0 : deriv_wrt_T -= 3 * MSQ_SQRT_THREE / tau * adjt;
136 : :
137 : 0 : return true;
138 : : }
139 : :
140 : 0 : bool TShapeSizeB3::evaluate_with_hess( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv_wrt_T,
141 : : MsqMatrix< 3, 3 > second_wrt_T[6], MsqError& err )
142 : : {
143 [ # # ]: 0 : const double tau = det( T );
144 [ # # ][ # # ]: 0 : if( invalid_determinant( tau ) )
145 : : { // barrier
146 [ # # ][ # # ]: 0 : MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
147 : 0 : return false;
148 : : }
149 : :
150 [ # # ]: 0 : double n = Frobenius( T );
151 : 0 : result = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
152 : :
153 [ # # ]: 0 : const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
154 : 0 : const double it = 1 / tau;
155 : 0 : deriv_wrt_T = T;
156 [ # # ]: 0 : deriv_wrt_T *= 3 * n;
157 [ # # ][ # # ]: 0 : deriv_wrt_T -= 3 * MSQ_SQRT_THREE * it * adjt;
158 : :
159 [ # # ]: 0 : if( n > 1e-50 )
160 : : {
161 [ # # ]: 0 : set_scaled_outer_product( second_wrt_T, 3 / n, T );
162 [ # # ]: 0 : pluseq_scaled_I( second_wrt_T, 3 * n );
163 [ # # ]: 0 : pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -3 * MSQ_SQRT_THREE * it, T );
164 [ # # ]: 0 : pluseq_scaled_outer_product( second_wrt_T, 3 * MSQ_SQRT_THREE * it * it, adjt );
165 : : }
166 : : else
167 : : {
168 [ # # ][ # # ]: 0 : std::cout << "Warning: Division by zero avoided in TShapeSizeB3::evaluate_with_hess()" << std::endl;
169 : : }
170 : :
171 : 0 : return true;
172 : : }
173 : :
174 [ + - ][ + - ]: 8 : } // namespace MBMesquite
|