From cdc8dd4202d3692d2cf3fbc68d279f10d4ae9180 Mon Sep 17 00:00:00 2001 From: Pietro Incardona <incardon@mpi-cbg.de> Date: Fri, 23 Oct 2015 18:07:12 +0200 Subject: [PATCH] Continuing coding FD mul part --- src/FiniteDifference/Derivative.hpp | 2 +- src/FiniteDifference/FDScheme.hpp | 2 +- src/FiniteDifference/eq.hpp | 43 ------------------------ src/FiniteDifference/eq_unit_test.hpp | 9 +++-- src/FiniteDifference/sum.hpp | 48 +++++++++++++++++++++++++-- src/main.cpp | 1 + 6 files changed, 56 insertions(+), 49 deletions(-) diff --git a/src/FiniteDifference/Derivative.hpp b/src/FiniteDifference/Derivative.hpp index d0df1be4..240d6507 100644 --- a/src/FiniteDifference/Derivative.hpp +++ b/src/FiniteDifference/Derivative.hpp @@ -31,7 +31,7 @@ class D * \tparam ord * */ - inline static std::unordered_map<long int,typename Sys_eqs::stype> value(grid_key_dx<Sys_eqs::dims> & pos, const grid_sm<Sys_eqs::dims,void> & gs, std::unordered_map<long int,typename Sys_eqs::stype > & cols, typename Sys_eqs::stype coeff) + inline static void value(const grid_key_dx<Sys_eqs::dims> & pos, const grid_sm<Sys_eqs::dims,void> & gs, std::unordered_map<long int,typename Sys_eqs::stype > & cols, typename Sys_eqs::stype coeff) { std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined"; } diff --git a/src/FiniteDifference/FDScheme.hpp b/src/FiniteDifference/FDScheme.hpp index ea0cc0e7..ea5154bd 100644 --- a/src/FiniteDifference/FDScheme.hpp +++ b/src/FiniteDifference/FDScheme.hpp @@ -46,7 +46,7 @@ public: // convert into global coordinate the position auto keyg = it.getGKey(key); - // Convert local key into global key + // Calculate the non-zero colums T::value(keyg,gs,cols,1.0); // create the triplet diff --git a/src/FiniteDifference/eq.hpp b/src/FiniteDifference/eq.hpp index ea83473e..321d0c39 100644 --- a/src/FiniteDifference/eq.hpp +++ b/src/FiniteDifference/eq.hpp @@ -103,49 +103,6 @@ class minus } }; -/*! \brief It model an expression expr1 * expr2 - * - * \warning expr1 MUST be a constant expression - * - * \tparam expr1 - * \tparam expr2 - * - */ -template<typename expr1,typename expr2, typename Sys_eqs> -class mul -{ - /*! \brief Create the row of the Matrix - * - * \tparam ord - * - */ - template<unsigned int ord=EQS_FIELD> static void value(const grid_key_dx<Sys_eqs::dims> & pos) - { - if (EQS_FIELD) - value_f(pos); - else - value_s(pos); - } - - /*! \brief fill the row - * - * - */ - static openfpm::vector<triplet<typename Sys_eqs::stype>> value_s(grid_key_dx<Sys_eqs::dims> & it) - { - return expr1::const_value(it) * expr2::value_s(it); - } - - /*! \brief fill the row - * - * - */ - static void value_f(grid_key_dx<Sys_eqs::dims> & it) - { - return expr1::const_value(it) * expr2::value_s(it); - } -}; - // spatial position + value template<unsigned int dim,typename T> diff --git a/src/FiniteDifference/eq_unit_test.hpp b/src/FiniteDifference/eq_unit_test.hpp index e9f97eda..4a6fb1ff 100644 --- a/src/FiniteDifference/eq_unit_test.hpp +++ b/src/FiniteDifference/eq_unit_test.hpp @@ -11,6 +11,7 @@ #include "Laplacian.hpp" #include "FiniteDifference/eq.hpp" #include "FiniteDifference/sum.hpp" +#include "FiniteDifference/mul.hpp" #include "Grid/grid_dist_id.hpp" #include "data_type/scalar.hpp" #include "Decomposition/CartDecomposition.hpp" @@ -41,12 +42,12 @@ struct lid_nn const bool lid_nn::boundary[] = {NON_PERIODIC,NON_PERIODIC}; // Constant Field - struct eta { + float val() {return 1.0;} }; -// Model the equation +// Model the equations constexpr unsigned int v[] = {0,1}; constexpr unsigned int P = 2; @@ -73,6 +74,8 @@ typedef D<x,v_x,lid_nn> dx_vx; typedef D<y,v_y,lid_nn> dy_vy; typedef sum<dx_vx,dy_vy> incompressibility; +BOOST_AUTO_TEST_SUITE( eq_test_suite ) + // Lid driven cavity, uncompressible fluid BOOST_AUTO_TEST_CASE( lid_driven_cavity ) @@ -107,4 +110,6 @@ BOOST_AUTO_TEST_CASE( lid_driven_cavity ) fd.impose(vy, g_dist.getGridInfo(), g_dist.getSubDomainIterator(bulk_start,bulk_end)); } +BOOST_AUTO_TEST_SUITE_END() + #endif /* OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_EQ_UNIT_TEST_HPP_ */ diff --git a/src/FiniteDifference/sum.hpp b/src/FiniteDifference/sum.hpp index f2eca613..0f4eb148 100644 --- a/src/FiniteDifference/sum.hpp +++ b/src/FiniteDifference/sum.hpp @@ -9,6 +9,46 @@ #define OPENFPM_NUMERICS_SRC_FINITEDIFFERENCE_SUM_HPP_ #include <boost/mpl/vector.hpp> +#include "config.h" +#include <unordered_map> +#include "util/for_each_ref.hpp" + +template<typename v_expr> +struct sum_functor_value +{ + //! Number of elements in the vector v_expr + typedef boost::mpl::size<v_expr> size; + + //! Last element of sum + typedef typename boost::mpl::at<v_expr,boost::mpl::int_<size::value-1> >::type last; + + //! sum functor + std::unordered_map<long int,typename last::stype> & cols; + + const grid_sm<last::dims,void> & gs; + + //! position + grid_key_dx<last::dims> & key; + + //! coefficent + typename last::stype coeff; + + /*! \brief constructor + * + */ + sum_functor_value(grid_key_dx<last::dims> & key, const grid_sm<last::dims,void> & gs, typename last::stype coeff) + :cols(cols),gs(gs),key(key),coeff(coeff) + {}; + + + + //! It call this function for every expression in the sum + template<typename T> + void operator()(T& t) const + { + boost::mpl::at<v_expr, boost::mpl::int_<T::value> >::type::value(key,gs,cols,coeff); + } +}; /*! \brief It model an expression expr1 + ... exprn * @@ -32,9 +72,13 @@ struct sum * \tparam ord * */ - inline static std::unordered_map<long int,typename Sys_eqs::stype> value(grid_key_dx<Sys_eqs::dims> & pos, const grid_sm<Sys_eqs::dims,void> & gs, std::unordered_map<long int,typename Sys_eqs::stype > & cols, typename Sys_eqs::stype coeff) + inline static void value(grid_key_dx<Sys_eqs::dims> & pos, const grid_sm<Sys_eqs::dims,void> & gs, std::unordered_map<long int,typename Sys_eqs::stype > & cols, typename Sys_eqs::stype coeff) { - std::cerr << "Error " << __FILE__ << ":" << __LINE__ << " only CENTRAL, FORWARD, BACKWARD derivative are defined"; + // Sum functor + sum_functor_value<v_expr> sm(pos,gs,coeff); + + // for each element in the expression calculate the non-zero Matrix elements + boost::mpl::for_each_ref< boost::mpl::range_c<int,0,v_sz::type::value - 1> >(sm); } /*! \brief Calculate the position where the derivative is calculated diff --git a/src/main.cpp b/src/main.cpp index 26c8feef..e8e3e392 100755 --- a/src/main.cpp +++ b/src/main.cpp @@ -6,3 +6,4 @@ #include "FiniteDifference/FDScheme_unit_tests.hpp" #include "FiniteDifference/util/common_test.hpp" #include "FiniteDifference/eq_unit_test.hpp" +#include "util/util_num_unit_tests.hpp" -- GitLab