LCOV - code coverage report
Current view: top level - src/mesquite/QualityMetric - QualityMetric.hpp (source / functions) Hit Total Coverage
Test: coverage_sk.info Lines: 2 2 100.0 %
Date: 2020-07-18 00:09:26 Functions: 2 2 100.0 %
Branches: 1 2 50.0 %

           Branch data     Line data    Source code
       1                 :            : /* *****************************************************************
       2                 :            :     MESQUITE -- The Mesh Quality Improvement Toolkit
       3                 :            : 
       4                 :            :     Copyright 2004 Sandia Corporation and Argonne National
       5                 :            :     Laboratory.  Under the terms of Contract DE-AC04-94AL85000
       6                 :            :     with Sandia Corporation, the U.S. Government retains certain
       7                 :            :     rights in this software.
       8                 :            : 
       9                 :            :     This library is free software; you can redistribute it and/or
      10                 :            :     modify it under the terms of the GNU Lesser General Public
      11                 :            :     License as published by the Free Software Foundation; either
      12                 :            :     version 2.1 of the License, or (at your option) any later version.
      13                 :            : 
      14                 :            :     This library is distributed in the hope that it will be useful,
      15                 :            :     but WITHOUT ANY WARRANTY; without even the implied warranty of
      16                 :            :     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
      17                 :            :     Lesser General Public License for more details.
      18                 :            : 
      19                 :            :     You should have received a copy of the GNU Lesser General Public License
      20                 :            :     (lgpl.txt) along with this library; if not, write to the Free Software
      21                 :            :     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
      22                 :            : 
      23                 :            :     [email protected], [email protected], [email protected],
      24                 :            :     [email protected], [email protected], [email protected]
      25                 :            : 
      26                 :            :     (2006) [email protected]
      27                 :            : 
      28                 :            :   ***************************************************************** */
      29                 :            : 
      30                 :            : /*! \file QualityMetric.hpp
      31                 :            :     \brief
      32                 :            : Header file for the MBMesquite::QualityMetric class
      33                 :            : 
      34                 :            :   \author Thomas Leurent
      35                 :            :   \author Michael Brewer
      36                 :            :   \date   2002-05-01
      37                 :            :  */
      38                 :            : 
      39                 :            : #ifndef QualityMetric_hpp
      40                 :            : #define QualityMetric_hpp
      41                 :            : 
      42                 :            : #include <cmath>
      43                 :            : #include <vector>
      44                 :            : #include <algorithm>
      45                 :            : 
      46                 :            : #include "Mesquite.hpp"
      47                 :            : #include "Vector3D.hpp"
      48                 :            : #include "Matrix3D.hpp"
      49                 :            : 
      50                 :            : #ifdef _WIN32
      51                 :            : typedef unsigned uint32_t;
      52                 :            : #elif defined( MOAB_HAVE_STDINT_H )
      53                 :            : #include <stdint.h>
      54                 :            : #elif defined( MOAB_HAVE_INTTYPES_H )
      55                 :            : #include <inttypes.h>
      56                 :            : #endif
      57                 :            : 
      58                 :            : namespace MBMesquite
      59                 :            : {
      60                 :            : 
      61                 :            : /*! \class QualityMetric
      62                 :            :   \brief Base class for concrete quality metrics.
      63                 :            : */
      64                 :            : class PatchData;
      65                 :            : class MsqMeshEntity;
      66                 :            : class Mesh;
      67                 :            : class MeshDomain;
      68                 :            : class MeshDomainAssoc;
      69                 :            : class Settings;
      70                 :            : 
      71                 :            : class QualityMetric
      72                 :            : {
      73                 :            :   protected:
      74                 :        358 :     QualityMetric() : keepFiniteDiffEps( false ), haveFiniteDiffEps( false ) {}
      75                 :            : 
      76                 :            :   public:
      77                 :            :     enum MetricType
      78                 :            :     {
      79                 :            :         VERTEX_BASED, /**< Iterate over vertices to evaluate metric. */
      80                 :            :         ELEMENT_BASED /**< Iterate over elements to evaluate metric. */
      81                 :            :     };
      82                 :            : 
      83         [ -  + ]:        344 :     MESQUITE_EXPORT virtual ~QualityMetric() {}
      84                 :            : 
      85                 :            :     MESQUITE_EXPORT virtual MetricType get_metric_type() const = 0;
      86                 :            : 
      87                 :            :     MESQUITE_EXPORT virtual std::string get_name() const = 0;
      88                 :            : 
      89                 :            :     //! 1 if metric should be minimized, -1 if metric should be maximized.
      90                 :            :     MESQUITE_EXPORT virtual int get_negate_flag() const = 0;
      91                 :            : 
      92                 :            :     /**\brief Get locations at which metric can be evaluated
      93                 :            :      *
      94                 :            :      * Different metrics are evaluated for different things within
      95                 :            :      * a patch.  For example, an element-based metric will be evaluated
      96                 :            :      * once for each element in patch, a vertex-based metric once for
      97                 :            :      * each veretx, and a target/sample-point based metric will be
      98                 :            :      * evaluated once for each samle point in each element.  This method
      99                 :            :      * returns a list of handles, one for each location in the patch
     100                 :            :      * at which the metric can be evaluated.  The handle values are used
     101                 :            :      * as input to the evaluate methods.
     102                 :            :      *\param pd       The patch
     103                 :            :      *\param handles  Output list of handles
     104                 :            :      *\param free_vertices_only If true, only pass back evaluation points
     105                 :            :      *         that depend on at least one free vertex.
     106                 :            :      */
     107                 :            :     MESQUITE_EXPORT virtual void get_evaluations( PatchData& pd, std::vector< size_t >& handles,
     108                 :            :                                                   bool free_vertices_only, MsqError& err ) = 0;
     109                 :            : 
     110                 :            :     /**\brief Get locations at which metric can be evaluated for
     111                 :            :      *        use in BCD intialization and QualityAssessor.
     112                 :            :      *
     113                 :            :      * For element-based, sample-based, and vertex-based metrics,
     114                 :            :      * this function is the same as get_evaluations.  For edge-based
     115                 :            :      * metrics it returns only a subset of the results for get_evaluations
     116                 :            :      * such that each edge in the mesh is visited only once even though
     117                 :            :      * it would normally be visited twice when iterating over patches
     118                 :            :      * of the mesh.  This assumes that no vertex occurs in more than one
     119                 :            :      * patch without its MSQ_PATCH_FIXED flag set.  This assumption is true for
     120                 :            :      * both element-on-vertex and global patches.
     121                 :            :      *\param pd       The patch
     122                 :            :      *\param handles  Output list of handles
     123                 :            :      *\param free_vertices_only If true, only pass back evaluation points
     124                 :            :      *         that depend on at least one free vertex.
     125                 :            :      */
     126                 :            :     MESQUITE_EXPORT virtual void get_single_pass( PatchData& pd, std::vector< size_t >& handles,
     127                 :            :                                                   bool free_vertices_only, MsqError& err );
     128                 :            : 
     129                 :            :     /**\brief Get metric value at a logical location in the patch.
     130                 :            :      *
     131                 :            :      * Evaluate the metric at one location in the PatchData.
     132                 :            :      *\param pd     The patch.
     133                 :            :      *\param handle The location in the patch (as passed back from get_evaluations).
     134                 :            :      *\param value  The output metric value.
     135                 :            :      */
     136                 :            :     MESQUITE_EXPORT virtual bool evaluate( PatchData& pd, size_t handle, double& value, MsqError& err ) = 0;
     137                 :            : 
     138                 :            :     /**\brief Get metric value at a logical location in the patch.
     139                 :            :      *
     140                 :            :      * Evaluate the metric at one location in the PatchData.
     141                 :            :      *\param pd      The patch.
     142                 :            :      *\param handle  The location in the patch (as passed back from get_evaluations).
     143                 :            :      *\param value   The output metric value.
     144                 :            :      *\param indices The free vertices that the evaluation is a function
     145                 :            :      *               of, specified as vertex indices in the PatchData.
     146                 :            :      */
     147                 :            :     MESQUITE_EXPORT virtual bool evaluate_with_indices( PatchData& pd, size_t handle, double& value,
     148                 :            :                                                         std::vector< size_t >& indices, MsqError& err ) = 0;
     149                 :            : 
     150                 :            :     /**\brief Get metric value and gradient at a logical location in the patch.
     151                 :            :      *
     152                 :            :      * Evaluate the metric at one location in the PatchData.
     153                 :            :      *\param pd      The patch.
     154                 :            :      *\param handle  The location in the patch (as passed back from get_evaluations).
     155                 :            :      *\param value   The output metric value.
     156                 :            :      *\param indices The free vertices that the evaluation is a function
     157                 :            :      *               of, specified as vertex indices in the PatchData.
     158                 :            :      *\param gradient The gradient of the metric as a function of the
     159                 :            :      *               coordinates of the free vertices passed back in
     160                 :            :      *               the indices list.
     161                 :            :      */
     162                 :            :     MESQUITE_EXPORT virtual bool evaluate_with_gradient( PatchData& pd, size_t handle, double& value,
     163                 :            :                                                          std::vector< size_t >& indices,
     164                 :            :                                                          std::vector< Vector3D >& gradient, MsqError& err );
     165                 :            : 
     166                 :            :     /**\brief Get metric value and gradient at a logical location in the patch.
     167                 :            :      *
     168                 :            :      * Evaluate the metric at one location in the PatchData.
     169                 :            :      *\param pd      The patch.
     170                 :            :      *\param handle  The location in the patch (as passed back from get_evaluations).
     171                 :            :      *\param value   The output metric value.
     172                 :            :      *\param indices The free vertices that the evaluation is a function
     173                 :            :      *               of, specified as vertex indices in the PatchData.
     174                 :            :      *\param gradient The gradient of the metric as a function of the
     175                 :            :      *               coordinates of the free vertices passed back in
     176                 :            :      *               the indices list.
     177                 :            :      *\param Hessian_diagonal The 3x3 blocks along the diagonal of
     178                 :            :      *               the Hessian matrix.
     179                 :            :      */
     180                 :            :     MESQUITE_EXPORT virtual bool evaluate_with_Hessian_diagonal( PatchData& pd, size_t handle, double& value,
     181                 :            :                                                                  std::vector< size_t >& indices,
     182                 :            :                                                                  std::vector< Vector3D >& gradient,
     183                 :            :                                                                  std::vector< SymMatrix3D >& Hessian_diagonal,
     184                 :            :                                                                  MsqError& err );
     185                 :            : 
     186                 :            :     /**\brief Get metric value and deravitives at a logical location in the patch.
     187                 :            :      *
     188                 :            :      * Evaluate the metric at one location in the PatchData.
     189                 :            :      *\param pd      The patch.
     190                 :            :      *\param handle  The location in the patch (as passed back from get_evaluations).
     191                 :            :      *\param value   The output metric value.
     192                 :            :      *\param indices The free vertices that the evaluation is a function
     193                 :            :      *               of, specified as vertex indices in the PatchData.
     194                 :            :      *\param gradient The gradient of the metric as a function of the
     195                 :            :      *               coordinates of the free vertices passed back in
     196                 :            :      *               the indices list.
     197                 :            :      *\param Hessian The Hessian of the metric as a function of the
     198                 :            :      *               coordinates. The Hessian is passed back as the
     199                 :            :      *               upper-triangular portion of the matrix in row-major
     200                 :            :      *               order, where each Matrix3D is the portion of the
     201                 :            :      *               Hessian with respect to the vertices at the
     202                 :            :      *               corresponding positions in the indices list.
     203                 :            :      */
     204                 :            :     MESQUITE_EXPORT virtual bool evaluate_with_Hessian( PatchData& pd, size_t handle, double& value,
     205                 :            :                                                         std::vector< size_t >& indices,
     206                 :            :                                                         std::vector< Vector3D >& gradient,
     207                 :            :                                                         std::vector< Matrix3D >& Hessian, MsqError& err );
     208                 :            : 
     209                 :            :     //! Escobar Barrier Function for Shape and Other Metrics
     210                 :            :     // det = signed determinant of Jacobian Matrix at a Vertex
     211                 :            :     // delta = scaling parameter
     212                 :            :     static inline double vertex_barrier_function( double det, double delta )
     213                 :            :     {
     214                 :            :         return 0.5 * ( det + sqrt( det * det + 4 * delta * delta ) );
     215                 :            :     }
     216                 :            :     // protected:
     217                 :            : 
     218                 :            :     /** \brief Remove from vector any gradient terms corresponding
     219                 :            :      *         to a fixed vertex.
     220                 :            :      *
     221                 :            :      * Remove terms from vector that correspond to fixed vertices.
     222                 :            :      *\param type            Element type
     223                 :            :      *\param fixed_vertices  Bit flags, one per vertex, 1 if
     224                 :            :      *                       vertex is fixed.
     225                 :            :      *\param gradients       Array of gradients
     226                 :            :      */
     227                 :            :     MESQUITE_EXPORT
     228                 :            :     static void remove_fixed_gradients( EntityTopology type, uint32_t fixed_vertices,
     229                 :            :                                         std::vector< Vector3D >& gradients );
     230                 :            : 
     231                 :            :     /** \brief Remove from vectors any gradient terms and hessian
     232                 :            :      *         diagonal blcoks corresponding to a fixed vertex.
     233                 :            :      *
     234                 :            :      * Remove terms from vector that correspond to fixed vertices.
     235                 :            :      *\param type            Element type
     236                 :            :      *\param fixed_vertices  Bit flags, one per vertex, 1 if
     237                 :            :      *                       vertex is fixed.
     238                 :            :      *\param gradients       Array of gradients
     239                 :            :      *\param hess_diagonal_blocks   Array of diagonal blocks of Hessian matrix.
     240                 :            :      */
     241                 :            :     MESQUITE_EXPORT
     242                 :            :     static void remove_fixed_diagonals( EntityTopology type, uint32_t fixed_vertices,
     243                 :            :                                         std::vector< Vector3D >& gradients,
     244                 :            :                                         std::vector< SymMatrix3D >& hess_diagonal_blocks );
     245                 :            : 
     246                 :            :     /** \brief Remove from vector any Hessian blocks corresponding
     247                 :            :      *         to a fixed vertex.
     248                 :            :      *
     249                 :            :      * Remove blocks from vector that correspond to fixed vertices.
     250                 :            :      *\param type            Element type
     251                 :            :      *\param fixed_vertices  Bit flags, one per vertex, 1 if
     252                 :            :      *                       vertex is fixed.
     253                 :            :      *\param hessians        Array of Hessian blocks (upper trianguler, row-major)
     254                 :            :      */
     255                 :            :     MESQUITE_EXPORT
     256                 :            :     static void remove_fixed_hessians( EntityTopology type, uint32_t fixed_vertices,
     257                 :            :                                        std::vector< Matrix3D >& hessians );
     258                 :            : 
     259                 :            :     /** \brief Convert fixed vertex format from list to bit flags
     260                 :            :      *
     261                 :            :      * Given list of pointers to fixed vertices as passed to
     262                 :            :      * evaluation functions, convert to bit flag format used
     263                 :            :      * for many utility functions in this class.  Bits correspond
     264                 :            :      * to vertices in the canonical vertex order, beginning with
     265                 :            :      * the least-significant bit.  The bit is cleared for free
     266                 :            :      * vertices and set (1) for fixed vertices.
     267                 :            :      */
     268                 :            :     MESQUITE_EXPORT
     269                 :            :     static uint32_t fixed_vertex_bitmap( PatchData& pd, const MsqMeshEntity* elem,
     270                 :            :                                          std::vector< size_t >& free_indices );
     271                 :            : 
     272                 :            :     //! takes an array of coefficients and an array of metrics (both of length num_value)
     273                 :            :     //! and averages the contents using averaging method 'method'.
     274                 :            :     MESQUITE_EXPORT
     275                 :            :     double weighted_average_metrics( const double coef[], const double metric_values[], const int& num_values,
     276                 :            :                                      MsqError& err );
     277                 :            : 
     278                 :            :     /*!AveragingMethod allows you to set how the quality metric values
     279                 :            :       attained at each sample point will be averaged together to produce
     280                 :            :       a single metric value for an element.
     281                 :            :     */
     282                 :            :     enum AveragingMethod
     283                 :            :     {
     284                 :            :         LINEAR,       //!< the linear average
     285                 :            :         RMS,          //!< the root-mean-squared average
     286                 :            :         HMS,          //!< the harmonic-mean-squared average
     287                 :            :         SUM,          //!< the sum of the values
     288                 :            :         SUM_SQUARED,  //!< the sum of the squares of the values
     289                 :            :         HARMONIC,     //!< the harmonic average
     290                 :            :         LAST_WITH_HESSIAN = HARMONIC,
     291                 :            :         MINIMUM,    //!< the minimum value
     292                 :            :         MAXIMUM,    //!< the maximum value
     293                 :            :         GEOMETRIC,  //!< the geometric average
     294                 :            :         LAST_WITH_GRADIENT = GEOMETRIC,
     295                 :            :         STANDARD_DEVIATION,    //!< the standard deviation squared of the values
     296                 :            :         MAX_OVER_MIN,          //!< the maximum value minus the minum value
     297                 :            :         MAX_MINUS_MIN,         //!< the maximum value divided by the minimum value
     298                 :            :         SUM_OF_RATIOS_SQUARED  //!< (1/(N^2))*(SUM (SUM (v_i/v_j)^2))
     299                 :            :     };
     300                 :            : 
     301                 :            :     //!\brief Called at start of instruction queue processing
     302                 :            :     //!
     303                 :            :     //! Do any preliminary global initialization, consistency checking,
     304                 :            :     //! etc.  Default implementation does nothing.
     305                 :            :     MESQUITE_EXPORT virtual void initialize_queue( MeshDomainAssoc* mesh_and_domain, const Settings* settings,
     306                 :            :                                                    MsqError& err );
     307                 :            : 
     308                 :            :   private:
     309                 :            :     int feasible;
     310                 :            : 
     311                 :            :     std::vector< Matrix3D > tmpHess;
     312                 :            :     bool keepFiniteDiffEps;  //!< True if gradient finite difference
     313                 :            :                              //!< calculation should set \c finiteDiffEps
     314                 :            :     bool haveFiniteDiffEps;  //!< True if finite difference Hessian code
     315                 :            :                              //!< has calculated \c finiteDiffEps
     316                 :            :     double finiteDiffEps;    //!< Location for finite difference Hessian code
     317                 :            :                              //!< to store this value so that it doesn't need
     318                 :            :                              //!< to be recalculated if the gradient calculation
     319                 :            :                              //!< is also finite difference
     320                 :            : };
     321                 :            : 
     322                 :            : }  // namespace MBMesquite
     323                 :            : 
     324                 :            : #endif  // QualityMetric_hpp

Generated by: LCOV version 1.11