SparseMatrix.hpp 3.58 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*
 * Matrix.hpp
 *
 *  Created on: Oct 5, 2015
 *      Author: i-bird
 */

#ifndef OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_
#define OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_

11 12 13
#include "config/config.h"
#include "util/linalgebra_lib.hpp"

14
#ifdef HAVE_EIGEN
15
#include <Eigen/Sparse>
Pietro Incardona's avatar
Pietro Incardona committed
16
#define DEFAULT_MATRIX  = EIGEN_BASE
17
#else
Pietro Incardona's avatar
Pietro Incardona committed
18
#define DEFAULT_MATRIX = 0
19
#endif
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

/*! \brief It store the non zero elements of the matrix
 *
 *
 */
template<typename T> struct cval
{
	size_t j;
	T value;
};

/*! \brief It store the non zero elements of the matrix
 *
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
35
template<typename T, int impl> struct triplet
36 37 38 39 40
{
	long int i;
	long int j;
	T val;

41 42 43 44 45 46 47 48 49 50 51
	triplet(long int i, long int j, T val)
	{
		row() = i;
		col() = j;
		value() = val;
	}

	triplet()
	{
	}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
	long int & row()
	{
		return i;
	}

	long int & col()
	{
		return j;
	}

	T & value()
	{
		return val;
	}
};

Pietro Incardona's avatar
Pietro Incardona committed
68
/*! \brief Sparse Matrix implementation
69
 *
Pietro Incardona's avatar
Pietro Incardona committed
70
 * \tparam T Type of the sparse Matrix store on each row,colums
71
 * \tparam id_t type of id
Pietro Incardona's avatar
Pietro Incardona committed
72
 * \tparam Mi implementation
73 74
 *
 */
Pietro Incardona's avatar
Pietro Incardona committed
75
template<typename T,typename id_t ,unsigned int Mi DEFAULT_MATRIX>
76 77
class SparseMatrix
{
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
public:

	//! Triplet implementation id
	typedef boost::mpl::int_<-1> triplet_impl;

	//! Triplet type
	typedef triplet<T,-1> triplet_type;

	openfpm::vector<triplet_type> stub_vt;
	int stub_i;
	T stub_t;

public:


	SparseMatrix(size_t N1, size_t N2) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
Pietro Incardona's avatar
Pietro Incardona committed
94
	SparseMatrix(size_t N1, size_t N2, size_t loc) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
95 96 97 98
	SparseMatrix()	{std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
	openfpm::vector<triplet_type> & getMatrixTriplets() {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_vt;}
	const int & getMat() const {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
	int & getMat() {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
99
	void resize(size_t row, size_t col, size_t row_n, size_t col_n) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;}
100 101 102 103
	T operator()(id_t i, id_t j) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_t;}
	bool save(const std::string & file) const {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl;return true;}
	bool load(const std::string & file) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return false;}
	T getValue(size_t r, size_t c) {std::cerr << __FILE__ << ":" << __LINE__ << " Error in order to use this class you must compile OpenFPM with linear algebra support" << std::endl; return stub_i;}
104 105
};

106
#ifdef HAVE_EIGEN
107
#include "SparseMatrix_Eigen.hpp"
108
#endif
109

Pietro Incardona's avatar
Pietro Incardona committed
110 111 112 113
#ifdef HAVE_PETSC
#include "SparseMatrix_petsc.hpp"
#endif

114
#endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_ */