Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace readItem in ProcessNumber to speed-up SolarShadingTest_ImportedShading #8819

Merged
merged 4 commits into from
Jun 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions src/EnergyPlus/UtilityRoutines.cc
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ extern "C" {

// C++ Headers
#include <cstdlib>
#include <exception>
#include <iostream>

// ObjexxFCL Headers
Expand All @@ -75,7 +74,6 @@ extern "C" {
#include <EnergyPlus/DataTimings.hh>
#include <EnergyPlus/DaylightingManager.hh>
#include <EnergyPlus/DisplayRoutines.hh>
#include <EnergyPlus/EPVector.hh>
#include <EnergyPlus/ExternalInterface.hh>
#include <EnergyPlus/FileSystem.hh>
#include <EnergyPlus/General.hh>
Expand All @@ -88,7 +86,6 @@ extern "C" {
#include <EnergyPlus/SQLiteProcedures.hh>
#include <EnergyPlus/SimulationManager.hh>
#include <EnergyPlus/SolarShading.hh>
#include <EnergyPlus/StringUtilities.hh>
#include <EnergyPlus/SystemReports.hh>
#include <EnergyPlus/UtilityRoutines.hh>

Expand Down Expand Up @@ -130,23 +127,25 @@ namespace UtilityRoutines {
std::string::size_type const StringLen(PString.length());
ErrorFlag = false;
if (StringLen == 0) return rProcessNumber;
bool parseFailed = false;
if (PString.find_first_not_of(ValidNumerics) == std::string::npos) {
// make FORTRAN floating point number (containing 'd' or 'D')
// standardized by replacing 'd' or 'D' with 'e'
std::replace_if(
std::begin(PString), std::end(PString), [](const char c) { return c == 'D' || c == 'd'; }, 'e');
// then parse as a normal floating point value
parseFailed = !readItem(PString, rProcessNumber);
ErrorFlag = false;
try {
rProcessNumber = std::stod(PString, nullptr);
} catch (std::invalid_argument &e) {
rProcessNumber = 0.0;
ErrorFlag = true;
} catch (std::out_of_range &e) {
rProcessNumber = 0.0;
ErrorFlag = true;
}
Comment on lines +136 to +144
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great!

} else {
rProcessNumber = 0.0;
ErrorFlag = true;
}
if (parseFailed) {
rProcessNumber = 0.0;
ErrorFlag = true;
}

return rProcessNumber;
}
Expand Down
54 changes: 54 additions & 0 deletions tst/EnergyPlus/unit/UtilityRoutines.unit.cc
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,57 @@ TEST_F(EnergyPlusFixture, UtilityRoutines_appendPerfLog2)
// clean up the file
fs::remove(state->dataStrGlobals->outputPerfLogFilePath);
}

TEST_F(EnergyPlusFixture, UtilityRoutines_ProcessNumber)
{
// acceptable strings
std::string goodString{"3.14159"};
double expectedVal{3.14159};
bool expectedError{false};
EXPECT_NEAR(UtilityRoutines::ProcessNumber(goodString, expectedError), expectedVal, 1E-5);
EXPECT_FALSE(expectedError);

goodString = "3.14159+E0";
EXPECT_NEAR(UtilityRoutines::ProcessNumber(goodString, expectedError), expectedVal, 1E-5);
EXPECT_FALSE(expectedError);

goodString = "3.14159+e0";
EXPECT_NEAR(UtilityRoutines::ProcessNumber(goodString, expectedError), expectedVal, 1E-5);
EXPECT_FALSE(expectedError);

goodString = "3.14159+D0";
EXPECT_NEAR(UtilityRoutines::ProcessNumber(goodString, expectedError), expectedVal, 1E-5);
EXPECT_FALSE(expectedError);

goodString = "3.14159+d0";
EXPECT_NEAR(UtilityRoutines::ProcessNumber(goodString, expectedError), expectedVal, 1E-5);
EXPECT_FALSE(expectedError);

// invalid strings
std::string badString{"É.14159"};
expectedVal = 0.0;
EXPECT_NEAR(UtilityRoutines::ProcessNumber(badString, expectedError), expectedVal, 1E-5);
EXPECT_TRUE(expectedError);

badString = "3.14159É0";
expectedVal = 0.0;
EXPECT_NEAR(UtilityRoutines::ProcessNumber(badString, expectedError), expectedVal, 1E-5);
EXPECT_TRUE(expectedError);

badString = "3.14159 0";
expectedVal = 0.0;
EXPECT_NEAR(UtilityRoutines::ProcessNumber(badString, expectedError), expectedVal, 1E-5);
EXPECT_TRUE(expectedError);

// invalid argument
badString = "E3.14159";
expectedVal = 0.0;
EXPECT_NEAR(UtilityRoutines::ProcessNumber(badString, expectedError), expectedVal, 1E-5);
EXPECT_TRUE(expectedError);

// out of range
badString = "1E5000";
mitchute marked this conversation as resolved.
Show resolved Hide resolved
expectedVal = 0.0;
EXPECT_NEAR(UtilityRoutines::ProcessNumber(badString, expectedError), expectedVal, 1E-5);
EXPECT_TRUE(expectedError);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this is comprehensive, and touched the cases I could think of.

}