MeshGraph.cpp 123 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
        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;
            while (attr)
            {
                std::string attrName(attr->Name());
                if (attrName == "DIM")
                {
129
                    int err = attr->QueryIntValue(&meshDim);
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
                    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
174
175
176
        boost::shared_ptr<MeshGraph> MeshGraph::Read(
                const std::string& infilename,
                bool pReadExpansions)
177
178
        {
            boost::shared_ptr<MeshGraph> returnval;
179

180
            MeshGraph mesh;
181

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

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

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

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
219

220

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

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

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


Chris Cantwell's avatar
Chris Cantwell committed
239
240
241
242
243
244
245
246
        /**
         *
         */
        void MeshGraph::ReadGeometry(TiXmlDocument &doc)
        {
            TiXmlHandle docHandle(&doc);
            TiXmlElement* mesh = NULL;
            TiXmlElement* master = NULL;    // Master tag within which all data is contained.
247

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

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
302
            NekDouble xscale,yscale,zscale;
303

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

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
346
347
            int indx;
            int nextVertexNumber = -1;
348

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

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

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

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

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

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

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

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

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

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

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

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

                vertex = vertex->NextSiblingElement("V");
410
            }
411
        }
412
413


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

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

Chris Cantwell's avatar
Chris Cantwell committed
431
432
            ReadGeometryInfo(doc);
        }
433
434


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

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

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

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

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

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


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

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

Chris Cantwell's avatar
Chris Cantwell committed
486
487
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
            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.");
534
                        }
Chris Cantwell's avatar
Chris Cantwell committed
535
536
537
538
539

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

Chris Cantwell's avatar
Chris Cantwell committed
543
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
544

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

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

                        }
586

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

Chris Cantwell's avatar
Chris Cantwell committed
594
595
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
596

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

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

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

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


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

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

626
627
628
629
630
631
632
633
634
635
                            // 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);
                            }
636

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

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

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

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

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

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
745
746
                    while (expansion)
                    {
747

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

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

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

Chris Cantwell's avatar
Chris Cantwell committed
765
                            m_expansionMapShPtrMap["DefaultVar"] = expansionMap;
766

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

Chris Cantwell's avatar
Chris Cantwell committed
777
778
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
                            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;
                            }
806

Chris Cantwell's avatar
Chris Cantwell committed
807
                        }
808

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

Chris Cantwell's avatar
Chris Cantwell committed
816
817
                        CompositeMap compositeVector;
                        GetCompositeList(compositeListStr, compositeVector);
818

819
820
821
822
823
824
                        ExpansionType expansion_type_x = eNoExpansionType;
                        ExpansionType expansion_type_y = eNoExpansionType;
                        ExpansionType expansion_type_z = eNoExpansionType;
                        int           num_modes_x = 0;
                        int           num_modes_y = 0;
                        int           num_modes_z = 0;
825

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

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

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

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

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

844
845
846
847
848
849
850
851
852
853
854
                            // 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);
                            }
855

Chris Cantwell's avatar
Chris Cantwell committed
856
                        }
857

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

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

874
875
876
877
878
879
880
881
882
883
                            // 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
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902

                        }

                        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;

903
904
905
906
907
908
909
910
911
912
                            // 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
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

                        }

                        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
                {
942
943
                    std::vector<LibUtilities::FieldDefinitionsSharedPtr> fielddefs;
                    LibUtilities::ImportFieldDefs(doc, fielddefs, true);
Chris Cantwell's avatar
Chris Cantwell committed
944
945
946
947
948
949
950
951
                    cout << "    Number of elements: " << fielddefs.size() << endl;
                    SetExpansions(fielddefs);
                }
                else
                {
                    ASSERTL0(false,"Expansion type not defined");
                }
            }
952
        }
953

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

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

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

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

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

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

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

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

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

        /**
         *
         */
        void MeshGraph::ReadCurves(TiXmlDocument &doc)