/* + Copyright 2019 Equinor ASA. + + This file is part of the Open Porous Media project (OPM). + + OPM 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. + + OPM 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 OPM. If not, see . + */ #include "config.h" #include #include #include #include #include #include #include #include #include #define BOOST_TEST_MODULE Test EclIO #include using namespace Opm::EclIO; template bool range_equal(InputIterator1 first1, InputIterator1 last1, InputIterator2 first2, InputIterator2 last2) { while(first1 != last1 && first2 != last2) { if(*first1 != *first2) return false; ++first1; ++first2; } return (first1 == last1) && (first2 == last2); } bool compare_files(const std::string& filename1, const std::string& filename2) { std::ifstream file1(filename1); std::ifstream file2(filename2); std::istreambuf_iterator begin1(file1); std::istreambuf_iterator begin2(file2); std::istreambuf_iterator end; return range_equal(begin1, end, begin2, end); } template bool operator==(const std::vector & t1, const std::vector & t2) { return std::equal(t1.begin(), t1.end(), t2.begin(), t2.end()); } BOOST_AUTO_TEST_CASE(TestEclFile_BINARY) { std::string testFile="ECLFILE.INIT"; // check that exception is thrown when input file doesn't exist BOOST_CHECK_THROW(EclFile file1("DUMMY.DAT") , std::invalid_argument ); EclFile file1(testFile); // check that exeption is thrown when member function get is used with wrong type BOOST_CHECK_THROW(std::vector vect1=file1.get(2) , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get("PORV") , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get(0) , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get("ICON") , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get(0) , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get("KEYWORDS") , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get(0) , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get("XCON") , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get(0) , std::runtime_error ); BOOST_CHECK_THROW(std::vector vect1=file1.get("XCON") , std::runtime_error ); // check member function hasKey BOOST_CHECK_EQUAL(file1.hasKey("PORV"), true); BOOST_CHECK_EQUAL(file1.hasKey("XPORV"), false); // test member functon get, use size of vector to confirm that vectror is ok std::vector vect1a=file1.get(0); std::vector vect1b=file1.get("ICON"); BOOST_CHECK_EQUAL(vect1a.size(), 1875); BOOST_CHECK_EQUAL(vect1b.size(), 1875); std::vector vect2a=file1.get(1); std::vector vect2b=file1.get("LOGIHEAD"); BOOST_CHECK_EQUAL(vect2a.size(), 121); BOOST_CHECK_EQUAL(vect2b.size(), 121); std::vector vect3a=file1.get(2); std::vector vect3b=file1.get("PORV"); BOOST_CHECK_EQUAL(vect3a.size(), 3146); BOOST_CHECK_EQUAL(vect3b.size(), 3146); std::vector vect4a=file1.get(3); std::vector vect4b=file1.get("XCON"); BOOST_CHECK_EQUAL(vect4a.size(), 1740); BOOST_CHECK_EQUAL(vect4b.size(), 1740); std::vector vect5a=file1.get(4); std::vector vect5b=file1.get("KEYWORDS"); BOOST_CHECK_EQUAL(vect5a.size(), 312); BOOST_CHECK_EQUAL(vect5b.size(), 312); } BOOST_AUTO_TEST_CASE(TestEclFile_FORMATTED) { std::string testFile1="ECLFILE.INIT"; std::string testFile2="ECLFILE.FINIT"; // loading data both from binary and formatted file. Check that // date vectors are identical EclFile file1(testFile1); file1.loadData(); EclFile file2(testFile2); file2.loadData(); std::vector vect1a=file1.get("ICON"); std::vector vect1b=file2.get("ICON"); BOOST_CHECK_EQUAL(vect1a.size(), vect1b.size()); BOOST_CHECK_EQUAL(vect1a==vect1b,true); std::vector vect2a=file1.get("PORV"); std::vector vect2b=file2.get("PORV"); BOOST_CHECK_EQUAL(vect2a.size(), vect2b.size()); BOOST_CHECK_EQUAL(vect2a==vect2b,true); std::vector vect3a=file1.get("XCON"); std::vector vect3b=file2.get("XCON"); BOOST_CHECK_EQUAL(vect3a.size(), vect3b.size()); BOOST_CHECK_EQUAL(vect3a==vect3b,true); std::vector vect4a=file1.get("LOGIHEAD"); std::vector vect4b=file2.get("LOGIHEAD"); BOOST_CHECK_EQUAL(vect4a.size(), vect4b.size()); BOOST_CHECK_EQUAL(vect4a==vect4b,true); std::vector vect5a=file1.get("KEYWORDS"); std::vector vect5b=file2.get("KEYWORDS"); BOOST_CHECK_EQUAL(vect5a.size(), vect5b.size()); BOOST_CHECK_EQUAL(vect5a==vect5b,true); } BOOST_AUTO_TEST_CASE(TestEcl_Write_binary) { std::string inputFile="ECLFILE.INIT"; std::string testFile="TEST.DAT"; // loading vectors from binary file and write these back to a binary file1 // compare input and output file and delete file. EclFile file1(inputFile); file1.loadData(); std::vector icon=file1.get("ICON"); std::vector porv=file1.get("PORV"); std::vector xcon=file1.get("XCON"); std::vector logihead=file1.get("LOGIHEAD"); std::vector keywords=file1.get("KEYWORDS"); // writing vectors to test file (TEST.DAT) using class EclOutput { EclOutput eclTest(testFile, false); eclTest.write("ICON",icon); eclTest.write("LOGIHEAD",logihead); eclTest.write("PORV",porv); eclTest.write("XCON",xcon); eclTest.write("KEYWORDS",keywords); eclTest.write("ENDSOL",std::vector()); } BOOST_CHECK_EQUAL(compare_files(inputFile, testFile), true); if (remove(testFile.c_str())==-1) { std::cout << " > Warning! temporary file was not deleted" << std::endl; }; } BOOST_AUTO_TEST_CASE(TestEcl_Write_formatted) { std::string inputFile="ECLFILE.FINIT"; std::string testFile="TEST.FDAT"; // loading vectors from formatted input file and write data back to a formatted file1 // compare input and output file, and delete file. EclFile file1(inputFile); file1.loadData(); std::vector icon = file1.get("ICON"); std::vector porv = file1.get("PORV"); std::vector xcon = file1.get("XCON"); std::vector logihead = file1.get("LOGIHEAD"); std::vector keywords = file1.get("KEYWORDS"); // writing vectors to test file (TEST.FDAT) using class EclOutput EclOutput eclTest(testFile, true); eclTest.write("ICON",icon); eclTest.write("LOGIHEAD",logihead); eclTest.write("PORV",porv); eclTest.write("XCON",xcon); eclTest.write("KEYWORDS",keywords); eclTest.write("ENDSOL",std::vector()); BOOST_CHECK_EQUAL(compare_files(inputFile, testFile), true); if (remove(testFile.c_str())==-1) { std::cout << " > Warning! temporary file was not deleted" << std::endl; }; } BOOST_AUTO_TEST_CASE(TestEcl_getList) { std::string inputFile="ECLFILE.INIT"; std::string testFile="TEST.DAT"; // use EclFile to read/open a binary file // Use API for class EclFile together with class EclOutput to write an // identical eclfile // EclFile::getList(), EclFile::get(int) EclFile file1(inputFile); file1.loadData(); { EclOutput eclTest(testFile, false); auto arrayList = file1.getList(); int n=0; for (auto array : arrayList) { std::string name = std::get<0>(array); eclArrType arrType = std::get<1>(array); if (arrType == INTE) { std::vector vect = file1.get(n); eclTest.write(name, vect); } else if (arrType == REAL) { std::vector vect = file1.get(n); eclTest.write(name, vect); } else if (arrType == DOUB) { std::vector vect = file1.get(n); eclTest.write(name, vect); } else if (arrType == LOGI) { std::vector vect = file1.get(n); eclTest.write(name, vect); } else if (arrType == CHAR) { std::vector vect = file1.get(n); eclTest.write(name, vect); } else if (arrType == MESS) { eclTest.write(name, std::vector()); } else { std::cout << "unknown type " << std::endl; exit(1); } n++; } } BOOST_CHECK_EQUAL(compare_files(inputFile, testFile), true); if (remove(testFile.c_str())==-1) { std::cout << " > Warning! temporary file was not deleted" << std::endl; }; } BOOST_AUTO_TEST_CASE(TestEcl_Write_CHAR) { std::string testFile="TEST.FDAT"; std::vector refStrList = {"This", "is", "a test.", "", "charact", "er >'<", "can be", "part of", "a string"}; { EclOutput eclTest(testFile, true); eclTest.write("TEST",refStrList); } { EclFile file1(testFile); std::vector strList=file1.get("TEST"); for (size_t n = 0; n < refStrList.size(); n++) { BOOST_CHECK(refStrList[n] == strList[n]); } } if (remove(testFile.c_str())==-1) { std::cout << " > Warning! temporary file was not deleted" << std::endl; }; }