This file is indexed.

/usr/include/dune/grid/io/visual/grape/grapegriddisplay.cc is in libdune-grid-dev 2.2.1-2.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#include <dune/geometry/referenceelements.hh>

namespace Dune 
{
template <int d, int w>
class AlbertaGrid;
template <class G>
struct HasLevelIntersections 
{
  static const bool value = true;
};
template <int d, int w>
struct HasLevelIntersections<AlbertaGrid<d,w> > 
{
  static const bool value = false;
};
//****************************************************************  
//
// --GrapeGridDisplay, GrapeGridDisplay for given grid
// 
//****************************************************************  

template<class GridType>
inline GrapeGridDisplay<GridType>::
GrapeGridDisplay(const GridType &grid, const int myrank ) 
 : 
#if HAVE_GRAPE
   setGridPartIter_(0)
 , entityIndex(GrapeGridDisplay<GridType>::template getEntityIndex<LeafIndexSetType>)
 , vertexIndex(GrapeGridDisplay<GridType>::template getVertexIndex<LeafIndexSetType>),
#endif
   grid_(grid) 
 , hasLevelIntersections_(HasLevelIntersections<GridType>::value)
 , gridPart_(0)
 , indexSet_( (void *) (&grid.leafIndexSet()) ) 
 , lid_(grid.localIdSet()) 
 , myRank_(myrank) 
 , hmesh_ (0)
{   
#if HAVE_GRAPE
  GrapeInterface<dim,dimworld>::init();
  if(!hmesh_) hmesh_ = setupHmesh();
#endif
}

template<class GridType>
template<class GridPartType>
inline GrapeGridDisplay<GridType>::
GrapeGridDisplay(const GridPartType &gridPart, const int myrank ) 
 : 
#if HAVE_GRAPE
   setGridPartIter_(&SetIter<GridPartType>::setGPIterator)
 , entityIndex(GrapeGridDisplay<GridType>::
     template getEntityIndex<typename GridPartType::IndexSetType>)
 , vertexIndex(GrapeGridDisplay<GridType>::
     template getVertexIndex<typename GridPartType::IndexSetType>),
#endif
   grid_(gridPart.grid()) 
 , hasLevelIntersections_(HasLevelIntersections<GridType>::value)
 , gridPart_((void *) &gridPart)
 , indexSet_( (void *) (&gridPart.indexSet()) ) 
 , lid_(grid_.localIdSet()) 
 , myRank_(myrank) 
 , hmesh_ (0)
{   
#if HAVE_GRAPE
  GrapeInterface<dim,dimworld>::init();
  if(!hmesh_) hmesh_ = setupHmesh();
#endif
}

template< class GridType >
template< class VT >
inline GrapeGridDisplay< GridType >
  ::GrapeGridDisplay ( const GridView< VT > &gridView, const int myrank )
: 
#if HAVE_GRAPE
  setGridPartIter_( &GridViewIterators< VT >::set ),
  entityIndex( getEntityIndex< typename GridView< VT >::IndexSet > ),
  vertexIndex( getVertexIndex< typename GridView< VT >::IndexSet > ),
#endif
  grid_( gridView.grid() ),
  hasLevelIntersections_(HasLevelIntersections<GridType>::value),
  gridPart_( (void *)&gridView ),
  indexSet_( (void *)&gridView.indexSet() ),
  lid_( grid_.localIdSet() ),
  myRank_( myrank ),
  hmesh_( 0 )
{
#if HAVE_GRAPE
  GrapeInterface< dim, dimworld >::init();
  if( !hmesh_ )
    hmesh_ = setupHmesh();
#endif
}

template<class GridType>
inline GrapeGridDisplay<GridType>::
~GrapeGridDisplay()
{ 
#if HAVE_GRAPE
  dune_.delete_iter(&hel_);
  ThisType::deleteStackEntry(stackEntry_);
  deleteHmesh();
#endif
}

#if HAVE_GRAPE
template<class GridType>
inline void GrapeGridDisplay<GridType>::
deleteStackEntry(StackEntryType & stackEntry)
{ 
  while( !stackEntry.empty() )
  {
    STACKENTRY * entry = stackEntry.top();
    stackEntry.pop();

    DUNE_ELEM * elem = (DUNE_ELEM *) entry->hel.user_data; 
    delete elem; 
    delete entry;
  }
}
  

//****************************************************************  
//
// --GridDisplay, Some Subroutines needed in display
// 
//****************************************************************  
/** hmesh functionen **/

template<class GridType>
template <class IntersectionIteratorType> 
inline void GrapeGridDisplay<GridType>::
checkNeighbors(IntersectionIteratorType & nit, 
               const IntersectionIteratorType & endnit, 
               DUNE_ELEM * he) 
{
  typedef typename GridType::Traits::template Codim<0>::Entity Entity;
  typedef typename IntersectionIteratorType :: Intersection IntersectionType;
  int lastElNum = -1;

  // check all faces for boundary or not 
  for( ; nit != endnit; ++nit )
  {
    const IntersectionType &intersection = *nit;
    const int number = mapDune2GrapeFace(he->type, intersection.indexInInside());
    assert( (number >= 0) && (number < MAX_EL_FACE) );

    if( number != lastElNum )
    {
      he->bnd[ number ]
        = (intersection.boundary() ? intersection.boundaryId() : 0);
      if( intersection.neighbor() )
      {
        if( intersection.outside()->partitionType() != InteriorEntity )
          he->bnd[ number ] = 2*(Entity :: dimensionworld) + (number+1);
      }
      lastElNum = number;
    }
  }
}

template<class GridType>
template <class Entity> 
inline void GrapeGridDisplay<GridType>::
el_update_base (Entity& en, DUNE_ELEM * he) 
{
  typedef typename Entity::Geometry DuneGeometryType;
  typedef typename DuneGeometryType :: ctype ctype;

  enum { dim      = Entity::dimension };
  enum { dimworld = Entity::dimensionworld };
  
  typedef FieldVector<ctype, dimworld> CoordinateType;

  const DuneGeometryType &geometry = en.geometry();
 
  he->eindex = this->entityIndex(indexSet_,en);
  he->level  = en.level();

  // if not true, only the macro level is drawn 
  he->has_children = 1;
  
  // know the type 
  int geomType = convertToGrapeType( geometry.type(), dim ); 
  he->type = geomType;

  // get pointer to coordinates and copy from geometry 
  double ** vpointer = he->vpointer;

  // number of corners and number of vertices schould be the same
  // grape visual does not work for other situations 
  assert( en.template count<dim>() == geometry.corners() );
  assert( geometry.corners() <= MAX_EL_DOF );
  
  for( int i = 0; i < geometry.corners(); ++i )
  {
    const int grapeVx = mapDune2GrapeVertex( geomType, i );
    he->vindex[ i ] = this->vertexIndex( indexSet_, en, grapeVx );
    
    assert( Entity::dimensionworld <= 3 );
    const CoordinateType coord = geometry.corner( grapeVx ); 
    for( int j = 0; j < Entity::dimensionworld ; ++j )
    {
      // here the mapping from dune to grape elements is done 
      // it's only different for quads and hexas 
      vpointer[ i ][ j ] = coord[ j ];
    }
  }
}

template<class GridType>
template <class EntityPointerType> 
inline int GrapeGridDisplay<GridType>::
el_update (EntityPointerType * it, DUNE_ELEM * he) 
{
  typedef typename GridType::Traits::template Codim<0>::Entity Entity;
  typedef typename Entity::Geometry DuneGeometryType;
  typedef typename DuneGeometryType :: ctype ctype;

  enum { dim      = Entity::dimension };
  enum { dimworld = Entity::dimensionworld };
  
  typedef FieldVector<ctype, dimworld> CoordinateType;

  Entity &en = (*it[0]);

  // only for debuging, becsaue normaly references are != NULL
  if(&en)
  {
    el_update_base( en, he );

    {
      if( en.hasBoundaryIntersections() )
      {
        // reset the boundary information 
        for(int i=0; i < MAX_EL_FACE; ++i) he->bnd[i] = -1;

        if( en.isLeaf() )
        {
          typedef typename Entity::LeafIntersectionIterator IntersectionIterator;
          IntersectionIterator endnit = en.ileafend();
          IntersectionIterator nit    = en.ileafbegin();

          checkNeighbors(nit,endnit,he);
        }
        else if(hasLevelIntersections_)
        {
          typedef typename Entity::LevelIntersectionIterator IntersectionIterator;
          IntersectionIterator endnit = en.ilevelend();
          IntersectionIterator nit    = en.ilevelbegin();

          checkNeighbors(nit,endnit,he);
        }
      }
      else 
      {
        // if no boundary intersections, then all faces are interior 
        for(int i=0; i < MAX_EL_FACE; ++i) he->bnd[i] = 0;
      }
    }

    // for data displaying 
    he->actElement = it;
    return 1;

  } // end if(&en)
  else 
  {
    he->actElement = 0;
    return 0;
  }
}

template<class GridType>
template <class EntityPointerType, class GridPartType> 
inline int GrapeGridDisplay<GridType>::
el_update (EntityPointerType * it, DUNE_ELEM * he, GridPartType& gridPart) 
{
  typedef typename GridType::Traits::template Codim<0>::Entity Entity;
  typedef typename Entity::Geometry DuneGeometryType;
  typedef typename DuneGeometryType :: ctype ctype;

  enum { dim      = Entity::dimension };
  enum { dimworld = Entity::dimensionworld };
  
  typedef FieldVector<ctype, dimworld> CoordinateType;

  const Entity &en = (*it[0]);

  // only for debuging, becsaue normaly references are != NULL
  if(&en)
  {
    el_update_base ( en , he );

    {
      typedef typename GridPartType :: IntersectionIteratorType IntersectionIteratorType;
      // reset the boundary information 
      for(int i=0; i < MAX_EL_FACE; ++i) he->bnd[i] = -1;

      IntersectionIteratorType endnit = gridPart.iend(en);
      IntersectionIteratorType    nit = gridPart.ibegin(en);

      checkNeighbors(nit,endnit,he);
    }

    // for data displaying 
    he->actElement = it;
    return 1;

  } // end if(&en)
  else 
  {
    he->actElement = 0;
    return 0;
  }
}


template< class GridType >
template< class EntityPointer, class VT >
inline int GrapeGridDisplay< GridType >
  ::el_update ( EntityPointer *it, DUNE_ELEM *he, const GridView< VT > &gridView )
{
  typedef typename GridView< VT >::template Codim< 0 >::Entity Entity;
  typedef typename Entity::Geometry DuneGeometryType;
  typedef typename DuneGeometryType::ctype ctype;

  //const int dim      = Entity::dimension;
  const int dimworld = Entity::dimensionworld;
  
  typedef FieldVector< ctype, dimworld > CoordinateType;

  Entity &entity = **it;
  assert( &entity != 0 );

  el_update_base ( entity, he );

  typedef typename GridView< VT >::IntersectionIterator IntersectionIterator;
  // reset the boundary information 
  for( int i = 0; i < MAX_EL_FACE; ++i )
    he->bnd[ i ] = -1;

  IntersectionIterator iend = gridView.iend( entity );
  IntersectionIterator iit  = gridView.ibegin( entity );

  checkNeighbors( iit, iend, he );

  // for data displaying 
  he->actElement = it;
  return 1;
}


template<class GridType>
template<PartitionIteratorType pitype> 
inline int GrapeGridDisplay<GridType>::
first_leaf (DUNE_ELEM * he) 
{
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LeafIterator LeafIteratorType; 

  if(he->liter) dune_.delete_iter(he);

  he->liter   = 0;
  he->enditer = 0;

  LeafIteratorType * it    = new LeafIteratorType ( grid_.leafView().template begin<0, pitype> () );
  LeafIteratorType * endit = new LeafIteratorType ( grid_.leafView().template end  <0, pitype> () );

  he->liter   = (void *) it;
  he->enditer = (void *) endit;

  if(it[0] == endit[0]) 
  {
    this->template delete_leaf<pitype>(he);
    return 0;
  }

  return el_update(it,he);
}

template<class GridType>
template<PartitionIteratorType pitype> 
inline int GrapeGridDisplay<GridType>::
next_leaf (DUNE_ELEM * he) 
{
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LeafIterator LeafIteratorType; 
  
  LeafIteratorType * it    = (LeafIteratorType *) he->liter;
  LeafIteratorType * endit = (LeafIteratorType *) he->enditer;
  assert( it );
  assert( endit );
  
  if( ++it[0] != endit[0] )
  {
    return el_update(it,he); 
  }
  else 
  {
    this->template delete_leaf<pitype> (he);
  }
  return 0;
}

template<class GridType>
template<class GridPartType> 
inline int GrapeGridDisplay<GridType>::
first_item (DUNE_ELEM * he) 
{
  typedef typename GridPartType :: template Codim<0> :: IteratorType IteratorType;

  if(he->liter) dune_.delete_iter(he);
  
  assert( he->gridPart );
  GridPartType & gridPart = *((GridPartType *) he->gridPart);

  assert( he->liter   == 0 );
  assert( he->enditer == 0 );
  
  IteratorType * it    = new IteratorType ( gridPart.template begin<0> () );
  IteratorType * endit = new IteratorType ( gridPart.template end  <0> () );

  he->liter   = (void *) it;
  he->enditer = (void *) endit;
  
  if(it[0] == endit[0]) 
  {
    this->template delete_iterators<IteratorType> (he);
    return 0;
  }

  return el_update(it,he,gridPart);
}

template<class GridType>
template<class GridPartType> 
inline int GrapeGridDisplay<GridType>::
next_item (DUNE_ELEM * he) 
{
  typedef typename GridPartType :: template Codim<0> :: IteratorType IteratorType;
  
  assert( he->gridPart );
  GridPartType & gridPart = *((GridPartType *) he->gridPart);

  IteratorType * it    = (IteratorType *) he->liter;
  IteratorType * endit = (IteratorType *) he->enditer;
  assert( it );
  assert( endit );
  
  if( ++it[0] != endit[0] )
  {
    return el_update(it,he,gridPart); 
  }
  else 
  {
    this->template delete_iterators<IteratorType> (he); 
  }
  return 0;
}

template<class GridType>
template<PartitionIteratorType pitype>
inline int GrapeGridDisplay<GridType>::
first_level (DUNE_ELEM * he, int lvl) 
{
  if(he->liter) dune_.delete_iter(he);
  
  assert( he->liter   == 0 );
  assert( he->enditer == 0 );

  // for leaf level, lvl has the value -1 
  int level = (lvl < 0) ? grid_.maxLevel() : lvl;

  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LevelIterator LevelIteratorType; 
  
  // class copy constructor  
  LevelIteratorType * it    = new LevelIteratorType( grid_.levelView( level ).template begin<0,pitype> () );
  LevelIteratorType * endit = new LevelIteratorType( grid_.levelView( level ).template end<0,pitype>   () );
  
  he->liter   = (void *) it;
  he->enditer = (void *) endit;

  if(it[0] == endit[0]) 
  {
    this->template delete_level<pitype>(he);
    return 0;
  }
  
  return el_update(it,he);
}


template<class GridType>
template<PartitionIteratorType pitype>
inline int GrapeGridDisplay<GridType>::
next_level (DUNE_ELEM * he) 
{
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LevelIterator LevelIteratorType; 
  
  LevelIteratorType * it    = ((LevelIteratorType *) he->liter);
  LevelIteratorType * endit = ((LevelIteratorType *) he->enditer); 

  assert( it );
  assert( endit );
  if( ++it[0] != endit[0] )
  {
    return el_update(it,he); 
  }
  else 
  {
    this->template delete_level<pitype>(he);

    // clear all hierachic iterators 
    while(!hierList_.empty())
    {
      HierarchicIteratorType * hit = hierList_.back();
      hierList_.pop_back();
      delete hit;
    }
    assert( hierList_.size () == 0 );
  }
  return 0;
}


template<class GridType>
template<class EntityPointerType>
inline int GrapeGridDisplay<GridType>::
child_update(EntityPointerType * it, DUNE_ELEM * he) 
{
  typedef typename  GridType :: template Codim<0> :: Entity EntityType;

  const EntityType & en = (*it[0]);

  HierarchicIteratorType * hit = (HierarchicIteratorType *) he->hiter;
  
  const EntityType *newEn = (!hit) ? (&en) : (hit[0].operator -> ()) ;

  assert( newEn );
  
  // if entity is leaf, then no first child 
  if( newEn->isLeaf() ) return 0;

  int childLevel = newEn->level() + 1;

  // store former pointer for removal later 
  if(hit) hierList_.push_back( hit );
  
  // create HierarchicIterator with default constructor 
  hit = new HierarchicIteratorType ( newEn->hbegin ( childLevel ) );
  
  assert( hit != 0 ); 
  if( hit[0] != newEn->hend( childLevel ) )
  { 
    he->hiter = (void *) hit;
    return el_update( hit, he);
  }
  else
  {
    hierList_.pop_back();
    delete hit;
    return 0;
  }
}

template<class GridType>
template<class EntityPointerType>
inline int GrapeGridDisplay<GridType>::
child_n_update(EntityPointerType *it, DUNE_ELEM * he) 
{
  typedef typename  GridType::Traits::template Codim<0>::Entity EntityType;
  
  const EntityType &en = (*it[0]);
  
  int childLevel = en.level();
  HierarchicIteratorType * hit = (HierarchicIteratorType *) he->hiter;
  assert( hit );
  
  //HierarchicIteratorType ehit = hit[0]->hend(childLevel);
  HierarchicIteratorType ehit = en.hend(childLevel);
  if( ++hit[0] != ehit )
  {
    return el_update(hit,he); 
  }
 
  hierList_.remove( hit );
  delete hit;
  he->hiter = 0;

  return 0;
}



template<class GridType>
template<class IteratorType> 
inline void GrapeGridDisplay<GridType>::
delete_iterators(DUNE_ELEM * he) 
{
  IteratorType * it  = ((IteratorType *) he->liter);
  if(it) 
  {
    IteratorType * endit = ((IteratorType *) he->enditer);
    assert( endit );

    delete it; 
    delete endit; 

    he->actElement = 0;
    he->liter      = 0;
    he->enditer    = 0;
  }
}

template<class GridType>
template<PartitionIteratorType pitype> 
inline void GrapeGridDisplay<GridType>::
delete_leaf (DUNE_ELEM * he) 
{
  assert( he );
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LeafIterator IteratorType; 

  this->template delete_iterators<IteratorType> (he);
}

template<class GridType>
template<PartitionIteratorType pitype> 
inline void GrapeGridDisplay<GridType>::
delete_level (DUNE_ELEM * he) 
{
  assert( he );
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LevelIterator IteratorType; 

  this->template delete_iterators<IteratorType> (he);
}

template<class GridType>
template<PartitionIteratorType pitype> 
inline void GrapeGridDisplay<GridType>::
delete_hier (DUNE_ELEM * he) 
{
  assert( he );
  typedef typename GridType :: template Codim<0> :: 
      template Partition<pitype> :: LevelIterator IteratorType; 

  this->template delete_iterators<IteratorType> (he);
  
  // clear all hierachic iterators 
  while(!hierList_.empty())
  {
    HierarchicIteratorType * hit = hierList_.back();
    hierList_.pop_back();
    delete hit;
  }
  assert( hierList_.size () == 0 );
}


template<class GridType>
inline int GrapeGridDisplay<GridType>::
first_child(DUNE_ELEM * he) 
{
  typedef typename GridType :: template Codim<0> :: 
    EntityPointer  EntityPointerType; 
  return child_update( ((EntityPointerType *) he->liter) , he);
}


template<class GridType>
inline int GrapeGridDisplay<GridType>::
next_child(DUNE_ELEM * he) 
{
  typedef typename GridType :: template Codim<0> :: 
    EntityPointer  EntityPointerType; 
  return child_n_update( ((EntityPointerType *) he->liter), he);
}


template<class GridType>
inline void * GrapeGridDisplay<GridType>::
copy_iterator (const void * i) 
{
  std::cerr << "ERROR: copt_iterator not implemented! file = " << __FILE__ << ", line = " << __LINE__ << "\n";
  DUNE_THROW(NotImplemented,"method copy_iterator not implemented!"); 
  return 0 ;
}

// checkInside 
template< class GridType >
template< class Entity >
inline int GrapeGridDisplay< GridType >
  ::checkInside( const Entity &entity, const double *c )
{
  const int dim = Entity::dimension;

  FieldVector< double, dim > local;
  for( int i = 0; i < dim; ++i )
    local[ i ] = c[ i ];
   
  // see hmesh doc page 32, if point is inside, -1 has to be returned
  // otherwise local face , grrrr
  const GenericReferenceElement< double, dim > &refElement
    = GenericReferenceElements< double, dim >::general( entity.type() );
  return (refElement.checkInside( local ) ? -1 : 0);
}

// check inside 
template<class GridType>
inline int GrapeGridDisplay<GridType>::
checkWhetherInside(DUNE_ELEM * he, const double * w) 
{
  typedef typename GridType::template Codim<0>::EntityPointer  EntityPointerType; 
  EntityPointerType * ep = (EntityPointerType *) he->actElement;
  assert( ep );
  return checkInside(*(ep[0]),w);
}

// world to local 
template<class GridType> template <class EntityType>
inline void GrapeGridDisplay<GridType>::
local_to_world(const EntityType &en, const double * c, double * w) 
{
  const int dim = EntityType::dimension;
  const int dimworld = EntityType::dimensionworld;

  FieldVector< double, dim > local;
  for( int i = 0; i < dim; ++i )
    local[ i ] = c[ i ];

  FieldVector< double, dimworld > global = en.geometry().global( local );
  for( int i = 0; i < dimworld; ++i )
    w[ i ] = global[ i ];
}


template<class GridType>
inline void GrapeGridDisplay<GridType>::
local2world (DUNE_ELEM * he, const double * c, double * w) 
{
  typedef typename GridType::template Codim<0>::EntityPointer  EntityPointerType; 
  EntityPointerType * ep = (EntityPointerType *) he->actElement;
  assert( ep );
  local_to_world(*(ep[0]),c,w);
  return;  
} 

// world to local 
template< class GridType >
template< class Entity >
inline int GrapeGridDisplay< GridType >
  ::world_to_local( const Entity &entity, const double *w, double *c )
{
  const int dim = Entity::dimension;
  const int dimworld = Entity::dimensionworld;

  const typename Entity::Geometry &geometry = entity.geometry();

  FieldVector< double, dimworld > global;
  for( int i = 0; i < dimworld; ++i )
    global[ i ] = w[ i ];
  FieldVector< double, dim > local = geometry.local( global );
  for( int i = 0; i < dim; ++i )
    c[ i ] = local[ i ];

  const GenericReferenceElement< double, dim > &refElement
    = GenericReferenceElements< double, dim >::general( geometry.type() );
  return (refElement.checkInside( local ) ? -1 : 0);
}

// world to local 
template<class GridType>
inline int GrapeGridDisplay<GridType>::
world2local(DUNE_ELEM * he, const double * w, double * c) 
{
  typedef typename GridType::template Codim<0>::EntityPointer  EntityPointerType; 
  EntityPointerType * ep = (EntityPointerType *) he->actElement;
  assert( ep );
  return world_to_local(*(ep[0]),w,c);
}


// world to local 
template<class GridType>
template<PartitionIteratorType pitype>
inline void GrapeGridDisplay<GridType>::
selectIterators(DUNE_DAT * dune, void * gridPart, setGridPartIterators_t * func) const
{
  // if pointer are 0, then no evaluation is done 
  dune->first_child = 0;
  dune->next_child  = 0;

  if(dune->iteratorType == g_LeafIterator) 
  { 
    dune->first_macro = &IterationMethods<pitype>::fst_leaf;
    dune->next_macro  = &IterationMethods<pitype>::nxt_leaf;
    dune->delete_iter = &IterationMethods<pitype>::del_leaf;

    return ;
  }

  if(dune->iteratorType == g_LevelIterator)
  {
    dune->first_macro = &IterationMethods<pitype>::first_lev;
    dune->next_macro  = &IterationMethods<pitype>::next_lev;
    dune->delete_iter = &IterationMethods<pitype>::del_level;

    return ;
  }
  
  if(dune->iteratorType == g_HierarchicIterator)
  {
    dune->first_macro = &IterationMethods<pitype>::first_mac;
    dune->next_macro  = &IterationMethods<pitype>::next_lev;
    dune->delete_iter = &IterationMethods<pitype>::del_hier;
    
    dune->first_child = &IterationMethods<pitype>::fst_child;
    dune->next_child  = &IterationMethods<pitype>::nxt_child;

    return ;
  }

  if(dune->iteratorType == g_GridPart)
  {
    static bool called = false;
    if(!func) 
    {
      std::string name("Null");
      if(!called)
        std::cerr << "No function for data '" <<name<<"' and therefore no GridPart! Defaulting Iterator to LeafIterator! \n";

      dune->first_macro = &IterationMethods<pitype>::fst_leaf;
      dune->next_macro  = &IterationMethods<pitype>::nxt_leaf;

      dune->gridPart = 0;
      called = true; 
      return ;
    }
  
    assert( func );
    assert( gridPart );
    // set first and next methods due to grid part 
    func(dune,gridPart);
    called = false;

    return ;
  }
 
  // wrong iteratorType here
  assert(false);
  abort();
}


// setIterationsMethods 
template<class GridType>
inline void GrapeGridDisplay<GridType>::
setIterationMethods(DUNE_DAT * dune, DUNE_FDATA * data) const
{
  if(dune->delete_iter) dune->delete_iter(dune->all);

  void * gridPart = 0;
  setGridPartIterators_t * func = 0;

  if(data) 
  {
    gridPart = data->gridPart;
    func = data->setGridPartIterators;
  }
  else if(gridPart_ && setGridPartIter_)
  {
    gridPart = gridPart_;
    func = setGridPartIter_;
  }
    
  switch(dune->partitionIteratorType)
  {
    case g_All_Partition:            selectIterators<All_Partition> (dune,gridPart,func) ;            
                                     return; 
    case g_Interior_Partition:       selectIterators<Interior_Partition> (dune,gridPart,func) ;       
                                     return; 
    case g_InteriorBorder_Partition: selectIterators<InteriorBorder_Partition> (dune,gridPart,func) ; 
                                     return; 
    case g_Overlap_Partition:        selectIterators<Overlap_Partition> (dune,gridPart,func) ;        
                                     return; 
    case g_OverlapFront_Partition:   selectIterators<OverlapFront_Partition> (dune,gridPart,func) ;   
                                     return; 
    case g_Ghost_Partition:          selectIterators<Ghost_Partition> (dune,gridPart,func) ;          
                                     return; 
    default: assert(false); 
             abort(); 
             return ;
  }
}

// setIterationsMethods 
template<class GridType>
inline void GrapeGridDisplay<GridType>::
changeIterationMethods(int iterType, int partType, DUNE_FDATA * data )
{
  dune_.iteratorType = iterType;
  dune_.partitionIteratorType = partType;
  setIterationMethods(&dune_,data);
}


// check inside 
template<class GridType>
inline int GrapeGridDisplay<GridType>::
check_inside(DUNE_ELEM * he, const double * w) 
{
  MyDisplayType * disp = (MyDisplayType *) he->display;
  return disp[0].checkWhetherInside(he,w);
}
// local to world 
template<class GridType>
inline void GrapeGridDisplay<GridType>::
ctow (DUNE_ELEM * he, const double * c, double * w) 
{
  MyDisplayType * disp = (MyDisplayType *) he->display;
  disp[0].local2world(he,c,w);
  return ;
}

// world to local 
template<class GridType>
inline int GrapeGridDisplay<GridType>::
wtoc(DUNE_ELEM * he, const double * w, double * c) 
{
  MyDisplayType * disp = (MyDisplayType *) he->display;
  return disp[0].world2local(he,w,c);
}

template<class GridType>
inline void GrapeGridDisplay<GridType>::
setIterationModus(DUNE_DAT * dat, DUNE_FDATA * func) 
{
  MyDisplayType * disp = (MyDisplayType *) dat->all->display;
  disp[0].setIterationMethods(dat,func);
}

template<class GridType>
inline void * GrapeGridDisplay<GridType>::getHmesh()
{ 
  if(!hmesh_) hmesh_ = setupHmesh();
  return (void *) hmesh_;
}

template<class GridType>
inline void GrapeGridDisplay<GridType>::
addMyMeshToTimeScene(void * timescene, double time, int proc)
{ 
  GrapeInterface<dim,dimworld>::addHmeshToTimeScene(timescene,time,this->getHmesh(),proc);
}

template<class GridType>
inline void * GrapeGridDisplay<GridType>::setupHmesh()
{ 
  int maxlevel = grid_.maxLevel();

  int noe = grid_.size(0);
  int nov = grid_.size(dim);

  // set pointer to me 
  hel_.display = (void *) this;

  // set dune pointers 
  DUNE_DAT * dune = &dune_;  
 
  dune->wtoc         = wtoc;
  dune->ctow         = ctow;
  dune->check_inside = check_inside;

  // set method to select iterators 
  dune->setIterationModus = &setIterationModus; 

  dune->get_stackentry = &getStackEn;
  dune->free_stackentry = &freeStackEn;

  dune->all          = &hel_;
  dune->partition    = myRank_;

  dune->iteratorType          = g_LeafIterator;  
  dune->partitionIteratorType = g_All_Partition;

  setIterationMethods(dune,0);
  
  std::string gridName( "Grid" );
  /* return hmesh with no data */
  return GrapeInterface<dim,dimworld>::
            setupHmesh(noe,nov,maxlevel,dune, gridName.c_str());
}

template<class GridType>
inline void GrapeGridDisplay<GridType>::deleteHmesh()
{ 
  if( hmesh_ )
  {
    GrapeInterface<dim,dimworld>::deleteHmesh(hmesh_);
  }
}


template<class GridType>
inline void * GrapeGridDisplay<GridType>::
getStackEntry(StackEntryType & stackEntry)
{
  STACKENTRY * entry = 0;
  
  if( stackEntry.empty() )
  {
    entry = new STACKENTRY ();
    DUNE_ELEM * elem = new DUNE_ELEM ();
    assert( elem );
    entry->hel.user_data = (void *)elem;
  }
  else
  {
    entry = stackEntry.top();
    stackEntry.pop();
  }
  assert( entry );
  return( (void *) entry);
}

template<class GridType>
inline void GrapeGridDisplay<GridType>::
freeStackEntry(StackEntryType & stackEntry, void * entry)
{ 
  assert( entry );
  stackEntry.push( ((STACKENTRY *) entry) );
}

template<class GridType>
inline void * GrapeGridDisplay<GridType>::
getStackEn(DUNE_DAT * dune)
{
  MyDisplayType * disp = (MyDisplayType *) dune->all->display;
  return MyDisplayType::getStackEntry(disp->stackEntry_);
}

template<class GridType>
inline void GrapeGridDisplay<GridType>::
freeStackEn(DUNE_DAT * dune, void * entry)
{
  MyDisplayType * disp = (MyDisplayType *) dune->all->display;
  MyDisplayType::freeStackEntry(disp->stackEntry_,entry);
}

#endif

template<class GridType>
inline void GrapeGridDisplay<GridType>::display()
{ 
#if HAVE_GRAPE
  /* call handle mesh in g_hmesh.c */
  GrapeInterface<dim,dimworld>::handleMesh ( hmesh_ , true ); 
#endif
  return ;
}

template<class GridType>
inline const GridType & GrapeGridDisplay<GridType>::getGrid() const
{ 
  return grid_;
}

} // end namespace Dune