added new algorythm
[alexxy/gromacs-rcore.git] / src / rcore.cpp
1 /*
2  * This file is part of the GROMACS molecular simulation package.
3  *
4  * Copyright (c) 2011,2012,2013,2014,2015, by the GROMACS development team, led by
5  * Mark Abraham, David van der Spoel, Berk Hess, and Erik Lindahl,
6  * and including many others, as listed in the AUTHORS file in the
7  * top-level source directory and at http://www.gromacs.org.
8  *
9  * GROMACS is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public License
11  * as published by the Free Software Foundation; either version 2.1
12  * of the License, or (at your option) any later version.
13  *
14  * GROMACS is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with GROMACS; if not, see
21  * http://www.gnu.org/licenses, or write to the Free Software Foundation,
22  * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA.
23  *
24  * If you want to redistribute modifications to GROMACS, please
25  * consider that scientific software is very special. Version
26  * control is crucial - bugs must be traceable. We will be happy to
27  * consider code for inclusion in the official distribution, but
28  * derived work must not be called official GROMACS. Details are found
29  * in the README & COPYING files - if they are missing, get the
30  * official version at http://www.gromacs.org.
31  *
32  * To help us fund GROMACS development, we humbly ask that you cite
33  * the research papers on the package. Check out http://www.gromacs.org.
34  */
35 #include <string>
36 #include <vector>
37 #include <iostream>
38 #include <omp.h>
39
40
41 #include <iostream>
42 #include <chrono>
43 #include <omp.h>
44 #include <thread>
45
46 #include <gromacs/utility/gmxomp.h>
47
48 #include <gromacs/trajectoryanalysis.h>
49 #include "new_fit.h"
50
51 using namespace gmx;
52
53 /*! \brief
54  * Template class to serve as a basis for user analysis tools.
55  */
56 class AnalysisTemplate : public TrajectoryAnalysisModule
57 {
58     public:
59         AnalysisTemplate();
60
61         virtual void initOptions(IOptionsContainer          *options,
62                                  TrajectoryAnalysisSettings *settings);
63         virtual void initAnalysis(const TrajectoryAnalysisSettings &settings,
64                                   const TopologyInformation        &top);
65
66         virtual void analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
67                                   TrajectoryAnalysisModuleData *pdata);
68
69         virtual void finishAnalysis(int nframes);
70         virtual void writeOutput();
71
72     private:
73         class ModuleData;
74
75         std::string                      fnDist_;
76         double                           cutoff_;
77         Selection                        refsel_;
78
79         AnalysisNeighborhood             nb_;
80
81         AnalysisData                     data_;
82         AnalysisDataAverageModulePointer avem_;
83         
84         
85         Selection                                           selec;
86         std::vector< std::pair< int, int > >                fitting_pairs;
87         std::vector< std::vector < RVec > >                 trajectory;
88         std::vector< std::vector < RVec > >                 fitting_temp;
89         std::vector< long double >                          noise;
90         std::vector< int >                                  index;
91         long double                                         epsi = 0.15;
92         long double                                         fitting_prec = 0.0001;
93         int                                                 frames = 0;
94         int                                                 iter_count = 0;
95         
96         
97 };
98
99
100 AnalysisTemplate::AnalysisTemplate()
101     : cutoff_(0.0)
102 {
103     registerAnalysisDataset(&data_, "avedist");
104 }
105
106
107 void
108 AnalysisTemplate::initOptions(IOptionsContainer          *options,
109                               TrajectoryAnalysisSettings *settings)
110 {
111     static const char *const desc[] = {
112         "This is a template for writing your own analysis tools for",
113         "GROMACS. The advantage of using GROMACS for this is that you",
114         "have access to all information in the topology, and your",
115         "program will be able to handle all types of coordinates and",
116         "trajectory files supported by GROMACS. In addition,",
117         "you get a lot of functionality for free from the trajectory",
118         "analysis library, including support for flexible dynamic",
119         "selections. Go ahead an try it![PAR]",
120         "To get started with implementing your own analysis program,",
121         "follow the instructions in the README file provided.",
122         "This template implements a simple analysis programs that calculates",
123         "average distances from a reference group to one or more",
124         "analysis groups."
125     };
126
127     settings->setHelpText(desc);
128
129     /*options->addOption(FileNameOption("o")
130                            .filetype(eftPlot).outputFile()
131                            .store(&fnDist_).defaultBasename("avedist")
132                            .description("Average distances from reference group"));
133
134     options->addOption(SelectionOption("reference")
135                            .store(&refsel_).required()
136                            .description("Reference group to calculate distances from"));*/
137     options->addOption(SelectionOption("select")
138                            .store(&selec).required()
139                            .description("Atoms that are considered as part of the excluded volume"));
140
141    /* options->addOption(DoubleOption("cutoff").store(&cutoff_)
142                            .description("Cutoff for distance calculation (0 = no cutoff)"));
143
144     settings->setFlag(TrajectoryAnalysisSettings::efRequireTop);*/
145 }
146
147 // -s '/home/toluk/Рабочий стол/reca_rd_2008/reca_rd.md.non-sol.tpr' -f '/home/toluk/Рабочий стол/reca_rd_2008/reca_rd.md.non-sol.xtc' -select 'name CA' -dt 500
148 // -s '/home/toluk/Data/dusc_trna/EcDusC_tRNA_FMN.md_npt.non-sol.tpr' -f '/home/toluk/Data/dusc_trna/EcDusC_tRNA_FMN.md_npt.non-sol.xtc' -select 'name CA' -dt 500
149
150 void
151 AnalysisTemplate::initAnalysis(const TrajectoryAnalysisSettings &settings,
152                                const TopologyInformation         & /*top*/)
153 {
154     //std::cout << "select start\n";
155     index.resize(0);
156     ConstArrayRef< int > atomind = selec.atomIndices();
157     for (ConstArrayRef< int >::iterator ai = atomind.begin(); (ai < atomind.end()); ai++) {
158         index.push_back(*ai);
159     }
160     //std::cout << "select finish\n";
161 }
162
163
164 void
165 AnalysisTemplate::analyzeFrame(int frnr, const t_trxframe &fr, t_pbc *pbc,
166                                TrajectoryAnalysisModuleData *pdata)
167 {
168     //std::cout << "trajectory start\n";
169     trajectory.resize(frames + 1);
170     trajectory[frames].resize(index.size());
171     for (int i = 0; i < index.size(); i++) {
172         trajectory[frames][i] = fr.x[index[i]];
173     }
174     frames++;
175     //std::cout << "trajectory finish\n";
176 }
177
178
179 void
180 AnalysisTemplate::finishAnalysis(int /*nframes*/)
181 {
182     std::cout << "analys start\n";
183     //std::cout << "\n";
184     fitting_pairs.resize(0);
185     for (int i = 0; i < index.size(); i++) {
186         fitting_pairs.push_back(std::make_pair (i, i));
187     }
188     //std::cout << "001\n";
189     long double noise_mid = 1;
190     bool flag = true;
191     std::vector< std::pair< int, int > > get_out;
192     //freopen("/home/toluk/Data/dusc_trna/output.txt", "w+", stdout);
193
194     /*
195
196     while (flag) {
197         std::cout << "start iteration\n";
198         noise.resize(index.size(), 0);
199         fitting_temp = trajectory;
200
201         for (int i = 1; i < frames; i++) {
202             new_fit(trajectory[0], fitting_temp[i], fitting_pairs, index.size(), fitting_prec);
203             if (i % 50 == 0) {
204                 std::cout << i << " / " << frames << "\n";
205             }
206             for (int j = 0; j < fitting_pairs.size(); j++) {
207                 noise[fitting_pairs[j].first] += norm(trajectory[0][fitting_pairs[j].first] - fitting_temp[i][fitting_pairs[j].first]);
208             }
209         }
210
211         flag = noise_mid;
212         noise_mid = 0;
213         for (int i = 0; i < index.size(); i++) {
214             noise[fitting_pairs[i].first] /= (frames - 1);
215             noise_mid += noise[fitting_pairs[i].first];
216         }
217         noise_mid /= fitting_pairs.size();
218
219         get_out = fitting_pairs;
220         fitting_pairs.resize(0);
221         for (int i = 0; i < index.size(); i++) {
222             fitting_pairs.push_back(std::make_pair (i, i));
223         }
224
225         for (int i = 0; i < fitting_pairs.size(); i++) {
226             if (noise[fitting_pairs[i].first] > noise_mid) {
227                 fitting_pairs.erase(fitting_pairs.begin() + i);
228                 i--;
229             }
230         }
231
232         if (get_out == fitting_pairs) {
233             flag = false;
234         }
235
236         std::cout << noise_mid << " | " << fitting_pairs.size() << "\n";
237         for (int i = 0; i < fitting_pairs.size(); i++) {
238             std::cout << fitting_pairs[i].first << " ";
239         }
240         std::cout << "\n\n";
241         for (int i = 0; i < get_out.size(); i++) {
242             std::cout << get_out[i].first << " ";
243         }
244         for (int i = 0; i < fitting_pairs.size(); i++) {
245             std::cout << noise[get_out[i].first] << " ";
246         }
247         std::cout << "\n\n";
248         std::cout << "finish iteration\n";
249         iter_count++;
250     }
251
252     */
253
254     noise_mid = 9999;
255
256     while (noise_mid > epsi) {
257         std::cout << "start iteration\n";
258         noise.resize(index.size(), 0);
259         fitting_temp = trajectory;
260
261         for (int i = 1; i < frames; i++) {
262             new_fit(trajectory[0], fitting_temp[i], fitting_pairs, index.size(), fitting_prec);
263             if (i % 50 == 0) {
264                 std::cout << i << " / " << frames << "\n";
265             }
266             for (int j = 0; j < index.size(); j++) {
267                 noise[j] += norm(trajectory[0][j] - fitting_temp[i][j]);
268             }
269         }
270
271         noise_mid = 0;
272         for (int i = 0; i < index.size(); i++) {
273             noise[i] /= (frames - 1);
274         }
275         for (int i = 0; i < fitting_pairs.size(); i++) {
276             noise_mid += noise[fitting_pairs[i].first];
277         }
278
279         noise_mid /= fitting_pairs.size();
280
281         for (int i = 0; i < fitting_pairs.size(); i++) {
282             if (noise[fitting_pairs[i].first] > noise_mid) {
283                 fitting_pairs.erase(fitting_pairs.begin() + i);
284                 i--;
285             }
286         }
287
288         std::cout << noise_mid << " | " << fitting_pairs.size() << "\n";
289         std::cout << "finish iteration\n";
290         iter_count++;
291     }
292
293     fitting_pairs.resize(0);
294     for (int i = 0; i < noise.size(); i++) {
295         if (noise[i] <= epsi) {
296             fitting_pairs.push_back(std::make_pair (i, i));
297         }
298     }
299
300
301
302     std::cout << "analys finish\n";
303 }
304
305
306 void
307 AnalysisTemplate::writeOutput()
308 {
309     std::cout << "output start\n";
310     std::cout << "number of iterations: " << iter_count << "\n";
311     for (int i = 0; i < fitting_pairs.size(); i++) {
312         std::cout << fitting_pairs[i].first << " ";
313     }
314     std::cout << "\n";
315     for (int i = 0; i < fitting_pairs.size(); i++) {
316         std::cout << noise[fitting_pairs[i].first] << " ";
317     }
318     std::cout << "output finish\n";
319 }
320
321 /*! \brief
322  * The main function for the analysis template.
323  */
324 int
325 main(int argc, char *argv[])
326 {
327     return gmx::TrajectoryAnalysisCommandLineRunner::runAsMain<AnalysisTemplate>(argc, argv);
328 }