1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
/*
 * NCWriteGCRM.cpp
 *
 *  Created on: April 9, 2014
 */

#include "NCWriteGCRM.hpp"
#include "MBTagConventions.hpp"

namespace moab
{

NCWriteGCRM::~NCWriteGCRM()
{
    // TODO Auto-generated destructor stub
}

ErrorCode NCWriteGCRM::collect_mesh_info()
{
    Interface*& mbImpl                   = _writeNC->mbImpl;
    std::vector< std::string >& dimNames = _writeNC->dimNames;
    std::vector< int >& dimLens          = _writeNC->dimLens;<--- Variable 'dimLens' can be declared with const
    Tag& mGlobalIdTag                    = _writeNC->mGlobalIdTag;

    ErrorCode rval;

    // Look for time dimension
    std::vector< std::string >::iterator vecIt;
    if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "Time" ) ) != dimNames.end() )
        tDim = vecIt - dimNames.begin();
    else if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "time" ) ) != dimNames.end() )
        tDim = vecIt - dimNames.begin();
    else
    {
        MB_SET_ERR( MB_FAILURE, "Couldn't find 'Time' or 'time' dimension" );
    }
    nTimeSteps = dimLens[tDim];

    // Get number of levels
    if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "layers" ) ) != dimNames.end() )
        levDim = vecIt - dimNames.begin();
    else
    {
        MB_SET_ERR( MB_FAILURE, "Couldn't find 'layers' dimension" );
    }
    nLevels = dimLens[levDim];

    // Get local vertices
    rval = mbImpl->get_entities_by_dimension( _fileSet, 0, localVertsOwned );MB_CHK_SET_ERR( rval, "Trouble getting local vertices in current file set" );
    assert( !localVertsOwned.empty() );

    // Get local edges
    rval = mbImpl->get_entities_by_dimension( _fileSet, 1, localEdgesOwned );MB_CHK_SET_ERR( rval, "Trouble getting local edges in current file set" );
    // There are no edges if NO_EDGES read option is set

    // Get local cells
    rval = mbImpl->get_entities_by_dimension( _fileSet, 2, localCellsOwned );MB_CHK_SET_ERR( rval, "Trouble getting local cells in current file set" );
    assert( !localCellsOwned.empty() );

#ifdef MOAB_HAVE_MPI
    bool& isParallel = _writeNC->isParallel;
    if( isParallel )
    {
        ParallelComm*& myPcomm = _writeNC->myPcomm;
        int rank               = myPcomm->proc_config().proc_rank();
        int procs              = myPcomm->proc_config().proc_size();
        if( procs > 1 )
        {
#ifndef NDEBUG
            unsigned int num_local_verts = localVertsOwned.size();
#endif
            rval = myPcomm->filter_pstatus( localVertsOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned vertices in current file set" );

            // Assume that PARALLEL_RESOLVE_SHARED_ENTS option is set
            // Verify that not all local vertices are owned by the last processor
            if( procs - 1 == rank )
                assert( "PARALLEL_RESOLVE_SHARED_ENTS option is set" && localVertsOwned.size() < num_local_verts );

            if( !localEdgesOwned.empty() )
            {
                rval = myPcomm->filter_pstatus( localEdgesOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned edges in current file set" );
            }

            rval = myPcomm->filter_pstatus( localCellsOwned, PSTATUS_NOT_OWNED, PSTATUS_NOT );MB_CHK_SET_ERR( rval, "Trouble getting owned cells in current file set" );
        }
    }
#endif

    std::vector< int > gids( localVertsOwned.size() );
    rval = mbImpl->tag_get_data( mGlobalIdTag, localVertsOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local vertices" );

    // Get localGidVertsOwned
    std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidVertsOwned ) );

    if( !localEdgesOwned.empty() )
    {
        gids.resize( localEdgesOwned.size() );
        rval = mbImpl->tag_get_data( mGlobalIdTag, localEdgesOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local edges" );

        // Get localGidEdgesOwned
        std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidEdgesOwned ) );
    }

    gids.resize( localCellsOwned.size() );
    rval = mbImpl->tag_get_data( mGlobalIdTag, localCellsOwned, &gids[0] );MB_CHK_SET_ERR( rval, "Trouble getting global IDs on local cells" );

    // Get localGidCellsOwned
    std::copy( gids.rbegin(), gids.rend(), range_inserter( localGidCellsOwned ) );

    return MB_SUCCESS;
}

