FieldIO.cpp 24.4 KB
Newer Older
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
////////////////////////////////////////////////////////////////////////////////
//
//  File: FieldIO.cpp
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
//  Department of Aeronautics, Imperial College London (UK), and Scientific
//  Computing and Imaging Institute, University of Utah (USA).
//
//  License for the specific language governing rights and limitations under
//  Permission is hereby granted, free of charge, to any person obtaining a
//  copy of this software and associated documentation files (the "Software"),
//  to deal in the Software without restriction, including without limitation
//  the rights to use, copy, modify, merge, publish, distribute, sublicense,
//  and/or sell copies of the Software, and to permit persons to whom the
//  Software is furnished to do so, subject to the following conditions:
//
//  The above copyright notice and this permission notice shall be included
//  in all copies or substantial portions of the Software.
//
//  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
//  OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
//  THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
//  FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//  DEALINGS IN THE SOFTWARE.
//
//  Description: I/O routines relating to Fields
//
////////////////////////////////////////////////////////////////////////////////

Dave Moxey's avatar
Dave Moxey committed
36
#include <boost/asio/ip/host_name.hpp>
37
38
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/date_time/posix_time/posix_time_io.hpp>
39
#include <boost/make_shared.hpp>
Dave Moxey's avatar
Dave Moxey committed
40
#include <boost/format.hpp>
41

Dave Moxey's avatar
Dave Moxey committed
42
#include <LibUtilities/BasicConst/GitRevision.h>
43
#include <LibUtilities/BasicUtils/FieldIO.h>
44
#include <LibUtilities/BasicUtils/FileSystem.h>
45

46
47
#include <loki/Singleton.h>

48
#include <fstream>
Dave Moxey's avatar
Dave Moxey committed
49
#include <set>
50

51
52
53
54
#ifdef NEKTAR_USE_MPI
#include <mpi.h>
#endif

55
56
57
58
#ifndef NEKTAR_VERSION
#define NEKTAR_VERSION "Unknown"
#endif

Dave Moxey's avatar
Dave Moxey committed
59
namespace berrc = boost::system::errc;
60
namespace ptime = boost::posix_time;
Dave Moxey's avatar
Dave Moxey committed
61
namespace ip    = boost::asio::ip;
62

63
64
namespace Nektar
{
Dave Moxey's avatar
Dave Moxey committed
65
66
namespace LibUtilities
{
67

68
69
70
std::string fldCmdFormat = SessionReader::RegisterCmdLineArgument(
    "io-format", "i", "Default input/output format (e.g. Xml, Hdf5)");

71
72
73
/**
 * @brief Returns the FieldIO factory.
 */
Dave Moxey's avatar
Dave Moxey committed
74
75
76
FieldIOFactory &GetFieldIOFactory()
{
    typedef Loki::
77
78
        SingletonHolder<FieldIOFactory, Loki::CreateUsingNew, Loki::NoDestroy,
                        Loki::ClassLevelLockable> Type;
Dave Moxey's avatar
Dave Moxey committed
79
80
81
    return Type::Instance();
}

82
83
84
85
86
87
/// Enumerator for auto-detection of FieldIO types.
enum FieldIOType {
    eXML,
    eHDF5
};

88

Dave Moxey's avatar
Dave Moxey committed
89
90
91
92
93
94
95
96
97
98
99
100
/**
 * @brief Determine file type of given input file.
 *
 * This method attempts to identify the file type of a given input file @p
 * filename. It returns a string corresponding to GetFieldIOFactory() or throws
 * an assertion if it cannot be identified.
 *
 * @param filename  Input filename
 * @param comm      Communicator for parallel runs
 *
 * @return FieldIO format of @p filename.
 */
Dave Moxey's avatar
Dave Moxey committed
101
102
103
const std::string FieldIO::GetFileType(const std::string &filename,
                                       CommSharedPtr comm)
{
104
    FieldIOType ioType = eXML;
Dave Moxey's avatar
Dave Moxey committed
105
106
107
108
    int size = comm->GetSize();
    int rank = comm->GetRank();

    if (size == 1 || rank == 0)
109
    {
Dave Moxey's avatar
Dave Moxey committed
110
        std::string datafilename;
Dave Moxey's avatar
Dave Moxey committed
111
112
113

        // If input is a directory, check for root processor file.
        if (fs::is_directory(filename))
114
        {
Dave Moxey's avatar
Dave Moxey committed
115
116
117
            fs::path p0file("P0000000.fld");
            fs::path fullpath = filename / p0file;
            datafilename      = PortablePath(fullpath);
118
        }
Dave Moxey's avatar
Dave Moxey committed
119
        else
120
        {
Dave Moxey's avatar
Dave Moxey committed
121
122
            datafilename = filename;
        }
123

Dave Moxey's avatar
Dave Moxey committed
124
125
126
        // Read first 8 bytes. If they correspond with magic bytes below it's an
        // HDF5 file. XML is potentially a nightmare with all the different
        // encodings so we'll just assume it's OK if it's not HDF.
127
128
        const unsigned char magic[8] = {
            0x89, 0x48, 0x44, 0x46, 0x0d, 0x0a, 0x1a, 0x0a};
129

Dave Moxey's avatar
Dave Moxey committed
130
        std::ifstream datafile(datafilename.c_str(), ios_base::binary);
131
        ASSERTL0(datafile.good(), "Unable to open file: " + filename);
132

133
        ioType = eHDF5;
134
        for (unsigned i = 0; i < 8 && datafile.good(); ++i)
Dave Moxey's avatar
Dave Moxey committed
135
        {
136
137
            unsigned char byte = datafile.get();
            if (byte != magic[i])
Dave Moxey's avatar
Dave Moxey committed
138
            {
139
                ioType = eXML;
Dave Moxey's avatar
Dave Moxey committed
140
                break;
141
142
            }
        }
Dave Moxey's avatar
Dave Moxey committed
143
    }
144

Dave Moxey's avatar
Dave Moxey committed
145
    if (size > 1)
Dave Moxey's avatar
Dave Moxey committed
146
    {
147
        int code = (int)ioType;
Dave Moxey's avatar
Dave Moxey committed
148
        comm->Bcast(code, 0);
149
        ioType = (FieldIOType)code;
Dave Moxey's avatar
Dave Moxey committed
150
    }
Dave Moxey's avatar
Dave Moxey committed
151
152

    std::string iofmt;
153
    if (ioType == eXML)
Dave Moxey's avatar
Dave Moxey committed
154
    {
Dave Moxey's avatar
Dave Moxey committed
155
        iofmt = "Xml";
Dave Moxey's avatar
Dave Moxey committed
156
    }
157
    else if (ioType == eHDF5)
Dave Moxey's avatar
Dave Moxey committed
158
    {
Dave Moxey's avatar
Dave Moxey committed
159
        iofmt = "Hdf5";
Dave Moxey's avatar
Dave Moxey committed
160
    }
Dave Moxey's avatar
Dave Moxey committed
161
    else
Dave Moxey's avatar
Dave Moxey committed
162
    {
Dave Moxey's avatar
Dave Moxey committed
163
164
        // Error
        ASSERTL0(false, "Unknown file format");
Dave Moxey's avatar
Dave Moxey committed
165
166
    }

Dave Moxey's avatar
Dave Moxey committed
167
168
169
    return iofmt;
}

170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
/**
 * @brief Returns an object for the default FieldIO method.
 *
 * This function returns a FieldIO class as determined by the hard-coded default
 * (XML), which can be overridden by changing the session reader SOLVERINFO
 * variable FieldIOFormat.
 *
 * @param session  Session reader
 *
 * @return FieldIO object
 */
FieldIOSharedPtr FieldIO::CreateDefault(
    const LibUtilities::SessionReaderSharedPtr session)
{
    std::string iofmt("Xml");
185
    if (session->DefinesSolverInfo("IOFormat"))
186
    {
187
        iofmt = session->GetSolverInfo("IOFormat");
188
    }
189
190
191
192
193
194

    if (session->DefinesCmdLineArgument("io-format"))
    {
        iofmt = session->GetCmdLineArgument<std::string>("io-format");
    }

195
196
197
    return GetFieldIOFactory().CreateInstance(
        iofmt,
        session->GetComm(),
198
        session->GetSharedFilesystem());
199
200
}

Dave Moxey's avatar
Dave Moxey committed
201
202
203
204
205
206
207
208
209
210
211
/**
 * @brief Construct a FieldIO object for a given input filename.
 *
 * This is a convenience function that takes an input filename and constructs
 * the appropriate FieldIO subclass, using FieldIO::GetFileType.
 *
 * @param session   Session reader
 * @param filename  Input filename
 *
 * @return FieldIO class reader for @p filename.
 */
212
FieldIOSharedPtr FieldIO::CreateForFile(
Dave Moxey's avatar
Dave Moxey committed
213
214
215
216
217
218
219
220
    const LibUtilities::SessionReaderSharedPtr session,
    const std::string &filename)
{
    const std::string iofmt =
        FieldIO::GetFileType(filename, session->GetComm());
    return GetFieldIOFactory().CreateInstance(
        iofmt,
        session->GetComm(),
221
        session->GetSharedFilesystem());
Dave Moxey's avatar
Dave Moxey committed
222
}
Dave Moxey's avatar
Dave Moxey committed
223

Dave Moxey's avatar
Dave Moxey committed
224
/**
Dave Moxey's avatar
Dave Moxey committed
225
226
227
 * @brief This function allows for data to be written to an FLD file when a
 * session and/or communicator is not instantiated. Typically used in utilities
 * which do not take XML input and operate in serial only.
228
229
230
231
232
233
 *
 * @param outFile       Output filename
 * @param fielddefs     Field definitions that define the output
 * @param fielddata     Binary field data that stores the output corresponding
 *                      to @p fielddefs.
 * @param fieldinfomap  Associated field metadata map.
Dave Moxey's avatar
Dave Moxey committed
234
235
236
237
 */
void Write(const std::string &outFile,
           std::vector<FieldDefinitionsSharedPtr> &fielddefs,
           std::vector<std::vector<NekDouble> > &fielddata,
238
239
           const FieldMetaDataMap &fieldinfomap,
           const bool backup)
Dave Moxey's avatar
Dave Moxey committed
240
{
241
#ifdef NEKTAR_USE_MPI
Dave Moxey's avatar
Dave Moxey committed
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
    int size;
    int init;
    MPI_Initialized(&init);

    // If MPI has been initialised we can check the number of processes
    // and, if > 1, tell the user he should not be running this
    // function in parallel. If it is not initialised, we do not
    // initialise it here, and assume the user knows what they are
    // doing.
    if (init)
    {
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        ASSERTL0(size == 1,
                 "This static function is not available in parallel. Please"
                 "instantiate a FieldIO object for parallel use.");
    }
258
#endif
Dave Moxey's avatar
Dave Moxey committed
259
260
    CommSharedPtr c    = GetCommFactory().CreateInstance("Serial", 0, 0);
    FieldIOSharedPtr f = GetFieldIOFactory().CreateInstance("Xml", c, false);
261
    f->Write(outFile, fielddefs, fielddata, fieldinfomap, backup);
Dave Moxey's avatar
Dave Moxey committed
262
}
263

Dave Moxey's avatar
Dave Moxey committed
264
/**
Dave Moxey's avatar
Dave Moxey committed
265
266
267
 * @brief This function allows for data to be imported from an FLD file when a
 * session and/or communicator is not instantiated. Typically used in utilities
 * which only operate in serial.
268
269
270
271
272
273
274
275
276
277
 *
 * @param infilename    Input filename (or directory if parallel format)
 * @param fielddefs     On return contains field definitions as read from the
 *                      input.
 * @param fielddata     On return, contains binary field data that stores the
 *                      input corresponding to @p fielddefs.
 * @param fieldinfo     On returnm, contains the associated field metadata map.
 * @param ElementIDs    Element IDs that lie on this processor, which can be
 *                      optionally supplied to avoid reading the entire file on
 *                      each processor.
Dave Moxey's avatar
Dave Moxey committed
278
279
280
281
282
283
 */
LIB_UTILITIES_EXPORT void Import(
    const std::string &infilename,
    std::vector<FieldDefinitionsSharedPtr> &fielddefs,
    std::vector<std::vector<NekDouble> > &fielddata,
    FieldMetaDataMap &fieldinfomap,
Dave Moxey's avatar
Dave Moxey committed
284
    const Array<OneD, int> &ElementIDs)
Dave Moxey's avatar
Dave Moxey committed
285
{
286
#ifdef NEKTAR_USE_MPI
Dave Moxey's avatar
Dave Moxey committed
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
    int size;
    int init;
    MPI_Initialized(&init);

    // If MPI has been initialised we can check the number of processes
    // and, if > 1, tell the user he should not be running this
    // function in parallel. If it is not initialised, we do not
    // initialise it here, and assume the user knows what they are
    // doing.
    if (init)
    {
        MPI_Comm_size(MPI_COMM_WORLD, &size);
        ASSERTL0(size == 1,
                 "This static function is not available in parallel. Please"
                 "instantiate a FieldIO object for parallel use.");
    }
303
#endif
Dave Moxey's avatar
Dave Moxey committed
304
    CommSharedPtr c    = GetCommFactory().CreateInstance("Serial", 0, 0);
305
306
    const std::string iofmt = FieldIO::GetFileType(infilename, c);
    FieldIOSharedPtr f = GetFieldIOFactory().CreateInstance(iofmt, c, false);
307
    f->Import(infilename, fielddefs, fielddata, fieldinfomap, ElementIDs);
Dave Moxey's avatar
Dave Moxey committed
308
}
309

Dave Moxey's avatar
Dave Moxey committed
310
/**
Dave Moxey's avatar
Dave Moxey committed
311
 * @brief Constructor for FieldIO base class.
Dave Moxey's avatar
Dave Moxey committed
312
313
 */
FieldIO::FieldIO(LibUtilities::CommSharedPtr pComm, bool sharedFilesystem)
314
    : m_comm(pComm), m_sharedFilesystem(sharedFilesystem)
Dave Moxey's avatar
Dave Moxey committed
315
316
{
}
317

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
/**
 * @brief Add provenance information to the field metadata map.
 *
 * This routine adds some basic provenance information to the field metadata to
 * enable better tracking of version information:
 *
 *   - Nektar++ version
 *   - Date and time of simulation
 *   - Hostname of the machine the simulation was performed on
 *   - git SHA1 and branch name, if Nektar++ was compiled from git and not
 *     e.g. a tarball.
 *
 * @param root              Root tag, which is encapsulated using the TagWriter
 *                          structure to enable multi-file format support.
 * @param fieldmetadatamap  Any existing field metadata.
 */
Dave Moxey's avatar
Dave Moxey committed
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
void FieldIO::AddInfoTag(TagWriterSharedPtr root,
                         const FieldMetaDataMap &fieldmetadatamap)
{
    FieldMetaDataMap ProvenanceMap;

    // Nektar++ release version from VERSION file
    ProvenanceMap["NektarVersion"] = string(NEKTAR_VERSION);

    // Date/time stamp
    ptime::time_facet *facet = new ptime::time_facet("%d-%b-%Y %H:%M:%S");
    std::stringstream wss;
    wss.imbue(locale(wss.getloc(), facet));
    wss << ptime::second_clock::local_time();
    ProvenanceMap["Timestamp"] = wss.str();

    // Hostname
    boost::system::error_code ec;
    ProvenanceMap["Hostname"] = ip::host_name(ec);

    // Git information
    // If built from a distributed package, do not include this
    if (NekConstants::kGitSha1 != "GITDIR-NOTFOUND")
    {
        ProvenanceMap["GitSHA1"]   = NekConstants::kGitSha1;
        ProvenanceMap["GitBranch"] = NekConstants::kGitBranch;
    }

    TagWriterSharedPtr infoTag = root->AddChild("Metadata");
362

Dave Moxey's avatar
Dave Moxey committed
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
    FieldMetaDataMap::const_iterator infoit;

    TagWriterSharedPtr provTag = infoTag->AddChild("Provenance");
    for (infoit = ProvenanceMap.begin(); infoit != ProvenanceMap.end();
         ++infoit)
    {
        provTag->SetAttr(infoit->first, infoit->second);
    }

    //---------------------------------------------
    // write field info section
    if (fieldmetadatamap != NullFieldMetaDataMap)
    {
        for (infoit = fieldmetadatamap.begin();
             infoit != fieldmetadatamap.end();
             ++infoit)
379
        {
Dave Moxey's avatar
Dave Moxey committed
380
            infoTag->SetAttr(infoit->first, infoit->second);
381
        }
Dave Moxey's avatar
Dave Moxey committed
382
383
    }
}
384

Dave Moxey's avatar
Dave Moxey committed
385
/**
386
387
388
389
390
391
392
393
394
395
396
397
398
399
 * @brief Set up the filesystem ready for output.
 *
 * This function sets up the output given an output filename @p outname. This
 * will therefore remove any file or directory with the desired output filename
 * and return the absolute path to the output.
 *
 * If @p perRank is set, a new directory will be created to contain one file per
 * process rank.
 *
 * @param outname   Desired output filename.
 * @param perRank   True if one file-per-rank output is required.
 *
 * @return Absolute path to resulting file.
 */
400
std::string FieldIO::SetUpOutput(const std::string outname, bool perRank, bool backup)
401
402
403
404
405
406
407
408
409
410
{
    ASSERTL0(!outname.empty(), "Empty path given to SetUpOutput()");

    int nprocs = m_comm->GetSize();
    int rank   = m_comm->GetRank();

    // Path to output: will be directory if parallel, normal file if
    // serial.
    fs::path specPath(outname), fulloutname;

Kilian Lackhove's avatar
Kilian Lackhove committed
411
    // in case we are rank 0 or not on a shared filesystem, check if the specPath already exists
412
    if (backup && (rank == 0 || !m_sharedFilesystem) && fs::exists(specPath))
413
    {
414
415
        // rename. foo/bar_123.chk -> foo/bar_123.bak0.chk and in case
        // foo/bar_123.bak0.chk already exists, foo/bar_123.chk -> foo/bar_123.bak1.chk
416
        fs::path bakPath = specPath;
417
        int cnt = 0;
418
        while (fs::exists(bakPath))
419
        {
420
421
            bakPath = specPath.parent_path();
            bakPath += specPath.stem();
422
            bakPath += fs::path(".bak" + boost::lexical_cast<std::string>(cnt++));
423
            bakPath += specPath.extension();
424
        }
425
        std::cout << "renaming " << specPath << " -> " << bakPath << std::endl;
426
427
428
429
430
431
432
433
434
        try
        {
            fs::rename(specPath, bakPath);
        }
        catch (fs::filesystem_error &e)
        {
            ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
                        "Filesystem error: " + string(e.what()));
        }
435
    }
Kilian Lackhove's avatar
Kilian Lackhove committed
436
    // wait until rank 0 has backed up the old specPath
Kilian Lackhove's avatar
Kilian Lackhove committed
437
438
439
440
    if (backup)
    {
        m_comm->Block();
    }
441

442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
    if (nprocs == 1)
    {
        fulloutname = specPath;
    }
    else
    {
        // Guess at filename that might belong to this process.
        boost::format pad("P%1$07d.%2$s");
        pad % m_comm->GetRank() % GetFileEnding();

        // Generate full path name
        fs::path poutfile(pad.str());
        fulloutname = specPath / poutfile;
    }

    // Remove any existing file which is in the way
    if (m_comm->RemoveExistingFiles())
    {
        if (m_sharedFilesystem)
        {
            // First, each process clears up its .fld file. This might or might
            // not be there (we might have changed numbers of processors between
            // runs, for example), but we can try anyway.
            try
            {
                fs::remove_all(fulloutname);
            }
            catch (fs::filesystem_error &e)
            {
                ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
                         "Filesystem error: " + string(e.what()));
            }
        }

        m_comm->Block();

        // Now get rank 0 processor to tidy everything else up.
        if (rank == 0 || !m_sharedFilesystem)
        {
            try
            {
                fs::remove_all(specPath);
            }
            catch (fs::filesystem_error &e)
            {
                ASSERTL0(e.code().value() == berrc::no_such_file_or_directory,
                         "Filesystem error: " + string(e.what()));
            }
        }

        m_comm->Block();
    }

495
    if (rank == 0)
496
    {
497
        cout << "Writing: " << specPath;
498
499
500
501
502
    }

    // serial processing just add ending.
    if (nprocs == 1)
    {
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
        return LibUtilities::PortablePath(specPath);
    }

    // Create the destination directory
    if (perRank)
    {
        try
        {
            if (rank == 0 || !m_sharedFilesystem)
            {
                fs::create_directory(specPath);
            }
        }
        catch (fs::filesystem_error &e)
        {
            ASSERTL0(false, "Filesystem error: " + string(e.what()));
        }

        m_comm->Block();
    }
    else
    {
        fulloutname = specPath;
    }

    // Return the full path to the partition for this process
    return LibUtilities::PortablePath(fulloutname);
}

/**
 * @brief Check field definitions for correctness and return storage size.
Dave Moxey's avatar
Dave Moxey committed
534
 *
535
 * @param fielddefs  Field definitions to check.
Dave Moxey's avatar
Dave Moxey committed
536
537
538
539
 */
int FieldIO::CheckFieldDefinition(const FieldDefinitionsSharedPtr &fielddefs)
{
    int i;
540

Dave Moxey's avatar
Dave Moxey committed
541
542
543
544
    if (fielddefs->m_elementIDs.size() == 0) // empty partition
    {
        return 0;
    }
545

Dave Moxey's avatar
Dave Moxey committed
546
    unsigned int numbasis = 0;
547

Dave Moxey's avatar
Dave Moxey committed
548
549
550
551
552
553
    // Determine nummodes vector lists are correct length
    switch (fielddefs->m_shapeType)
    {
        case eSegment:
            numbasis = 1;
            if (fielddefs->m_numHomogeneousDir)
554
            {
Dave Moxey's avatar
Dave Moxey committed
555
                numbasis += fielddefs->m_numHomogeneousDir;
556
557
            }

Dave Moxey's avatar
Dave Moxey committed
558
559
560
561
            break;
        case eTriangle:
        case eQuadrilateral:
            if (fielddefs->m_numHomogeneousDir)
562
            {
Dave Moxey's avatar
Dave Moxey committed
563
                numbasis = 3;
564
            }
Dave Moxey's avatar
Dave Moxey committed
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
            else
            {
                numbasis = 2;
            }
            break;
        case eTetrahedron:
        case ePyramid:
        case ePrism:
        case eHexahedron:
            numbasis = 3;
            break;
        default:
            ASSERTL0(false, "Unsupported shape type.");
            break;
    }
580

Dave Moxey's avatar
Dave Moxey committed
581
582
583
584
    unsigned int datasize = 0;

    ASSERTL0(fielddefs->m_basis.size() == numbasis,
             "Length of basis vector is incorrect");
585

Dave Moxey's avatar
Dave Moxey committed
586
587
588
589
590
591
592
    if (fielddefs->m_uniOrder == true)
    {
        unsigned int cnt = 0;
        // calculate datasize
        switch (fielddefs->m_shapeType)
        {
            case eSegment:
593
            {
Dave Moxey's avatar
Dave Moxey committed
594
595
596
                int l = fielddefs->m_numModes[cnt++];
                if (fielddefs->m_numHomogeneousDir == 1)
                {
Dave Moxey's avatar
Dave Moxey committed
597
598
                    datasize += l * fielddefs->m_homogeneousZIDs.size();
                    cnt++;
Dave Moxey's avatar
Dave Moxey committed
599
600
                }
                else if (fielddefs->m_numHomogeneousDir == 2)
601
                {
Dave Moxey's avatar
Dave Moxey committed
602
603
                    datasize += l * fielddefs->m_homogeneousYIDs.size();
                    cnt += 2;
604
605
606
                }
                else
                {
Dave Moxey's avatar
Dave Moxey committed
607
                    datasize += l;
608
609
                }
            }
Dave Moxey's avatar
Dave Moxey committed
610
611
612
613
614
            break;
            case eTriangle:
            {
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
615

Dave Moxey's avatar
Dave Moxey committed
616
617
618
619
620
621
622
623
624
625
626
627
                if (fielddefs->m_numHomogeneousDir == 1)
                {
                    datasize += StdTriData::getNumberOfCoefficients(l, m) *
                                fielddefs->m_homogeneousZIDs.size();
                }
                else
                {
                    datasize += StdTriData::getNumberOfCoefficients(l, m);
                }
            }
            break;
            case eQuadrilateral:
628
            {
Dave Moxey's avatar
Dave Moxey committed
629
630
631
632
633
634
635
636
637
638
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
                if (fielddefs->m_numHomogeneousDir == 1)
                {
                    datasize += l * m * fielddefs->m_homogeneousZIDs.size();
                }
                else
                {
                    datasize += l * m;
                }
639
            }
Dave Moxey's avatar
Dave Moxey committed
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
            break;
            case eTetrahedron:
            {
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
                int n = fielddefs->m_numModes[cnt++];
                datasize += StdTetData::getNumberOfCoefficients(l, m, n);
            }
            break;
            case ePyramid:
            {
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
                int n = fielddefs->m_numModes[cnt++];
                datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
            }
            break;
            case ePrism:
            {
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
                int n = fielddefs->m_numModes[cnt++];
                datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
            }
            break;
            case eHexahedron:
            {
                int l = fielddefs->m_numModes[cnt++];
                int m = fielddefs->m_numModes[cnt++];
                int n = fielddefs->m_numModes[cnt++];
                datasize += l * m * n;
            }
            break;
            default:
                ASSERTL0(false, "Unsupported shape type.");
                break;
        }
677

Dave Moxey's avatar
Dave Moxey committed
678
679
680
681
682
683
684
685
        datasize *= fielddefs->m_elementIDs.size();
    }
    else
    {
        unsigned int cnt = 0;
        // calculate data length
        for (i = 0; i < fielddefs->m_elementIDs.size(); ++i)
        {
686
            switch (fielddefs->m_shapeType)
687
688
            {
                case eSegment:
689
690
                {
                    int l = fielddefs->m_numModes[cnt++];
Dave Moxey's avatar
Dave Moxey committed
691
                    if (fielddefs->m_numHomogeneousDir == 1)
692
                    {
Dave Moxey's avatar
Dave Moxey committed
693
                        datasize += l * fielddefs->m_homogeneousZIDs.size();
694
                        cnt++;
695
                    }
Dave Moxey's avatar
Dave Moxey committed
696
                    else if (fielddefs->m_numHomogeneousDir == 2)
697
                    {
Dave Moxey's avatar
Dave Moxey committed
698
                        datasize += l * fielddefs->m_homogeneousYIDs.size();
699
                        cnt += 2;
700
701
702
703
704
705
                    }
                    else
                    {
                        datasize += l;
                    }
                }
Dave Moxey's avatar
Dave Moxey committed
706
                break;
707
                case eTriangle:
Dave Moxey's avatar
Dave Moxey committed
708
709
710
                {
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
Dave Moxey's avatar
Dave Moxey committed
711
                    if (fielddefs->m_numHomogeneousDir == 1)
712
                    {
Dave Moxey's avatar
Dave Moxey committed
713
714
                        datasize += StdTriData::getNumberOfCoefficients(l, m) *
                                    fielddefs->m_homogeneousZIDs.size();
715
716
717
718
                        cnt++;
                    }
                    else
                    {
Dave Moxey's avatar
Dave Moxey committed
719
                        datasize += StdTriData::getNumberOfCoefficients(l, m);
720
                    }
Dave Moxey's avatar
Dave Moxey committed
721
                }
Dave Moxey's avatar
Dave Moxey committed
722
                break;
723
                case eQuadrilateral:
Dave Moxey's avatar
Dave Moxey committed
724
725
726
                {
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
Dave Moxey's avatar
Dave Moxey committed
727
                    if (fielddefs->m_numHomogeneousDir == 1)
728
                    {
Dave Moxey's avatar
Dave Moxey committed
729
                        datasize += l * m * fielddefs->m_homogeneousZIDs.size();
730
731
732
733
                        cnt++;
                    }
                    else
                    {
Dave Moxey's avatar
Dave Moxey committed
734
                        datasize += l * m;
735
                    }
Dave Moxey's avatar
Dave Moxey committed
736
                }
Dave Moxey's avatar
Dave Moxey committed
737
                break;
738
                case eTetrahedron:
Dave Moxey's avatar
Dave Moxey committed
739
740
741
742
743
744
                {
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
                    int n = fielddefs->m_numModes[cnt++];
                    datasize += StdTetData::getNumberOfCoefficients(l, m, n);
                }
Dave Moxey's avatar
Dave Moxey committed
745
                break;
746
                case ePyramid:
Dave Moxey's avatar
Dave Moxey committed
747
748
749
750
751
752
                {
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
                    int n = fielddefs->m_numModes[cnt++];
                    datasize += StdPyrData::getNumberOfCoefficients(l, m, n);
                }
Dave Moxey's avatar
Dave Moxey committed
753
                break;
754
                case ePrism:
755
                {
Dave Moxey's avatar
Dave Moxey committed
756
757
758
759
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
                    int n = fielddefs->m_numModes[cnt++];
                    datasize += StdPrismData::getNumberOfCoefficients(l, m, n);
760
                }
Dave Moxey's avatar
Dave Moxey committed
761
                break;
Dave Moxey's avatar
Dave Moxey committed
762
                case eHexahedron:
763
                {
Dave Moxey's avatar
Dave Moxey committed
764
765
766
767
                    int l = fielddefs->m_numModes[cnt++];
                    int m = fielddefs->m_numModes[cnt++];
                    int n = fielddefs->m_numModes[cnt++];
                    datasize += l * m * n;
768
                }
Dave Moxey's avatar
Dave Moxey committed
769
                break;
Dave Moxey's avatar
Dave Moxey committed
770
771
772
                default:
                    ASSERTL0(false, "Unsupported shape type.");
                    break;
773
774
775
            }
        }
    }
Dave Moxey's avatar
Dave Moxey committed
776
777
778
779

    return datasize;
}
}
780
}