Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2018 The VES code team
3 : (see the PEOPLE-VES file at the root of this folder for a list of names)
4 :
5 : See http://www.ves-code.org for more information.
6 :
7 : This file is part of VES code module.
8 :
9 : The VES code module is free software: you can redistribute it and/or modify
10 : it under the terms of the GNU Lesser General Public License as published by
11 : the Free Software Foundation, either version 3 of the License, or
12 : (at your option) any later version.
13 :
14 : The VES code module 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
17 : GNU Lesser General Public License for more details.
18 :
19 : You should have received a copy of the GNU Lesser General Public License
20 : along with the VES code module. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 :
23 : #include "GridIntegrationWeights.h"
24 :
25 : #include "tools/Grid.h"
26 : #include "tools/File.h"
27 : #include "tools/Exception.h"
28 :
29 :
30 : namespace PLMD {
31 : namespace ves {
32 :
33 1770 : std::vector<double> GridIntegrationWeights::getIntegrationWeights(const Grid* grid_pntr, const std::string& fname_weights_grid, const std::string& weights_type) {
34 1770 : std::vector<double> dx = grid_pntr->getDx();
35 1770 : std::vector<bool> isPeriodic = grid_pntr->getIsPeriodic();
36 1770 : std::vector<unsigned int> nbins = grid_pntr->getNbin();
37 1770 : std::vector<std::vector<double> > weights_perdim;
38 6270 : for(unsigned int k=0; k<grid_pntr->getDimension(); k++) {
39 : std::vector<double> weights_tmp;
40 2250 : if(weights_type=="trapezoidal") {
41 11250 : weights_tmp = getOneDimensionalTrapezoidalWeights(nbins[k],dx[k],isPeriodic[k]);
42 : }
43 : else {
44 0 : plumed_merror("getIntegrationWeights: unknown weight type, the available type is trapezoidal");
45 : }
46 2250 : weights_perdim.push_back(weights_tmp);
47 : }
48 :
49 1770 : std::vector<double> weights_vector(grid_pntr->getSize(),0.0);
50 10209406 : for(Grid::index_t l=0; l<grid_pntr->getSize(); l++) {
51 5103818 : std::vector<unsigned int> ind = grid_pntr->getIndices(l);
52 : double value = 1.0;
53 25061024 : for(unsigned int k=0; k<grid_pntr->getDimension(); k++) {
54 29935809 : value *= weights_perdim[k][ind[k]];
55 : }
56 5103818 : weights_vector[l] = value;
57 : }
58 :
59 1770 : if(fname_weights_grid.size()>0) {
60 0 : Grid weights_grid = Grid(*grid_pntr);
61 0 : for(Grid::index_t l=0; l<weights_grid.getSize(); l++) {
62 0 : weights_grid.setValue(l,weights_vector[l]);
63 : }
64 0 : OFile ofile;
65 0 : ofile.enforceBackup();
66 0 : ofile.open(fname_weights_grid);
67 0 : weights_grid.writeToFile(ofile);
68 0 : ofile.close();
69 : }
70 : //
71 1770 : return weights_vector;
72 : }
73 :
74 :
75 19 : void GridIntegrationWeights::getOneDimensionalIntegrationPointsAndWeights(std::vector<double>& points, std::vector<double>& weights, const unsigned int nbins, const double min, const double max, const std::string& weights_type) {
76 19 : double dx = (max-min)/(static_cast<double>(nbins)-1.0);
77 19 : points.resize(nbins);
78 38057 : for(unsigned int i=0; i<nbins; i++) {points[i] = min + i*dx;}
79 19 : if(weights_type=="trapezoidal") {
80 38 : weights = getOneDimensionalTrapezoidalWeights(nbins,dx,false);
81 : }
82 : else {
83 0 : plumed_merror("getOneDimensionalIntegrationWeights: unknown weight type, the available type is trapezoidal");
84 : }
85 19 : }
86 :
87 :
88 2269 : std::vector<double> GridIntegrationWeights::getOneDimensionalTrapezoidalWeights(const unsigned int nbins, const double dx, const bool periodic) {
89 2269 : std::vector<double> weights_1d(nbins);
90 682177 : for(unsigned int i=1; i<(nbins-1); i++) {
91 679908 : weights_1d[i] = dx;
92 : }
93 2269 : if(!periodic) {
94 954 : weights_1d[0]= 0.5*dx;
95 1908 : weights_1d[(nbins-1)]= 0.5*dx;
96 : }
97 : else {
98 : // as for periodic arguments the first point should be counted twice as the
99 : // grid doesn't include its periodic copy
100 1315 : weights_1d[0]= dx;
101 2630 : weights_1d[(nbins-1)]= dx;
102 : }
103 2269 : return weights_1d;
104 : }
105 :
106 :
107 : }
108 : }
|