Branch data Line data Source code
1 : : /* *****************************************************************
2 : : MESQUITE -- The Mesh Quality Improvement Toolkit
3 : :
4 : : Copyright 2006 Sandia National Laboratories. Developed at the
5 : : University of Wisconsin--Madison under SNL contract number
6 : : 624796. The U.S. Government and the University of Wisconsin
7 : : retain certain rights to 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 : : (2006) [email protected]
24 : :
25 : : ***************************************************************** */
26 : :
27 : : /** \file ElementMaxQM.cpp
28 : : * \brief
29 : : * \author Jason Kraftcheck
30 : : */
31 : :
32 : : #include "Mesquite.hpp"
33 : : #include "ElementMaxQM.hpp"
34 : : #include "ElemSampleQM.hpp"
35 : : #include "MsqError.hpp"
36 : : #include "PatchData.hpp"
37 : : #include <limits>
38 : :
39 : : namespace MBMesquite
40 : : {
41 : :
42 [ + - ]: 1 : ElementMaxQM::ElementMaxQM( ElemSampleQM* metric ) : mMetric( metric ) {}
43 : :
44 [ - + ]: 2 : ElementMaxQM::~ElementMaxQM() {}
45 : :
46 : 2 : std::string ElementMaxQM::get_name() const
47 : : {
48 [ + - ]: 2 : std::string result( "ElementMaxQM(" );
49 [ + - ][ + - ]: 2 : result += mMetric->get_name();
50 [ + - ]: 2 : result += ")";
51 : 2 : return result;
52 : : }
53 : :
54 : 14676 : int ElementMaxQM::get_negate_flag() const
55 : : {
56 : 14676 : return get_quality_metric()->get_negate_flag();
57 : : }
58 : :
59 : 83173 : bool ElementMaxQM::evaluate( PatchData& pd, size_t handle, double& value, MsqError& err )
60 : : {
61 [ + - ]: 83173 : ElemSampleQM* qm = get_quality_metric();
62 : 83173 : mHandles.clear();
63 [ + - ]: 83173 : qm->get_element_evaluations( pd, handle, mHandles, err );
64 [ + - ][ - + ]: 83173 : MSQ_ERRFALSE( err );
[ # # ][ # # ]
[ - + ]
65 : :
66 : 83173 : bool valid = true;
67 : : double tmpval;
68 : : bool tmpvalid;
69 : :
70 : 83173 : value = -1.e+100; // initialize max computation
71 [ + - ][ + - ]: 165214 : for( std::vector< size_t >::iterator h = mHandles.begin(); h != mHandles.end(); ++h )
[ + + ]
72 : : {
73 [ + - ][ + - ]: 83173 : tmpvalid = qm->evaluate( pd, *h, tmpval, err ); // MSQ_ERRZERO(err);
74 [ + + ]: 83173 : if( !tmpvalid )
75 : : {
76 : 1132 : value = +1.e+100;
77 : 1132 : return false; // if any handle within the element makes tmpvalid false, then valid is
78 : : // false, no matter what the other handles say
79 : : }
80 [ + - ]: 82041 : else if( tmpval > value )
81 : 82041 : value = tmpval;
82 : : }
83 : :
84 : 83173 : return valid;
85 : : }
86 : : /*
87 : : bool ElementMaxQM::evaluate_with_gradient( PatchData& pd,
88 : : size_t handle,
89 : : double& value,
90 : : std::vector<size_t>& indices,
91 : : std::vector<Vector3D>& gradient,
92 : : MsqError& err )
93 : : {
94 : : ElemSampleQM* qm = get_quality_metric();
95 : : mHandles.clear();
96 : : qm->get_element_evaluations( pd, handle, mHandles, err ); MSQ_ERRFALSE(err);
97 : :
98 : : bool valid = true;
99 : : double tmpval;
100 : : bool tmpvalid;
101 : : unsigned count = 0;
102 : : std::vector<size_t>::iterator h, i, j;
103 : :
104 : : value = -std::numeric_limits<double>::maximum();
105 : : for (h = mHandles.begin(); h != mHandles.end(); ++h) {
106 : : mIndices.clear();
107 : : mGrad.clear();
108 : : tmpvalid = qm->evaluate_with_gradient( pd, *h, tmpval, mIndices, mGrad, err );
109 : : MSQ_ERRZERO(err);
110 : : if (!tmpvalid) {
111 : : valid = false;
112 : : }
113 : : // new value greater than previous max value
114 : : else if (tmpval - value > 1e-6) {
115 : : indices = mIndices;
116 : : gradient = mGrad;
117 : : count = 1;
118 : : value = tmpval;
119 : : }
120 : : // new value equal to previous max value
121 : : else if (tmpval - value >= -1e-6) {
122 : : ++count;
123 : : for (i = mIndices.begin(); i != mIndices.end(); ++i) {
124 : : j = std::find( indices.begin(), indices.end(), *i );
125 : : if (j == indices.end()) {
126 : : indices.push_back( *i );
127 : : gradient.push_back( mGrad[i - mIndices.begin()] );
128 : : }
129 : : else {
130 : : gradient[j - indices.begin()] += mGrad[i - mIndices.begin()];
131 : : }
132 : : }
133 : : }
134 : : }
135 : : if (count > 1) {
136 : : const double inv_count = 1.0 / count;
137 : : for (std::vector<Vector3D>::iterator g = gradient.begin(); g != gradient.end(); ++g)
138 : : *g *= inv_count;
139 : : }
140 : :
141 : : return valid;
142 : : }
143 : : */
144 : :
145 [ + - ][ + - ]: 120 : } // namespace MBMesquite
|