Mesh Oriented datABase  (version 5.5.0)
An array-based unstructured mesh library
NCWriteHelper.hpp
Go to the documentation of this file.
1 /*
2  * NCWriteHelper.hpp
3  *
4  * Purpose : Climate NC writer file helper; abstract, will be implemented for each type
5  *
6  * Created on: Mar 28, 2014
7  */
8 
9 #ifndef NCWRITEHELPER_HPP_
10 #define NCWRITEHELPER_HPP_
11 #include "WriteNC.hpp"
12 
13 #ifdef WIN32
14 #ifdef size_t
15 #undef size_t
16 #endif
17 #endif
18 
19 namespace moab
20 {
21 
23 {
24  public:
25  NCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
26  : _writeNC( writeNC ), _fileId( fileId ), _opts( opts ), _fileSet( fileSet ), nTimeSteps( 0 ), nLevels( 1 ),
27  tDim( -1 ), levDim( -1 )
28  {
29  }
30  virtual ~NCWriteHelper(){};
31 
32  //! Get appropriate helper instance for WriteNC class based on some info in the file set
33  static NCWriteHelper* get_nc_helper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet );
34 
35  //! Collect necessary info about local mesh (implemented in child classes)
37 
38  //! Collect data for specified variables (partially implemented in child classes)
39  virtual ErrorCode collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
40 
41  //! Initialize file: this is where all defines are done
42  //! The VarData dimension ids are filled up after define
43  ErrorCode init_file( std::vector< std::string >& var_names,
44  std::vector< std::string >& desired_names,
45  bool _append );
46 
47  //! Take the info from VarData and write first non-set variables, then set variables
48  ErrorCode write_values( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
49 
50  private:
51  // Write set variables (common to scd mesh and ucd mesh)
52  ErrorCode write_set_variables( std::vector< WriteNC::VarData >& vsetdatas, std::vector< int >& tstep_nums );
53 
54  protected:
55  // Write non-set variables (implemented in child classes)
56  virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas,
57  std::vector< int >& tstep_nums ) = 0;
58 
59  //! Allow NCWriteHelper to directly access members of WriteNC
61 
62  //! Cache some information from WriteNC
63  int _fileId;
66 
67  //! Dimensions of time and level
69 
70  //! Dimension numbers for time and level
71  int tDim, levDim;
72 
73  //! Local owned cells, edges and vertices
75 
76  //! Time values of output timesteps
77  std::vector< double > timeStepVals;
78 };
79 
80 //! Child helper class for scd mesh, e.g. CAM_EL or CAM_FV
82 {
83  public:
84  ScdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
85  : NCWriteHelper( writeNC, fileId, opts, fileSet )
86  {
87  for( unsigned int i = 0; i < 6; i++ )
88  {
89  lDims[i] = -1;
90  lCDims[i] = -1;
91  }
92  }
93  virtual ~ScdNCWriteHelper() {}
94 
95  private:
96  //! Implementation of NCWriteHelper::collect_mesh_info()
97  virtual ErrorCode collect_mesh_info();
98 
99  //! Collect data for specified variables
100  virtual ErrorCode collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums );
101 
102  //! Implementation of NCWriteHelper::write_nonset_variables()
103  virtual ErrorCode write_nonset_variables( std::vector< WriteNC::VarData >& vdatas, std::vector< int >& tstep_nums );
104 
105  template < typename T >
106  void jik_to_kji( size_t ni, size_t nj, size_t nk, T* dest, T* source )
107  {
108  size_t nik = ni * nk, nij = ni * nj;
109  for( std::size_t k = 0; k != nk; k++ )
110  for( std::size_t j = 0; j != nj; j++ )
111  for( std::size_t i = 0; i != ni; i++ )
112  dest[k * nij + j * ni + i] = source[j * nik + i * nk + k];
113  }
114 
115  protected:
116  //! Dimensions of my local part of grid
117  int lDims[6];
118 
119  //! Center dimensions of my local part of grid
120  int lCDims[6];
121 };
122 
123 //! Child helper class for ucd mesh, e.g. CAM_SE (HOMME) or MPAS
125 {
126  public:
127  UcdNCWriteHelper( WriteNC* writeNC, int fileId, const FileOptions& opts, EntityHandle fileSet )
128  : NCWriteHelper( writeNC, fileId, opts, fileSet ), cDim( -1 ), eDim( -1 ), vDim( -1 )
129  {
130  }
131  virtual ~UcdNCWriteHelper() {}
132 
133  protected:
134  //! This version takes as input the moab range, from which we actually need just the
135  //! size of each sequence, for a proper transpose of the data
136  template < typename T >
137  void jik_to_kji_stride( size_t, size_t nj, size_t nk, T* dest, T* source, Range& localGid )
138  {
139  std::size_t idxInSource = 0; // Position of the start of the stride
140  // For each subrange, we will transpose a matrix of size
141  // subrange*nj*nk (subrange takes the role of ni)
142  for( Range::pair_iterator pair_iter = localGid.pair_begin(); pair_iter != localGid.pair_end(); ++pair_iter )
143  {
144  std::size_t size_range = pair_iter->second - pair_iter->first + 1;
145  std::size_t nik = size_range * nk, nij = size_range * nj;
146  for( std::size_t k = 0; k != nk; k++ )
147  for( std::size_t j = 0; j != nj; j++ )
148  for( std::size_t i = 0; i != size_range; i++ )
149  dest[idxInSource + k * nij + j * size_range + i] = source[idxInSource + j * nik + i * nk + k];
150  idxInSource += ( size_range * nj * nk );
151  }
152  }
153 
154  //! Dimension numbers for nCells, nEdges and nVertices
155  int cDim, eDim, vDim;
156 
157  //! Local global ID for owned cells, edges and vertices
159 };
160 
161 } // namespace moab
162 
163 #endif