MOAB: Mesh Oriented datABase  (version 5.2.1)
TShapeSize3DB4.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2009 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     (2009) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file TShapeSize3DB4.cpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "Mesquite.hpp"
00033 #include "TShapeSize3DB4.hpp"
00034 #include "TMPDerivs.hpp"
00035 #include "MsqError.hpp"
00036 
00037 #include <iostream>
00038 
00039 namespace MBMesquite
00040 {
00041 
00042 std::string TShapeSize3DB4::get_name() const
00043 {
00044     return "TShapeSize3DB4";
00045 }
00046 
00047 TShapeSize3DB4::~TShapeSize3DB4() {}
00048 
00049 bool TShapeSize3DB4::evaluate( const MsqMatrix< 3, 3 >& 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     const double norm = Frobenius( T );
00059     result            = norm * norm * norm / ( 3 * MSQ_SQRT_THREE * tau ) - 1 + mGamma * ( tau + 1 / tau - 2 );
00060     return true;
00061 }
00062 
00063 bool TShapeSize3DB4::evaluate_with_grad( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv,
00064                                          MsqError& err )
00065 {
00066     const double tau = det( T );
00067     if( invalid_determinant( tau ) )
00068     {  // barrier
00069         MSQ_SETERR( err )( barrier_violated_msg, MsqError::BARRIER_VIOLATED );
00070         return false;
00071     }
00072 
00073     const double norm    = Frobenius( T );
00074     const double f       = norm * norm / 3.0;
00075     const double g       = norm / ( MSQ_SQRT_THREE * tau );
00076     const double inv_tau = 1.0 / tau;
00077     result               = f * g - 1 + mGamma * ( tau + inv_tau - 2 );
00078 
00079     deriv = g * T;
00080     deriv += ( mGamma * ( 1 - inv_tau * inv_tau ) - f * g * inv_tau ) * transpose_adj( T );
00081 
00082     return true;
00083 }
00084 
00085 bool TShapeSize3DB4::evaluate_with_hess( const MsqMatrix< 3, 3 >& T, double& result, MsqMatrix< 3, 3 >& deriv,
00086                                          MsqMatrix< 3, 3 > second[6], 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     const double norm    = Frobenius( T );
00096     const double f       = norm * norm / 3.0;
00097     const double h       = 1 / ( MSQ_SQRT_THREE * tau );
00098     const double g       = norm * h;
00099     const double inv_tau = 1.0 / tau;
00100     result               = f * g - 1 + mGamma * ( tau + inv_tau - 2 );
00101 
00102     const double g1              = mGamma * ( 1 - inv_tau * inv_tau );
00103     const MsqMatrix< 3, 3 > adjt = transpose_adj( T );
00104     deriv                        = g * T;
00105     deriv += ( g1 - f * g * inv_tau ) * adjt;
00106 
00107     if( norm > 1e-50 )
00108     {
00109         const double inv_norm = 1 / norm;
00110         set_scaled_outer_product( second, h * inv_norm, T );
00111         pluseq_scaled_I( second, norm * h );
00112         pluseq_scaled_2nd_deriv_of_det( second, g1 - f * g * inv_tau, T );
00113         pluseq_scaled_outer_product( second, ( f * g + mGamma * inv_tau ) * 2 * inv_tau * inv_tau, adjt );
00114         pluseq_scaled_sum_outer_product( second, -g * inv_tau, T, adjt );
00115     }
00116     else
00117     {
00118         std::cout << "Warning: Division by zero avoided in TShapeSize3DB4::evaluate_with_hess()" << std::endl;
00119     }
00120 
00121     return true;
00122 }
00123 
00124 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines