Vandermonde_unit_tests.cpp 6.73 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
//
// Created by tommaso on 22/03/19.
//

#define BOOST_TEST_DYN_LINK

#include <boost/test/unit_test.hpp>
#include <DCPSE/MonomialBasis.hpp>
#include <DCPSE/VandermondeRowBuilder.hpp>
#include <DCPSE/Vandermonde.hpp>
#include "../../openfpm_numerics/src/DMatrix/EMatrix.hpp"

BOOST_AUTO_TEST_SUITE(Vandermonde_tests)

    BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_AllOnes_test)
    {
        MonomialBasis<2> mb({1, 0}, 2);
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
        Point<2, double> x({2,2});
        double eps = 2;
        VandermondeRowBuilder<2, double> vrb(mb);
        vrb.buildRow(row, 0, x, eps);
        // For the way the row has been constructed, it should be composed of only 1s
        bool isRowAllOnes = true;
        for (int i = 0; i < mb.size(); ++i)
        {
            isRowAllOnes = isRowAllOnes && (row(0,i)==1);
        }
        BOOST_REQUIRE(isRowAllOnes);
    }

    BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_OneZero_test)
    {
        MonomialBasis<2> mb({1, 0}, 2);
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
        Point<2, double> x({1,0});
        double eps = 1;
        VandermondeRowBuilder<2, double> vrb(mb);
        vrb.buildRow(row, 0, x, eps);
        // For the way the row has been constructed, it should be composed of only 1s
        bool areValuesOk = true;
        for (int i = 0; i < mb.size(); ++i)
        {
            bool isThereY = mb.getElement(i).getExponent(1) > 0;
            bool curCheck = (row(0,i)==!isThereY);
            areValuesOk = areValuesOk && curCheck;
        }
        BOOST_REQUIRE(areValuesOk);
    }

    BOOST_AUTO_TEST_CASE(VandermondeRowBuilder_ZeroOne_test)
    {
        MonomialBasis<2> mb({1, 0}, 2);
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> row(1, mb.size());
        Point<2, double> x({0,1});
        double eps = 1;
        VandermondeRowBuilder<2, double> vrb(mb);
        vrb.buildRow(row, 0, x, eps);
        // For the way the row has been constructed, it should be composed of only 1s
        bool areValuesOk = true;
        for (int i = 0; i < mb.size(); ++i)
        {
            bool isThereX = mb.getElement(i).getExponent(0) > 0;
            bool curCheck = (row(0,i)==!isThereX);
            areValuesOk = areValuesOk && curCheck;
        }
        BOOST_REQUIRE(areValuesOk);
    }

    BOOST_AUTO_TEST_CASE(Vandermonde_KnownValues_test)
    {
        MonomialBasis<2> mb({1, 0}, 2);
//        std::cout << mb << std::endl; // Debug
        Point<2, double> x({0,0});
        std::vector<Point<2, double>> neighbours;
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> V(mb.size(), mb.size());
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> ExpectedV(mb.size(), mb.size());
        // Now push 4 points on diagonal as neighbours
        neighbours.push_back({1,1});
        neighbours.push_back({-1,-1});
        neighbours.push_back({1,-1});
        neighbours.push_back({-1,1});
        neighbours.push_back({0,1});
        neighbours.push_back({0,-1});
        // ...and get the matrix V
        Vandermonde<2, double, EMatrix<double, Eigen::Dynamic, Eigen::Dynamic>> vandermonde(x, neighbours, mb);
        vandermonde.getMatrix(V);

        // Now build the matrix of expected values
        ExpectedV(0,0) = 1; ExpectedV(0,1) = -0.3; ExpectedV(0,2) = 0.09; ExpectedV(0,3) = -0.3; ExpectedV(0,4) = +0.09; ExpectedV(0,5) = 0.09;
        ExpectedV(1,0) = 1; ExpectedV(1,1) = +0.3; ExpectedV(1,2) = 0.09; ExpectedV(1,3) = +0.3; ExpectedV(1,4) = +0.09; ExpectedV(1,5) = 0.09;
        ExpectedV(2,0) = 1; ExpectedV(2,1) = -0.3; ExpectedV(2,2) = 0.09; ExpectedV(2,3) = +0.3; ExpectedV(2,4) = -0.09; ExpectedV(2,5) = 0.09;
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
        ExpectedV(3,0) = 1; ExpectedV(3,1) = +0.3; ExpectedV(3,2) = 0.09; ExpectedV(3,3) = -0.3; ExpectedV(3,4) = -0.09; ExpectedV(3,5) = 0.09;
        ExpectedV(4,0) = 1; ExpectedV(4,1) = 0;    ExpectedV(4,2) = 0;    ExpectedV(4,3) = -0.3; ExpectedV(4,4) = 0;     ExpectedV(4,5) = 0.09;
        ExpectedV(5,0) = 1; ExpectedV(5,1) = 0;    ExpectedV(5,2) = 0;    ExpectedV(5,3) = +0.3; ExpectedV(5,4) = 0;     ExpectedV(5,5) = 0.09;

        // Now check that the values in V are all the expected ones
        for (int i = 0; i < mb.size(); ++i)
        {
//            std::cout << "N[" << i << "] = " << neighbours[i].toString() << std::endl;
            for (int j = 0; j < mb.size(); ++j)
            {
//                std::cout << ">> V[" << i << "," << j << "] = " << V(i,j) << std::endl;
                BOOST_REQUIRE_CLOSE(V(i,j), ExpectedV(i,j), 1e-6);
            }
        }
    }

    BOOST_AUTO_TEST_CASE(Vandermonde_TranslatedSetup_test)
    {
        MonomialBasis<2> mb({1, 0}, 2);
//        std::cout << mb << std::endl; // Debug
        Point<2, double> x({1,1});
        std::vector<Point<2, double>> neighbours;
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> V(mb.size(), mb.size());
        EMatrix<double, Eigen::Dynamic, Eigen::Dynamic> ExpectedV(mb.size(), mb.size());
        // Now push 4 points on diagonal as neighbours
        neighbours.push_back({2,2});
        neighbours.push_back({0,0});
        neighbours.push_back({2,0});
        neighbours.push_back({0,2});
        neighbours.push_back({1,2});
        neighbours.push_back({1,0});
        // ...and get the matrix V
        Vandermonde<2, double, EMatrix<double, Eigen::Dynamic, Eigen::Dynamic>> vandermonde(x, neighbours, mb);
        vandermonde.getMatrix(V);

        // Now build the matrix of expected values
        ExpectedV(0,0) = 1; ExpectedV(0,1) = -0.3; ExpectedV(0,2) = 0.09; ExpectedV(0,3) = -0.3; ExpectedV(0,4) = +0.09; ExpectedV(0,5) = 0.09;
        ExpectedV(1,0) = 1; ExpectedV(1,1) = +0.3; ExpectedV(1,2) = 0.09; ExpectedV(1,3) = +0.3; ExpectedV(1,4) = +0.09; ExpectedV(1,5) = 0.09;
        ExpectedV(2,0) = 1; ExpectedV(2,1) = -0.3; ExpectedV(2,2) = 0.09; ExpectedV(2,3) = +0.3; ExpectedV(2,4) = -0.09; ExpectedV(2,5) = 0.09;
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
        ExpectedV(3,0) = 1; ExpectedV(3,1) = +0.3; ExpectedV(3,2) = 0.09; ExpectedV(3,3) = -0.3; ExpectedV(3,4) = -0.09; ExpectedV(3,5) = 0.09;
        ExpectedV(4,0) = 1; ExpectedV(4,1) = 0;    ExpectedV(4,2) = 0;    ExpectedV(4,3) = -0.3; ExpectedV(4,4) = 0;     ExpectedV(4,5) = 0.09;
        ExpectedV(5,0) = 1; ExpectedV(5,1) = 0;    ExpectedV(5,2) = 0;    ExpectedV(5,3) = +0.3; ExpectedV(5,4) = 0;     ExpectedV(5,5) = 0.09;

        // Now check that the values in V are all the expected ones
        for (int i = 0; i < mb.size(); ++i)
        {
//            std::cout << "N[" << i << "] = " << neighbours[i].toString() << std::endl;
            for (int j = 0; j < mb.size(); ++j)
            {
//                std::cout << ">> V[" << i << "," << j << "] = " << V(i,j) << std::endl;
                BOOST_REQUIRE_CLOSE(V(i,j), ExpectedV(i,j), 1e-6);
            }
        }
    }

BOOST_AUTO_TEST_SUITE_END()