Read and write formatted representations of NAN and INF

This commit is contained in:
Joakim Hove
2019-10-21 09:18:03 +02:00
parent 33e72c2a77
commit abb70835fb
2 changed files with 50 additions and 8 deletions

View File

@@ -324,6 +324,16 @@ std::string EclOutput::make_real_string(float value) const
if (value == 0.0) {
return "0.00000000E+00";
} else {
if (std::isnan(value))
return "NAN";
if (std::isinf(value)) {
if (value > 0)
return "INF";
else
return "-INF";
}
std::string tmpstr(buffer);
int exp = value < 0.0 ? std::stoi(tmpstr.substr(11, 3)) : std::stoi(tmpstr.substr(10, 3));
@@ -350,9 +360,18 @@ std::string EclOutput::make_doub_string(double value) const
if (value == 0.0) {
return "0.00000000000000D+00";
} else {
std::string tmpstr(buffer);
if (std::isnan(value))
return "NAN";
int exp = value < 0.0 ? std::stoi(tmpstr.substr(17, 4)) : std::stoi(tmpstr.substr(16, 4));
if (std::isinf(value)) {
if (value > 0)
return "INF";
else
return "-INF";
}
std::string tmpstr(buffer);
int exp = value < 0.0 ? std::stoi(tmpstr.substr(17, 4)) : std::stoi(tmpstr.substr(16, 4));
if (value < 0.0) {
if (std::abs(exp) < 100) {
@@ -360,8 +379,7 @@ std::string EclOutput::make_doub_string(double value) const
} else {
tmpstr = "-0." + tmpstr.substr(1, 1) + tmpstr.substr(3, 13);
}
}
else {
} else {
if (std::abs(exp) < 100) {
tmpstr = "0." + tmpstr.substr(0, 1) + tmpstr.substr(2, 13) + "D";
} else {
@@ -369,9 +387,8 @@ std::string EclOutput::make_doub_string(double value) const
}
}
std::sprintf (buffer, "%+03i", exp+1);
std::sprintf(buffer, "%+03i", exp + 1);
tmpstr = tmpstr + buffer;
return tmpstr;
}
}

View File

@@ -17,16 +17,20 @@
+ */
#include "config.h"
#include <math.h>
#include <stdio.h>
#include <algorithm>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <math.h>
#include <stdio.h>
#include <limits>
#include <tuple>
#include <cmath>
#include <opm/io/eclipse/EclFile.hpp>
#include "WorkArea.cpp"
#include <opm/io/eclipse/EclOutput.hpp>
#define BOOST_TEST_MODULE Test EclIO
@@ -249,6 +253,27 @@ BOOST_AUTO_TEST_CASE(TestEcl_Write_formatted) {
};
}
BOOST_AUTO_TEST_CASE(TestEcl_Write_formatted_not_finite) {
WorkArea wa;
std::vector<float> float_vector{std::numeric_limits<float>::infinity() , std::numeric_limits<float>::quiet_NaN()};
std::vector<double> double_vector{std::numeric_limits<double>::infinity(), std::numeric_limits<double>::quiet_NaN()};
{
EclOutput testfile("TEST.FINIT", true);
testfile.write("FLOAT", float_vector);
testfile.write("DOUBLE", double_vector);
}
EclFile file1("TEST.FINIT");
file1.loadData();
auto f = file1.get<float>("FLOAT");
auto d = file1.get<double>("DOUBLE");
BOOST_CHECK(std::isinf(f[0]));
BOOST_CHECK(std::isinf(d[0]));
BOOST_CHECK(std::isnan(f[1]));
BOOST_CHECK(std::isnan(d[1]));
}
BOOST_AUTO_TEST_CASE(TestEcl_getList) {