Sierra Toolkit Version of the Day
IO_Fixture.cpp
00001 #include <stk_io/util/IO_Fixture.hpp>
00002 #include <stk_io/util/UseCase_mesh.hpp>
00003 
00004 #include <init/Ionit_Initializer.h>
00005 #include <Ioss_SubSystem.h>
00006 
00007 namespace stk {
00008 namespace io {
00009 namespace util {
00010 
00011 IO_Fixture::IO_Fixture(stk::ParallelMachine comm)
00012   : m_comm(comm)
00013   , m_fem_meta_data(NULL)
00014   , m_bulk_data(NULL)
00015   , m_ioss_input_region(NULL)
00016   , m_ioss_output_region(NULL)
00017   , m_mesh_type()
00018   , m_mesh_data()
00019 {}
00020 
00021 IO_Fixture::~IO_Fixture() {}
00022 
00023 void IO_Fixture::create_output_mesh(
00024                                     const std::string & base_exodus_filename,
00025                                     bool  add_transient,
00026                                     bool  add_all_fields
00027                                    )
00028 {
00029   // TODO: Check that the meta-data has a coordinates field and IO parts defined.
00030 
00031   Ioss::DatabaseIO *dbo = Ioss::IOFactory::create(
00032       "exodusII",
00033       base_exodus_filename,
00034       Ioss::WRITE_RESULTS,
00035       bulk_data().parallel()
00036       );
00037 
00038   ThrowErrorMsgIf(dbo == NULL || !dbo->ok(),
00039       "ERROR: Could not open results database '" << base_exodus_filename <<
00040       "' of type 'exodusII'");
00041 
00042   // NOTE: 'm_ioss_output_region' owns 'dbo' pointer at this time
00043   m_ioss_output_region = Teuchos::rcp(new Ioss::Region(dbo, "results_output"));
00044 
00045   /* Given the newly created Ioss::Region 'm_ioss_output_region', define the
00046    * model corresponding to the stk::mesh 'bulk_data'.  If the
00047    * optional 'input_region' is passed as an argument, then
00048    * synchronize all names and ids found on 'input_region' to the
00049    * output region 'm_ioss_output_region'.  The routine will query all parts
00050    * in 'bulk_data' and if they are io_parts (define by the existance
00051    * of the IOPartAttribute attribute on the part), then a
00052    * corresponding Ioss entity will be defined.  This routine only
00053    * deals with the non-transient portion of the model; no transient
00054    * fields are defined at this point.
00055    */
00056   stk::io::define_output_db( *m_ioss_output_region, bulk_data(), m_ioss_input_region.get() );
00057 
00058   /* Given an Ioss::Region 'm_ioss_output_region' which has already had its
00059    * metadata defined via 'define_output_db()' call; transfer all bulk
00060    * data (node coordinates, element connectivity, ...) to the
00061    * output database that corresponds to this Ioss::Region. At
00062    * return, all non-transient portions of the output database will
00063    * have been output.
00064    */
00065   stk::io::write_output_db( *m_ioss_output_region, bulk_data());
00066 
00067   if (add_transient) {
00068     m_ioss_output_region->begin_mode(Ioss::STATE_DEFINE_TRANSIENT);
00069 
00070     // Special processing for nodeblock (all nodes in model)...
00071     stk::io::ioss_add_fields(
00072                               meta_data().universal_part(),
00073                               meta_data().node_rank(),
00074                               m_ioss_output_region->get_node_blocks()[0],
00075                               Ioss::Field::TRANSIENT,
00076                               add_all_fields
00077                             );
00078 
00079     const stk::mesh::PartVector & all_parts = meta_data().get_parts();
00080     for ( stk::mesh::PartVector::const_iterator ip = all_parts.begin();
00081           ip != all_parts.end();
00082           ++ip
00083         )
00084     {
00085       stk::mesh::Part & part = **ip;
00086 
00087       // Check whether this part should be output to results database.
00088       if (stk::io::is_part_io_part(part)) {
00089         // Get Ioss::GroupingEntity corresponding to this part...
00090         Ioss::GroupingEntity *entity = m_ioss_output_region->get_entity(part.name());
00091         if (entity != NULL && entity->type() == Ioss::ELEMENTBLOCK) {
00092           stk::io::ioss_add_fields(
00093                                     part,
00094                                     part.primary_entity_rank(),
00095                                     entity,
00096                                     Ioss::Field::TRANSIENT,
00097                                     add_all_fields
00098                                   );
00099         }
00100       }
00101     }
00102     m_ioss_output_region->end_mode(Ioss::STATE_DEFINE_TRANSIENT);
00103   }
00104 }
00105 
00106 void IO_Fixture::add_timestep_to_output_mesh( double time )
00107 {
00108   ThrowErrorMsgIf( Teuchos::is_null(m_ioss_output_region),
00109                    "Please call create_output_mesh before add_timestep_to_output_mesh" );
00110   m_ioss_output_region->begin_mode(Ioss::STATE_TRANSIENT);
00111   int out_step = m_ioss_output_region->add_state(time);
00112   stk::io::util::process_output_request(*m_ioss_output_region, bulk_data(), out_step);
00113   m_ioss_output_region->end_mode(Ioss::STATE_TRANSIENT);
00114 }
00115 
00116 void IO_Fixture::set_meta_data( Teuchos::RCP<stk::mesh::fem::FEMMetaData> arg_meta_data )
00117 {
00118   ThrowErrorMsgIf( !Teuchos::is_null(m_fem_meta_data),
00119                    "Meta data already initialized" );
00120   m_fem_meta_data = arg_meta_data;
00121 }
00122 
00123 void IO_Fixture::set_bulk_data( Teuchos::RCP<stk::mesh::BulkData> arg_bulk_data )
00124 {
00125   ThrowErrorMsgIf( !Teuchos::is_null(m_bulk_data),
00126                    "Bulk data already initialized" );
00127   m_bulk_data = arg_bulk_data;
00128 }
00129 
00130 void IO_Fixture::initialize_meta_data( const std::string & base_filename, const std::string & mesh_type)
00131 {
00132   ThrowErrorMsgIf( !Teuchos::is_null(m_fem_meta_data),
00133                    "Meta data already initialized" );
00134   ThrowErrorMsgIf( !Teuchos::is_null(m_ioss_input_region),
00135                    "Input region was already initialized");
00136 
00137   m_fem_meta_data = Teuchos::rcp( new stk::mesh::fem::FEMMetaData());
00138   m_mesh_type = mesh_type;
00139 
00140   std::string no_working_dir = "";
00141 
00142   Ioss::Init::Initializer init_db;
00143 
00144   stk::io::util::create_input_mesh(
00145                                     m_mesh_type,
00146                                     base_filename,
00147                                     no_working_dir,
00148                                     m_comm,
00149                                     meta_data(),
00150                                     m_mesh_data
00151                                   );
00152 
00153   // TODO: Restore this once m_mesh_data is fixed
00154   //m_ioss_input_region = Teuchos::rcp( m_mesh_data.m_region );
00155 }
00156 
00157 void IO_Fixture::initialize_bulk_data()
00158 {
00159   ThrowErrorMsgIf( !Teuchos::is_null(m_bulk_data),
00160                    "Bulk data already initialized" );
00161 
00162   // TODO: Probable better to check m_ioss_input_region once that's fixed
00163   ThrowErrorMsgIf( m_mesh_type == "",
00164     "Can only use this method if meta-data was initialized with initialize_meta_data");
00165 
00166   m_bulk_data = Teuchos::rcp( new stk::mesh::BulkData(stk::mesh::fem::FEMMetaData::get_meta_data(meta_data()), m_comm));
00167 
00168   stk::io::util::populate_bulk_data(
00169                                     bulk_data(),
00170                                     m_mesh_data,
00171                                     m_mesh_type
00172                                    );
00173 }
00174 
00175 void IO_Fixture::set_input_ioss_region( Teuchos::RCP<Ioss::Region> input_region )
00176 {
00177   ThrowErrorMsgIf( !Teuchos::is_null(m_ioss_input_region),
00178                    "Input region was already initialized");
00179 
00180   m_ioss_input_region = input_region;
00181 }
00182 
00183 } // namespace util
00184 } // namespace io
00185 } // namespace stk
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends