void set_field_relations( Entity & e_from ,
                          Entity & e_to ,
                          const unsigned ident )
{
  const std::vector<FieldRelation> & field_rels =
    e_from.bucket().mesh().mesh_meta_data().get_field_relations();

  for ( std::vector<FieldRelation>::const_iterator
        j = field_rels.begin() ; j != field_rels.end() ; ++j ) {

    const FieldRelation & fr = *j ;

    void ** const ptr = (void**) field_data( * fr.m_root , e_from );

    if ( ptr ) {

      void * const src = field_data( * fr.m_target , e_to );

      const size_t number =
        field_data_size(*fr.m_root,e_from) / sizeof(void*);

      const size_t offset =
         (*fr.m_function)( e_from.entity_rank() ,
                           e_to.entity_rank() , ident );

      if ( offset < number ) {
        ptr[ offset ] = src ;
      }
    }
  }
}
Beispiel #2
0
void pack_field_values( CommBuffer & buf , Entity & entity )
{
  const Bucket   & bucket = entity.bucket();
  const BulkData & mesh   = BulkData::get(bucket);
  const MetaData & mesh_meta_data = MetaData::get(mesh);

  const std::vector< FieldBase * > & fields = mesh_meta_data.get_fields();

  for ( std::vector< FieldBase * >::const_iterator
        i = fields.begin() ; i != fields.end() ; ++i ) {

    const FieldBase & f = **i ;

    if ( f.data_traits().is_pod ) {
      const unsigned size = field_data_size( f , bucket );

      buf.pack<unsigned>( size );

      if ( size ) {
        unsigned char * const ptr =
          reinterpret_cast<unsigned char *>( field_data( f , entity ) );
        buf.pack<unsigned char>( ptr , size );
      }
    }
  }
}
Beispiel #3
0
    void BeamFixture::populate()
    {
      // Populate mesh with all node types

      m_bulkData.modification_begin();

      if (m_bulkData.parallel_rank() == 0)
        {
          stk::mesh::EntityId curr_elem_id = 1;

          // For each element topology declare elements
          for ( unsigned i = 0 ; i < number_beam ; ++i , ++curr_elem_id ) {
            stk::mesh::fem::declare_element( m_bulkData, m_block_beam, curr_elem_id, beam_node_ids[i] );
          }

          // For all nodes assign nodal coordinates
          for ( unsigned i = 0 ; i < node_count ; ++i ) {
            stk::mesh::Entity * const node = m_bulkData.get_entity( stk::mesh::fem::FEMMetaData::NODE_RANK , i + 1 );
            double * const coord = field_data( m_coordinates_field , *node );
            coord[0] = node_coord_data[i][0] ;
            coord[1] = node_coord_data[i][1] ;
            coord[2] = node_coord_data[i][2] ;
          }

        }
      m_bulkData.modification_end();

    }
