MOAB: Mesh Oriented datABase  (version 5.2.1)
TShapeSize2DB2.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) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file TShapeSize2DB2.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "TShapeSize2DB2.hpp"
00034 #include "MsqMatrix.hpp"
00035 #include "MsqError.hpp"
00036 #include "TMPDerivs.hpp"
00037 
00038 #include <iostream>
00039 
00040 namespace MBMesquite
00041 {
00042 
00043 std::string TShapeSize2DB2::get_name() const
00044 {
00045     return "TShapeSize2DB2";
00046 }
00047 
00048 TShapeSize2DB2::~TShapeSize2DB2() {}
00049 
00050 bool TShapeSize2DB2::evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
00051 {
00052     const double two_det = 2.0 * det( T );
00053     if( invalid_determinant( two_det ) )
00054     {  // barrier
00055         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00056         return false;
00057     }
00058 
00059     const double frob_sqr = sqr_Frobenius( T );
00060     result                = ( frob_sqr - 2.0 * sqrt( frob_sqr + two_det ) + 2.0 ) / two_det;
00061     return true;
00062 }
00063 
00064 bool TShapeSize2DB2::evaluate_with_grad( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
00065                                          MsqError& err )
00066 {
00067     const double d = det( T );
00068     if( invalid_determinant( d ) )
00069     {  // barrier
00070         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00071         return false;
00072     }
00073     const double frob_sqr = sqr_Frobenius( T );
00074     const double psi      = sqrt( frob_sqr + 2.0 * det( T ) );
00075     const double v        = frob_sqr - 2.0 * psi + 2.0;
00076     result                = v / ( 2 * d );
00077 
00078     // deriv of V wrt T
00079     MsqMatrix< 2, 2 > adjt = transpose_adj( T );
00080     MsqMatrix< 2, 2 > v_wrt_T( T );
00081     if( psi > 1e-50 )
00082     {
00083         v_wrt_T *= ( 1.0 - 1.0 / psi );
00084         v_wrt_T -= 1.0 / psi * adjt;
00085         v_wrt_T *= 2;
00086     }
00087     else
00088     {
00089         std::cout << "Warning: Division by zero avoided in TShapeSize2DB2::evaluate_with_grad()" << std::endl;
00090     }
00091 
00092     // deriv of mu wrt T
00093     deriv_wrt_T = v_wrt_T;
00094     deriv_wrt_T *= 0.5 / d;
00095     deriv_wrt_T -= v / ( 2 * d * d ) * adjt;
00096 
00097     return true;
00098 }
00099 
00100 bool TShapeSize2DB2::evaluate_with_hess( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
00101                                          MsqMatrix< 2, 2 > second[3], MsqError& err )
00102 {
00103     const double d = det( T );
00104     if( invalid_determinant( d ) )
00105     {  // barrier
00106         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00107         return false;
00108     }
00109     const double frob_sqr = sqr_Frobenius( T );
00110     const double psi      = sqrt( frob_sqr + 2.0 * det( T ) );
00111     const double v        = frob_sqr - 2.0 * psi + 2.0;
00112     result                = v / ( 2 * d );
00113 
00114     // deriv of V wrt T
00115     MsqMatrix< 2, 2 > adjt = transpose_adj( T );
00116     MsqMatrix< 2, 2 > v_wrt_T( T );
00117     if( psi > 1e-50 )
00118     {
00119         v_wrt_T *= ( 1.0 - 1.0 / psi );
00120         v_wrt_T -= 1.0 / psi * adjt;
00121         v_wrt_T *= 2;
00122     }
00123     else
00124     {
00125         std::cout << "Warning: Division by zero avoided in TShapeSize2DB2::evaluate_with_hess()" << std::endl;
00126     }
00127 
00128     // deriv of mu wrt T
00129     deriv_wrt_T = v_wrt_T;
00130     deriv_wrt_T *= 0.5 / d;
00131     deriv_wrt_T -= v / ( 2 * d * d ) * adjt;
00132 
00133     // second of V wrt T
00134     const double s    = T( 0, 1 ) - T( 1, 0 );
00135     const double tr   = trace( T );
00136     const double f    = -2.0 / ( psi * psi * psi );
00137     second[0]( 0, 0 ) = second[1]( 0, 1 ) = second[2]( 1, 1 ) = f * s * s;
00138     second[0]( 0, 1 ) = second[0]( 1, 0 ) = second[1]( 1, 1 ) = -f * s * tr;
00139     second[1]( 0, 0 ) = second[2]( 0, 1 ) = second[2]( 1, 0 ) = f * s * tr;
00140     second[0]( 1, 1 ) = second[2]( 0, 0 ) = -( second[1]( 1, 0 ) = -f * tr * tr );
00141     pluseq_scaled_I( second, 2 );
00142 
00143     // second of mu wrt T
00144     const double x = 1.0 / ( 2 * d );
00145     second[0] *= x;
00146     second[1] *= x;
00147     second[2] *= x;
00148     pluseq_scaled_2nd_deriv_of_det( second, v / ( -2 * d * d ) );
00149     pluseq_scaled_outer_product( second, v / ( d * d * d ), adjt );
00150     pluseq_scaled_sum_outer_product( second, -1 / ( 2 * d * d ), v_wrt_T, adjt );
00151 
00152     return true;
00153 }
00154 
00155 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines