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

#include <MetricLInf.h>

namespace Nektar
{
    std::string MetricLInf::type = GetMetricFactory().
        RegisterCreatorFunction("LINF", MetricLInf::create);

43 44 45 46 47
    // Guess default tolerance for generation routine.
    std::string MetricLInf::defaultTolerance = "1e-12";

    MetricLInf::MetricLInf(TiXmlElement *metric, bool generate) : 
        MetricRegex(metric, generate)
Chris Cantwell's avatar
Chris Cantwell committed
48 49 50 51
    {
        // Set up the regular expression. This (optionally) matches a variable
        // name if it exists: first field is variable name, second field is L2
        // error.
52
        m_regex = "^L inf\\w* error\\s*(?:\\(variable "
53
              "(\\w+)\\))?\\s*:\\s*([+-]?\\d.+\\d|-?\\d|[+-]?nan|[+-]?inf).*";
54
        
Chris Cantwell's avatar
Chris Cantwell committed
55 56
        // Find the L2 error to match against.
        TiXmlElement *value = metric->FirstChildElement("value");
57 58
        ASSERTL0(value || m_generate, "Missing value tag for LInf metric!");
        
Chris Cantwell's avatar
Chris Cantwell committed
59 60 61 62
        while (value)
        {
            // Set up a match with two fields which correspond with the
            // subexpression above. The first is the variable name, second is
63 64
            // the L2 error.
            ASSERTL0(value->Attribute("tolerance"),
65
                     "Missing tolerance in L2 metric");
66
            ASSERTL0(value->GetText() || value->GetText() == "",
67
                     "Missing value in L2 metric.");
68 69

            MetricRegexFieldValue var;
70 71 72 73
            if (value->Attribute("variable"))
            {
                var.m_value = value->Attribute("variable");
            }
Chris Cantwell's avatar
Chris Cantwell committed
74

75 76 77 78 79
            MetricRegexFieldValue val;
            val.m_value = value->GetText();
            val.m_useTolerance = true;
            val.m_tolerance = atof(value->Attribute("tolerance"));

80 81 82 83 84 85 86 87 88 89 90
            if (!m_generate)
            {
                std::vector<MetricRegexFieldValue> tmp(2);
                tmp[0] = var;
                tmp[1] = val;
                m_matches.push_back(tmp);
            }
            else
            {
                m_varTolerance[var.m_value] = value->Attribute("tolerance");
            }
Chris Cantwell's avatar
Chris Cantwell committed
91 92 93 94

            value = value->NextSiblingElement("value");
        }
    }
95 96 97 98 99 100 101 102
    
    void MetricLInf::v_Generate(std::istream& pStdout, std::istream& pStderr)
    {
        // Run MetricRegex to generate matches.
        MetricRegex::v_Generate(pStdout, pStderr);

        // First remove all existing values.
        m_metric->Clear();
Chris Cantwell's avatar
Chris Cantwell committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145
        ASSERTL0(m_matches.size() > 0,
                 "Unable to find L infinity norm in output!");

        // Now create new values.
        for (int i = 0; i < m_matches.size(); ++i)
        {
            ASSERTL0(m_matches[i].size() == 2,
                     "Wrong number of matches for regular expression.");

            bool          tolSet = false;
            std::string   tol    = MetricLInf::defaultTolerance;
            TiXmlElement *value  = new TiXmlElement("value");
            
            // See if there is a tolerance found already for this variable
            // (including empty variables).
            std::map<std::string,std::string>::iterator it = 
                m_varTolerance.find(m_matches[i][0].m_value);

            if (it != m_varTolerance.end())
            {
                tol    = it->second;
                tolSet = true;
            }                
            
            if (m_matches[i][0].m_value.size() > 0)
            {
                value->SetAttribute("variable", m_matches[i][0].m_value);
                
                if (m_matches[i][0].m_value == "p" && !tolSet)
                {
                    // Set lower tolerance for pressure fields automatically if
                    // we haven't already got a tolerance from the existing
                    // file.
                    tol = "1e-8";
                }
            }
            
            value->SetAttribute("tolerance", tol);
            value->LinkEndChild(new TiXmlText(m_matches[i][1].m_value));
            m_metric->LinkEndChild(value);
        }
    }
Chris Cantwell's avatar
Chris Cantwell committed
146
}