1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487 | /* *****************************************************************
MESQUITE -- The Mesh Quality Improvement Toolkit
Copyright 2004 Sandia Corporation and Argonne National
Laboratory. Under the terms of Contract DE-AC04-94AL85000
with Sandia Corporation, the U.S. Government retains certain
rights in this software.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
(lgpl.txt) along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
[email protected], [email protected], [email protected],
[email protected], [email protected], [email protected]
***************************************************************** */
/*!
\file LPtoPTemplate.cpp
\brief
This Objective Function is evaluated using an L P norm to the pth power.
total=(sum (x_i)^pVal)
\author Michael Brewer
\author Thomas Leurent
\date 2002-01-23
*/
#include <cmath>
#include "LPtoPTemplate.hpp"
#include "MsqFreeVertexIndexIterator.hpp"
#include "MsqTimer.hpp"
#include "MsqHessian.hpp"
#include "MsqDebug.hpp"
#include "QualityMetric.hpp"
using namespace MBMesquite;
LPtoPTemplate::LPtoPTemplate( QualityMetric* qualitymetric, short Pinput, MsqError& err )
: ObjectiveFunctionTemplate( qualitymetric )
{
pVal = Pinput;
if( pVal < 1 )
{
MSQ_SETERR( err )( "P_VALUE must be greater than 0.", MsqError::INVALID_ARG );
return;
}
dividingByN = false;
clear();
}
LPtoPTemplate::LPtoPTemplate( short P, QualityMetric* qm )
: ObjectiveFunctionTemplate( qm ), pVal( P ), dividingByN( false )
{
clear();
}
void LPtoPTemplate::clear()
{
mCount = 0;
mPowSum = 0;
saveCount = 0;
savePowSum = 0;
}
// Michael: need to clean up here
LPtoPTemplate::~LPtoPTemplate() {}
ObjectiveFunction* LPtoPTemplate::clone() const
{
return new LPtoPTemplate( *this );
}
double LPtoPTemplate::get_value( double power_sum,
size_t count,
EvalType type,
size_t& global_count,
MsqError& /*err*/ )
{
double result = 0;
switch( type )
{
default:
case CALCULATE:
result = power_sum;
global_count = count;
break;
case ACCUMULATE:
mPowSum += power_sum;
mCount += count;
result = mPowSum;
global_count = mCount;
break;
case SAVE:
savePowSum = power_sum;
saveCount = count;
result = mPowSum;
global_count = mCount;
break;
case UPDATE:
mPowSum -= savePowSum;
mCount -= saveCount;
savePowSum = power_sum;
saveCount = count;
mPowSum += savePowSum;
mCount += saveCount;
result = mPowSum;
global_count = mCount;
break;
case TEMPORARY:
result = mPowSum - savePowSum + power_sum;
global_count = mCount + count - saveCount;
break;
}
// if (!global_count)
// {
// MSQ_SETERR(err)(" global_count is zero, possibly due to an invalid mesh.",
// MsqError::INVALID_MESH); return -1; // result is invalid
// }
if( dividingByN && global_count ) result /= global_count;
return result;
}
bool LPtoPTemplate::evaluate( EvalType type, PatchData& pd, double& value_out, bool free, MsqError& err )
{
QualityMetric* qm = get_quality_metric();
if( type == ObjectiveFunction::ACCUMULATE )
qm->get_single_pass( pd, qmHandles, free, err );
else
qm->get_evaluations( pd, qmHandles, free, err );
MSQ_ERRFALSE( err );
// calculate OF value for just the patch
std::vector< size_t >::const_iterator i;
double value, working_sum = 0.0;
for( i = qmHandles.begin(); i != qmHandles.end(); ++i )
{
bool result = qm->evaluate( pd, *i, value, err );
if( MSQ_CHKERR( err ) || !result ) return false;
double tmp_val = value;
for( short j = 1; j < pVal; ++j )
tmp_val *= value;
working_sum += fabs( tmp_val );
}
// get overall OF value, update member data, etc.
size_t global_count;
value_out = qm->get_negate_flag() * get_value( working_sum, qmHandles.size(), type, global_count, err );
// if (!global_count)
// return false; // invalid mesh
// else
return true;
}
bool LPtoPTemplate::evaluate_with_gradient( EvalType type,
PatchData& pd,
double& OF_val,
std::vector< Vector3D >& grad_out,
MsqError& err )
{
QualityMetric* qm = get_quality_metric();
qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
MSQ_ERRFALSE( err );
// zero gradient
grad_out.clear();
grad_out.resize( pd.num_free_vertices(), Vector3D( 0.0, 0.0, 0.0 ) );
bool qm_bool = true;<--- The scope of the variable 'qm_bool' can be reduced. [+]The scope of the variable 'qm_bool' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:<--- Variable 'qm_bool' is assigned a value that is never used.
void f(int x)<--- Variable 'qm_bool' is assigned a value that is never used.
{<--- Variable 'qm_bool' is assigned a value that is never used.
int i = 0;<--- Variable 'qm_bool' is assigned a value that is never used.
if (x) {<--- Variable 'qm_bool' is assigned a value that is never used.
// it's safe to move 'int i = 0;' here<--- Variable 'qm_bool' is assigned a value that is never used.
for (int n = 0; n < 10; ++n) {<--- Variable 'qm_bool' is assigned a value that is never used.
// it is possible but not safe to move 'int i = 0;' here<--- Variable 'qm_bool' is assigned a value that is never used.
do_something(&i);<--- Variable 'qm_bool' is assigned a value that is never used.
}<--- Variable 'qm_bool' is assigned a value that is never used.
}<--- Variable 'qm_bool' is assigned a value that is never used.
}<--- Variable 'qm_bool' is assigned a value that is never used.
When you see this message it is always safe to reduce the variable scope 1 level. <--- Variable 'qm_bool' is assigned a value that is never used.
double QM_val;
OF_val = 0.;
int p1;
// calculate OF value and gradient for just the patch
std::vector< size_t >::const_iterator i;
for( i = qmHandles.begin(); i != qmHandles.end(); ++i )
{
qm_bool = qm->evaluate_with_gradient( pd, *i, QM_val, mIndices, mGradient, err );
if( MSQ_CHKERR( err ) || !qm_bool ) return false;
QM_val = fabs( QM_val );
double QM_pow = 1.0;
double factor = qm->get_negate_flag();
if( pVal == 1 )
QM_pow = 1.0;
else
{
QM_pow = QM_val;
for( p1 = 2; p1 < pVal; ++p1 )
QM_pow *= QM_val;
factor *= QM_pow * pVal;
}
OF_val += QM_pow * QM_val;
for( size_t j = 0; j < mIndices.size(); ++j )
{
mGradient[j] *= factor;
grad_out[mIndices[j]] += mGradient[j];
}
}
// get overall OF value, update member data, etc.
size_t global_count;
OF_val = qm->get_negate_flag() * get_value( OF_val, qmHandles.size(), type, global_count, err );
// if (!global_count)
// return false; // invalid mesh
if( dividingByN && global_count )
{
const double inv_n = 1.0 / global_count;
std::vector< Vector3D >::iterator g;
for( g = grad_out.begin(); g != grad_out.end(); ++g )
*g *= inv_n;
}
return true;
}
bool LPtoPTemplate::evaluate_with_Hessian_diagonal( EvalType type,
PatchData& pd,
double& OF_val,
std::vector< Vector3D >& grad,
std::vector< SymMatrix3D >& hess_diag,
MsqError& err )
{
QualityMetric* qm = get_quality_metric();
qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
MSQ_ERRFALSE( err );
// zero gradient and hessian
grad.clear();
grad.resize( pd.num_free_vertices(), 0.0 );
hess_diag.clear();
hess_diag.resize( pd.num_free_vertices(), 0.0 );
double QM_val, QM_pow = 1.0;
double fac1, fac2;
const double negate_flag = qm->get_negate_flag();
bool qm_bool;<--- The scope of the variable 'qm_bool' can be reduced. [+]The scope of the variable 'qm_bool' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
size_t i;
short p;
// Loops over all elements in the patch.
OF_val = 0.0;
std::vector< size_t >::const_iterator k;
for( k = qmHandles.begin(); k != qmHandles.end(); ++k )
{
// Computes \nabla^2 Q(e). Only the free vertices will have non-zero entries.
qm_bool = qm->evaluate_with_Hessian_diagonal( pd, *k, QM_val, mIndices, mGradient, mDiag, err );
if( MSQ_CHKERR( err ) || !qm_bool ) return false;
QM_val = fabs( QM_val );
// **** Computes Hessian ****
const size_t nve = mIndices.size();
if( pVal == 1 )
{
QM_pow = 1.0;
for( i = 0; i < nve; ++i )
{
mDiag[i] *= negate_flag;
hess_diag[mIndices[i]] += mDiag[i];
}
fac1 = 1;
}
else if( pVal >= 2 )
{
// Computes the coefficients:
QM_pow = 1.0;
for( p = 0; p < pVal - 2; ++p )
QM_pow *= QM_val;
// 1 - computes p(p-1)Q(e)^{p-2}
fac2 = pVal * ( pVal - 1 ) * QM_pow;
// 2 - computes pQ(e)^{p-1}
QM_pow *= QM_val;
fac1 = pVal * QM_pow;
// fac1 *= qm->get_negate_flag();
// fac2 *= qm->get_negate_flag();
for( i = 0; i < nve; ++i )
{
SymMatrix3D op( mGradient[i] );
op *= fac2;
mDiag[i] *= fac1;
op += mDiag[i];
op *= negate_flag;
hess_diag[mIndices[i]] += op;
}
}
else
{
MSQ_SETERR( err )( " invalid P value.", MsqError::INVALID_STATE );
return false;
}
// **** Computes Gradient ****
// For each vertex in the element ...
for( i = 0; i < nve; ++i )
{
// ... computes p*q^{p-1}*grad(q) ...
mGradient[i] *= fac1 * negate_flag;
// ... and accumulates it in the objective function gradient.
// also scale the gradient by the scaling factor
assert( mIndices[i] < pd.num_free_vertices() );
grad[mIndices[i]] += mGradient[i];
}
// **** computes Objective Function value \sum_{i=1}^{N_e} |q_i|^P ****
OF_val += QM_pow * QM_val;
}
size_t global_count;
OF_val = negate_flag * get_value( OF_val, qmHandles.size(), type, global_count, err );
// if (!global_count)
// return false; // invalid mesh
if( dividingByN && global_count )
{
const double inv_n = 1.0 / global_count;
for( i = 0; i < pd.num_free_vertices(); ++i )
{
grad[i] *= inv_n;
hess_diag[i] *= inv_n;
}
}
return true;
}
/*\ For each element, each entry to be accumulated in the Hessian for
this objective function (\f$ \sum_{e \in E} Q(e)^p \f$ where \f$ E \f$
is the set of all elements in the patch) has the form:
\f$ pQ(e)^{p-1} \nabla^2 Q(e) + p(p-1)Q(e)^{p-2} \nabla Q(e) [\nabla Q(e)]^T \f$.
For \f$ p=2 \f$, this simplifies to
\f$ 2Q(e) \nabla^2 Q(e) + 2 \nabla Q(e) [\nabla Q(e)]^T \f$.
For \f$ p=1 \f$, this simplifies to \f$ \nabla^2 Q(e) \f$.
The \f$ p=1 \f$ simplified version is implemented directly
to speed up computation.
This function does not support vertex-based metrics.
\param pd The PatchData object for which the objective function
hessian is computed.
\param hessian this object must have been previously initialized.
*/
bool LPtoPTemplate::evaluate_with_Hessian( EvalType type,
PatchData& pd,
double& OF_val,
std::vector< Vector3D >& grad,
MsqHessian& hessian,
MsqError& err )
{
QualityMetric* qm = get_quality_metric();
qm->get_evaluations( pd, qmHandles, OF_FREE_EVALS_ONLY, err );
MSQ_ERRFALSE( err );
double negate_flag = qm->get_negate_flag();
// zero gradient and hessian
grad.clear();
grad.resize( pd.num_free_vertices(), 0.0 );
hessian.zero_out();
double QM_val, QM_pow = 1.0;
double fac1, fac2;
Matrix3D elem_outer_product;
bool qm_bool;<--- The scope of the variable 'qm_bool' can be reduced. [+]The scope of the variable 'qm_bool' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
size_t i, j, n;
short p;
// Loops over all elements in the patch.
OF_val = 0.0;
std::vector< size_t >::const_iterator k;
for( k = qmHandles.begin(); k != qmHandles.end(); ++k )
{
// Computes \nabla^2 Q(e). Only the free vertices will have non-zero entries.
qm_bool = qm->evaluate_with_Hessian( pd, *k, QM_val, mIndices, mGradient, mHessian, err );
if( MSQ_CHKERR( err ) || !qm_bool ) return false;
QM_val = fabs( QM_val );
// **** Computes Hessian ****
const size_t nve = mIndices.size();
if( pVal == 1 )
{
QM_pow = 1.0;
n = 0;
for( i = 0; i < nve; ++i )
{
for( j = i; j < nve; ++j )
{
// negate if necessary
mHessian[n] *= negate_flag;
hessian.add( mIndices[i], mIndices[j], mHessian[n], err );
MSQ_ERRFALSE( err );
++n;
}
}
fac1 = 1;
}
else if( pVal >= 2 )
{
// Computes the coefficients:
QM_pow = 1.0;
for( p = 0; p < pVal - 2; ++p )
QM_pow *= QM_val;
// 1 - computes p(p-1)Q(e)^{p-2}
fac2 = pVal * ( pVal - 1 ) * QM_pow;
// 2 - computes pQ(e)^{p-1}
QM_pow *= QM_val;
fac1 = pVal * QM_pow;
// fac1 *= qm->get_negate_flag();
// fac2 *= qm->get_negate_flag();
n = 0;
for( i = 0; i < nve; ++i )
{
for( j = i; j < nve; ++j )
{
elem_outer_product.outer_product( mGradient[i], mGradient[j] );
elem_outer_product *= fac2;
mHessian[n] *= fac1;
mHessian[n] += elem_outer_product;
mHessian[n] *= negate_flag;
hessian.add( mIndices[i], mIndices[j], mHessian[n], err );
MSQ_ERRFALSE( err );
++n;
}
}
}
else
{
MSQ_SETERR( err )( " invalid P value.", MsqError::INVALID_STATE );
return false;
}
// **** Computes Gradient ****
// For each vertex in the element ...
for( i = 0; i < nve; ++i )
{
// ... computes p*q^{p-1}*grad(q) ...
mGradient[i] *= fac1 * negate_flag;
// ... and accumulates it in the objective function gradient.
// also scale the gradient by the scaling factor
assert( mIndices[i] < pd.num_free_vertices() );
grad[mIndices[i]] += mGradient[i];
}
// **** computes Objective Function value \sum_{i=1}^{N_e} |q_i|^P ****
OF_val += QM_pow * QM_val;
}
size_t global_count;
OF_val = negate_flag * get_value( OF_val, qmHandles.size(), type, global_count, err );
// if (!global_count)
// return false; // invalid mesh
if( dividingByN && global_count )
{
const double inv_n = 1.0 / global_count;
std::vector< Vector3D >::iterator g;
for( g = grad.begin(); g != grad.end(); ++g )
*g *= inv_n;
hessian.scale( inv_n );
}
return true;
}
|