-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Also make Timer.cpp actually compile (is this still needed? It's at least not used anywhere)
- Loading branch information
Showing
11 changed files
with
229 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,6 @@ | ||
[submodule "libs/h5xx"] | ||
path = libs/h5xx | ||
url = https://github.com/h5md/h5xx.git | ||
[submodule "libs/boost_matheval"] | ||
path = libs/boost_matheval | ||
url = https://github.com/hmenke/boost_matheval.git |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule boost_matheval
added at
72edf4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright (C) 2017 The ESPResSo project | ||
This file is part of ESPResSo. | ||
ESPResSo is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
ESPResSo is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
#define BOOST_TEST_MODULE Expression parser test | ||
#define BOOST_TEST_DYN_LINK | ||
#include <boost/test/unit_test.hpp> | ||
|
||
#include <cmath> | ||
#include <limits> | ||
#include <map> | ||
#include <stdexcept> | ||
#include <string> | ||
|
||
#include "utils/ExpressionParser.hpp" | ||
|
||
// We only test the integration into ESPResSo as Boost Matheval has | ||
// plenty of tests itself. | ||
|
||
BOOST_AUTO_TEST_CASE(integration1) { | ||
std::string expr = "cbrt(x/2 + sqrt(x**2/4 + y**3/24))"; | ||
double x = 2.0, y = -1.0; | ||
|
||
Utils::ExpressionParser parser; | ||
BOOST_CHECK_NO_THROW(parser.parse(expr)); | ||
|
||
std::map<std::string,double> symbol_table = { | ||
std::make_pair("x",x), | ||
std::make_pair("y",y), | ||
}; | ||
|
||
double result = 0; | ||
BOOST_CHECK_NO_THROW(result = parser.evaluate(symbol_table)); | ||
|
||
double expected = std::cbrt(x/2. + std::sqrt(std::pow(x,2.)/4. + std::pow(y,3.)/24.)); | ||
BOOST_CHECK_CLOSE_FRACTION(result, expected, std::numeric_limits<double>::epsilon()); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(integration2) { | ||
std::string expr = "("; | ||
|
||
Utils::ExpressionParser parser; | ||
|
||
// Parsing should fail | ||
BOOST_CHECK_THROW(parser.parse(expr), std::runtime_error); | ||
} | ||
|
||
BOOST_AUTO_TEST_CASE(integration3) { | ||
std::string expr = "x"; | ||
|
||
Utils::ExpressionParser parser; | ||
|
||
BOOST_CHECK_NO_THROW(parser.parse(expr)); | ||
|
||
// Evaluating should fail | ||
BOOST_CHECK_THROW(parser.evaluate({}), std::invalid_argument); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
/* | ||
Copyright (C) 2017 The ESPResSo project | ||
This file is part of ESPResSo. | ||
ESPResSo is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
ESPResSo is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#include <map> | ||
#include <string> | ||
|
||
#include "ExpressionParser.hpp" | ||
#include "utils/make_unique.hpp" | ||
|
||
#include "matheval.hpp" | ||
|
||
namespace Utils | ||
{ | ||
|
||
class ExpressionParser::impl | ||
{ | ||
matheval::Parser<double> parser; | ||
public: | ||
void parse(std::string const &expr) | ||
{ | ||
parser.parse(expr); | ||
} | ||
|
||
double evaluate(std::map<std::string,double> const &st) | ||
{ | ||
return parser.evaluate(st); | ||
} | ||
}; | ||
|
||
ExpressionParser::ExpressionParser() | ||
: pimpl{Utils::make_unique<ExpressionParser::impl>()} | ||
{} | ||
|
||
ExpressionParser::~ExpressionParser() {} | ||
|
||
void ExpressionParser::parse(std::string const &expr) | ||
{ | ||
pimpl->parse(expr); | ||
} | ||
|
||
double ExpressionParser::evaluate(std::map<std::string,double> const &st) | ||
{ | ||
return pimpl->evaluate(st); | ||
} | ||
|
||
} // namespace Utils |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/* | ||
Copyright (C) 2017 The ESPResSo project | ||
This file is part of ESPResSo. | ||
ESPResSo is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
ESPResSo is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU General Public License for more details. | ||
You should have received a copy of the GNU General Public License | ||
along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
#ifndef CORE_UTILS_EXPRESSION_PARSER_HPP | ||
#define CORE_UTILS_EXPRESSION_PARSER_HPP | ||
|
||
#include <map> | ||
#include <memory> | ||
#include <string> | ||
|
||
namespace Utils | ||
{ | ||
|
||
/** @brief Parse a mathematical expression | ||
* | ||
* This can parse and evaluate a mathematical expression for a given | ||
* symbol table using Boost Matheval. The templates of Boost.Spirit | ||
* (which is the basis of Boost Matheval) are very expensive to parse | ||
* and instantiate, which is why we hide it behind an opaque pointer. | ||
* | ||
* The drawback of this approach is that calls can no longer be | ||
* inlined and because the pointer crosses translation unit | ||
* boundaries, dereferencing it can also not be optimized out at | ||
* compile time. We have to rely entirely on link-time optimization | ||
* which might be not as good. | ||
* | ||
* The pointer to the implementation is a std::unique_ptr which makes | ||
* the class not copyable but only moveable. Copying shouldn't be | ||
* required but is easy to implement. | ||
*/ | ||
class ExpressionParser | ||
{ | ||
class impl; | ||
std::unique_ptr<impl> pimpl; | ||
public: | ||
/** @brief Constructor */ | ||
ExpressionParser(); | ||
|
||
/** @brief Destructor */ | ||
~ExpressionParser(); | ||
|
||
/** @brief Parse the mathematical expression into an abstract syntax tree | ||
* | ||
* @param[in] expr The expression given as a std::string | ||
*/ | ||
void parse(std::string const &expr); | ||
|
||
/** @brief Evaluate the abstract syntax tree for a given symbol table | ||
* | ||
* @param[in] st The symbol table | ||
*/ | ||
double evaluate(std::map<std::string,double> const &st); | ||
}; | ||
|
||
} // namespace Utils | ||
|
||
#endif // CORE_UTILS_EXPRESSION_PARSER_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters