Utils.hpp 3.24 KB
Newer Older
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
1 2 3 4 5 6 7 8 9 10 11
#ifndef UTILS_HPP
#define UTILS_HPP

#include "Base.hpp"
#include "Block.hpp"

/**
 * @brief This struct will hold some output concerning an arnoldi
 * procedure.
 */
struct ArnReturn{
12
    int sizeKSpace;
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
13 14 15 16 17 18 19 20
    int nbIteDone;
    bool hasConverged;
};


/**
 * @brief This function test if the current solution is accurate
 * enough. It will works only if the RHS are scaled, (i.e each column
21 22 23
 * vector of B has a norm of 1).
 * If the epsilon conditon is o completed, at the end, X0 contains
 * Base*Y + X0, or PreCond*Base*Y + X0 if a right precond is used.
PIACIBELLO Cyrille's avatar
PIACIBELLO Cyrille committed
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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
 *
 * @param A Matrix provided buy the user
 * @param B RHS (each column vector of B must be of norm 1)
 * @param X0 : initial guess, will be overwritten by
 * solution if criteria is satisfied
 * @param Base Current base
 * @param Y Current solution of least square problem
 * @param epsilon target accuracy
 */
template<class Matrix, class Scalar, class Primary=Scalar>
bool CheckCurrentSolution(Matrix&A, Block<Scalar,Primary>& B,
                          Block<Scalar,Primary>& X0,
                          Base<Scalar,Primary>& Base,
                          Block<Scalar,Primary>& Y, Primary epsilon){
    int dim = A.size();
    int SizeBlock = B.getSizeBlock();

    //Create temporary block to store solution
    Block<Scalar,Primary> Sol(SizeBlock,dim);
    if(A.useRightPreCond()){
        Block<Scalar,Primary> temp(SizeBlock,dim);
        Base.ComputeProduct(Y,temp);
        A.preCondBlockVect(temp,Sol);
    }else{//No pre Cond used
        Base.ComputeProduct(Y,Sol);
    }
    //Adding X0
    for(int i=0 ; i<SizeBlock ; ++i){
        for(int k=0 ; k<dim ; ++k){
            Sol.getPtr(i)[k] += X0.getPtr(i)[k];
        }
    }
    //Then ||A*Sol -B||
    Block<Scalar,Primary> AxSol(SizeBlock,dim);
    A.MatBlockVect(Sol,AxSol);
    for(int i=0 ; i<SizeBlock ; ++i){
        for(int j=0 ; j<dim ; ++j){
            AxSol.getPtr(i)[j] -= B.getPtr(i)[j];
        }
    }
    std::pair<Primary,Primary> RealMinMax = AxSol.getMinMaxNorm();
    if(RealMinMax.second < epsilon){
        std::cout<<"Convergence !!"<<std::endl;
        X0.CopyBlock(Sol);
        return true;
    }
    return false;
}


/**
 * @brief This function compare two blocks of vectors of the same
 * size, and display the min and max of frobenius norm overs the vectors
 *
 * @param block1
 * @param block2
 */
template<class Scalar, class Primary>
std::pair<Primary,Primary>
CompareBlocks(Block<Scalar,Primary>& block1, Block<Scalar,Primary>& block2){

    std::pair<Primary,Primary> MinMax{1000,0};
    if(block1.getSizeBlock() != block2.getSizeBlock()){
        std::cout<<"Size of blocks not compatible\nExiting\n";
        exit(0);
    }
    if(block1.getLeadingDim() != block2.getLeadingDim()){
        std::cout<<"Leading dim of blocks not compatible\nExiting\n";
        exit(0);
    }
    for(int i=0 ; i<block1.getSizeBlock() ; ++i){
        Primary norm{0};
        for(int j=0 ; j<block1.getLeadingDim() ; ++j){
            norm += module<Scalar,Primary>(block1.getPtr(i)[j] - block2.getPtr(i)[j]);
        }
        auto norm2 = std::sqrt(norm);
        if(norm2 > MinMax.second){
            MinMax.second = norm2;
        }
        if(norm2 < MinMax.first){
            MinMax.first = norm2;
        }
    }
    return MinMax;
}

#endif //UTILS_HPP