MOAB: Mesh Oriented datABase  (version 5.2.1)
NCWriteHelper.hpp
Go to the documentation of this file.
00001 /*
00002  * NCWriteHelper.hpp
00003  *
00004  * Purpose       : Climate NC writer file helper; abstract, will be implemented for each type
00005  *
00006  *  Created on: Mar 28, 2014
00007  */
00008 
00009 #ifndef NCWRITEHELPER_HPP_
00010 #define NCWRITEHELPER_HPP_
00011 #include "WriteNC.hpp"
00012 
00013 #ifdef WIN32
00014 #ifdef size_t
00015 #undef size_t
00016 #endif
00017 #endif
00018 
00019 namespace moab
00020 {
00021 
00022 class NCWriteHelper
00023 {
00024   public:
00025     NCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
00026         : _writeNC( writeNC ), _fileId( fileId ), _opts( opts ), _fileSet( fileSet ), nTimeSteps( 0 ), nLevels( 1 ),
00027           tDim( -1 ), levDim( -1 )
00028     {
00029     }
00030     virtual ~NCWriteHelper(){};
00031 
00032     //! Get appropriate helper instance for WriteNC class based on some info in the file set
00033     static NCWriteHelper* get_nc_helper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet );
00034 
00035     //! Collect necessary info about local mesh (implemented in child classes)
00036     virtual ErrorCode collect_mesh_info() = 0;
00037 
00038     //! Collect data for specified variables (partially implemented in child classes)
00039     virtual ErrorCode collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
00040 
00041     //! Initialize file: this is where all defines are done
00042     //! The VarData dimension ids are filled up after define
00043     ErrorCode init_file( std::vector< std::string >& var_names, std::vector< std::string >& desired_names,
00044                          bool _append );
00045 
00046     //! Take the info from VarData and write first non-set variables, then set variables
00047     ErrorCode write_values( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
00048 
00049   private:
00050     // Write set variables (common to scd mesh and ucd mesh)
00051     ErrorCode write_set_variables( std::vector< WriteNC::VarData >& vsetdatas, std::vector< int >& tstep_nums );
00052 
00053   protected:
00054     // Write non-set variables (implemented in child classes)
00055     virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas,
00056                                               std::vector< int >& tstep_nums ) = 0;
00057 
00058     //! Allow NCWriteHelper to directly access members of WriteNC
00059     WriteNC* _writeNC;
00060 
00061     //! Cache some information from WriteNC
00062     int _fileId;
00063     const FileOptions& _opts;
00064     EntityHandle _fileSet;
00065 
00066     //! Dimensions of time and level
00067     int nTimeSteps, nLevels;
00068 
00069     //! Dimension numbers for time and level
00070     int tDim, levDim;
00071 
00072     //! Local owned cells, edges and vertices
00073     Range localCellsOwned, localEdgesOwned, localVertsOwned;
00074 
00075     //! Time values of output timesteps
00076     std::vector< double > timeStepVals;
00077 };
00078 
00079 //! Child helper class for scd mesh, e.g. CAM_EL or CAM_FV
00080 class ScdNCWriteHelper : public NCWriteHelper
00081 {
00082   public:
00083     ScdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
00084         : NCWriteHelper( writeNC, fileId, opts, fileSet )
00085     {
00086         for( unsigned int i = 0; i < 6; i++ )
00087         {
00088             lDims[i]  = -1;
00089             lCDims[i] = -1;
00090         }
00091     }
00092     virtual ~ScdNCWriteHelper() {}
00093 
00094   private:
00095     //! Implementation of NCWriteHelper::collect_mesh_info()
00096     virtual ErrorCode collect_mesh_info();
00097 
00098     //! Collect data for specified variables
00099     virtual ErrorCode collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
00100 
00101     //! Implementation of NCWriteHelper::write_nonset_variables()
00102     virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas, std::vector< int >& tstep_nums );
00103 
00104     template < typename T >
00105     void jik_to_kji( size_t ni, size_t nj, size_t nk, T* dest, T* source )
00106     {
00107         size_t nik = ni * nk, nij = ni * nj;
00108         for( std::size_t k = 0; k != nk; k++ )
00109             for( std::size_t j = 0; j != nj; j++ )
00110                 for( std::size_t i = 0; i != ni; i++ )
00111                     dest[k * nij + j * ni + i] = source[j * nik + i * nk + k];
00112     }
00113 
00114   protected:
00115     //! Dimensions of my local part of grid
00116     int lDims[6];
00117 
00118     //! Center dimensions of my local part of grid
00119     int lCDims[6];
00120 };
00121 
00122 //! Child helper class for ucd mesh, e.g. CAM_SE (HOMME) or MPAS
00123 class UcdNCWriteHelper : public NCWriteHelper
00124 {
00125   public:
00126     UcdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
00127         : NCWriteHelper( writeNC, fileId, opts, fileSet ), cDim( -1 ), eDim( -1 ), vDim( -1 )
00128     {
00129     }
00130     virtual ~UcdNCWriteHelper() {}
00131 
00132   protected:
00133     //! This version takes as input the moab range, from which we actually need just the
00134     //! size of each sequence, for a proper transpose of the data
00135     template < typename T >
00136     void jik_to_kji_stride( size_t, size_t nj, size_t nk, T* dest, T* source, Range& localGid )
00137     {
00138         std::size_t idxInSource = 0;  // Position of the start of the stride
00139         // For each subrange, we will transpose a matrix of size
00140         // subrange*nj*nk (subrange takes the role of ni)
00141         for( Range::pair_iterator pair_iter = localGid.pair_begin(); pair_iter != localGid.pair_end(); ++pair_iter )
00142         {
00143             std::size_t size_range = pair_iter->second - pair_iter->first + 1;
00144             std::size_t nik = size_range * nk, nij = size_range * nj;
00145             for( std::size_t k = 0; k != nk; k++ )
00146                 for( std::size_t j = 0; j != nj; j++ )
00147                     for( std::size_t i = 0; i != size_range; i++ )
00148                         dest[idxInSource + k * nij + j * size_range + i] = source[idxInSource + j * nik + i * nk + k];
00149             idxInSource += ( size_range * nj * nk );
00150         }
00151     }
00152 
00153     //! Dimension numbers for nCells, nEdges and nVertices
00154     int cDim, eDim, vDim;
00155 
00156     //! Local global ID for owned cells, edges and vertices
00157     Range localGidCellsOwned, localGidEdgesOwned, localGidVertsOwned;
00158 };
00159 
00160 }  // namespace moab
00161 
00162 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines