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
/**
 * MOAB, a Mesh-Oriented datABase, is a software component for creating,
 * storing and accessing finite element mesh data.
 *
 * Copyright 2004 Sandia Corporation.  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.
 *
 */

/*
 * Tim's Quick 'N Dirty Cub File Reader (Tqdcfr)
 *
 */

#ifndef TQDCFR
#define TQDCFR

#include "moab/Forward.hpp"
#include "moab/ReaderIface.hpp"
#include "MBTagConventions.hpp"
#include "moab/Range.hpp"

#include <cstdio>
#include <string>
#include <vector>
#include <map>

namespace moab
{

class ReadUtilIface;
class FEModelHeader;
class GeomHeader;
class GroupHeader;
class BlockHeader;
class NodesetHeader;
class SidesetHeader;

class Tqdcfr : public ReaderIface
{
  public:
    void FSEEK( unsigned offset );                           // set cubFile offset to specified value
    void FREADI( unsigned num_ents );                        // read integers into uint_buf
    void FREADD( unsigned num_ents );                        // read doubles into dbl_buf
    void FREADC( unsigned num_ents );                        // read characters into char_buf
    void FREADIA( unsigned num_ents, unsigned int* array );  // read integers
    void FREADDA( unsigned num_ents, double* array );        // read doubles
    void FREADCA( unsigned num_ents, char* arrat );          // read bytes
    void CONVERT_TO_INTS( unsigned int num_ents );           // convert uint_buf to int_buf in-place

    // class for holding the file table of contents
    class FileTOC
    {
      public:
        unsigned int fileEndian, fileSchema, numModels, modelTableOffset, modelMetaDataOffset, activeFEModel;

        FileTOC();
        void print();
    };

    //
    class FEModelHeader
    {
      public:
        unsigned int feEndian, feSchema, feCompressFlag, feLength;

        class ArrayInfo
        {
          public:
            unsigned numEntities, tableOffset, metaDataOffset;

            ArrayInfo();

            void print();
            void init( const std::vector< unsigned int >& uint_buf_in );
        };

        ArrayInfo geomArray, nodeArray, elementArray, groupArray, blockArray, nodesetArray, sidesetArray;

        void init( const unsigned int offset, Tqdcfr* instance );

        void print();
    };

    class MetaDataContainer
    {
      public:
        unsigned int mdSchema, compressFlag;

        class MetaDataEntry
        {
          public:
            unsigned int mdOwner, mdDataType, mdIntValue;
            std::string mdName, mdStringValue;
            std::vector< unsigned int > mdIntArrayValue;
            double mdDblValue;
            std::vector< double > mdDblArrayValue;

            MetaDataEntry();

            void print();
        };

        void print();

        int get_md_entry( const unsigned int owner, const std::string& name );

        std::vector< MetaDataEntry > metadataEntries;
        MetaDataContainer();
    };

    class GeomHeader
    {
      public:
        unsigned int geomID, nodeCt, nodeOffset, elemCt, elemOffset, elemTypeCt, elemLength;

        int maxDim;

        EntityHandle setHandle;

        void print();

        static ErrorCode read_info_header( const unsigned int model_offset,
                                           const FEModelHeader::ArrayInfo& info,
                                           Tqdcfr* instance,
                                           GeomHeader*& entity_headers );

        GeomHeader();
    };

    class GroupHeader
    {
      public:
        unsigned int grpID, grpType, memCt, memOffset, memTypeCt, grpLength;

        EntityHandle setHandle;

        void print();

        static ErrorCode read_info_header( const unsigned int model_offset,
                                           const FEModelHeader::ArrayInfo& info,
                                           Tqdcfr* instance,
                                           GroupHeader*& entity_headers );

        GroupHeader();
    };

    class BlockHeader
    {
      public:
        unsigned int blockID, blockElemType, memCt, memOffset, memTypeCt, attribOrder, blockCol, blockMixElemType,
            blockPyrType, blockMat, blockLength, blockDim;

        EntityHandle setHandle;

        EntityType blockEntityType;

        int hasMidNodes[4];

        void print();

        static ErrorCode read_info_header( const double data_version,
                                           const unsigned int model_offset,
                                           const FEModelHeader::ArrayInfo& info,
                                           Tqdcfr* instance,
                                           BlockHeader*& block_headers );

        BlockHeader();
    };

    class NodesetHeader
    {
      public:
        unsigned int nsID, memCt, memOffset, memTypeCt, pointSym, nsCol, nsLength;

        EntityHandle setHandle;

        void print();

        static ErrorCode read_info_header( const unsigned int model_offset,
                                           const FEModelHeader::ArrayInfo& info,
                                           Tqdcfr* instance,
                                           NodesetHeader*& entity_headers );

        NodesetHeader();
    };

