MeshGraph.cpp 138 KB
Newer Older
Mike Kirby's avatar
Mike Kirby committed
1
2
////////////////////////////////////////////////////////////////////////////////
//
Chris Cantwell's avatar
Chris Cantwell committed
3
//  File: MeshGraph.cpp
Mike Kirby's avatar
Mike Kirby committed
4
5
6
7
8
9
//
//  For more information, please see: http://www.nektar.info/
//
//  The MIT License
//
//  Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10
//  Department of Aeronautics, Imperial College London (UK), and Scientific
Mike Kirby's avatar
Mike Kirby committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//  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.
//
32
//  Description:
Mike Kirby's avatar
Mike Kirby committed
33
34
35
36
//
////////////////////////////////////////////////////////////////////////////////
#include "pchSpatialDomains.h"

Joe Frazier's avatar
Joe Frazier committed
37
38
#include <boost/foreach.hpp>

Mike Kirby's avatar
Mike Kirby committed
39
#include <SpatialDomains/MeshGraph.h>
40
#include <LibUtilities/BasicUtils/ParseUtils.hpp>
Mike Kirby's avatar
Mike Kirby committed
41
42

// Use the stl version, primarily for string.
Blake Nelson's avatar
Blake Nelson committed
43
#ifndef TIXML_USE_STL
Mike Kirby's avatar
Mike Kirby committed
44
#define TIXML_USE_STL
Blake Nelson's avatar
Blake Nelson committed
45
46
#endif

Mike Kirby's avatar
Mike Kirby committed
47
#include <tinyxml/tinyxml.h>
48
#include <cstring>
Sophia Han's avatar
Sophia Han committed
49
#include <sstream>
50
51
52
#if defined(__INTEL_COMPILER)
#include <mathimf.h>
#else
Claes Eskilsson's avatar
Claes Eskilsson committed
53
#include <cmath>
54
#endif
Mike Kirby's avatar
Mike Kirby committed
55

56
57
#include <SpatialDomains/MeshGraph1D.h>
#include <SpatialDomains/MeshGraph2D.h>
Michael DeLisi's avatar
Michael DeLisi committed
58
#include <SpatialDomains/MeshGraph3D.h>
59

60
61
62
63
64
// These are required for the Write(...) and Import(...) functions.
#include <boost/archive/iterators/base64_from_binary.hpp>
#include <boost/archive/iterators/binary_from_base64.hpp>
#include <boost/archive/iterators/transform_width.hpp>
#include <boost/iostreams/copy.hpp>
65
#include <boost/iostreams/filter/zlib.hpp>
66
67
#include <boost/iostreams/filtering_stream.hpp>

Mike Kirby's avatar
Mike Kirby committed
68
69
namespace Nektar
{
70
71
    namespace SpatialDomains
    {
Chris Cantwell's avatar
Chris Cantwell committed
72
73
74
        /**
         *
         */
75
76
77
78
79
        MeshGraph::MeshGraph():
            m_meshDimension(3),
            m_spaceDimension(3)
        {
        }
80

Chris Cantwell's avatar
Chris Cantwell committed
81
82
83
84
85
86
87

        /**
         *
         */
        MeshGraph::MeshGraph(
                unsigned int meshDimension,
                unsigned int spaceDimension) :
88
89
90
91
            m_meshDimension(meshDimension),
            m_spaceDimension(spaceDimension)
        {
        }
92

Chris Cantwell's avatar
Chris Cantwell committed
93
94
95
96
97
98
99

        /**
         *
         */
        MeshGraph::MeshGraph(
                const LibUtilities::SessionReaderSharedPtr &pSession) :
            m_session(pSession)
100
        {
Chris Cantwell's avatar
Chris Cantwell committed
101
        }
102

Chris Cantwell's avatar
Chris Cantwell committed
103
104
105
106
107
108

        /**
         *
         */
        MeshGraph::~MeshGraph()
        {
109
110
        }

Chris Cantwell's avatar
Chris Cantwell committed
111
112
113
114
115
116
117

        /**
         *
         */
        boost::shared_ptr<MeshGraph> MeshGraph::Read(
                const std::string& infilename,
                bool pReadExpansions)
118
119
        {
            boost::shared_ptr<MeshGraph> returnval;
120

121
            MeshGraph mesh;
122

123
124
            mesh.ReadGeometry(infilename);
            int meshDim = mesh.GetMeshDimension();
125

126
127
128
129
130
            switch(meshDim)
            {
            case 1:
                returnval = MemoryManager<MeshGraph1D>::AllocateSharedPtr();
                break;
131

132
133
134
            case 2:
                returnval = MemoryManager<MeshGraph2D>::AllocateSharedPtr();
                break;
135

136
137
138
            case 3:
                returnval = MemoryManager<MeshGraph3D>::AllocateSharedPtr();
                break;
139

140
141
142
143
144
145
146
            default:
                std::string err = "Invalid mesh dimension: ";
                std::stringstream strstrm;
                strstrm << meshDim;
                err += strstrm.str();
                NEKERROR(ErrorUtil::efatal, err.c_str());
            }
147

148
149
150
151
            if (returnval)
            {
                returnval->ReadGeometry(infilename);
                returnval->ReadGeometryInfo(infilename);
152
153
154
155
                if (pReadExpansions)
                {
                    returnval->ReadExpansions(infilename);
                }
156
157
158
            }
            return returnval;
        }
159

Chris Cantwell's avatar
Chris Cantwell committed
160
161
162
163
164

        /**
         *
         */
        void MeshGraph::ReadGeometry(const std::string& infilename)
165
        {
Chris Cantwell's avatar
Chris Cantwell committed
166
167
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
168

Chris Cantwell's avatar
Chris Cantwell committed
169
170
171
172
173
            std::stringstream errstr;
            errstr << "Unable to load file: " << infilename << " (";
            errstr << doc.ErrorDesc() << ", line " << doc.ErrorRow()
                                 << ", column " << doc.ErrorCol() << ")";
            ASSERTL0(loadOkay, errstr.str());
174

Chris Cantwell's avatar
Chris Cantwell committed
175
176
            ReadGeometry(doc);
        }
177
178


Chris Cantwell's avatar
Chris Cantwell committed
179
180
181
182
183
184
185
186
187
        /**
         *
         */
        void MeshGraph::ReadGeometry(TiXmlDocument &doc)
        {
            TiXmlHandle docHandle(&doc);
            TiXmlNode* node = NULL;
            TiXmlElement* mesh = NULL;
            TiXmlElement* master = NULL;    // Master tag within which all data is contained.
188

Chris Cantwell's avatar
Chris Cantwell committed
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
            int err;    /// Error value returned by TinyXML.

            master = doc.FirstChildElement("NEKTAR");
            ASSERTL0(master, "Unable to find NEKTAR tag in file.");

            // Find the Mesh tag and same the dim and space attributes
            mesh = master->FirstChildElement("GEOMETRY");

            ASSERTL0(mesh, "Unable to find GEOMETRY tag in file.");
            TiXmlAttribute *attr = mesh->FirstAttribute();

            // Initialize the mesh and space dimensions to 3 dimensions.
            // We want to do this each time we read a file, so it should
            // be done here and not just during class initialization.
            m_meshPartitioned = false;
            m_meshDimension = 3;
            m_spaceDimension = 3;

            while (attr)
            {
                std::string attrName(attr->Name());
                if (attrName == "DIM")
211
                {
Chris Cantwell's avatar
Chris Cantwell committed
212
213
                    err = attr->QueryIntValue(&m_meshDimension);
                    ASSERTL1(err==TIXML_SUCCESS, "Unable to read mesh dimension.");
214
                }
Chris Cantwell's avatar
Chris Cantwell committed
215
                else if (attrName == "SPACE")
216
                {
Chris Cantwell's avatar
Chris Cantwell committed
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
                    err = attr->QueryIntValue(&m_spaceDimension);
                    ASSERTL1(err==TIXML_SUCCESS, "Unable to read space dimension.");
                }
                else if (attrName == "PARTITION")
                {
                    err = attr->QueryIntValue(&m_partition);
                    ASSERTL1(err==TIXML_SUCCESS, "Unable to read partition.");
                    m_meshPartitioned = true;
                }
                else
                {
                    std::string errstr("Unknown attribute: ");
                    errstr += attrName;
                    ASSERTL1(false, errstr.c_str());
                }
232

Chris Cantwell's avatar
Chris Cantwell committed
233
234
235
                // Get the next attribute.
                attr = attr->Next();
            }
236

Chris Cantwell's avatar
Chris Cantwell committed
237
            ASSERTL1(m_meshDimension<=m_spaceDimension, "Mesh dimension greater than space dimension");
238

Chris Cantwell's avatar
Chris Cantwell committed
239
240
241
            // Now read the vertices
            TiXmlElement* element = mesh->FirstChildElement("VERTEX");
            ASSERTL0(element, "Unable to find mesh VERTEX tag in file.");
242

Chris Cantwell's avatar
Chris Cantwell committed
243
            NekDouble xscale,yscale,zscale;
244

Chris Cantwell's avatar
Chris Cantwell committed
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
            // check to see if any scaling parameters are in
            // attributes and determine these values
            LibUtilities::AnalyticExpressionEvaluator expEvaluator;
            //LibUtilities::ExpressionEvaluator expEvaluator;
            const char *xscal =  element->Attribute("XSCALE");
            if(!xscal)
            {
                xscale = 1.0;
            }
            else
            {
                std::string xscalstr = xscal;
                int expr_id = expEvaluator.DefineFunction("",xscalstr);
                xscale = expEvaluator.Evaluate(expr_id);
            }
260

Chris Cantwell's avatar
Chris Cantwell committed
261
262
263
264
265
266
267
268
269
270
271
            const char *yscal =  element->Attribute("YSCALE");
            if(!yscal)
            {
                yscale = 1.0;
            }
            else
            {
                std::string yscalstr = yscal;
                int expr_id = expEvaluator.DefineFunction("",yscalstr);
                yscale = expEvaluator.Evaluate(expr_id);
            }
272

Chris Cantwell's avatar
Chris Cantwell committed
273
274
275
276
277
278
279
280
281
282
283
            const char *zscal = element->Attribute("ZSCALE");
            if(!zscal)
            {
                zscale = 1.0;
            }
            else
            {
                std::string zscalstr = zscal;
                int expr_id = expEvaluator.DefineFunction("",zscalstr);
                zscale = expEvaluator.Evaluate(expr_id);
            }
284

Chris Cantwell's avatar
Chris Cantwell committed
285
            TiXmlElement *vertex = element->FirstChildElement("V");
286

Chris Cantwell's avatar
Chris Cantwell committed
287
288
            int indx;
            int nextVertexNumber = -1;
289

Chris Cantwell's avatar
Chris Cantwell committed
290
291
292
            while (vertex)
            {
                nextVertexNumber++;
293

Chris Cantwell's avatar
Chris Cantwell committed
294
295
                TiXmlAttribute *vertexAttr = vertex->FirstAttribute();
                std::string attrName(vertexAttr->Name());
296

Chris Cantwell's avatar
Chris Cantwell committed
297
                ASSERTL0(attrName == "ID", (std::string("Unknown attribute name: ") + attrName).c_str());
Spencer Sherwin's avatar
Spencer Sherwin committed
298

Chris Cantwell's avatar
Chris Cantwell committed
299
300
                err = vertexAttr->QueryIntValue(&indx);
                ASSERTL0(err == TIXML_SUCCESS, "Unable to read attribute ID.");
Spencer Sherwin's avatar
Spencer Sherwin committed
301

Chris Cantwell's avatar
Chris Cantwell committed
302
303
                // Now read body of vertex
                std::string vertexBodyStr;
Spencer Sherwin's avatar
Spencer Sherwin committed
304

Chris Cantwell's avatar
Chris Cantwell committed
305
                TiXmlNode *vertexBody = vertex->FirstChild();
Spencer Sherwin's avatar
Spencer Sherwin committed
306

Chris Cantwell's avatar
Chris Cantwell committed
307
308
309
310
311
312
313
314
                while (vertexBody)
                {
                    // Accumulate all non-comment body data.
                    if (vertexBody->Type() == TiXmlNode::TEXT)
                    {
                        vertexBodyStr += vertexBody->ToText()->Value();
                        vertexBodyStr += " ";
                    }
Spencer Sherwin's avatar
Spencer Sherwin committed
315

Chris Cantwell's avatar
Chris Cantwell committed
316
317
                    vertexBody = vertexBody->NextSibling();
                }
Spencer Sherwin's avatar
Spencer Sherwin committed
318

Chris Cantwell's avatar
Chris Cantwell committed
319
                ASSERTL0(!vertexBodyStr.empty(), "Vertex definitions must contain vertex data.");
Spencer Sherwin's avatar
Spencer Sherwin committed
320

Chris Cantwell's avatar
Chris Cantwell committed
321
322
323
                // Get vertex data from the data string.
                NekDouble xval, yval, zval;
                std::istringstream vertexDataStrm(vertexBodyStr.c_str());
Spencer Sherwin's avatar
Spencer Sherwin committed
324

Chris Cantwell's avatar
Chris Cantwell committed
325
326
327
328
329
330
331
332
333
334
335
336
337
338
                try
                {
                    while(!vertexDataStrm.fail())
                    {
                        vertexDataStrm >> xval >> yval >> zval;

                        xval *= xscale;
                        yval *= yscale;
                        zval *= zscale;

                        // Need to check it here because we may not be
                        // good after the read indicating that there
                        // was nothing to read.
                        if (!vertexDataStrm.fail())
339
                        {
Chris Cantwell's avatar
Chris Cantwell committed
340
341
                            VertexComponentSharedPtr vert(MemoryManager<VertexComponent>::AllocateSharedPtr(m_spaceDimension, indx, xval, yval, zval));
                            m_vertSet[indx] = vert;
342
                        }
343
344
                    }
                }
Chris Cantwell's avatar
Chris Cantwell committed
345
                catch(...)
346
                {
Chris Cantwell's avatar
Chris Cantwell committed
347
                    ASSERTL0(false, "Unable to read VERTEX data.");
348
                }
Chris Cantwell's avatar
Chris Cantwell committed
349
350

                vertex = vertex->NextSiblingElement("V");
351
            }
352
        }
353
354


Chris Cantwell's avatar
Chris Cantwell committed
355
356
357
358
359
360
361
        /**
         * Read the geometry-related information from the given file. This
         * information is located within the XML tree under
         * <NEKTAR><GEOMETRY><GEOMINFO>.
         * @param   infilename      Filename of XML file.
         */
        void MeshGraph::ReadGeometryInfo(const std::string &infilename)
362
        {
Chris Cantwell's avatar
Chris Cantwell committed
363
364
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
365

Chris Cantwell's avatar
Chris Cantwell committed
366
367
368
369
370
            std::stringstream errstr;
            errstr << "Unable to load file: " << infilename << std::endl;
            errstr << "Reason: " << doc.ErrorDesc() << std::endl;
            errstr << "Position: Line " << doc.ErrorRow() << ", Column " << doc.ErrorCol() << std::endl;
            ASSERTL0(loadOkay, errstr.str());
371

Chris Cantwell's avatar
Chris Cantwell committed
372
373
            ReadGeometryInfo(doc);
        }
374
375


Chris Cantwell's avatar
Chris Cantwell committed
376
377
378
379
380
381
382
383
384
385
        /**
         * Read the geometry-related information from the given XML document.
         * This information is located within the XML tree under
         * <NEKTAR><GEOMETRY><GEOMINFO>.
         * @param   doc             XML document.
         */
        void MeshGraph::ReadGeometryInfo(TiXmlDocument &doc)
        {
            TiXmlElement *master = doc.FirstChildElement("NEKTAR");
            ASSERTL0(master, "Unable to find NEKTAR tag in file.");
386

Chris Cantwell's avatar
Chris Cantwell committed
387
388
389
            // Find the Expansions tag
            TiXmlElement *geomTag = master->FirstChildElement("GEOMETRY");
            ASSERTL0(geomTag, "Unable to find GEOMETRY tag in file.");
390

Chris Cantwell's avatar
Chris Cantwell committed
391
392
393
            // See if we have GEOMINFO. If there is none, it's fine.
            TiXmlElement *geomInfoTag = geomTag->FirstChildElement("GEOMINFO");
            if (!geomInfoTag) return;
394

Chris Cantwell's avatar
Chris Cantwell committed
395
            TiXmlElement *infoItem = geomInfoTag->FirstChildElement("I");
396

Chris Cantwell's avatar
Chris Cantwell committed
397
398
399
400
401
402
403
            // Multiple nodes will only occur if there is a comment in between
            // definitions.
            while (infoItem)
            {
                std::string geomProperty = infoItem->Attribute("PROPERTY");
                std::string geomValue    = infoItem->Attribute("VALUE");
                GeomInfoMap::iterator x  = m_geomInfo.find(geomProperty);
404

Chris Cantwell's avatar
Chris Cantwell committed
405
406
407
408
409
410
                ASSERTL0(x == m_geomInfo.end(),
                        "Property " + geomProperty + " already specified.");
                m_geomInfo[geomProperty] = geomValue;
                infoItem = infoItem->NextSiblingElement("I");
            }
        }
411
412


Chris Cantwell's avatar
Chris Cantwell committed
413
414
415
416
417
418
419
        /**
         *
         */
        void MeshGraph::ReadExpansions(const std::string& infilename)
        {
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
420

Chris Cantwell's avatar
Chris Cantwell committed
421
422
423
424
425
            std::stringstream errstr;
            errstr << "Unable to load file: " << infilename << std::endl;
            errstr << "Reason: " << doc.ErrorDesc() << std::endl;
            errstr << "Position: Line " << doc.ErrorRow() << ", Column " << doc.ErrorCol() << std::endl;
            ASSERTL0(loadOkay, errstr.str());
426

Chris Cantwell's avatar
Chris Cantwell committed
427
428
429
430
431
432
433
434
435
436
437
438
439
440
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
            ReadExpansions(doc);
        }


        /**
         *
         */
        void MeshGraph::ReadExpansions(TiXmlDocument &doc)
        {
            TiXmlElement *master = doc.FirstChildElement("NEKTAR");
            ASSERTL0(master, "Unable to find NEKTAR tag in file.");

            // Find the Expansions tag
            TiXmlElement *expansionTypes = master->FirstChildElement("EXPANSIONS");
            ASSERTL0(expansionTypes, "Unable to find EXPANSIONS tag in file.");

            if(expansionTypes)
            {
                // Find the Expansion type
                TiXmlElement *expansion = expansionTypes->FirstChildElement();
                std::string   expType   = expansion->Value();

                if(expType == "E")
                {
                    int i;
                    ExpansionMapShPtr expansionMap;

                    /// Expansiontypes will contain composite,
                    /// nummodes, and expansiontype (eModified, or
                    /// eOrthogonal) Or a full list of data of
                    /// basistype, nummodes, pointstype, numpoints;

                    /// Expansiontypes may also contain a list of
                    /// fields that this expansion relates to. If this
                    /// does not exist the variable is only set to
                    /// "DefaultVar".

                    while (expansion)
                    {

                        const char *fStr = expansion->Attribute("FIELDS");
                        std::vector<std::string> fieldStrings;

                        if(fStr) // extract other fields.
                        {
                            std::string fieldStr = fStr;
                            bool  valid = ParseUtils::GenerateOrderedStringVector(fieldStr.c_str(),fieldStrings);
                            ASSERTL0(valid,"Unable to correctly parse the field string in ExpansionTypes.");
475
                        }
Chris Cantwell's avatar
Chris Cantwell committed
476
477
478
479
480

                        // check to see if m_expasionVectorShPtrMap has
                        // already been intiailised and if not intiailse
                        // vector.
                        if(m_expansionMapShPtrMap.count("DefaultVar") == 0) // no previous definitions
481
                        {
Chris Cantwell's avatar
Chris Cantwell committed
482
                            expansionMap = SetUpExpansionMap();
483

Chris Cantwell's avatar
Chris Cantwell committed
484
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
485

Chris Cantwell's avatar
Chris Cantwell committed
486
487
488
                            // make sure all fields in this search point
                            // to same expansion vector;
                            for(i = 0; i < fieldStrings.size(); ++i)
489
                            {
Chris Cantwell's avatar
Chris Cantwell committed
490
                                m_expansionMapShPtrMap[fieldStrings[i]] = expansionMap;
491
492
                            }
                        }
Chris Cantwell's avatar
Chris Cantwell committed
493
                        else // default variable is defined
494
                        {
495

Chris Cantwell's avatar
Chris Cantwell committed
496
                            if(fieldStrings.size()) // fields are defined
497
                            {
Chris Cantwell's avatar
Chris Cantwell committed
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
                                //see if field exists
                                if(m_expansionMapShPtrMap.count(fieldStrings[0]))
                                {
                                    expansionMap = m_expansionMapShPtrMap.find(fieldStrings[0])->second;
                                }
                                else
                                {
                                    expansionMap = SetUpExpansionMap();
                                    // make sure all fields in this search point
                                    // to same expansion vector;
                                    for(i = 0; i < fieldStrings.size(); ++i)
                                    {
                                        if(m_expansionMapShPtrMap.count(fieldStrings[i]) == 0)
                                        {
                                            m_expansionMapShPtrMap[fieldStrings[i]] = expansionMap;
                                        }
                                        else
                                        {
                                            ASSERTL0(false,"Expansion vector for this field is already  setup");
                                        }
                                    }
                                }
520
                            }
Chris Cantwell's avatar
Chris Cantwell committed
521
                            else // use default variable list
522
                            {
Chris Cantwell's avatar
Chris Cantwell committed
523
                                expansionMap = m_expansionMapShPtrMap.find("DefaultVar")->second;
524
                            }
525
526

                        }
527

Chris Cantwell's avatar
Chris Cantwell committed
528
529
530
531
532
533
                        /// Mandatory components...optional are to follow later.
                        std::string compositeStr = expansion->Attribute("COMPOSITE");
                        ASSERTL0(compositeStr.length() > 3, "COMPOSITE must be specified in expansion definition");
                        int beg = compositeStr.find_first_of("[");
                        int end = compositeStr.find_first_of("]");
                        std::string compositeListStr = compositeStr.substr(beg+1,end-beg-1);
534

Chris Cantwell's avatar
Chris Cantwell committed
535
536
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
537

Chris Cantwell's avatar
Chris Cantwell committed
538
539
540
                        bool          useExpansionType = false;
                        ExpansionType expansion_type;
                        int           num_modes;
541

Chris Cantwell's avatar
Chris Cantwell committed
542
543
                        LibUtilities::BasisKeyVector basiskeyvec;
                        const char * tStr = expansion->Attribute("TYPE");
544

Chris Cantwell's avatar
Chris Cantwell committed
545
546
547
548
549
550
                        if(tStr) // use type string to define expansion
                        {
                            std::string typeStr = tStr;
                            const std::string* begStr = kExpansionTypeStr;
                            const std::string* endStr = kExpansionTypeStr+eExpansionTypeSize;
                            const std::string* expStr = std::find(begStr, endStr, typeStr);
551

Chris Cantwell's avatar
Chris Cantwell committed
552
553
                            ASSERTL0(expStr != endStr, "Invalid expansion type.");
                            expansion_type = (ExpansionType)(expStr - begStr);
554
555


556
557
558
559
                            /// \todo solvers break the pattern 'instantiate Session -> instantiate MeshGraph'
                            /// and parse command line arguments by themselves; one needs to unify command
                            /// line arguments handling.
                            /// Solvers tend to call MeshGraph::Read statically -> m_session
560
                            /// is not defined -> no info about command line arguments presented
561
562
                            /// ASSERTL0(m_session != 0, "One needs to instantiate SessionReader first");

563
564
565
                            const char *nStr = expansion->Attribute("NUMMODES");
                            ASSERTL0(nStr,"NUMMODES was not defined in EXPANSION section of input");
                            std::string nummodesStr = nStr;
566

567
                            LibUtilities::Equation nummodesEqn(nummodesStr);
568

569
                            num_modes = (int) nummodesEqn.Evaluate();
570

Chris Cantwell's avatar
Chris Cantwell committed
571
572
573
574
575
576
577
578
                            useExpansionType = true;
                        }
                        else // assume expansion is defined individually
                        {
                            // Extract the attributes.
                            const char *bTypeStr = expansion->Attribute("BASISTYPE");
                            ASSERTL0(bTypeStr,"TYPE or BASISTYPE was not defined in EXPANSION section of input");
                            std::string basisTypeStr = bTypeStr;
579

Chris Cantwell's avatar
Chris Cantwell committed
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
                            // interpret the basis type string.
                            std::vector<std::string> basisStrings;
                            std::vector<LibUtilities::BasisType> basis;
                            bool valid = ParseUtils::GenerateOrderedStringVector(basisTypeStr.c_str(), basisStrings);
                            ASSERTL0(valid, "Unable to correctly parse the basis types.");
                            for (vector<std::string>::size_type i = 0; i < basisStrings.size(); i++)
                            {
                                valid = false;
                                for (unsigned int j = 0; j < LibUtilities::SIZE_BasisType; j++)
                                {
                                    if (LibUtilities::BasisTypeMap[j] == basisStrings[i])
                                    {
                                        basis.push_back((LibUtilities::BasisType) j);
                                        valid = true;
                                        break;
                                    }
                                }
                                ASSERTL0(valid, std::string("Unable to correctly parse the basis type: ").append(basisStrings[i]).c_str());
                            }
                            const char *nModesStr = expansion->Attribute("NUMMODES");
                            ASSERTL0(nModesStr,"NUMMODES was not defined in EXPANSION section of input");
601

Chris Cantwell's avatar
Chris Cantwell committed
602
603
604
605
606
                            std::string numModesStr = nModesStr;
                            std::vector<unsigned int> numModes;
                            valid = ParseUtils::GenerateOrderedVector(numModesStr.c_str(), numModes);
                            ASSERTL0(valid, "Unable to correctly parse the number of modes.");
                            ASSERTL0(numModes.size() == basis.size(),"information for num modes does not match the number of basis");
607

Chris Cantwell's avatar
Chris Cantwell committed
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
                            const char *pTypeStr =  expansion->Attribute("POINTSTYPE");
                            ASSERTL0(pTypeStr,"POINTSTYPE was not defined in EXPANSION section of input");
                            std::string pointsTypeStr = pTypeStr;
                            // interpret the points type string.
                            std::vector<std::string> pointsStrings;
                            std::vector<LibUtilities::PointsType> points;
                            valid = ParseUtils::GenerateOrderedStringVector(pointsTypeStr.c_str(), pointsStrings);
                            ASSERTL0(valid, "Unable to correctly parse the points types.");
                            for (vector<std::string>::size_type i = 0; i < pointsStrings.size(); i++)
                            {
                                valid = false;
                                for (unsigned int j = 0; j < LibUtilities::SIZE_PointsType; j++)
                                {
                                    if (LibUtilities::kPointsTypeStr[j] == pointsStrings[i])
                                    {
                                        points.push_back((LibUtilities::PointsType) j);
                                        valid = true;
                                        break;
                                    }
                                }
                                ASSERTL0(valid, std::string("Unable to correctly parse the points type: ").append(pointsStrings[i]).c_str());
                            }
630

Chris Cantwell's avatar
Chris Cantwell committed
631
632
633
634
635
636
637
                            const char *nPointsStr = expansion->Attribute("NUMPOINTS");
                            ASSERTL0(nPointsStr,"NUMPOINTS was not defined in EXPANSION section of input");
                            std::string numPointsStr = nPointsStr;
                            std::vector<unsigned int> numPoints;
                            valid = ParseUtils::GenerateOrderedVector(numPointsStr.c_str(), numPoints);
                            ASSERTL0(valid, "Unable to correctly parse the number of points.");
                            ASSERTL0(numPoints.size() == numPoints.size(),"information for num points does not match the number of basis");
638

Chris Cantwell's avatar
Chris Cantwell committed
639
640
641
642
643
644
645
                            for(int i = 0; i < basis.size(); ++i)
                            {
                                //Generate Basis key  using information
                                const LibUtilities::PointsKey pkey(numPoints[i],points[i]);
                                basiskeyvec.push_back(LibUtilities::BasisKey(basis[i],numModes[i],pkey));
                            }
                        }
646

Chris Cantwell's avatar
Chris Cantwell committed
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
                        // Now have composite and basiskeys.  Cycle through
                        // all composites for the geomShPtrs and set the modes
                        // and types for the elements contained in the element
                        // list.
                        CompositeMapIter compVecIter;
                        for (compVecIter = compositeVector.begin(); compVecIter != compositeVector.end(); ++compVecIter)
                        {
                            GeometryVectorIter geomVecIter;
                            for (geomVecIter = (compVecIter->second)->begin(); geomVecIter != (compVecIter->second)->end(); ++geomVecIter)
                            {
                                ExpansionMapIter x = expansionMap->find((*geomVecIter)->GetGlobalID());
                                ASSERTL0(x != expansionMap->end(), "Expansion not found!!");
                                if(useExpansionType)
                                {
                                    (x->second)->m_basisKeyVector = MeshGraph::DefineBasisKeyFromExpansionType(*geomVecIter,expansion_type,num_modes);
                                }
                                else
                                {
                                    ASSERTL0((*geomVecIter)->GetShapeDim() == basiskeyvec.size()," There is an incompatible expansion dimension with geometry dimension");
                                    (x->second)->m_basisKeyVector = basiskeyvec;
                                }
                            }
                        }
670

Chris Cantwell's avatar
Chris Cantwell committed
671
672
673
674
675
676
677
                        expansion = expansion->NextSiblingElement("E");
                    }
                }
                else if(expType == "H")
                {
                    int i;
                    ExpansionMapShPtr expansionMap;
678

Chris Cantwell's avatar
Chris Cantwell committed
679
680
                    while (expansion)
                    {
681

Chris Cantwell's avatar
Chris Cantwell committed
682
683
                        const char *fStr = expansion->Attribute("FIELDS");
                        std::vector<std::string> fieldStrings;
684

Chris Cantwell's avatar
Chris Cantwell committed
685
686
687
688
689
690
                        if(fStr) // extract other fields.
                        {
                            std::string fieldStr = fStr;
                            bool  valid = ParseUtils::GenerateOrderedStringVector(fieldStr.c_str(),fieldStrings);
                            ASSERTL0(valid,"Unable to correctly parse the field string in ExpansionTypes.");
                        }
691

Chris Cantwell's avatar
Chris Cantwell committed
692
693
694
695
696
697
                        // check to see if m_expasionVectorShPtrMap has
                        // already been intiailised and if not intiailse
                        // vector.
                        if(m_expansionMapShPtrMap.count("DefaultVar") == 0) // no previous definitions
                        {
                            expansionMap = SetUpExpansionMap();
698

Chris Cantwell's avatar
Chris Cantwell committed
699
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
700

Chris Cantwell's avatar
Chris Cantwell committed
701
702
703
704
705
706
707
708
709
                            // make sure all fields in this search point
                            // to same expansion vector;
                            for(i = 0; i < fieldStrings.size(); ++i)
                            {
                                m_expansionMapShPtrMap[fieldStrings[i]] = expansionMap;
                            }
                        }
                        else // default variable is defined
                        {
710

Chris Cantwell's avatar
Chris Cantwell committed
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
                            if(fieldStrings.size()) // fields are defined
                            {
                                //see if field exists
                                if(m_expansionMapShPtrMap.count(fieldStrings[0]))
                                {
                                    expansionMap = m_expansionMapShPtrMap.find(fieldStrings[0])->second;
                                }
                                else
                                {
                                    expansionMap = SetUpExpansionMap();
                                    // make sure all fields in this search point
                                    // to same expansion vector;
                                    for(i = 0; i < fieldStrings.size(); ++i)
                                    {
                                        if(m_expansionMapShPtrMap.count(fieldStrings[i]) == 0)
                                        {
                                            m_expansionMapShPtrMap[fieldStrings[i]] = expansionMap;
                                        }
                                        else
                                        {
                                            ASSERTL0(false,"Expansion vector for this field is already  setup");
                                        }
                                    }
                                }
                            }
                            else // use default variable list
                            {
                                expansionMap = m_expansionMapShPtrMap.find("DefaultVar")->second;
                            }
740

Chris Cantwell's avatar
Chris Cantwell committed
741
                        }
742

Chris Cantwell's avatar
Chris Cantwell committed
743
744
745
746
747
748
                        /// Mandatory components...optional are to follow later.
                        std::string compositeStr = expansion->Attribute("COMPOSITE");
                        ASSERTL0(compositeStr.length() > 3, "COMPOSITE must be specified in expansion definition");
                        int beg = compositeStr.find_first_of("[");
                        int end = compositeStr.find_first_of("]");
                        std::string compositeListStr = compositeStr.substr(beg+1,end-beg-1);
749

Chris Cantwell's avatar
Chris Cantwell committed
750
751
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
752

Chris Cantwell's avatar
Chris Cantwell committed
753
754
755
                        bool          useExpansionType = false;
                        ExpansionType expansion_type_x, expansion_type_y, expansion_type_z;
                        int           num_modes_x, num_modes_y, num_modes_z;
756

Chris Cantwell's avatar
Chris Cantwell committed
757
                        LibUtilities::BasisKeyVector basiskeyvec;
758

Chris Cantwell's avatar
Chris Cantwell committed
759
                        const char * tStr_x = expansion->Attribute("TYPE-X");
760

Chris Cantwell's avatar
Chris Cantwell committed
761
                        if(tStr_x) // use type string to define expansion
762
                        {
Chris Cantwell's avatar
Chris Cantwell committed
763
764
765
766
                            std::string typeStr = tStr_x;
                            const std::string* begStr = kExpansionTypeStr;
                            const std::string* endStr = kExpansionTypeStr+eExpansionTypeSize;
                            const std::string* expStr = std::find(begStr, endStr, typeStr);
767

Chris Cantwell's avatar
Chris Cantwell committed
768
769
                            ASSERTL0(expStr != endStr, "Invalid expansion type.");
                            expansion_type_x = (ExpansionType)(expStr - begStr);
770

Chris Cantwell's avatar
Chris Cantwell committed
771
772
773
                            const char *nStr = expansion->Attribute("NUMMODES-X");
                            ASSERTL0(nStr,"NUMMODES-X was not defined in EXPANSION section of input");
                            std::string nummodesStr = nStr;
774

Chris Cantwell's avatar
Chris Cantwell committed
775
                            LibUtilities::Equation nummodesEqn(nummodesStr);
776

Chris Cantwell's avatar
Chris Cantwell committed
777
778
                            num_modes_x = (int) nummodesEqn.Evaluate();
                        }
779

Chris Cantwell's avatar
Chris Cantwell committed
780
                        const char * tStr_y = expansion->Attribute("TYPE-Y");
781

Chris Cantwell's avatar
Chris Cantwell committed
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
                        if(tStr_y) // use type string to define expansion
                        {
                            std::string typeStr = tStr_y;
                            const std::string* begStr = kExpansionTypeStr;
                            const std::string* endStr = kExpansionTypeStr+eExpansionTypeSize;
                            const std::string* expStr = std::find(begStr, endStr, typeStr);

                            ASSERTL0(expStr != endStr, "Invalid expansion type.");
                            expansion_type_y = (ExpansionType)(expStr - begStr);

                            const char *nStr = expansion->Attribute("NUMMODES-Y");
                            ASSERTL0(nStr,"NUMMODES-Y was not defined in EXPANSION section of input");
                            std::string nummodesStr = nStr;

                            LibUtilities::Equation nummodesEqn(nummodesStr);

                            num_modes_y = (int) nummodesEqn.Evaluate();
                        }

                        const char * tStr_z = expansion->Attribute("TYPE-Z");

                        if(tStr_z) // use type string to define expansion
                        {
                            std::string typeStr = tStr_z;
                            const std::string* begStr = kExpansionTypeStr;
                            const std::string* endStr = kExpansionTypeStr+eExpansionTypeSize;
                            const std::string* expStr = std::find(begStr, endStr, typeStr);

                            ASSERTL0(expStr != endStr, "Invalid expansion type.");
                            expansion_type_z = (ExpansionType)(expStr - begStr);

                            const char *nStr = expansion->Attribute("NUMMODES-Z");
                            ASSERTL0(nStr,"NUMMODES-Z was not defined in EXPANSION section of input");
                            std::string nummodesStr = nStr;

                            LibUtilities::Equation nummodesEqn(nummodesStr);

                            num_modes_z = (int) nummodesEqn.Evaluate();
                        }

                        CompositeMapIter compVecIter;
                        for (compVecIter = compositeVector.begin(); compVecIter != compositeVector.end(); ++compVecIter)
                        {
                            GeometryVectorIter geomVecIter;
                            for (geomVecIter = (compVecIter->second)->begin(); geomVecIter != (compVecIter->second)->end(); ++geomVecIter)
                            {
                                ExpansionMapIter expVecIter;
                                for (expVecIter = expansionMap->begin(); expVecIter != expansionMap->end(); ++expVecIter)
                                {

                                    (expVecIter->second)->m_basisKeyVector = DefineBasisKeyFromExpansionTypeHomo(*geomVecIter,
                                            expansion_type_x,
                                            expansion_type_y,
                                            expansion_type_z,
                                            num_modes_x,
                                            num_modes_y,
                                            num_modes_z);
                                }
                            }
                        }

                        expansion = expansion->NextSiblingElement("H");
                    }
                }
                else if(expType == "ELEMENTS")  // Reading a file with the expansion definition
                {
                    std::vector<FieldDefinitionsSharedPtr> fielddefs;
                    ImportFieldDefs(doc, fielddefs, true);
                    cout << "    Number of elements: " << fielddefs.size() << endl;
                    SetExpansions(fielddefs);
                }
                else
                {
                    ASSERTL0(false,"Expansion type not defined");
                }
            }
858
        }
859

Chris Cantwell's avatar
Chris Cantwell committed
860
861
862
863
864

