Nektar++
MetricLInf.cpp
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: MetricLInf.cpp
4//
5// For more information, please see: http://www.nektar.info
6//
7// The MIT License
8//
9// Copyright (c) 2006 Division of Applied Mathematics, Brown University (USA),
10// Department of Aeronautics, Imperial College London (UK), and Scientific
11// Computing and Imaging Institute, University of Utah (USA).
12//
13// Permission is hereby granted, free of charge, to any person obtaining a
14// copy of this software and associated documentation files (the "Software"),
15// to deal in the Software without restriction, including without limitation
16// the rights to use, copy, modify, merge, publish, distribute, sublicense,
17// and/or sell copies of the Software, and to permit persons to whom the
18// Software is furnished to do so, subject to the following conditions:
19//
20// The above copyright notice and this permission notice shall be included
21// in all copies or substantial portions of the Software.
22//
23// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
24// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
25// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
26// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
27// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
28// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
29// DEALINGS IN THE SOFTWARE.
30//
31// Description: Implementation of the LInf metric.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#include <MetricLInf.h>
36
37namespace Nektar
38{
39std::string MetricLInf::type =
41
42// Guess default tolerance for generation routine.
43std::string MetricLInf::defaultTolerance = "1e-12";
44
45MetricLInf::MetricLInf(TiXmlElement *metric, bool generate)
46 : MetricRegex(metric, generate)
47{
48 // Set up the regular expression. This (optionally) matches a variable
49 // name if it exists: first field is variable name, second field is L2
50 // error.
51 m_regex = "^L inf\\w* error\\s*(?:\\(variable "
52 "(\\w+)\\))?\\s*:\\s*([+-]?\\d.+\\d|-?\\d|[+-]?nan|[+-]?inf).*";
53
54 // Find the L2 error to match against.
55 TiXmlElement *value = metric->FirstChildElement("value");
56 ASSERTL0(value || m_generate, "Missing value tag for LInf metric!");
57
58 while (value)
59 {
60 // Set up a match with two fields which correspond with the
61 // subexpression above. The first is the variable name, second is
62 // the L2 error.
63 ASSERTL0(value->Attribute("tolerance"),
64 "Missing tolerance in L2 metric");
65 ASSERTL0(!EmptyString(value->GetText()), "Missing value in L2 metric.");
66
68 if (value->Attribute("variable"))
69 {
70 var.m_value = value->Attribute("variable");
71 }
72
74 val.m_value = value->GetText();
75 val.m_useTolerance = true;
76 val.m_tolerance = atof(value->Attribute("tolerance"));
77
78 if (!m_generate)
79 {
80 std::vector<MetricRegexFieldValue> tmp(2);
81 tmp[0] = var;
82 tmp[1] = val;
83 m_matches.push_back(tmp);
84 }
85 else
86 {
87 m_varTolerance[var.m_value] = value->Attribute("tolerance");
88 }
89
90 value = value->NextSiblingElement("value");
91 }
92}
93
94void MetricLInf::v_Generate(std::istream &pStdout, std::istream &pStderr)
95{
96 // Run MetricRegex to generate matches.
97 MetricRegex::v_Generate(pStdout, pStderr);
98
99 // First remove all existing values.
100 m_metric->Clear();
101
102 ASSERTL0(m_matches.size() > 0, "Unable to find L infinity norm in output!");
103
104 // Now create new values.
105 for (int i = 0; i < m_matches.size(); ++i)
106 {
107 ASSERTL0(m_matches[i].size() == 2,
108 "Wrong number of matches for regular expression.");
109
110 bool tolSet = false;
111 std::string tol = MetricLInf::defaultTolerance;
112 TiXmlElement *value = new TiXmlElement("value");
113
114 // See if there is a tolerance found already for this variable
115 // (including empty variables).
116 std::map<std::string, std::string>::iterator it =
117 m_varTolerance.find(m_matches[i][0].m_value);
118
119 if (it != m_varTolerance.end())
120 {
121 tol = it->second;
122 tolSet = true;
123 }
124
125 if (m_matches[i][0].m_value.size() > 0)
126 {
127 value->SetAttribute("variable", m_matches[i][0].m_value);
128
129 if (m_matches[i][0].m_value == "p" && !tolSet)
130 {
131 // Set lower tolerance for pressure fields automatically if
132 // we haven't already got a tolerance from the existing
133 // file.
134 tol = "1e-8";
135 }
136 }
137
138 value->SetAttribute("tolerance", tol);
139 value->LinkEndChild(new TiXmlText(m_matches[i][1].m_value));
140 m_metric->LinkEndChild(value);
141 }
142}
143} // namespace Nektar
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:133
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:96
static std::string defaultTolerance
Definition: MetricLInf.h:51
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
Virtual function to generate the metric. Should be redefined in derived classes.
Definition: MetricLInf.cpp:94
static std::string type
Definition: MetricLInf.h:50
MetricLInf(TiXmlElement *metric, bool generate)
Definition: MetricLInf.cpp:45
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
Definition: MetricLInf.h:45
std::map< std::string, std::string > m_varTolerance
Definition: MetricLInf.h:56
void v_Generate(std::istream &pStdout, std::istream &pStderr) override
Test output against a regex expression and set of matches.
std::vector< std::vector< MetricRegexFieldValue > > m_matches
Stores the multiple matches defined in each <MATCH> tag.
Definition: MetricRegex.h:84
std::regex m_regex
Storage for the boost regex.
Definition: MetricRegex.h:82
bool EmptyString(const char *s)
Check to see whether the given string s is empty (or null).
Definition: Metric.h:50
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42
Data structure for a Regex value to match.
Definition: MetricRegex.h:49