MOAB: Mesh Oriented datABase  (version 5.4.1)
TShapeSizeB3.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2006 Sandia National Laboratories.  Developed at the
00005     University of Wisconsin--Madison under SNL contract number
00006     624796.  The U.S. Government and the University of Wisconsin
00007     retain certain rights to this software.
00008 
00009     This library is free software; you can redistribute it and/or
00010     modify it under the terms of the GNU Lesser General Public
00011     License as published by the Free Software Foundation; either
00012     version 2.1 of the License, or (at your option) any later version.
00013 
00014     This library is distributed in the hope that it will be useful,
00015     but WITHOUT ANY WARRANTY; without even the implied warranty of
00016     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017     Lesser General Public License for more details.
00018 
00019     You should have received a copy of the GNU Lesser General Public License
00020     (lgpl.txt) along with this library; if not, write to the Free Software
00021     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022 
00023     (2006) [email protected]
00024 
00025   ***************************************************************** */
00026 
00027 /** \file TShapeSizeB3.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "TShapeSizeB3.hpp"
00034 #include "TMPDerivs.hpp"
00035 #include "MsqError.hpp"
00036 
00037 #include <iostream>
00038 
00039 namespace MBMesquite
00040 {
00041 
00042 std::string TShapeSizeB3::get_name() const
00043 {
00044     return "TShapeSizeB3";
00045 }
00046 
00047 TShapeSizeB3::~TShapeSizeB3() {}
00048 
00049 bool TShapeSizeB3::evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
00050 {
00051     const double tau = det( T );
00052     if( invalid_determinant( tau ) )
00053     {  // barrier
00054         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00055         return false;
00056     }
00057 
00058     result = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
00059     return true;
00060 }
00061 
00062 bool TShapeSizeB3::evaluate_with_grad( const MsqMatrix< 2, 2 >& T,
00063                                        double& result,
00064                                        MsqMatrix< 2, 2 >& deriv_wrt_T,
00065                                        MsqError& err )
00066 {
00067     const double tau = det( T );
00068     if( invalid_determinant( tau ) )
00069     {  // barrier
00070         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00071         return false;
00072     }
00073 
00074     result      = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
00075     deriv_wrt_T = T;
00076     deriv_wrt_T -= 1 / tau * transpose_adj( T );
00077     deriv_wrt_T *= 2;
00078 
00079     return true;
00080 }
00081 
00082 bool TShapeSizeB3::evaluate_with_hess( const MsqMatrix< 2, 2 >& T,
00083                                        double& result,
00084                                        MsqMatrix< 2, 2 >& deriv_wrt_T,
00085                                        MsqMatrix< 2, 2 > second_wrt_T[3],
00086                                        MsqError& err )
00087 {
00088     const double tau = det( T );
00089     if( invalid_determinant( tau ) )
00090     {  // barrier
00091         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00092         return false;
00093     }
00094 
00095     result = sqr_Frobenius( T ) - 2.0 * std::log( tau ) - 2;
00096 
00097     const MsqMatrix< 2, 2 > adjt = transpose_adj( T );
00098     const double it              = 1 / tau;
00099     deriv_wrt_T                  = T;
00100     deriv_wrt_T -= it * adjt;
00101     deriv_wrt_T *= 2;
00102 
00103     set_scaled_outer_product( second_wrt_T, 2 * it * it, adjt );
00104     pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -2 * it );
00105     pluseq_scaled_I( second_wrt_T, 2.0 );
00106 
00107     return true;
00108 }
00109 
00110 bool TShapeSizeB3::evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& err )
00111 {
00112     const double tau = det( T );
00113     if( invalid_determinant( tau ) )
00114     {  // barrier
00115         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00116         return false;
00117     }
00118 
00119     double n = Frobenius( T );
00120     result   = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
00121     return true;
00122 }
00123 
00124 bool TShapeSizeB3::evaluate_with_grad( const MsqMatrix< 3, 3 >& T,
00125                                        double& result,
00126                                        MsqMatrix< 3, 3 >& deriv_wrt_T,
00127                                        MsqError& err )
00128 {
00129     const double tau = det( T );
00130     if( invalid_determinant( tau ) )
00131     {  // barrier
00132         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00133         return false;
00134     }
00135 
00136     double n = Frobenius( T );
00137     result   = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
00138 
00139     const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
00140     deriv_wrt_T                  = T;
00141     deriv_wrt_T *= 3 * n;
00142     deriv_wrt_T -= 3 * MSQ_SQRT_THREE / tau * adjt;
00143 
00144     return true;
00145 }
00146 
00147 bool TShapeSizeB3::evaluate_with_hess( const MsqMatrix< 3, 3 >& T,
00148                                        double& result,
00149                                        MsqMatrix< 3, 3 >& deriv_wrt_T,
00150                                        MsqMatrix< 3, 3 > second_wrt_T[6],
00151                                        MsqError& err )
00152 {
00153     const double tau = det( T );
00154     if( invalid_determinant( tau ) )
00155     {  // barrier
00156         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00157         return false;
00158     }
00159 
00160     double n = Frobenius( T );
00161     result   = n * n * n - 3 * MSQ_SQRT_THREE * ( log( tau ) + 1 );
00162 
00163     const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
00164     const double it              = 1 / tau;
00165     deriv_wrt_T                  = T;
00166     deriv_wrt_T *= 3 * n;
00167     deriv_wrt_T -= 3 * MSQ_SQRT_THREE * it * adjt;
00168 
00169     if( n > 1e-50 )
00170     {
00171         set_scaled_outer_product( second_wrt_T, 3 / n, T );
00172         pluseq_scaled_I( second_wrt_T, 3 * n );
00173         pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -3 * MSQ_SQRT_THREE * it, T );
00174         pluseq_scaled_outer_product( second_wrt_T, 3 * MSQ_SQRT_THREE * it * it, adjt );
00175     }
00176     else
00177     {
00178         std::cout << "Warning: Division by zero avoided in TShapeSizeB3::evaluate_with_hess()" << std::endl;
00179     }
00180 
00181     return true;
00182 }
00183 
00184 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines