NonlinearSolverEbos: put stabilizeNonlinearUpdate in compile unit

This commit is contained in:
Arne Morten Kvarving
2023-03-01 10:48:17 +01:00
parent 4aeb980f4f
commit cbaaca4f14
2 changed files with 73 additions and 43 deletions
@@ -20,7 +20,13 @@
#include <config.h>
#include <opm/simulators/flow/NonlinearSolverEbos.hpp>
#include <dune/common/fvector.hh>
#include <dune/istl/bvector.hh>
#include <opm/common/ErrorMacros.hpp>
#include <cmath>
#include <stdexcept>
namespace Opm {
namespace detail {
@@ -59,5 +65,56 @@ void detectOscillations(const std::vector<std::vector<double>>& residualHistory,
oscillate = (oscillatePhase > 1);
}
template <class BVector>
void stabilizeNonlinearUpdate(BVector& dx, BVector& dxOld,
const double omega,
NonlinearRelaxType relaxType)
{
// The dxOld is updated with dx.
// If omega is equal to 1., no relaxtion will be appiled.
BVector tempDxOld = dxOld;
dxOld = dx;
switch (relaxType) {
case NonlinearRelaxType::Dampen: {
if (omega == 1.) {
return;
}
for (auto& d : dx) {
d *= omega;
}
return;
}
case NonlinearRelaxType::SOR: {
if (omega == 1.) {
return;
}
auto i = dx.size();
for (i = 0; i < dx.size(); ++i) {
dx[i] *= omega;
tempDxOld[i] *= (1.-omega);
dx[i] += tempDxOld[i];
}
return;
}
default:
OPM_THROW(std::runtime_error, "Can only handle Dampen and SOR relaxation type.");
}
return;
}
template<int Size> using BV = Dune::BlockVector<Dune::FieldVector<double,Size>>;
#define INSTANCE(Size) \
template void stabilizeNonlinearUpdate<BV<Size>>(BV<Size>&, BV<Size>&, \
const double, NonlinearRelaxType);
INSTANCE(1)
INSTANCE(2)
INSTANCE(3)
INSTANCE(4)
INSTANCE(5)
INSTANCE(6)
} // namespace detail
} // namespace Opm