MeshGraph.cpp 155 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
//
////////////////////////////////////////////////////////////////////////////////

Joe Frazier's avatar
Joe Frazier committed
36

Mike Kirby's avatar
Mike Kirby committed
37
#include <SpatialDomains/MeshGraph.h>
38
#include <LibUtilities/BasicUtils/ParseUtils.hpp>
39
40
41
42
43
#include <LibUtilities/BasicUtils/Equation.h>
#include <StdRegions/StdTriExp.h>
#include <StdRegions/StdTetExp.h>
#include <StdRegions/StdPyrExp.h>
#include <StdRegions/StdPrismExp.h>
Mike Kirby's avatar
Mike Kirby committed
44
45

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

Mike Kirby's avatar
Mike Kirby committed
50
#include <tinyxml/tinyxml.h>
51
#include <cstring>
Sophia Han's avatar
Sophia Han committed
52
#include <sstream>
Mike Kirby's avatar
Mike Kirby committed
53

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

58
59
60
61
62
// 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>
63
#include <boost/iostreams/filter/zlib.hpp>
64
65
#include <boost/iostreams/filtering_stream.hpp>

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

Chris Cantwell's avatar
Chris Cantwell committed
79
80
81
82
83
84
85

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

Chris Cantwell's avatar
Chris Cantwell committed
91
92
93
94
95
96
97

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

Chris Cantwell's avatar
Chris Cantwell committed
101

102

Chris Cantwell's avatar
Chris Cantwell committed
103
104
105
106
107
        /**
         *
         */
        MeshGraph::~MeshGraph()
        {
108
109
        }

Chris Cantwell's avatar
Chris Cantwell committed
110
111
112
113

        /**
         *
         */
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
        boost::shared_ptr<MeshGraph> MeshGraph::Read(
                const LibUtilities::SessionReaderSharedPtr &pSession)
        {
            boost::shared_ptr<MeshGraph> returnval;

            // read the geometry tag to get the dimension

            TiXmlElement* geometry_tag = pSession->GetElement("NEKTAR/GEOMETRY");
            TiXmlAttribute *attr = geometry_tag->FirstAttribute();
            int meshDim = 0;
            int err = 0;
            while (attr)
            {
                std::string attrName(attr->Name());
                if (attrName == "DIM")
                {
                    err = attr->QueryIntValue(&meshDim);
                    ASSERTL1(err==TIXML_SUCCESS, "Unable to read mesh dimension.");
                    break;
                }
                else
                {
                    std::string errstr("Unknown attribute: ");
                    errstr += attrName;
                    ASSERTL1(false, errstr.c_str());
                }

                // Get the next attribute.
                attr = attr->Next();
            }

            // instantiate the dimension-specific meshgraph classes

            switch(meshDim)
            {
            case 1:
                returnval = MemoryManager<MeshGraph1D>::AllocateSharedPtr(pSession);
                break;

            case 2:
                returnval = MemoryManager<MeshGraph2D>::AllocateSharedPtr(pSession);
                break;

            case 3:
                returnval = MemoryManager<MeshGraph3D>::AllocateSharedPtr(pSession);
                break;

            default:
                std::string err = "Invalid mesh dimension: ";
                std::stringstream strstrm;
                strstrm << meshDim;
                err += strstrm.str();
                NEKERROR(ErrorUtil::efatal, err.c_str());
            }

            return returnval;
        }



        /*  ====  OUTDATED ROUTINE, PLEASE NOT USE  ==== */
Chris Cantwell's avatar
Chris Cantwell committed
175
176
177
        boost::shared_ptr<MeshGraph> MeshGraph::Read(
                const std::string& infilename,
                bool pReadExpansions)
178
179
        {
            boost::shared_ptr<MeshGraph> returnval;
180

181
            MeshGraph mesh;
182

183
184
            mesh.ReadGeometry(infilename);
            int meshDim = mesh.GetMeshDimension();
185

186
187
188
189
190
            switch(meshDim)
            {
            case 1:
                returnval = MemoryManager<MeshGraph1D>::AllocateSharedPtr();
                break;
191

192
193
194
            case 2:
                returnval = MemoryManager<MeshGraph2D>::AllocateSharedPtr();
                break;
195

196
197
198
            case 3:
                returnval = MemoryManager<MeshGraph3D>::AllocateSharedPtr();
                break;
199

200
201
202
203
204
205
206
            default:
                std::string err = "Invalid mesh dimension: ";
                std::stringstream strstrm;
                strstrm << meshDim;
                err += strstrm.str();
                NEKERROR(ErrorUtil::efatal, err.c_str());
            }
207

208
209
210
211
            if (returnval)
            {
                returnval->ReadGeometry(infilename);
                returnval->ReadGeometryInfo(infilename);
212
213
214
215
                if (pReadExpansions)
                {
                    returnval->ReadExpansions(infilename);
                }
216
217
218
            }
            return returnval;
        }
