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
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988 | /* *****************************************************************
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]
(2006) [email protected]
***************************************************************** */
/*!
\file AveragingQM.cpp
\brief
\author Michael Brewer
\author Thomas Leurent
\author Jason Kraftcheck
\date 2002-05-14
*/
#include "AveragingQM.hpp"
#include "MsqVertex.hpp"
#include "MsqMeshEntity.hpp"
#include "MsqDebug.hpp"
#include "MsqTimer.hpp"
#include "PatchData.hpp"
namespace MBMesquite
{
double AveragingQM::average_corner_gradients( EntityTopology type,
uint32_t fixed_vertices,
unsigned num_corner,
double corner_values[],
const Vector3D corner_grads[],
Vector3D vertex_grads[],
MsqError& err )
{
const unsigned num_vertex = TopologyInfo::corners( type );
const unsigned dim = TopologyInfo::dimension( type );
const unsigned per_vertex = dim + 1;
unsigned i, j, num_adj;
const unsigned *adj_idx, *rev_idx;
// NOTE: This function changes the corner_values array such that
// it contains the gradient coefficients.
double avg = average_metric_and_weights( corner_values, num_corner, err );
MSQ_ERRZERO( err );
for( i = 0; i < num_vertex; ++i )
{
if( fixed_vertices & ( 1 << i ) ) // skip fixed vertices
continue;
adj_idx = TopologyInfo::adjacent_vertices( type, i, num_adj );
rev_idx = TopologyInfo::reverse_vertex_adjacency_offsets( type, i, num_adj );
if( i < num_corner ) // not all vertices are corners (e.g. pyramid)
vertex_grads[i] = corner_values[i] * corner_grads[per_vertex * i];
else
vertex_grads[i] = 0;
for( j = 0; j < num_adj; ++j )
{
const unsigned v = adj_idx[j], c = rev_idx[j] + 1;
if( v >= num_corner ) // if less corners than vertices (e.g. pyramid apex)
continue;
vertex_grads[i] += corner_values[v] * corner_grads[per_vertex * v + c];
}
}
return avg;
}
/**\brief Iterate over only diagonal blocks of element corner Hessian data
*
* Given concatenation of corner Hessian data for an element, iterate
* over only the diagonal terms for each corner. This class allows
* common code to be used to generate Hessian diagonal blocks from either
* the diagonal blocks for each corner or the full Hessian data for each
* corner, where this class is used for the latter.
*/
class CornerHessDiagIterator
{
private:
const Matrix3D* cornerHess; //!< Current location in concatenated Hessian data.
const EntityTopology elemType; //!< Element topology for Hessian data
unsigned mCorner; //!< The element corner for which cornerHess
//!< is pointing into the corresponding Hessian data.
unsigned mStep; //!< Amount to step to reach next diagonal block.
public:
CornerHessDiagIterator( const Matrix3D* corner_hessians, EntityTopology elem_type )
: cornerHess( corner_hessians ), elemType( elem_type ), mCorner( 0 )
{
TopologyInfo::adjacent_vertices( elemType, mCorner, mStep );
++mStep;
}
SymMatrix3D operator*() const
{
return cornerHess->upper();
}
CornerHessDiagIterator& operator++()
{
cornerHess += mStep;
if( !--mStep )
{
TopologyInfo::adjacent_vertices( elemType, ++mCorner, mStep );
++mStep;
}
return *this;
}
CornerHessDiagIterator operator++( int )
{
CornerHessDiagIterator copy( *this );
operator++();
return copy;
}
};
template < typename HessIter >
static inline double sum_corner_diagonals( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
HessIter corner_diag_blocks,
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[] )
{
unsigned i, n, r, R, idx[4];
const unsigned* adj_list;
double avg = 0.0;
// calculate mean
for( i = 0; i < num_corner; ++i )
avg += corner_values[i];
const Vector3D* grad = corner_grads;
HessIter hess = corner_diag_blocks;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
for( r = 0; r <= n; ++r )
{
R = idx[r];
vertex_grads[R] += *grad;
vertex_hessians[R] += *hess;
++grad;
++hess;
}
}
return avg;
}
template < typename HessIter >
static inline double sum_sqr_corner_diagonals( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
HessIter corner_diag_blocks,
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[] )
{
unsigned i, n, r, R, idx[4];
const unsigned* adj_list;
double v, avg = 0.0;<--- The scope of the variable 'v' can be reduced. [+]The scope of the variable 'v' 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.
// calculate mean
for( i = 0; i < num_corner; ++i )
avg += corner_values[i] * corner_values[i];
const Vector3D* grad = corner_grads;
HessIter hess = corner_diag_blocks;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
++n;
v = 2.0 * corner_values[i];
for( r = 0; r < n; ++r )
{
R = idx[r];
vertex_grads[R] += v * *grad;
vertex_hessians[R] += 2.0 * outer( *grad );
vertex_hessians[R] += v * *hess;
++grad;
++hess;
}
}
return avg;
}
template < typename HessIter >
static inline double pmean_corner_diagonals( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
HessIter corner_diag_blocks,
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[],
double p )
{
const unsigned N = TopologyInfo::corners( type );
unsigned i, n, r, R, idx[4];
const unsigned* adj_list;
double m = 0.0, nm;
double gf[8], hf[8];
double inv = 1.0 / num_corner;
assert( num_corner <= 8 );
// calculate mean
for( i = 0; i < num_corner; ++i )
{
nm = pow( corner_values[i], p );
m += nm;
gf[i] = inv * p * nm / corner_values[i];
hf[i] = ( p - 1 ) * gf[i] / corner_values[i];
}
nm = inv * m;
const Vector3D* grad = corner_grads;
HessIter hess = corner_diag_blocks;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
++n;
for( r = 0; r < n; ++r )
{
R = idx[r];
vertex_grads[R] += gf[i] * *grad;
vertex_hessians[R] += hf[i] * outer( *grad );
vertex_hessians[R] += gf[i] * *hess;
++grad;
++hess;
}
}
m = pow( nm, 1.0 / p );
gf[0] = m / ( p * nm );
hf[0] = ( 1.0 / p - 1 ) * gf[0] / nm;
for( r = 0; r < N; ++r )
{
vertex_hessians[r] *= gf[0];
vertex_hessians[r] += hf[0] * outer( vertex_grads[r] );
vertex_grads[r] *= gf[0];
}
return m;
}
template < typename HessIter >
static inline double average_corner_diagonals( EntityTopology type,
QualityMetric::AveragingMethod method,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
HessIter corner_diag_blocks,
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[],
MsqError& err )
{
unsigned i;
double avg, inv;
// Zero gradients and Hessians
const unsigned num_vertex = TopologyInfo::corners( type );
for( i = 0; i < num_vertex; ++i )
{
vertex_grads[i].set( 0.0 );
vertex_hessians[i] = SymMatrix3D( 0.0 );
}
switch( method )
{
case QualityMetric::SUM:
avg = sum_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks, vertex_grads,
vertex_hessians );
break;
case QualityMetric::LINEAR:
avg = sum_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks, vertex_grads,
vertex_hessians );
inv = 1.0 / num_corner;
avg *= inv;
for( i = 0; i < num_vertex; ++i )
{
vertex_grads[i] *= inv;
vertex_hessians[i] *= inv;
}
break;
case QualityMetric::SUM_SQUARED:
avg = sum_sqr_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks,
vertex_grads, vertex_hessians );
break;
case QualityMetric::RMS:
avg = pmean_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks,
vertex_grads, vertex_hessians, 2.0 );
break;
case QualityMetric::HARMONIC:
avg = pmean_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks,
vertex_grads, vertex_hessians, -1.0 );
break;
case QualityMetric::HMS:
avg = pmean_corner_diagonals( type, num_corner, corner_values, corner_grads, corner_diag_blocks,
vertex_grads, vertex_hessians, -2.0 );
break;
default:
MSQ_SETERR( err )( "averaging method not available.", MsqError::INVALID_STATE );
return 0.0;
}
return avg;
}
double AveragingQM::average_corner_hessian_diagonals( EntityTopology element_type,
uint32_t,
unsigned num_corners,
const double corner_values[],
const Vector3D corner_grads[],
const Matrix3D corner_hessians[],
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[],
MsqError& err )
{
return average_corner_diagonals( element_type, avgMethod, num_corners, corner_values, corner_grads,
CornerHessDiagIterator( corner_hessians, element_type ), vertex_grads,
vertex_hessians, err );
}
double AveragingQM::average_corner_hessian_diagonals( EntityTopology element_type,
uint32_t,
unsigned num_corners,
const double corner_values[],
const Vector3D corner_grads[],
const SymMatrix3D corner_hess_diag[],
Vector3D vertex_grads[],
SymMatrix3D vertex_hessians[],
MsqError& err )
{
return average_corner_diagonals( element_type, avgMethod, num_corners, corner_values, corner_grads,
corner_hess_diag, vertex_grads, vertex_hessians, err );
}
static inline double sum_corner_hessians( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
const Matrix3D corner_hessians[],
Vector3D vertex_grads[],
Matrix3D vertex_hessians[] )
{
const unsigned N = TopologyInfo::corners( type );
unsigned i, n, r, c, R, C, idx[4];
const unsigned* adj_list;
double avg = 0.0;
// calculate mean
for( i = 0; i < num_corner; ++i )
avg += corner_values[i];
const Vector3D* grad = corner_grads;
const Matrix3D* hess = corner_hessians;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
for( r = 0; r <= n; ++r )
{
R = idx[r];
vertex_grads[R] += *grad;
++grad;
for( c = r; c <= n; ++c )
{
C = idx[c];
if( R <= C )
vertex_hessians[N * R - R * ( R + 1 ) / 2 + C] += *hess;
else
vertex_hessians[N * C - C * ( C + 1 ) / 2 + R].plus_transpose_equal( *hess );
++hess;
}
}
}
return avg;
}
static inline double sum_sqr_corner_hessians( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
const Matrix3D corner_hessians[],
Vector3D vertex_grads[],
Matrix3D vertex_hessians[] )
{
const unsigned N = TopologyInfo::corners( type );
unsigned i, n, r, c, R, C, idx[4];
const unsigned* adj_list;
double v, avg = 0.0;<--- The scope of the variable 'v' can be reduced. [+]The scope of the variable 'v' 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.
Matrix3D op;
// calculate mean
for( i = 0; i < num_corner; ++i )
avg += corner_values[i] * corner_values[i];
const Vector3D* grad = corner_grads;
const Matrix3D* hess = corner_hessians;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
++n;
v = 2.0 * corner_values[i];
for( r = 0; r < n; ++r )
{
R = idx[r];
vertex_grads[R] += v * grad[r];
for( c = r; c < n; ++c )
{
C = idx[c];
op.outer_product( 2.0 * grad[r], grad[c] );
op += v * *hess;
if( R <= C )
vertex_hessians[N * R - R * ( R + 1 ) / 2 + C] += op;
else
vertex_hessians[N * C - C * ( C + 1 ) / 2 + R].plus_transpose_equal( op );
++hess;
}
}
grad += n;
}
return avg;
}
static inline double pmean_corner_hessians( EntityTopology type,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
const Matrix3D corner_hessians[],
Vector3D vertex_grads[],
Matrix3D vertex_hessians[],
double p )
{
const unsigned N = TopologyInfo::corners( type );
unsigned i, n, r, c, R, C, idx[4];
const unsigned* adj_list;
double m = 0.0, nm;
Matrix3D op;
double gf[8], hf[8];
double inv = 1.0 / num_corner;
assert( num_corner <= 8 );
// calculate mean
for( i = 0; i < num_corner; ++i )
{
nm = pow( corner_values[i], p );
m += nm;
gf[i] = inv * p * nm / corner_values[i];
hf[i] = ( p - 1 ) * gf[i] / corner_values[i];
}
nm = inv * m;
const Vector3D* grad = corner_grads;
const Matrix3D* hess = corner_hessians;
for( i = 0; i < num_corner; ++i )
{
adj_list = TopologyInfo::adjacent_vertices( type, i, n );
idx[0] = i;
idx[1] = adj_list[0];
idx[2] = adj_list[1];
idx[3] = adj_list[2 % n]; // %n so don't read off end if 2D
++n;
for( r = 0; r < n; ++r )
{
R = idx[r];
vertex_grads[R] += gf[i] * grad[r];
for( c = r; c < n; ++c )
{
C = idx[c];
op.outer_product( grad[r], grad[c] );
op *= hf[i];
op += gf[i] * *hess;
if( R <= C )
vertex_hessians[N * R - R * ( R + 1 ) / 2 + C] += op;
else
vertex_hessians[N * C - C * ( C + 1 ) / 2 + R].plus_transpose_equal( op );
++hess;
}
}
grad += n;
}
m = pow( nm, 1.0 / p );
gf[0] = m / ( p * nm );
hf[0] = ( 1.0 / p - 1 ) * gf[0] / nm;
for( r = 0; r < N; ++r )
{
for( c = r; c < N; ++c )
{
op.outer_product( vertex_grads[r], vertex_grads[c] );
op *= hf[0];
*vertex_hessians *= gf[0];
*vertex_hessians += op;
++vertex_hessians;
}
vertex_grads[r] *= gf[0];
}
return m;
}
double AveragingQM::average_corner_hessians( EntityTopology type,
uint32_t,
unsigned num_corner,
const double corner_values[],
const Vector3D corner_grads[],
const Matrix3D corner_hessians[],
Vector3D vertex_grads[],
Matrix3D vertex_hessians[],
MsqError& err )
{
unsigned i;
double avg, inv;
// Zero gradients and Hessians
const unsigned num_vertex = TopologyInfo::corners( type );
for( i = 0; i < num_vertex; ++i )
vertex_grads[i].set( 0.0 );
const unsigned num_hess = num_vertex * ( num_vertex + 1 ) / 2;
for( i = 0; i < num_hess; ++i )
vertex_hessians[i].zero();
switch( avgMethod )
{
case QualityMetric::SUM:
avg = sum_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians );
break;
case QualityMetric::LINEAR:
avg = sum_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians );
inv = 1.0 / num_corner;
avg *= inv;
for( i = 0; i < num_vertex; ++i )
vertex_grads[i] *= inv;
for( i = 0; i < num_hess; ++i )
vertex_hessians[i] *= inv;
break;
case QualityMetric::SUM_SQUARED:
avg = sum_sqr_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians );
break;
case QualityMetric::RMS:
avg = pmean_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians, 2.0 );
break;
case QualityMetric::HARMONIC:
avg = pmean_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians, -1.0 );
break;
case QualityMetric::HMS:
avg = pmean_corner_hessians( type, num_corner, corner_values, corner_grads, corner_hessians, vertex_grads,
vertex_hessians, -2.0 );
break;
default:
MSQ_SETERR( err )( "averaging method not available.", MsqError::INVALID_STATE );
return 0.0;
}
return avg;
}
double AveragingQM::average_metric_and_weights( double metrics[], int count, MsqError& err )
{
static bool min_max_warning = false;
double avg = 0.0;
int i, tmp_count;
double f;
switch( avgMethod )
{
case QualityMetric::MINIMUM:
if( !min_max_warning )
{
MSQ_DBGOUT( 1 ) << "Minimum and maximum not continuously differentiable.\n"
"Element of subdifferential will be returned.\n";
min_max_warning = true;
}
avg = metrics[0];
for( i = 1; i < count; ++i )
if( metrics[i] < avg ) avg = metrics[i];
tmp_count = 0;
for( i = 0; i < count; ++i )
{
if( metrics[i] - avg <= MSQ_MIN )
{
metrics[i] = 1.0;
++tmp_count;
}
else
{
metrics[i] = 0.0;
}
}
f = 1.0 / tmp_count;
for( i = 0; i < count; ++i )
metrics[i] *= f;
break;
case QualityMetric::MAXIMUM:
if( !min_max_warning )
{
MSQ_DBGOUT( 1 ) << "Minimum and maximum not continuously differentiable.\n"
"Element of subdifferential will be returned.\n";
min_max_warning = true;
}
avg = metrics[0];
for( i = 1; i < count; ++i )
if( metrics[i] > avg ) avg = metrics[i];
tmp_count = 0;
for( i = 0; i < count; ++i )
{
if( avg - metrics[i] <= MSQ_MIN )
{
metrics[i] = 1.0;
++tmp_count;
}
else
{
metrics[i] = 0.0;
}
}
f = 1.0 / tmp_count;
for( i = 0; i < count; ++i )
metrics[i] *= f;
break;
case QualityMetric::SUM:
for( i = 0; i < count; ++i )
{
avg += metrics[i];
metrics[i] = 1.0;
}
break;
case QualityMetric::SUM_SQUARED:
for( i = 0; i < count; ++i )
{
avg += ( metrics[i] * metrics[i] );
metrics[i] *= 2;
}
break;
case QualityMetric::LINEAR:
f = 1.0 / count;
for( i = 0; i < count; ++i )
{
avg += metrics[i];
metrics[i] = f;
}
avg *= f;
break;
case QualityMetric::GEOMETRIC:
avg = 1.0;
for( i = 0; i < count; ++i )
avg *= metrics[i];
avg = pow( avg, 1.0 / count );
f = avg / count;
for( i = 0; i < count; ++i )
metrics[i] = f / metrics[i];
break;
case QualityMetric::RMS:
for( i = 0; i < count; ++i )
avg += metrics[i] * metrics[i];
avg = sqrt( avg / count );
f = 1. / ( avg * count );
for( i = 0; i < count; ++i )
metrics[i] *= f;
break;
case QualityMetric::HARMONIC:
for( i = 0; i < count; ++i )
avg += 1.0 / metrics[i];
avg = count / avg;
for( i = 0; i < count; ++i )
metrics[i] = ( avg * avg ) / ( count * metrics[i] * metrics[i] );
break;
case QualityMetric::HMS:
for( i = 0; i < count; ++i )
avg += 1. / ( metrics[i] * metrics[i] );
avg = sqrt( count / avg );
f = avg * avg * avg / count;
for( i = 0; i < count; ++i )
metrics[i] = f / ( metrics[i] * metrics[i] * metrics[i] );
break;
default:
MSQ_SETERR( err )( "averaging method not available.", MsqError::INVALID_STATE );
}
return avg;
}
/*!
average_metrics takes an array of length num_value and averages the
contents using averaging method 'method'.
*/
double AveragingQM::average_metrics( const double metric_values[], int num_values, MsqError& err )
{
// MSQ_MAX needs to be made global?
// double MSQ_MAX=1e10;
double total_value = 0.0;
double temp_value = 0.0;
int i = 0;
int j = 0;
// if no values, return zero
if( num_values <= 0 )
{
return 0.0;
}
switch( avgMethod )
{
case QualityMetric::GEOMETRIC:
total_value = 1.0;
for( i = 0; i < num_values; ++i )
{
total_value *= ( metric_values[i] );
}
total_value = pow( total_value, 1.0 / num_values );
break;
case QualityMetric::HARMONIC:
// ensure no divide by zero, return zero
for( i = 0; i < num_values; ++i )
{
if( metric_values[i] < MSQ_MIN )<--- outer condition: metric_values[i]
{
if( metric_values[i] > MSQ_MIN )<--- opposite inner condition: metric_values[i]>MSQ_MIN
{
return 0.0;
}
}
total_value += ( 1 / metric_values[i] );
}
// ensure no divide by zero, return MSQ_MAX_CAP
if( total_value < MSQ_MIN )<--- outer condition: total_value
{
if( total_value > MSQ_MIN )<--- opposite inner condition: total_value>MSQ_MIN
{
return MSQ_MAX_CAP;
}
}
total_value = num_values / total_value;
break;
case QualityMetric::LINEAR:
for( i = 0; i < num_values; ++i )
{
total_value += metric_values[i];
}
total_value /= (double)num_values;
break;
case QualityMetric::MAXIMUM:
total_value = metric_values[0];
for( i = 1; i < num_values; ++i )
{
if( metric_values[i] > total_value )
{
total_value = metric_values[i];
}
}
break;
case QualityMetric::MINIMUM:
total_value = metric_values[0];
for( i = 1; i < num_values; ++i )
{
if( metric_values[i] < total_value )
{
total_value = metric_values[i];
}
}
break;
case QualityMetric::RMS:
for( i = 0; i < num_values; ++i )
{
total_value += ( metric_values[i] * metric_values[i] );
}
total_value /= (double)num_values;
total_value = sqrt( total_value );
break;
case QualityMetric::HMS:
// ensure no divide by zero, return zero
for( i = 0; i < num_values; ++i )
{
if( metric_values[i] * metric_values[i] < MSQ_MIN )
{
return 0.0;
}
total_value += ( 1.0 / ( metric_values[i] * metric_values[i] ) );
}
// ensure no divide by zero, return MSQ_MAX_CAP
if( total_value < MSQ_MIN )
{
return MSQ_MAX_CAP;
}
total_value = sqrt( num_values / total_value );
break;
case QualityMetric::STANDARD_DEVIATION:
total_value = 0;
temp_value = 0;
for( i = 0; i < num_values; ++i )
{
temp_value += metric_values[i];
total_value += ( metric_values[i] * metric_values[i] );
}
temp_value /= (double)num_values;
temp_value *= temp_value;
total_value /= (double)num_values;
total_value = total_value - temp_value;
break;
case QualityMetric::SUM:
for( i = 0; i < num_values; ++i )
{
total_value += metric_values[i];
}
break;
case QualityMetric::SUM_SQUARED:
for( i = 0; i < num_values; ++i )
{
total_value += ( metric_values[i] * metric_values[i] );
}
break;
case QualityMetric::MAX_MINUS_MIN:
// total_value used to store the maximum
// temp_value used to store the minimum
temp_value = MSQ_MAX_CAP;
for( i = 0; i < num_values; ++i )
{
if( metric_values[i] < temp_value )
{
temp_value = metric_values[i];
}
if( metric_values[i] > total_value )
{
total_value = metric_values[i];
}
}
total_value -= temp_value;
break;
case QualityMetric::MAX_OVER_MIN:
// total_value used to store the maximum
// temp_value used to store the minimum
temp_value = MSQ_MAX_CAP;
for( i = 0; i < num_values; ++i )
{
if( metric_values[i] < temp_value )
{
temp_value = metric_values[i];
}
if( metric_values[i] > total_value )
{
total_value = metric_values[i];
}
}
// ensure no divide by zero, return MSQ_MAX_CAP
if( fabs( temp_value ) < MSQ_MIN )
{
return MSQ_MAX_CAP;
}
total_value /= temp_value;
break;
case QualityMetric::SUM_OF_RATIOS_SQUARED:
for( j = 0; j < num_values; ++j )
{
// ensure no divide by zero, return MSQ_MAX_CAP
if( fabs( metric_values[j] ) < MSQ_MIN )
{
return MSQ_MAX_CAP;
}
for( i = 0; i < num_values; ++i )
{
total_value +=
( ( metric_values[i] / metric_values[j] ) * ( metric_values[i] / metric_values[j] ) );
}
}
total_value /= (double)( num_values * num_values );
break;
default:
// Return error saying Averaging Method mode not implemented
MSQ_SETERR( err )
( "Requested Averaging Method Not Implemented", MsqError::NOT_IMPLEMENTED );
return 0;
}
return total_value;
}
} // namespace MBMesquite
|