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
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
/* *****************************************************************
    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   VertexMover.cpp
  \brief

  The VertexMover Class is the base class for all the smoothing algorythms

  \author Thomas Leurent
  \date   2002-01-17
*/

#include "VertexMover.hpp"
#include "NonGradient.hpp"
#include "QualityMetric.hpp"
#include "TQualityMetric.hpp"
#include "TMetricBarrier.hpp"
#include "AWMetricBarrier.hpp"
#include "ElementMaxQM.hpp"
#include "ElementAvgQM.hpp"
#include "ElementPMeanP.hpp"
#include "PlanarDomain.hpp"
#include "ObjectiveFunctionTemplate.hpp"
#include "MaxTemplate.hpp"
#include "MsqTimer.hpp"
#include "MsqDebug.hpp"
#include "PatchSet.hpp"
#include "PatchData.hpp"
#include "ParallelHelperInterface.hpp"
#include <algorithm>

namespace MBMesquite
{

extern int get_parallel_rank();
extern int get_parallel_size();
extern void parallel_barrier();

VertexMover::VertexMover( ObjectiveFunction* OF ) : QualityImprover(), objFuncEval( OF ), jacobiOpt( false ) {}

VertexMover::~VertexMover() {}

/*

    +-----------+
    |Reset Outer|
    |Criterion  |
    +-----------+
          |
          V
          +
        /   \
       /Outer\  YES
+--> <Criterion>-----> DONE
|      \Done?/
|       \   /
|         +
|         |NO
|         V
|   +-----------+
1   |Reset Mesh |
|   | Iteration |
|   +-----------+
|         |
|         V
|         +
|       /  \
|   NO /Next\
+-----<Patch > <-----+
       \    /        |
        \  /         |
          +          |
       YES|          |
          V          |
    +-----------+    |
    |Reset Inner|    |
    |Criterion  |    2
    +-----------+    |
          |          |
          V          |
          +          |
        /   \        |
       /Inner\  YES  |
+--> <Criterion>-----+    --------------+
|      \Done?/                          |
|       \   /                           |
|         +                             |
|         |NO                           |
|         V                          Inside
3   +-----------+                    Smoother
|   |   Smooth  |                       |
|   |   Patch   |                       |
|   +-----------+                       |
|         |                             |
----------+               --------------+

*/

/*! \brief Improves the quality of the MeshSet, calling some
    methods specified in a class derived from VertexMover

    \param const MeshSet &: this MeshSet is looped over. Only the
    mutable data members are changed (such as currentVertexInd).
  */
double VertexMover::loop_over_mesh( MeshDomainAssoc* mesh_and_domain, const Settings* settings, MsqError& err )
{
    Mesh* mesh         = mesh_and_domain->get_mesh();
    MeshDomain* domain = mesh_and_domain->get_domain();

    TagHandle coord_tag      = 0;  // store uncommitted coords for jacobi optimization
    TagHandle* coord_tag_ptr = 0;

    TerminationCriterion* outer_crit = 0;
    TerminationCriterion* inner_crit = 0;

    // Clear culling flag, set hard fixed flag, etc on all vertices
    initialize_vertex_byte( mesh_and_domain, settings, err );
    MSQ_ERRZERO( err );

    // Get the patch data to use for the first iteration
    OFEvaluator& obj_func = get_objective_function_evaluator();

    // Check for impromer use of MaxTemplate
    ObjectiveFunctionTemplate* maxt_ptr = dynamic_cast< MaxTemplate* >( obj_func.get_objective_function() );
    if( maxt_ptr )
    {
        QualityImprover* ngqi_ptr = dynamic_cast< NonGradient* >( this );
        if( !ngqi_ptr )
            std::cout << "Warning: MaxTemplate results in non-differentiable objective function." << std::endl
                      << "   Therefore, it is best to use the NonGradient solver. Other Mesquite" << std::endl
                      << "   solvers require derivative information." << std::endl;
    }

    PatchData patch;
    patch.set_mesh( mesh );
    patch.set_domain( domain );
    if( settings ) patch.attach_settings( settings );
    bool one_patch = false, inner_crit_terminated, all_culled;
    std::vector< Mesh::VertexHandle > patch_vertices;
    std::vector< Mesh::ElementHandle > patch_elements;
    bool valid;

    PatchSet* patch_set = get_patch_set();
    if( !patch_set )
    {
        MSQ_SETERR( err )( "No PatchSet for QualityImprover!", MsqError::INVALID_STATE );
        return 0.0;
    }
    patch_set->set_mesh( mesh );

    std::vector< PatchSet::PatchHandle > patch_list;
    patch_set->get_patch_handles( patch_list, err );
    MSQ_ERRZERO( err );

    // check for inverted elements when using a barrietr target metric
    TQualityMetric* tqm_ptr        = NULL;
    TMetricBarrier* tm_ptr         = NULL;
    AWMetricBarrier* awm_ptr       = NULL;
    ElemSampleQM* sample_qm_ptr    = NULL;
    QualityMetric* qm_ptr          = NULL;
    TQualityMetric* pmeanp_ptr     = NULL;
    ElementMaxQM* elem_max_ptr     = NULL;
    ElementAvgQM* elem_avg_ptr     = NULL;
    ElementPMeanP* elem_pmeanp_ptr = NULL;

    ObjectiveFunctionTemplate* of_ptr = dynamic_cast< ObjectiveFunctionTemplate* >( obj_func.get_objective_function() );
    if( of_ptr ) qm_ptr = of_ptr->get_quality_metric();
    if( qm_ptr )
    {
        pmeanp_ptr      = dynamic_cast< TQualityMetric* >( qm_ptr );  // PMeanP case
        elem_max_ptr    = dynamic_cast< ElementMaxQM* >( qm_ptr );
        elem_avg_ptr    = dynamic_cast< ElementAvgQM* >( qm_ptr );
        elem_pmeanp_ptr = dynamic_cast< ElementPMeanP* >( qm_ptr );
    }
    if( elem_max_ptr )
        sample_qm_ptr = elem_max_ptr->get_quality_metric();
    else if( elem_pmeanp_ptr )
        sample_qm_ptr = elem_pmeanp_ptr->get_quality_metric();
    else if( elem_avg_ptr )
        sample_qm_ptr = elem_avg_ptr->get_quality_metric();
    else if( pmeanp_ptr )
    {
        tm_ptr  = dynamic_cast< TMetricBarrier* >( pmeanp_ptr->get_target_metric() );
        awm_ptr = dynamic_cast< AWMetricBarrier* >( pmeanp_ptr->get_target_metric() );
    }

    if( sample_qm_ptr || pmeanp_ptr )
    {
        if( !pmeanp_ptr )
        {
            tqm_ptr = dynamic_cast< TQualityMetric* >( sample_qm_ptr );
            if( tqm_ptr )
            {
                tm_ptr  = dynamic_cast< TMetricBarrier* >( tqm_ptr->get_target_metric() );
                awm_ptr = dynamic_cast< AWMetricBarrier* >( tqm_ptr->get_target_metric() );
            }
        }
        else
        {
            tqm_ptr = dynamic_cast< TQualityMetric* >( pmeanp_ptr );
        }

        if( tqm_ptr && ( tm_ptr || awm_ptr ) )
        {
            // check for inverted elements
            this->initialize( patch, err );
            std::vector< size_t > handles;

            // set up patch data
            std::vector< PatchSet::PatchHandle >::iterator patch_iter = patch_list.begin();
            while( patch_iter != patch_list.end() )
            {
                do
                {
                    patch_set->get_patch( *patch_iter, patch_elements, patch_vertices, err );
                    if( MSQ_CHKERR( err ) ) goto ERROR;
                    ++patch_iter;
                } while( patch_elements.empty() && patch_iter != patch_list.end() );

                patch.set_mesh_entities( patch_elements, patch_vertices, err );
                if( MSQ_CHKERR( err ) ) goto ERROR;

                qm_ptr->get_evaluations( patch, handles, true, err );  // MSQ_ERRFALSE(err);

                // do actual check for inverted elements
                std::vector< size_t >::const_iterator i;
                double tvalue;
                for( i = handles.begin(); i != handles.end(); ++i )
                {
                    bool result = tqm_ptr->evaluate( patch, *i, tvalue, err );
                    if( MSQ_CHKERR( err ) || !result ) return false;  // inverted element detected
                }
            }
        }
    }

    // Get termination criteria
    outer_crit = this->get_outer_termination_criterion();
    inner_crit = this->get_inner_termination_criterion();
    if( outer_crit == 0 )
    {
        MSQ_SETERR( err )( "Termination Criterion pointer is Null", MsqError::INVALID_STATE );
        return 0.;
    }
    if( inner_crit == 0 )
    {
        MSQ_SETERR( err )
        ( "Termination Criterion pointer for inner loop is Null", MsqError::INVALID_STATE );
        return 0.;
    }

    // Set Termination Criterion defaults if no other Criterion is set
    if( !outer_crit->criterion_is_set() ) outer_crit->add_iteration_limit( 1 );
    if( !inner_crit->criterion_is_set() ) inner_crit->add_iteration_limit( 10 );

    // If using a local patch, suppress output of inner termination criterion
    if( patch_list.size() > 1 )
        inner_crit->set_debug_output_level( 3 );
    else
        one_patch = true;

    if( jacobiOpt )
    {
        coord_tag = get_jacobi_coord_tag( mesh, err );
        MSQ_ERRZERO( err );
        coord_tag_ptr = &coord_tag;
    }

    // Initialize outer loop

    this->initialize( patch, err );
    if( MSQ_CHKERR( err ) ) goto ERROR;

    valid = obj_func.initialize( mesh_and_domain, settings, patch_set, err );
    if( MSQ_CHKERR( err ) ) goto ERROR;
    if( !valid )
    {
        MSQ_SETERR( err )
        ( "ObjectiveFunction initialization failed.  Mesh "
          "invalid at one or more sample points.",
          MsqError::INVALID_MESH );
        goto ERROR;
    }

    outer_crit->reset_outer( mesh, domain, obj_func, settings, err );
    if( MSQ_CHKERR( err ) ) goto ERROR;

    // if only one patch, get the patch now
    if( one_patch )
    {
        patch_set->get_patch( patch_list[0], patch_elements, patch_vertices, err );
        if( MSQ_CHKERR( err ) ) goto ERROR;
        patch.set_mesh_entities( patch_elements, patch_vertices, err );
        if( MSQ_CHKERR( err ) ) goto ERROR;
    }

    // Loop until outer termination criterion is met
    inner_crit_terminated = false;
    while( !outer_crit->terminate() )
    {
        if( inner_crit_terminated )
        {
            MSQ_SETERR( err )
            ( "Inner termiation criterion satisfied for all patches "
              "without meeting outer termination criterion.  This is "
              "an infinite loop.  Aborting.",
              MsqError::INVALID_STATE );
            break;
        }
        inner_crit_terminated = true;
        all_culled            = true;

        int num_patches = 0;
        // Loop over each patch
        std::vector< PatchSet::PatchHandle >::iterator p_iter = patch_list.begin();
        while( p_iter != patch_list.end() )
        {
            if( !one_patch )
            {  // if only one patch (global) re-use the previous one
                // loop until we get a non-empty patch.  patch will be empty
                // for culled vertices with element-on-vertex patches
                do
                {
                    patch_set->get_patch( *p_iter, patch_elements, patch_vertices, err );
                    if( MSQ_CHKERR( err ) ) goto ERROR;
                    ++p_iter;
                } while( patch_elements.empty() && p_iter != patch_list.end() );

                if( patch_elements.empty() )
                {  // no more non-culled vertices
                    if( 0 ) std::cout << "P[" << get_parallel_rank() << "] tmp srk all vertices culled." << std::endl;
                    break;
                }

                all_culled = false;
                patch.set_mesh_entities( patch_elements, patch_vertices, err );
                if( MSQ_CHKERR( err ) ) goto ERROR;
            }
            else
            {
                ++p_iter;
                all_culled = false;
            }

            ++num_patches;

            // Initialize for inner iteration

            this->initialize_mesh_iteration( patch, err );
            if( MSQ_CHKERR( err ) ) goto ERROR;

            obj_func.reset();

            outer_crit->reset_patch( patch, err );
            if( MSQ_CHKERR( err ) ) goto ERROR;

            inner_crit->reset_inner( patch, obj_func, err );
            if( MSQ_CHKERR( err ) ) goto ERROR;

            inner_crit->reset_patch( patch, err );
            if( MSQ_CHKERR( err ) ) goto ERROR;

            // Don't even call optimizer if inner termination
            // criterion has already been met.
            if( !inner_crit->terminate() )
            {
                inner_crit_terminated = false;

                // Call optimizer - should loop on inner_crit->terminate()
                this->optimize_vertex_positions( patch, err );
                if( MSQ_CHKERR( err ) ) goto ERROR;

                // Update for changes during inner iteration
                // (during optimizer loop)

                outer_crit->accumulate_patch( patch, err );
                if( MSQ_CHKERR( err ) ) goto ERROR;

                inner_crit->cull_vertices( patch, obj_func, err );
                if( MSQ_CHKERR( err ) ) goto ERROR;

                // FIXME
                if( 0 )
                {
                    inner_crit->cull_vertices_global( patch, mesh, domain, settings, obj_func, err );
                    if( MSQ_CHKERR( err ) ) goto ERROR;
                }

                patch.update_mesh( err, coord_tag_ptr );
                if( MSQ_CHKERR( err ) ) goto ERROR;
            }
        }

        if( jacobiOpt ) commit_jacobi_coords( coord_tag, mesh, err );

        this->terminate_mesh_iteration( patch, err );
        if( MSQ_CHKERR( err ) ) goto ERROR;

        outer_crit->accumulate_outer( mesh, domain, obj_func, settings, err );
        if( MSQ_CHKERR( err ) ) goto ERROR;

        if( all_culled ) break;
    }

ERROR:
    if( jacobiOpt ) mesh->tag_delete( coord_tag, err );

    // call the criteria's cleanup funtions.
    if( outer_crit ) outer_crit->cleanup( mesh, domain, err );
    if( inner_crit ) inner_crit->cleanup( mesh, domain, err );
    // call the optimization cleanup function.
    this->cleanup();

    return 0.;
}

static void checkpoint_bytes( Mesh* mesh, std::vector< unsigned char >& saved_bytes, MsqError& err )
{
    std::vector< Mesh::VertexHandle > vertexHandlesArray;
    mesh->get_all_vertices( vertexHandlesArray, err );MSQ_ERRRTN( err );
    saved_bytes.resize( vertexHandlesArray.size() );
    mesh->vertices_get_byte( &vertexHandlesArray[0], &saved_bytes[0], vertexHandlesArray.size(), err );MSQ_ERRRTN( err );
}

static void restore_bytes( Mesh* mesh, std::vector< unsigned char >& saved_bytes, MsqError& err )
{
    std::vector< Mesh::VertexHandle > vertexHandlesArray;
    mesh->get_all_vertices( vertexHandlesArray, err );MSQ_ERRRTN( err );
    mesh->vertices_set_byte( &vertexHandlesArray[0], &saved_bytes[0], vertexHandlesArray.size(), err );MSQ_ERRRTN( err );
}

static void save_or_restore_debug_state( bool save )
{
    static bool debug[3] = { false, false, false };
    if( save )
    {
        debug[0] = MsqDebug::get( 1 );
        debug[1] = MsqDebug::get( 2 );
        debug[2] = MsqDebug::get( 3 );
    }
    else
    {
        if( debug[0] ) MsqDebug::enable( 1 );
        if( debug[1] ) MsqDebug::enable( 2 );
        if( debug[2] ) MsqDebug::enable( 3 );
    }
}

/*! \brief Improves the quality of the MeshSet, calling some
    methods specified in a class derived from VertexMover

    \param const MeshSet &: this MeshSet is looped over. Only the
    mutable data members are changed (such as currentVertexInd).
  */
double VertexMover::loop_over_mesh( ParallelMesh* mesh, MeshDomain* domain, const Settings* settings, MsqError& err )
{
    std::vector< size_t > junk;
    Mesh::VertexHandle vertex_handle;
    TagHandle coord_tag      = 0;  // store uncommitted coords for jacobi optimization
    TagHandle* coord_tag_ptr = 0;
    int outer_iter           = 0;
    int inner_iter           = 0;
    bool one_patch           = false;

    // Clear culling flag, set hard fixed flag, etc on all vertices
    MeshDomainAssoc mesh_and_domain = MeshDomainAssoc( (Mesh*)mesh, 0 );
    initialize_vertex_byte( &mesh_and_domain, settings, err );
    MSQ_ERRZERO( err );

    // Get the patch data to use for the first iteration
    OFEvaluator& obj_func = get_objective_function_evaluator();

    PatchData patch;
    patch.set_mesh( (Mesh*)mesh );
    patch.set_domain( domain );
    patch.attach_settings( settings );

    ParallelHelper* helper = mesh->get_parallel_helper();
    if( !helper )
    {
        MSQ_SETERR( err )( "No ParallelHelper instance", MsqError::INVALID_STATE );
        return 0;
    }

    helper->smoothing_init( err );
    MSQ_ERRZERO( err );

    bool inner_crit_terminated, all_culled;
    std::vector< Mesh::VertexHandle > patch_vertices;
    std::vector< Mesh::ElementHandle > patch_elements;
    std::vector< Mesh::VertexHandle > fixed_vertices;
    std::vector< Mesh::VertexHandle > free_vertices;

    // Get termination criteria
    TerminationCriterion* outer_crit = this->get_outer_termination_criterion();
    TerminationCriterion* inner_crit = this->get_inner_termination_criterion();
    if( outer_crit == 0 )
    {
        MSQ_SETERR( err )( "Termination Criterion pointer is Null", MsqError::INVALID_STATE );
        return 0.;
    }
    if( inner_crit == 0 )
    {
        MSQ_SETERR( err )
        ( "Termination Criterion pointer for inner loop is Null", MsqError::INVALID_STATE );
        return 0.;
    }

    PatchSet* patch_set = get_patch_set();
    if( !patch_set )
    {
        MSQ_SETERR( err )( "No PatchSet for QualityImprover!", MsqError::INVALID_STATE );
        return 0.0;
    }
    patch_set->set_mesh( (Mesh*)mesh );

    std::vector< PatchSet::PatchHandle > patch_list;
    patch_set->get_patch_handles( patch_list, err );
    MSQ_ERRZERO( err );

    if( patch_list.size() > 1 )
        inner_crit->set_debug_output_level( 3 );
    else
        one_patch = true;

    if( jacobiOpt )
    {
        coord_tag = get_jacobi_coord_tag( mesh, err );
        MSQ_ERRZERO( err );
        coord_tag_ptr = &coord_tag;
    }

    // parallel error checking
    MsqError perr;
    bool pdone = false;

#define PERROR_COND continue

    // Initialize outer loop

    this->initialize( patch, err );
    if( MSQ_CHKERR( err ) )
    {
        MSQ_SETERR( perr )( "initialize patch", MsqError::INVALID_STATE );
    }  // goto ERROR;

    MeshDomainAssoc mesh_and_domain2 = MeshDomainAssoc( (Mesh*)mesh, domain );
    obj_func.initialize( &mesh_and_domain2, settings, patch_set, err );
    if( MSQ_CHKERR( err ) )
    {
        MSQ_SETERR( perr )( "initialize obj_func", MsqError::INVALID_STATE );
    }  // goto ERROR;

    outer_crit->reset_outer( (Mesh*)mesh, domain, obj_func, settings, err );
    if( MSQ_CHKERR( err ) )
    {
        MSQ_SETERR( perr )( "reset_outer", MsqError::INVALID_STATE );
    }  // goto ERROR;

    // Loop until outer termination criterion is met
    inner_crit_terminated = false;
    all_culled            = false;
    for( ;; )
    {
        if( 0 )
            std::cout << "P[" << get_parallel_rank() << "] tmp srk inner_iter= " << inner_iter
                      << " outer_iter= " << outer_iter << std::endl;

        // PERROR:

        if( MSQ_CHKERR( perr ) )
        {
            std::cout << "P[" << get_parallel_rank() << "] VertexMover::loop_over_mesh found parallel error: " << perr
                      << "\n quitting... pdone= " << pdone << std::endl;
            pdone = true;
        }

        helper->communicate_any_true( pdone, err );

        if( 0 )
            std::cout << "P[" << get_parallel_rank() << "] tmp srk inner_iter= " << inner_iter
                      << " outer_iter= " << outer_iter << " pdone= " << pdone << std::endl;

        if( pdone )
        {
            std::cout << "P[" << get_parallel_rank()
                      << "] VertexMover::loop_over_mesh found parallel error, quitting... pdone= " << pdone
                      << std::endl;
            MSQ_SETERR( err )( "PARALLEL ERROR", MsqError::PARALLEL_ERROR );
            break;
        }

        ++outer_iter;

        /// [email protected] 1/19/12: the logic here was changed so that all proc's must agree
        ///   on the values used for outer and inner termination before the iteration is stopped.
        ///   Previously, the ParallelHelper::communicate_all_true method returned true if any
        ///   proc sent it a true value, which seems to be a bug, at least in the name of the
        ///   method. The method has been changed to return true only if all proc's values are true.
        ///   In the previous version, this meant that if any proc hit its inner or outer
        ///   termination criterion, the loop was exited, and thus some parts of the mesh
        ///   are potentially left unconverged.  Also, if the outer criterion was satisfied on
        ///   part of the mesh (say a uniform part), the iterations were not executed at all.
        /// Also, changed name of "did_some" to "inner_crit_terminated", and flipped its boolean
        ///   value to be consistent with the name - for readability and for correctness since
        ///   we want to communicate a true value through the helper.

        bool outer_crit_terminated       = outer_crit->terminate();
        bool outer_crit_terminated_local = outer_crit_terminated;
        helper->communicate_all_true( outer_crit_terminated, err );

        bool inner_crit_terminated_local = inner_crit_terminated;
        helper->communicate_all_true( inner_crit_terminated, err );

        bool all_culled_local = all_culled;
        helper->communicate_all_true( all_culled, err );

        bool done = all_culled || outer_crit_terminated;
        if( inner_crit_terminated )
        {
            MSQ_SETERR( err )
            ( "Inner termination criterion satisfied for all patches "
              "without meeting outer termination criterion.  This is "
              "an infinite loop.  Aborting.",
              MsqError::INVALID_STATE );
            done = true;
            helper->communicate_any_true( done, err );
        }

        bool local_done = done;

        helper->communicate_all_true( done, err );

        if( 0 )
            std::cout << "P[" << get_parallel_rank() << "] tmp srk done= " << done << " local_done= " << local_done
                      << " all_culled= " << all_culled << " outer_crit->terminate()= " << outer_crit->terminate()
                      << " outer_term= " << outer_crit_terminated
                      << " outer_term_local= " << outer_crit_terminated_local
                      << " inner_crit_terminated = " << inner_crit_terminated
                      << " inner_crit_terminated_local = " << inner_crit_terminated_local
                      << " all_culled = " << all_culled << " all_culled_local = " << all_culled_local << std::endl;

        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( "loop start", MsqError::INVALID_STATE );
        }  // goto ERROR;

        if( done ) break;

        inner_crit_terminated = true;
        all_culled            = true;

        ///*** smooth the interior ***////

        // get the fixed vertices (i.e. the ones *not* part of the first independent set)
        helper->compute_first_independent_set( fixed_vertices );

        // sort the fixed vertices
        std::sort( fixed_vertices.begin(), fixed_vertices.end() );

        // Loop over each patch
        if( 0 && MSQ_DBG( 2 ) )
            std::cout << "P[" << get_parallel_rank() << "] tmp srk number of patches = " << patch_list.size()
                      << " inner_iter= " << inner_iter << " outer_iter= " << outer_iter
                      << " inner.globalInvertedCount = " << inner_crit->globalInvertedCount
                      << " outer.globalInvertedCount = " << outer_crit->globalInvertedCount
                      << " inner.patchInvertedCount = " << inner_crit->patchInvertedCount
                      << " outer.patchInvertedCount = " << outer_crit->patchInvertedCount << std::endl;

        save_or_restore_debug_state( true );
        // MsqDebug::disable_all();

        int num_patches = 0;

        std::vector< PatchSet::PatchHandle >::iterator p_iter = patch_list.begin();
        while( p_iter != patch_list.end() )
        {

            // loop until we get a non-empty patch.  patch will be empty
            // for culled vertices with element-on-vertex patches
            do
            {
                patch_set->get_patch( *p_iter, patch_elements, patch_vertices, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "get_patch", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;
                ++p_iter;
            } while( patch_elements.empty() && p_iter != patch_list.end() );

            if( patch_elements.empty() )
            {  // no more non-culled vertices
                if( 0 ) std::cout << "P[" << get_parallel_rank() << "] tmp srk all vertices culled." << std::endl;
                break;
            }

            if( patch_vertices.empty() )  // global patch hack (means all mesh vertices)
            {
                mesh->get_all_vertices( patch_vertices, err );
            }

            free_vertices.clear();

            for( size_t i = 0; i < patch_vertices.size(); ++i )
                if( !std::binary_search( fixed_vertices.begin(), fixed_vertices.end(), patch_vertices[i] ) )
                    free_vertices.push_back( patch_vertices[i] );

            if( free_vertices.empty() )
            {  // all vertices were fixed -> skip patch
                continue;
            }

            ++num_patches;
            all_culled = false;
            patch.set_mesh_entities( patch_elements, free_vertices, err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )( "set_mesh_entities", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;

            // Initialize for inner iteration

            this->initialize_mesh_iteration( patch, err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )( "initialize_mesh_iteration", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;

            obj_func.reset();

            outer_crit->reset_patch( patch, err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )( "reset_patch outer", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;

            inner_crit->reset_inner( patch, obj_func, err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )( "reset_inner", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;

            inner_crit->reset_patch( patch, err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )( "inner reset_patch", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;

            // Don't even call optimizer if inner termination
            // criterion has already been met.
            if( !inner_crit->terminate() )
            {
                inner_crit_terminated = false;
                if( one_patch ) ++inner_iter;

                // Call optimizer - should loop on inner_crit->terminate()
                // size_t num_vert=patch.num_free_vertices();
                // std::cout << "P[" << get_parallel_rank() << "] tmp srk VertexMover num_vert= " <<
                // num_vert << std::endl;

                this->optimize_vertex_positions( patch, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "optimize_vertex_positions", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                // Update for changes during inner iteration
                // (during optimizer loop)

                outer_crit->accumulate_patch( patch, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "outer accumulate_patch", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                inner_crit->cull_vertices( patch, obj_func, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "inner cull_vertices", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                // experimental...
                if( 0 )
                {
                    inner_crit->cull_vertices_global( patch, mesh, domain, settings, obj_func, err );
                    if( MSQ_CHKERR( err ) )
                    {
                        MSQ_SETERR( perr )( "cull_vertices_global", MsqError::INVALID_STATE );
                        PERROR_COND;
                    }  // goto ERROR;
                }

                patch.update_mesh( err, coord_tag_ptr );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "update_mesh", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;
            }
        }  // while(p_iter....

        save_or_restore_debug_state( false );

        if( 1 )
        {
            bool pdone_inner = false;

            if( MSQ_CHKERR( perr ) )
            {
                std::cout << "P[" << get_parallel_rank()
                          << "] VertexMover::loop_over_mesh found parallel error: " << perr
                          << "\n quitting... pdone_inner= " << pdone_inner << std::endl;
                pdone_inner = true;
            }

            helper->communicate_any_true( pdone_inner, err );

            if( 0 )
                std::cout << "P[" << get_parallel_rank() << "] tmp srk inner_iter= " << inner_iter
                          << " outer_iter= " << outer_iter << " pdone_inner= " << pdone_inner << std::endl;

            if( pdone_inner )
            {
                if( 0 )
                    std::cout << "P[" << get_parallel_rank()
                              << "] tmp srk found parallel error, quitting... pdone_inner= " << pdone_inner
                              << std::endl;
                MSQ_SETERR( err )( "PARALLEL ERROR", MsqError::PARALLEL_ERROR );
                break;
            }
        }

        /// [email protected] save vertex bytes since boundary smoothing changes them
        std::vector< unsigned char > saved_bytes;
        checkpoint_bytes( mesh, saved_bytes, err );
        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( "checkpoint_bytes ", MsqError::INVALID_STATE );
            PERROR_COND;
        }  // goto ERROR;

        helper->communicate_first_independent_set( err );
        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( "communicate_first_independent_set ", MsqError::INVALID_STATE );
            PERROR_COND;
        }  // goto ERROR;

        ///*** smooth the boundary ***////
        save_or_restore_debug_state( true );
        MsqDebug::disable_all();

        while( helper->compute_next_independent_set() )
        {
            // Loop over all boundary elements
            while( helper->get_next_partition_boundary_vertex( vertex_handle ) )
            {

                patch_vertices.clear();
                patch_vertices.push_back( vertex_handle );
                patch_elements.clear();
                mesh->vertices_get_attached_elements( &vertex_handle, 1, patch_elements, junk, err );

                all_culled = false;
                patch.set_mesh_entities( patch_elements, patch_vertices, err );

                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "set_mesh_entities 2 ", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                // Initialize for inner iteration

                this->initialize_mesh_iteration( patch, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( " initialize_mesh_iteration 2", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                obj_func.reset();

                outer_crit->reset_patch( patch, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( "outer reset_patch 2 ", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                inner_crit->reset_inner( patch, obj_func, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( " inner reset_inner 2", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                inner_crit->reset_patch( patch, err );
                if( MSQ_CHKERR( err ) )
                {
                    MSQ_SETERR( perr )( " inner_crit reset_patch 2", MsqError::INVALID_STATE );
                    PERROR_COND;
                }  // goto ERROR;

                // Don't even call optimizer if inner termination
                // criterion has already been met.
                if( !inner_crit->terminate() )
                {
                    inner_crit_terminated = false;

                    // Call optimizer - should loop on inner_crit->terminate()
                    this->optimize_vertex_positions( patch, err );
                    if( MSQ_CHKERR( err ) )
                    {
                        MSQ_SETERR( perr )
                        ( " optimize_vertex_positions 2", MsqError::INVALID_STATE );
                        PERROR_COND;
                    }  // goto ERROR;

                    // Update for changes during inner iteration
                    // (during optimizer loop)

                    outer_crit->accumulate_patch( patch, err );
                    if( MSQ_CHKERR( err ) )
                    {
                        MSQ_SETERR( perr )( " outer accumulate_patch 2", MsqError::INVALID_STATE );
                        PERROR_COND;
                    }  // goto ERROR;

                    inner_crit->cull_vertices( patch, obj_func, err );
                    if( MSQ_CHKERR( err ) )
                    {
                        MSQ_SETERR( perr )( " inner cull_vertices", MsqError::INVALID_STATE );
                        PERROR_COND;
                    }  // goto ERROR;

                    // FIXME
                    if( 0 )
                    {
                        inner_crit->cull_vertices_global( patch, mesh, domain, settings, obj_func, err );
                        if( MSQ_CHKERR( err ) )
                        {
                            MSQ_SETERR( perr )
                            ( " cull_vertices_global 2 ", MsqError::INVALID_STATE );
                            PERROR_COND;
                        }  // goto ERROR;
                    }

                    patch.update_mesh( err, coord_tag_ptr );
                    if( MSQ_CHKERR( err ) )
                    {
                        MSQ_SETERR( perr )( " update_mesh 2", MsqError::INVALID_STATE );
                        PERROR_COND;
                    }  // goto ERROR;
                }
            }
            helper->communicate_next_independent_set( err );
            if( MSQ_CHKERR( err ) )
            {
                MSQ_SETERR( perr )
                ( " communicate_next_independent_set 2", MsqError::INVALID_STATE );
                PERROR_COND;
            }  // goto ERROR;
        }      // while(helper->compute_next_independent_set())

        save_or_restore_debug_state( false );

        // if (!get_parallel_rank())
        // std::cout << "P[" << get_parallel_rank() << "] tmp srk num_patches= " << num_patches <<
        // std::endl;

        /// [email protected] restore vertex bytes since boundary smoothing changes them
        restore_bytes( mesh, saved_bytes, err );
        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( " restore_bytes", MsqError::INVALID_STATE );
            PERROR_COND;
        }  // goto ERROR;

        if( jacobiOpt ) commit_jacobi_coords( coord_tag, mesh, err );

        this->terminate_mesh_iteration( patch, err );
        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( " terminate_mesh_iteration", MsqError::INVALID_STATE );
            PERROR_COND;
        }  // goto ERROR;

        outer_crit->accumulate_outer( mesh, domain, obj_func, settings, err );
        if( MSQ_CHKERR( err ) )
        {
            MSQ_SETERR( perr )( " outer_crit accumulate_outer", MsqError::INVALID_STATE );
            PERROR_COND;
        }  // goto ERROR;
    }

    // ERROR:

    if( MSQ_CHKERR( err ) )
    {
        std::cout << "P[" << get_parallel_rank() << "] VertexMover::loop_over_mesh error = " << err.error_message()
                  << std::endl;
    }

    if( jacobiOpt ) mesh->tag_delete( coord_tag, err );

    // call the criteria's cleanup funtions.
    outer_crit->cleanup( mesh, domain, err );MSQ_CHKERR( err );
    inner_crit->cleanup( mesh, domain, err );MSQ_CHKERR( err );
    // call the optimization cleanup function.
    this->cleanup();
    // close the helper
    helper->smoothing_close( err );MSQ_CHKERR( err );

    return 0.;
}

void VertexMover::initialize_queue( MeshDomainAssoc* mesh_and_domain, const Settings* settings, MsqError& err )
{
    QualityImprover::initialize_queue( mesh_and_domain, settings, err );MSQ_ERRRTN( err );
    objFuncEval.initialize_queue( mesh_and_domain, settings, err );MSQ_ERRRTN( err );
}

TagHandle VertexMover::get_jacobi_coord_tag( Mesh* mesh, MsqError& err )
{
    // Get tag handle
    const char tagname[] = "msq_jacobi_temp_coords";
    TagHandle tag        = mesh->tag_create( tagname, Mesh::DOUBLE, 3, 0, err );
    MSQ_ERRZERO( err );
    /* VertexMover will always delete the tag when it is done, so it is probably
       best not to accept an existing tag
  if (err.error_code() == TAG_ALREADY_EXISTS) {
    err.clear();
    tag = tag_get( tagname, err ); MSQ_ERRZERO(err);
    std::string name;
    Mesh::TagType type;
    unsigned length;
    mesh->tag_properties( tag, name, type, length, err ); MSQ_ERRZERO(err);
    if (type != Mesh::DOUBLE || length != 3) {
      MSQ_SETERR(err)(TAG_ALREADY_EXISTS,
                     "Tag \"%s\" already exists with invalid type",
                     tagname);
      return 0;
    }
  }
    */

    // Initialize tag value with initial vertex coordinates so that
    // vertices that never get moved end up with the correct coords.
    std::vector< Mesh::VertexHandle > vertices;
    mesh->get_all_vertices( vertices, err );
    MSQ_ERRZERO( err );
    // remove fixed vertices
    // to avoid really huge arrays (especially since we need to copy
    // coords out of annoying MsqVertex class to double array), work
    // in blocks
    const size_t blocksize = 4096;
    std::vector< bool > fixed( blocksize );
    MsqVertex coords[blocksize];
    double tag_data[3 * blocksize];
    for( size_t i = 0; i < vertices.size(); i += blocksize )
    {
        size_t count = std::min( blocksize, vertices.size() - i );
        // remove fixed vertices
        mesh->vertices_get_fixed_flag( &vertices[i], fixed, count, err );
        MSQ_ERRZERO( err );
        size_t w = 0;
        for( size_t j = 0; j < count; ++j )
            if( !fixed[j] ) vertices[i + w++] = vertices[i + j];
        count = w;
        // store tag data for free vertices
        mesh->vertices_get_coordinates( &vertices[i], coords, count, err );
        MSQ_ERRZERO( err );
        for( size_t j = 0; j < count; ++j )
        {
            tag_data[3 * j]     = coords[j][0];
            tag_data[3 * j + 1] = coords[j][1];
            tag_data[3 * j + 2] = coords[j][2];
        }
        mesh->tag_set_vertex_data( tag, count, &vertices[i], tag_data, err );
        MSQ_ERRZERO( err );
    }

    return tag;
}

void VertexMover::commit_jacobi_coords( TagHandle tag, Mesh* mesh, MsqError& err )
{
    Vector3D coords;
    std::vector< Mesh::VertexHandle > vertices;
    mesh->get_all_vertices( vertices, err );MSQ_ERRRTN( err );
    std::vector< bool > fixed( vertices.size() );
    mesh->vertices_get_fixed_flag( &vertices[0], fixed, vertices.size(), err );
    for( size_t i = 0; i < vertices.size(); ++i )
    {
        if( !fixed[i] )
        {
            mesh->tag_get_vertex_data( tag, 1, &vertices[i], coords.to_array(), err );MSQ_ERRRTN( err );
            mesh->vertex_set_coordinates( vertices[i], coords, err );MSQ_ERRRTN( err );
        }
    }
}

}  // namespace MBMesquite