219

Chris Cantwell's avatar
Chris Cantwell committed
220

221

Chris Cantwell's avatar
Chris Cantwell committed
222
223
224
225
        /**
         *
         */
        void MeshGraph::ReadGeometry(const std::string& infilename)
226
        {
Chris Cantwell's avatar
Chris Cantwell committed
227
228
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
229

Chris Cantwell's avatar
Chris Cantwell committed
230
231
232
233
234
            std::stringstream errstr;
            errstr << "Unable to load file: " << infilename << " (";
            errstr << doc.ErrorDesc() << ", line " << doc.ErrorRow()
                                 << ", column " << doc.ErrorCol() << ")";
            ASSERTL0(loadOkay, errstr.str());
235

Chris Cantwell's avatar
Chris Cantwell committed
236
237
            ReadGeometry(doc);
        }
238
239


Chris Cantwell's avatar
Chris Cantwell committed
240
241
242
243
244
245
246
247
248
        /**
         *
         */
        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.
249

Chris Cantwell's avatar
Chris Cantwell committed
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
            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")
272
                {
Chris Cantwell's avatar
Chris Cantwell committed
273
274
                    err = attr->QueryIntValue(&m_meshDimension);
                    ASSERTL1(err==TIXML_SUCCESS, "Unable to read mesh dimension.");
275
                }
Chris Cantwell's avatar
Chris Cantwell committed
276
                else if (attrName == "SPACE")
277
                {
Chris Cantwell's avatar
Chris Cantwell committed
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
                    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());
                }
293

Chris Cantwell's avatar
Chris Cantwell committed
294
295
296
                // Get the next attribute.
                attr = attr->Next();
            }
297

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

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

Chris Cantwell's avatar
Chris Cantwell committed
304
            NekDouble xscale,yscale,zscale;
305

Chris Cantwell's avatar
Chris Cantwell committed
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
            // 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);
            }
321

Chris Cantwell's avatar
Chris Cantwell committed
322
323
324
325
326
327
328
329
330
331
332
            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);
            }
333

Chris Cantwell's avatar
Chris Cantwell committed
334
335
336
337
338
339
340
341
342
343
344
            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);
            }
345

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

Chris Cantwell's avatar
Chris Cantwell committed
348
349
            int indx;
            int nextVertexNumber = -1;
350

Chris Cantwell's avatar
Chris Cantwell committed
351
352
353
            while (vertex)
            {
                nextVertexNumber++;
354

Chris Cantwell's avatar
Chris Cantwell committed
355
356
                TiXmlAttribute *vertexAttr = vertex->FirstAttribute();
                std::string attrName(vertexAttr->Name());
357

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

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

Chris Cantwell's avatar
Chris Cantwell committed
363
364
                // Now read body of vertex
                std::string vertexBodyStr;
Spencer Sherwin's avatar
Spencer Sherwin committed
365

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

Chris Cantwell's avatar
Chris Cantwell committed
368
369
370
371
372
373
374
375
                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
376

Chris Cantwell's avatar
Chris Cantwell committed
377
378
                    vertexBody = vertexBody->NextSibling();
                }
Spencer Sherwin's avatar
Spencer Sherwin committed
379

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

Chris Cantwell's avatar
Chris Cantwell committed
382
383
384
                // Get vertex data from the data string.
                NekDouble xval, yval, zval;
                std::istringstream vertexDataStrm(vertexBodyStr.c_str());
Spencer Sherwin's avatar
Spencer Sherwin committed
385

Chris Cantwell's avatar
Chris Cantwell committed
386
387
388
389
390
391
392
393
394
                try
                {
                    while(!vertexDataStrm.fail())
                    {
                        vertexDataStrm >> xval >> yval >> zval;

                        xval *= xscale;
                        yval *= yscale;
                        zval *= zscale;
395
                        
Chris Cantwell's avatar
Chris Cantwell committed
396
397
398
399
                        // Need to check it here because we may not be
                        // good after the read indicating that there
                        // was nothing to read.
                        if (!vertexDataStrm.fail())
400
                        {
Chris Cantwell's avatar
Chris Cantwell committed
401
402
                            VertexComponentSharedPtr vert(MemoryManager<VertexComponent>::AllocateSharedPtr(m_spaceDimension, indx, xval, yval, zval));
                            m_vertSet[indx] = vert;
403
                        }
404
405
                    }
                }
Chris Cantwell's avatar
Chris Cantwell committed
406
                catch(...)
407
                {
Chris Cantwell's avatar
Chris Cantwell committed
408
                    ASSERTL0(false, "Unable to read VERTEX data.");
409
                }
Chris Cantwell's avatar
Chris Cantwell committed
410
411

                vertex = vertex->NextSiblingElement("V");
412
            }
413
        }
414
415


Chris Cantwell's avatar
Chris Cantwell committed
416
417
418
419
420
421
422
        /**
         * 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)
423
        {
Chris Cantwell's avatar
Chris Cantwell committed
424
425
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
426

Chris Cantwell's avatar
Chris Cantwell committed
427
428
429
430
431
            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());
432

Chris Cantwell's avatar
Chris Cantwell committed
433
434
            ReadGeometryInfo(doc);
        }
435
436


Chris Cantwell's avatar
Chris Cantwell committed
437
438
439
440
441
442
443
444
445
446
        /**
         * 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.");
447

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
458
459
460
461
462
463
464
            // 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);
465

Chris Cantwell's avatar
Chris Cantwell committed
466
467
468
469
470
471
                ASSERTL0(x == m_geomInfo.end(),
                        "Property " + geomProperty + " already specified.");
                m_geomInfo[geomProperty] = geomValue;
                infoItem = infoItem->NextSiblingElement("I");
            }
        }
472
473


Chris Cantwell's avatar
Chris Cantwell committed
474
475
476
477
478
479
480
        /**
         *
         */
        void MeshGraph::ReadExpansions(const std::string& infilename)
        {
            TiXmlDocument doc(infilename);
            bool loadOkay = doc.LoadFile();
481

Chris Cantwell's avatar
Chris Cantwell committed
482
483
484
485
486
            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());
487

Chris Cantwell's avatar
Chris Cantwell committed
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
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
534
535
            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.");
536
                        }
Chris Cantwell's avatar
Chris Cantwell committed
537
538
539
540
541

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

Chris Cantwell's avatar
Chris Cantwell committed
545
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
546

Chris Cantwell's avatar
Chris Cantwell committed
547
548
549
                            // make sure all fields in this search point
                            // to same expansion vector;
                            for(i = 0; i < fieldStrings.size(); ++i)
550
                            {
Chris Cantwell's avatar
Chris Cantwell committed
551
                                m_expansionMapShPtrMap[fieldStrings[i]] = expansionMap;
552
553
                            }
                        }
Chris Cantwell's avatar
Chris Cantwell committed
554
                        else // default variable is defined
555
                        {
556

Chris Cantwell's avatar
Chris Cantwell committed
557
                            if(fieldStrings.size()) // fields are defined
558
                            {
Chris Cantwell's avatar
Chris Cantwell committed
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
                                //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");
                                        }
                                    }
                                }
581
                            }
Chris Cantwell's avatar
Chris Cantwell committed
582
                            else // use default variable list
583
                            {
Chris Cantwell's avatar
Chris Cantwell committed
584
                                expansionMap = m_expansionMapShPtrMap.find("DefaultVar")->second;
585
                            }
586
587

                        }
588

Chris Cantwell's avatar
Chris Cantwell committed
589
590
591
592
593
594
                        /// 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);
595

Chris Cantwell's avatar
Chris Cantwell committed
596
597
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
598

Chris Cantwell's avatar
Chris Cantwell committed
599
600
601
                        bool          useExpansionType = false;
                        ExpansionType expansion_type;
                        int           num_modes;
602

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

Chris Cantwell's avatar
Chris Cantwell committed
606
607
608
609
610
611
                        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);
612

Chris Cantwell's avatar
Chris Cantwell committed
613
614
                            ASSERTL0(expStr != endStr, "Invalid expansion type.");
                            expansion_type = (ExpansionType)(expStr - begStr);
615
616


617
618
619
620
                            /// \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
621
                            /// is not defined -> no info about command line arguments presented
622
623
                            /// ASSERTL0(m_session != 0, "One needs to instantiate SessionReader first");

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

628
629
630
631
632
633
634
635
636
637
                            // ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
                            if (m_session)
                            {
                                LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
                                num_modes = (int) nummodesEqn.Evaluate();
                            }
                            else
                            {
                                num_modes = boost::lexical_cast<int>(nummodesStr);
                            }
638

Chris Cantwell's avatar
Chris Cantwell committed
639
640
641
642
643
644
645
646
                            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;
647

Chris Cantwell's avatar
Chris Cantwell committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
                            // 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");
669

Chris Cantwell's avatar
Chris Cantwell committed
670
671
672
673
674
                            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");
675

Chris Cantwell's avatar
Chris Cantwell committed
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
                            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());
                            }
698

Chris Cantwell's avatar
Chris Cantwell committed
699
700
701
702
703
704
705
                            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");
706

Chris Cantwell's avatar
Chris Cantwell committed
707
708
709
710
711
712
713
                            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));
                            }
                        }
714

Chris Cantwell's avatar
Chris Cantwell committed
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
                        // 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;
                                }
                            }
                        }
738

Chris Cantwell's avatar
Chris Cantwell committed
739
740
741
742
743
744
745
                        expansion = expansion->NextSiblingElement("E");
                    }
                }
                else if(expType == "H")
                {
                    int i;
                    ExpansionMapShPtr expansionMap;
746

Chris Cantwell's avatar
Chris Cantwell committed
747
748
                    while (expansion)
                    {
749

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

Chris Cantwell's avatar
Chris Cantwell committed
753
754
755
756
757
758
                        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.");
                        }
759

Chris Cantwell's avatar
Chris Cantwell committed
760
761
762
763
764
765
                        // 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();
766

Chris Cantwell's avatar
Chris Cantwell committed
767
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
768

Chris Cantwell's avatar
Chris Cantwell committed
769
770
771
772
773
774
775
776
777
                            // 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
                        {
778

Chris Cantwell's avatar
Chris Cantwell committed
779
780
781
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
                            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;
                            }
808

Chris Cantwell's avatar
Chris Cantwell committed
809
                        }
810

Chris Cantwell's avatar
Chris Cantwell committed
811
812
813
814
815
816
                        /// 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);
817

Chris Cantwell's avatar
Chris Cantwell committed
818
819
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
820

Chris Cantwell's avatar
Chris Cantwell committed
821
822
823
                        bool          useExpansionType = false;
                        ExpansionType expansion_type_x, expansion_type_y, expansion_type_z;
                        int           num_modes_x, num_modes_y, num_modes_z;
824

Chris Cantwell's avatar
Chris Cantwell committed
825
                        LibUtilities::BasisKeyVector basiskeyvec;
826

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

Chris Cantwell's avatar
Chris Cantwell committed
829
                        if(tStr_x) // use type string to define expansion
830
                        {
Chris Cantwell's avatar
Chris Cantwell committed
831
832
833
834
                            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);
835

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

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

843
844
845
846
847
848
849
850
851
852
853
                            // ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");

                            if (m_session)
                            {
                                LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
                                num_modes_x = (int) nummodesEqn.Evaluate();
                            }
                            else
                            {
                                num_modes_x = boost::lexical_cast<int>(nummodesStr);
                            }
854

Chris Cantwell's avatar
Chris Cantwell committed
855
                        }
856

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

Chris Cantwell's avatar
Chris Cantwell committed
859
860
861
862
863
864
865
866
867
868
869
870
871
872
                        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;

873
874
875
876
877
878
879
880
881
882
                            // ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
                            if (m_session)
                            {
                                LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
                                num_modes_y = (int) nummodesEqn.Evaluate();
                            }
                            else
                            {
                                num_modes_y = boost::lexical_cast<int>(nummodesStr);
                            }
Chris Cantwell's avatar
Chris Cantwell committed
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901

                        }

                        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;

902
903
904
905
906
907
908
909
910
911
                            // ASSERTL0(m_session,"Session should be defined to evaluate nummodes ");
                            if (m_session)
                            {
                                LibUtilities::Equation nummodesEqn(m_session, nummodesStr);
                                num_modes_z = (int) nummodesEqn.Evaluate();
                            }
                            else
                            {
                                num_modes_z = boost::lexical_cast<int>(nummodesStr);
                            }
Chris Cantwell's avatar
Chris Cantwell committed
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950

                        }

                        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");
                }
            }
951
        }
952

Chris Cantwell's avatar
Chris Cantwell committed
953
954
955
956
957

        /**
         *
         */
        void MeshGraph::ReadDomain(TiXmlDocument &doc)
958
        {
Chris Cantwell's avatar
Chris Cantwell committed
959
            TiXmlHandle docHandle(&doc);
960

Chris Cantwell's avatar
Chris Cantwell committed
961
962
963
964
965
966
967
968
969
970
971
972
973
            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)
974
            {
Chris Cantwell's avatar
Chris Cantwell committed
975
                elementChild = elementChild->NextSibling();
976
            }
977

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

Chris Cantwell's avatar
Chris Cantwell committed
981
            elementStr = elementStr.substr(elementStr.find_first_not_of(" "));
982

Chris Cantwell's avatar
Chris Cantwell committed
983
984
985
            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);
986

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

Chris Cantwell's avatar
Chris Cantwell committed
989
990
991
992
            // 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());
993
        }
994

Chris Cantwell's avatar
Chris Cantwell committed
995
996
997
998
999

        /**
         *
         */
        void MeshGraph::ReadCurves(TiXmlDocument &doc)
1000
        {
Chris Cantwell's avatar
Chris Cantwell committed
1001
1002
1003
1004
            /// 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;
1005

Chris Cantwell's avatar
Chris Cantwell committed
1006
1007
1008
1009
            // 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.");
1010

Chris Cantwell's avatar
Chris Cantwell committed
1011
            NekDouble xscale,yscale,zscale;
1012

Chris Cantwell's avatar
Chris Cantwell committed
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
            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);
            }
1025

Chris Cantwell's avatar
Chris Cantwell committed
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
            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);
            }
1037

Chris Cantwell's avatar
Chris Cantwell committed
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
            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);
            }
1049

Chris Cantwell's avatar
Chris Cantwell committed
1050
            int err;
1051

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

Chris Cantwell's avatar
Chris Cantwell committed
1055
1056
1057
1058
1059
1060
1061
1062
            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).
1063

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

Chris Cantwell's avatar
Chris Cantwell committed
1066
1067
            int edgeindx, edgeid;
            int nextEdgeNumber = -1;
1068

Chris Cantwell's avatar
Chris Cantwell committed
1069
            while(edgelement)
1070
            {
Chris Cantwell's avatar
Chris Cantwell committed
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
                /// 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)
1090
                {
Chris Cantwell's avatar
Chris Cantwell committed
1091
1092
                    // Accumulate all non-comment element data
                    if (elementChild->Type() == TiXmlNode::TEXT)
1093
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1094
1095
                        elementStr += elementChild->ToText()->ValueStr();
                        elementStr += " ";
1096
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
                    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
1127
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
                        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);
                            }

                        }
1146
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1147
                    catch(...)
1148
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1149
1150
1151
                        NEKERROR(ErrorUtil::efatal,
                                (std::string("Unable to read curve data for EDGE: ") + elementStr).c_str());

1152
                    }
Chris Cantwell's avatar
Chris Cantwell committed
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169

                    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)
1170
            {
Chris Cantwell's avatar
Chris Cantwell committed
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
                /// 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)
1192
                {
Chris Cantwell's avatar
Chris Cantwell committed
1193
1194
                    // Accumulate all non-comment element data
                    if (elementChild->Type() == TiXmlNode::TEXT)
1195
                    {
Chris Cantwell's avatar
Chris Cantwell committed
1196
1197
1198
1199
                        elementStr += elementChild->ToText()->ValueStr();
                        elementStr += " ";
                    }
                    elementChild = elementChild->NextSibling();
1200
                }
Chris Cantwell's avatar
Chris Cantwell committed
1201
1202
1203
1204
1205
1206

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


                /// Parse out the element components corresponding to type of element.
                if(face == "F")
1207
                {
Chris Cantwell's avatar
Chris Cantwell committed
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
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
                    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;
1322
                {
Chris Cantwell's avatar
Chris Cantwell committed
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
                    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();
1334
                }
Chris Cantwell's avatar
Chris Cantwell committed
1335
1336
1337
1338
                elemTag->SetAttribute("FIELDS", fieldsString);

                // Write SHAPE
                std::string shapeString;
1339
                {
Chris Cantwell's avatar
Chris Cantwell committed
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
                    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";
                    }