Beispiel #4
0
bool field_data_valid( const FieldBase & f ,
                       const Bucket & k ,
                       unsigned ord ,
                       const char * required_by )
{
  const MetaData * const k_mesh_meta_data = & MetaData::get(k);
  const MetaData * const f_mesh_meta_data = & MetaData::get(f);
  const bool ok_mesh_meta_data  = k_mesh_meta_data == f_mesh_meta_data ;
  const bool ok_ord     = ord < k.size() ;
  const bool exists     = ok_mesh_meta_data && ok_ord &&
                          NULL != field_data( f , k.begin() );

  if ( required_by && ! exists ) {
    std::ostringstream msg_begin ;
    msg_begin << "For args: " ;
    msg_begin << f << " , " ;
    msg_begin << k << " , " ;
    msg_begin << ord << " , " ;
    msg_begin << required_by ;
    msg_begin << "; operation FAILED with " ;
    ThrowErrorMsgIf( ! ok_mesh_meta_data,
                     msg_begin.str() << " different MetaData");
    ThrowErrorMsgIf( ! ok_ord, msg_begin.str() <<
                     " Ordinal " <<  ord << " >= " << " size " << k.size());
    ThrowErrorMsg( msg_begin.str() << " no data");
  }

  return exists ;
}
void centroid_algorithm(
  const BulkData & bulkData ,
  const VectorFieldType             & elem_centroid ,
  const ElementNodePointerFieldType & elem_node_coord ,
  Part & elem_part,
  EntityRank element_rank )
{
  // Use the "homogeneous subset" concept (see the Domain Model document)
  // for field data storage.  A "homogeneous subset" is called
  // a 'Bucket'.

  // Iterate the set of element buckets:

  const std::vector<Bucket*> & buckets = bulkData.buckets( element_rank );

  for ( std::vector<Bucket*>::const_iterator
        k = buckets.begin() ; k != buckets.end() ; ++k ) {
    Bucket & bucket = **k ;

    // If this bucket is a subset of the given elem_part
    // then want to compute on it.

    if ( has_superset( bucket, elem_part ) ) {

      // Number of elements in the bucket:

      const unsigned size = bucket.size();

      // Aggressive "gather" field data for the elements
      // in the bucket.
      //   double * node_ptr[ nodes_per_element * number_of_elements ]

      double ** node_ptr = field_data( elem_node_coord , bucket.begin() );

      // Element centroid field data
      //   double elem_ptr[ SpatialDim * number_of_elements ]

      double *  elem_ptr = field_data( elem_centroid , bucket.begin() );

      // Call an element function to calculate centroid for
      // contiguous arrays of element field data.

      centroid< ElementTraits >( size , elem_ptr , node_ptr );
    }
  }
}
Beispiel #6
0
void UseCase_3_Mesh::populate()
{
  // Populate mesh with all node types

  m_bulkData.modification_begin();

  EntityId curr_elem_id = 1;

  // For each element topology declare elements

  for ( unsigned i = 0 ; i < number_hex ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_hex, curr_elem_id, hex_node_ids[i] );
  }

  for ( unsigned i = 0 ; i < number_wedge ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_wedge, curr_elem_id, wedge_node_ids[i] );
  }

  for ( unsigned i = 0 ; i < number_tetra ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_tet, curr_elem_id, tetra_node_ids[i] );
  }

  for ( unsigned i = 0 ; i < number_pyramid ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_pyramid, curr_elem_id, pyramid_node_ids[i] );
  }

  for ( unsigned i = 0 ; i < number_shell_quad ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_quad_shell, curr_elem_id, shell_quad_node_ids[i]);
  }

  for ( unsigned i = 0 ; i < number_shell_tri ; ++i , ++curr_elem_id ) {
    fem::declare_element( m_bulkData, m_block_tri_shell, curr_elem_id, shell_tri_node_ids[i] );
  }

  // For all nodes assign nodal coordinates
  for ( unsigned i = 0 ; i < node_count ; ++i ) {
    Entity * const node = m_bulkData.get_entity( m_node_rank , i + 1 );
    double * const coord = field_data( m_coordinates_field , *node );
    coord[0] = node_coord_data[i][0] ;
    coord[1] = node_coord_data[i][1] ;
    coord[2] = node_coord_data[i][2] ;
  }

  m_bulkData.modification_end();
  // No parallel stuff for now
}
Beispiel #7
0
bool unpack_field_values(
  CommBuffer & buf , Entity & entity , std::ostream & error_msg )
{
  const Bucket   & bucket = entity.bucket();
  const BulkData & mesh   = BulkData::get(bucket);
  const MetaData & mesh_meta_data = MetaData::get(mesh);

  const std::vector< FieldBase * > & fields = mesh_meta_data.get_fields();

  const std::vector< FieldBase * >::const_iterator i_end = fields.end();
  const std::vector< FieldBase * >::const_iterator i_beg = fields.begin();

  std::vector< FieldBase * >::const_iterator i ;

  bool ok = true ;

  for ( i = i_beg ; i_end != i ; ) {
    const FieldBase & f = **i ; ++i ;

    if ( f.data_traits().is_pod ) {

      const unsigned size = field_data_size( f , bucket );
      unsigned recv_data_size = 0 ;
      buf.unpack<unsigned>( recv_data_size );

      if ( size != recv_data_size ) {
        if ( ok ) {
          ok = false ;
          print_entity_key( error_msg , mesh_meta_data , entity.key() );
        }
        error_msg << " " << f.name();
        error_msg << " " << size ;
        error_msg << " != " << recv_data_size ;
        buf.skip<unsigned char>( recv_data_size );
      }
      else if ( size ) { // Non-zero and equal
        unsigned char * ptr =
          reinterpret_cast<unsigned char *>( field_data( f , entity ) );
        buf.unpack<unsigned char>( ptr , size );
      }
    }
  }

  return ok ;
}
Beispiel #8
0
bool field_data_valid( const FieldBase & f ,
                       const Bucket & k ,
                       unsigned ord ,
                       const char * required_by )
{
  const MetaData * const k_mesh_meta_data = & k.mesh().mesh_meta_data();
  const MetaData * const f_mesh_meta_data = & f.mesh_meta_data();
  const bool ok_mesh_meta_data  = k_mesh_meta_data == f_mesh_meta_data ;
  const bool ok_ord     = ord < k.size() ;
  const bool exists     = ok_mesh_meta_data && ok_ord &&
                          NULL != field_data( f , k.begin() );

  if ( required_by && ! exists ) {
    std::ostringstream msg ;
    msg << "stk::mesh::field_data_valid( " ;
    msg << f ;
    msg << " , " ;
    msg << k ;
    msg << " , " ;
    msg << ord ;
    msg << " , " ;
    msg << required_by ;
    msg << " ) FAILED with " ;
    if ( ! ok_mesh_meta_data ) {
      msg << " different MetaData" ;
    }
    else if ( ! ok_ord ) {
      msg << " Ordinal " ;
      msg << ord ;
      msg << " >= " ;
      msg << " size " ;
      msg << k.size();
    }
    else {
      msg << " no data" ;
    }
    throw std::runtime_error( msg.str() );
  }

  return exists ;
}
Beispiel #9
0
bool gather_field_data( const field_type & field ,
                        const Entity     & entity ,
                        typename FieldTraits< field_type >::data_type * dst )
{
  typedef typename FieldTraits< field_type >::data_type T ;

  PairIterRelation rel = entity.relations( EType );

  bool result = NRel == (unsigned) rel.size();

  if ( result ) {
    T * const dst_end = dst + NType * NRel ;
    for ( const T * src ;
          ( dst < dst_end ) &&
          ( src = field_data( field , * rel->entity() ) ) ;
          ++rel , dst += NType ) {
      Copy<NType>( dst , src );
    }
    result = dst == dst_end ;
  }
  return result ;
}
Beispiel #10
0
inline
typename FieldTraits< field_type >::data_type *
field_data( const field_type & f , const Bucket::iterator i )
{
  return field_data(f,*i);
}
    void HeterogeneousFixture::populate()
    {
      // Populate mesh with all node types

      m_bulkData.modification_begin();

      if (m_bulkData.parallel_rank() == 0)
        {
          stk_classic::mesh::EntityId curr_elem_id = 1;

          // For each element topology declare elements

          stk_classic::mesh::Entity *wedges[number_wedge];

          for ( unsigned i = 0 ; i < number_hex ; ++i , ++curr_elem_id ) {
            stk_classic::mesh::fem::declare_element( m_bulkData, m_block_hex, curr_elem_id, hex_node_ids[i] );
          }

          for ( unsigned i = 0 ; i < number_wedge ; ++i , ++curr_elem_id ) {
            wedges[i] = &stk_classic::mesh::fem::declare_element( m_bulkData, m_block_wedge, curr_elem_id, wedge_node_ids[i] );
          }

          for ( unsigned i = 0 ; i < number_tetra ; ++i , ++curr_elem_id ) {
            stk_classic::mesh::fem::declare_element( m_bulkData, m_block_tet, curr_elem_id, tetra_node_ids[i] );
          }

          for ( unsigned i = 0 ; i < number_pyramid ; ++i , ++curr_elem_id ) {
            stk_classic::mesh::fem::declare_element( m_bulkData, m_block_pyramid, curr_elem_id, pyramid_node_ids[i] );
          }

#if HET_FIX_INCLUDE_EXTRA_ELEM_TYPES
          for ( unsigned i = 0 ; i < number_shell_quad ; ++i , ++curr_elem_id ) {
            stk_classic::mesh::fem::declare_element( m_bulkData, m_block_quad_shell, curr_elem_id, shell_quad_node_ids[i]);
          }

          for ( unsigned i = 0 ; i < number_shell_tri ; ++i , ++curr_elem_id ) {
            stk_classic::mesh::fem::declare_element( m_bulkData, m_block_tri_shell, curr_elem_id, shell_tri_node_ids[i] );
          }
#endif

          if (m_sideset_quad)
            {
              for ( unsigned i = 0 ; i < number_quad ; ++i , ++curr_elem_id ) {
                std::cout << "quad i= " << i << std::endl;
                stk_classic::mesh::fem::declare_element_side( m_bulkData, 
                                                      curr_elem_id, //side_id,
                                                      *wedges[quad_node_side_ids[i][0] - 4], // element,
                                                      quad_node_side_ids[i][1],   //j_side, // local_side_ord,
                                                      m_sideset_quad_subset);
              }
            }

          if (m_sideset_tri)
            {
              for ( unsigned i = 0 ; i < number_tri ; ++i , ++curr_elem_id ) {
                std::cout << "tri i= " << i << std::endl;
                stk_classic::mesh::fem::declare_element_side( m_bulkData, 
                                                      curr_elem_id, //side_id,
                                                      *wedges[tri_node_side_ids[i][0] - 4], // element,
                                                      tri_node_side_ids[i][1],   //j_side, // local_side_ord,
                                                      m_sideset_tri_subset);
              }
            }

          // For all nodes assign nodal coordinates
          for ( unsigned i = 0 ; i < node_count ; ++i ) {
            stk_classic::mesh::Entity * const node = m_bulkData.get_entity( stk_classic::mesh::fem::FEMMetaData::NODE_RANK , i + 1 );
            double * const coord = field_data( m_coordinates_field , *node );
            coord[0] = node_coord_data[i][0] ;
            coord[1] = node_coord_data[i][1] ;
            coord[2] = node_coord_data[i][2] ;
          }

        }
      m_bulkData.modification_end();

    }
