Mesh Oriented datABase  (version 5.4.1)
Array-based unstructured mesh datastructure
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,
00044                          std::vector< std::string >& desired_names,
00045                          bool _append );
00046 
00047     //! Take the info from VarData and write first non-set variables, then set variables
00048     ErrorCode write_values( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
00049 
00050   private:
00051     // Write set variables (common to scd mesh and ucd mesh)
00052     ErrorCode write_set_variables( std::vector< WriteNC::VarData >& vsetdatas, std::vector< int >& tstep_nums );
00053 
00054   protected:
00055     // Write non-set variables (implemented in child classes)
00056     virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas,
00057                                               std::vector< int >& tstep_nums ) = 0;
00058 
00059     //! Allow NCWriteHelper to directly access members of WriteNC
00060     WriteNC* _writeNC;
00061 
00062     //! Cache some information from WriteNC
00063     int _fileId;
00064     const FileOptions& _opts;
00065     EntityHandle _fileSet;
00066 
00067     //! Dimensions of time and level
00068     int nTimeSteps, nLevels;
00069 
00070     //! Dimension numbers for time and level
00071     int tDim, levDim;
00072 
00073     //! Local owned cells, edges and vertices
00074     Range localCellsOwned, localEdgesOwned, localVertsOwned;
00075 
00076     //! Time values of output timesteps
00077     std::vector< double > timeStepVals;
00078 };
00079 
00080 //! Child helper class for scd mesh, e.g. CAM_EL or CAM_FV
00081 class ScdNCWriteHelper : public NCWriteHelper
00082 {
00083   public:
00084     ScdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
00085         : NCWriteHelper( writeNC, fileId, opts, fileSet )
00086     {
00087         for( unsigned int i = 0; i < 6; i++ )
00088         {
00089             lDims[i]  = -1;
00090             lCDims[i] = -1;
00091         }
00092     }
00093     virtual ~ScdNCWriteHelper() {}
00094 
00095   private:
00096     //! Implementation of NCWriteHelper::collect_mesh_info()
00097     virtual ErrorCode collect_mesh_info();
00098 
00099     //! Collect data for specified variables
00100     virtual ErrorCode collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
00101 
00102     //! Implementation of NCWriteHelper::write_nonset_variables()
00103     virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas, std::vector< int >& tstep_nums );
00104 
00105     template < typename T >
00106     void jik_to_kji( size_t ni, size_t nj, size_t nk, T* dest, T* source )
00107     {
00108         size_t nik = ni * nk, nij = ni * nj;
00109         for( std::size_t k = 0; k != nk; k++ )
00110             for( std::size_t j = 0; j != nj; j++ )
00111                 for( std::size_t i = 0; i != ni; i++ )
00112                     dest[k * nij + j * ni + i] = source[j * nik + i * nk + k];
00113     }
00114 
00115   protected:
00116     //! Dimensions of my local part of grid
00117     int lDims[6];
00118 
00119     //! Center dimensions of my local part of grid
00120     int lCDims[6];
00121 };
00122 
00123 //! Child helper class for ucd mesh, e.g. CAM_SE (HOMME) or MPAS
00124 class UcdNCWriteHelper : public NCWriteHelper
00125 {
00126   public:
00127     UcdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
00128         : NCWriteHelper( writeNC, fileId, opts, fileSet ), cDim( -1 ), eDim( -1 ), vDim( -1 )
00129     {
00130     }
00131     virtual ~UcdNCWriteHelper() {}
00132 
00133   protected:
00134     //! This version takes as input the moab range, from which we actually need just the
00135     //! size of each sequence, for a proper transpose of the data
00136     template < typename T >
00137     void jik_to_kji_stride( size_t, size_t nj, size_t nk, T* dest, T* source, Range& localGid )
00138     {
00139         std::size_t idxInSource = 0;  // Position of the start of the stride
00140         // For each subrange, we will transpose a matrix of size
00141         // subrange*nj*nk (subrange takes the role of ni)
00142         for( Range::pair_iterator pair_iter = localGid.pair_begin(); pair_iter != localGid.pair_end(); ++pair_iter )
00143         {
00144             std::size_t size_range = pair_iter->second - pair_iter->first + 1;
00145             std::size_t nik = size_range * nk, nij = size_range * nj;
00146             for( std::size_t k = 0; k != nk; k++ )
00147                 for( std::size_t j = 0; j != nj; j++ )
00148                     for( std::size_t i = 0; i != size_range; i++ )
00149                         dest[idxInSource + k * nij + j * size_range + i] = source[idxInSource + j * nik + i * nk + k];
00150             idxInSource += ( size_range * nj * nk );
00151         }
00152     }
00153 
00154     //! Dimension numbers for nCells, nEdges and nVertices
00155     int cDim, eDim, vDim;
00156 
00157     //! Local global ID for owned cells, edges and vertices
00158     Range localGidCellsOwned, localGidEdgesOwned, localGidVertsOwned;
00159 };
00160 
00161 }  // namespace moab
00162 
00163 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines