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