Beispiel #12
0
void VisionModule::updateVisionField() {

    portals::Message<messages::VisionField> field_data(0);

    // setting lines info
    const std::vector<boost::shared_ptr<VisualLine> >* visualLines = vision->fieldLines->getLines();
    for(std::vector<boost::shared_ptr<VisualLine> >::const_iterator i = visualLines->begin();
        i != visualLines->end(); i++)
    {
        messages::VisualLine *visLine = field_data.get()->add_visual_line();
        visLine->mutable_visual_detection()->set_distance(i->get()->getDistance());
        visLine->mutable_visual_detection()->set_bearing(i->get()->getBearing());
        visLine->mutable_visual_detection()->set_distance_sd(i->get()->getDistanceSD());
        visLine->mutable_visual_detection()->set_bearing_sd(i->get()->getBearingSD());
        //we wont set concrete coords for the lines, since they are lines
        visLine->set_start_x(i->get()->getStartpoint().x);
        visLine->set_start_y(i->get()->getStartpoint().y);
        visLine->set_start_dist(i->get()->getStartEst().dist);
        visLine->set_start_bearing(i->get()->getStartEst().bearing);
        visLine->set_end_x(i->get()->getEndpoint().x);
        visLine->set_end_y(i->get()->getEndpoint().y);
        visLine->set_end_dist(i->get()->getEndEst().dist);
        visLine->set_end_bearing(i->get()->getEndEst().bearing);
        visLine->set_angle(i->get()->getAngle());
        visLine->set_avg_width(i->get()->getAvgWidth());
        visLine->set_length(i->get()->getLength());
        visLine->set_slope(i->get()->getSlope());
        visLine->set_y_int(i->get()->getYIntercept());
        const std::vector<lineID> id_for_line = i->get()->getIDs();
        for (unsigned int k = 0; k < id_for_line.size(); k++) {
            visLine->add_possibilities(id_for_line[k]);
        }

    }

    const std::vector<boost::shared_ptr<VisualLine> >* bottomLines = vision->bottomLines->getLines();
    for(std::vector<boost::shared_ptr<VisualLine> >::const_iterator i = bottomLines->begin();
        i != bottomLines->end(); i++)
    {
        messages::VisualLine *botLine = field_data.get()->add_bottom_line();
        botLine->mutable_visual_detection()->set_distance(i->get()->getDistance());
        botLine->mutable_visual_detection()->set_bearing(i->get()->getBearing());
        botLine->mutable_visual_detection()->set_distance_sd(i->get()->getDistanceSD());
        botLine->mutable_visual_detection()->set_bearing_sd(i->get()->getBearingSD());
        botLine->set_start_x(i->get()->getStartpoint().x);
        botLine->set_start_y(i->get()->getStartpoint().y);
        botLine->set_end_x(i->get()->getEndpoint().x);
        botLine->set_end_y(i->get()->getEndpoint().y);
        botLine->set_start_dist(i->get()->getStartEst().dist);
        botLine->set_start_bearing(i->get()->getStartEst().bearing);
        botLine->set_end_dist(i->get()->getEndEst().dist);
        botLine->set_end_bearing(i->get()->getEndEst().bearing);
        botLine->set_angle(i->get()->getAngle());
        botLine->set_avg_width(i->get()->getAvgWidth());
        botLine->set_length(i->get()->getLength());
        botLine->set_slope(i->get()->getSlope());
        botLine->set_y_int(i->get()->getYIntercept());
        const std::vector<lineID> id_for_line = i->get()->getIDs();
        for (unsigned int k = 0; k < id_for_line.size(); k++) {
            botLine->add_possibilities(id_for_line[k]);
        }
    }
    //end lines info

    //setting the corner info
    std::list<VisualCorner>* visualCorners = vision->fieldLines->getCorners();
    for(std::list<VisualCorner>::iterator i = visualCorners->begin();
        i != visualCorners->end(); i++)
    {
        messages::VisualCorner *visCorner = field_data.get()->add_visual_corner();
        visCorner->set_orientation(i->getOrientation());
        visCorner->set_corner_type(i->getShape());
        visCorner->set_physical_orientation(i->getPhysicalOrientation());
        visCorner->mutable_visual_detection()->set_distance(i->getDistance());
        visCorner->mutable_visual_detection()->set_bearing(i->getBearing());
        visCorner->mutable_visual_detection()->set_bearing_deg(i->getBearingDeg());
        visCorner->mutable_visual_detection()->set_distance_sd(i->getDistanceSD());
        visCorner->mutable_visual_detection()->set_bearing_sd(i->getBearingSD());
        visCorner->mutable_visual_detection()->set_angle_x_deg(i->getAngleXDeg());
        visCorner->mutable_visual_detection()->set_angle_y_deg(i->getAngleYDeg());
        visCorner->mutable_visual_detection()->set_bearing_deg(i->getBearingDeg());
        visCorner->set_x(i->getX());
        visCorner->set_y(i->getY());

        const std::list<const ConcreteCorner *>* possible = i->getPossibilities();
        for(std::list<const ConcreteCorner*>::const_iterator j = possible->begin();
            j != possible->end(); j++)
        {
            messages::Point *field_point =
                visCorner->mutable_visual_detection()->add_concrete_coords();

            field_point->set_x((**j).getFieldX());
            field_point->set_y((**j).getFieldY());
            field_point->set_field_angle((**j).getFieldAngle());
        }

        const std::vector<cornerID> p_id = i->getIDs();
        for (unsigned int k = 0; k < p_id.size(); k++)
        {
            visCorner->add_poss_id(p_id[k]);
        }


    }

   std::list<VisualCorner>* bottomCorners = vision->bottomLines->getCorners();
    for(std::list<VisualCorner>::iterator i = bottomCorners->begin();
        i != bottomCorners->end(); i++)
    {
        messages::VisualCorner *botCorner = field_data.get()->add_bottom_corner();
        botCorner->set_orientation(i->getOrientation());
        botCorner->set_corner_type(i->getShape());
        botCorner->set_physical_orientation(i->getPhysicalOrientation());
        botCorner->mutable_visual_detection()->set_distance(i->getDistance());
        botCorner->mutable_visual_detection()->set_bearing(i->getBearing());
        botCorner->mutable_visual_detection()->set_bearing_deg(i->getBearingDeg());
        botCorner->mutable_visual_detection()->set_distance_sd(i->getDistanceSD());
        botCorner->mutable_visual_detection()->set_bearing_sd(i->getBearingSD());
        botCorner->mutable_visual_detection()->set_angle_x_deg(i->getAngleXDeg());
        botCorner->mutable_visual_detection()->set_angle_y_deg(i->getAngleYDeg());
        botCorner->set_x(i->getX());
        botCorner->set_y(i->getY());

        const std::list<const ConcreteCorner *>* possible = i->getPossibilities();
        for(std::list<const ConcreteCorner*>::const_iterator j = possible->begin();
            j != possible->end(); j++)
        {
            messages::Point *field_point =
                botCorner->mutable_visual_detection()->add_concrete_coords();

            field_point->set_x((**j).getFieldX());
            field_point->set_y((**j).getFieldY());
            field_point->set_field_angle((**j).getFieldAngle());
        }

        const std::vector<cornerID> p_id = i->getIDs();
        for (unsigned int k = 0; k < p_id.size(); k++)
        {
            botCorner->add_poss_id(p_id[k]);
        }


    }
    //end corner info

    //setting goalpostleft info
    field_data.get()->mutable_goal_post_l()->set_height(vision->yglp->getHeight());
    field_data.get()->mutable_goal_post_l()->set_width(vision->yglp->getWidth());

    field_data.get()->mutable_goal_post_l()->mutable_left_top()->
        set_x((float)vision->yglp->getLeftTopX());
    field_data.get()->mutable_goal_post_l()->mutable_left_top()->
        set_y((float)vision->yglp->getLeftTopY());
    field_data.get()->mutable_goal_post_l()->mutable_right_top()->
        set_x((float)vision->yglp->getRightTopX());
    field_data.get()->mutable_goal_post_l()->mutable_right_top()->
        set_y((float)vision->yglp->getRightTopY());
    field_data.get()->mutable_goal_post_l()->mutable_left_bot()->
        set_x((float)vision->yglp->getLeftBottomX());
    field_data.get()->mutable_goal_post_l()->mutable_left_bot()->
        set_y((float)vision->yglp->getLeftBottomY());
    field_data.get()->mutable_goal_post_l()->mutable_right_bot()->
        set_x((float)vision->yglp->getRightBottomX());
    field_data.get()->mutable_goal_post_l()->mutable_right_bot()->
        set_y((float)vision->yglp->getRightBottomY());

    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_intopcam(vision->yglp->isTopCam());

    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_distance(vision->yglp->getDistance());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_bearing(vision->yglp->getBearing());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_bearing_deg(vision->yglp->getBearingDeg());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_distance_sd(vision->yglp->getDistanceSD());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_bearing_sd(vision->yglp->getBearingSD());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_on(vision->yglp->isOn());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_frames_on(vision->yglp->getFramesOn());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_frames_off(vision->yglp->getFramesOff());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_certainty(vision->yglp->getIDCertainty());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_angle_x_deg(vision->yglp->getAngleXDeg());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_angle_y_deg(vision->yglp->getAngleYDeg());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_red_goalie(vision->yglp->getRedGoalieCertain());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_navy_goalie(vision->yglp->getNavyGoalieCertain());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_angle_x_deg(vision->yglp->getAngleXDeg());
    field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
        set_angle_y_deg(vision->yglp->getAngleYDeg());

    const std::list<const ConcreteFieldObject *>* possible_l = vision->yglp->getPossibilities();
    for(std::list<const ConcreteFieldObject*>::const_iterator i = possible_l->begin();
        i != possible_l->end(); i++)
    {
        messages::Point *field_point =
            field_data.get()->mutable_goal_post_l()->mutable_visual_detection()->
            add_concrete_coords();

        field_point->set_x((**i).getFieldX());
        field_point->set_y((**i).getFieldY());
    }
    //end goalpostleft info

    //setting goalpostright info
    field_data.get()->mutable_goal_post_r()->set_height(vision->ygrp->getHeight());
    field_data.get()->mutable_goal_post_r()->set_width(vision->ygrp->getWidth());

    field_data.get()->mutable_goal_post_r()->mutable_left_top()->
        set_x((float)vision->ygrp->getLeftTopX());
    field_data.get()->mutable_goal_post_r()->mutable_left_top()->
        set_y((float)vision->ygrp->getLeftTopY());
    field_data.get()->mutable_goal_post_r()->mutable_right_top()->
        set_x((float)vision->ygrp->getRightTopX());
    field_data.get()->mutable_goal_post_r()->mutable_right_top()->
        set_y((float)vision->ygrp->getRightTopY());
    field_data.get()->mutable_goal_post_r()->mutable_left_bot()->
        set_x((float)vision->ygrp->getLeftBottomX());
    field_data.get()->mutable_goal_post_r()->mutable_left_bot()->
        set_y((float)vision->ygrp->getLeftBottomY());
    field_data.get()->mutable_goal_post_r()->mutable_right_bot()->
        set_x((float)vision->ygrp->getRightBottomX());
    field_data.get()->mutable_goal_post_r()->mutable_right_bot()->
        set_y((float)vision->ygrp->getRightBottomY());

    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_intopcam(vision->ygrp->isTopCam());

    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_distance(vision->ygrp->getDistance());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_bearing(vision->ygrp->getBearing());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_bearing_deg(vision->ygrp->getBearingDeg());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_distance_sd(vision->ygrp->getDistanceSD());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_bearing_sd(vision->ygrp->getBearingSD());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_on(vision->ygrp->isOn());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_frames_on(vision->ygrp->getFramesOn());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_frames_off(vision->ygrp->getFramesOff());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_certainty(vision->ygrp->getIDCertainty());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_red_goalie(vision->ygrp->getRedGoalieCertain());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_navy_goalie(vision->ygrp->getNavyGoalieCertain());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_angle_x_deg(vision->ygrp->getAngleXDeg());
    field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
        set_angle_y_deg(vision->ygrp->getAngleYDeg());

    const std::list<const ConcreteFieldObject *>* possible_r = vision->ygrp->getPossibilities();
    for(std::list<const ConcreteFieldObject*>::const_iterator i = possible_r->begin();
        i != possible_r->end(); i++)
    {
        messages::Point *field_point =
            field_data.get()->mutable_goal_post_r()->mutable_visual_detection()->
            add_concrete_coords();

        field_point->set_x((**i).getFieldX());
        field_point->set_y((**i).getFieldY());
    }
    //end goalpostright info

    //setting fieldedge data
    field_data.get()->mutable_visual_field_edge()->set_distance_l(vision->fieldEdge->
                                                                  getDistanceLeft());
    field_data.get()->mutable_visual_field_edge()->set_distance_m(vision->fieldEdge->
                                                                  getDistanceCenter());
    field_data.get()->mutable_visual_field_edge()->set_distance_r(vision->fieldEdge->
                                                                  getDistanceRight());
    //end fieldedge data


    //setting cross info
    field_data.get()->mutable_visual_cross()->set_distance(vision->cross->getDistance());
    field_data.get()->mutable_visual_cross()->set_bearing(vision->cross->getBearing());
    field_data.get()->mutable_visual_cross()->set_distance_sd(vision->cross->getDistanceSD());
    field_data.get()->mutable_visual_cross()->set_bearing_sd(vision->cross->getBearingSD());
    field_data.get()->mutable_visual_cross()->set_angle_x_deg(vision->cross->getAngleXDeg());
    field_data.get()->mutable_visual_cross()->set_angle_y_deg(vision->cross->getAngleYDeg());
    field_data.get()->mutable_visual_cross()->set_x(vision->cross->getX());
    field_data.get()->mutable_visual_cross()->set_y(vision->cross->getY());

    const std::list<const ConcreteCross *>* possible_cross = vision->cross->getPossibilities();
    for (std::list<const ConcreteCross*>::const_iterator i = possible_cross->begin();
         i != possible_cross->end(); i++)
    {
        messages::Point *field_point =
            field_data.get()->mutable_visual_cross()->add_concrete_coords();

        field_point->set_x((**i).getFieldX());
        field_point->set_y((**i).getFieldY());
    }

    vision_field.setMessage(field_data);
}
Beispiel #13
0
void communicate_field_data(
  const BulkData & mesh ,
  const unsigned field_count ,
  const FieldBase * fields[] ,
  CommAll & sparse )
{
  const std::vector<Entity*> & entity_comm = mesh.entity_comm();

  const unsigned parallel_size = mesh.parallel_size();

  // Sizing for send and receive

  const unsigned zero = 0 ;
  std::vector<unsigned> msg_size( parallel_size , zero );

  size_t j = 0;

  for ( j = 0 ; j < field_count ; ++j ) {
    const FieldBase & f = * fields[j] ;
    for ( std::vector<Entity*>::const_iterator
          i = entity_comm.begin() ; i != entity_comm.end() ; ++i ) {
      Entity & e = **i ;
      const unsigned size = field_data_size( f , e );
      if ( size ) {
        for ( PairIterEntityComm
              ec = e.comm() ; ! ec.empty() && ec->ghost_id == 0 ; ++ec ) {
          msg_size[ ec->proc ] += size ;
        }
      }
    }
  }

  // Allocate send and receive buffers:

  {
    const unsigned * const s_size = & msg_size[0] ;
    sparse.allocate_buffers( mesh.parallel(), parallel_size / 4 , s_size, s_size);
  }

  // Pack for send:

  for ( j = 0 ; j < field_count ; ++j ) {
    const FieldBase & f = * fields[j] ;
    for ( std::vector<Entity*>::const_iterator
          i = entity_comm.begin() ; i != entity_comm.end() ; ++i ) {
      Entity & e = **i ;
      const unsigned size = field_data_size( f , e );
      if ( size ) {
        unsigned char * ptr =
          reinterpret_cast<unsigned char *>(field_data( f , e ));
        for ( PairIterEntityComm
              ec = e.comm() ; ! ec.empty() && ec->ghost_id == 0 ; ++ec ) {
          CommBuffer & b = sparse.send_buffer( ec->proc );
          b.pack<unsigned char>( ptr , size );
        }
      }
    }
  }

  // Communicate:

  sparse.communicate();
}
Beispiel #14
0
void communicate_field_data(
  const Ghosting                        & ghosts ,
  const std::vector< const FieldBase *> & fields )
{
  if ( fields.empty() ) { return; }

  const BulkData & mesh = BulkData::get(ghosts);
  const unsigned parallel_size = mesh.parallel_size();
  const unsigned parallel_rank = mesh.parallel_rank();

  const std::vector<const FieldBase *>::const_iterator fe = fields.end();
  const std::vector<const FieldBase *>::const_iterator fb = fields.begin();
        std::vector<const FieldBase *>::const_iterator fi ;

  // Sizing for send and receive

  const unsigned zero = 0 ;
  std::vector<unsigned> send_size( parallel_size , zero );
  std::vector<unsigned> recv_size( parallel_size , zero );

  for ( std::vector<Entity*>::const_iterator
        i =  mesh.entity_comm().begin() ;
        i != mesh.entity_comm().end() ; ++i ) {
    Entity & e = **i ;
    const bool owned = e.owner_rank() == parallel_rank ;

    unsigned e_size = 0 ;
    for ( fi = fb ; fi != fe ; ++fi ) {
      const FieldBase & f = **fi ;
      e_size += field_data_size( f , e );
    }

    for ( PairIterEntityComm ec = e.comm() ; ! ec.empty() ; ++ec ) {
      if ( ghosts.ordinal() == ec->ghost_id ) {
        if ( owned ) {
          send_size[ ec->proc ] += e_size ;
        }
        else {
          recv_size[ ec->proc ] += e_size ;
        }
      }
    }
  }

  // Allocate send and receive buffers:

  CommAll sparse ;

  {
    const unsigned * const s_size = & send_size[0] ;
    const unsigned * const r_size = & recv_size[0] ;
    sparse.allocate_buffers( mesh.parallel(), parallel_size / 4 , s_size, r_size);
  }

  // Send packing:

  for ( std::vector<Entity*>::const_iterator
        i =  mesh.entity_comm().begin() ;
        i != mesh.entity_comm().end() ; ++i ) {
    Entity & e = **i ;
    if ( e.owner_rank() == parallel_rank ) {

      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        const unsigned size = field_data_size( f , e );

        if ( size ) {
          unsigned char * ptr =
            reinterpret_cast<unsigned char *>(field_data( f , e ));

          for ( PairIterEntityComm ec = e.comm() ; ! ec.empty() ; ++ec ) {

            if ( ghosts.ordinal() == ec->ghost_id ) {
              CommBuffer & b = sparse.send_buffer( ec->proc );
              b.pack<unsigned char>( ptr , size );
            }
          }
        }
      }
    }
  }

  // Communicate:

  sparse.communicate();

  // Unpack for recv:

  for ( std::vector<Entity*>::const_iterator
        i =  mesh.entity_comm().begin() ;
        i != mesh.entity_comm().end() ; ++i ) {
    Entity & e = **i ;
    if ( e.owner_rank() != parallel_rank ) {

      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        const unsigned size = field_data_size( f , e );

        if ( size ) {
          unsigned char * ptr =
            reinterpret_cast<unsigned char *>(field_data( f , e ));

          for ( PairIterEntityComm ec = e.comm() ; ! ec.empty() ; ++ec ) {

            if ( ghosts.ordinal() == ec->ghost_id ) {
              CommBuffer & b = sparse.recv_buffer( ec->proc );
              b.unpack<unsigned char>( ptr , size );
            }
          }
        }
      }
    }
  }
}
Beispiel #15
0
 inline
 Scalar * field_ptr(NgpFieldVector<Scalar> & xField, const stk::mesh::Bucket & b)
 {
     return static_cast<Scalar*>(field_data(xField.get_field(), b));
 }