ErrorCode NCWriteGCRM::collect_variable_data( std::vector< std::string >& var_names, std::vector< int >& tstep_nums )
{
    NCWriteHelper::collect_variable_data( var_names, tstep_nums );

    std::vector< std::string >& dimNames = _writeNC->dimNames;
    std::vector< int >& dimLens          = _writeNC->dimLens;<--- Variable 'dimLens' can be declared with const

    // Dimension indices for other optional levels
    std::vector< unsigned int > opt_lev_dims;

    unsigned int lev_idx;
    std::vector< std::string >::iterator vecIt;

    // Get index of interface levels
    if( ( vecIt = std::find( dimNames.begin(), dimNames.end(), "interfaces" ) ) != dimNames.end() )
    {
        lev_idx = vecIt - dimNames.begin();
        opt_lev_dims.push_back( lev_idx );
    }

    std::map< std::string, WriteNC::VarData >& varInfo = _writeNC->varInfo;

    for( size_t i = 0; i < var_names.size(); i++ )
    {
        std::string varname                                     = var_names[i];
        std::map< std::string, WriteNC::VarData >::iterator vit = varInfo.find( varname );
        if( vit == varInfo.end() ) MB_SET_ERR( MB_FAILURE, "Can't find variable " << varname );

        WriteNC::VarData& currentVarData = vit->second;
        std::vector< int >& varDims      = currentVarData.varDims;

        // Skip edge variables, if there are no edges
        if( localEdgesOwned.empty() && currentVarData.entLoc == WriteNC::ENTLOCEDGE ) continue;

        // If layers dimension is not found, try other optional levels such as interfaces
        if( std::find( varDims.begin(), varDims.end(), levDim ) == varDims.end() )
        {
            for( unsigned int j = 0; j < opt_lev_dims.size(); j++ )
            {
                if( std::find( varDims.begin(), varDims.end(), opt_lev_dims[j] ) != varDims.end() )
                {
                    currentVarData.numLev = dimLens[opt_lev_dims[j]];
                    break;
                }
            }
        }

        // Skip set variables, which were already processed in
        // NCWriteHelper::collect_variable_data()
        if( WriteNC::ENTLOCSET == currentVarData.entLoc ) continue;

        // Set up writeStarts and writeCounts (maximum number of dimensions is 3)
        currentVarData.writeStarts.resize( 3 );
        currentVarData.writeCounts.resize( 3 );
        unsigned int dim_idx = 0;

        // First: time
        if( currentVarData.has_tsteps )
        {
            // Non-set variables with timesteps
            // 3 dimensions like (time, cells, layers)
            // 2 dimensions like (time, cells)
            assert( 3 == varDims.size() || 2 == varDims.size() );

            // Time should be the first dimension
            assert( tDim == varDims[0] );

            currentVarData.writeStarts[dim_idx] = 0;  // This value is timestep dependent, will be set later
            currentVarData.writeCounts[dim_idx] = 1;
            dim_idx++;
        }
        else
        {
            // Non-set variables without timesteps
            // 2 dimensions like (cells, layers)
            // 1 dimension like (cells)
            assert( 2 == varDims.size() || 1 == varDims.size() );
        }

        // Next: corners / cells / edges
        switch( currentVarData.entLoc )
        {
            case WriteNC::ENTLOCVERT:
                // Vertices
                // Start from the first localGidVerts
                // Actually, this will be reset later for writing
                currentVarData.writeStarts[dim_idx] = localGidVertsOwned[0] - 1;
                currentVarData.writeCounts[dim_idx] = localGidVertsOwned.size();
                break;
            case WriteNC::ENTLOCFACE:
                // Faces
                // Start from the first localGidCells
                // Actually, this will be reset later for writing
                currentVarData.writeStarts[dim_idx] = localGidCellsOwned[0] - 1;
                currentVarData.writeCounts[dim_idx] = localGidCellsOwned.size();
                break;
            case WriteNC::ENTLOCEDGE:
                // Edges
                // Start from the first localGidEdges
                // Actually, this will be reset later for writing
                currentVarData.writeStarts[dim_idx] = localGidEdgesOwned[0] - 1;
                currentVarData.writeCounts[dim_idx] = localGidEdgesOwned.size();
                break;
            default:
                MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << varname );
        }
        dim_idx++;

        // Finally: layers or other optional levels, it is possible that there is no
        // level dimension (numLev is 0) for this non-set variable
        if( currentVarData.numLev > 0 )
        {
            // Non-set variables with levels
            // 3 dimensions like (time, cells, layers)
            // 2 dimensions like (cells, layers)
            assert( 3 == varDims.size() || 2 == varDims.size() );

            currentVarData.writeStarts[dim_idx] = 0;
            currentVarData.writeCounts[dim_idx] = currentVarData.numLev;
            dim_idx++;
        }
        else
        {
            // Non-set variables without levels
            // 2 dimensions like (time, cells)
            // 1 dimension like (cells)
            assert( 2 == varDims.size() || 1 == varDims.size() );
        }

        // Get variable size
        currentVarData.sz = 1;
        for( std::size_t idx = 0; idx < dim_idx; idx++ )
            currentVarData.sz *= currentVarData.writeCounts[idx];
    }  // for (size_t i = 0; i < var_names.size(); i++)

    return MB_SUCCESS;
}

ErrorCode NCWriteGCRM::write_nonset_variables( std::vector< WriteNC::VarData >& vdatas, std::vector< int >& tstep_nums )
{
    Interface*& mbImpl = _writeNC->mbImpl;

    int success;

    // For each indexed variable tag, write a time step data
    for( unsigned int i = 0; i < vdatas.size(); i++ )
    {
        WriteNC::VarData& variableData = vdatas[i];

        // Skip edge variables, if there are no edges
        if( localEdgesOwned.empty() && variableData.entLoc == WriteNC::ENTLOCEDGE ) continue;

        // Get local owned entities of this variable
        Range* pLocalEntsOwned    = NULL;
        Range* pLocalGidEntsOwned = NULL;
        switch( variableData.entLoc )
        {
            case WriteNC::ENTLOCVERT:
                // Vertices
                pLocalEntsOwned    = &localVertsOwned;
                pLocalGidEntsOwned = &localGidVertsOwned;
                break;
            case WriteNC::ENTLOCEDGE:
                // Edges
                pLocalEntsOwned    = &localEdgesOwned;
                pLocalGidEntsOwned = &localGidEdgesOwned;
                break;
            case WriteNC::ENTLOCFACE:
                // Cells
                pLocalEntsOwned    = &localCellsOwned;
                pLocalGidEntsOwned = &localGidCellsOwned;
                break;
            default:
                MB_SET_ERR( MB_FAILURE, "Unexpected entity location type for variable " << variableData.varName );
        }

        unsigned int num_timesteps;
        unsigned int ents_idx = 0;
        if( variableData.has_tsteps )
        {
            // Non-set variables with timesteps
            // 3 dimensions like (time, cells, layers)
            // 2 dimensions like (time, cells)
            num_timesteps = tstep_nums.size();
            ents_idx++;
        }
        else
        {
            // Non-set variables without timesteps
            // 2 dimensions like (cells, layers)
            // 1 dimension like (cells)
            num_timesteps = 1;
        }

        unsigned int num_lev;
        if( variableData.numLev > 0 )
        {
            // Non-set variables with levels
            // 3 dimensions like (time, cells, layers)
            // 2 dimensions like (cells, layers)
            num_lev = variableData.numLev;
        }
        else
        {
            // Non-set variables without levels
            // 2 dimensions like (time, cells)
            // 1 dimension like (cells)
            num_lev = 1;
        }

        for( unsigned int t = 0; t < num_timesteps; t++ )
        {
            // We will write one time step, and count will be one; start will be different
            // Use tag_get_data instead of tag_iterate to copy tag data, as localEntsOwned
            // might not be contiguous.
            if( tDim == variableData.varDims[0] ) variableData.writeStarts[0] = t;  // This is start for time
            std::vector< double > tag_data( pLocalEntsOwned->size() * num_lev );
            ErrorCode rval = mbImpl->tag_get_data( variableData.varTags[t], *pLocalEntsOwned, &tag_data[0] );MB_CHK_SET_ERR( rval, "Trouble getting tag data on owned entities" );

#ifdef MOAB_HAVE_PNETCDF
            size_t nb_writes = pLocalGidEntsOwned->psize();
            std::vector< int > requests( nb_writes ), statuss( nb_writes );
            size_t idxReq = 0;
#endif

            // Now write copied tag data
            // Use nonblocking put (request aggregation)
            switch( variableData.varDataType )
            {
                case NC_DOUBLE: {
                    size_t indexInDoubleArray = 0;
                    size_t ic                 = 0;
                    for( Range::pair_iterator pair_iter = pLocalGidEntsOwned->pair_begin();
                         pair_iter != pLocalGidEntsOwned->pair_end(); ++pair_iter, ic++ )
                    {
                        EntityHandle starth                = pair_iter->first;
                        EntityHandle endh                  = pair_iter->second;
                        variableData.writeStarts[ents_idx] = (NCDF_SIZE)( starth - 1 );
                        variableData.writeCounts[ents_idx] = (NCDF_SIZE)( endh - starth + 1 );

                        // Do a partial write, in each subrange
#ifdef MOAB_HAVE_PNETCDF
                        // Wait outside this loop
                        success =
                            NCFUNCREQP( _vara_double )( _fileId, variableData.varId, &( variableData.writeStarts[0] ),
                                                        &( variableData.writeCounts[0] ),
                                                        &( tag_data[indexInDoubleArray] ), &requests[idxReq++] );
#else
                        success = NCFUNCAP(
                            _vara_double )( _fileId, variableData.varId, &( variableData.writeStarts[0] ),
                                            &( variableData.writeCounts[0] ), &( tag_data[indexInDoubleArray] ) );
#endif
                        if( success )
                            MB_SET_ERR( MB_FAILURE,
                                        "Failed to write double data in a loop for variable " << variableData.varName );
                        // We need to increment the index in double array for the
                        // next subrange
                        indexInDoubleArray += ( endh - starth + 1 ) * num_lev;
                    }
                    assert( ic == pLocalGidEntsOwned->psize() );
#ifdef MOAB_HAVE_PNETCDF
                    success = ncmpi_wait_all( _fileId, requests.size(), &requests[0], &statuss[0] );
                    if( success ) MB_SET_ERR( MB_FAILURE, "Failed on wait_all" );
#endif
                    break;
                }
                default:
                    MB_SET_ERR( MB_NOT_IMPLEMENTED, "Writing non-double data is not implemented yet" );
            }
        }
    }

    return MB_SUCCESS;
}

} /* namespace moab */