Merge pull request #4027 from aritorto/lambda

Code modernization
This commit is contained in:
Markus Blatt 2022-08-26 12:19:47 +02:00 committed by GitHub
commit c58a30068e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 78 additions and 91 deletions

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2015 Dr. Blatt - HPC-Simulation-Software & Services Copyright 2015, 2022 Dr. Blatt - HPC-Simulation-Software & Services
Copyright 2015 Statoil AS Copyright 2015 Statoil AS
This file is part of the Open Porous Media project (OPM). This file is part of the Open Porous Media project (OPM).
@ -32,6 +32,21 @@
#include <array> #include <array>
template <typename T>
T Opm::detail::identityFunctor(const T& t){return t;};
template <typename T>
T Opm::detail::oneFunctor(const T&){return 1.0;};
template <typename T>
T Opm::detail::signFunctor(const T& t){if (t< 0){return -1;} else{return 1;}};
template <typename T>
T Opm::detail::isPositiveFunctor(const T& t){if (t<0){return 0;} else{return 1;}};
template <typename T>
T Opm::detail::absFunctor(const T& t){return std::abs(t);};
namespace Opm namespace Opm
{ {
@ -55,8 +70,8 @@ MILU_VARIANT convertString2Milu(const std::string& milu)
namespace detail namespace detail
{ {
template<class M, class F1, class F2> template<class M>
void milu0_decomposition(M& A, F1 absFunctor, F2 signFunctor, void milu0_decomposition(M& A, FieldFunct<M> absFunctor, FieldFunct<M> signFunctor,
std::vector<typename M::block_type>* diagonal) std::vector<typename M::block_type>* diagonal)
{ {
if( diagonal ) if( diagonal )
@ -217,16 +232,17 @@ void milun_decomposition(const M& A, int n, MILU_VARIANT milu, M& ILU,
detail::milu0_decomposition ( ILU); detail::milu0_decomposition ( ILU);
break; break;
case MILU_VARIANT::MILU_2: case MILU_VARIANT::MILU_2:
detail::milu0_decomposition ( ILU, detail::IdentityFunctor(), detail::milu0_decomposition ( ILU, identityFunctor<typename M::field_type>,
detail::SignFunctor() ); signFunctor<typename M::field_type>);
break; break;
case MILU_VARIANT::MILU_3: case MILU_VARIANT::MILU_3:
detail::milu0_decomposition ( ILU, detail::AbsFunctor(), detail::milu0_decomposition ( ILU, absFunctor<typename M::field_type>,
detail::SignFunctor() ); signFunctor<typename M::field_type>);
break; break;
case MILU_VARIANT::MILU_4: case MILU_VARIANT::MILU_4:
detail::milu0_decomposition ( ILU, detail::IdentityFunctor(), detail::milu0_decomposition ( ILU, identityFunctor<typename M::field_type> ,
detail::IsPositiveFunctor() ); isPositiveFunctor<typename M::field_type>);
break; break;
default: default:
#if DUNE_VERSION_LT(DUNE_GRID, 2, 8) #if DUNE_VERSION_LT(DUNE_GRID, 2, 8)
@ -238,26 +254,30 @@ void milun_decomposition(const M& A, int n, MILU_VARIANT milu, M& ILU,
} }
} }
#define INSTANCE(F1,F2,...) \
template void milu0_decomposition<__VA_ARGS__, F1, F2> \
(__VA_ARGS__&, F1, F2, \
std::vector<typename __VA_ARGS__::block_type>*);
#define INSTANCE_ILUN(...) \ template double Opm::detail::identityFunctor(const double&);
template double Opm::detail::oneFunctor(const double&);
template double Opm::detail::signFunctor(const double&);
template double Opm::detail::isPositiveFunctor(const double&);
template double Opm::detail::absFunctor(const double&);
#define INSTANCE(...) \
template void milu0_decomposition<__VA_ARGS__> \
(__VA_ARGS__&,std::function<double(const double&)>, std::function<double(const double&)>, \
std::vector<typename __VA_ARGS__::block_type>*);
#define INSTANCE_ILUN(...) \
template void milun_decomposition(const __VA_ARGS__&, int, MILU_VARIANT, \ template void milun_decomposition(const __VA_ARGS__&, int, MILU_VARIANT, \
__VA_ARGS__&,Reorderer&,Reorderer&); __VA_ARGS__&,Reorderer&,Reorderer&);
#define INSTANCE_FULL(...) \ #define INSTANCE_FULL(...) \
INSTANCE(AbsFunctor,SignFunctor,__VA_ARGS__) \ INSTANCE(__VA_ARGS__) \
INSTANCE(IdentityFunctor,IsPositiveFunctor,__VA_ARGS__) \
INSTANCE(IdentityFunctor,OneFunctor,__VA_ARGS__) \
INSTANCE(IdentityFunctor,SignFunctor,__VA_ARGS__) \
INSTANCE_ILUN(__VA_ARGS__) INSTANCE_ILUN(__VA_ARGS__)
#define INSTANCE_BLOCK(Dim) \ #define INSTANCE_BLOCK(Dim) \
INSTANCE_FULL(Dune::BCRSMatrix<MatrixBlock<double,Dim,Dim>>) INSTANCE_FULL(Dune::BCRSMatrix<MatrixBlock<double,Dim,Dim>>)
#define INSTANCE_FM(Dim) \ #define INSTANCE_FM(Dim) \
INSTANCE_FULL(Dune::BCRSMatrix<Dune::FieldMatrix<double,Dim,Dim>>) INSTANCE_FULL(Dune::BCRSMatrix<Dune::FieldMatrix<double,Dim,Dim>>)
INSTANCE_FM(1) INSTANCE_FM(1)

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2015 Dr. Blatt - HPC-Simulation-Software & Services Copyright 2015, 2022 Dr. Blatt - HPC-Simulation-Software & Services
Copyright 2015 Statoil AS Copyright 2015 Statoil AS
This file is part of the Open Porous Media project (OPM). This file is part of the Open Porous Media project (OPM).
@ -22,9 +22,12 @@
#include <cmath> #include <cmath>
#include <cstddef> #include <cstddef>
#include <functional>
#include <string> #include <string>
#include <vector> #include <vector>
namespace Opm namespace Opm
{ {
@ -43,9 +46,26 @@ enum class MILU_VARIANT{
MILU_VARIANT convertString2Milu(const std::string& milu); MILU_VARIANT convertString2Milu(const std::string& milu);
namespace detail namespace detail
{ {
template <typename T>
T identityFunctor(const T&);
template <typename T>
T oneFunctor(const T&);
template <typename T>
T signFunctor(const T&);
template <typename T>
T isPositiveFunctor(const T&);
template <typename T>
T absFunctor(const T&);
struct Reorderer struct Reorderer
{ {
virtual std::size_t operator[](std::size_t i) const = 0; virtual std::size_t operator[](std::size_t i) const = 0;
@ -72,74 +92,21 @@ struct RealReorderer : public Reorderer
const std::vector<std::size_t>* ordering_; const std::vector<std::size_t>* ordering_;
}; };
struct IdentityFunctor
{
template<class T>
T operator()(const T& t)
{
return t;
}
};
struct OneFunctor template<class M>
{ using FieldFunct = std::function<typename M::field_type(const typename M::field_type&)>;
template<class T>
T operator()(const T&)
{
return 1.0;
}
};
struct SignFunctor
{
template<class T>
double operator()(const T& t)
{
if (t < 0.0)
{
return -1;
}
else
{
return 1.0;
}
}
};
struct IsPositiveFunctor template <typename M>
{ void milu0_decomposition(M& A, FieldFunct<M> absFunctor = signFunctor<typename M::field_type>,
template<class T> FieldFunct<M> signFunctor = oneFunctor<typename M::field_type>,
double operator()(const T& t)
{
if (t < 0.0)
{
return 0;
}
else
{
return 1;
}
}
};
struct AbsFunctor
{
template<class T>
T operator()(const T& t)
{
return std::abs(t);
}
};
template<class M, class F1=IdentityFunctor, class F2=OneFunctor >
void milu0_decomposition(M& A, F1 absFunctor = F1(), F2 signFunctor = F2(),
std::vector<typename M::block_type>* diagonal = nullptr); std::vector<typename M::block_type>* diagonal = nullptr);
template<class M> template<class M>
void milu0_decomposition(M& A, void milu0_decomposition(M& A, std::vector<typename M::block_type>* diagonal)
std::vector<typename M::block_type>* diagonal)
{ {
milu0_decomposition(A, detail::IdentityFunctor(), detail::OneFunctor(), milu0_decomposition(A, identityFunctor<typename M::field_type>, oneFunctor<typename M::field_type>, diagonal);
diagonal); };
}
template<class M> template<class M>
void milun_decomposition(const M& A, int n, MILU_VARIANT milu, M& ILU, void milun_decomposition(const M& A, int n, MILU_VARIANT milu, M& ILU,

View File

@ -1,5 +1,5 @@
/* /*
Copyright 2015 Dr. Blatt - HPC-Simulation-Software & Services Copyright 2015, 2022 Dr. Blatt - HPC-Simulation-Software & Services
Copyright 2015 Statoil AS Copyright 2015 Statoil AS
This file is part of the Open Porous Media project (OPM). This file is part of the Open Porous Media project (OPM).
@ -442,16 +442,16 @@ update()
detail::milu0_decomposition ( *ILU); detail::milu0_decomposition ( *ILU);
break; break;
case MILU_VARIANT::MILU_2: case MILU_VARIANT::MILU_2:
detail::milu0_decomposition ( *ILU, detail::IdentityFunctor(), detail::milu0_decomposition ( *ILU, detail::identityFunctor<typename Matrix::field_type>,
detail::SignFunctor() ); detail::signFunctor<typename Matrix::field_type> );
break; break;
case MILU_VARIANT::MILU_3: case MILU_VARIANT::MILU_3:
detail::milu0_decomposition ( *ILU, detail::AbsFunctor(), detail::milu0_decomposition ( *ILU, detail::absFunctor<typename Matrix::field_type>,
detail::SignFunctor() ); detail::signFunctor<typename Matrix::field_type> );
break; break;
case MILU_VARIANT::MILU_4: case MILU_VARIANT::MILU_4:
detail::milu0_decomposition ( *ILU, detail::IdentityFunctor(), detail::milu0_decomposition ( *ILU, detail::identityFunctor<typename Matrix::field_type>,
detail::IsPositiveFunctor() ); detail::isPositiveFunctor<typename Matrix::field_type> );
break; break;
default: default:
if (interiorSize_ == A_->N()) if (interiorSize_ == A_->N())