    class SidesetHeader
    {
      public:
        unsigned int ssID, memCt, memOffset, memTypeCt, numDF, ssCol, useShell, ssLength;

        EntityHandle setHandle;

        void print();

        static ErrorCode read_info_header( const unsigned int model_offset,
                                           const FEModelHeader::ArrayInfo& info,
                                           Tqdcfr* instance,
                                           SidesetHeader*& entity_headers );

        SidesetHeader();
    };

    // class to hold model entry data for various kinds of models
    // (acis, free mesh, etc.)
    class ModelEntry
    {
      public:
        ModelEntry();

        ~ModelEntry();

        unsigned int modelHandle, modelOffset, modelLength, modelType, modelOwner, modelPad;

        FEModelHeader feModelHeader;
        GeomHeader* feGeomH;
        GroupHeader* feGroupH;
        BlockHeader* feBlockH;
        NodesetHeader* feNodeSetH;
        SidesetHeader* feSideSetH;

        MetaDataContainer geomMD, nodeMD, elementMD, groupMD, blockMD, nodesetMD, sidesetMD;

        void print();

        void print_geom_headers( const char* prefix, GeomHeader* header, unsigned int num_headers );

        void print_group_headers( const char* prefix, GroupHeader* header, const unsigned int num_headers );

        void print_block_headers( const char* prefix, BlockHeader* header, const unsigned int num_headers );

        void print_nodeset_headers( const char* prefix, NodesetHeader* header, const unsigned int num_headers );

        void print_sideset_headers( const char* prefix, SidesetHeader* header, const unsigned int num_headers );

        ErrorCode read_header_info( Tqdcfr* instance, const double data_version );
        ErrorCode read_metadata_info( Tqdcfr* tqd );
    };

    enum
    {
        aBODY,
        LUMP,
        SHELL,
        FACE,
        LOOP,
        COEDGE,
        aEDGE,
        aVERTEX,
        ATTRIB,
        UNKNOWN
    };

    struct AcisRecord
    {
        unsigned int rec_type;
        std::string att_string;
        bool processed;
        int first_attrib;
        int att_prev, att_next, att_ent_num;
        EntityHandle entity;
    };

    ~Tqdcfr();

    ReadUtilIface* readUtilIface;
    Interface* mdbImpl;
    FILE* cubFile;
    FileTOC fileTOC;
    std::vector< ModelEntry > modelEntries;
    MetaDataContainer modelMetaData;
    long currVHandleOffset;
    Range beforeEnts;
    long currElementIdOffset[MBMAXTYPE];
    Tag globalIdTag, cubIdTag, geomTag, uniqueIdTag, blockTag, nsTag, ssTag, attribVectorTag, entityNameTag,
        categoryTag, hasMidNodesTag;
    std::map< int, EntityHandle > uidSetMap;
    std::map< int, EntityHandle > gidSetMap[6];
    bool swapForEndianness;

    std::vector< unsigned int > uint_buf;
    int* int_buf;
    std::vector< double > dbl_buf;
    std::vector< char > char_buf;

    static ReaderIface* factory( Interface* );

    // read cub file
    ErrorCode load_file( const char* file_name,
                         const EntityHandle* file_set,
                         const FileOptions& opts,
                         const SubsetList* subset_list = 0,
                         const Tag* file_id_tag        = 0 );

    ErrorCode read_tag_values( const char* file_name,
                               const char* tag_name,
                               const FileOptions& opts,
                               std::vector< int >& tag_values_out,
                               const SubsetList* subset_list = 0 );

    ErrorCode read_nodeset( const unsigned int nsindex, ModelEntry* model, NodesetHeader* nodeseth );
    ErrorCode read_sideset( const unsigned int ssindex,
                            const double data_version,
                            ModelEntry* model,
                            SidesetHeader* sideseth );
    ErrorCode read_block( const unsigned int blindex,
                          const double data_version,
                          ModelEntry* model,
                          BlockHeader* blockh );
    ErrorCode read_group( const unsigned int gr_index, ModelEntry* model, GroupHeader* grouph );
    ErrorCode read_nodes( const unsigned int gindex, ModelEntry* model, GeomHeader* entity );
    ErrorCode read_elements( ModelEntry* model, GeomHeader* entity );
    ErrorCode read_file_header();
    ErrorCode read_model_entries();
    int find_model( const unsigned int model_type );
    ErrorCode read_meta_data( const unsigned int metadata_offset, MetaDataContainer& mc );
    ErrorCode read_md_string( std::string& name );

    enum
    {
        mesh,
        acist,
        acisb,
        facet,
        exodusmesh
    };
    EntityType type_from_cub_type( const unsigned int cub_type, const unsigned int nodes_per_elem );
    void check_contiguous( const unsigned int num_ents, int& contig, unsigned int& min_id, unsigned int& max_id );