Beispiel #16
0
int main( int a_argc, char* a_argv[] )
{
#ifdef CH_MPI
   // Start MPI
   MPI_Init( &a_argc, &a_argv );
   setChomboMPIErrorHandler();
#endif

   string RZ_mapping_file_name;
   string field_file_name;
   string block_name;

   if (a_argc==4) {
      RZ_mapping_file_name = a_argv[1];
      field_file_name = a_argv[2];
      block_name = a_argv[3];
   }
   else {
      cout << "Usage:  magGeomData...ex <RZ_mapping_file> [<field_file>] <blockname>," << endl;
      cout << "        where <RZ_mapping_file> defines the coordinate mapping, and" << endl;
      cout << "        where <field_file> defines the magnetic field, and" << endl;
      cout << "        where <blockname> is one of lcore, rcore, lcsol, rcsol, lsol, rsol, lpf, rpf, mcore, mcsol" << endl;
      return -1;
   }

   // Construct the field data object
   FieldData field_data(RZ_mapping_file_name, field_file_name, block_name);

   /*
     Fill an FArrayBox with physical coordinates.  In the
     new mesh generator code using the Brackbill-Saltzmann
     approach, the Euler equation will solved for these
     coordinates.  Here, however, we simply create a grid
     in the unit square, which is the assumed mapped
     domain used internally in the FieldData object and
     get the corresponding physical coordinates.
   */

   // Get a grid on the unit square.  The argument sets the size.
   FArrayBox xi;
   getUnitSquareGrid(10, 50, xi);

   const Box& box(xi.box());

   // Get the corresponding physical coordinates.
   FArrayBox physical_coordinates(box,2);
   field_data.getPhysicalCoordinates(xi, physical_coordinates);

   // Write a file containing the physical coordinates that
   // can be plotted with the plot_coords.m script using
   // either MatLab or Octave.  The file name is "coordsX",
   // where X is the block number.
   field_data.writePhysicalCoordinates(physical_coordinates);

   FArrayBox field_unit_vector(box,6);

#ifdef USE_DCT_FIELD  

   // Using the discrete cosine transform (DCT) field representation,
   // whose coefficients are contained in "field_file_name", get the field
   // unit vector (first two components) and the derivatives of its components
   // with respect to the physical coordinates (next 4 components) at
   // this set of physical coordinates.  The numbering of the 6 components
   // is explained at the top of FieldData::convertToMappedDerivatives(),
   // which converts the derivatives in physical space to derivatives
   // in mapped space.

   field_data.getFieldUnitVectorFromDCT(physical_coordinates, field_unit_vector);
   field_data.convertToMappedDerivatives(xi, field_unit_vector);

   FArrayBox magnetic_flux(physical_coordinates.box(),1);
   field_data.getMagneticFluxFromDCT(physical_coordinates, magnetic_flux);

   field_data.writeMagneticFlux(physical_coordinates, magnetic_flux);
#else

   // Using the field data contained in the coordinate mapping
   // file "geometry_file_name" get the field unit vector (first two
   // components) and the derivatives of its components with
   // respect to the mapped coordinates (next 4 components) at
   // this set of physical coordinates.  The numbering of the 6 components
   // is explained at the top of FieldData::getFieldUnitVectorFromMapping().

   field_data.getFieldUnitVectorFromMappingFile(physical_coordinates, field_unit_vector);

#endif

   // Write a file containing the field unit vector that
   // can be plotted with the plot_vectors.m script using
   // either MatLab or Octave.  The file name is "vectorsX",
   // where X is the block number.
   field_data.writeVectors(physical_coordinates, field_unit_vector);

#ifdef CH_MPI
   MPI_Finalize();
#endif

   return 0;
}
Beispiel #17
0
 EntityArray( const field_type & f , const Entity & e )
   : array_type( field_data( f , e ) ) {}
Beispiel #18
0
void Gear::mesh( stk::mesh::BulkData & M )
{
  stk::mesh::EntityRank element_rank = stk::topology::ELEMENT_RANK;
  stk::mesh::EntityRank side_rank = m_mesh_meta_data.side_rank();

  M.modification_begin();

  m_mesh = & M ;

  const unsigned p_size = M.parallel_size();
  const unsigned p_rank = M.parallel_rank();

  std::vector<size_t> counts ;
  stk::mesh::comm_mesh_counts(M, counts);

  // max_id is no longer available from comm_mesh_stats.
  // If we assume uniform numbering from 1.., then max_id
  // should be equal to counts...
  const stk::mesh::EntityId node_id_base = counts[ stk::topology::NODE_RANK ] + 1 ;
  const stk::mesh::EntityId elem_id_base = counts[ element_rank ] + 1 ;

  const unsigned long elem_id_gear_max =
    m_angle_num * ( m_rad_num - 1 ) * ( m_z_num - 1 );

  std::vector<stk::mesh::Part*> elem_parts ;
  std::vector<stk::mesh::Part*> face_parts ;
  std::vector<stk::mesh::Part*> node_parts ;

  {
    stk::mesh::Part * const p_gear = & m_gear ;
    stk::mesh::Part * const p_surf = & m_surf ;

    elem_parts.push_back( p_gear );
    face_parts.push_back( p_surf );
  }

  for ( unsigned ia = 0 ; ia < m_angle_num ; ++ia ) {
    for ( unsigned ir = 0 ; ir < m_rad_num - 1 ; ++ir ) {
      for ( unsigned iz = 0 ; iz < m_z_num - 1 ; ++iz ) {

        stk::mesh::EntityId elem_id_gear = identifier( m_z_num-1 , m_rad_num-1 , iz , ir , ia );

        if ( ( ( elem_id_gear * p_size ) / elem_id_gear_max ) == p_rank ) {

          stk::mesh::EntityId elem_id = elem_id_base + elem_id_gear ;

          // Create the node and set the model_coordinates

          const size_t ia_1 = ( ia + 1 ) % m_angle_num ;
          const size_t ir_1 = ir + 1 ;
          const size_t iz_1 = iz + 1 ;

          stk::mesh::Entity node[8] ;

          node[0] = create_node( node_parts, node_id_base, iz  , ir  , ia_1 );
          node[1] = create_node( node_parts, node_id_base, iz_1, ir  , ia_1 );
          node[2] = create_node( node_parts, node_id_base, iz_1, ir  , ia   );
          node[3] = create_node( node_parts, node_id_base, iz  , ir  , ia   );
          node[4] = create_node( node_parts, node_id_base, iz  , ir_1, ia_1 );
          node[5] = create_node( node_parts, node_id_base, iz_1, ir_1, ia_1 );
          node[6] = create_node( node_parts, node_id_base, iz_1, ir_1, ia   );
          node[7] = create_node( node_parts, node_id_base, iz  , ir_1, ia   );
#if 0 /* VERIFY_CENTROID */

          // Centroid of the element for verification

          const double TWO_PI = 2.0 * acos( -1.0 );
          const double angle = m_ang_inc * (0.5 + ia);
          const double z = m_center[2] + m_z_min + m_z_inc * (0.5 + iz);

          double c[3] = { 0 , 0 , 0 };

          for ( size_t j = 0 ; j < 8 ; ++j ) {
            double * const coord_data = field_data( m_model_coord , *node[j] );
            c[0] += coord_data[0] ;
            c[1] += coord_data[1] ;
            c[2] += coord_data[2] ;
          }
          c[0] /= 8 ; c[1] /= 8 ; c[2] /= 8 ;
          c[0] -= m_center[0] ;
          c[1] -= m_center[1] ;

          double val_a = atan2( c[1] , c[0] );
          if ( val_a < 0 ) { val_a += TWO_PI ; }
          const double err_a = angle - val_a ;
          const double err_z = z - c[2] ;

          const double eps = 100 * std::numeric_limits<double>::epsilon();

          if ( err_z < - eps || eps < err_z ||
               err_a < - eps || eps < err_a ) {
            std::string msg ;
            msg.append("problem setup element centroid error" );
            throw std::logic_error( msg );
          }
#endif

          stk::mesh::Entity elem =
            M.declare_entity( element_rank, elem_id, elem_parts );

          for ( size_t j = 0 ; j < 8 ; ++j ) {
            M.declare_relation( elem , node[j] ,
                                static_cast<unsigned>(j) );
          }
        }
      }
    }
  }

  // Array of faces on the surface

  {
    const size_t ir = m_rad_num - 1 ;

    for ( size_t ia = 0 ; ia < m_angle_num ; ++ia ) {
      for ( size_t iz = 0 ; iz < m_z_num - 1 ; ++iz ) {

        stk::mesh::EntityId elem_id_gear =
          identifier( m_z_num-1 , m_rad_num-1 , iz , ir-1 , ia );

        if ( ( ( elem_id_gear * p_size ) / elem_id_gear_max ) == p_rank ) {

          stk::mesh::EntityId elem_id = elem_id_base + elem_id_gear ;

          unsigned face_ord = 5 ;
          stk::mesh::EntityId face_id = elem_id * 10 + face_ord + 1;

          stk::mesh::Entity node[4] ;

          const size_t ia_1 = ( ia + 1 ) % m_angle_num ;
          const size_t iz_1 = iz + 1 ;

          node[0] = create_node( node_parts, node_id_base, iz  , ir  , ia_1 );
          node[1] = create_node( node_parts, node_id_base, iz_1, ir  , ia_1 );
          node[2] = create_node( node_parts, node_id_base, iz_1, ir  , ia   );
          node[3] = create_node( node_parts, node_id_base, iz  , ir  , ia   );

          stk::mesh::Entity face =
            M.declare_entity( side_rank, face_id, face_parts );

          for ( size_t j = 0 ; j < 4 ; ++j ) {
            M.declare_relation( face , node[j] ,
                                static_cast<unsigned>(j) );
          }

          stk::mesh::Entity elem = M.get_entity(element_rank, elem_id);

          M.declare_relation( elem , face , face_ord );
        }
      }
    }
  }
  M.modification_begin();
}
Beispiel #19
0
    void PyramidFixture::populate()
    {
      // Populate mesh with all node types

      m_bulkData.modification_begin();

      if (m_bulkData.parallel_rank() == 0)
        {
          stk_classic::mesh::EntityId curr_elem_id = 1;

          // For each element topology declare elements

          stk_classic::mesh::Entity *pyramids[2];
          for ( unsigned i = 0 ; i < number_pyramid ; ++i , ++curr_elem_id ) {
            pyramids[i] = &stk_classic::mesh::fem::declare_element( m_bulkData, m_block_pyramid, curr_elem_id, pyramid_node_ids[i] );
          }

          if (m_sideset_quad)
            {
              for ( unsigned i = 0 ; i < number_quad ; ++i , ++curr_elem_id ) {
                stk_classic::mesh::fem::declare_element_side( m_bulkData, 
                                                      curr_elem_id, //side_id,
                                                      *pyramids[i], // element,
                                                      4,            //j_side, // local_side_ord,
                                                      m_sideset_quad_subset);
              }
            }

          if (m_sideset_tri)
            {
              unsigned j_side=0;
              for ( unsigned i = 0 ; i < 3 ; ++i , ++curr_elem_id ) {
                if (i == 2) ++j_side;
                stk_classic::mesh::fem::declare_element_side( m_bulkData, 
                                                      curr_elem_id, //side_id,
                                                      *pyramids[0], // element,
                                                      j_side,            //j_side, // local_side_ord,
                                                      m_sideset_tri_subset);
                ++j_side;
              }
              j_side=1;
              for ( unsigned i = 0 ; i < 3 ; ++i , ++curr_elem_id ) {
                stk_classic::mesh::fem::declare_element_side( m_bulkData, 
                                                      curr_elem_id, //side_id,
                                                      *pyramids[1], // element,
                                                      j_side,            //j_side, // local_side_ord,
                                                      m_sideset_tri_subset);
                ++j_side;
              }
            }

          // For all nodes assign nodal coordinates
          for ( unsigned i = 0 ; i < node_count ; ++i ) {
            stk_classic::mesh::Entity * const node = m_bulkData.get_entity( stk_classic::mesh::fem::FEMMetaData::NODE_RANK , i + 1 );
            double * const coord = field_data( m_coordinates_field , *node );
            coord[0] = node_coord_data[i][0] ;
            coord[1] = node_coord_data[i][1] ;
            coord[2] = node_coord_data[i][2] ;
          }

        }
      m_bulkData.modification_end();

    }
