MOAB: Mesh Oriented datABase  (version 5.2.1)
TShapeOrientB1.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     (2010) kraftche@cae.wisc.edu
00025 
00026   ***************************************************************** */
00027 
00028 /** \file TShapeOrientB1.cpp
00029  *  \brief
00030  *  \author Jason Kraftcheck
00031  */
00032 
00033 #include "Mesquite.hpp"
00034 #include "TShapeOrientB1.hpp"
00035 #include "MsqMatrix.hpp"
00036 #include "MsqError.hpp"
00037 #include "TMPDerivs.hpp"
00038 #include "TMPCommon.hpp"
00039 
00040 namespace MBMesquite
00041 {
00042 
00043 std::string TShapeOrientB1::get_name() const
00044 {
00045     return "TShapeOrientB1";
00046 }
00047 
00048 TShapeOrientB1::~TShapeOrientB1() {}
00049 
00050 bool TShapeOrientB1::evaluate( const MsqMatrix< 2, 2 >& T, double& result, MsqError& err )
00051 {
00052     const double tau = det( T );
00053     if( TMetric::invalid_determinant( tau ) )
00054     {  // barrier
00055         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00056         return false;
00057     }
00058     result = 0.5 / tau * ( Frobenius( T ) - trace( T ) / MSQ_SQRT_TWO );
00059     return true;
00060 }
00061 
00062 bool TShapeOrientB1::evaluate_with_grad( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
00063                                          MsqError& err )
00064 {
00065     const double norm    = Frobenius( T );
00066     const double invroot = 1.0 / MSQ_SQRT_TWO;
00067     const double tau     = det( T );
00068     if( TMetric::invalid_determinant( tau ) )
00069     {  // barrier
00070         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00071         return false;
00072     }
00073     const double inv_tau = 1.0 / tau;
00074     const double invnorm = 1.0 / norm;
00075 
00076     result = 0.5 * inv_tau * ( norm - invroot * trace( T ) );
00077 
00078     deriv_wrt_T = invnorm * T;
00079     pluseq_scaled_I( deriv_wrt_T, -invroot );
00080     deriv_wrt_T *= 0.5;
00081     deriv_wrt_T -= result * transpose_adj( T );
00082     deriv_wrt_T *= inv_tau;
00083     return true;
00084 }
00085 
00086 bool TShapeOrientB1::evaluate_with_hess( const MsqMatrix< 2, 2 >& T, double& result, MsqMatrix< 2, 2 >& deriv_wrt_T,
00087                                          MsqMatrix< 2, 2 > second_wrt_T[3], MsqError& err )
00088 {
00089     const double norm    = Frobenius( T );
00090     const double invroot = 1.0 / MSQ_SQRT_TWO;
00091     const double tau     = det( T );
00092     if( TMetric::invalid_determinant( tau ) )
00093     {  // barrier
00094         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00095         return false;
00096     }
00097     const double inv_tau = 1.0 / tau;
00098     const double invnorm = 1.0 / norm;
00099 
00100     const double f = norm - invroot * trace( T );
00101     result         = 0.5 * inv_tau * f;
00102 
00103     const MsqMatrix< 2, 2 > adjt = transpose_adj( T );
00104     deriv_wrt_T                  = invnorm * T;
00105     pluseq_scaled_I( deriv_wrt_T, -invroot );
00106     deriv_wrt_T *= 0.5;
00107     deriv_wrt_T -= result * adjt;
00108     deriv_wrt_T *= inv_tau;
00109 
00110     const double a = 0.5 * inv_tau * invnorm;
00111     set_scaled_outer_product( second_wrt_T, -a * invnorm * invnorm, T );
00112     pluseq_scaled_I( second_wrt_T, a );
00113     pluseq_scaled_outer_product( second_wrt_T, f * inv_tau * inv_tau * inv_tau, adjt );
00114     pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -0.5 * f * inv_tau * inv_tau, T );
00115     pluseq_scaled_sum_outer_product( second_wrt_T, -0.5 * inv_tau * inv_tau * invnorm, T, adjt );
00116     pluseq_scaled_sum_outer_product_I( second_wrt_T, 0.5 * inv_tau * inv_tau * invroot, adjt );
00117     return true;
00118 }
00119 
00120 bool TShapeOrientB1::evaluate( const MsqMatrix< 3, 3 >& T, double& result, MsqError& err )
00121 {
00122     const double tau = det( T );
00123     if( TMetric::invalid_determinant( tau ) )
00124     {  // barrier
00125         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00126         return false;
00127     }
00128     result = 0.5 / tau * ( Frobenius( T ) - trace( T ) / MSQ_SQRT_THREE );
00129     return true;
00130 }
00131 
00132 bool TShapeOrientB1::evaluate_with_grad( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv_wrt_T,
00133                                          MsqError& err )
00134 {
00135     const double norm    = Frobenius( T );
00136     const double invroot = 1.0 / MSQ_SQRT_THREE;
00137     const double tau     = det( T );
00138     if( TMetric::invalid_determinant( tau ) )
00139     {  // barrier
00140         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00141         return false;
00142     }
00143     const double inv_tau = 1.0 / tau;
00144     const double invnorm = 1.0 / norm;
00145 
00146     result = 0.5 * inv_tau * ( norm - invroot * trace( T ) );
00147 
00148     deriv_wrt_T = invnorm * T;
00149     pluseq_scaled_I( deriv_wrt_T, -invroot );
00150     deriv_wrt_T *= 0.5;
00151     deriv_wrt_T -= result * transpose_adj( T );
00152     deriv_wrt_T *= inv_tau;
00153     return true;
00154 }
00155 
00156 bool TShapeOrientB1::evaluate_with_hess( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv_wrt_T,
00157                                          MsqMatrix< 3, 3 > second_wrt_T[6], MsqError& err )
00158 {
00159     const double norm    = Frobenius( T );
00160     const double invroot = 1.0 / MSQ_SQRT_THREE;
00161     const double tau     = det( T );
00162     if( TMetric::invalid_determinant( tau ) )
00163     {  // barrier
00164         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00165         return false;
00166     }
00167     const double inv_tau = 1.0 / tau;
00168     const double invnorm = 1.0 / norm;
00169 
00170     const double f = norm - invroot * trace( T );
00171     result         = 0.5 * inv_tau * f;
00172 
00173     const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
00174     deriv_wrt_T                  = invnorm * T;
00175     pluseq_scaled_I( deriv_wrt_T, -invroot );
00176     deriv_wrt_T *= 0.5;
00177     deriv_wrt_T -= result * adjt;
00178     deriv_wrt_T *= inv_tau;
00179 
00180     const double a = 0.5 * inv_tau * invnorm;
00181     set_scaled_outer_product( second_wrt_T, -a * invnorm * invnorm, T );
00182     pluseq_scaled_I( second_wrt_T, a );
00183     pluseq_scaled_outer_product( second_wrt_T, f * inv_tau * inv_tau * inv_tau, adjt );
00184     pluseq_scaled_2nd_deriv_of_det( second_wrt_T, -0.5 * f * inv_tau * inv_tau, T );
00185     pluseq_scaled_sum_outer_product( second_wrt_T, -0.5 * inv_tau * inv_tau * invnorm, T, adjt );
00186     pluseq_scaled_sum_outer_product_I( second_wrt_T, 0.5 * inv_tau * inv_tau * invroot, adjt );
00187     return true;
00188 }
00189 
00190 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines