Nektar++
Metric.h
Go to the documentation of this file.
1///////////////////////////////////////////////////////////////////////////////
2//
3// File: Metric.h
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: Definition of the metric base class.
32//
33///////////////////////////////////////////////////////////////////////////////
34
35#ifndef NEKTAR_TESTS_METRIC_H
36#define NEKTAR_TESTS_METRIC_H
37
38#include <boost/filesystem.hpp>
39#include <map>
40#include <memory>
41#include <string>
42#include <tinyxml.h>
43
44#include <TestException.hpp>
45
46namespace fs = boost::filesystem;
47
48std::string PortablePath(const boost::filesystem::path &path);
49
50namespace Nektar
51{
52/**
53 * @brief Check to see whether the given string @p s is empty (or null).
54 */
55inline bool EmptyString(const char *s)
56{
57 if (!s)
58 {
59 return true;
60 }
61 return std::string(s) == "";
62}
63
64/**
65 * @brief Base class for all metrics.
66 * Metric represents a test metric that can be used to evaluate the
67 * functionality or performance of a Nektar++ executable.
68 */
69class Metric
70{
71public:
72 Metric(TiXmlElement *metric, bool generate);
73
74 virtual ~Metric() = default;
75
76 bool Test(std::istream &pStdout, std::istream &pStderr);
77 void Generate(std::istream &pStdout, std::istream &pStderr);
78 /// Return metric type
79 std::string GetType()
80 {
81 return m_type;
82 }
83 /// Return metric ID
84 int GetID()
85 {
86 return m_id;
87 }
88 /// Return whether this metric supports averaging results from multiple
89 /// runs.
90 bool SupportsAverage() const
91 {
92 return m_average;
93 }
94
95protected:
96 /// Stores the ID of this metric.
97 int m_id;
98 /// Stores the type of this metric (uppercase).
99 std::string m_type;
100 /// Determines whether to generate this metric or not.
102 /// Indicates whether a metric supports averaging results from multiple
103 /// runs.
104 bool m_average = false;
105 /// Pointer to XML structure containing metric definition.
106 TiXmlElement *m_metric;
107
108 /**
109 * @brief Virtual function to test the metric. Should be redefined in
110 * derived classes.
111 *
112 * @param pStdout Reference to test output stream.
113 * @param pStderr Reference to test error stream.
114 * @return \p true if the test passes, \p false otherwise.
115 */
116 virtual bool v_Test(std::istream &pStdout, std::istream &pStderr) = 0;
117
118 /**
119 * @brief Virtual function to generate the metric. Should be redefined in
120 * derived classes.
121 *
122 * @param pStdout Reference to test output stream.
123 * @param pSrderr Reference to test error stream.
124 */
125 virtual void v_Generate(std::istream &pStdout, std::istream &pSrderr) = 0;
126};
127
128/// A shared pointer to an EquationSystem object
129typedef std::shared_ptr<Metric> MetricSharedPtr;
130
131/// Datatype of the NekFactory used to instantiate classes derived from the
132/// Advection class.
134{
135public:
136 typedef MetricSharedPtr (*CreatorFunction)(TiXmlElement *, bool);
137
138 std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
139 {
140 m_map[key] = func;
141 return key;
142 }
143
144 MetricSharedPtr CreateInstance(std::string key, TiXmlElement *elmt,
145 bool generate)
146 {
147 return m_map[key](elmt, generate);
148 }
149
150private:
151 std::map<std::string, CreatorFunction> m_map;
152};
153
155} // namespace Nektar
156
157#endif
std::string PortablePath(const boost::filesystem::path &path)
Definition: Tester.cpp.in:71
Datatype of the NekFactory used to instantiate classes derived from the Advection class.
Definition: Metric.h:134
std::map< std::string, CreatorFunction > m_map
Definition: Metric.h:151
MetricSharedPtr(* CreatorFunction)(TiXmlElement *, bool)
Definition: Metric.h:136
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:138
MetricSharedPtr CreateInstance(std::string key, TiXmlElement *elmt, bool generate)
Definition: Metric.h:144
Base class for all metrics. Metric represents a test metric that can be used to evaluate the function...
Definition: Metric.h:70
bool Test(std::istream &pStdout, std::istream &pStderr)
Calls a metric's v_Test function (or v_Generate if m_generate).
Definition: Metric.cpp:67
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:106
Metric(TiXmlElement *metric, bool generate)
Constructor.
Definition: Metric.cpp:51
virtual bool v_Test(std::istream &pStdout, std::istream &pStderr)=0
Virtual function to test the metric. Should be redefined in derived classes.
virtual void v_Generate(std::istream &pStdout, std::istream &pSrderr)=0
Virtual function to generate the metric. Should be redefined in derived classes.
int GetID()
Return metric ID.
Definition: Metric.h:84
virtual ~Metric()=default
std::string GetType()
Return metric type.
Definition: Metric.h:79
bool m_average
Indicates whether a metric supports averaging results from multiple runs.
Definition: Metric.h:104
int m_id
Stores the ID of this metric.
Definition: Metric.h:97
std::string m_type
Stores the type of this metric (uppercase).
Definition: Metric.h:99
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:101
bool SupportsAverage() const
Return whether this metric supports averaging results from multiple runs.
Definition: Metric.h:90
void Generate(std::istream &pStdout, std::istream &pStderr)
The above copyright notice and this permission notice shall be included.
Definition: CoupledSolver.h:2
std::shared_ptr< Metric > MetricSharedPtr
A shared pointer to an EquationSystem object.
Definition: Metric.h:129
bool EmptyString(const char *s)
Check to see whether the given string s is empty (or null).
Definition: Metric.h:55
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42