Beispiel #20
0
void communicate_field_data(
  ParallelMachine machine,
  const std::vector<EntityProc> & domain ,
  const std::vector<EntityProc> & range ,
  const std::vector<const FieldBase *> & fields)
{
  if ( fields.empty() ) { return; }

  const unsigned parallel_size = parallel_machine_size( machine );
  const unsigned parallel_rank = parallel_machine_rank( machine );
  const bool     asymmetric    = & domain != & range ;

  const std::vector<const FieldBase *>::const_iterator fe = fields.end();
  const std::vector<const FieldBase *>::const_iterator fb = fields.begin();
        std::vector<const FieldBase *>::const_iterator fi ;

  // Sizing for send and receive

  const unsigned zero = 0 ;
  std::vector<unsigned> send_size( parallel_size , zero );
  std::vector<unsigned> recv_size( parallel_size , zero );

  std::vector<EntityProc>::const_iterator i ;

  for ( i = domain.begin() ; i != domain.end() ; ++i ) {
    Entity       & e = * i->first ;
    const unsigned p = i->second ;

    if ( asymmetric || parallel_rank == e.owner_rank() ) {
      unsigned e_size = 0 ;
      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        e_size += field_data_size( f , e );
      }
      send_size[ p ] += e_size ;
    }
  }

  for ( i = range.begin() ; i != range.end() ; ++i ) {
    Entity       & e = * i->first ;
    const unsigned p = i->second ;

    if ( asymmetric || p == e.owner_rank() ) {
      unsigned e_size = 0 ;
      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        e_size += field_data_size( f , e );
      }
      recv_size[ p ] += e_size ;
    }
  }

  // Allocate send and receive buffers:

  CommAll sparse ;

  {
    const unsigned * const s_size = & send_size[0] ;
    const unsigned * const r_size = & recv_size[0] ;
    sparse.allocate_buffers( machine, parallel_size / 4 , s_size, r_size);
  }

  // Pack for send:

  for ( i = domain.begin() ; i != domain.end() ; ++i ) {
    Entity       & e = * i->first ;
    const unsigned p = i->second ;

    if ( asymmetric || parallel_rank == e.owner_rank() ) {
      CommBuffer & b = sparse.send_buffer( p );
      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        const unsigned size = field_data_size( f , e );
        if ( size ) {
          unsigned char * ptr = reinterpret_cast<unsigned char *>(field_data( f , e ));
          b.pack<unsigned char>( ptr , size );
        }
      }
    }
  }

  // Communicate:

  sparse.communicate();

  // Unpack for recv:

  for ( i = range.begin() ; i != range.end() ; ++i ) {
    Entity       & e = * i->first ;
    const unsigned p = i->second ;

    if ( asymmetric || p == e.owner_rank() ) {
      CommBuffer & b = sparse.recv_buffer( p );
      for ( fi = fb ; fi != fe ; ++fi ) {
        const FieldBase & f = **fi ;
        const unsigned size = field_data_size( f , e );
        if ( size ) {
          unsigned char * ptr = reinterpret_cast<unsigned char *>(field_data( f , e ));
          b.unpack<unsigned char>( ptr , size );
        }
      }
    }
  }
}
void use_case_5_generate_mesh(
  const std::string& mesh_options ,
  stk::mesh::BulkData & mesh ,
  const VectorFieldType & node_coord ,
  stk::mesh::Part & hex_block ,
  stk::mesh::Part & quad_shell_block )
{
  mesh.modification_begin();

  const unsigned parallel_size = mesh.parallel_size();
  const unsigned parallel_rank = mesh.parallel_rank();

  double t = 0 ;
  size_t num_hex = 0 ;
  size_t num_shell = 0 ;
  size_t num_nodes = 0 ;
  size_t num_block = 0 ;
  int error_flag = 0 ;

  try {

    Iogn::GeneratedMesh gmesh( mesh_options, parallel_size, parallel_rank );

    num_nodes = gmesh.node_count_proc();
    num_block = gmesh.block_count();

    t = stk::wall_time();

    std::vector<int> node_map( num_nodes , 0 );

    gmesh.node_map( node_map );

    {

      for ( size_t i = 1 ; i <= num_block ; ++i ) {
        const size_t                        num_elem = gmesh.element_count_proc(i);
        const std::pair<std::string,int> top_info = gmesh.topology_type(i);

	std::vector<int> elem_map( num_elem , 0 );
        std::vector<int> elem_conn( num_elem * top_info.second );

	gmesh.element_map( i, elem_map );
        gmesh.connectivity( i , elem_conn );

        if ( top_info.second == 8 ) {

          for ( size_t j = 0 ; j < num_elem ; ++j ) {

            const int * const local_node_id = & elem_conn[ j * 8 ] ;

            const stk::mesh::EntityId node_id[8] = {
              local_node_id[0] ,
              local_node_id[1] ,
              local_node_id[2] ,
              local_node_id[3] ,
              local_node_id[4] ,
              local_node_id[5] ,
              local_node_id[6] ,
              local_node_id[7]
            };

            const stk::mesh::EntityId elem_id = elem_map[ j ];

            stk::mesh::fem::declare_element( mesh , hex_block , elem_id , node_id );

            ++num_hex ;
          }
        }
        else if ( top_info.second == 4 ) {

          for ( size_t j = 0 ; j < num_elem ; ++j ) {

            const int * const local_node_id = & elem_conn[ j * 4 ] ;

            const stk::mesh::EntityId node_id[4] = {
              local_node_id[0] ,
              local_node_id[1] ,
              local_node_id[2] ,
              local_node_id[3]
            };

            const stk::mesh::EntityId elem_id = elem_map[ j ];

            stk::mesh::fem::declare_element( mesh , quad_shell_block , elem_id , node_id );

            ++num_shell ;
          }
        }
      }
    }

    std::vector<double> node_coordinates( 3 * node_map.size() );

    gmesh.coordinates( node_coordinates );

    if ( 3 * node_map.size() != node_coordinates.size() ) {
      std::ostringstream msg ;
      msg << "  P" << mesh.parallel_rank()
          << ": ERROR, node_map.size() = "
          << node_map.size()
          << " , node_coordinates.size() / 3 = "
          << ( node_coordinates.size() / 3 );
      throw std::runtime_error( msg.str() );
    }

    for ( unsigned i = 0 ; i < node_map.size() ; ++i ) {
      const unsigned i3 = i * 3 ;

      stk::mesh::Entity * const node = mesh.get_entity( stk::mesh::fem::FEMMetaData::NODE_RANK , node_map[i] );

      if ( NULL == node ) {
        std::ostringstream msg ;
        msg << "  P:" << mesh.parallel_rank()
            << " ERROR, Node not found: "
            << node_map[i] << " = node_map[" << i << "]" ;
        throw std::runtime_error( msg.str() );
      }

      double * const data = field_data( node_coord , *node );
      data[0] = node_coordinates[ i3 + 0 ];
      data[1] = node_coordinates[ i3 + 1 ];
      data[2] = node_coordinates[ i3 + 2 ];
    }
  }
  catch ( const std::exception & X ) {
    std::cout << "  P:" << mesh.parallel_rank() << ": " << X.what()
              << std::endl ;
    std::cout.flush();
    error_flag = 1 ;
  }
  catch( ... ) {
    std::cout << "  P:" << mesh.parallel_rank()
              << " Caught unknown exception"
              << std::endl ;
    std::cout.flush();
    error_flag = 1 ;
  }

  stk::all_reduce( mesh.parallel() , stk::ReduceMax<1>( & error_flag ) );

  if ( error_flag ) {
    std::string msg( "Failed mesh generation" );
    throw std::runtime_error( msg );
  }

  mesh.modification_end();

  double dt = stk::wall_dtime( t );

  stk::all_reduce( mesh.parallel() , stk::ReduceMax<1>( & dt ) );

  std::cout << "  P" << mesh.parallel_rank()
            << ": Meshed Hex = " << num_hex
            << " , Shell = " << num_shell
            << " , Node = " << num_nodes
            << " in " << dt << " sec"
            << std::endl ;
  std::cout.flush();
}
inline void copy_from_owned(
  const BulkData                        & mesh ,
  const std::vector< const FieldBase *> & fields )
{
  if ( fields.empty() ) { return; }

  const int parallel_size = mesh.parallel_size();
  const int parallel_rank = mesh.parallel_rank();

  const std::vector<const FieldBase *>::const_iterator fe = fields.end();
  const std::vector<const FieldBase *>::const_iterator fb = fields.begin();
        std::vector<const FieldBase *>::const_iterator fi ;

  std::vector<std::vector<unsigned char> > send_data(parallel_size);
  std::vector<std::vector<unsigned char> > recv_data(parallel_size);

  const EntityCommListInfoVector &comm_info_vec = mesh.internal_comm_list();
  size_t comm_info_vec_size = comm_info_vec.size();

  std::vector<unsigned> send_sizes(parallel_size, 0);
  std::vector<unsigned> recv_sizes(parallel_size, 0);

  //this first loop calculates send_sizes and recv_sizes.
  for(fi = fb; fi != fe; ++fi)
  {
      const FieldBase & f = **fi;
      for(size_t i = 0; i<comm_info_vec_size; ++i)
      {
          const Bucket* bucket = comm_info_vec[i].bucket;

          int owner = comm_info_vec[i].owner;
          const bool owned = (owner == parallel_rank);

          unsigned e_size = 0;

          if(is_matching_rank(f, *bucket))
          {
              const unsigned bucketId = bucket->bucket_id();
              unsigned size = field_bytes_per_entity(f, bucketId);
              e_size += size;
          }

          if(e_size == 0)
          {
              continue;
          }

          if(owned)
          {
              const EntityCommInfoVector& infovec = comm_info_vec[i].entity_comm->comm_map;
              size_t infovec_size = infovec.size();
              for(size_t j=0; j<infovec_size; ++j)
              {
                  int proc = infovec[j].proc;

                  send_sizes[proc] += e_size;
              }
          }
          else
          {
              recv_sizes[owner] += e_size;
          }
      }
  }

  //now size the send_data buffers
  size_t max_len = 0;
  for(int p=0; p<parallel_size; ++p)
  {
      if (send_sizes[p] > 0)
      {
          if (send_sizes[p] > max_len)
          {
              max_len = send_sizes[p];
          }
          send_data[p].resize(send_sizes[p]);
          send_sizes[p] = 0;
      }
  }

  //now pack the send buffers
  std::vector<unsigned char> field_data(max_len);
  unsigned char* field_data_ptr = field_data.data();

  for(fi = fb; fi != fe; ++fi)
  {
      const FieldBase & f = **fi;
      for(size_t i = 0; i<comm_info_vec_size; ++i)
      {
          const Bucket* bucket = comm_info_vec[i].bucket;

          int owner = comm_info_vec[i].owner;
          const bool owned = (owner == parallel_rank);

          unsigned e_size = 0;

          if(is_matching_rank(f, *bucket))
          {
              const unsigned bucketId = bucket->bucket_id();
              unsigned size = field_bytes_per_entity(f, bucketId);
              if (owned && size > 0)
              {
                  unsigned char * ptr = reinterpret_cast<unsigned char*>(stk::mesh::field_data(f, bucketId, comm_info_vec[i].bucket_ordinal, size));
                  std::memcpy(field_data_ptr+e_size, ptr, size);
 //                 field_data.insert(field_data.end(), ptr, ptr+size);
              }
              e_size += size;
          }

          if(e_size == 0)
          {
              continue;
          }

          if(owned)
          {
              const EntityCommInfoVector& infovec = comm_info_vec[i].entity_comm->comm_map;
              size_t infovec_size = infovec.size();
              for(size_t j=0; j<infovec_size; ++j)
              {
                  int proc = infovec[j].proc;

                  unsigned char* dest_ptr = send_data[proc].data()+send_sizes[proc];
                  unsigned char* src_ptr = field_data_ptr;
                  std::memcpy(dest_ptr, src_ptr, e_size);
                  send_sizes[proc] += e_size;
     //             send_data[proc].insert(send_data[proc].end(), field_data.begin(), field_data.end());
              }
          }
          else
          {
              recv_sizes[owner] += e_size;
          }
      }
  }

  for(int p=0; p<parallel_size; ++p)
  {
      if (recv_sizes[p] > 0)
      {
          recv_data[p].resize(recv_sizes[p]);
          recv_sizes[p] = 0;
      }
  }

  parallel_data_exchange_nonsym_known_sizes_t(send_data, recv_data, mesh.parallel());

  //now unpack and store the recvd data
  for(fi = fb; fi != fe; ++fi)
  {
      const FieldBase & f = **fi;

      for(size_t i=0; i<comm_info_vec_size; ++i)
      {
          int owner = comm_info_vec[i].owner;
          const bool owned = (owner == parallel_rank);

          if(owned || recv_data[owner].size() == 0)
          {
              continue;
          }

          const Bucket* bucket = comm_info_vec[i].bucket;

          if(is_matching_rank(f, *bucket))
          {
              const unsigned bucketId = bucket->bucket_id();
              unsigned size = field_bytes_per_entity(f, bucketId);
              if (size > 0)
              {
                  unsigned char * ptr = reinterpret_cast<unsigned char*>(stk::mesh::field_data(f, bucketId, comm_info_vec[i].bucket_ordinal, size));

                  std::memcpy(ptr, &(recv_data[owner][recv_sizes[owner]]), size);
//                for(unsigned j = 0; j < size; ++j)
//                {
//                    ptr[j] = recv_data[owner][recv_sizes[owner]+j];
//                }
                  recv_sizes[owner] += size;
              }
          }
      }
  }
}