    Tqdcfr( Interface* impl );

    ErrorCode create_set( EntityHandle& h, unsigned int flags = MESHSET_SET );

  private:
    EntityHandle mFileSet;  // set containing read entities.

    bool printedSeqWarning;  // only print acis sequence #'s warning once

    bool printedElemWarning;  // only print element #'s warning once

    ErrorCode convert_nodesets_sidesets();

    ErrorCode read_acis_records( const char* sat_file_name = 0 );

    ErrorCode parse_acis_attribs( const unsigned int entity_rec_num, std::vector< AcisRecord >& records );
    ErrorCode interpret_acis_records( std::vector< AcisRecord >& records );

    ErrorCode reset_record( AcisRecord& this_record );

    ErrorCode process_record( AcisRecord& this_record );

    static const char geom_categories[][CATEGORY_TAG_SIZE];

    FILE* acisDumpFile;

    // map between cub ids and MOAB handles
    std::vector< EntityHandle >* cubMOABVertexMap;

    // enum used to identify element/entity type in groups
    enum
    {
        GROUP = 0,
        BODY,
        VOLUME,
        SURFACE,
        CURVE,
        VERTEX,
        HEX,
        TET,
        PYRAMID,
        QUAD,
        TRI,
        EDGE,
        NODE
    };
    static const EntityType group_type_to_mb_type[];

    enum
    {
        SPHERE_EXO = 0,
        BAR,
        BAR2,
        BAR3,
        BEAM,
        BEAM2,
        BEAM3,
        TRUSS,
        TRUSS2,
        TRUSS3,
        SPRING,
        TRIthree,
        TRI3,
        TRI6,
        TRI7,
        TRISHELL,
        TRISHELL3,
        TRISHELL6,
        TRISHELL7,
        SHEL,
        SHELL4,
        SHELL8,
        SHELL9,
        QUADfour,
        QUAD4,
        QUAD5,
        QUAD8,
        QUAD9,
        TETRAfour,
        TETRA4,
        TETRA8,
        TETRA10,
        TETRA14,
        PYRAMIDfive,
        PYRAMID5,
        PYRAMID8,
        PYRAMID13,
        PYRAMID18,
        HEXeight,
        HEX8,
        HEX9,
        HEX20,
        HEX27,
        HEXSHELL,
        INVALID_ELEMENT_TYPE
    };
    static const EntityType block_type_to_mb_type[];
    static const int cub_elem_num_verts[];
    static const int cub_elem_num_verts_len;

    //! mapping from mesh packet type to moab type
    static const EntityType mp_type_to_mb_type[];

    //! get entities with individually-specified types; if is_group is false,
    //! increment each mem_type by 2 since they're CSOEntityType's and not group types
    ErrorCode get_entities( const unsigned int* mem_types,
                            int* id_buf,
                            const unsigned int id_buf_size,
                            const bool is_group,
                            std::vector< EntityHandle >& entities );

    //! get entities specified by type and ids, append to entities
    ErrorCode get_entities( const unsigned int this_type,
                            int* id_buf,
                            const unsigned int id_buf_size,
                            std::vector< EntityHandle >& entities,
                            std::vector< EntityHandle >& excl_entities );

    //! get ref entity sets with specified type and ids
    ErrorCode get_ref_entities( const unsigned int this_type,
                                int* id_buf,
                                const unsigned id_buf_size,
                                std::vector< EntityHandle >& entities );

    //! get mesh entities with specified type and ids
    ErrorCode get_mesh_entities( const unsigned int this_type,
                                 int* id_buf,
                                 const unsigned id_buf_size,
                                 std::vector< EntityHandle >& entities,
                                 std::vector< EntityHandle >& excl_entities );

    //! process entities in a sideset according to sense flags stored in uint_buf
    //! or char_buf (depending on sense_size)
    ErrorCode process_sideset_10( const int this_type,
                                  const int num_ents,
                                  const int sense_size,
                                  std::vector< EntityHandle >& ss_entities,
                                  Tqdcfr::SidesetHeader* sideseth );

    ErrorCode process_sideset_11( std::vector< EntityHandle >& ss_entities,
                                  int num_wrts,
                                  Tqdcfr::SidesetHeader* sideseth );

    // put entities into the specfied set, and excluded entities into a
    // std::vector pointed to by the "Exclude_Entities" tag on that set
    ErrorCode put_into_set( EntityHandle set_handle,
                            std::vector< EntityHandle >& entities,
                            std::vector< EntityHandle >& excl_entities );

    // look in metadatacontainer[set_index] for name data; if found, set name (and extra names,
    // if multiple found) on set handle
    ErrorCode get_names( MetaDataContainer& md, unsigned int set_index, EntityHandle seth );
};

}  // namespace moab

#endif