MOAB: Mesh Oriented datABase  (version 5.2.1)
AWMetric.cpp
Go to the documentation of this file.
00001 /* *****************************************************************
00002     MESQUITE -- The Mesh Quality Improvement Toolkit
00003 
00004     Copyright 2008 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     (2008) kraftche@cae.wisc.edu
00024 
00025   ***************************************************************** */
00026 
00027 /** \file AWMetric.hpp
00028  *  \brief
00029  *  \author Jason Kraftcheck
00030  */
00031 
00032 #include "AWMetric.hpp"
00033 #include "TMetricBarrier.hpp"
00034 #include "MsqMatrix.hpp"
00035 #include "MsqError.hpp"
00036 #include <limits>
00037 
00038 namespace MBMesquite
00039 {
00040 
00041 template < unsigned Dim >
00042 static inline double do_finite_difference( int r, int c, AWMetric* metric, MsqMatrix< Dim, Dim > A,
00043                                            const MsqMatrix< Dim, Dim >& W, double value, MsqError& err )
00044 {
00045     const double INITAL_STEP = std::max( 1e-6, fabs( 1e-14 * value ) );
00046     const double init        = A( r, c );
00047     bool valid;
00048     double diff_value;
00049     for( double step = INITAL_STEP; step > std::numeric_limits< double >::epsilon(); step *= 0.1 )
00050     {
00051         A( r, c ) = init + step;
00052         valid     = metric->evaluate( A, W, diff_value, err );
00053         MSQ_ERRZERO( err );
00054         if( valid ) return ( diff_value - value ) / step;
00055     }
00056 
00057     // If we couldn't find a valid step, try stepping in the other
00058     // direciton
00059     for( double step = INITAL_STEP; step > std::numeric_limits< double >::epsilon(); step *= 0.1 )
00060     {
00061         A( r, c ) = init - step;
00062         valid     = metric->evaluate( A, W, diff_value, err );
00063         MSQ_ERRZERO( err );
00064         if( valid ) return ( value - diff_value ) / step;
00065     }
00066 
00067     // If that didn't work either, then give up.
00068     MSQ_SETERR( err )
00069     ( "No valid step size for finite difference of 2D target metric.", MsqError::INTERNAL_ERROR );
00070     return 0.0;
00071 }
00072 
00073 template < unsigned Dim >
00074 static inline bool do_numerical_gradient( AWMetric* mu, MsqMatrix< Dim, Dim > A, const MsqMatrix< Dim, Dim >& W,
00075                                           double& result, MsqMatrix< Dim, Dim >& wrt_A, MsqError& err )
00076 {
00077     bool valid;
00078     valid = mu->evaluate( A, W, result, err );
00079     MSQ_ERRZERO( err );
00080     if( MSQ_CHKERR( err ) || !valid ) return valid;
00081 
00082     switch( Dim )
00083     {
00084         case 3:
00085             wrt_A( 0, 2 ) = do_finite_difference( 0, 2, mu, A, W, result, err );
00086             MSQ_ERRZERO( err );
00087             wrt_A( 1, 2 ) = do_finite_difference( 1, 2, mu, A, W, result, err );
00088             MSQ_ERRZERO( err );
00089             wrt_A( 2, 0 ) = do_finite_difference( 2, 0, mu, A, W, result, err );
00090             MSQ_ERRZERO( err );
00091             wrt_A( 2, 1 ) = do_finite_difference( 2, 1, mu, A, W, result, err );
00092             MSQ_ERRZERO( err );
00093             wrt_A( 2, 2 ) = do_finite_difference( 2, 2, mu, A, W, result, err );
00094             MSQ_ERRZERO( err );
00095         case 2:
00096             wrt_A( 0, 1 ) = do_finite_difference( 0, 1, mu, A, W, result, err );
00097             MSQ_ERRZERO( err );
00098             wrt_A( 1, 0 ) = do_finite_difference( 1, 0, mu, A, W, result, err );
00099             MSQ_ERRZERO( err );
00100             wrt_A( 1, 1 ) = do_finite_difference( 1, 1, mu, A, W, result, err );
00101             MSQ_ERRZERO( err );
00102         case 1:
00103             wrt_A( 0, 0 ) = do_finite_difference( 0, 0, mu, A, W, result, err );
00104             MSQ_ERRZERO( err );
00105             break;
00106         default:
00107             assert( false );
00108     }
00109     return true;
00110 }
00111 
00112 template < unsigned Dim >
00113 static inline bool do_numerical_hessian( AWMetric* metric, MsqMatrix< Dim, Dim > A, const MsqMatrix< Dim, Dim >& W,
00114                                          double& value, MsqMatrix< Dim, Dim >& grad, MsqMatrix< Dim, Dim >* Hess,
00115                                          MsqError& err )
00116 {
00117     // zero hessian data
00118     const int num_block = Dim * ( Dim + 1 ) / 2;
00119     for( int i = 0; i < num_block; ++i )
00120         Hess[i].zero();
00121 
00122     // evaluate gradient for input values
00123     bool valid;
00124     valid = metric->evaluate_with_grad( A, W, value, grad, err );
00125     if( MSQ_CHKERR( err ) || !valid ) return false;
00126 
00127     // do finite difference for each term of A
00128     const double INITAL_STEP = std::max( 1e-6, fabs( 1e-14 * value ) );
00129     double value2;
00130     MsqMatrix< Dim, Dim > grad2;
00131     for( unsigned r = 0; r < Dim; ++r )
00132     {  // for each row of A
00133         for( unsigned c = 0; c < Dim; ++c )
00134         {  // for each column of A
00135             const double in_val = A( r, c );
00136             double step;
00137             for( step = INITAL_STEP; step > std::numeric_limits< double >::epsilon(); step *= 0.1 )
00138             {
00139                 A( r, c ) = in_val + step;
00140                 valid     = metric->evaluate_with_grad( A, W, value2, grad2, err );
00141                 MSQ_ERRZERO( err );
00142                 if( valid ) break;
00143             }
00144 
00145             // if no valid step size, try step in other direction
00146             if( !valid )
00147             {
00148                 for( step = -INITAL_STEP; step < -std::numeric_limits< double >::epsilon(); step *= 0.1 )
00149                 {
00150                     A( r, c ) = in_val + step;
00151                     valid     = metric->evaluate_with_grad( A, W, value2, grad2, err );
00152                     MSQ_ERRZERO( err );
00153                     if( valid ) break;
00154                 }
00155 
00156                 // if still no valid step size, give up.
00157                 if( !valid )
00158                 {
00159                     MSQ_SETERR( err )
00160                     ( "No valid step size for finite difference of 2D target metric.", MsqError::INTERNAL_ERROR );
00161                     return false;
00162                 }
00163             }
00164 
00165             // restore A.
00166             A( r, c ) = in_val;
00167 
00168             // add values into result matrix
00169             // values of grad2, in row-major order, are a single 9-value row of the Hessian
00170             grad2 -= grad;
00171             grad2 /= step;
00172             for( unsigned b = 0; b < r; ++b )
00173             {
00174                 const int idx = Dim * b - b * ( b + 1 ) / 2 + r;
00175                 Hess[idx].add_column( c, transpose( grad2.row( b ) ) );
00176             }
00177             for( unsigned b = r; b < Dim; ++b )
00178             {
00179                 const int idx = Dim * r - r * ( r + 1 ) / 2 + b;
00180                 Hess[idx].add_row( c, grad2.row( b ) );
00181             }
00182         }  // for (c)
00183     }      // for (r)
00184 
00185     // Values in non-diagonal blocks were added twice.
00186     for( unsigned r = 0, h = 1; r < Dim - 1; ++r, ++h )
00187         for( unsigned c = r + 1; c < Dim; ++c, ++h )
00188             Hess[h] *= 0.5;
00189 
00190     return true;
00191 }
00192 
00193 AWMetric::~AWMetric() {}
00194 
00195 bool AWMetric::evaluate( const MsqMatrix< 2, 2 >& /*A*/, const MsqMatrix< 2, 2 >& /*W*/, double& /*result*/,
00196                          MsqError& /*err*/ )
00197 {
00198     return false;
00199 }
00200 
00201 bool AWMetric::evaluate( const MsqMatrix< 3, 3 >& /*A*/, const MsqMatrix< 3, 3 >& /*W*/, double& /*result*/,
00202                          MsqError& /*err*/ )
00203 {
00204     return false;
00205 }
00206 
00207 bool AWMetric::evaluate_with_grad( const MsqMatrix< 2, 2 >& A, const MsqMatrix< 2, 2 >& W, double& result,
00208                                    MsqMatrix< 2, 2 >& wrt_A, MsqError& err )
00209 {
00210     return do_numerical_gradient( this, A, W, result, wrt_A, err );
00211 }
00212 
00213 bool AWMetric::evaluate_with_grad( const MsqMatrix< 3, 3 >& A, const MsqMatrix< 3, 3 >& W, double& result,
00214                                    MsqMatrix< 3, 3 >& wrt_A, MsqError& err )
00215 {
00216     return do_numerical_gradient( this, A, W, result, wrt_A, err );
00217 }
00218 
00219 bool AWMetric::evaluate_with_hess( const MsqMatrix< 2, 2 >& A, const MsqMatrix< 2, 2 >& W, double& result,
00220                                    MsqMatrix< 2, 2 >& deriv_wrt_A, MsqMatrix< 2, 2 > hess_wrt_A[3], MsqError& err )
00221 {
00222     return do_numerical_hessian( this, A, W, result, deriv_wrt_A, hess_wrt_A, err );
00223 }
00224 
00225 bool AWMetric::evaluate_with_hess( const MsqMatrix< 3, 3 >& A, const MsqMatrix< 3, 3 >& W, double& result,
00226                                    MsqMatrix< 3, 3 >& deriv_wrt_A, MsqMatrix< 3, 3 > hess_wrt_A[6], MsqError& err )
00227 {
00228     return do_numerical_hessian( this, A, W, result, deriv_wrt_A, hess_wrt_A, err );
00229 }
00230 
00231 AWMetric2D::~AWMetric2D() {}
00232 AWMetric3D::~AWMetric3D() {}
00233 
00234 bool AWMetric2D::evaluate( const MsqMatrix< 3, 3 >&, const MsqMatrix< 3, 3 >&, double&, MsqError& err )
00235 {
00236     MSQ_SETERR( err )
00237     ( "2D target metric cannot be evaluated for volume elements", MsqError::UNSUPPORTED_ELEMENT );
00238     return false;
00239 }
00240 
00241 bool AWMetric3D::evaluate( const MsqMatrix< 2, 2 >&, const MsqMatrix< 2, 2 >&, double&, MsqError& err )
00242 {
00243     MSQ_SETERR( err )
00244     ( "2D target metric cannot be evaluated for volume elements", MsqError::UNSUPPORTED_ELEMENT );
00245     return false;
00246 }
00247 
00248 }  // namespace MBMesquite
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines