SessionReader.cpp 83.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
///////////////////////////////////////////////////////////////////////////////
//
// File SessionReader.cpp
//
// For more information, please see: http://www.nektar.info
//
// The MIT License
//
// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
// Department of Aeronautics, Imperial College London (UK), and Scientific
// Computing and Imaging Institute, University of Utah (USA).
//
// License for the specific language governing rights and limitations under
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included
Dave Moxey's avatar
Dave Moxey committed
22
// in all copies or substantial portions of the Software.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
//
// Description: Session reader
//
///////////////////////////////////////////////////////////////////////////////

#ifndef TIXML_USE_STL
#define TIXML_USE_STL
#endif

40
#include <LibUtilities/BasicUtils/SessionReader.h>
41
#include <LibUtilities/BasicConst/GitRevision.h>
42

43
#include <iostream>
44
#include <fstream>
45 46 47
#include <string>
using namespace std;

48 49 50
#include <boost/iostreams/filtering_streambuf.hpp>
#include <boost/iostreams/copy.hpp>
#include <boost/iostreams/filter/gzip.hpp>
51
#include <boost/algorithm/string.hpp>
52
#include <tinyxml.h>
53
#include <LibUtilities/BasicUtils/ErrorUtil.hpp>
54 55
#include <LibUtilities/BasicUtils/Equation.h>
#include <LibUtilities/Memory/NekMemoryManager.hpp>
56
#include <LibUtilities/BasicUtils/ParseUtils.h>
57
#include <LibUtilities/BasicUtils/FileSystem.h>
58

59
#include <boost/program_options.hpp>
60
#include <boost/format.hpp>
61

62 63 64 65
#ifndef NEKTAR_VERSION
#define NEKTAR_VERSION "Unknown"
#endif

66
namespace po = boost::program_options;
67
namespace io = boost::iostreams;
68

69 70 71 72
namespace Nektar
{
    namespace LibUtilities
    {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
        /**
         * @class SessionReader
         *
         * This class provides an interface to Nektar++-specific content in a
         * supplied XML document. It also initialises a Nektar++ session
         * including setting up communication for parallel execution and where
         * necessary partitioning the supplied mesh for running across multiple
         * processes.
         *
         * A session should be initialised at the beginning of a user's
         * application by passing the command-line arguments. This not only
         * allows the SessionReader to extract the name of the XML document to
         * load containing Nektar++ session information, but also supplies the
         * MPI arguments necessary for setting up parallel communication. The
         * SessionReader should be initialised using the #CreateInstance
         * function:
         * @code
         * LibUtilities::SessionReaderSharedPtr vSession
         *          = LibUtilities::SessionReader::CreateInstance(argc, argv);
         * @endcode
         * The instance \c vSession can now be passed to other key Nektar++
         * components during their construction.
         * @note At the end of the user application, it is important to call the
         * #Finalise routine in order to finalise any MPI communication and
         * correctly free resources.
         *
         * The SessionReader class provides streamlined, validated access to
         * session parameters, solver information and functions defined within a
         * Nektar++ XML document. The available routines and their usage is
         * documented below.
         *
         * In the case of solver information properties, the classes to which
         * these parameters are pertinent may register with the SessionReader
         * class the set of valid values for a given property. Such values may
         * also be associated with an enumeration value for more transparent use
         * of the property values in code.
         */

111 112 113 114 115 116 117 118 119 120 121 122 123
        /**
         * This map of maps stores the list of valid string values for a number
         * of solver information parameters. The top level map connects
         * different parameter names to their list of possible values. The list
         * of possible values is also a map, mapping a valid string to a
         * corresponding enum value.
         *
         * This list is populated through the #RegisterEnumValue static member
         * function which is called statically from various classes to register
         * the valid values for solver info parameters associated with them. The
         * map is therefore fully populated before the SessionReader class is
         * instantiated and a file is read in and parsed.
         */
124 125 126 127 128
        EnumMapList& SessionReader::GetSolverInfoEnums()
        {
            static EnumMapList solverInfoEnums;
            return solverInfoEnums;
        }
129 130 131 132 133 134 135 136 137 138


        /**
         * List of default values for solver information parameters to be used
         * in the case of them not being provided.
         *
         * This list is populated through the #RegisterDefaultSolverInfo static
         * member variable which is called statically from various classes to
         * register the default value for a given parameter.
         */
139 140 141 142 143
        SolverInfoMap& SessionReader::GetSolverInfoDefaults()
        {
            static SolverInfoMap solverInfoMap;
            return solverInfoMap;
        }
144 145


146 147 148 149 150 151 152
        /**
         * List of values for GlobalSysSoln parameters to be used to override
         * details given in SolverInfo
         *
         * This list is populated by ReadGlobalSysSoln if the
         * GLOBALSYSSOLNINFO section is defined in the input file.
         * This List allows for details to define for the Global Sys
153
         * solver for each variable.
154
         */
155 156 157 158 159
        GloSysSolnInfoList& SessionReader::GetGloSysSolnList()
        {
            static GloSysSolnInfoList gloSysSolnInfoList;
            return gloSysSolnInfoList;
        }
160

161 162 163 164 165 166 167 168
        /**
         * Lists the possible command-line argument which can be specified for
         * this executable.
         *
         * This list is populated through the #RegisterCmdLineArgument static
         * member function which is called statically from various classes to
         * register command-line arguments they need.
         */
169 170 171 172 173
        CmdLineArgMap& SessionReader::GetCmdLineArgMap()
        {
            static CmdLineArgMap cmdLineArguments;
            return cmdLineArguments;
        }
174 175


176 177 178 179 180 181 182 183
        /**
         * This constructor parses the command-line arguments given to the user
         * application to set up any MPI communication, read supplied XML
         * session files, and partition meshes where necessary.
         *
         * @param   argc        Number of command-line arguments
         * @param   argv        Array of command-line arguments
         */
184 185
        SessionReader::SessionReader(int argc, char *argv[])
        {
Chris Cantwell's avatar
Chris Cantwell committed
186
            m_xmlDoc    = 0;
187
            m_filenames = ParseCommandLineArguments(argc, argv);
188

189
            ASSERTL0(m_filenames.size() > 0, "No session file(s) given.");
190

191
            m_sessionName = ParseSessionName(m_filenames);
192

193
            // Create communicator
194
            CreateComm(argc, argv);
195

196 197
            TestSharedFilesystem();

198 199 200 201
            // If running in parallel change the default global sys solution
            // type.
            if (m_comm->GetSize() > 1)
            {
202
                GetSolverInfoDefaults()["GLOBALSYSSOLN"] =
203 204
                    "IterativeStaticCond";
            }
205 206 207 208 209 210 211

            m_exprEvaluator = MemoryManager<AnalyticExpressionEvaluator>
                ::AllocateSharedPtr();
            m_exprEvaluator->SetRandomSeed((m_comm->GetRank() + 1) * time(NULL));

            // Split up the communicator
            PartitionComm();
212 213 214
        }


215 216 217
        /**
         *
         */
218
        SessionReader::SessionReader(
219 220 221
            int                             argc,
            char                           *argv[],
            const std::vector<std::string> &pFilenames,
222
            const CommSharedPtr            &pComm)
223 224 225
        {
            ASSERTL0(pFilenames.size() > 0, "No filenames specified.");

226
            ParseCommandLineArguments(argc, argv);
Chris Cantwell's avatar
Chris Cantwell committed
227
            m_xmlDoc      = 0;
228
            m_filenames   = pFilenames;
229

230
            m_sessionName = ParseSessionName(m_filenames);
231

232
            // Create communicator
233 234
            if (!pComm.get())
            {
235
                CreateComm(argc, argv);
236 237 238 239 240
            }
            else
            {
                m_comm = pComm;
            }
241

242 243
            TestSharedFilesystem();

244 245 246 247
            // If running in parallel change the default global sys solution
            // type.
            if (m_comm->GetSize() > 1)
            {
248
                GetSolverInfoDefaults()["GLOBALSYSSOLN"] =
249
                    "IterativeStaticCond";
250
            }
251 252 253 254 255 256 257

            m_exprEvaluator = MemoryManager<AnalyticExpressionEvaluator>
                ::AllocateSharedPtr();
            m_exprEvaluator->SetRandomSeed((m_comm->GetRank() + 1) * time(NULL));

            // Split up the communicator
            PartitionComm();
258 259 260
        }


261 262 263
        /**
         *
         */
264 265
        SessionReader::~SessionReader()
        {
266 267 268 269
            if (m_xmlDoc)
            {
                delete m_xmlDoc;
            }
270 271
        }

272 273 274 275 276 277 278

        /**
         * Performs the main initialisation of the object. The XML file provided
         * on the command-line is loaded and any mesh partitioning is done. The
         * resulting process-specific XML file (containing the process's
         * geometry partition) is then reloaded and parsed.
         */
279 280
        void SessionReader::InitSession(
            const std::vector<std::string> &filenames)
281
        {
282 283 284 285 286
            // Re-load filenames for session if required.
            if (filenames.size() > 0)
            {
                m_filenames = filenames;
            }
287

288 289 290 291 292
            // Merge document if required.
            if (m_xmlDoc)
            {
                delete m_xmlDoc;
            }
293

Dave Moxey's avatar
Dave Moxey committed
294
            m_xmlDoc = MergeDoc(m_filenames);
295

296 297
            // Parse the XML data in #m_xmlDoc
            ParseDocument();
298 299 300 301

            // Override SOLVERINFO and parameters with any specified on the
            // command line.
            CmdLineOverride();
302

303 304 305
            // Verify SOLVERINFO values
            VerifySolverInfo();

306 307 308
            // In verbose mode, print out parameters and solver info sections
            if (m_verbose && m_comm)
            {
309
                if (m_comm->TreatAsRankZero() && m_parameters.size() > 0)
310 311
                {
                    cout << "Parameters:" << endl;
312
                    for (auto &x : m_parameters)
313
                    {
314
                        cout << "\t" << x.first << " = " << x.second << endl;
315 316 317 318
                    }
                    cout << endl;
                }

319
                if (m_comm->TreatAsRankZero() && m_solverInfo.size() > 0)
320 321
                {
                    cout << "Solver Info:" << endl;
322
                    for (auto &x : m_solverInfo)
323
                    {
324
                        cout << "\t" << x.first << " = " << x.second << endl;
325 326 327 328
                    }
                    cout << endl;
                }
            }
329 330
        }

331 332 333 334 335 336 337 338
        void SessionReader::TestSharedFilesystem()
        {
            m_sharedFilesystem = false;

            if (m_comm->GetSize() > 1)
            {
                if (m_comm->GetRank() == 0)
                {
Kilian Lackhove's avatar
Kilian Lackhove committed
339
                    std::ofstream testfile("shared-fs-testfile");
340
                    testfile << "" << std::endl;
Kilian Lackhove's avatar
Kilian Lackhove committed
341
                    ASSERTL1(!testfile.fail(), "Test file creation failed");
342 343 344 345
                    testfile.close();
                }
                m_comm->Block();

Kilian Lackhove's avatar
Kilian Lackhove committed
346
                int exists = (bool)boost::filesystem::exists("shared-fs-testfile");
347 348 349 350
                m_comm->AllReduce(exists, LibUtilities::ReduceSum);

                m_sharedFilesystem = (exists == m_comm->GetSize());

Kilian Lackhove's avatar
Kilian Lackhove committed
351 352
                if ((m_sharedFilesystem && m_comm->GetRank() == 0) ||
                    !m_sharedFilesystem)
353 354 355 356 357 358 359 360 361 362 363
                {
                    std::remove("shared-fs-testfile");
                }
            }
            else
            {
                m_sharedFilesystem = false;
            }

            if (m_verbose && m_comm->GetRank() == 0 && m_sharedFilesystem)
            {
Kilian Lackhove's avatar
Kilian Lackhove committed
364
                cout << "Shared filesystem detected" << endl;
365 366 367 368
            }
        }


369
        /**
370 371
         * @brief Parses the command-line arguments for known options and
         * filenames.
372
         */
373 374
        std::vector<std::string> SessionReader::ParseCommandLineArguments(
            int argc, char *argv[])
375 376 377 378
        {
            // List the publically visible options (listed using --help)
            po::options_description desc("Allowed options");
            desc.add_options()
379
                ("verbose,v",    "be verbose")
380
                ("version,V",    "print version information")
381
                ("help,h",       "print this help message")
382
                ("solverinfo,I", po::value<vector<std::string> >(),
383 384 385
                                 "override a SOLVERINFO property")
                ("parameter,P",  po::value<vector<std::string> >(),
                                 "override a parameter")
386 387 388 389 390 391
                ("npx",          po::value<int>(),
                                 "number of procs in X-dir")
                ("npy",          po::value<int>(),
                                 "number of procs in Y-dir")
                ("npz",          po::value<int>(),
                                 "number of procs in Z-dir")
392
                ("nsz",          po::value<int>(),
393
                                 "number of slices in Z-dir")
394 395
                ("part-only",    po::value<int>(),
                                 "only partition mesh into N partitions.")
396 397
                ("part-only-overlapping",    po::value<int>(),
                                 "only partition mesh into N overlapping partitions.")
398
                ("part-info",    "Output partition information")
399
            ;
400

401
            for (auto &cmdIt : GetCmdLineArgMap())
402
            {
403 404
                std::string names = cmdIt.first;
                if (cmdIt.second.shortName != "")
405
                {
406
                    names += "," + cmdIt.second.shortName;
407
                }
408
                if (cmdIt.second.isFlag)
409 410
                {
                    desc.add_options()
411
                        (names.c_str(), cmdIt.second.description.c_str())
412 413 414 415 416 417
                    ;
                }
                else
                {
                    desc.add_options()
                        (names.c_str(), po::value<std::string>(),
418
                         cmdIt.second.description.c_str())
419 420
                    ;
                }
421
            }
422 423 424 425 426

            // List hidden options (e.g. session file arguments are not actually
            // specified using the input-file option by the user).
            po::options_description hidden("Hidden options");
            hidden.add_options()
427
                    ("input-file", po::value< vector<string> >(),
428
                                   "input filename")
429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452
            ;

            // Combine all options for the parser
            po::options_description all("All options");
            all.add(desc).add(hidden);

            // Session file is a positional option
            po::positional_options_description p;
            p.add("input-file", -1);

            // Parse the command-line options
            po::parsed_options parsed = po::command_line_parser(argc, argv).
                                                options(all).
                                                positional(p).
                                                allow_unregistered().
                                                run();

            // Extract known options to map and update
            po::store(parsed, m_cmdLineOptions);
            po::notify(m_cmdLineOptions);

            // Help message
            if (m_cmdLineOptions.count("help"))
            {
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
                cout << desc;
                exit(0);
            }

            // Version information
            if (m_cmdLineOptions.count("version"))
            {
                cout << "Nektar++ version " << NEKTAR_VERSION;

                if (NekConstants::kGitSha1 != "GITDIR-NOTFOUND")
                {
                    string sha1(NekConstants::kGitSha1);
                    string branch(NekConstants::kGitBranch);
                    boost::replace_all(branch, "refs/heads/", "");

Dave Moxey's avatar
Dave Moxey committed
468 469 470 471 472 473 474 475 476 477 478 479
                    cout << " (git changeset " << sha1.substr(0, 8) << ", ";

                    if (branch == "")
                    {
                        cout << "detached head";
                    }
                    else
                    {
                        cout << "head " << branch;
                    }

                    cout << ")";
480 481 482
                }

                cout << endl;
483 484 485 486 487 488 489 490 491 492 493 494
                exit(0);
            }

            // Enable verbose mode
            if (m_cmdLineOptions.count("verbose"))
            {
                m_verbose = true;
            }
            else
            {
                m_verbose = false;
            }
495

496
            // Print a warning for unknown options
497
            for (auto &x : parsed.options)
498
            {
499
                if (x.unregistered)
500
                {
501
                    cout << "Warning: Unknown option: " << x.string_key
502
                         << endl;
503 504 505 506 507 508
                }
            }

            // Return the vector of filename(s) given as positional options
            if (m_cmdLineOptions.count("input-file"))
            {
509 510
                return m_cmdLineOptions["input-file"].as<
                    std::vector<std::string> >();
511 512 513 514 515 516 517 518
            }
            else
            {
                return std::vector<std::string>();
            }
        }


519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556
        /**
         *
         */
        std::string SessionReader::ParseSessionName(
                std::vector<std::string> &filenames)
        {
            ASSERTL0(!filenames.empty(),
                     "At least one filename expected.");

            std::string retval = "";

            // First input file defines the session name
            std::string fname = filenames[0];

            // If loading a pre-partitioned mesh, remove _xml extension
            if (fname.size() > 4 &&
                fname.substr(fname.size() - 4, 4) == "_xml")
            {
                retval = fname.substr(0, fname.find_last_of("_"));
            }
            // otherwise remove the .xml extension
            else if (fname.size() > 4 &&
                fname.substr(fname.size() - 4, 4) == ".xml")
            {
                retval = fname.substr(0, fname.find_last_of("."));
            }
            // If compressed .xml.gz, remove both extensions
            else if (fname.size() > 7 &&
                fname.substr(fname.size() - 7, 7) == ".xml.gz")
            {
                retval = fname.substr(0, fname.find_last_of("."));
                retval = retval.substr(0, retval.find_last_of("."));
            }

            return retval;
        }


557 558 559
        /**
         *
         */
560 561
        TiXmlDocument& SessionReader::GetDocument()
        {
562
            ASSERTL1(m_xmlDoc, "XML Document not defined.");
563 564 565
            return *m_xmlDoc;
        }

566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

        /**
         * The single parameter specifies a path to the requested element in a
         * similar format to the filesystem path. Given the following XML:
         * @code
         * <NEKTAR>
         *   <CONDITIONS>
         *     <PARAMETERS>
         *     ...
         *     </PARAMETERS>
         *   </CONDITIONS>
         * </NEKTAR>
         * @endcode
         * the PARAMETERS element would be retrieved by requesting the path:
         * @code
         * Nektar/Conditions/Parameters
         * @endcode
         * @note Paths are case-insensitive.
         *
         * @param   pPath       Path to requested element.
586 587
         *
         * @return Direct pointer to requested XML Element.
588
         */
589 590 591
        TiXmlElement* SessionReader::GetElement(const string& pPath)
        {
            std::string vPath = boost::to_upper_copy(pPath);
592 593 594
            std::vector<std::string> st;
            boost::split(st, vPath, boost::is_any_of("\\/ "));
            ASSERTL0(st.size() > 0, "No path given in XML element request.");
595

596
            TiXmlElement* vReturn = m_xmlDoc->FirstChildElement(st[0].c_str());
597
            ASSERTL0(vReturn, std::string("Cannot find element '")
598 599
                              + st[0] + std::string("'."));
            for (int i = 1; i < st.size(); ++i)
600
            {
601
                vReturn = vReturn->FirstChildElement(st[i].c_str());
602
                ASSERTL0(vReturn, std::string("Cannot find element '")
603
                                  + st[i] + std::string("'."));
604 605 606 607
            }
            return vReturn;
        }

608 609 610 611 612

        /**
         *
         */
        bool SessionReader::DefinesElement(const std::string &pPath) const
613 614
        {
            std::string vPath = boost::to_upper_copy(pPath);
615 616 617
            std::vector<std::string> st;
            boost::split(st, vPath, boost::is_any_of("\\/ "));
            ASSERTL0(st.size() > 0, "No path given in XML element request.");
618

619
            TiXmlElement* vReturn = m_xmlDoc->FirstChildElement(st[0].c_str());
620
            ASSERTL0(vReturn, std::string("Cannot find element '")
621 622
                              + st[0] + std::string("'."));
            for (int i = 1; i < st.size(); ++i)
623
            {
624
                vReturn = vReturn->FirstChildElement(st[i].c_str());
625 626 627 628 629
                if (!vReturn) return false;
            }
            return true;
        }

630 631 632 633

        /**
         *
         */
634
        const std::vector<std::string>& SessionReader::GetFilenames() const
635
        {
636
            return m_filenames;
637 638
        }

639 640 641 642

        /**
         *
         */
643 644 645 646 647
        const std::string& SessionReader::GetSessionName() const
        {
            return m_sessionName;
        }

648

649 650 651 652 653 654
        /**
         * Output is of the form [sessionName]_P[idx] where idx is the rank
         * of the process.
         */
        const std::string SessionReader::GetSessionNameRank() const
        {
655
            std::string  dirname = m_sessionName + "_xml";
656
            fs::path     pdirname(dirname);
657

658
            std::string vFilename = "P" + boost::lexical_cast<std::string>(m_comm->GetRowComm()->GetRank());
659
            fs::path    pFilename(vFilename);
660

661 662 663 664
            fs::path fullpath = pdirname / pFilename;

            return PortablePath(fullpath);
        }
665

666 667 668
        /**
         *
         */
669 670 671 672 673
        CommSharedPtr& SessionReader::GetComm()
        {
            return m_comm;
        }

674 675 676 677
        bool SessionReader::GetSharedFilesystem()
        {
            return m_sharedFilesystem;
        }
678 679 680 681 682 683 684

        /**
         * This routine finalises any parallel communication.
         *
         * @note This routine should be called at the very end of a users
         * application.
         */
685 686 687 688 689
        void SessionReader::Finalise()
        {
            m_comm->Finalise();
        }

690

691 692 693 694 695 696
        /**
         *
         */
        bool SessionReader::DefinesParameter(const std::string& pName) const
        {
            std::string vName = boost::to_upper_copy(pName);
697
            return m_parameters.find(vName) != m_parameters.end();
698 699
        }

700 701 702 703 704 705 706 707 708

        /**
         * If the parameter is not defined, termination occurs. Therefore, the
         * parameters existence should be tested for using #DefinesParameter
         * before calling this function.
         *
         * @param   pName       The name of a floating-point parameter.
         * @returns The value of the floating-point parameter.
         */
709 710
        const NekDouble& SessionReader::GetParameter(
            const std::string& pName) const
711 712
        {
            std::string vName = boost::to_upper_copy(pName);
713
            auto paramIter = m_parameters.find(vName);
714

715 716
            ASSERTL0(paramIter != m_parameters.end(),
                     "Unable to find requested parameter: " + pName);
717

718
            return paramIter->second;
719 720
        }

721 722 723 724

        /**
         *
         */
725 726
        void SessionReader::LoadParameter(
            const std::string &pName, int &pVar) const
727 728
        {
            std::string vName = boost::to_upper_copy(pName);
729
            auto paramIter = m_parameters.find(vName);
730
            ASSERTL0(paramIter != m_parameters.end(), "Required parameter '" +
731
                     pName + "' not specified in session.");
732
            pVar = (int)round(paramIter->second);
733 734
        }

735 736 737 738

        /**
         *
         */
739 740
        void SessionReader::LoadParameter(
            const std::string &pName, int &pVar, const int &pDefault) const
741 742
        {
            std::string vName = boost::to_upper_copy(pName);
743
            auto paramIter = m_parameters.find(vName);
744
            if(paramIter != m_parameters.end())
745
            {
746
                pVar = (int)round(paramIter->second);
747 748 749 750 751 752 753
            }
            else
            {
                pVar  = pDefault;
            }
        }

754 755 756 757

        /**
         *
         */
758 759
        void SessionReader::LoadParameter(
            const std::string &pName, NekDouble& pVar) const
760 761
        {
            std::string vName = boost::to_upper_copy(pName);
762
            auto paramIter = m_parameters.find(vName);
763
            ASSERTL0(paramIter != m_parameters.end(), "Required parameter '" +
764 765
                     pName + "' not specified in session.");
            pVar = paramIter->second;
766 767
        }

768 769 770 771

        /**
         *
         */
772
        void SessionReader::LoadParameter(
773 774
            const std::string &pName,
                  NekDouble   &pVar,
775
            const NekDouble   &pDefault) const
776 777
        {
            std::string vName = boost::to_upper_copy(pName);
778
            auto paramIter = m_parameters.find(vName);
779
            if(paramIter != m_parameters.end())
780
            {
781
                pVar = paramIter->second;
782 783 784
            }
            else
            {
785
                pVar = pDefault;
786 787 788
            }
        }

789

Spencer Sherwin's avatar
Spencer Sherwin committed
790 791 792 793

        /**
         *
         */
794
        void SessionReader::SetParameter(const std::string &pName, int &pVar)
Spencer Sherwin's avatar
Spencer Sherwin committed
795 796 797 798 799 800 801 802 803
        {
            std::string vName = boost::to_upper_copy(pName);
            m_parameters[vName] = pVar;
        }


        /**
         *
         */
804
        void SessionReader::SetParameter(
805
            const std::string &pName, NekDouble& pVar)
Spencer Sherwin's avatar
Spencer Sherwin committed
806 807 808 809 810 811 812
        {
            std::string vName = boost::to_upper_copy(pName);
            m_parameters[vName] = pVar;
        }



813 814 815 816
        /**
         *
         */
        bool SessionReader::DefinesSolverInfo(const std::string &pName) const
817 818
        {
            std::string vName = boost::to_upper_copy(pName);
819
            auto infoIter = m_solverInfo.find(vName);
820
            return (infoIter != m_solverInfo.end());
821 822
        }

823 824 825 826

        /**
         *
         */
827 828
        const std::string& SessionReader::GetSolverInfo(
            const std::string &pProperty) const
829 830
        {
            std::string vProperty = boost::to_upper_copy(pProperty);
831
            auto iter = m_solverInfo.find(vProperty);
832

833 834
            ASSERTL1(iter != m_solverInfo.end(),
                     "Unable to find requested property: " + pProperty);
835

836
            return iter->second;
837 838
        }

Sergey Yakovlev's avatar
Sergey Yakovlev committed
839 840 841 842
        /**
         *
         */
        void SessionReader::SetSolverInfo(
843
            const std::string &pProperty, const std::string &pValue)
Sergey Yakovlev's avatar
Sergey Yakovlev committed
844 845
        {
            std::string vProperty = boost::to_upper_copy(pProperty);
846
            auto iter = m_solverInfo.find(vProperty);
Sergey Yakovlev's avatar
Sergey Yakovlev committed
847 848 849 850 851 852

            ASSERTL1(iter != m_solverInfo.end(),
                     "Unable to find requested property: " + pProperty);

            iter->second = pValue;
        }
853 854 855 856

        /**
         *
         */
857
        void SessionReader::LoadSolverInfo(
858 859
            const std::string &pName,
                  std::string &pVar,
860
            const std::string &pDefault) const
861 862
        {
            std::string vName = boost::to_upper_copy(pName);
863
            auto infoIter = m_solverInfo.find(vName);
864
            if(infoIter != m_solverInfo.end())
865
            {
866
                pVar = infoIter->second;
867 868 869
            }
            else
            {
870
                pVar = pDefault;
871 872 873
            }
        }

874 875 876 877

        /**
         *
         */
878 879 880 881 882
        void SessionReader::MatchSolverInfo(
            const std::string &pName,
            const std::string &pTrueVal,
                  bool        &pVar,
            const bool        &pDefault) const
883 884
        {
            std::string vName = boost::to_upper_copy(pName);
885
            auto infoIter = m_solverInfo.find(vName);
886
            if(infoIter != m_solverInfo.end())
887
            {
888
                pVar = boost::iequals(infoIter->second, pTrueVal);
889 890 891
            }
            else
            {
892
                pVar = pDefault;
893 894 895
            }
        }

896 897 898 899

        /**
         *
         */
900 901 902
        bool SessionReader::MatchSolverInfo(
            const std::string &pName,
            const std::string &pTrueVal) const
903 904 905 906
        {
            if (DefinesSolverInfo(pName))
            {
                std::string vName = boost::to_upper_copy(pName);
907
                auto iter = m_solverInfo.find(vName);
908
                if(iter != m_solverInfo.end())
909 910 911 912 913 914 915 916
                {
                    return true;
                }
            }
            return false;
        }


917 918 919
        /**
         *
         */
920
        bool SessionReader::DefinesGlobalSysSolnInfo(const std::string &pVariable,
921 922
                                                     const std::string &pProperty) const
        {
923
            auto iter = GetGloSysSolnList().find(pVariable);
924
            if(iter == GetGloSysSolnList().end())
925 926 927 928 929
            {
                return false;
            }

            std::string vProperty = boost::to_upper_copy(pProperty);
930

931
            auto iter1 = iter->second.find(vProperty);
932 933 934 935
            if(iter1 == iter->second.end())
            {
                return false;
            }
936

937 938 939
            return true;
        }

940

941 942 943 944 945
        /**
         *
         */
        const std::string &SessionReader::GetGlobalSysSolnInfo(const std::string &pVariable, const std::string &pProperty) const
        {
946 947 948
            auto iter = GetGloSysSolnList().find(pVariable);
            ASSERTL0(iter != GetGloSysSolnList().end(),
                     "Failed to find variable in GlobalSysSolnInfoList");
949 950

            std::string vProperty = boost::to_upper_copy(pProperty);
951
            auto iter1 = iter->second.find(vProperty);
952

953 954
            ASSERTL0(iter1 != iter->second.end(),
                     "Failed to find property: " + vProperty + " in GlobalSysSolnInfoList");
955

956 957
            return iter1->second;
        }
958

959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
        std::string SessionReader::GetGeometryType() const
        {
            TiXmlElement *xmlGeom = m_xmlDoc->FirstChildElement("NEKTAR")
                ->FirstChildElement("GEOMETRY");
            TiXmlAttribute *attr  = xmlGeom->FirstAttribute();
            while (attr)
            {
                std::string attrName(attr->Name());
                if (attrName == "HDF5FILE")
                {
                    // there is a file pointer, therefore is HDF5
                    return "HDF5";
                }
                // Get the next attribute.
                attr = attr->Next();
            }

            // Check the VERTEX block. If this is compressed, assume the file is
            // compressed, otherwise assume uncompressed.
            TiXmlElement *element = xmlGeom->FirstChildElement("VERTEX");
            string IsCompressed;
            element->QueryStringAttribute("COMPRESSED", &IsCompressed);

            if (IsCompressed.size() > 0)
            {
                return "XmlCompressed";
            }

            // no file pointer or compressed, just standard xml
            return "Xml";
        }

       /**
992 993 994
         *
         */
        bool SessionReader::DefinesGeometricInfo(const std::string &pName) const
995 996
        {
            std::string vName = boost::to_upper_copy(pName);
997
            return m_geometricInfo.find(vName) != m_geometricInfo.end();
998 999
        }

1000 1001 1002 1003

        /**
         *
         */
1004 1005 1006 1007
        void SessionReader::LoadGeometricInfo(
            const std::string &pName,
                  std::string &pVar,
            const std::string &pDefault) const
1008 1009
        {
            std::string vName = boost::to_upper_copy(pName);
1010
            auto iter = m_geometricInfo.find(vName);
1011
            if(iter != m_geometricInfo.end())
1012
            {
1013
                pVar = iter->second;
1014 1015 1016
            }
            else
            {
1017
                pVar = pDefault;
1018 1019 1020
            }
        }

1021 1022 1023 1024

        /**
         *
         */
1025 1026 1027 1028
        void SessionReader::LoadGeometricInfo(
            const std::string &pName,
                  bool        &pVar,
            const bool        &pDefault) const
1029 1030
        {
            std::string vName = boost::to_upper_copy(pName);
1031
            auto iter = m_geometricInfo.find(vName);
1032
            if(iter != m_geometricInfo.end())
1033
            {
1034
                if (iter->second == "TRUE")
1035 1036 1037 1038 1039 1040 1041 1042 1043 1044
                {
                    pVar = true;
                }
                else
                {
                    pVar = false;
                }
            }
            else
            {
1045
                pVar = pDefault;
1046 1047 1048
            }
        }

1049

1050 1051 1052
        /**
         *
         */
1053 1054 1055 1056
        void SessionReader::LoadGeometricInfo(
            const std::string &pName,
                  NekDouble   &pVar,
            const NekDouble   &pDefault) const
1057 1058
        {
            std::string vName = boost::to_upper_copy(pName);
1059
            auto iter = m_geometricInfo.find(vName);
1060
            if(iter != m_geometricInfo.end())
1061
            {
1062
                pVar = std::atoi(iter->second.c_str());
1063 1064 1065
            }
            else
            {
1066
                pVar = pDefault;
1067 1068 1069 1070
            }
        }


1071 1072 1073
        /**
         *
         */
1074 1075 1076 1077 1078
        void SessionReader::MatchGeometricInfo(
            const std::string &pName,
            const std::string &pTrueVal,
                  bool        &pVar,
            const bool        &pDefault) const
1079 1080
        {
            std::string vName = boost::to_upper_copy(pName);
1081
            auto iter = m_geometricInfo.find(vName);
1082
            if(iter != m_geometricInfo.end())
1083
            {
1084
                pVar = boost::iequals(iter->second, pTrueVal);
1085 1086 1087 1088 1089 1090 1091 1092
            }
            else
            {
                pVar  = pDefault;
            }
        }


1093 1094 1095
        /**
         *
         */
1096 1097
        const std::string& SessionReader::GetVariable(
            const unsigned int &idx) const
1098 1099 1100 1101 1102
        {
            ASSERTL0(idx < m_variables.size(), "Variable index out of range.");
            return m_variables[idx];
        }

1103

1104 1105 1106 1107

        /**
         *
         */
1108 1109
        void SessionReader::SetVariable(const unsigned int &idx,
                                        std::string newname)
1110 1111 1112 1113 1114 1115
        {
            ASSERTL0(idx < m_variables.size(), "Variable index out of range.");
            m_variables[idx] = newname;
        }


1116 1117 1118
        /**
         *
         */
1119 1120 1121 1122 1123
        std::vector<std::string> SessionReader::GetVariables() const
        {
            return m_variables;
        }

1124 1125 1126 1127 1128 1129 1130

        /**
         *
         */
        bool SessionReader::DefinesFunction(const std::string &pName) const
        {
            std::string vName = boost::to_upper_copy(pName);
1131
            return m_functions.find(vName) != m_functions.end();
1132 1133 1134 1135 1136 1137
        }


        /**
         *
         */
1138 1139
        bool SessionReader::DefinesFunction(
            const std::string &pName,
1140 1141
            const std::string &pVariable,
            const int pDomain) const
1142 1143 1144
        {
            std::string vName = boost::to_upper_copy(pName);

1145
            // Check function exists
1146 1147
            auto it1 = m_functions.find(vName);
            if (it1 != m_functions.end())
1148
            {
1149 1150
                pair<std::string, int> key(pVariable,pDomain);
                pair<std::string, int> defkey("*",pDomain);
1151
                bool varExists =
1152 1153
                    it1->second.find(key)    != it1->second.end() ||
                    it1->second.find(defkey) != it1->second.end();
1154
                return varExists;
1155 1156 1157 1158 1159 1160 1161 1162
            }
            return false;
        }


        /**
         *
         */
1163 1164
        EquationSharedPtr SessionReader::GetFunction(
            const std::string &pName,
1165 1166
            const std::string &pVariable,
            const int pDomain) const
1167 1168
        {
            std::string vName = boost::to_upper_copy(pName);
1169
            auto it1 = m_functions.find(vName);
1170

1171
            ASSERTL0(it1 != m_functions.end(),
1172 1173
                     std::string("No such function '") + pName
                     + std::string("' has been defined in the session file."));
1174 1175

            // Check for specific and wildcard definitions
1176 1177
            pair<std::string,int> key(pVariable,pDomain);
            pair<std::string,int> defkey("*",pDomain);
1178 1179 1180 1181 1182

            auto it2 = it1->second.find(key);
            auto it3 = it1->second.find(defkey);
            bool specific = it2 != it1->second.end();
            bool wildcard = it3 != it1->second.end();
1183 1184 1185

            // Check function is defined somewhere
            ASSERTL0(specific || wildcard,
1186
                     "No such variable " + pVariable
1187
                     + " in domain " + boost::lexical_cast<string>(pDomain)
1188 1189
                     + " defined for function " + pName
                     + " in session file.");
1190 1191 1192 1193 1194 1195 1196

            // If not specific, must be wildcard
            if (!specific)
            {
                it2 = it3;
            }

1197
            ASSERTL0((it2->second.m_type == eFunctionTypeExpression),