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
#include "meshkit/CAMALTriAdvance.hpp"
#include "meshkit/MeshScheme.hpp"
#include "meshkit/ModelEnt.hpp"
#include "moab/Interface.hpp"
#include "moab/ReadUtilIface.hpp"
#include "moab/Range.hpp"
#include "meshkit/RegisterMeshOp.hpp"
#include "CAMALSurfEval.hpp"
#include "CAMALSizeEval.hpp"
#include "CMLTriAdvance.hpp"
#include "RefEntity.hpp"

#ifdef PARALLEL
#ifdef HAVE_PARALLEL_MOAB
#include "moab/ParallelComm.hpp"
#endif
#endif

#include <vector>

const bool debug_camaltriadv = false;

namespace MeshKit
{

moab::EntityType CAMALTriAdvance::meshTps[] = {moab::MBVERTEX, moab::MBTRI, moab::MBMAXTYPE};

CAMALTriAdvance::CAMALTriAdvance(MKCore *mk_core, const MEntVector &me_vec)
        : MeshScheme(mk_core, me_vec)
{
}

CAMALTriAdvance::~CAMALTriAdvance()
{
}

void CAMALTriAdvance::setup_this()
{
    // just call setup_boundary, since that's the only constraint we have
  setup_boundary();
}

void CAMALTriAdvance::execute_this()
{
  for (MEntSelection::iterator sit = mentSelection.begin(); sit != mentSelection.end(); sit++) {<--- Prefer prefix ++/-- operators for non-primitive types.
      // make a me, for convenience
    ModelEnt *me = (*sit).first;

      // create a surface evaluator for this modelent, and a size evaluator
    CAMALSurfEval cse(me);

    SizingFunction * sz = mk_core()->sizing_function(me->sizing_function_index());
    if (!sz->variable())
      sz = NULL; // no function variable

    CAMALSizeEval mesize(me->mesh_interval_size(), sz);
      // make sure the size isn't negative
    if (mesize.get_size() == -1) mesize.set_size(1.0);
    
    // assemble bounding mesh
    std::vector<moab::EntityHandle> bdy;
    std::vector<int> group_sizes, bdy_ids;
    me->boundary(0, bdy, NULL, &group_sizes);

      // convert the handles to integers for input to TriAdv
    moab::Range bdy_vrange;
    std::vector<double> coords;
    me->get_indexed_connect_coords(bdy, NULL, NULL, bdy_ids, coords, &bdy_vrange);

    // now construct the CAMAL mesher, and pass it initial conditions
    CMLTriAdvance triadv(&cse, &mesize);
    bool success = triadv.set_boundary_mesh(bdy_vrange.size(), &coords[0], group_sizes.size(), &group_sizes[0], &bdy_ids[0]);
    if (!success)
      ECERRCHK(MK_FAILURE, "Trouble setting boundary mesh.");

      // ok, now generate the mesh
    int num_pts, num_tris;
    success = triadv.generate_mesh(num_pts, num_tris);
    if (!success) {
      if (debug_camaltriadv) print_debug(me, coords, bdy_vrange, group_sizes, bdy_ids);
      ECERRCHK(MK_FAILURE, "Trouble generating tri mesh.");
    }

    moab::Range &new_ents = (*sit).second;
    moab::ErrorCode rval;

      // resize the coords array, then get the coords of the new points
    assert(num_pts >= (int)bdy_vrange.size());
    if (num_pts > (int)bdy_vrange.size()) {
      coords.resize(3*(num_pts-bdy_vrange.size()));
      unsigned int pts_returned = triadv.get_points_buf(coords.size(), &coords[0], bdy_vrange.size());
      if (pts_returned != num_pts-bdy_vrange.size()) 
        ECERRCHK(MK_FAILURE, "Number of new points returned from TriAdv doesn't agree with previous value output.");
    
        // create the new vertices' entities on the face
      rval = mk_core()->moab_instance()->create_vertices(&coords[0], pts_returned, new_ents);
      MBERRCHK(rval, mk_core()->moab_instance());
    }

      // for tris, pre-allocate connectivity
    moab::ReadUtilIface *iface;
    rval = mk_core()-> moab_instance() -> query_interface(iface);
    MBERRCHK(rval, mk_core()->moab_instance());		

      //create the tris, get a direct ptr to connectivity
    moab::EntityHandle starth, *connect;
    rval = iface->get_element_connect(num_tris, 3, moab::MBTRI, 1, starth, connect);
    MBERRCHK(rval, mk_core()->moab_instance());

      // read connectivity directly into that array, as int's
    int *connecti = (int*) connect;
    int tris_returned = triadv.get_tris_buf(3*num_tris, connecti);
    if (tris_returned != num_tris)
      ECERRCHK(MK_FAILURE, "Number of new tris returned from TriAdv doesn't agree with previous value output.");

      // put vertex handles into an indexible array
    std::vector<moab::EntityHandle> bdy_vvec;
    std::copy(bdy_vrange.begin(), bdy_vrange.end(), std::back_inserter(bdy_vvec));
    std::copy(new_ents.begin(), new_ents.end(), std::back_inserter(bdy_vvec));
    
      // now convert vertex indices into handles in-place, working from the back
    for (int i = 3*num_tris-1; i >= 0; i--) {
      assert(connecti[i] >= 0 && connecti[i] < (int)bdy_vvec.size());
      connect[i] = bdy_vvec[connecti[i]];
    }
    
      // put new tris into new entity range, then commit the mesh
    new_ents.insert(starth, starth+num_tris-1);
    me->commit_mesh(new_ents, COMPLETE_MESH);
  }
}

void CAMALTriAdvance::print_debug(ModelEnt *me, std::vector<double> &coords,
                                  moab::Range &bdy_vrange, std::vector<int> &group_sizes,
                                  std::vector<int> &bdy_ids)
{
  std::cout << "Surface_bounadry_mesh: mesh_size = "
            << me->mesh_interval_size() << std::endl;
  
  for (int i = 0; i < (int) bdy_vrange.size(); i++) {
    std::cout << coords[3 * i] << "  " << coords[3 * i + 1]
              << "  " << coords[3 * i + 2] << std::endl;
  }
  
  std::cout << "bdy_vertex_size:" << bdy_vrange.size()
            << ", group_size:" << group_sizes.size() << std::endl;
  
  int index = 0;
  for (int i = 0; i < (int) group_sizes.size(); i++) {
    int g_size = group_sizes[i];
    std::cout << "boundary_order_group" << i + 1 << ", group_size="
              << g_size << std::endl;
    for (int j = 0; j < g_size; j++) {
          std::cout << bdy_ids[index + j] << " ";
    }
    std::cout << std::endl;
    index += g_size;
  }
  
  moab::ErrorCode rval;
  moab::EntityHandle outset;
  std::string outfile;
  std::stringstream ss;
  
  RefEntity* entity = reinterpret_cast<RefEntity*> (me->geom_handle());
  ss << "CAMALTri_boundary_surf";
  ss << entity->id();
  ss << "_";
#ifdef PARALLEL
#ifdef HAVE_PARALLEL_MOAB
  moab::ParallelComm* pcomm = moab::ParallelComm::get_pcomm(mk_core()->moab_instance(), 0);
  ss << "proc";
  ss << pcomm->proc_config().proc_rank();
#endif
#endif
  ss >> outfile;
  outfile += ".vtk";
  rval = mk_core()->moab_instance()->create_meshset(0, outset);
  MBERRCHK(rval, mk_core()->moab_instance());
  rval = mk_core()->moab_instance()->add_entities(outset, bdy_vrange);
  MBERRCHK(rval, mk_core()->moab_instance());
  rval = mk_core()->moab_instance()->write_mesh(outfile.c_str(), &outset, 1);
  MBERRCHK(rval, mk_core()->moab_instance());
  std::cout << outfile.c_str() << " is saved." << std::endl;
}
} // namespace MeshKit