- changed routs' output
[alexxy/gromacs-spacetimecorr.git] / src / spacetimecorr.cpp
index ad95a5824c4eb9b1c449873441cbd8bccf094558..3cbc4730b15ae7045bfea18b9ed5c3172a812029 100644 (file)
@@ -53,6 +53,8 @@
 #include <gromacs/utility/smalloc.h>
 #include <gromacs/math/do_fit.h>
 #include <gromacs/trajectoryanalysis/topologyinformation.h>
+#include "gromacs/topology/topology.h"
+#include "gromacs/topology/index.h"
 
 using namespace gmx;
 
@@ -88,121 +90,92 @@ const std::vector< std::vector< std::vector< double > > > read_correlation_matri
     return crrlts;
 }
 
-void make_correlation_matrix_file(const std::vector< std::vector< std::vector< double > > > correlations, const char* file_name)
-{
-    FILE *file;
-    file = std::fopen(file_name, "w+");
-    std::cout << "writing correlation matrixes: \n";
-    for (unsigned int i = 0; i < correlations.size(); i++) {
-        std::fprintf(file, "%d\n", i);
-        for (unsigned int j = 0; j < correlations[i].size(); j++) {
-            for (unsigned int f = 0; f < correlations[i][j].size(); f++) {
-                std::fprintf(file, "%.6f ", correlations[i][j][f]); //~16
-            }
-            std::fprintf(file, "\n");
+template< typename T >
+unsigned int posSrch(std::vector< T > a, T b) {
+    for (unsigned i01 = 0; i01 < a.size(); i01++) {
+        if (a[01] == b) {
+            return i01;
+            break;
         }
-        std::cout << i << " | ";
     }
-    std::fclose(file);
-    std::cout << "\n";
+    return a.size() + 1;
 }
 
-void make_correlation_pairs_file(std::vector< std::vector< std::vector< double > > > correlations, const char* file_name)
+void make_rout_file(double crl_border, std::vector< int > indx, std::vector< std::string > resNames, std::vector< std::vector< std::vector< std::pair< unsigned int, unsigned int > > > > rout, const char* file_name01)
 {
-    FILE *file;
-    file = std::fopen(file_name, "w+");
-    for (unsigned int i = 0; i < correlations.front().size(); i++) {
-        for (unsigned int j = 0; j < correlations.front().front().size(); j++) {
-            //std::fprintf(file, "correlation between point '%d' and point '%d'\n", i, j);
-            std::fprintf(file, "%d %d\n", i, j);
-            for (unsigned int k = 0; k < correlations.size(); k++) {
-                std::fprintf(file, "%3.4f ", correlations[k][i][j]);
+    FILE *file01;
+    file01 = std::fopen(file_name01, "w+");
+
+    unsigned int f;
+    std::vector< std::tuple< int, int, std::vector< int > > > table;
+
+    for (unsigned int i01 = 0; i01 < rout.size(); i01++) {
+        f = i01;
+        for (unsigned int i02 = i01 + 1; i02 <rout.size(); i02++) {
+            if (rout[i02] == rout[i01]) {
+                f = i02;
+            } else {
+                break;
             }
-            std::fprintf(file, "\n");
         }
-        if (i % 10 == 0) {
-            std::cout << "correlations in row " << i << " out of " << correlations.front().size() << " completed\n";
+        if (i01 == f) {
+            std::fprintf(file01, "tau = %d | correlations >= %0.2f\n\n", i01, crl_border);
+        } else {
+            std::fprintf(file01, "tau = [%d ; %d] | correlations >= %0.2f\n\n", i01, f, crl_border);
         }
-    }
-    std::fclose(file);
-}
-
-void make_rout_file(double crl_border, std::vector< int > indx, std::vector< std::vector< std::vector< std::pair< unsigned int, unsigned int > > > > rout, const char* file_name)
-{
-    FILE *file;
-    file = std::fopen(file_name, "w+");
-    for (unsigned int i1 = 0; i1 < rout.size(); i1++) {
-        std::fprintf(file, "tau = %d | correlations >= %0.2f\n\n", i1, crl_border);
-        for (unsigned int i2 = 0; i2 < rout[i1].size(); i2++) {
-            for (unsigned int i3 = 0; i3 < rout[i1][i2].size(); i3++) {
-                std::fprintf(file, "cgo_arrow (id %3d), (id %3d), radius=0.15\n", indx[rout[i1][i2][i3].first] + 1, indx[rout[i1][i2][i3].second] + 1);
+        for (unsigned int i02 = 0; i02 < rout[i01].size(); i02++) {
+            for (unsigned int i03 = 0; i03 < rout[i01][i02].size(); i03++) {
+                std::fprintf(file01, "cgo_arrow (id %3d), (id %3d), radius=0.15\n", indx[rout[i01][i02][i03].first] + 1, indx[rout[i01][i02][i03].second] + 1);
             }
-            std::fprintf(file, "\n\n");
+            std::fprintf(file01, "\n");
         }
-    }
-    std::fclose(file);
-}
-
-void make_table_routs(std::vector< RVec > ref, double crl_border, std::vector< int > indx, std::vector< std::vector< std::pair< unsigned int, unsigned int > > > rout, const char* file_name)
-{
-    FILE *file;
-    file = std::fopen(file_name, "w+");
-    std::fprintf(file, "correlations >= %0.2f\n\n", crl_border);
-    std::vector< std::tuple< int, int, std::vector< int > > > table;
-
-    for (unsigned int i1 = 0; i1 < rout.size(); i1++) {
-        for (unsigned int i2 = 0; i2 < rout[i1].size(); i2++) {
-            bool flag = true;
-            for (unsigned int i3 = 0; i3 < table.size(); i3++) {
-                if (std::get<0>(table[i3]) == indx[rout[i1][i2].first] + 1) {
-                    std::get<1>(table[i3])++;
-                    std::get<2>(table[i3]).push_back(indx[rout[i1][i2].second] + 1);
-                    flag = false;
-                } else if (std::get<0>(table[i3]) == indx[rout[i1][i2].second] + 1) {
-                    std::get<1>(table[i3])++;
-                    std::get<2>(table[i3]).push_back(indx[rout[i1][i2].first] + 1);
-                    flag = false;
+        table.resize(0);
+        for (unsigned int i02 = 0; i02 < rout[i01].size(); i02++) {
+            for (unsigned int i03 = 0; i03 < rout[i01][i02].size(); i03++) {
+                bool flag = true;
+                for (unsigned int i04 = 0; i04 < table.size(); i04++) {
+                    if (std::get<0>(table[i04]) == indx[rout[i01][i02][i03].first] + 1) {
+                        std::get<1>(table[i04])++;
+                        std::get<2>(table[i04]).push_back(indx[rout[i01][i02][i03].second] + 1);
+                        flag = false;
+                    } else if (std::get<0>(table[i04]) == indx[rout[i01][i02][i03].second] + 1) {
+                        std::get<1>(table[i04])++;
+                        std::get<2>(table[i04]).push_back(indx[rout[i01][i02][i03].first] + 1);
+                        flag = false;
+                    }
+                }
+                if (flag) {
+                    std::vector< int > a;
+                    a.resize(0);
+                    a.push_back(indx[rout[i01][i02][i03].second] + 1);
+                    table.push_back(std::make_tuple(indx[rout[i01][i02][i03].first] + 1, 1, a));
+                    a.resize(0);
+                    a.push_back(indx[rout[i01][i02][i03].first] + 1);
+                    table.push_back(std::make_tuple(indx[rout[i01][i02][i03].second] + 1, 1, a));
                 }
             }
-            if (flag) {
-                std::vector< int > a;
-                a.resize(0);
-                a.push_back(indx[rout[i1][i2].second] + 1);
-                table.push_back(std::make_tuple(indx[rout[i1][i2].first] + 1, 1, a));
-                a.resize(0);
-                a.push_back(indx[rout[i1][i2].first] + 1);
-                table.push_back(std::make_tuple(indx[rout[i1][i2].second] + 1, 1, a));
+        }
+        for (unsigned int i02 = 0; i02 < table.size(); i02++) {
+            std::fprintf(file01, "residue %s connections %d | ", (resNames[posSrch(indx, std::get<0>(table[i02]))]).c_str(), std::get<1>(table[i02]));
+            for (unsigned int i03 = 0; i03 < std::get<2>(table[i03]).size(); i03++) {
+                std::fprintf(file01, "%s ", (resNames[posSrch(indx, std::get<2>(table[i02])[i03])]).c_str());
             }
+            std::fprintf(file01, "\n");
         }
-    }
 
-    for (unsigned int i = 0; i < table.size(); i++) {
-        std::fprintf(file, "atomN %d connections %d | ", std::get<0>(table[i]), std::get<1>(table[i]));
-        for (unsigned int j = 0; j < std::get<2>(table[i]).size(); j++) {
-            std::fprintf(file, "%d ", std::get<2>(table[i])[j]);
-        }
-        std::fprintf(file, "\n");
-    }
-    std::fclose(file);
-}
+        /*
+         *
+         *
+         * добавить вывод разницы в плане что +, а что -
+         *
+         *
+         *
+         */
+
 
-void make_best_corrs_graphics(std::vector< std::vector< std::vector< double > > > correlations,
-                              std::vector< std::vector< std::pair< unsigned int, unsigned int > > > rout_pairs,
-                              std::vector< int > indx,
-                              const char* file_name)
-{
-    FILE *file;
-    file = std::fopen(file_name, "w+");
-    for (unsigned int i = 0; i < rout_pairs.size(); i++) {
-        for (unsigned int j = 0; j < rout_pairs[i].size(); j++) {
-            std::fprintf(file, "%3d %3d\n", indx[rout_pairs[i][j].first]/* + 1*/, indx[rout_pairs[i][j].second]/* + 1*/);
-            for (unsigned int k = 0; k < correlations.size(); k++) {
-                std::fprintf(file, "%3.5f ", correlations[k][rout_pairs[i][j].first][rout_pairs[i][j].second]);
-            }
-            std::fprintf(file, "\n");
-        }
     }
-    std::fclose(file);
+
+    std::fclose(file01);
 }
 
 bool mysortfunc (std::vector< int > a, std::vector< int > b) {
@@ -532,6 +505,8 @@ class SpaceTimeCorr : public TrajectoryAnalysisModule
         std::vector< RVec >                                         reference;
 
         std::vector< int >                                          index;
+        std::vector< std::string >                                  resNms;
+
         int                                                         frames              = 0;
         int                                                         tau                 = 0;    // selectable
         int                                                         window              = 0;
@@ -609,11 +584,10 @@ SpaceTimeCorr::initAnalysis(const TrajectoryAnalysisSettings &settings, const To
     index.resize(0);
     for (ArrayRef< const int >::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
         index.push_back(*ai);
+        resNms.push_back(*(top.atoms()->resinfo[top.atoms()->atom[*ai].resind].name) + std::to_string((top.atoms()->atom[*ai].resind + 1)));
     }
     trajectory.resize(0);
 
-
-
     reference.resize(0);
     if (top.hasFullTopology()) {
         for (unsigned int i = 0; i < index.size(); i++) {
@@ -658,19 +632,13 @@ SpaceTimeCorr::finishAnalysis(int nframes)
 
         correlation_evaluation(reference, trajectory, k, 1000, (OutPutName + "-DumpData.txt").c_str());
 
-        //make_correlation_matrix_file(crltns, (OutPutName + "-matrix.txt").c_str());
-
-        //make_correlation_pairs_file(crltns, (OutPutName + "-matrix-pairs.txt").c_str());
-
-        //std::cout << "corelation matrix printed\n";
-
         std::cout << "Correlation's evaluation - end\n";
     } else if (mode == 1) {
-        rout_new.resize(trajectory.size() - window - k);
+        rout_new.resize(trajectory.size() - static_cast< unsigned long>(window) - k);
         FILE *file;
         file = std::fopen((OutPutName + "-DumpData.txt").c_str(), "r+");
         #pragma omp parallel for ordered schedule(dynamic) shared(rout_new) firstprivate(reference, crl_border, eff_rad, m, k)
-        for(unsigned int i = 0; i < trajectory.size() - window - k; i++) {
+        for(unsigned long i = 0; i < trajectory.size() - static_cast< unsigned int>(window) - k; i++) {
             std::vector< std::vector< std::vector< double > > > crltns;
             std::vector< std::vector< std::pair< double, long int > > > graph;
             std::vector< std::vector< unsigned int > > sub_graph;
@@ -706,21 +674,9 @@ SpaceTimeCorr::finishAnalysis(int nframes)
         #pragma omp barrier
     }
 
-    make_rout_file(static_cast< double >(crl_border), index, rout_new, (OutPutName + "-routs.txt").c_str());
-
-/*
-    make_rout_file(static_cast< double >(crl_border), index, rout_new, (OutPutName + "-routs.txt").c_str());
-    std::cout << "corelation routs printed\n";
-
-    make_best_corrs_graphics(crltns, rout_new, index, (OutPutName + "-routs-graphics.txt").c_str());
-    std::cout << "corelation routs' pairs' graphics printed\n";
-
-    make_table_routs(reference, static_cast< double >(crl_border), index, rout_new, (OutPutName + "-routs-table.txt").c_str());
+    make_rout_file(static_cast< double >(crl_border), index, resNms, rout_new, (OutPutName + "-routs.txt").c_str());
 
     std::cout << "Finish Analysis - end\n\n";
-*/
-
-
 }
 
 void