        /**
         *
         */
        void MeshGraph::ReadDomain(TiXmlDocument &doc)
865
        {
Chris Cantwell's avatar
Chris Cantwell committed
866
            TiXmlHandle docHandle(&doc);
867

Chris Cantwell's avatar
Chris Cantwell committed
868
869
870
871
872
873
874
875
876
877
878
879
880
            TiXmlElement* mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
            TiXmlElement* domain = NULL;

            ASSERTL0(mesh, "Unable to find GEOMETRY tag in file.");

            /// Look for data in DOMAIN block.
            domain = mesh->FirstChildElement("DOMAIN");

            ASSERTL0(domain, "Unable to find DOMAIN tag in file.");

            // find the non comment portion of the body.
            TiXmlNode* elementChild = domain->FirstChild();
            while(elementChild && elementChild->Type() != TiXmlNode::TEXT)
881
            {
Chris Cantwell's avatar
Chris Cantwell committed
882
                elementChild = elementChild->NextSibling();
883
            }
884

Chris Cantwell's avatar
Chris Cantwell committed
885
886
            ASSERTL0(elementChild, "Unable to read DOMAIN body.");
            std::string elementStr = elementChild->ToText()->ValueStr();
887

Chris Cantwell's avatar
Chris Cantwell committed
888
            elementStr = elementStr.substr(elementStr.find_first_not_of(" "));
889

Chris Cantwell's avatar
Chris Cantwell committed
890
891
892
            std::string::size_type indxBeg = elementStr.find_first_of('[') + 1;
            std::string::size_type indxEnd = elementStr.find_last_of(']') - 1;
            std::string indxStr = elementStr.substr(indxBeg, indxEnd - indxBeg + 1);
893

Chris Cantwell's avatar
Chris Cantwell committed
894
            ASSERTL0(!indxStr.empty(), "Unable to read domain's composite index (index missing?).");
895

Chris Cantwell's avatar
Chris Cantwell committed
896
897
898
899
            // Read the domain composites.
            // Parse the composites into a list.
            GetCompositeList(indxStr, m_domain);
            ASSERTL0(!m_domain.empty(), (std::string("Unable to obtain domain's referenced composite: ") + indxStr).c_str());
900
        }
901

Chris Cantwell's avatar
Chris Cantwell committed
902
903
904
905
906

        /**
         *
         */
        void MeshGraph::ReadCurves(TiXmlDocument &doc)
907
        {
Chris Cantwell's avatar
Chris Cantwell committed
908
909
910
911
            /// We know we have it since we made it this far.
            TiXmlHandle docHandle(&doc);
            TiXmlElement* mesh = docHandle.FirstChildElement("NEKTAR").FirstChildElement("GEOMETRY").Element();
            TiXmlElement* field = NULL;
912

Chris Cantwell's avatar
Chris Cantwell committed
913
914
915
916
            // check to see if any scaling parameters are in
            // attributes and determine these values
            TiXmlElement* element = mesh->FirstChildElement("VERTEX");
            ASSERTL0(element, "Unable to find mesh VERTEX tag in file.");
917

Chris Cantwell's avatar
Chris Cantwell committed
918
            NekDouble xscale,yscale,zscale;
919

Chris Cantwell's avatar
Chris Cantwell committed
920
921
922
923
924
925
926
927
928
929
930
931
            LibUtilities::AnalyticExpressionEvaluator expEvaluator;
            const char *xscal =  element->Attribute("XSCALE");
            if(!xscal)
            {
                xscale = 1.0;
            }
            else
            {
                std::string xscalstr = xscal;
                int expr_id = expEvaluator.DefineFunction("",xscalstr);
                xscale = expEvaluator.Evaluate(expr_id);
            }
932

Chris Cantwell's avatar
Chris Cantwell committed
933
934
935
936
937
938
939
940
941
942
943
            const char *yscal =  element->Attribute("YSCALE");
            if(!yscal)
            {
                yscale = 1.0;
            }
            else
            {
                std::string yscalstr = yscal;
                int expr_id = expEvaluator.DefineFunction("",yscalstr);
                yscale = expEvaluator.Evaluate(expr_id);
            }
944

Chris Cantwell's avatar
Chris Cantwell committed
945
946
947
948
949
950
951
952
953
954
955
            const char *zscal = element->Attribute("ZSCALE");
            if(!zscal)
            {
                zscale = 1.0;
            }
            else
            {
                std::string zscalstr = zscal;
                int expr_id = expEvaluator.DefineFunction("",zscalstr);
                zscale = expEvaluator.Evaluate(expr_id);
            }
956

Chris Cantwell's avatar
Chris Cantwell committed
957
            int err;
958

Chris Cantwell's avatar
Chris Cantwell committed
959
960
            /// Look for elements in CURVE block.
            field = mesh->FirstChildElement("CURVED");
961

Chris Cantwell's avatar
Chris Cantwell committed
962
963
964
965
966
967
968
969
            if(!field) //return if no curved entities
            {
                return;
            }

            /// All curves are of the form: "<? ID="#" TYPE="GLL OR other
            /// points type" NUMPOINTS="#"> ... </?>", with ? being an
            /// element type (either E or F).
970

Chris Cantwell's avatar
Chris Cantwell committed
971
            TiXmlElement *edgelement = field->FirstChildElement("E");
972

Chris Cantwell's avatar
Chris Cantwell committed
973
974
            int edgeindx, edgeid;
            int nextEdgeNumber = -1;
975

Chris Cantwell's avatar
Chris Cantwell committed
976
            while(edgelement)
977
            {
Chris Cantwell's avatar
Chris Cantwell committed
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
                /// These should be ordered.
                nextEdgeNumber++;

                std::string edge(edgelement->ValueStr());
                ASSERTL0(edge == "E", (std::string("Unknown 3D curve type:") + edge).c_str());

                /// Read id attribute.
                err = edgelement->QueryIntAttribute("ID", &edgeindx);
                ASSERTL0(err == TIXML_SUCCESS, "Unable to read curve attribute ID.");

                /// Read edge id attribute.
                err = edgelement->QueryIntAttribute("EDGEID", &edgeid);
                ASSERTL0(err == TIXML_SUCCESS, "Unable to read curve attribute EDGEID.");

                /// Read text edgelement description.
                std::string elementStr;
                TiXmlNode* elementChild = edgelement->FirstChild();

                while(elementChild)
997
                {
Chris Cantwell's avatar
Chris Cantwell committed
998
999
                    // Accumulate all non-comment element data
                    if (elementChild->Type() == TiXmlNode::TEXT)
1000
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1001
1002
                        elementStr += elementChild->ToText()->ValueStr();
                        elementStr += " ";
1003
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
                    elementChild = elementChild->NextSibling();
                }

                ASSERTL0(!elementStr.empty(), "Unable to read curve description body.");

                /// Parse out the element components corresponding to type of element.
                if (edge == "E")
                {
                    int numPts=0;
                    // Determine the points type
                    std::string typeStr = edgelement->Attribute("TYPE");
                    ASSERTL0(!typeStr.empty(), "TYPE must be specified in " "points definition");

                    LibUtilities::PointsType type;
                    const std::string* begStr = LibUtilities::kPointsTypeStr;
                    const std::string* endStr = LibUtilities::kPointsTypeStr + LibUtilities::SIZE_PointsType;
                    const std::string* ptsStr = std::find(begStr, endStr, typeStr);

                    ASSERTL0(ptsStr != endStr, "Invalid points type.");
                    type = (LibUtilities::PointsType)(ptsStr - begStr);

                    //Determine the number of points
                    err = edgelement->QueryIntAttribute("NUMPOINTS", &numPts);
                    ASSERTL0(err == TIXML_SUCCESS, "Unable to read curve attribute NUMPOINTS.");
                    CurveSharedPtr curve(MemoryManager<Curve>::AllocateSharedPtr(edgeid, type));

                    // Read points (x, y, z)
                    NekDouble xval, yval, zval;
                    std::istringstream elementDataStrm(elementStr.c_str());
                    try
1034
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
                        while(!elementDataStrm.fail())
                        {
                            elementDataStrm >> xval >> yval >> zval;

                            xval *= xscale;
                            yval *= yscale;
                            zval *= zscale;
                            // Need to check it here because we may not be
                            // good after the read indicating that there
                            // was nothing to read.
                            if (!elementDataStrm.fail())
                            {
                                VertexComponentSharedPtr vert(MemoryManager<VertexComponent>::AllocateSharedPtr(m_meshDimension, edgeindx, xval, yval, zval));

                                curve->m_points.push_back(vert);
                            }

                        }
1053
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1054
                    catch(...)
1055
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1056
1057
1058
                        NEKERROR(ErrorUtil::efatal,
                                (std::string("Unable to read curve data for EDGE: ") + elementStr).c_str());

1059
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076

                    ASSERTL0(curve->m_points.size() == numPts,"Number of points specificed by attribute NUMPOINTS is different from number of points in list");

                    m_curvedEdges.push_back(curve);

                    edgelement = edgelement->NextSiblingElement("E");

                } // end if-loop

            } // end while-loop


            TiXmlElement *facelement = field->FirstChildElement("F");
            int faceindx, faceid;
            int nextFaceNumber = -1;

            while(facelement)
1077
            {
Chris Cantwell's avatar
Chris Cantwell committed
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
                /// These should be ordered.
                nextFaceNumber++;

                std::string face(facelement->ValueStr());
                ASSERTL0(face == "F", (std::string("Unknown 3D curve type: ") + face).c_str());

                /// Read id attribute.
                err = facelement->QueryIntAttribute("ID", &faceindx);

                ASSERTL0(err == TIXML_SUCCESS, "Unable to read curve attribute ID.");
                ASSERTL0(faceindx == nextFaceNumber, "Face IDs must begin with zero and be sequential.");

                /// Read face id attribute.
                err = facelement->QueryIntAttribute("FACEID", &faceid);
                ASSERTL0(err == TIXML_SUCCESS, "Unable to read curve attribute FACEID.");

                /// Read text face element description.
                std::string elementStr;
                TiXmlNode* elementChild = facelement->FirstChild();

                while(elementChild)
1099
                {
Chris Cantwell's avatar
Chris Cantwell committed
1100
1101
                    // Accumulate all non-comment element data
                    if (elementChild->Type() == TiXmlNode::TEXT)
1102
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1103
1104
1105
1106
                        elementStr += elementChild->ToText()->ValueStr();
                        elementStr += " ";
                    }
                    elementChild = elementChild->NextSibling();
1107
                }
Chris Cantwell's avatar
Chris Cantwell committed
1108
1109
1110
1111
1112
1113

                ASSERTL0(!elementStr.empty(), "Unable to read curve description body.");


                /// Parse out the element components corresponding to type of element.
                if(face == "F")
1114
                {
Chris Cantwell's avatar
Chris Cantwell committed
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
                    std::string typeStr = facelement->Attribute("TYPE");
                    ASSERTL0(!typeStr.empty(), "TYPE must be specified in " "points definition");
                    LibUtilities::PointsType type;
                    const std::string* begStr = LibUtilities::kPointsTypeStr;
                    const std::string* endStr = LibUtilities::kPointsTypeStr + LibUtilities::SIZE_PointsType;
                    const std::string* ptsStr = std::find(begStr, endStr, typeStr);

                    ASSERTL0(ptsStr != endStr, "Invalid points type.");
                    type = (LibUtilities::PointsType)(ptsStr - begStr);

                    std::string numptsStr = facelement->Attribute("NUMPOINTS");
                    ASSERTL0(!numptsStr.empty(), "NUMPOINTS must be specified in points definition");
                    int numPts=0;
                    std::stringstream s;
                    s << numptsStr;
                    s >> numPts;

                    CurveSharedPtr curve(MemoryManager<Curve>::AllocateSharedPtr(faceid, type));

                    ASSERTL0(numPts >= 3, "NUMPOINTS for face must be greater than 2");

                    if(numPts == 3)
                    {
                        ASSERTL0(ptsStr != endStr, "Invalid points type.");
                    }

                    // Read points (x, y, z)
                    NekDouble xval, yval, zval;
                    std::istringstream elementDataStrm(elementStr.c_str());
                    try
                    {
                        while(!elementDataStrm.fail())
                        {
                            elementDataStrm >> xval >> yval >> zval;

                            // Need to check it here because we may not be good after the read
                            // indicating that there was nothing to read.
                            if (!elementDataStrm.fail())
                            {
                                VertexComponentSharedPtr vert(MemoryManager<VertexComponent>::AllocateSharedPtr(m_meshDimension, faceindx, xval, yval, zval));
                                curve->m_points.push_back(vert);
                            }
                        }
                    }
                    catch(...)
                    {
                        NEKERROR(ErrorUtil::efatal,
                                (std::string("Unable to read curve data for FACE: ")
                        + elementStr).c_str());
                    }
                    m_curvedFaces.push_back(curve);

                    facelement = facelement->NextSiblingElement("F");

                } // end if-loop
            } // end while-loop
        } // end of ReadCurves()


        /**
         *
         */
        void MeshGraph::ReadCurves(std::string &infilename)
        {
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();

            std::stringstream errstr;
            errstr << "Unable to load file: " << infilename << std::endl;
            errstr << "Reason: " << doc.ErrorDesc() << std::endl;
            errstr << "Position: Line " << doc.ErrorRow() << ", Column " << doc.ErrorCol() << std::endl;
            ASSERTL0(loadOkay, errstr.str());

            ReadCurves(doc);
        }


        /**
         *
         */
        void MeshGraph::Write(
                const std::string &outFile,
                std::vector<FieldDefinitionsSharedPtr> &fielddefs,
                std::vector<std::vector<NekDouble> > &fielddata)
        {
            ASSERTL1(fielddefs.size() == fielddata.size(),
                    "Length of fielddefs and fielddata incompatible");

            TiXmlDocument doc;
            TiXmlDeclaration * decl = new TiXmlDeclaration("1.0", "utf-8", "");
            doc.LinkEndChild(decl);

            cout << "Writing outfile: " << outFile << endl;

            TiXmlElement * root = new TiXmlElement("NEKTAR");
            doc.LinkEndChild(root);

            for (int f = 0; f < fielddefs.size(); ++f)
            {

                ASSERTL1(fielddata[f].size() > 0,
                        "Fielddata vector must contain at least one value.");

                int datasize = CheckFieldDefinition(fielddefs[f]);
                ASSERTL1(fielddata[f].size() == fielddefs[f]->m_fields.size()
                        * datasize, "Invalid size of fielddata vector.");

                //---------------------------------------------
                // Write ELEMENTS
                TiXmlElement * elemTag = new TiXmlElement("ELEMENTS");
                root->LinkEndChild(elemTag);

                // Write FIELDS
                std::string fieldsString;
1229
                {
Chris Cantwell's avatar
Chris Cantwell committed
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
                    std::stringstream fieldsStringStream;
                    bool first = true;
                    for (std::vector<int>::size_type i = 0; i
                    < fielddefs[f]->m_fields.size(); i++)
                    {
                        if (!first)
                            fieldsStringStream << ",";
                        fieldsStringStream << fielddefs[f]->m_fields[i];
                        first = false;
                    }
                    fieldsString = fieldsStringStream.str();
1241
                }
Chris Cantwell's avatar
Chris Cantwell committed
1242
1243
1244
1245
                elemTag->SetAttribute("FIELDS", fieldsString);

                // Write SHAPE
                std::string shapeString;
1246
                {
Chris Cantwell's avatar
Chris Cantwell committed
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
                    std::stringstream shapeStringStream;
                    shapeStringStream << GeomShapeTypeMap[fielddefs[f]->m_shapeType];
                    if(fielddefs[f]->m_numHomogeneousDir == 1)
                    {
                        shapeStringStream << "-HomogenousExp1D";
                    }
                    else if (fielddefs[f]->m_numHomogeneousDir == 2)
                    {
                        shapeStringStream << "-HomogenousExp2D";
                    }

                    shapeString = shapeStringStream.str();
1259
                }
Chris Cantwell's avatar
Chris Cantwell committed
1260
1261
1262
1263
                elemTag->SetAttribute("SHAPE", shapeString);

                // Write BASIS
                std::string basisString;
1264
                {
Chris Cantwell's avatar
Chris Cantwell committed
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
                    std::stringstream basisStringStream;
                    bool first = true;
                    for (std::vector<LibUtilities::BasisType>::size_type i = 0; i < fielddefs[f]->m_basis.size(); i++)
                    {
                        if (!first)
                            basisStringStream << ",";
                        basisStringStream
                        << LibUtilities::BasisTypeMap[fielddefs[f]->m_basis[i]];
                        first = false;
                    }
                    basisString = basisStringStream.str();
1276
                }
Chris Cantwell's avatar
Chris Cantwell committed
1277
1278
1279
1280
                elemTag->SetAttribute("BASIS", basisString);

                // Write homogeneuous length details
                if(fielddefs[f]->m_numHomogeneousDir)
1281
                {
Chris Cantwell's avatar
Chris Cantwell committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
                    std::string homoLenString;
                    {
                        std::stringstream homoLenStringStream;
                        bool first = true;
                        for (int i = 0; i < fielddefs[f]->m_numHomogeneousDir; ++i)
                        {
                            if (!first)
                                homoLenStringStream << ",";
                            homoLenStringStream
                            << fielddefs[f]->m_homogeneousLengths[i];
                            first = false;
                        }
                        homoLenString = homoLenStringStream.str();
                    }
                    elemTag->SetAttribute("HOMOGENEOUSLENGTHS", homoLenString);
1297
                }
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
				
				// Write homogeneuous planes/lines details
                if(fielddefs[f]->m_numHomogeneousDir)
                {
					if(fielddefs[f]->m_homogeneousYIDs.size() > 0)
					{
						std::string homoYIDsString;
						{
							std::stringstream homoYIDsStringStream;
							bool first = true;
							for(int i = 0; i < fielddefs[f]->m_homogeneousYIDs.size(); i++)
							{
								if (!first)
									homoYIDsStringStream << ",";
								homoYIDsStringStream << fielddefs[f]->m_homogeneousYIDs[i];
								first = false;
							}
							homoYIDsString = homoYIDsStringStream.str();
						}
						elemTag->SetAttribute("HOMOGENEOUSYIDS", homoYIDsString);
					}
					
					if(fielddefs[f]->m_homogeneousZIDs.size() > 0)
					{
						std::string homoZIDsString;
						{
							std::stringstream homoZIDsStringStream;
							bool first = true;
							for(int i = 0; i < fielddefs[f]->m_homogeneousZIDs.size(); i++)
							{
								if (!first)
									homoZIDsStringStream << ",";
								homoZIDsStringStream << fielddefs[f]->m_homogeneousZIDs[i];
								first = false;
							}
							homoZIDsString = homoZIDsStringStream.str();
						}
						elemTa