ATSuite C++  v1.0
Scientific C++ routines originally developed by Alexis Tantet
transferOperatorTest.hpp
1 #ifndef TRANSFEROPERATORTEST_HPP
2 #define TRANSFEROPERATORTEST_HPP
3 
4 #include <vector>
5 #include <cmath>
6 #include <gsl/gsl_vector.h>
7 #include <gsl/gsl_vector_int.h>
8 #include <gsl/gsl_matrix.h>
9 #include <gsl/gsl_rng.h>
10 #include <gsl/gsl_randist.h>
11 #include <Eigen/Dense>
12 #include <Eigen/Sparse>
13 #include "atmath.hpp"
14 #include "atio.hpp"
15 #include "atspectrum.hpp"
16 #include "transferOperator.hpp"
17 
18 typedef Eigen::Triplet<double> triplet;
19 typedef std::vector<triplet> tripletVector;
20 typedef Eigen::SparseMatrix<double, Eigen::ColMajor> SpMatCSC;
21 typedef Eigen::SparseMatrix<double, Eigen::RowMajor> SpMatCSR;
22 
23 
24 // Declarations
25 void getSurrogateSpectrum(gsl_vector_int *, size_t, size_t, gsl_matrix *, gsl_matrix *,
26  const char *, double, int, int, bool, double *);
27 void getSurrogateSpectrumFromCount(SpMatCSR *, gsl_rng *, gsl_matrix *, gsl_matrix*,
28  const char *, double, int, int, bool, double *);
29 SpMatCSR *getShuffledCountMatrix(SpMatCSR *, gsl_vector *, gsl_rng *);
30 void getShuffledRow(SpMatCSR *, size_t, size_t, tripletVector *, gsl_rng *);
31 
32 
33 // Definitions
34 void getSurrogateSpectrum(gsl_vector_int * gridMem, size_t N, size_t tauStep,
35  gsl_matrix *EigValRealDist, gsl_matrix *EigValImagDist,
36  const char *which="LM", double tol=0., int maxit=0, int ncv=0,
37  bool AutoShift=true, double *resid=NULL)
38 {
39  SpMatCSR *C = new SpMatCSR(N, N);
40  tripletVector *T;
41  std::vector< std::vector <int > > boxDest;
42  std::vector<int> rowTmp;
43  const gsl_rng_type *rngType = gsl_rng_ranlxs1;
44  int seed=0.;
45 
46  // Get transition count triplets
47  T = getTransitionCountTriplets(gridMem, tauStep);
48 
49  // Get transition count matrix
50  C->setFromTriplets(T->begin(), T->end());
51 
52  // Set random generator and its seed
53  gsl_rng *r = gsl_rng_alloc(rngType);
54  gsl_rng_set(r, seed);
55 
56  // Get surrogate spectrum
57  getSurrogateSpectrumFromCount(C, r, EigValRealDist, EigValImagDist,
58  which, tol, maxit, ncv, AutoShift, resid);
59 
60  gsl_rng_free(r);
61  delete C;
62  delete T;
63 
64  return;
65 }
66 
67 
68 void getSurrogateSpectrumFromCount(SpMatCSR *C, gsl_rng *r,
69  gsl_matrix *EigValRealDist, gsl_matrix *EigValImagDist,
70  const char *which="LM", double tol=0., int maxit=0,
71  int ncv=0, bool AutoShift=true, double *resid=NULL)
72 {
73  int nev = EigValRealDist->size1;
74  int Ns = EigValRealDist->size2;
75  int N = C->rows();
76  gsl_vector *nTransPerRow;
77 
78  // Get row sums
79  nTransPerRow = getRowSum(C);
80 
81 #pragma omp parallel
82  {
83  double *EigValReal;
84  double *EigValImag;
85  SpMatCSR *Ps;
86  SpMatCSR CCopy(N, N);
87  ARluNonSymMatrix<double, double> *PsAR;
88 #pragma omp critical
89  {
90  CCopy = *C;
91  }
92 
93  // Get Ns surrogate spectra
94 #pragma omp for
95  for (int s = 0; s < Ns; s++){
96  // Get shuffled count matrix
97  std::cout << "Getting surrogate eigenvalues " << s << " out of " << (Ns-1)
98  << std::endl;
99  Ps = getShuffledCountMatrix(&CCopy, nTransPerRow, r);
100  // Get transition matrix
101  toStochastic(Ps);
102  // Convert to Arpack matrix format
103  PsAR = Eigen2AR(Ps);
104 
105 #pragma omp critical
106  {
107  // Get eigenvalues
108  EigValReal = new double[nev + 2];
109  EigValImag = new double[nev + 2];
110  getEigValNonSym(PsAR, EigValReal, EigValImag, nev, which, tol, maxit, ncv,
111  AutoShift, resid);
112 
113  // Store
114  for (int ev = 0; ev < nev; ev++){
115  gsl_matrix_set(EigValRealDist, ev, s, EigValReal[ev]);
116  gsl_matrix_set(EigValImagDist, ev, s, EigValImag[ev]);
117  }
118  }
119 
120  // Clean
121  delete[] EigValReal;
122  delete[] EigValImag;
123  delete Ps;
124  delete PsAR;
125  }
126  }
127 
128  gsl_vector_free(nTransPerRow);
129 
130  return;
131 }
132 
133 
134 SpMatCSR *getShuffledCountMatrix(SpMatCSR *C, gsl_vector *nTransPerRow, gsl_rng *r)
135 {
136  size_t N = C->rows();
137  SpMatCSR *Cs = new SpMatCSR(N, N);
138  tripletVector *Ts = new tripletVector;
139  size_t nTrans;
140 
141  // Reserve shuffled transition count triplet
142  Ts->reserve(C->nonZeros());
143 
144  // Shuffle each row
145  for (size_t i = 0; i < N; i++){
146  nTrans = (size_t) round(gsl_vector_get(nTransPerRow, i));
147  if (nTrans > 0)
148  getShuffledRow(C, i, nTrans, Ts, r);
149  }
150 
151  // Get shuffled matrix
152  Cs->setFromTriplets(Ts->begin(), Ts->end());
153  delete Ts;
154 
155  return Cs;
156 }
157 
158 void getShuffledRow(SpMatCSR *C, size_t iRow, size_t nTrans,
159  tripletVector *Ts,
160  gsl_rng *r)
161 {
162  size_t k;
163  gsl_vector_int *dstRepeat = gsl_vector_int_alloc(nTrans);
164  unsigned long int sampleIdx;
165  int sample;
166 
167  // Get repeated list of destinations
168  k = 0;
169  for (SpMatCSR::InnerIterator it(*C, iRow); it; ++it){
170  for (int r = 0; r < it.value(); r++){
171  gsl_vector_int_set(dstRepeat, k, it.col());
172  k++;
173  }
174  }
175 
176  // Shuffle row
177  for (k = 0; k < nTrans; k++){
178  sampleIdx = gsl_rng_uniform_int(r, nTrans);
179  sample = gsl_vector_int_get(dstRepeat, sampleIdx);
180  Ts->push_back(triplet(iRow, sample, 1));
181  }
182 
183  gsl_vector_int_free(dstRepeat);
184  return;
185 }
186 
187 
188 #endif
Eigen::Triplet< double > triplet
Eigen triplet of double.
Eigen::SparseMatrix< double, Eigen::RowMajor > SpMatCSR
Eigen sparse CSR matrix of double type.
Definition: atio.hpp:25
Mathematical routines.
gsl_vector * getRowSum(SpMatCSR *)
Get the sum of each row of an Eigen CSR matrix.
Definition: atmatrix.hpp:56
std::vector< triplet > tripletVector
STD vector of Eigen triplet of double.
Input, output and conversion routines.
Calculate discretized approximation of transfer operators from time series.
Get spectrum of sparse matrices using ARPACK++.
ARluNonSymMatrix< double, double > * Eigen2AR(SpMatCSC *)
Converts an Eigen CSC matrix to an ARPACK++ nonsymmetric CSC matrix.
Definition: atspectrum.hpp:168
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen sparse CSC matrix of double type.
Definition: atio.hpp:27