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
#ifndef __smoab_SimpleMoab_h
#define __smoab_SimpleMoab_h

#include "moab/Core.hpp"
#include "moab/Interface.hpp"
#include "moab/Range.hpp"
#include "moab/CN.hpp"

#include "MBTagConventions.hpp"

#include <iostream>

namespace smoab
{
//adjacency intersect / union named enum to match
//the types from moab
enum adjacency_type
{
    INTERSECT = moab::Interface::INTERSECT,
    UNION     = moab::Interface::UNION
};

//make our range equal to moabs range
typedef moab::Range Range;
typedef moab::EntityHandle EntityHandle;
typedef moab::EntityType EntityType;

//bring in range functions
using moab::intersect;
using moab::subtract;
using moab::unite;

class Tag
{
    const std::string Name_;

  public:
    Tag( std::string const& n ) : Name_( n ) {}

    virtual ~Tag() {}

    const char* name() const
    {
        return this->Name_.c_str();
    }
    moab::DataType virtual dataType() const
    {
        return moab::MB_TYPE_INTEGER;
    }
    virtual bool isComparable() const<--- Virtual function in base class
    {
        return false;
    }
    virtual int value() const<--- Virtual function in base class
    {
        return int();
    }
};

//lightweight structs to wrap set names, so we detected
//incorrect names at compile time. In the future I expect material and
//boundary conditions to be comparable
class MaterialTag : public Tag
{
  public:
    MaterialTag() : Tag( "MATERIAL_SET" ) {}
};
class DirichletTag : public Tag
{
  public:
    DirichletTag() : Tag( "DIRICHLET_SET" ) {}
};
class NeumannTag : public Tag
{
  public:
    NeumannTag() : Tag( "NEUMANN_SET" ) {}
};
class GroupTag : public Tag
{
  public:
    GroupTag() : Tag( "GROUP" ) {}
};

//geom is the only comparable tag, since it can have a dimension.
class GeomTag : public Tag
{
    int dim;

  public:
    GeomTag( int d ) : Tag( "GEOM_DIMENSION" ), dim( d ) {}
    GeomTag() : Tag( "GEOM_DIMENSION" ), dim( 0 ) {}

    virtual ~GeomTag() {}

    bool isComparable() const<--- Function in derived class
    {
        return dim > 0;
    }
    int value() const<--- Function in derived class
    {
        return dim;
    }
};

//forward declare this->Moab for Tag
struct Interface;

//forward declare the DataSetConverter so it can be a friend of Interface
class DataSetConverter;

//forward declare the LoadGeometry so it can be a friend of Interface
namespace detail
{
    class LoadGeometry;
}
namespace detail
{
    class LoadPoly;
}

//light weight wrapper on a moab this->Moab that exposes only the reduced class
//that we need
class Interface
{
  public:
    Interface( const std::string& file )
    {
        this->Moab = new moab::Core();<--- Class 'Interface' does not have a copy constructor which is recommended since it has dynamic memory/resource allocation(s).<--- Class 'Interface' does not have a operator= which is recommended since it has dynamic memory/resource allocation(s).
        this->Moab->load_file( file.c_str() );
    }

    ~Interface()
    {
        if( this->Moab )
        {
            delete this->Moab;
            this->Moab = NULL;
        }
    }

    //----------------------------------------------------------------------------
    moab::Tag getMoabTag( const smoab::Tag& simpleTag ) const
    {
        moab::Tag tag;
        this->Moab->tag_get_handle( simpleTag.name(), 1, simpleTag.dataType(), tag );
        return tag;
    }

    //----------------------------------------------------------------------------
    template < typename T >
    T getDefaultTagVaue( moab::Tag tag ) const
    {
        T defaultValue;
        this->Moab->tag_get_default_value( tag, &defaultValue );
        return defaultValue;
    }

    //----------------------------------------------------------------------------
    template < typename T >
    T getDefaultTagVaue( smoab::Tag tag ) const
    {
        return this->getDefaultTagVaue< T >( getMoabTag( tag ) );
    }

    //----------------------------------------------------------------------------
    template < typename T >
    T getTagData( moab::Tag tag, const smoab::EntityHandle& entity, T value ) const
    {
        this->Moab->tag_get_data( tag, &entity, 1, &value );
        return value;
    }

    //----------------------------------------------------------------------------
    template < typename T >
    T getTagData( smoab::Tag tag, const smoab::EntityHandle& entity, T value = T() ) const
    {
        return this->getTagData( getMoabTag( tag ), entity, value );
    }

    //----------------------------------------------------------------------------
    //returns the moab name for the given entity handle if it has a sparse Name tag
    std::string name( const smoab::EntityHandle& entity ) const
    {
        moab::Tag nameTag;
        moab::ErrorCode rval =
            this->Moab->tag_get_handle( NAME_TAG_NAME, NAME_TAG_SIZE, moab::MB_TYPE_OPAQUE, nameTag );
        if( rval != moab::MB_SUCCESS )
        {
            return std::string();
        }

        char name[NAME_TAG_SIZE];
        rval = this->Moab->tag_get_data( nameTag, &entity, 1, &name );
        if( rval != moab::MB_SUCCESS )
        {
            return std::string();
        }

        return std::string( name );
    }

    //----------------------------------------------------------------------------
    //returns the geometeric dimension of an entity.
    int dimension( const smoab::EntityHandle& entity ) const
    {
        return this->Moab->dimension_from_handle( entity );
    }

    //----------------------------------------------------------------------------
    //returns the geometeric dimension of an entity.
    smoab::EntityType entityType( const smoab::EntityHandle& entity ) const
    {
        return this->Moab->type_from_handle( entity );
    }

    //----------------------------------------------------------------------------
    smoab::EntityHandle getRoot() const
    {
        return this->Moab->get_root_set();
    }

    //----------------------------------------------------------------------------
    smoab::Range findEntities( const smoab::EntityHandle root, moab::EntityType type ) const
    {
        smoab::Range result;
        // get all the sets of that type in the mesh
        this->Moab->get_entities_by_type( root, type, result );
        return result;
    }

    //----------------------------------------------------------------------------
    //given a single entity handle find all items in that mesh set that aren't
    //them selves entitysets. If recurse is true we also recurse sub entitysets
    smoab::Range findAllMeshEntities( smoab::EntityHandle const& entity, bool recurse = false ) const
    {
        smoab::Range result;
        this->Moab->get_entities_by_handle( entity, result, recurse );
        return result;
    }

    //----------------------------------------------------------------------------
    //Find all entities with a given tag. We don't use geom as a tag as that
    //isn't a fast operation. Yes finding the intersection of geom entities and
    //a material / boundary tag will be more work, but it is rarely done currently
    //Returns the found group of entities
    smoab::Range findEntitiesWithTag( const smoab::Tag& tag,
                                      smoab::EntityHandle root,
                                      moab::EntityType type = moab::MBENTITYSET ) const
    {
        smoab::Range result;

        moab::Tag t = this->getMoabTag( tag );

        // get all the entities of that type in the mesh
        this->Moab->get_entities_by_type_and_tag( root, type, &t, NULL, 1, result );

        if( tag.isComparable() )
        {
            int value = 0;<--- Variable 'value' is assigned a value that is never used.
            //now we have to remove any that doesn't match the tag value
            smoab::Range resultMatchingTag;
            typedef moab::Range::const_iterator iterator;
            for( iterator i = result.begin(); i != result.end(); ++i )
            {
                value                     = 0;
                moab::EntityHandle handle = *i;
                this->Moab->tag_get_data( t, &handle, 1, &value );
                if( value == tag.value() )
                {
                    resultMatchingTag.insert( *i );
                }
            }

            return resultMatchingTag;
        }
        else
        {
            //we return all the items we found
            return result;
        }
    }

    //----------------------------------------------------------------------------
    //Find all entities from a given root of a given dimensionality
    smoab::Range findEntitiesWithDimension( const smoab::EntityHandle root,
                                            const int dimension,
                                            bool recurse = false ) const
    {
        typedef smoab::Range::const_iterator iterator;

        smoab::Range result;
        this->Moab->get_entities_by_dimension( root, dimension, result, recurse );

        if( recurse )
        {
            smoab::Range children;
            this->Moab->get_child_meshsets( root, children, 0 );
            for( iterator i = children.begin(); i != children.end(); ++i )
            {
                this->Moab->get_entities_by_dimension( *i, dimension, result );
            }
        }
        return result;
    }

    //----------------------------------------------------------------------------
    smoab::Range findHighestDimensionEntities( const smoab::EntityHandle& entity, bool recurse = false ) const
    {
        //the goal is to load all entities that are not entity sets of this
        //node, while also subsetting by the highest dimension

        //lets find the entities of only the highest dimension
        int num_ents = 0;
        int dim      = 3;
        while( num_ents <= 0 && dim > 0 )
        {
            this->Moab->get_number_entities_by_dimension( entity, dim, num_ents, recurse );
            --dim;
        }
        ++dim;  //reincrement to correct last decrement
        if( num_ents > 0 )
        {
            //we have found entities of a given dimension
            return this->findEntitiesWithDimension( entity, dim, recurse );
        }
        return smoab::Range();
    }

    //----------------------------------------------------------------------------
    //Find all elements in the database that have children and zero parents.
    //this doesn't find
    smoab::Range findEntityRootParents( const smoab::EntityHandle& root ) const
    {
        smoab::Range parents;

        typedef moab::Range::const_iterator iterator;
        moab::Range sets;

        this->Moab->get_entities_by_type( root, moab::MBENTITYSET, sets );
        for( iterator i = sets.begin(); i != sets.end(); ++i )
        {
            int numParents = 0, numChildren = 0;
            this->Moab->num_parent_meshsets( *i, &numParents );
            if( numParents == 0 )
            {
                this->Moab->num_child_meshsets( *i, &numChildren );
                if( numChildren >= 0 )
                {
                    parents.insert( *i );
                }
            }
        }
        return parents;
    }

    //----------------------------------------------------------------------------
    //finds entities that have zero children and zero parents
    smoab::Range findDetachedEntities( const moab::EntityHandle& root ) const
    {
        smoab::Range detached;

        typedef moab::Range::const_iterator iterator;
        moab::Range sets;

        this->Moab->get_entities_by_type( root, moab::MBENTITYSET, sets );
        for( iterator i = sets.begin(); i != sets.end(); ++i )
        {
            int numParents = 0, numChildren = 0;
            this->Moab->num_parent_meshsets( *i, &numParents );
            if( numParents == 0 )
            {
                this->Moab->num_child_meshsets( *i, &numChildren );
                if( numChildren == 0 )
                {
                    detached.insert( *i );
                }
            }
        }
        return detached;
    }

    //----------------------------------------------------------------------------
    //find all children of the entity passed in that has multiple parents
    smoab::Range findEntitiesWithMultipleParents( const smoab::EntityHandle& root ) const
    {
        smoab::Range multipleParents;
        typedef moab::Range::const_iterator iterator;

        //for all the elements in the range, find all items with multiple parents
        moab::Range children;
        this->Moab->get_child_meshsets( root, children, 0 );
        for( iterator i = children.begin(); i != children.end(); ++i )
        {
            int numParents = 0;
            this->Moab->num_parent_meshsets( *i, &numParents );
            if( numParents > 1 )
            {
                multipleParents.insert( *i );
            }
        }
        return multipleParents;
    }

    //----------------------------------------------------------------------------
    //find all entities that are adjacent to a single entity
    smoab::Range findAdjacencies( const smoab::EntityHandle& entity, int dimension ) const
    {
        const int adjType = static_cast< int >( smoab::INTERSECT );
        smoab::Range result;
        const bool create_if_missing = false;
        this->Moab->get_adjacencies( &entity, 1, dimension, create_if_missing, result, adjType );
        return result;
    }

    //----------------------------------------------------------------------------
    smoab::Range findAdjacencies( const smoab::Range& range,
                                  int dimension,
                                  const smoab::adjacency_type type = smoab::UNION ) const
    {
        //the smoab and moab adjacent intersection enums are in the same order
        const int adjType = static_cast< int >( type );
        smoab::Range result;
        const bool create_if_missing = false;
        this->Moab->get_adjacencies( range, dimension, create_if_missing, result, adjType );

        return result;
    }

    //----------------------------------------------------------------------------
    //create adjacencies, only works when the dimension requested is lower than
    //dimension of the range of entities
    smoab::Range createAdjacencies( const smoab::Range& range,
                                    int dimension,
                                    const smoab::adjacency_type type = smoab::UNION ) const
    {
        //the smoab and moab adjacent intersection enums are in the same order
        const int adjType = static_cast< int >( type );
        smoab::Range result;
        const bool create_if_missing = true;
        this->Moab->get_adjacencies( range, dimension, create_if_missing, result, adjType );

        return result;
    }

    //----------------------------------------------------------------------------
    int numChildMeshSets( const smoab::EntityHandle& root ) const
    {
        int numChildren;
        this->Moab->num_child_meshsets( root, &numChildren );
        return numChildren;
    }

    //----------------------------------------------------------------------------
    smoab::Range getChildSets( const smoab::EntityHandle& root ) const
    {
        smoab::Range children;
        this->Moab->get_child_meshsets( root, children, 0 );
        return children;
    }

    //----------------------------------------------------------------------------
    //remove a collection of entities from the database
    void remove( smoab::Range const& toDelete ) const
    {
        this->Moab->delete_entities( toDelete );
    }

    //----------------------------------------------------------------------------
    //a entityHandle with value zero means no side element was found
    smoab::EntityHandle sideElement( smoab::EntityHandle const& cell, int dim, int side ) const
    {
        smoab::EntityHandle result( 0 );
        this->Moab->side_element( cell, dim, side, result );
        return result;
    }

    //----------------------------------------------------------------------------
    //returns all the existing side elements of a cell, elements that
    //are zero mean that side element doesn't exist
    std::vector< smoab::EntityHandle > sideElements( smoab::EntityHandle const& cell, int dim ) const
    {
        const EntityType volumeCellType = this->Moab->type_from_handle( cell );
        const int numSides              = static_cast< int >( moab::CN::NumSubEntities( volumeCellType, dim ) );

        std::vector< smoab::EntityHandle > result( numSides );
        for( int side = 0; side < numSides; ++side )
        {
            smoab::EntityHandle* sideElem = &result[side];  //get memory of vector
            this->Moab->side_element( cell, dim, side, *sideElem );
        }
        return result;
    }

    //----------------------------------------------------------------------------
    //prints all elements in a range objects
    void printRange( const smoab::Range& range ) const
    {
        typedef Range::const_iterator iterator;
        for( iterator i = range.begin(); i != range.end(); ++i )
        {
            std::cout << "entity id: " << *i << std::endl;
            this->Moab->list_entity( *i );
        }
    }
    friend class smoab::DataSetConverter;
    friend class smoab::detail::LoadGeometry;
    friend class smoab::detail::LoadPoly;

  private:
    moab::Interface* Moab;
};

//----------------------------------------------------------------------------
void RangeToVector( const smoab::Range& range, std::vector< smoab::EntityHandle >& vector )
{
    vector.reserve( range.size() );
    std::copy( range.begin(), range.end(), std::back_inserter( vector ) );
}

}  // namespace smoab

#endif