2012-08-24 09:08:26 -05:00
|
|
|
/*
|
|
|
|
Copyright 2012 SINTEF ICT, Applied Mathematics.
|
|
|
|
|
|
|
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2013-03-18 06:38:04 -05:00
|
|
|
#include <opm/core/tof/TofReorder.hpp>
|
2012-08-24 09:08:26 -05:00
|
|
|
#include <opm/core/grid.h>
|
|
|
|
#include <opm/core/utility/ErrorMacros.hpp>
|
|
|
|
#include <algorithm>
|
2012-09-25 07:00:17 -05:00
|
|
|
#include <numeric>
|
|
|
|
#include <cmath>
|
2012-08-24 09:08:26 -05:00
|
|
|
|
|
|
|
namespace Opm
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
/// Construct solver.
|
|
|
|
/// \param[in] grid A 2d or 3d grid.
|
2013-03-21 08:51:49 -05:00
|
|
|
/// \param[in] use_multidim_upwind If true, use multidimensional tof upwinding.
|
2013-03-14 10:18:39 -05:00
|
|
|
TofReorder::TofReorder(const UnstructuredGrid& grid,
|
|
|
|
const bool use_multidim_upwind)
|
2013-01-03 07:07:51 -06:00
|
|
|
: grid_(grid),
|
|
|
|
darcyflux_(0),
|
|
|
|
porevolume_(0),
|
|
|
|
source_(0),
|
|
|
|
tof_(0),
|
|
|
|
tracer_(0),
|
|
|
|
num_tracers_(0),
|
|
|
|
use_multidim_upwind_(use_multidim_upwind)
|
2012-08-24 09:08:26 -05:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-10-09 02:54:54 -05:00
|
|
|
/// Solve for time-of-flight.
|
2012-09-25 07:00:17 -05:00
|
|
|
/// \param[in] darcyflux Array of signed face fluxes.
|
|
|
|
/// \param[in] porevolume Array of pore volumes.
|
2012-10-09 02:54:54 -05:00
|
|
|
/// \param[in] source Source term. Sign convention is:
|
|
|
|
/// (+) inflow flux,
|
|
|
|
/// (-) outflow flux.
|
2012-09-25 07:00:17 -05:00
|
|
|
/// \param[out] tof Array of time-of-flight values.
|
2012-11-16 09:06:01 -06:00
|
|
|
void TofReorder::solveTof(const double* darcyflux,
|
2012-08-24 09:08:26 -05:00
|
|
|
const double* porevolume,
|
2012-09-25 07:00:17 -05:00
|
|
|
const double* source,
|
2012-08-24 09:08:26 -05:00
|
|
|
std::vector<double>& tof)
|
|
|
|
{
|
|
|
|
darcyflux_ = darcyflux;
|
|
|
|
porevolume_ = porevolume;
|
2012-09-25 07:00:17 -05:00
|
|
|
source_ = source;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Sanity check for sources.
|
|
|
|
const double cum_src = std::accumulate(source, source + grid_.number_of_cells, 0.0);
|
|
|
|
if (std::fabs(cum_src) > *std::max_element(source, source + grid_.number_of_cells)*1e-2) {
|
|
|
|
THROW("Sources do not sum to zero: " << cum_src);
|
|
|
|
}
|
|
|
|
#endif
|
2012-08-24 09:08:26 -05:00
|
|
|
tof.resize(grid_.number_of_cells);
|
|
|
|
std::fill(tof.begin(), tof.end(), 0.0);
|
|
|
|
tof_ = &tof[0];
|
2012-10-30 07:10:50 -05:00
|
|
|
if (use_multidim_upwind_) {
|
|
|
|
face_tof_.resize(grid_.number_of_faces);
|
|
|
|
std::fill(face_tof_.begin(), face_tof_.end(), 0.0);
|
|
|
|
}
|
2013-01-03 07:07:51 -06:00
|
|
|
num_tracers_ = 0;
|
|
|
|
reorderAndTransport(grid_, darcyflux);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/// Solve for time-of-flight and a number of tracers.
|
|
|
|
/// One tracer will be used for each inflow flux specified in
|
|
|
|
/// the source parameter.
|
|
|
|
/// \param[in] darcyflux Array of signed face fluxes.
|
|
|
|
/// \param[in] porevolume Array of pore volumes.
|
|
|
|
/// \param[in] source Source term. Sign convention is:
|
|
|
|
/// (+) inflow flux,
|
|
|
|
/// (-) outflow flux.
|
|
|
|
/// \param[out] tof Array of time-of-flight values (1 per cell).
|
|
|
|
/// \param[out] tracer Array of tracer values (N per cell, where N is
|
|
|
|
/// the number of cells c for which source[c] > 0.0).
|
2013-03-14 15:43:21 -05:00
|
|
|
void TofReorder::solveTofTracer(const double* darcyflux,
|
|
|
|
const double* porevolume,
|
|
|
|
const double* source,
|
|
|
|
std::vector<double>& tof,
|
|
|
|
std::vector<double>& tracer)
|
2013-01-03 07:07:51 -06:00
|
|
|
{
|
|
|
|
darcyflux_ = darcyflux;
|
|
|
|
porevolume_ = porevolume;
|
|
|
|
source_ = source;
|
|
|
|
#ifndef NDEBUG
|
|
|
|
// Sanity check for sources.
|
|
|
|
const double cum_src = std::accumulate(source, source + grid_.number_of_cells, 0.0);
|
|
|
|
if (std::fabs(cum_src) > *std::max_element(source, source + grid_.number_of_cells)*1e-2) {
|
|
|
|
THROW("Sources do not sum to zero: " << cum_src);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
tof.resize(grid_.number_of_cells);
|
|
|
|
std::fill(tof.begin(), tof.end(), 0.0);
|
|
|
|
tof_ = &tof[0];
|
|
|
|
// Find the tracer heads (injectors).
|
|
|
|
std::vector<int> tracerheads;
|
|
|
|
for (int c = 0; c < grid_.number_of_cells; ++c) {
|
|
|
|
if (source[c] > 0.0) {
|
|
|
|
tracerheads.push_back(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
num_tracers_ = tracerheads.size();
|
|
|
|
tracer.resize(grid_.number_of_cells*num_tracers_);
|
|
|
|
std::fill(tracer.begin(), tracer.end(), 0.0);
|
|
|
|
for (int tr = 0; tr < num_tracers_; ++tr) {
|
|
|
|
tracer[tracerheads[tr]*num_tracers_ + tr] = 1.0;
|
|
|
|
}
|
|
|
|
tracer_ = &tracer[0];
|
|
|
|
if (use_multidim_upwind_) {
|
|
|
|
face_tof_.resize(grid_.number_of_faces);
|
|
|
|
std::fill(face_tof_.begin(), face_tof_.end(), 0.0);
|
|
|
|
THROW("Multidimensional upwind not yet implemented for tracer.");
|
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
reorderAndTransport(grid_, darcyflux);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-16 09:06:01 -06:00
|
|
|
void TofReorder::solveSingleCell(const int cell)
|
2012-08-24 09:08:26 -05:00
|
|
|
{
|
2012-11-05 07:26:00 -06:00
|
|
|
if (use_multidim_upwind_) {
|
|
|
|
solveSingleCellMultidimUpwind(cell);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
// Compute flux terms.
|
2012-09-25 07:00:17 -05:00
|
|
|
// Sources have zero tof, and therefore do not contribute
|
|
|
|
// to upwind_term. Sinks on the other hand, must be added
|
|
|
|
// to the downwind_flux (note sign change resulting from
|
|
|
|
// different sign conventions: pos. source is injection,
|
|
|
|
// pos. flux is outflow).
|
2012-08-24 09:08:26 -05:00
|
|
|
double upwind_term = 0.0;
|
2012-09-25 07:00:17 -05:00
|
|
|
double downwind_flux = std::max(-source_[cell], 0.0);
|
2012-08-24 09:08:26 -05:00
|
|
|
for (int i = grid_.cell_facepos[cell]; i < grid_.cell_facepos[cell+1]; ++i) {
|
|
|
|
int f = grid_.cell_faces[i];
|
|
|
|
double flux;
|
|
|
|
int other;
|
|
|
|
// Compute cell flux
|
|
|
|
if (cell == grid_.face_cells[2*f]) {
|
|
|
|
flux = darcyflux_[f];
|
|
|
|
other = grid_.face_cells[2*f+1];
|
|
|
|
} else {
|
|
|
|
flux =-darcyflux_[f];
|
|
|
|
other = grid_.face_cells[2*f];
|
|
|
|
}
|
2012-12-05 03:39:00 -06:00
|
|
|
// Add flux to upwind_term or downwind_flux
|
|
|
|
if (flux < 0.0) {
|
|
|
|
// Using tof == 0 on inflow, so we only add a
|
|
|
|
// nonzero contribution if we are on an internal
|
|
|
|
// face.
|
|
|
|
if (other != -1) {
|
2012-11-05 07:26:00 -06:00
|
|
|
upwind_term += flux*tof_[other];
|
2013-01-03 07:07:51 -06:00
|
|
|
for (int tr = 0; tr < num_tracers_; ++tr) {
|
|
|
|
tracer_[num_tracers_*cell + tr] += flux*tracer_[num_tracers_*other + tr];
|
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
}
|
2012-12-05 03:39:00 -06:00
|
|
|
} else {
|
|
|
|
downwind_flux += flux;
|
2012-08-24 09:08:26 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute tof.
|
|
|
|
tof_[cell] = (porevolume_[cell] - upwind_term)/downwind_flux;
|
2013-01-03 07:07:51 -06:00
|
|
|
|
|
|
|
// Compute tracers (if any).
|
|
|
|
// Do not change tracer solution in source cells.
|
|
|
|
if (source_[cell] <= 0.0) {
|
|
|
|
for (int tr = 0; tr < num_tracers_; ++tr) {
|
|
|
|
tracer_[num_tracers_*cell + tr] *= -1.0/downwind_flux;
|
|
|
|
}
|
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2013-03-14 15:43:21 -05:00
|
|
|
void TofReorder::solveSingleCellMultidimUpwind(const int cell)
|
2012-11-05 07:26:00 -06:00
|
|
|
{
|
|
|
|
// Compute flux terms.
|
|
|
|
// Sources have zero tof, and therefore do not contribute
|
|
|
|
// to upwind_term. Sinks on the other hand, must be added
|
|
|
|
// to the downwind terms (note sign change resulting from
|
|
|
|
// different sign conventions: pos. source is injection,
|
|
|
|
// pos. flux is outflow).
|
|
|
|
double upwind_term = 0.0;
|
|
|
|
double downwind_term_cell_factor = std::max(-source_[cell], 0.0);
|
|
|
|
double downwind_term_face = 0.0;
|
|
|
|
for (int i = grid_.cell_facepos[cell]; i < grid_.cell_facepos[cell+1]; ++i) {
|
|
|
|
int f = grid_.cell_faces[i];
|
|
|
|
double flux;
|
|
|
|
// Compute cell flux
|
|
|
|
if (cell == grid_.face_cells[2*f]) {
|
|
|
|
flux = darcyflux_[f];
|
|
|
|
} else {
|
|
|
|
flux =-darcyflux_[f];
|
|
|
|
}
|
2012-12-05 03:39:00 -06:00
|
|
|
// Add flux to upwind_term or downwind_term_[face|cell_factor].
|
|
|
|
if (flux < 0.0) {
|
|
|
|
upwind_term += flux*face_tof_[f];
|
|
|
|
} else {
|
|
|
|
double fterm, cterm_factor;
|
|
|
|
multidimUpwindTerms(f, cell, fterm, cterm_factor);
|
|
|
|
downwind_term_face += fterm*flux;
|
|
|
|
downwind_term_cell_factor += cterm_factor*flux;
|
2012-11-05 07:26:00 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Compute tof for cell.
|
|
|
|
tof_[cell] = (porevolume_[cell] - upwind_term - downwind_term_face)/downwind_term_cell_factor; // }
|
|
|
|
|
|
|
|
// Compute tof for downwind faces.
|
|
|
|
for (int i = grid_.cell_facepos[cell]; i < grid_.cell_facepos[cell+1]; ++i) {
|
|
|
|
int f = grid_.cell_faces[i];
|
|
|
|
const double outflux_f = (grid_.face_cells[2*f] == cell) ? darcyflux_[f] : -darcyflux_[f];
|
|
|
|
if (outflux_f > 0.0) {
|
|
|
|
double fterm, cterm_factor;
|
|
|
|
multidimUpwindTerms(f, cell, fterm, cterm_factor);
|
|
|
|
face_tof_[f] = fterm + cterm_factor*tof_[cell];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2012-11-16 09:06:01 -06:00
|
|
|
void TofReorder::solveMultiCell(const int num_cells, const int* cells)
|
2012-08-24 09:08:26 -05:00
|
|
|
{
|
2012-08-27 04:26:51 -05:00
|
|
|
std::cout << "Pretending to solve multi-cell dependent equation with " << num_cells << " cells." << std::endl;
|
|
|
|
for (int i = 0; i < num_cells; ++i) {
|
|
|
|
solveSingleCell(cells[i]);
|
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-11-05 07:26:00 -06:00
|
|
|
|
|
|
|
|
|
|
|
// Assumes that face_tof_[f] is known for all upstream faces f of upwind_cell.
|
|
|
|
// Assumes that darcyflux_[face] is != 0.0.
|
|
|
|
// This function returns factors to compute the tof for 'face':
|
|
|
|
// tof(face) = face_term + cell_term_factor*tof(upwind_cell).
|
|
|
|
// It is not computed here, since these factors are needed to
|
|
|
|
// compute the tof(upwind_cell) itself.
|
2013-03-14 15:43:21 -05:00
|
|
|
void TofReorder::multidimUpwindTerms(const int face,
|
|
|
|
const int upwind_cell,
|
|
|
|
double& face_term,
|
|
|
|
double& cell_term_factor) const
|
2012-10-29 11:23:17 -05:00
|
|
|
{
|
2012-10-30 07:10:50 -05:00
|
|
|
// Implements multidim upwind according to
|
|
|
|
// "Multidimensional upstream weighting for multiphase transport on general grids"
|
|
|
|
// by Keilegavlen, Kozdon, Mallison.
|
|
|
|
// However, that article does not give a 3d extension other than noting that using
|
|
|
|
// multidimensional upwinding in the XY-plane and not in the Z-direction may be
|
|
|
|
// a good idea. We have here attempted some generalization, by looking at all
|
|
|
|
// face-neighbours across edges as upwind candidates, and giving them all uniform weight.
|
|
|
|
// This will over-weight the immediate upstream cell value in an extruded 2d grid with
|
|
|
|
// one layer (top and bottom no-flow faces will enter the computation) compared to the
|
|
|
|
// original 2d case. Improvements are welcome.
|
|
|
|
|
|
|
|
// Identify the adjacent faces of the upwind cell.
|
|
|
|
const int* face_nodes_beg = grid_.face_nodes + grid_.face_nodepos[face];
|
|
|
|
const int* face_nodes_end = grid_.face_nodes + grid_.face_nodepos[face + 1];
|
|
|
|
ASSERT(face_nodes_end - face_nodes_beg == 2 || grid_.dimensions != 2);
|
|
|
|
adj_faces_.clear();
|
|
|
|
for (int hf = grid_.cell_facepos[upwind_cell]; hf < grid_.cell_facepos[upwind_cell + 1]; ++hf) {
|
|
|
|
const int f = grid_.cell_faces[hf];
|
|
|
|
if (f != face) {
|
|
|
|
const int* f_nodes_beg = grid_.face_nodes + grid_.face_nodepos[f];
|
|
|
|
const int* f_nodes_end = grid_.face_nodes + grid_.face_nodepos[f + 1];
|
|
|
|
// Find out how many vertices they have in common.
|
|
|
|
// Using simple linear searches since sets are small.
|
|
|
|
int num_common = 0;
|
|
|
|
for (const int* f_iter = f_nodes_beg; f_iter < f_nodes_end; ++f_iter) {
|
|
|
|
num_common += std::count(face_nodes_beg, face_nodes_end, *f_iter);
|
|
|
|
}
|
|
|
|
if (num_common == grid_.dimensions - 1) {
|
|
|
|
// Neighbours over an edge (3d) or vertex (2d).
|
|
|
|
adj_faces_.push_back(f);
|
|
|
|
} else {
|
|
|
|
ASSERT(num_common == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Indentify adjacent faces with inflows, compute omega_star, omega,
|
|
|
|
// add up contributions.
|
|
|
|
const int num_adj = adj_faces_.size();
|
|
|
|
ASSERT(num_adj == face_nodes_end - face_nodes_beg);
|
|
|
|
const double flux_face = std::fabs(darcyflux_[face]);
|
2012-11-05 07:26:00 -06:00
|
|
|
face_term = 0.0;
|
|
|
|
cell_term_factor = 0.0;
|
2012-10-30 07:10:50 -05:00
|
|
|
for (int ii = 0; ii < num_adj; ++ii) {
|
|
|
|
const int f = adj_faces_[ii];
|
|
|
|
const double influx_f = (grid_.face_cells[2*f] == upwind_cell) ? -darcyflux_[f] : darcyflux_[f];
|
|
|
|
const double omega_star = influx_f/flux_face;
|
|
|
|
// SPU
|
|
|
|
// const double omega = 0.0;
|
|
|
|
// TMU
|
|
|
|
// const double omega = omega_star > 0.0 ? std::min(omega_star, 1.0) : 0.0;
|
|
|
|
// SMU
|
|
|
|
const double omega = omega_star > 0.0 ? omega_star/(1.0 + omega_star) : 0.0;
|
2012-11-05 07:26:00 -06:00
|
|
|
face_term += omega*face_tof_[f];
|
|
|
|
cell_term_factor += (1.0 - omega);
|
2012-10-30 07:10:50 -05:00
|
|
|
}
|
2012-11-05 07:26:00 -06:00
|
|
|
face_term /= double(num_adj);
|
|
|
|
cell_term_factor /= double(num_adj);
|
2012-10-29 11:23:17 -05:00
|
|
|
}
|
2012-08-24 09:08:26 -05:00
|
|
|
|
2012-11-05 07:26:00 -06:00
|
|
|
|
|
|
|
|
2012-08-24 09:08:26 -05:00
|
|
|
} // namespace Opm
|