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

/**\class moab::SplitVertices
 *\brief A dictionary of new vertices.
 *
 * An array of existing vertex ids used as a key in a dictionary of new vertices.
 */
#ifndef MOAB_SPLIT_VERTICES_HPP
#define MOAB_SPLIT_VERTICES_HPP

#include "moab/Types.hpp"
#include "ProcessSet.hpp"
#include "MBTagConventions.hpp"

#include <map>
#include <vector>
#include <algorithm>

#undef MB_DEBUG

namespace moab
{

class RefinerTagManager;

template < int _n >
class SplitVertexIndex
{
  public:
    SplitVertexIndex() {}
    SplitVertexIndex( const int* src )
    {
        for( int i = 0; i < _n; ++i )
            this->ids[i] = src[i];
        std::sort( this->ids, this->ids + _n );
    }
    SplitVertexIndex( const SplitVertexIndex< _n >& src )
    {
        for( int i = 0; i < _n; ++i )
            this->ids[i] = src.ids[i];
        this->process_set = src.process_set;
    }
    SplitVertexIndex& operator=( const SplitVertexIndex< _n >& src )
    {
        for( int i = 0; i < _n; ++i )
            this->ids[i] = src.ids[i];
        this->process_set = src.process_set;
        return *this;
    }

    void set_common_processes( const ProcessSet& procs )
    {
        this->process_set = procs;
    }
    ProcessSet& common_processes()
    {
        return this->process_set;
    }
    const ProcessSet& common_processes() const
    {
        return this->process_set;
    }

    bool operator<( const SplitVertexIndex< _n >& other ) const
    {
        // Ignore the process set. Only program errors lead to mismatched process sets with
        // identical ids.
        for( int i = 0; i < _n; ++i )
            if( this->ids[i] < other.ids[i] )
                return true;
            else if( this->ids[i] > other.ids[i] )
                return false;
        return false;
    }

    int ids[_n + 1];
    ProcessSet process_set;
};

template < int _n >
std::ostream& operator<<( std::ostream& os, const SplitVertexIndex< _n >& idx )
{
    for( int i = 0; i < _n; ++i )
    {
        os << idx.ids[i] << " ";
    }
    os << "(" << idx.process_set << ")";
    return os;
}

class EntitySourceRecord
{
  public:
    EntitySourceRecord() {}
    EntitySourceRecord( int nc, EntityHandle ent, const ProcessSet& procs )
    {
        this->ids.resize( nc );
        this->handle      = ent;
        this->process_set = procs;
    }
    EntitySourceRecord( const EntitySourceRecord& src )
    {
        this->handle      = src.handle;
        this->process_set = src.process_set;
        this->ids         = src.ids;
    }
    EntitySourceRecord& operator=( const EntitySourceRecord& src )
    {
        this->handle      = src.handle;
        this->process_set = src.process_set;
        this->ids         = src.ids;
        return *this;
    }

    void set_common_processes( const ProcessSet& procs )
    {
        this->process_set = procs;
    }
    ProcessSet& common_processes()
    {
        return this->process_set;
    }
    const ProcessSet& common_processes() const
    {
        return this->process_set;
    }

    bool operator<( const EntitySourceRecord& other ) const
    {
        // assert( this->ids.size() == other.ids.size() );
        std::vector< int >::size_type N = this->ids.size();
        std::vector< int >::size_type i;
        // Ignore the process set. Only program errors lead to mismatched process sets with
        // identical ids.
        for( i = 0; i < N; ++i )
            if( this->ids[i] < other.ids[i] )
                return true;
            else if( this->ids[i] > other.ids[i] )
                return false;
        return false;
    }

    std::vector< int > ids;
    ProcessSet process_set;
    EntityHandle handle;
};

/** A non-templated base class that the SplitVertices template subclasses all share.
 *
 * All methods that need to be accessed by other classes should be
 * declared by the base class so that no knowledge of template parameters
 * is required.
 */
class SplitVerticesBase
{
  public:
    SplitVerticesBase( RefinerTagManager* tag_mgr );
    virtual ~SplitVerticesBase();

    virtual bool find_or_create( const EntityHandle* split_src,<--- Virtual function in base class<--- Virtual function in base class
                                 const double* coords,
                                 EntityHandle& vert_handle,
                                 std::map< ProcessSet, int >& proc_partition_counts,
                                 bool handles_on_output_mesh ) = 0;

    virtual void assign_global_ids( std::map< ProcessSet, int >& gids ) = 0;<--- Virtual function in base class<--- Virtual function in base class

    Interface* mesh_out;  // Output mesh. Needed for new vertex set in vert_handle
    RefinerTagManager* tag_manager;
    std::vector< int > split_gids;   // Used to hold global IDs of split vertices
    ProcessSet common_shared_procs;  // Holds intersection of several shared_procs_ins.
};

/** A vector of pre-existing entities to a new mesh entity.
 *
 * This is used as a dictionary to determine whether a new vertex should be
 * created on the given n-simplex (n being the template parameter) or whether
 * it has already been created as part of the refinement of a neighboring entity.
 */
class EntitySource : public std::vector< EntitySourceRecord >
{
  public:
    typedef std::vector< EntitySourceRecord > VecType;
    typedef std::vector< EntitySourceRecord >::iterator VecIteratorType;

    EntitySource( int num_corners, RefinerTagManager* tag_mgr );
    ~EntitySource();
    bool create_element( EntityType etyp,
                         int nconn,
                         const EntityHandle* split_src,
                         EntityHandle& elem_handle,
                         std::map< ProcessSet, int >& proc_partition_counts );

    void assign_global_ids( std::map< ProcessSet, int >& gids );

    Interface* mesh_out;  // Output mesh. Needed for new vertex set in vert_handle
    RefinerTagManager* tag_manager;
    ProcessSet common_shared_procs;  // Holds intersection of several shared_procs_ins.
    int num_corners;
};

/** A map from a set of pre-existing vertices to a new mesh vertex.
 *
 * This is used as a dictionary to determine whether a new vertex should be
 * created on the given n-simplex (n being the template parameter) or whether
 * it has already been created as part of the refinement of a neighboring entity.
 */
template < int _n >
class SplitVertices : public std::map< SplitVertexIndex< _n >, EntityHandle >, public SplitVerticesBase
{
  public:
    typedef std::map< SplitVertexIndex< _n >, EntityHandle > MapType;
    typedef typename std::map< SplitVertexIndex< _n >, EntityHandle >::iterator MapIteratorType;

    SplitVertices( RefinerTagManager* tag_mgr );
    virtual ~SplitVertices();
    virtual bool find_or_create( const EntityHandle* split_src,<--- Function in derived class<--- Function in derived class
                                 const double* coords,
                                 EntityHandle& vert_handle,
                                 std::map< ProcessSet, int >& proc_partition_counts,
                                 bool handles_on_output_mesh );

    virtual void assign_global_ids( std::map< ProcessSet, int >& gids );<--- Function in derived class<--- Function in derived class
};

// ------------------------- Template member definitions ----------------------
template < int _n >
SplitVertices< _n >::SplitVertices( RefinerTagManager* tag_mgr ) : SplitVerticesBase( tag_mgr )
{
    this->split_gids.resize( _n );
}

template < int _n >
SplitVertices< _n >::~SplitVertices()
{
}

template < int _n >
bool SplitVertices< _n >::find_or_create( const EntityHandle* split_src,
                                          const double* coords,
                                          EntityHandle& vert_handle,
                                          std::map< ProcessSet, int >& proc_partition_counts,
                                          bool handles_on_output_mesh )
{
    // Get the global IDs of the input vertices
    if( handles_on_output_mesh )
    {
        this->tag_manager->get_output_gids( _n, split_src, this->split_gids );
    }
    else
    {
        this->tag_manager->get_input_gids( _n, split_src, this->split_gids );
    }
    SplitVertexIndex< _n > key( &this->split_gids[0] );
    MapIteratorType it = this->find( key );
    if( it == this->end() )
    {
#ifdef MB_DEBUG
        std::cout << " wrt output: " << handles_on_output_mesh << " ";
#endif  // MB_DEBUG
        this->tag_manager->get_common_processes( _n, split_src, this->common_shared_procs, handles_on_output_mesh );
        proc_partition_counts[this->common_shared_procs]++;
        key.set_common_processes( this->common_shared_procs );
        if( this->mesh_out->create_vertex( coords + 3, vert_handle ) != MB_SUCCESS )
        {
            return false;
        }
        ( *this )[key] = vert_handle;
        this->tag_manager->set_sharing( vert_handle, this->common_shared_procs );
        return true;
    }
    vert_handle = it->second;
    return false;
}

template < int _n >
void SplitVertices< _n >::assign_global_ids( std::map< ProcessSet, int >& gids )
{
    typename std::map< SplitVertexIndex< _n >, EntityHandle >::iterator it;
    for( it = this->begin(); it != this->end(); ++it )
    {
        int gid = gids[it->first.process_set]++;
        this->tag_manager->set_gid( it->second, gid );
#ifdef MB_DEBUG
        std::cout << "Assigning entity: " << it->first << " GID: " << gid << "\n";
#endif  // MB_DEBUG
    }
}

}  // namespace moab

#endif /* MOAB_SPLIT_VERTICES_HPP */