SparseMatrix.hpp 3.64 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
#include "config/config.h"
#include "util/linalgebra_lib.hpp"
incardon's avatar
incardon committed
13 14
#include "Vector/map_vector.hpp"
#include "VCluster/VCluster.hpp"
15

16
#ifdef HAVE_EIGEN
17
#include <Eigen/Sparse>
18
#define DEFAULT_MATRIX  = EIGEN_BASE
19
#else
20
#define DEFAULT_MATRIX = 0
21
#endif
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36

/*! \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
 *
 *
 */
37
template<typename T, int impl> struct triplet
38 39 40 41 42
{
	long int i;
	long int j;
	T val;

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

	triplet()
	{
	}

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

	long int & col()
	{
		return j;
	}

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

Pietro Incardona's avatar
Pietro Incardona committed
70
/*! \brief Sparse Matrix implementation
71
 *
Pietro Incardona's avatar
Pietro Incardona committed
72
 * \tparam T Type of the sparse Matrix store on each row,colums
73
 * \tparam id_t type of id
Pietro Incardona's avatar
Pietro Incardona committed
74
 * \tparam Mi implementation
75 76
 *
 */
77
template<typename T,typename id_t ,unsigned int Mi DEFAULT_MATRIX>
78 79
class SparseMatrix
{
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
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;}
96
	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;}
97 98 99 100
	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;}
101
	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;}
102 103 104 105
	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;}
106 107
};

108
#ifdef HAVE_EIGEN
109
#include "SparseMatrix_Eigen.hpp"
110
#endif
111

112 113 114 115
#ifdef HAVE_PETSC
#include "SparseMatrix_petsc.hpp"
#endif

116
#endif /* OPENFPM_NUMERICS_SRC_MATRIX_SPARSEMATRIX_HPP_ */