Sierra Toolkit Version of the Day
io_example.cpp
00001 /*------------------------------------------------------------------------*/
00002 /*                 Copyright 2010, 2011 Sandia Corporation.                     */
00003 /*  Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive   */
00004 /*  license for use of this work by or on behalf of the U.S. Government.  */
00005 /*  Export of this program may require a license from the                 */
00006 /*  United States Government.                                             */
00007 /*------------------------------------------------------------------------*/
00008 
00009 #include <iostream>
00010 #include <assert.h>
00011 
00012 #include <stk_util/parallel/Parallel.hpp>
00013 #include <init/Ionit_Initializer.h>
00014 #include <Ioss_SubSystem.h>
00015 
00016 #include <stk_mesh/base/Field.hpp>
00017 #include <stk_mesh/base/FieldData.hpp>
00018 #include <stk_mesh/base/BulkData.hpp>
00019 
00020 #include <stk_mesh/fem/FEMMetaData.hpp>
00021 #include <stk_mesh/fem/TopologyDimensions.hpp>
00022 
00023 #include <stk_io/IossBridge.hpp>
00024 
00041 namespace stk_example_io {
00042 
00046   void process_nodeblocks    (Ioss::Region &region, stk::mesh::MetaData &meta);
00047 
00053   void process_elementblocks (Ioss::Region &region, stk::mesh::MetaData &meta);
00054 
00061   void process_nodesets      (Ioss::Region &region, stk::mesh::MetaData &meta);
00062 
00076   void process_sidesets      (Ioss::Region &region, stk::mesh::MetaData &meta);
00077 
00084   void process_nodeblocks    (Ioss::Region &region, stk::mesh::BulkData &bulk);
00085 
00097   void process_elementblocks (Ioss::Region &region, stk::mesh::BulkData &bulk);
00098 
00105   void process_nodesets      (Ioss::Region &region, stk::mesh::BulkData &bulk);
00106 
00112   void process_sidesets      (Ioss::Region &region, stk::mesh::BulkData &bulk);
00113 
00120   void process_input_request (Ioss::Region &region, stk::mesh::BulkData &bulk, int step);
00121 
00134   void process_output_request(Ioss::Region &region, stk::mesh::BulkData &bulk, int step);
00135 
00162   void io_example( stk::ParallelMachine comm,
00163        const std::string& in_filename,
00164        const std::string& out_filename)
00165   {
00166     // Initialize IO system.  Registers all element types and storage
00167     // types and the exodusII default database type.
00168     Ioss::Init::Initializer init_db;
00169 
00170     std::cout << "========================================================================\n"
00171         << " Use Case: Subsetting with df and attribute field input/output          \n"
00172         << "========================================================================\n";
00173 
00174     std::string dbtype("exodusII");
00175     Ioss::DatabaseIO *dbi = Ioss::IOFactory::create(dbtype, in_filename, Ioss::READ_MODEL,
00176                 comm);
00177     if (dbi == NULL || !dbi->ok()) {
00178       std::cerr  << "ERROR: Could not open database '" << in_filename
00179      << "' of type '" << dbtype << "'\n";
00180       std::exit(EXIT_FAILURE);
00181     }
00182 
00183     // NOTE: 'in_region' owns 'dbi' pointer at this time...
00184     Ioss::Region in_region(dbi, "input_model");
00185 
00186     // SUBSETTING PARSING/PREPROCESSING...
00187     // Just an example of how application could control whether an
00188     // entity is subsetted or not...
00189 
00190 
00191     // Example command line in current code corresponding to behavior below:
00192     std::cout << "\nWhen processing file multi-block.g for use case 2, the blocks below will be omitted:\n";
00193     std::cout << "\tOMIT BLOCK Cblock Eblock I1 I2\n\n";
00194     Ioss::ElementBlock *eb = in_region.get_element_block("cblock");
00195     if (eb != NULL)
00196       eb->property_add(Ioss::Property(std::string("omitted"), 1));
00197 
00198     eb = in_region.get_element_block("eblock");
00199     if (eb != NULL)
00200       eb->property_add(Ioss::Property(std::string("omitted"), 1));
00201 
00202     eb = in_region.get_element_block("i1");
00203     if (eb != NULL)
00204       eb->property_add(Ioss::Property(std::string("omitted"), 1));
00205 
00206     eb = in_region.get_element_block("i2");
00207     if (eb != NULL)
00208       eb->property_add(Ioss::Property(std::string("omitted"), 1));
00209 
00210 #if 0
00211     // Example for subsetting -- omit "odd" blocks
00212     if (entity->type() == Ioss::ELEMENTBLOCK) {
00213       int id = entity->get_property("id").get_int();
00214       if (id % 2) {
00215   entity->property_add(Ioss::Property(std::string("omitted"), 1));
00216   std::cout << "Skipping " << entity->type_string() << ": "  << entity->name() << "\n";
00217       }
00218     }
00219 #endif
00220 
00221     //----------------------------------
00222     // Process Entity Types. Subsetting is possible.
00223 
00224     static size_t spatial_dimension = in_region.get_property("spatial_dimension").get_int();
00225 
00226     stk::mesh::fem::FEMMetaData fem_meta_data( spatial_dimension );
00227     stk::mesh::MetaData &meta_data = fem_meta_data.get_meta_data(fem_meta_data);
00228     process_elementblocks(in_region, meta_data);
00229     process_nodeblocks(in_region,    meta_data);
00230     process_sidesets(in_region,      meta_data);
00231     process_nodesets(in_region,      meta_data);
00232 
00233     //----------------------------------
00234     // Done populating meta data, commit and create bulk data
00235     meta_data.commit();
00236 
00237     //----------------------------------
00238     // Process Bulkdata for all Entity Types. Subsetting is possible.
00239     stk::mesh::BulkData bulk_data(meta_data, comm);
00240 
00241     bulk_data.modification_begin();
00242     process_elementblocks(in_region, bulk_data);
00243     process_nodeblocks(in_region,    bulk_data);
00244     process_sidesets(in_region,      bulk_data);
00245     process_nodesets(in_region,      bulk_data);
00246     bulk_data.modification_end();
00247 
00248     //----------------------------------
00249     // OUTPUT...Create the output "mesh" portion
00250 
00251     Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(dbtype, out_filename,
00252                 Ioss::WRITE_RESULTS,
00253                 comm);
00254     if (dbo == NULL || !dbo->ok()) {
00255       std::cerr << "ERROR: Could not open results database '" << out_filename
00256     << "' of type '" << dbtype << "'\n";
00257       std::exit(EXIT_FAILURE);
00258     }
00259 
00260     // NOTE: 'out_region' owns 'dbo' pointer at this time...
00261     Ioss::Region out_region(dbo, "results_output");
00262 
00263     stk::io::define_output_db(out_region, bulk_data, &in_region);
00264     stk::io::write_output_db(out_region,  bulk_data);
00265 
00266     // ------------------------------------------------------------------------
00281     out_region.begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
00282 
00283     // Special processing for nodeblock (all nodes in model)...
00284     stk::io::ioss_add_fields(meta_data.universal_part(), stk::mesh::fem::FEMMetaData::NODE_RANK,
00285            out_region.get_node_blocks()[0],
00286            Ioss::Field::TRANSIENT);
00287 
00288     const stk::mesh::PartVector & all_parts = meta_data.get_parts();
00289     for ( stk::mesh::PartVector::const_iterator
00290       ip = all_parts.begin(); ip != all_parts.end(); ++ip ) {
00291 
00292       stk::mesh::Part * const part = *ip;
00293 
00294       const stk::mesh::EntityRank part_rank = part->primary_entity_rank();
00295 
00296       // Check whether this part should be output to results database.
00297       if (stk::io::is_part_io_part(*part)) {
00298   // Get Ioss::GroupingEntity corresponding to this part...
00299   Ioss::GroupingEntity *entity = out_region.get_entity(part->name());
00300   if (entity != NULL) {
00301     if (entity->type() == Ioss::SIDESET) {
00302       Ioss::SideSet *sset = dynamic_cast<Ioss::SideSet*>(entity);
00303       assert(sset != NULL);
00304       int block_count = sset->block_count();
00305       for (int i=0; i < block_count; i++) {
00306         Ioss::SideBlock *fb = sset->get_block(i);
00307         stk::io::ioss_add_fields(*part, part_rank,
00308                fb, Ioss::Field::TRANSIENT);
00309       }
00310     } else {
00311       stk::io::ioss_add_fields(*part, part_rank,
00312              entity, Ioss::Field::TRANSIENT);
00313     }
00314   } else {
00317   }
00318       }
00319     }
00320     out_region.end_mode(Ioss::STATE_DEFINE_TRANSIENT);
00321     // ------------------------------------------------------------------------
00322 
00323     // Read and Write transient fields...
00324     out_region.begin_mode(Ioss::STATE_TRANSIENT);
00325     int timestep_count = in_region.get_property("state_count").get_int();
00326     for (int step = 1; step <= timestep_count; step++) {
00327       double time = in_region.get_state_time(step);
00328 
00329       // Read data from the io input mesh database into stk::mesh fields...
00330       process_input_request(in_region, bulk_data, step);
00331 
00332       // execute()
00333 
00334       // Write data from the stk::mesh fields out to the output database.a
00335       int out_step = out_region.add_state(time);
00336       process_output_request(out_region, bulk_data, out_step);
00337     }
00338     out_region.end_mode(Ioss::STATE_TRANSIENT);
00339   }
00340 
00341   // ========================================================================
00342   void process_nodeblocks(Ioss::Region &region, stk::mesh::MetaData &meta)
00343   {
00344     const Ioss::NodeBlockContainer& node_blocks = region.get_node_blocks();
00345     assert(node_blocks.size() == 1);
00346 
00347     Ioss::NodeBlock *nb = node_blocks[0];
00348 
00349     assert(nb->field_exists("mesh_model_coordinates"));
00350     Ioss::Field coordinates = nb->get_field("mesh_model_coordinates");
00351     int spatial_dim = coordinates.transformed_storage()->component_count();
00352 
00353     stk::mesh::Field<double,stk::mesh::Cartesian> & coord_field =
00354       meta.declare_field<stk::mesh::Field<double,stk::mesh::Cartesian> >("coordinates");
00355 
00356     stk::mesh::put_field( coord_field, stk::mesh::fem::FEMMetaData::NODE_RANK, meta.universal_part(),
00357                           spatial_dim);
00358 
00363     stk::io::define_io_fields(nb, Ioss::Field::TRANSIENT, meta.universal_part(),stk::mesh::fem::FEMMetaData::NODE_RANK);
00364   }
00365 
00366   // ========================================================================
00367   void process_elementblocks(Ioss::Region &region, stk::mesh::MetaData &meta)
00368   {
00369     const stk::mesh::fem::FEMMetaData &fem_meta_data = stk::mesh::fem::FEMMetaData::get(meta);
00370     const stk::mesh::EntityRank element_rank = fem_meta_data.element_rank();
00371 
00372     const Ioss::ElementBlockContainer& elem_blocks = region.get_element_blocks();
00373     stk::io::default_part_processing(elem_blocks, meta, element_rank);
00374 
00375     // Parts were created above, now handle element block specific
00376     // information (topology, attributes, ...);
00377     for(Ioss::ElementBlockContainer::const_iterator it = elem_blocks.begin();
00378   it != elem_blocks.end(); ++it) {
00379       Ioss::ElementBlock *entity = *it;
00380 
00381       if (stk::io::include_entity(entity)) {
00382   stk::mesh::Part* const part = meta.get_part(entity->name());
00383   assert(part != NULL);
00384 
00385       const stk::mesh::EntityRank part_rank = part->primary_entity_rank();
00386 
00387   // Element Block attributes (if any)...
00392   stk::io::define_io_fields(entity, Ioss::Field::ATTRIBUTE,
00393           *part,
00394                                   part_rank);
00395 
00400   stk::io::define_io_fields(entity, Ioss::Field::TRANSIENT,
00401           *part,
00402                                   part_rank);
00403 
00404   const CellTopologyData* cell_topo = fem_meta_data.get_cell_topology(*part).getCellTopologyData();
00405   std::string cell_topo_name = "UNKNOWN";
00406   if (cell_topo != NULL)
00407     cell_topo_name = cell_topo->name;
00408 
00409   std::cout << entity->type_string() << ": " << entity->name()
00410       << " , celltop = " << cell_topo_name
00411       << std::endl ;
00412       }
00413     }
00414   }
00415 
00416   // ========================================================================
00417   void process_nodesets(Ioss::Region &region, stk::mesh::MetaData &meta)
00418   {
00419     const Ioss::NodeSetContainer& node_sets = region.get_nodesets();
00420     stk::io::default_part_processing(node_sets, meta, stk::mesh::fem::FEMMetaData::NODE_RANK);
00421 
00426     stk::mesh::Field<double> & distribution_factors_field =
00427       meta.declare_field<stk::mesh::Field<double> >("distribution_factors");
00428 
00434     for(Ioss::NodeSetContainer::const_iterator it = node_sets.begin();
00435   it != node_sets.end(); ++it) {
00436       Ioss::NodeSet *entity = *it;
00437 
00438       if (stk::io::include_entity(entity)) {
00439   stk::mesh::Part* const part = meta.get_part(entity->name());
00440   assert(part != NULL);
00441   assert(entity->field_exists("distribution_factors"));
00442 
00443   stk::mesh::put_field(distribution_factors_field, stk::mesh::fem::FEMMetaData::NODE_RANK, *part);
00444 
00449   stk::io::define_io_fields(entity, Ioss::Field::TRANSIENT,
00450           *part,
00451                                   part->primary_entity_rank() ) ;
00452       }
00453     }
00454   }
00455 
00456   // ========================================================================
00457   void process_surface_entity(Ioss::SideSet *sset, stk::mesh::MetaData &meta,
00458             stk::mesh::EntityRank sset_rank)
00459   {
00460     assert(sset->type() == Ioss::SIDESET);
00461     Ioss::SideSet *fs = dynamic_cast<Ioss::SideSet *>(sset);
00462     assert(fs != NULL);
00463     const Ioss::SideBlockContainer& blocks = fs->get_side_blocks();
00464     stk::io::default_part_processing(blocks, meta, sset_rank);
00465 
00466     stk::mesh::Part* const fs_part = meta.get_part(sset->name());
00467     assert(fs_part != NULL);
00468 
00469     stk::mesh::Field<double, stk::mesh::ElementNode> *distribution_factors_field = NULL;
00470     bool surface_df_defined = false; // Has the surface df field been defined yet?
00471 
00472 
00473     int block_count = sset->block_count();
00474     for (int i=0; i < block_count; i++) {
00475       Ioss::SideBlock *side_block = sset->get_block(i);
00476       if (stk::io::include_entity(side_block)) {
00477   std::cout << side_block->type_string() << " " << side_block->name() << "\n";
00478   stk::mesh::Part * const side_block_part = meta.get_part(side_block->name());
00479   assert(side_block_part != NULL);
00480   meta.declare_part_subset(*fs_part, *side_block_part);
00481 
00482         const stk::mesh::EntityRank part_rank = side_block_part->primary_entity_rank();
00483 
00484   if (side_block->field_exists("distribution_factors")) {
00485     if (!surface_df_defined) {
00486       std::string field_name = sset->name() + "_distribution_factors";
00487       distribution_factors_field =
00488         &meta.declare_field<stk::mesh::Field<double, stk::mesh::ElementNode> >(field_name);
00489       stk::io::set_distribution_factor_field(*fs_part, *distribution_factors_field);
00490       surface_df_defined = true;
00491     }
00492     stk::io::set_distribution_factor_field(*side_block_part, *distribution_factors_field);
00493     int side_node_count = side_block->topology()->number_nodes();
00494     stk::mesh::put_field(*distribution_factors_field,
00495                                part_rank,
00496                                *side_block_part, side_node_count);
00497   }
00498 
00503   stk::io::define_io_fields(side_block, Ioss::Field::TRANSIENT,
00504           *side_block_part,
00505                                   part_rank);
00506       }
00507     }
00508   }
00509 
00510   // ========================================================================
00511   void process_sidesets(Ioss::Region &region, stk::mesh::MetaData &meta)
00512   {
00513     stk::mesh::fem::FEMMetaData &fem = stk::mesh::fem::FEMMetaData::get(meta);
00514     const stk::mesh::EntityRank side_rank = fem.side_rank();
00515 
00516     const Ioss::SideSetContainer& side_sets = region.get_sidesets();
00517     stk::io::default_part_processing(side_sets, meta, side_rank);
00518 
00519     for(Ioss::SideSetContainer::const_iterator it = side_sets.begin();
00520   it != side_sets.end(); ++it) {
00521       Ioss::SideSet *entity = *it;
00522 
00523       if (stk::io::include_entity(entity)) {
00524   process_surface_entity(entity, meta, side_rank);
00525       }
00526     }
00527   }
00528 
00529   // ========================================================================
00530   // Bulk Data
00531   // ========================================================================
00532   void process_nodeblocks(Ioss::Region &region, stk::mesh::BulkData &bulk)
00533   {
00534     // This must be called after the "process_element_blocks" call
00535     // since there may be nodes that exist in the database that are
00536     // not part of the analysis mesh due to subsetting of the element
00537     // blocks.
00538 
00539     const Ioss::NodeBlockContainer& node_blocks = region.get_node_blocks();
00540     assert(node_blocks.size() == 1);
00541 
00542     Ioss::NodeBlock *nb = node_blocks[0];
00543 
00544     std::vector<stk::mesh::Entity*> nodes;
00545     stk::io::get_entity_list(nb, stk::mesh::fem::FEMMetaData::NODE_RANK, bulk, nodes);
00546 
00551     const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00552     stk::mesh::Field<double,stk::mesh::Cartesian> *coord_field =
00553       meta.get_field<stk::mesh::Field<double,stk::mesh::Cartesian> >("coordinates");
00554 
00555     stk::io::field_data_from_ioss(coord_field, nodes, nb, "mesh_model_coordinates");
00556   }
00557 
00558   // ========================================================================
00559   void process_elementblocks(Ioss::Region &region, stk::mesh::BulkData &bulk)
00560   {
00561     const Ioss::ElementBlockContainer& elem_blocks = region.get_element_blocks();
00562 
00563     for(Ioss::ElementBlockContainer::const_iterator it = elem_blocks.begin();
00564   it != elem_blocks.end(); ++it) {
00565       Ioss::ElementBlock *entity = *it;
00566 
00567       if (stk::io::include_entity(entity)) {
00568   const std::string &name = entity->name();
00569         const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00570         const stk::mesh::fem::FEMMetaData &fem_meta_data = stk::mesh::fem::FEMMetaData::get(meta);
00571   stk::mesh::Part* const part = meta.get_part(name);
00572   assert(part != NULL);
00573 
00574   const CellTopologyData* cell_topo = fem_meta_data.get_cell_topology(*part).getCellTopologyData();
00575   if (cell_topo == NULL) {
00576           std::ostringstream msg ;
00577     msg << " INTERNAL_ERROR: Part " << part->name() << " returned NULL from get_cell_topology()";
00578     throw std::runtime_error( msg.str() );
00579   }
00580 
00581   std::vector<int> elem_ids ;
00582   std::vector<int> connectivity ;
00583   std::vector<stk::mesh::EntityId> connectivity2 ;
00584 
00585   entity->get_field_data("ids", elem_ids);
00586   entity->get_field_data("connectivity", connectivity);
00587         connectivity2.reserve(connectivity.size());
00588         std::copy(connectivity.begin(), connectivity.end(), std::back_inserter(connectivity2));
00589 
00590   size_t element_count = elem_ids.size();
00591   int nodes_per_elem = cell_topo->node_count ;
00592 
00593   std::vector<stk::mesh::Entity*> elements(element_count);
00594   for(size_t i=0; i<element_count; ++i) {
00595     stk::mesh::EntityId *conn = &connectivity2[i*nodes_per_elem];
00596     elements[i] = &stk::mesh::fem::declare_element(bulk, *part, elem_ids[i], conn);
00597   }
00598 
00599   // For this example, we are just taking all attribute fields
00600   // found on the io database and populating fields on the
00601   // corresponding mesh part.  In practice, would probably be
00602   // selective about which attributes to use...
00603   Ioss::NameList names;
00604   entity->field_describe(Ioss::Field::ATTRIBUTE, &names);
00605   for (Ioss::NameList::const_iterator I = names.begin(); I != names.end(); ++I) {
00606     if (*I == "attribute" && names.size() > 1)
00607       continue;
00608     stk::mesh::FieldBase *field = meta.get_field<stk::mesh::FieldBase>(*I);
00609     stk::io::field_data_from_ioss(field, elements, entity, *I);
00610 
00611   }
00612       }
00613     }
00614   }
00615 
00616   // ========================================================================
00617   void process_nodesets(Ioss::Region &region, stk::mesh::BulkData &bulk)
00618   {
00619     // Should only process nodes that have already been defined via the element
00620     // blocks connectivity lists.
00621     const Ioss::NodeSetContainer& node_sets = region.get_nodesets();
00622 
00623     for(Ioss::NodeSetContainer::const_iterator it = node_sets.begin();
00624   it != node_sets.end(); ++it) {
00625       Ioss::NodeSet *entity = *it;
00626 
00627       if (stk::io::include_entity(entity)) {
00628   const std::string & name = entity->name();
00629   const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00630   stk::mesh::Part* const part = meta.get_part(name);
00631   assert(part != NULL);
00632   stk::mesh::PartVector add_parts( 1 , part );
00633 
00634   std::vector<int> node_ids ;
00635   int node_count = entity->get_field_data("ids", node_ids);
00636 
00637   std::vector<stk::mesh::Entity*> nodes(node_count);
00638   for(int i=0; i<node_count; ++i) {
00639     nodes[i] = bulk.get_entity( stk::mesh::fem::FEMMetaData::NODE_RANK, node_ids[i] );
00640     if (nodes[i] != NULL)
00641       bulk.declare_entity(stk::mesh::fem::FEMMetaData::NODE_RANK, node_ids[i], add_parts );
00642   }
00643 
00648   stk::mesh::Field<double> *df_field =
00649     meta.get_field<stk::mesh::Field<double> >("distribution_factors");
00650 
00651   if (df_field != NULL) {
00652     stk::io::field_data_from_ioss(df_field, nodes, entity, "distribution_factors");
00653   }
00654       }
00655     }
00656   }
00657 
00658   // ========================================================================
00659   void process_surface_entity(const Ioss::SideSet* sset ,
00660             stk::mesh::BulkData & bulk)
00661   {
00662     assert(sset->type() == Ioss::SIDESET);
00663 
00664     const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00665     const stk::mesh::fem::FEMMetaData &fem_meta_data = stk::mesh::fem::FEMMetaData::get(meta);
00666     const stk::mesh::EntityRank element_rank = fem_meta_data.element_rank();
00667 
00668     int block_count = sset->block_count();
00669     for (int i=0; i < block_count; i++) {
00670       Ioss::SideBlock *block = sset->get_block(i);
00671       if (stk::io::include_entity(block)) {
00672   std::vector<int> side_ids ;
00673   std::vector<int> elem_side ;
00674 
00675   stk::mesh::Part * const side_block_part = meta.get_part(block->name());
00676 
00677   block->get_field_data("ids", side_ids);
00678   block->get_field_data("element_side", elem_side);
00679 
00680   assert(side_ids.size() * 2 == elem_side.size());
00681   stk::mesh::PartVector add_parts( 1 , side_block_part );
00682 
00683   size_t side_count = side_ids.size();
00684   std::vector<stk::mesh::Entity*> sides(side_count);
00685   for(size_t is=0; is<side_count; ++is) {
00686 
00687     stk::mesh::Entity* const elem = bulk.get_entity(element_rank, elem_side[is*2]);
00688     // If NULL, then the element was probably assigned to an
00689     // Ioss uses 1-based side ordinal, stk::mesh uses 0-based.
00690     // Hence the '-1' in the following line.
00691     int side_ordinal = elem_side[is*2+1] - 1 ;
00692 
00693     // element block that appears in the database, but was
00694     // subsetted out of the analysis mesh. Only process if
00695     // non-null.
00696     if (elem != NULL) {
00697       stk::mesh::Entity& side =
00698         stk::mesh::fem::declare_element_side(bulk, side_ids[is], *elem, side_ordinal);
00699       bulk.change_entity_parts( side, add_parts );
00700       sides[is] = &side;
00701     } else {
00702       sides[is] = NULL;
00703     }
00704   }
00705 
00706   const stk::mesh::Field<double, stk::mesh::ElementNode> *df_field =
00707     stk::io::get_distribution_factor_field(*side_block_part);
00708   if (df_field != NULL) {
00709     stk::io::field_data_from_ioss(df_field, sides, block, "distribution_factors");
00710   }
00711       }
00712     }
00713   }
00714 
00715   // ========================================================================
00716   void process_sidesets(Ioss::Region &region, stk::mesh::BulkData &bulk)
00717   {
00718     const Ioss::SideSetContainer& side_sets = region.get_sidesets();
00719 
00720     for(Ioss::SideSetContainer::const_iterator it = side_sets.begin();
00721   it != side_sets.end(); ++it) {
00722       Ioss::SideSet *entity = *it;
00723 
00724       if (stk::io::include_entity(entity)) {
00725   process_surface_entity(entity, bulk);
00726       }
00727     }
00728   }
00729 
00730   // ========================================================================
00731   // ========================================================================
00732   void get_field_data(stk::mesh::BulkData &bulk, stk::mesh::Part &part,
00733           stk::mesh::EntityRank part_type,
00734           Ioss::GroupingEntity *io_entity,
00735           Ioss::Field::RoleType filter_role)
00736   {
00737     std::vector<stk::mesh::Entity*> entities;
00738     stk::io::get_entity_list(io_entity, part_type, bulk, entities);
00739 
00740     stk::mesh::MetaData& meta = stk::mesh::MetaData::get(part);
00741     stk::mesh::Part &universal = meta.universal_part();
00742     const std::vector<stk::mesh::FieldBase*> &fields = meta.get_fields();
00743 
00744     std::vector<stk::mesh::FieldBase *>::const_iterator I = fields.begin();
00745     while (I != fields.end()) {
00746       const stk::mesh::FieldBase *f = *I; ++I;
00747       if (stk::io::is_valid_part_field(f, part_type, part, universal, filter_role)) {
00748   stk::io::field_data_from_ioss(f, entities, io_entity, f->name());
00749       }
00750     }
00751   }
00752 
00753   void process_input_request(Ioss::Region &region,
00754            stk::mesh::BulkData &bulk,
00755            int step)
00756   {
00757     region.begin_state(step);
00758 
00759     // Special processing for nodeblock (all nodes in model)...
00760     const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00761 
00762     // ??? Get field data from nodeblock...
00763     get_field_data(bulk, meta.universal_part(), stk::mesh::fem::FEMMetaData::NODE_RANK,
00764        region.get_node_blocks()[0], Ioss::Field::TRANSIENT);
00765 
00766     const stk::mesh::PartVector & all_parts = meta.get_parts();
00767     for ( stk::mesh::PartVector::const_iterator
00768       ip = all_parts.begin(); ip != all_parts.end(); ++ip ) {
00769 
00770       stk::mesh::Part * const part = *ip;
00771 
00772       const stk::mesh::EntityRank part_rank = part->primary_entity_rank();
00773 
00774       // Check whether this part should be output to results database.
00775       if (stk::io::is_part_io_part(*part)) {
00776   // Get Ioss::GroupingEntity corresponding to this part...
00777   Ioss::GroupingEntity *entity = region.get_entity(part->name());
00778   if (entity != NULL) {
00779     if (entity->type() == Ioss::SIDESET) {
00780       Ioss::SideSet *sset = dynamic_cast<Ioss::SideSet*>(entity);
00781       assert(sset != NULL);
00782       int block_count = sset->block_count();
00783       for (int i=0; i < block_count; i++) {
00784         Ioss::SideBlock *side_block = sset->get_block(i);
00786     get_field_data(bulk, *part,
00787                                part_rank,
00788              side_block, Ioss::Field::TRANSIENT);
00789       }
00790     } else {
00791       get_field_data(bulk, *part,
00792                            part_rank,
00793          entity, Ioss::Field::TRANSIENT);
00794     }
00795   } else {
00798   }
00799       }
00800     }
00801 
00802     region.end_state(step);
00803   }
00804 
00805   void put_field_data(stk::mesh::BulkData &bulk, stk::mesh::Part &part,
00806           stk::mesh::EntityRank part_type,
00807           Ioss::GroupingEntity *io_entity,
00808           Ioss::Field::RoleType filter_role)
00809   {
00810     std::vector<stk::mesh::Entity*> entities;
00811     stk::io::get_entity_list(io_entity, part_type, bulk, entities);
00812 
00813     stk::mesh::MetaData& meta = stk::mesh::MetaData::get(part);
00814     stk::mesh::Part &universal = meta.universal_part();
00815     const std::vector<stk::mesh::FieldBase*> &fields = meta.get_fields();
00816 
00817     std::vector<stk::mesh::FieldBase *>::const_iterator I = fields.begin();
00818     while (I != fields.end()) {
00819       const stk::mesh::FieldBase *f = *I; ++I;
00820       if (stk::io::is_valid_part_field(f, part_type, part, universal, filter_role)) {
00821   stk::io::field_data_to_ioss(f, entities, io_entity, f->name(), filter_role);
00822       }
00823     }
00824   }
00825 
00826   void process_output_request(Ioss::Region &region,
00827             stk::mesh::BulkData &bulk,
00828             int step)
00829   {
00830     region.begin_state(step);
00831     // Special processing for nodeblock (all nodes in model)...
00832     const stk::mesh::MetaData& meta = stk::mesh::MetaData::get(bulk);
00833 
00834     put_field_data(bulk, meta.universal_part(), stk::mesh::fem::FEMMetaData::NODE_RANK,
00835        region.get_node_blocks()[0], Ioss::Field::TRANSIENT);
00836 
00837     const stk::mesh::PartVector & all_parts = meta.get_parts();
00838     for ( stk::mesh::PartVector::const_iterator
00839       ip = all_parts.begin(); ip != all_parts.end(); ++ip ) {
00840 
00841       stk::mesh::Part * const part = *ip;
00842 
00843       const stk::mesh::EntityRank part_rank = part->primary_entity_rank();
00844 
00845       // Check whether this part should be output to results database.
00846       if (stk::io::is_part_io_part(*part)) {
00847 
00848   // Get Ioss::GroupingEntity corresponding to this part...
00849   Ioss::GroupingEntity *entity = region.get_entity(part->name());
00850   if (entity != NULL) {
00851 
00852     if (entity->type() == Ioss::SIDESET) {
00853       Ioss::SideSet *sset = dynamic_cast<Ioss::SideSet*>(entity);
00854       assert(sset != NULL);
00855       int block_count = sset->block_count();
00856 
00857       for (int i=0; i < block_count; i++) {
00858         Ioss::SideBlock *side_block = sset->get_block(i);
00860     put_field_data(bulk, *part, part_rank,
00861              side_block, Ioss::Field::TRANSIENT);
00862       }
00863     } else {
00864       put_field_data(bulk, *part, part_rank,
00865          entity, Ioss::Field::TRANSIENT);
00866     }
00867   } else {
00870   }
00871       }
00872     }
00873     region.end_state(step);
00874   }
00875 }
00876 
00877 // ========================================================================
00878 #include <boost/program_options.hpp>
00879 
00880 #include <stk_util/parallel/BroadcastArg.hpp>
00881 #include <stk_util/environment/ProgramOptions.hpp>
00882 
00883   namespace bopt = boost::program_options;
00884   int main(int argc, char** argv)
00885   {
00886     //----------------------------------
00887     // Broadcast argc and argv to all processors.
00888 
00889     stk::ParallelMachine comm = stk::parallel_machine_init(&argc, &argv);
00890 
00891     stk::BroadcastArg b_arg(comm, argc, argv);
00892 
00893     //----------------------------------
00894     // Process the broadcast command line arguments
00895 
00896     bopt::options_description desc("options");
00897 
00898     desc.add_options()
00899       ("help,h",        "produce help message")
00900       ("mesh",         bopt::value<std::string>(), "mesh file" )
00901       ("directory,d",  bopt::value<std::string>(), "working directory" )
00902       ("output-log,o", bopt::value<std::string>(), "output log path" )
00903       ("runtest,r",    bopt::value<std::string>(), "runtest pid file" );
00904 
00905     stk::get_options_description().add(desc);
00906 
00907     bopt::variables_map &vm = stk::get_variables_map();
00908     try {
00909       bopt::store(bopt::parse_command_line(b_arg.m_argc, b_arg.m_argv, desc), vm);
00910       bopt::notify(vm);
00911     }
00912     catch (std::exception & /* x */) {
00913       std::exit(1);
00914     }
00915 
00916     if (vm.count("help")) {
00917       std::cout << desc << "\n";
00918       std::exit(EXIT_SUCCESS);
00919     }
00920 
00921     //----------------------------------
00922 
00923     if ( vm.count("mesh") ) {
00924       std::string in_filename = boost::any_cast<std::string>(vm["mesh"].value());
00925       std::string out_filename = in_filename + ".out";
00926       stk_example_io::io_example(comm, in_filename, out_filename );
00927     } else {
00928       std::cout << "OPTION ERROR: The '--mesh <filename>' option is required!\n";
00929       std::exit(EXIT_FAILURE);
00930     }
00931     stk::parallel_machine_finalize();
00932 
00933     return 0;
00934   }
00935 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends