Mesh Oriented datABase  (version 5.6.0)
An array-based unstructured mesh library
DeformMeshRemap.cpp
Go to the documentation of this file.
1 /** @example DeformMeshRemap.cpp
2  * Description: Account for mesh deformation of a solid due to structural mechanics\n
3  * In this example there are two meshes, a "master" and "slave" mesh. In the master mesh,
4  * the solid material is deformed, to mimic what happens when a solid heats up and deforms.
5  * The fluid mesh is smoothed to account for those deformations, and tags on the fluid are
6  * remapped to those new positions. Then mesh positions and state variables are transferred
7  * to the slave mesh, mimicing another mesh used by some other physics.
8  *
9  * To run: ./DeformMeshRemap [\c master_meshfile \c slave_meshfile]\n
10  * (default values can run if users don't specify the mesh files)
11  */
12 
13 #include "moab/Core.hpp"
14 #include "moab/Range.hpp"
15 #include "moab/Skinner.hpp"
16 #include "moab/LloydSmoother.hpp"
17 #include "moab/ProgOptions.hpp"
18 #include "moab/BoundBox.hpp"
19 #include "moab/SpatialLocator.hpp"
20 #include "MBTagConventions.hpp"
21 #include "DataCoupler.hpp"
22 
23 #ifdef USE_MPI
24 #include "moab/ParallelComm.hpp"
25 #endif
26 
27 #include <iostream>
28 #include <set>
29 #include <sstream>
30 #include <cassert>
31 
32 using namespace moab;
33 using namespace std;
34 
35 #ifndef MESH_DIR
36 #define MESH_DIR "."
37 #endif
38 
39 ErrorCode read_file( string& fname,
40  EntityHandle& seth,
41  Range& solids,
42  Range& solid_elems,
43  Range& fluids,
44  Range& fluid_elems );
45 void deform_func( const BoundBox& bbox, double* xold, double* xnew );
46 ErrorCode deform_master( Range& fluid_elems, Range& solid_elems, Tag& xnew );
47 ErrorCode smooth_master( int dim, Tag xnew, EntityHandle& master, Range& fluids );
49 
50 const int SOLID_SETNO = 100, FLUID_SETNO = 200;
51 
53 
54 const bool debug = true;
55 
57 {
58  public:
59  //! Enumerator for solid/fluid, master/slave
60  enum
61  {
62  MASTER = 0,
65  FLUID
66  };
67 
68  //! Constructor
69  //! If master is NULL, the MOAB part is run in serial;
70  //! If slave is NULL but the master isn't, the slave is copied from the master
71  //! Create communicators using moab::ParallelComm::get_pcomm
72  DeformMeshRemap( Interface* impl, ParallelComm* master = NULL, ParallelComm* slave = NULL );
73 
74  //! Destructor
75  ~DeformMeshRemap();
76 
77  //! Execute the deformed mesh process
78  ErrorCode execute();
79 
80  //! Add a set number
81  ErrorCode add_set_no( int m_or_s, int fluid_or_solid, int set_no );
82 
83  //! Remove a set number
84  ErrorCode remove_set_no( int m_or_s, int fluid_or_solid, int set_no );
85 
86  //! Get the set numbers
87  ErrorCode get_set_nos( int m_or_s, int fluid_or_solid, set< int >& set_nos ) const;
88 
89  //! Get the xNew tag handle
90  inline Tag x_new() const
91  {
92  return xNew;
93  }
94 
95  //! Get the tag name
96  string x_new_name() const
97  {
98  return xNewName;
99  }
100 
101  //! Set the tag name
102  void x_new_name( const string& name )
103  {
104  xNewName = name;
105  }
106 
107  //! Get/set the file name
108  string get_file_name( int m_or_s ) const;
109 
110  //! Get/set the file name
111  void set_file_name( int m_or_s, const string& name );
112 
113  //! Get/set the x displacement tag names
114  string xdisp_name( int idx = 0 );
115  void xdisp_name( const string& nm, int idx = 0 );
116 
117  private:
118  //! Apply a known deformation to the solid elements, putting the results in the xNew tag; also
119  //! write current coordinates to the xNew tag for fluid elements
120  ErrorCode deform_master( Range& fluid_elems, Range& solid_elems, const char* tag_name = NULL );
121 
122  //! Read a file and establish proper ranges
123  ErrorCode read_file( int m_or_s, string& fname, EntityHandle& seth );
124 
125  //! Write the input tag to the coordinates for the vertices in the input elems
126  //! If non-zero tmp_tag is input, save coords to tmp_tag before over-writing with tag value
127  ErrorCode write_to_coords( Range& elems, Tag tagh, Tag tmp_tag = 0 );
128 
129  //! Write the tag to the vertices, then save to the specified file
130  //! If restore_coords is true, coords are restored to their initial state after file is written
131  ErrorCode write_and_save( Range& ents,
132  EntityHandle seth,
133  Tag tagh,
134  const char* filename,
135  bool restore_coords = false );
136 
137  //! Find fluid/solid sets from complement of solid/fluid sets
138  ErrorCode find_other_sets( int m_or_s, EntityHandle file_set );
139 
140  //! moab interface
142 
143 #ifdef USE_MPI
144  //! ParallelComm for master, slave meshes
145  ParallelComm *pcMaster, *pcSlave;
146 #endif
147 
148  //! Material set numbers for fluid materials, for master/slave
149  set< int > fluidSetNos[2];
150 
151  //! Material set numbers for solid materials, for master/slave
152  set< int > solidSetNos[2];
153 
154  //! Sets defining master/slave meshes
156 
157  //! Sets in master/slave meshes
158  Range fluidSets[2], solidSets[2];
159 
160  //! Elements in master/slave meshes
161  Range fluidElems[2], solidElems[2];
162 
163  //! Filenames for master/slave meshes
164  string masterFileName, slaveFileName;
165 
166  //! Tag from file, might be 3
167  Tag xDisp[3];
168 
169  //! Tag used for new positions
171 
172  //! Tag name used to read disps from file
173  string xDispNames[3];
174 
175  //! Tag name used for new positions
176  string xNewName;
177 };
178 
179 //! Add a set number
180 inline ErrorCode DeformMeshRemap::add_set_no( int m_or_s, int f_or_s, int set_no )
181 {
182  set< int >* this_set;
183  assert( ( m_or_s == MASTER || m_or_s == SLAVE ) && "m_or_s should be MASTER or SLAVE." );
184  if( m_or_s != MASTER && m_or_s != SLAVE ) return MB_INDEX_OUT_OF_RANGE;
185 
186  switch( f_or_s )
187  {
188  case FLUID:
189  this_set = &fluidSetNos[m_or_s];
190  break;
191  case SOLID:
192  this_set = &solidSetNos[m_or_s];
193  break;
194  default:
195  assert( false && "f_or_s should be FLUID or SOLID." );
196  return MB_FAILURE;
197  }
198 
199  this_set->insert( set_no );
200 
201  return MB_SUCCESS;
202 }
203 
204 //! Remove a set number
205 inline ErrorCode DeformMeshRemap::remove_set_no( int m_or_s, int f_or_s, int set_no )
206 {
207  set< int >* this_set;
208  assert( ( m_or_s == MASTER || m_or_s == SLAVE ) && "m_or_s should be MASTER or SLAVE." );
209  if( m_or_s != MASTER && m_or_s != SLAVE ) return MB_INDEX_OUT_OF_RANGE;
210  switch( f_or_s )
211  {
212  case FLUID:
213  this_set = &fluidSetNos[m_or_s];
214  break;
215  case SOLID:
216  this_set = &solidSetNos[m_or_s];
217  break;
218  default:
219  assert( false && "f_or_s should be FLUID or SOLID." );
220  return MB_FAILURE;
221  }
222  set< int >::iterator sit = this_set->find( set_no );
223  if( sit != this_set->end() )
224  {
225  this_set->erase( *sit );
226  return MB_SUCCESS;
227  }
228 
229  return MB_FAILURE;
230 }
231 
232 //! Get the set numbers
233 inline ErrorCode DeformMeshRemap::get_set_nos( int m_or_s, int f_or_s, set< int >& set_nos ) const
234 {
235  const set< int >* this_set;
236  assert( ( m_or_s == MASTER || m_or_s == SLAVE ) && "m_or_s should be MASTER or SLAVE." );
237  if( m_or_s != MASTER && m_or_s != SLAVE ) return MB_INDEX_OUT_OF_RANGE;
238  switch( f_or_s )
239  {
240  case FLUID:
241  this_set = &fluidSetNos[m_or_s];
242  break;
243  case SOLID:
244  this_set = &solidSetNos[m_or_s];
245  break;
246  default:
247  assert( false && "f_or_s should be FLUID or SOLID." );
248  return MB_FAILURE;
249  }
250 
251  set_nos = *this_set;
252 
253  return MB_SUCCESS;
254 }
255 
256 inline string DeformMeshRemap::xdisp_name( int idx )
257 {
258  return xDispNames[idx];
259 }
260 
261 void DeformMeshRemap::xdisp_name( const string& nm, int idx )
262 {
263  xDispNames[idx] = nm;
264 }
265 
267 {
268  // Read master/slave files and get fluid/solid material sets
269  MB_CHK_ERR( read_file( MASTER, masterFileName, masterSet ) );
270 
271  if( solidSetNos[MASTER].empty() || fluidSetNos[MASTER].empty() )
272  {
273  MB_CHK_SET_ERR( find_other_sets( MASTER, masterSet ), "Failed to find other sets in master mesh" );
274  }
275 
276  bool have_slave = !( slaveFileName == "none" );
277  if( have_slave )
278  {
279  MB_CHK_ERR( read_file( SLAVE, slaveFileName, slaveSet ) );
280 
281  if( solidSetNos[SLAVE].empty() || fluidSetNos[SLAVE].empty() )
282  {
283  MB_CHK_SET_ERR( find_other_sets( SLAVE, slaveSet ), "Failed to find other sets in slave mesh" );
284  }
285  }
286 
287  if( debug ) cout << "Constructing data coupler/search tree on master mesh..." << endl;
288 
289  Range src_elems = solidElems[MASTER];
290  src_elems.merge( fluidElems[MASTER] );
291 
292  // Initialize data coupler on source elements
293  DataCoupler dc_master( mbImpl, src_elems, 0, NULL );
294 
295  Range tgt_verts;
296  if( have_slave )
297  {
298  // Locate slave vertices in master, orig coords; do this with a data coupler, so you can
299  // later interpolate
300  Range tmp_range = solidElems[SLAVE];
301  tmp_range.merge( fluidElems[SLAVE] );
302  MB_CHK_SET_ERR( mbImpl->get_adjacencies( tmp_range, 0, false, tgt_verts, Interface::UNION ),
303  "Failed to get target verts" );
304 
305  // Locate slave vertices, caching results in dc
306  if( debug ) cout << "Locating slave vertices in master mesh..." << endl;
307  MB_CHK_SET_ERR( dc_master.locate_points( tgt_verts ), "Point location of tgt verts failed" );
308  int num_located = dc_master.spatial_locator()->local_num_located();
309  if( num_located != (int)tgt_verts.size() )
310  {
311  MB_CHK_ERR( MB_FAILURE );
312  cout << "Only " << num_located << " out of " << tgt_verts.size() << " target points successfully located."
313  << endl;
314  return MB_FAILURE;
315  }
316  }
317 
318  // Deform the master's solid mesh, put results in a new tag
319  if( debug ) cout << "Deforming fluid elements in master mesh..." << endl;
320  MB_CHK_ERR( deform_master( fluidElems[MASTER], solidElems[MASTER], "xnew" ) );
321 
322  { // To isolate the lloyd smoother & delete when done
323  if( debug )
324  {
325  // Output the skin of smoothed elems, as a check
326  // Get the skin; get facets, because we might need to filter on shared entities
327  Skinner skinner( mbImpl );
328  Range skin;
329  MB_CHK_SET_ERR( skinner.find_skin( 0, fluidElems[MASTER], false, skin ), "Unable to find skin" );
330  EntityHandle skin_set;
331  cout << "Writing skin_mesh.g and fluid_mesh.g." << endl;
332  MB_CHK_SET_ERR( mbImpl->create_meshset( MESHSET_SET, skin_set ), "Failed to create skin set" );
333  MB_CHK_SET_ERR( mbImpl->add_entities( skin_set, skin ), "Failed to add skin entities to set" );
334  MB_CHK_SET_ERR( mbImpl->write_file( "skin_mesh.vtk", NULL, NULL, &skin_set, 1 ),
335  "Failure to write skin set" );
336  MB_CHK_SET_ERR( mbImpl->remove_entities( skin_set, skin ), "Failed to remove skin entities from set" );
337  MB_CHK_SET_ERR( mbImpl->add_entities( skin_set, fluidElems[MASTER] ),
338  "Failed to add fluid entities to set" );
339  MB_CHK_SET_ERR( mbImpl->write_file( "fluid_mesh.vtk", NULL, NULL, &skin_set, 1 ),
340  "Failure to write fluid set" );
341  MB_CHK_SET_ERR( mbImpl->delete_entities( &skin_set, 1 ), "Failed to delete skin set" );
342  }
343 
344  // Smooth the master mesh
345  if( debug ) cout << "Smoothing fluid elements in master mesh..." << endl;
346  LloydSmoother ll( mbImpl, NULL, fluidElems[MASTER], xNew );
347  MB_CHK_SET_ERR( ll.perform_smooth(), "Failed in lloyd smoothing" );
348  cout << "Lloyd smoothing required " << ll.num_its() << " iterations." << endl;
349  }
350 
351  // Transfer xNew to coords, for master
352  if( debug ) cout << "Transferring coords tag to vertex coordinates in master mesh..." << endl;
353  MB_CHK_SET_ERR( write_to_coords( solidElems[MASTER], xNew ), "Failed writing tag to master fluid verts" );
354  MB_CHK_SET_ERR( write_to_coords( fluidElems[MASTER], xNew ), "Failed writing tag to master fluid verts" );
355 
356  if( have_slave )
357  {
358  // Map new locations to slave
359  // Interpolate xNew to slave points
360  if( debug ) cout << "Interpolating new coordinates to slave vertices..." << endl;
361  MB_CHK_SET_ERR( dc_master.interpolate( (int)DataCoupler::VOLUME, "xnew" ),
362  "Failed to interpolate target solution" );
363  // Transfer xNew to coords, for slave
364  if( debug ) cout << "Transferring coords tag to vertex coordinates in slave mesh..." << endl;
365  MB_CHK_SET_ERR( write_to_coords( tgt_verts, xNew ), "Failed writing tag to slave verts" );
366  }
367 
368  if( debug )
369  {
370  string str;
371 #ifdef USE_MPI
372  if( pcMaster && pcMaster->size() > 1 ) str = "PARALLEL=WRITE_PART";
373 #endif
374  if( debug ) cout << "Writing smoothed_master.h5m..." << endl;
375  MB_CHK_ERR( mbImpl->write_file( "smoothed_master.h5m", NULL, str.c_str(), &masterSet, 1 ) );
376 
377  if( have_slave )
378  {
379 #ifdef USE_MPI
380  str.clear();
381  if( pcSlave && pcSlave->size() > 1 ) str = "PARALLEL=WRITE_PART";
382 #endif
383  if( debug ) cout << "Writing slave_interp.h5m..." << endl;
384  MB_CHK_ERR( mbImpl->write_file( "slave_interp.h5m", NULL, str.c_str(), &slaveSet, 1 ) );
385  } // if have_slave
386  } // if debug
387 
388  if( debug ) dc_master.spatial_locator()->get_tree()->tree_stats().print();
389 
390  return MB_SUCCESS;
391 }
392 
393 string DeformMeshRemap::get_file_name( int m_or_s ) const
394 {
395  switch( m_or_s )
396  {
397  case MASTER:
398  return masterFileName;
399  case SLAVE:
400  return slaveFileName;
401  default:
402  assert( false && "m_or_s should be MASTER or SLAVE." );
403  return string();
404  }
405 }
406 
407 void DeformMeshRemap::set_file_name( int m_or_s, const string& name )
408 {
409  switch( m_or_s )
410  {
411  case MASTER:
412  masterFileName = name;
413  break;
414  case SLAVE:
415  slaveFileName = name;
416  break;
417  default:
418  assert( false && "m_or_s should be MASTER or SLAVE." );
419  }
420 }
421 
423  : mbImpl( impl ), pcMaster( master ), pcSlave( slave ), masterSet( 0 ), slaveSet( 0 ), xNew( 0 ), xNewName( "xnew" )
424 {
425  xDisp[0] = xDisp[1] = xDisp[2] = 0;
426 
427  if( !pcSlave && pcMaster ) pcSlave = pcMaster;
428 }
429 
431 
432 int main( int argc, char** argv )
433 {
434  ErrorCode rval;
435 
436  ProgOptions po( "Deformed mesh options" );
437  po.addOpt< string >( "master,m", "Specify the master meshfile name" );
438  po.addOpt< string >( "worker,w", "Specify the slave/worker meshfile name, or 'none' (no quotes) if master only" );
439  po.addOpt< string >( "d1,", "Tag name for displacement x or xyz" );
440  po.addOpt< string >( "d2,", "Tag name for displacement y" );
441  po.addOpt< string >( "d3,", "Tag name for displacement z" );
442  po.addOpt< int >( "fm,", "Specify master fluid material set number(s). If none specified, "
443  "fluid sets derived from complement of solid sets." );
444  po.addOpt< int >( "fs,", "Specify master solid material set number(s). If none specified, "
445  "solid sets derived from complement of fluid sets." );
446  po.addOpt< int >( "sm,", "Specify slave fluid material set number(s). If none specified, fluid "
447  "sets derived from complement of solid sets." );
448  po.addOpt< int >( "ss,", "Specify slave solid material set number(s). If none specified, solid "
449  "sets derived from complement of fluid sets." );
450 
451  po.parseCommandLine( argc, argv );
452 
453  mb = new Core();
454 
455  DeformMeshRemap* dfr;
456 #ifdef USE_MPI
457  ParallelComm* pc = new ParallelComm( mb, MPI_COMM_WORLD );
458  dfr = new DeformMeshRemap( mb, pc );
459 #else
460  dfr = new DeformMeshRemap( mb );
461 #endif
462 
463  string masterf, slavef;
464  if( !po.getOpt( "master", &masterf ) ) masterf = string( MESH_DIR ) + string( "/rodquad.g" );
465  dfr->set_file_name( DeformMeshRemap::MASTER, masterf );
466 
467  if( !po.getOpt( "worker", &slavef ) ) slavef = string( MESH_DIR ) + string( "/rodtri.g" );
468  dfr->set_file_name( DeformMeshRemap::SLAVE, slavef );
469  if( slavef.empty() )
470  {
471  cerr << "Empty slave file name; if no slave, use filename 'none' (no quotes)." << endl;
472  return 1;
473  }
474 
475  vector< int > set_nos;
476  po.getOptAllArgs( "fm", set_nos );
477  for( vector< int >::iterator vit = set_nos.begin(); vit != set_nos.end(); ++vit )
479  set_nos.clear();
480 
481  po.getOptAllArgs( "fs", set_nos );
482  for( vector< int >::iterator vit = set_nos.begin(); vit != set_nos.end(); ++vit )
484  set_nos.clear();
485 
486  po.getOptAllArgs( "sm", set_nos );
487  for( vector< int >::iterator vit = set_nos.begin(); vit != set_nos.end(); ++vit )
489 
490  po.getOptAllArgs( "ss", set_nos );
491  for( vector< int >::iterator vit = set_nos.begin(); vit != set_nos.end(); ++vit )
493 
494  string tnames[3];
495  po.getOpt( "d1", &tnames[0] );
496  po.getOpt( "d2", &tnames[1] );
497  po.getOpt( "d3", &tnames[2] );
498  for( int i = 0; i < 3; i++ )
499  if( !tnames[i].empty() ) dfr->xdisp_name( tnames[i], i );
500 
501  rval = dfr->execute();
502 
503  delete dfr;
504  delete mb;
505 
506  return rval;
507 }
508 
510  EntityHandle seth,
511  Tag tagh,
512  const char* filename,
513  bool restore_coords )
514 {
515  Tag tmp_tag = 0;
516  ErrorCode rval;
517  if( restore_coords ) rval = mbImpl->tag_get_handle( "", 3, MB_TYPE_DOUBLE, tmp_tag, MB_TAG_CREAT | MB_TAG_DENSE );
518 
519  MB_CHK_ERR( write_to_coords( ents, tagh, tmp_tag ) );
520  MB_CHK_ERR( mbImpl->write_file( filename, NULL, NULL, &seth, 1 ) );
521 
522  if( restore_coords )
523  {
524  MB_CHK_ERR( write_to_coords( ents, tmp_tag ) );
525  MB_CHK_ERR( mbImpl->tag_delete( tmp_tag ) );
526  }
527 
528  return rval;
529 }
530 
532 {
533  // Write the tag to coordinates
534  Range verts;
535  MB_CHK_SET_ERR( mbImpl->get_adjacencies( elems, 0, false, verts, Interface::UNION ), "Failed to get adj vertices" );
536 
537  // Save current coordinates
538  vector< double > coords( 3 * verts.size() );
539  MB_CHK_SET_ERR( mbImpl->get_coords( verts, &coords[0] ), "Failed to get tmp copy of coords" );
540  MB_CHK_SET_ERR( mbImpl->tag_set_data( tmp_tag, verts, &coords[0] ), "Failed to save tmp copy of coords" );
541 
542  // Get tag data and set as coordinates
543  MB_CHK_SET_ERR( mbImpl->tag_get_data( tagh, verts, &coords[0] ), "Failed to get tag data" );
544  MB_CHK_SET_ERR( mbImpl->set_coords( verts, &coords[0] ), "Failed to set coordinates" );
545  return MB_SUCCESS;
546 }
547 
548 void deform_func( const BoundBox& bbox, double* xold, double* xnew )
549 {
550  /* Deformation function based on max delx and dely at top of rod
551  const double RODWIDTH = 0.2, RODHEIGHT = 0.5;
552  // function: origin is at middle base of rod, and is .5 high
553  // top of rod is (0,.55) on left and (.2,.6) on right
554  double delx = 0.5*RODWIDTH;
555 
556  double xfrac = (xold[0] + .5*RODWIDTH)/RODWIDTH, yfrac = xold[1]/RODHEIGHT;
557  xnew[0] = xold[0] + yfrac * delx;
558  xnew[1] = xold[1] + yfrac * (1.0 + xfrac) * 0.05;
559  */
560  /* Deformation function based on fraction of bounding box dimension in each direction */
561  double frac = 0.01; // taken from approximate relative deformation from LLNL Diablo of XX09 assys
562  CartVect *xo = reinterpret_cast< CartVect* >( xold ), *xn = reinterpret_cast< CartVect* >( xnew );
563  CartVect disp = frac * ( *xo - bbox.bMin );
564  *xn = *xo + disp;
565 }
566 
567 ErrorCode DeformMeshRemap::deform_master( Range& fluid_elems, Range& solid_elems, const char* tag_name )
568 {
569  // Deform elements with an analytic function
570  ErrorCode rval;
571 
572  // Get vertices of solid elements
573  Range solid_verts;
574  MB_CHK_SET_ERR( mbImpl->get_adjacencies( solid_elems, 0, false, solid_verts, Interface::UNION ),
575  "Failed to get vertices" );
576 
577  // Get coordinates of solid vertices
578  vector< double > coords( 3 * solid_verts.size() );
579  MB_CHK_SET_ERR( mbImpl->get_coords( solid_verts, &coords[0] ), "Failed to get vertex coords" );
580  unsigned int num_verts = solid_verts.size();
581 
582  // Get or create the tag
583  if( !xDispNames[0].empty() && !xDispNames[1].empty() && !xDispNames[2].empty() )
584  {
585  // 3 tags, specifying xyz individual data, integrate into one tag
586  MB_CHK_SET_ERR( mbImpl->tag_get_handle( ( tag_name ? tag_name : "" ), 3, MB_TYPE_DOUBLE, xNew,
588  "Failed to create xnew tag" );
589  vector< double > disps( num_verts );
590  for( int i = 0; i < 3; i++ )
591  {
593  "Failed to get xDisp tag" );
594  MB_CHK_SET_ERR( mbImpl->tag_get_data( xDisp[i], solid_verts, &disps[0] ),
595  "Failed to get xDisp tag values" );
596  for( unsigned int j = 0; j < num_verts; j++ )
597  new_coords[3 * j + i] = coords[3 * j + i] + disps[j];
598  }
599  }
600  else if( !xDispNames[0].empty() )
601  {
603  "Failed to get first xDisp tag" );
604  xNew = xDisp[0];
605  vector< double > disps( 3 * num_verts );
606  rval = mbImpl->tag_get_data( xDisp[0], solid_verts, &disps[0] );
607  for( unsigned int j = 0; j < 3 * num_verts; j++ )
608  new_coords[j] = coords[j] + disps[j];
609  }
610  else
611  {
612  // Get the bounding box of the solid mesh
613  BoundBox bbox;
614  bbox.update( *mbImpl, solid_elems );
615 
616  for( unsigned int j = 0; j < num_verts; j++ )
617  deform_func( bbox, &coords[3 * j], &new_coords[3 * j] );
618  }
619 
620  if( debug )
621  {
622  double len = 0.0;
623  for( unsigned int i = 0; i < num_verts; i++ )
624  {
625  CartVect dx = CartVect( &new_coords[3 * i] ) - CartVect( &coords[3 * i] );
626  double tmp_len = dx.length_squared();
627  if( tmp_len > len ) len = tmp_len;
628  }
629  Range tmp_elems( fluid_elems );
630  tmp_elems.merge( solid_elems );
631  BoundBox box;
632  box.update( *mbImpl, tmp_elems );
633  double max_len =
634  std::max( box.bMax[2] - box.bMin[2], std::max( box.bMax[1] - box.bMin[1], box.bMax[0] - box.bMin[0] ) );
635 
636  cout << "Max displacement = " << len << " (" << 100.0 * len / max_len << "% of max box length)" << endl;
637  }
638 
639  if( !xNew )
640  {
641  MB_CHK_SET_ERR( mbImpl->tag_get_handle( ( tag_name ? tag_name : "" ), 3, MB_TYPE_DOUBLE, xDisp[0],
643  "Failed to get xNew tag" );
644  xNew = xDisp[0];
645  }
646 
647  // Set the new tag to those coords
648  MB_CHK_SET_ERR( mbImpl->tag_set_data( xNew, solid_verts, &new_coords[0] ), "Failed to set tag data" );
649 
650  // Get all the vertices and coords in the fluid, set xnew to them
651  MB_CHK_SET_ERR( mbImpl->get_adjacencies( fluid_elems, 0, false, fluid_verts, Interface::UNION ),
652  "Failed to get vertices" );
653  fluid_verts = subtract( fluid_verts, solid_verts );
654 
655  if( coords.size() < 3 * fluid_verts.size() ) coords.resize( 3 * fluid_verts.size() );
656  MB_CHK_SET_ERR( mbImpl->get_coords( fluid_verts, &coords[0] ), "Failed to get vertex coords" );
657  MB_CHK_SET_ERR( mbImpl->tag_set_data( xNew, fluid_verts, &coords[0] ), "Failed to set xnew tag on fluid verts" );
658 
659  if( debug )
660  {
661  // Save deformed mesh coords to new file for visualizing
662  Range tmp_range( fluidElems[MASTER] );
663  tmp_range.merge( solidElems[MASTER] );
664  MB_CHK_ERR( write_and_save( tmp_range, masterSet, xNew, "deformed_master.h5m", true ) );
665  }
666 
667  return MB_SUCCESS;
668 }
669 
670 ErrorCode DeformMeshRemap::read_file( int m_or_s, string& fname, EntityHandle& seth )
671 {
672  // Create meshset
673  MB_CHK_SET_ERR( mbImpl->create_meshset( 0, seth ), "Couldn't create master/slave set" );
674  ostringstream options;
675 #ifdef USE_MPI
676  ParallelComm* pc = ( m_or_s == MASTER ? pcMaster : pcSlave );
677  if( pc && pc->size() > 1 )
678  {
679  if( debug ) options << "DEBUG_IO=1;CPUTIME;";
680  options << "PARALLEL=READ_PART;PARTITION=PARALLEL_PARTITION;PARALLEL_RESOLVE_SHARED_ENTS;"
681  << "PARALLEL_GHOSTS=2.0.1;PARALLEL_COMM=" << pc->get_id();
682  }
683 #endif
684  MB_CHK_SET_ERR( mbImpl->load_file( fname.c_str(), &seth, options.str().c_str() ),
685  "Couldn't load master/slave mesh" );
686 
687  if( *solidSetNos[m_or_s].begin() == -1 || *fluidSetNos[m_or_s].begin() == -1 ) return MB_SUCCESS;
688 
689  // Get material sets for solid/fluid
690  Tag tagh;
691  MB_CHK_SET_ERR( mbImpl->tag_get_handle( MATERIAL_SET_TAG_NAME, tagh ), "Couldn't get material set tag name" );
692  for( set< int >::iterator sit = solidSetNos[m_or_s].begin(); sit != solidSetNos[m_or_s].end(); ++sit )
693  {
694  Range sets;
695  int set_no = *sit;
696  const void* setno_ptr = &set_no;
697  rval = mbImpl->get_entities_by_type_and_tag( seth, MBENTITYSET, &tagh, &setno_ptr, 1, sets );
698  if( MB_SUCCESS != rval || sets.empty() )
699  {
700  MB_SET_ERR( MB_FAILURE, "Couldn't find solid set #" << *sit );
701  }
702  else
703  solidSets[m_or_s].merge( sets );
704  }
705 
706  // Get solid entities, and dimension
707  Range tmp_range;
708  for( Range::iterator rit = solidSets[m_or_s].begin(); rit != solidSets[m_or_s].end(); ++rit )
709  {
710  MB_CHK_SET_ERR( mbImpl->get_entities_by_handle( *rit, tmp_range, true ), "Failed to get entities in solid" );
711  }
712  if( !tmp_range.empty() )
713  {
714  int dim = mbImpl->dimension_from_handle( *tmp_range.rbegin() );
715  assert( dim > 0 && dim < 4 );
716  solidElems[m_or_s] = tmp_range.subset_by_dimension( dim );
717  }
718 
719  if( debug )
720  cout << "Read " << solidElems[m_or_s].size() << " solid elements from " << solidSets[m_or_s].size()
721  << " sets in " << ( m_or_s == MASTER ? "master" : "slave" ) << " mesh." << endl;
722 
723  for( set< int >::iterator sit = fluidSetNos[m_or_s].begin(); sit != fluidSetNos[m_or_s].end(); ++sit )
724  {
725  Range sets;
726  int set_no = *sit;
727  const void* setno_ptr = &set_no;
728  rval = mbImpl->get_entities_by_type_and_tag( seth, MBENTITYSET, &tagh, &setno_ptr, 1, sets );
729  if( MB_SUCCESS != rval || sets.empty() )
730  {
731  MB_SET_ERR( MB_FAILURE, "Couldn't find fluid set #" << *sit );
732  }
733  else
734  fluidSets[m_or_s].merge( sets );
735  }
736 
737  // Get fluid entities, and dimension
738  tmp_range.clear();
739  for( Range::iterator rit = fluidSets[m_or_s].begin(); rit != fluidSets[m_or_s].end(); ++rit )
740  {
741  MB_CHK_SET_ERR( mbImpl->get_entities_by_handle( *rit, tmp_range, true ), "Failed to get entities in fluid" );
742  }
743  if( !tmp_range.empty() )
744  {
745  int dim = mbImpl->dimension_from_handle( *tmp_range.rbegin() );
746  assert( dim > 0 && dim < 4 );
747  fluidElems[m_or_s] = tmp_range.subset_by_dimension( dim );
748  }
749 
750  if( debug )
751  cout << "Read " << fluidElems[m_or_s].size() << " fluid elements from " << fluidSets[m_or_s].size()
752  << " sets in " << ( m_or_s == MASTER ? "master" : "slave" ) << " mesh." << endl;
753 
754  return rval;
755 }
756 
758 {
759  // Solid or fluid sets are missing; find the other
760  Range *filled_sets = NULL, *unfilled_sets = NULL, *unfilled_elems = NULL;
761 
762  if( fluidSets[m_or_s].empty() && !solidSets[m_or_s].empty() )
763  {
764  unfilled_sets = &fluidSets[m_or_s];
765  filled_sets = &solidSets[m_or_s];
766  unfilled_elems = &fluidElems[m_or_s];
767  if( debug )
768  cout << "Finding unspecified fluid elements in " << ( m_or_s == MASTER ? "master" : "slave" ) << " mesh...";
769  }
770  else if( !fluidSets[m_or_s].empty() && solidSets[m_or_s].empty() )
771  {
772  filled_sets = &fluidSets[m_or_s];
773  unfilled_sets = &solidSets[m_or_s];
774  unfilled_elems = &solidElems[m_or_s];
775  if( debug )
776  cout << "Finding unspecified solid elements in " << ( m_or_s == MASTER ? "master" : "slave" ) << " mesh...";
777  }
778 
779  // Ok, we know the filled sets, now fill the unfilled sets, and the elems from those
780  Tag tagh;
781  MB_CHK_SET_ERR( mbImpl->tag_get_handle( MATERIAL_SET_TAG_NAME, tagh ), "Couldn't get material set tag name" );
782  Range matsets;
783  rval = mbImpl->get_entities_by_type_and_tag( file_set, MBENTITYSET, &tagh, NULL, 1, matsets );
784  if( MB_SUCCESS != rval || matsets.empty() )
785  {
786  MB_SET_ERR( MB_FAILURE, "Couldn't get any material sets" );
787  }
788  *unfilled_sets = subtract( matsets, *filled_sets );
789  if( unfilled_sets->empty() )
790  {
791  MB_SET_ERR( MB_FAILURE, "Failed to find any unfilled material sets" );
792  }
793  Range tmp_range;
794  for( Range::iterator rit = unfilled_sets->begin(); rit != unfilled_sets->end(); ++rit )
795  {
796  MB_CHK_SET_ERR( mbImpl->get_entities_by_handle( *rit, tmp_range, true ),
797  "Failed to get entities in unfilled set" );
798  }
799  int dim = mbImpl->dimension_from_handle( *tmp_range.rbegin() );
800  assert( dim > 0 && dim < 4 );
801  *unfilled_elems = tmp_range.subset_by_dimension( dim );
802  if( unfilled_elems->empty() )
803  {
804  MB_SET_ERR( MB_FAILURE, "Failed to find any unfilled set entities" );
805  }
806 
807  if( debug )
808  cout << "found " << unfilled_sets->size() << " sets and " << unfilled_elems->size() << " elements." << endl;
809 
810  return MB_SUCCESS;
811 }