1 #ifndef TRANSFEROPERATOR_HPP 2 #define TRANSFEROPERATOR_HPP 6 #include <gsl/gsl_vector.h> 7 #include <gsl/gsl_vector_uint.h> 8 #include <gsl/gsl_matrix.h> 9 #include <gsl/gsl_matrix_uint.h> 10 #include <Eigen/Dense> 11 #include <Eigen/Sparse> 12 #include <ATSuite/atmatrix.hpp> 13 #include <ATSuite/atmarkov.hpp> 14 #include <ATSuite/atio.hpp> 15 #if defined (WITH_OMP) && WITH_OMP == 1 40 typedef Eigen::SparseMatrix<double, Eigen::ColMajor>
SpMatCSC;
42 typedef Eigen::SparseMatrix<double, Eigen::RowMajor>
SpMatCSR;
56 void allocate(gsl_vector_uint *);
58 void getRectGrid(gsl_vector_uint *,
const gsl_vector *,
const gsl_vector *);
73 Grid(gsl_vector_uint *nx_){ allocate(nx_); }
75 Grid(gsl_vector_uint *,
const gsl_vector *,
const gsl_vector *);
77 Grid(
size_t,
size_t,
double,
double);
82 int printGrid(
const char *,
const char *,
bool);
104 void allocate(
size_t);
106 void buildFromMembership(
const gsl_matrix_uint *);
132 int printForwardTransition(
const char *,
const char *);
134 int printBackwardTransition(
const char *,
const char *);
136 int printInitDist(
const char *,
const char *);
138 int printFinalDist(
const char *,
const char *);
179 buildFromMembership(gridMem);
193 const gsl_matrix *finalStates,
196 gsl_matrix_uint *gridMem;
206 buildFromMembership(gridMem);
209 gsl_matrix_uint_free(gridMem);
222 const size_t tauStep)
224 gsl_matrix_uint *gridMem;
234 buildFromMembership(gridMem);
237 gsl_matrix_uint_free(gridMem);
247 gsl_vector_free(initDist);
248 gsl_vector_free(finalDist);
257 Grid::Grid(gsl_vector_uint *nx_,
const gsl_vector *xmin,
const gsl_vector *xmax)
260 getRectGrid(nx_, xmin, xmax);
270 Grid::Grid(
size_t dim_,
size_t inx,
double dxmin,
double dxmax)
273 gsl_vector_uint *nx_ = gsl_vector_uint_alloc(dim_);
274 gsl_vector *xmin_ = gsl_vector_alloc(dim_);
275 gsl_vector *xmax_ = gsl_vector_alloc(dim_);
276 gsl_vector_uint_set_all(nx_, inx);
277 gsl_vector_set_all(xmin_, dxmin);
278 gsl_vector_set_all(xmax_, dxmax);
281 getRectGrid(nx_, xmin_, xmax_);
284 gsl_vector_uint_free(nx_);
285 gsl_vector_free(xmin_);
286 gsl_vector_free(xmax_);
292 gsl_vector_uint_free(
nx);
293 for (
size_t d = 0; d <
dim; d++)
308 transferOperator::allocate(
size_t gridSize)
313 initDist = gsl_vector_alloc(
N);
314 finalDist = gsl_vector_alloc(
N);
325 transferOperator::buildFromMembership(
const gsl_matrix_uint *gridMem)
331 P->setFromTriplets(T->begin(), T->end());
362 if ((fp = fopen(path,
"w")) == NULL){
363 fprintf(stderr,
"Can't open %s for printing the forward transition matrix!\n", path);
364 return(EXIT_FAILURE);
388 if ((fp = fopen(path,
"w")) == NULL){
389 fprintf(stderr,
"Can't open %s for printing the backward transition matrix!\n", path);
390 return(EXIT_FAILURE);
414 if ((fp = fopen(path,
"w")) == NULL){
415 fprintf(stderr,
"Can't open %s for printing the initial distribution!\n", path);
416 return(EXIT_FAILURE);
420 gsl_vector_fprintf(fp, initDist, dataFormat);
440 if ((fp = fopen(path,
"w")) == NULL){
441 fprintf(stderr,
"Can't open %s for printing the final distribution!\n", path);
442 return(EXIT_FAILURE);
446 gsl_vector_fprintf(fp, finalDist, dataFormat);
459 Grid::allocate(gsl_vector_uint *nx_)
463 nx = gsl_vector_uint_alloc(
dim);
464 gsl_vector_uint_memcpy(
nx, nx_);
468 for (
size_t d = 0; d <
dim; d++){
469 N *= gsl_vector_uint_get(
nx, d);
470 (*gridBounds)[d] = gsl_vector_alloc(gsl_vector_uint_get(
nx, d) + 1);
483 Grid::getRectGrid(gsl_vector_uint *nx_,
const gsl_vector *xmin,
const gsl_vector *xmax)
491 for (
size_t d = 0; d <
dim; d++) {
493 delta = (gsl_vector_get(xmax, d) - gsl_vector_get(xmin, d))
494 / gsl_vector_uint_get(
nx, d);
496 gsl_vector_set((*
gridBounds)[d], 0, gsl_vector_get(xmin, d));
497 for (
size_t i = 1; i < gsl_vector_uint_get(
nx, d) + 1; i++)
499 gsl_vector_get((*
gridBounds)[d], i-1) + delta);
519 if ((fp = fopen(path,
"w")) == NULL){
520 fprintf(stderr,
"Can't open %s for printing the grid!\n", path);
521 return(EXIT_FAILURE);
525 std::cout <<
"Domain grid (min, max, n):" << std::endl;
528 for (
size_t d = 0; d <
dim; d++) {
529 bounds = (*gridBounds)[d];
531 std::cout <<
"dim " << d+1 <<
": (" 532 << gsl_vector_get(bounds, 0) <<
", " 533 << gsl_vector_get(bounds, bounds->size - 1) <<
", " 534 << (bounds->size - 1) <<
")" << std::endl;
537 for (
size_t i = 0; i < bounds->size; i++){
538 fprintf(fp, dataFormat, gsl_vector_get((*
gridBounds)[d], i));
566 const size_t nTraj = initStates->size1;
567 const size_t dim = initStates->size2;
568 gsl_matrix_uint *gridMem;
571 gridMem = gsl_matrix_uint_alloc(grid->
N, 2);
576 gsl_vector *X = gsl_vector_alloc(dim);
579 for (
size_t traj = 0; traj < nTraj; traj++) {
581 gsl_matrix_get_row(X, initStates, traj);
585 gsl_matrix_get_row(X, finalStates, traj);
603 const size_t nStates = states->size1;
604 const size_t dim = states->size2;
605 gsl_vector_uint *gridMem = gsl_vector_uint_alloc(nStates);
610 gsl_vector *X = gsl_vector_alloc(dim);
613 for (
size_t traj = 0; traj < nStates; traj++) {
615 gsl_matrix_get_row(X, states, traj);
636 const size_t nStates = gridMemVect->size;
637 gsl_matrix_uint *gridMem = gsl_matrix_uint_alloc(nStates - tauStep, 2);
640 for (
size_t traj = 0; traj < (nStates - tauStep); traj++) {
641 gsl_matrix_uint_set(gridMem, traj, 0,
642 gsl_vector_uint_get(gridMemVect, traj));
643 gsl_matrix_uint_set(gridMem, traj, 1,
644 gsl_vector_uint_get(gridMemVect, traj + tauStep));
667 gsl_vector_uint_free(gridMemVect);
682 size_t nStatesTot = 0;
684 const size_t listSize = memList->size();
685 gsl_matrix_uint *gridMem, *gridMemMatrixL;
688 for (
size_t l = 0; l < listSize; l++)
689 nStatesTot += (memList->at(l))->size;
690 gridMem = gsl_matrix_uint_alloc(nStatesTot - tauStep * listSize, 2);
694 for (
size_t l = 0; l < listSize; l++) {
696 for (
size_t t = 0; t < gridMemMatrixL->size1; t++) {
697 gsl_matrix_uint_set(gridMem, count, 0,
698 gsl_matrix_uint_get(gridMemMatrixL, t, 0));
699 gsl_matrix_uint_set(gridMem, count, 1,
700 gsl_matrix_uint_get(gridMemMatrixL, t, 1));
703 gsl_matrix_uint_free(gridMemMatrixL);
718 const size_t dim = state->size;
719 size_t inBox, nBoxDir;
721 size_t subbp, subbn, ids;
726 for (
size_t box = 0; box < grid->
N; box++){
729 for (
size_t d = 0; d <
dim; d++){
731 nBoxDir = bounds->size - 1;
732 subbn = (size_t) (subbp / nBoxDir);
733 ids = subbp - subbn * nBoxDir;
734 inBox += (size_t) ((gsl_vector_get(state, d)
735 >= gsl_vector_get(bounds, ids))
736 & (gsl_vector_get(state, d)
737 < gsl_vector_get(bounds, ids+1)));
759 const size_t nTraj = gridMem->size1;
765 for (
size_t traj = 0; traj < nTraj; traj++) {
766 box0 = gsl_matrix_uint_get(gridMem, traj, 0);
767 boxf = gsl_matrix_uint_get(gridMem, traj, 1);
770 if ((box0 < N) && (boxf < N))
775 std::cout << nOut * 100. / nTraj
776 <<
"% of the trajectories ended up out of the domain." << std::endl;
int printFinalDist(const char *, const char *)
Print final distribution to file.
Eigen::SparseMatrix< double, Eigen::ColMajor > SpMatCSC
Eigen CSC matrix of double type.
gsl_vector_uint * getGridMemVector(const gsl_matrix *, const Grid *)
Get the grid membership matrix from a single long trajectory.
std::vector< gsl_vector * > * gridBounds
void toLeftStochastic(SpMatCSR *)
Make an Eigen CSR matrix left stochastic.
Eigen::SparseMatrix< double, Eigen::RowMajor > SpMatCSR
Eigen CSR matrix of double type.
int printForwardTransition(const char *, const char *)
Print forward transition matrix to file in compressed matrix format.
transferOperator(size_t gridSize)
Empty constructor allocating for grid size.
gsl_vector * finalDist
Final distribution.
Eigen::Triplet< double > triplet
Eigen triplet of double.
Eigen::Triplet< size_t > tripletUInt
Eigen triplet of integer.
~transferOperator()
Destructor.
transferOperator()
Default constructor.
tripletUIntVector * getTransitionCountTriplet(const gsl_matrix_uint *, size_t)
Get triplet vector from membership matrix.
SpMatCSR * Q
Backward transition matrix.
gsl_vector * getColSum(SpMatCSR *)
Get the sum of each column of an Eigen CSR matrix.
gsl_vector * initDist
Initial distribution.
gsl_vector * getRowSum(SpMatCSR *)
Get the sum of each row of an Eigen CSR matrix.
int printBackwardTransition(const char *, const char *)
Print backward transition matrix to file in compressed matrix format.
Grid()
Default constructor.
gsl_matrix_uint * memVectorList2memMatrix(const std::vector< gsl_vector_uint * > *, size_t)
Concatenate a list of membership vectors into one membership matrix.
std::vector< triplet > tripletVector
STD vector of Eigen triplet of double.
SpMatCSR * P
Forward transition matrix.
gsl_matrix_uint * getGridMemMatrix(const gsl_matrix *, const gsl_matrix *, const Grid *)
Get membership matrix from initial and final states for a grid.
gsl_matrix_uint * memVector2memMatrix(const gsl_vector_uint *, size_t)
Get the grid membership matrix from the membership vector for a given lag.
int printInitDist(const char *, const char *)
Print initial distribution to file.
std::vector< tripletUInt > tripletUIntVector
STD vector of Eigen triplet of integer.
int getBoxMembership(const gsl_vector *, const Grid *)
Get membership to a grid box of a single realization.
Grid(gsl_vector_uint *nx_)
Constructor allocating an empty grid.
void Eigen2Compressed(FILE *, const SpMatCSC *, const char *)
Print an Eigen CSC matrix to file in compressed format.
size_t N
Size of the grid.
void normalizeVector(gsl_vector *)
Normalize a GSL vector by the sum of its elements.
int printGrid(const char *, const char *, bool)
Print the grid to file.