Nektar++
Public Member Functions | Static Public Member Functions | Static Public Attributes | Protected Member Functions | Protected Attributes | List of all members
Nektar::MetricFile Class Reference

#include <MetricFile.h>

Inheritance diagram for Nektar::MetricFile:
[legend]

Public Member Functions

 ~MetricFile () override
 
- Public Member Functions inherited from Nektar::Metric
 Metric (TiXmlElement *metric, bool generate)
 Constructor. More...
 
virtual ~Metric ()=default
 
bool Test (std::istream &pStdout, std::istream &pStderr)
 Calls a metric's v_Test function (or v_Generate if m_generate). More...
 
void Generate (std::istream &pStdout, std::istream &pStderr)
 
std::string GetType ()
 Return metric type. More...
 
int GetID ()
 Return metric ID. More...
 
bool SupportsAverage () const
 Return whether this metric supports averaging results from multiple runs. More...
 

Static Public Member Functions

static MetricSharedPtr create (TiXmlElement *metric, bool generate)
 

Static Public Attributes

static std::string type
 

Protected Member Functions

 MetricFile (TiXmlElement *metric, bool generate)
 
std::string CalculateHash (std::string filename)
 
bool v_Test (std::istream &pStdout, std::istream &pStderr) override
 Virtual function to test the metric. Should be redefined in derived classes. More...
 
void v_Generate (std::istream &pStdout, std::istream &pStderr) override
 Virtual function to generate the metric. Should be redefined in derived classes. More...
 
virtual bool v_Test (std::istream &pStdout, std::istream &pStderr)=0
 Virtual function to test the metric. Should be redefined in derived classes. More...
 
virtual void v_Generate (std::istream &pStdout, std::istream &pSrderr)=0
 Virtual function to generate the metric. Should be redefined in derived classes. More...
 

Protected Attributes

std::map< std::string, std::string > m_filehash
 Stores filenames to perform hash on. More...
 
- Protected Attributes inherited from Nektar::Metric
int m_id
 Stores the ID of this metric. More...
 
std::string m_type
 Stores the type of this metric (uppercase). More...
 
bool m_generate
 Determines whether to generate this metric or not. More...
 
bool m_average = false
 Indicates whether a metric supports averaging results from multiple runs. More...
 
TiXmlElement * m_metric
 Pointer to XML structure containing metric definition. More...
 

Detailed Description

Definition at line 43 of file MetricFile.h.

Constructor & Destructor Documentation

◆ ~MetricFile()

Nektar::MetricFile::~MetricFile ( )
inlineoverride

Definition at line 46 of file MetricFile.h.

47 {
48 }

◆ MetricFile()

Nektar::MetricFile::MetricFile ( TiXmlElement *  metric,
bool  generate 
)
protected

Definition at line 52 of file MetricFile.cpp.

53 : Metric(metric, generate)
54{
55 TiXmlElement *file = metric->FirstChildElement("file");
56 ASSERTL0(file, "Missing file tag for file metric!");
57
58 while (file)
59 {
60 std::string filename, sha1hash;
61
62 if (file->Attribute("filename"))
63 {
64 filename = file->Attribute("filename");
65 }
66 else
67 {
68 ASSERTL0(false, "Missing filename for file tag!");
69 }
70
71 if (!m_generate)
72 {
73 TiXmlElement *sha1 = file->FirstChildElement("sha1");
74 ASSERTL0(sha1, "Missing SHA1 hash for file " + filename);
75 sha1hash = sha1->GetText();
76 ASSERTL0(sha1hash.size() == 40, "Incorrect length for SHA1 hash");
77 }
78
79 m_filehash[filename] = sha1hash;
80
81 file = file->NextSiblingElement("file");
82 }
83}
#define ASSERTL0(condition, msg)
Definition: ErrorUtil.hpp:208
std::map< std::string, std::string > m_filehash
Stores filenames to perform hash on.
Definition: MetricFile.h:66
Metric(TiXmlElement *metric, bool generate)
Constructor.
Definition: Metric.cpp:51
bool m_generate
Determines whether to generate this metric or not.
Definition: Metric.h:96
Definition: sha1.cpp:72

References ASSERTL0, m_filehash, and Nektar::Metric::m_generate.

Referenced by create().

Member Function Documentation

◆ CalculateHash()

std::string Nektar::MetricFile::CalculateHash ( std::string  filename)
protected

Definition at line 85 of file MetricFile.cpp.

86{
87 int fdot = pfilename.find_last_of('.');
88 string ending = pfilename.substr(fdot);
89 string filename;
90
91 if (ending == ".fld" || ending == ".chk" || ending == ".rst")
92 {
93 TiXmlDocument *xmlFldFile;
94 fs::path pathfilename(pfilename);
95
96 if (fs::is_directory(pathfilename))
97 {
98 std::vector<fs::path> dirfiles;
99
100 // make list of all files in directoryj
101 copy(fs::directory_iterator(pathfilename), fs::directory_iterator(),
102 back_inserter(dirfiles));
103
104 xmlFldFile = new TiXmlDocument;
105
106 // load all them into the
107 for (int i = 0; i < dirfiles.size(); ++i)
108 {
109 std::string infile = LibUtilities::PortablePath(dirfiles[i]);
110 std::ifstream file(infile.c_str());
111 ASSERTL0(file.good(), "Unable to open file: " + infile);
112 file >> (*xmlFldFile);
113 }
114 }
115 else
116 {
117 xmlFldFile = new TiXmlDocument(pfilename);
118 xmlFldFile->LoadFile(pfilename);
119 }
120
121 // strip out meta data before check
122 TiXmlElement *vNektar = xmlFldFile->FirstChildElement("NEKTAR");
123 while (vNektar)
124 {
125
126 TiXmlNode *vMetaData = vNektar->FirstChild("Metadata");
127
128 // delete MetaData section
129 if (vMetaData)
130 {
131 vNektar->RemoveChild(vMetaData);
132 }
133 vNektar = vNektar->NextSiblingElement("NEKTAR");
134 }
135
136 filename = pfilename + ".tmp";
137 xmlFldFile->SaveFile(filename);
138 }
139 else
140 {
141 filename = pfilename;
142 }
143
144 // Open file.
145 std::ifstream testFile(filename.c_str(), std::ios::binary);
146 ASSERTL0(testFile.is_open(), "Error opening file " + filename);
147
148 // Read in file contents.
149 std::vector<char> fileContents((std::istreambuf_iterator<char>(testFile)),
150 std::istreambuf_iterator<char>());
151
152 // Calculate SHA1 hash.
153 unsigned char hash[20];
154 char strhash[41];
155
156 sha1::calc((void *)&fileContents[0], fileContents.size(), hash);
157 sha1::toHexString(hash, strhash);
158
159 // Close file and return string containing SHA1 hash.
160 testFile.close();
161
162 return std::string(strhash);
163}
def copy(self)
Definition: pycml.py:2663
static std::string PortablePath(const fs::path &path)
create portable path on different platforms for std::filesystem path.
Definition: Filesystem.hpp:56
void toHexString(const unsigned char *hash, char *hexstring)
Calculate a string which represents the SHA1 hash as a hexadecimal number.
Definition: sha1.cpp:213
void calc(const void *src, const int bytelength, unsigned char *hash)
Calculate the SHA1 hash of some data set.
Definition: sha1.cpp:150

References ASSERTL0, sha1::calc(), CellMLToNektar.pycml::copy(), Nektar::LibUtilities::PortablePath(), and sha1::toHexString().

Referenced by v_Generate(), and v_Test().

◆ create()

static MetricSharedPtr Nektar::MetricFile::create ( TiXmlElement *  metric,
bool  generate 
)
inlinestatic

Definition at line 50 of file MetricFile.h.

51 {
52 return MetricSharedPtr(new MetricFile(metric, generate));
53 }
MetricFile(TiXmlElement *metric, bool generate)
Definition: MetricFile.cpp:52
std::shared_ptr< Metric > MetricSharedPtr
A shared pointer to an EquationSystem object.
Definition: Metric.h:124

References MetricFile().

◆ v_Generate()

void Nektar::MetricFile::v_Generate ( std::istream &  pStdout,
std::istream &  pSrderr 
)
overrideprotectedvirtual

Virtual function to generate the metric. Should be redefined in derived classes.

Parameters
pStdoutReference to test output stream.
pSrderrReference to test error stream.

Implements Nektar::Metric.

Definition at line 187 of file MetricFile.cpp.

188{
189 boost::ignore_unused(pStdout, pStderr);
190
191 std::map<std::string, std::string>::iterator it;
192
193 // Update SHA1 hashes.
194 for (it = m_filehash.begin(); it != m_filehash.end(); ++it)
195 {
196 std::string filehash = CalculateHash(it->first);
197 m_filehash[it->first] = filehash;
198 }
199
200 // Write new XML structure.
201 TiXmlElement *file = m_metric->FirstChildElement("file");
202 while (file)
203 {
204 std::string filename = file->Attribute("filename");
205 file->Clear();
206
207 TiXmlElement *sha1 = new TiXmlElement("sha1");
208
209 ASSERTL0(m_filehash.count(filename) != 0, "Couldn't find file " +
210 filename +
211 " in list of calculated"
212 "hashes");
213
214 sha1->LinkEndChild(new TiXmlText(m_filehash[filename]));
215 file->LinkEndChild(sha1);
216 file = file->NextSiblingElement("file");
217 }
218}
std::string CalculateHash(std::string filename)
Definition: MetricFile.cpp:85
TiXmlElement * m_metric
Pointer to XML structure containing metric definition.
Definition: Metric.h:101

References ASSERTL0, CalculateHash(), m_filehash, and Nektar::Metric::m_metric.

◆ v_Test()

bool Nektar::MetricFile::v_Test ( std::istream &  pStdout,
std::istream &  pStderr 
)
overrideprotectedvirtual

Virtual function to test the metric. Should be redefined in derived classes.

Parameters
pStdoutReference to test output stream.
pStderrReference to test error stream.
Returns
true if the test passes, false otherwise.

Implements Nektar::Metric.

Definition at line 165 of file MetricFile.cpp.

166{
167 boost::ignore_unused(pStdout, pStderr);
168
169 std::map<std::string, std::string>::iterator it;
170 bool success = true;
171
172 for (it = m_filehash.begin(); it != m_filehash.end(); ++it)
173 {
174 std::string filehash = CalculateHash(it->first);
175 if (!boost::iequals(filehash, it->second))
176 {
177 std::cerr << "Failed SHA1 hash test." << std::endl;
178 std::cerr << " Expected: " << it->second << std::endl;
179 std::cerr << " Result: " << filehash << std::endl;
180 success = false;
181 }
182 }
183
184 return success;
185}

References CalculateHash(), and m_filehash.

Member Data Documentation

◆ m_filehash

std::map<std::string, std::string> Nektar::MetricFile::m_filehash
protected

Stores filenames to perform hash on.

Definition at line 66 of file MetricFile.h.

Referenced by MetricFile(), v_Generate(), and v_Test().

◆ type

std::string Nektar::MetricFile::type
static
Initial value:
=
std::string RegisterCreatorFunction(std::string key, CreatorFunction func)
Definition: Metric.h:133
static MetricSharedPtr create(TiXmlElement *metric, bool generate)
Definition: MetricFile.h:50
MetricFactory & GetMetricFactory()
Definition: Metric.cpp:42

Definition at line 55 of file MetricFile.h.