Merge pull request #1823 from akva2/fix-nnc-output

Make NNC output more eclipse compliant - with bisectability
This commit is contained in:
Andreas Lauser 2019-05-08 13:57:00 +02:00 committed by GitHub
commit 9136b01659
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 255 additions and 56 deletions

View File

@ -23,6 +23,7 @@
# originally generated with the command:
# find opm -name '*.c*' -printf '\t%p\n' | sort
list (APPEND MAIN_SOURCE_FILES
ebos/nncsorter.cpp
opm/autodiff/MPIUtilities.cpp
opm/autodiff/MissingFeatures.cpp
opm/core/props/rock/RockFromDeck.cpp
@ -58,6 +59,7 @@ list (APPEND TEST_SOURCE_FILES
tests/test_vfpproperties.cpp
tests/test_milu.cpp
tests/test_multmatrixtransposed.cpp
tests/test_nncsorter.cpp
tests/test_wellmodel.cpp
tests/test_deferredlogger.cpp
tests/test_timer.cpp

View File

@ -28,6 +28,8 @@
#ifndef EWOMS_ECL_TRANSMISSIBILITY_HH
#define EWOMS_ECL_TRANSMISSIBILITY_HH
#include <ebos/nncsorter.hpp>
#include <ewoms/common/propertysystem.hh>
#include <opm/parser/eclipse/EclipseState/EclipseState.hpp>
@ -617,51 +619,7 @@ private:
if (!nnc.hasNNC())
return make_tuple(processedNnc, unprocessedNnc);
auto nncData = nnc.nncdata();
auto editnncData = vanguard_.eclState().getInputEDITNNC().data();
auto nncLess =
[](const Opm::NNCdata& d1, const Opm::NNCdata& d2)
{
return
(d1.cell1 < d2.cell1)
|| (d1.cell1 == d2.cell1 && d1.cell2 < d2.cell2);
};
std::sort(nncData.begin(), nncData.end(), nncLess);
auto candidate = nncData.begin();
for (const auto& edit: editnncData) {
auto printNncWarning =
[](int c1, int c2)
{
std::ostringstream sstr;
sstr << "Cannot edit NNC from " << c1 << " to " << c2
<< " as it does not exist";
Opm::OpmLog::warning(sstr.str());
};
if (candidate == nncData.end()) {
// no more NNCs left
printNncWarning(edit.cell1, edit.cell2);
continue;
}
if (candidate->cell1 != edit.cell1 || candidate->cell2 != edit.cell2) {
candidate = std::lower_bound(candidate, nncData.end(), Opm::NNCdata(edit.cell1, edit.cell2, 0), nncLess);
if (candidate == nncData.end()) {
// no more NNCs left
printNncWarning(edit.cell1, edit.cell2);
continue;
}
}
auto firstCandidate = candidate;
while (candidate != nncData.end()
&& candidate->cell1 == edit.cell1
&& candidate->cell2 == edit.cell2)
{
candidate->trans *= edit.trans;
++candidate;
}
// start with first match in next iteration to catch case where next
// EDITNNC is for same pair.
candidate = firstCandidate;
}
auto nncData = sortNncAndApplyEditnnc(nnc.nncdata(), vanguard_.eclState().getInputEDITNNC().data());
for (const auto& nncEntry : nncData) {
auto c1 = nncEntry.cell1;

View File

@ -36,6 +36,8 @@
#include <ewoms/io/baseoutputwriter.hh>
#include <ewoms/parallel/tasklets.hh>
#include <ebos/nncsorter.hpp>
#include <opm/output/eclipse/EclipseIO.hpp>
#include <opm/output/eclipse/RestartValue.hpp>
#include <opm/parser/eclipse/Units/UnitSystem.hpp>
@ -45,6 +47,8 @@
#include <opm/material/common/Valgrind.hpp>
#include <opm/material/common/Exceptions.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <list>
#include <utility>
#include <string>
@ -372,9 +376,35 @@ private:
Opm::NNC exportNncStructure_() const
{
Opm::NNC nnc = eclState().getInputNNC();
int nx = eclState().getInputGrid().getNX();
int ny = eclState().getInputGrid().getNY();
std::size_t nx = eclState().getInputGrid().getNX();
std::size_t ny = eclState().getInputGrid().getNY();
auto nncData = sortNncAndApplyEditnnc(eclState().getInputNNC().nncdata(),
eclState().getInputEDITNNC().data());
const auto& unitSystem = simulator_.vanguard().deck().getActiveUnitSystem();
std::vector<Opm::NNCdata> outputNnc;
std::size_t index = 0;
for( const auto& entry : nncData ) {
// test whether NNC is not a neighboring connection
// cell2>=cell1 holds due to sortNncAndApplyEditnnc
assert( entry.cell2 >= entry.cell1 );
auto cellDiff = entry.cell2 - entry.cell1;
if (cellDiff != 1 && cellDiff != nx && cellDiff != nx*ny) {
auto tt = unitSystem.from_si(Opm::UnitSystem::measure::transmissibility, entry.trans);
// Eclipse ignores NNCs (with EDITNNC applied) that are small. Seems like the threshold is 1.0e-6
if ( tt >= 1.0e-6 )
outputNnc.emplace_back(entry.cell1, entry.cell2, entry.trans);
}
++index;
}
auto nncCompare = []( const Opm::NNCdata& nnc1, const Opm::NNCdata& nnc2){
return nnc1.cell1 < nnc2.cell1 ||
( nnc1.cell1 == nnc2.cell1 && nnc1.cell2 < nnc2.cell2);};
// Sort the nncData values from the deck as they need to be
// Checked when writing NNC transmissibilities from the simulation.
std::sort(nncData.begin(), nncData.end(), nncCompare);
const auto& globalGridView = globalGrid_.leafGridView();
#if DUNE_VERSION_NEWER(DUNE_GRID, 2,6)
@ -419,18 +449,40 @@ private:
// TODO (?): use the cartesian index mapper to make this code work
// with grids other than Dune::CpGrid. The problem is that we need
// the a mapper for the sequential grid, not for the distributed one.
int cc1 = globalGrid_.globalCell()[c1];
int cc2 = globalGrid_.globalCell()[c2];
std::size_t cc1 = globalGrid_.globalCell()[c1];
std::size_t cc2 = globalGrid_.globalCell()[c2];
if (std::abs(cc1 - cc2) != 1 &&
std::abs(cc1 - cc2) != nx &&
std::abs(cc1 - cc2) != nx*ny)
{
nnc.addNNC(cc1, cc2, globalTrans->transmissibility(c1, c2));
if ( cc2 < cc1 )
std::swap(cc1, cc2);
auto cellDiff = cc2 - cc1;
if (cellDiff != 1 &&
cellDiff != nx &&
cellDiff != nx*ny) {
// We need to check whether an NNC for this face was also specified
// via the NNC keyword in the deck (i.e. in the first origNncSize entries.
auto t = globalTrans->transmissibility(c1, c2);
auto candidate = std::lower_bound(nncData.begin(), nncData.end(), Opm::NNCdata(cc1, cc2, 0.0), nncCompare);
while ( candidate != nncData.end() && candidate->cell1 == cc1
&& candidate->cell2 == cc2) {
t -= candidate->trans;
++candidate;
}
// eclipse ignores NNCs with zero transmissibility (different threshold than for NNC
// with corresponding EDITNNC above). In addition we do set small transmissibilties
// to zero when setting up the simulator. These will be ignored here, too.
auto tt = unitSystem.from_si(Opm::UnitSystem::measure::transmissibility, std::abs(t));
if ( tt > 1e-12 )
outputNnc.push_back({cc1, cc2, t});
}
}
}
return nnc;
Opm::NNC ret;
for(const auto& nncItem: outputNnc)
ret.addNNC(nncItem.cell1, nncItem.cell2, nncItem.trans);
return ret;
}
struct EclWriteTasklet

95
ebos/nncsorter.cpp Normal file
View File

@ -0,0 +1,95 @@
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
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 2 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 <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#include <config.h>
#include <ebos/nncsorter.hpp>
#include <opm/common/OpmLog/OpmLog.hpp>
#include <sstream>
#include <algorithm>
#include <iostream>
namespace Ewoms
{
std::vector<Opm::NNCdata> sortNncAndApplyEditnnc(const std::vector<Opm::NNCdata>& nncDataIn, std::vector<Opm::NNCdata> editnncData,
bool log )
{
auto nncLess =
[](const Opm::NNCdata& d1, const Opm::NNCdata& d2) {
return
(d1.cell1 < d2.cell1)
|| (d1.cell1 == d2.cell1 && d1.cell2 < d2.cell2);
};
auto makeCell1LessCell2 =
[](const Opm::NNCdata& entry) {
if ( entry.cell2 < entry.cell1)
return Opm::NNCdata(entry.cell2, entry.cell1, entry.trans);
else
return entry;
};
// We need to make sure that for each entry cell1<=cell2 holds. Otherwise sorting
// will not make the search more accurate if the engineer chooses to define NNCs
// differently.
std::vector<Opm::NNCdata> nncData(nncDataIn);
std::transform(nncData.begin(), nncData.end(), nncData.begin(), makeCell1LessCell2);
std::transform(editnncData.begin(), editnncData.end(), editnncData.begin(), makeCell1LessCell2);
std::sort(nncData.begin(), nncData.end(), nncLess);
auto candidate = nncData.begin();
for (const auto& edit: editnncData) {
auto printNncWarning =
[](int c1, int c2) {
std::ostringstream sstr;
sstr << "Cannot edit NNC from " << c1 << " to " << c2
<< " as it does not exist";
Opm::OpmLog::warning(sstr.str());
};
if (candidate == nncData.end() && log) {
// no more NNCs left
printNncWarning(edit.cell1, edit.cell2);
continue;
}
if (candidate->cell1 != edit.cell1 || candidate->cell2 != edit.cell2) {
candidate = std::lower_bound(nncData.begin(), nncData.end(), Opm::NNCdata(edit.cell1, edit.cell2, 0), nncLess);
if (candidate == nncData.end() && log) {
// no more NNCs left
printNncWarning(edit.cell1, edit.cell2);
continue;
}
}
auto firstCandidate = candidate;
while (candidate != nncData.end()
&& candidate->cell1 == edit.cell1
&& candidate->cell2 == edit.cell2)
{
candidate->trans *= edit.trans;
++candidate;
}
// start with first match in next iteration to catch case where next
// EDITNNC is for same pair.
candidate = firstCandidate;
}
return nncData;
}
} // end namespace Ewoms

42
ebos/nncsorter.hpp Normal file
View File

@ -0,0 +1,42 @@
// -*- mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
// vi: set et ts=4 sw=4 sts=4:
/*
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 2 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 <http://www.gnu.org/licenses/>.
Consult the COPYING file in the top-level source directory of this
module for the precise wording of the license and the list of
copyright holders.
*/
#ifndef EWOMS_EBOS_NNCSORTER_HPP
#define EWOMS_EBOS_NNCSORTER_HPP
#include <opm/parser/eclipse/EclipseState/Grid/NNC.hpp>
#include <vector>
namespace Ewoms
{
/// \brief Scale NNC data wit informtion form EDITNNC and sort it.
/// \param nncData The NNC data as provided by the deck.
/// \param editnncData The EDITNNC data as provided by the deck.
/// \return A lexicographically sorted vector of the scaled NNC data.
/// For each entry entry.cell1<entry.cell2 will hold for convenience.
std::vector<Opm::NNCdata> sortNncAndApplyEditnnc(const std::vector<Opm::NNCdata>& nncData, std::vector<Opm::NNCdata> editnncData,
bool log = true);
}
#endif

50
tests/test_nncsorter.cpp Normal file
View File

@ -0,0 +1,50 @@
/*
Copyright 2019 Equinor.
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 <http://www.gnu.org/licenses/>.
*/
#define BOOST_TEST_MODULE NNCSortTest
#include <boost/test/unit_test.hpp>
#include <ebos/nncsorter.hpp>
#include <opm/parser/eclipse/EclipseState/Grid/NNC.hpp>
#include <cmath>
BOOST_AUTO_TEST_CASE(Test1) {
std::vector<Opm::NNCdata> nncDataIn =
{ {9, 8, 10.0 }, { 1, 2, 3.0 }, { 3, 4, 2.0 }, { 2, 1, 5.0 } };
std::vector<Opm::NNCdata> editnncData =
{ {20, 5, .001}, { 2, 1, .1}, {3, 4, .001}, {0, 0, 0.0}, {4, 3, 2.0} };
std::vector<Opm::NNCdata> nncDataOut1 =
{ { 1, 2, 0.3 }, { 1, 2, 0.5 }, { 3, 4, 0.004 }, { 8, 9, 10.0 } };
std::vector<Opm::NNCdata> nncDataOut2 =
{ { 1, 2, 0.5 }, { 1, 2, 0.3 }, { 3, 4, 0.4 }, { 8, 9, 10.0 } };
auto nncDataProcessed = Ewoms::sortNncAndApplyEditnnc(nncDataIn, editnncData);
BOOST_CHECK(nncDataProcessed.size() == nncDataOut1.size());
auto expectedNnc1 = nncDataOut1.begin();
auto expectedNnc2 = nncDataOut2.begin();
for(const auto& entry: nncDataProcessed) {
BOOST_CHECK( entry.cell1 == expectedNnc1->cell1);
BOOST_CHECK( entry.cell2 == expectedNnc1->cell2);
BOOST_CHECK( std::abs(entry.trans - expectedNnc1->trans) / std::abs(entry.trans) < 1e-5 ||
std::abs(entry.trans - expectedNnc2->trans) / std::abs(entry.trans) < 1e-5);
++expectedNnc1;
++expectedNnc2;
}
}