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 "BasisFunctions.h"
24 : #include "TargetDistribution.h"
25 :
26 : #include "CoeffsVector.h"
27 : #include "VesTools.h"
28 :
29 : #include "core/ActionRegister.h"
30 : #include "core/ActionSet.h"
31 : #include "core/PlumedMain.h"
32 : #include "core/Value.h"
33 : #include "tools/File.h"
34 : #include "tools/Grid.h"
35 :
36 :
37 : namespace PLMD {
38 : namespace ves {
39 :
40 : //+PLUMEDOC VES_UTILS VES_OUTPUT_BASISFUNCTIONS
41 : /*
42 : Output basis functions to file.
43 :
44 : This action can be used to write out to a grid file the values and derivatives of
45 : given basis functions. This is normally used for debugging when programming new
46 : types of basis functions. For example, it is possible to calculate the
47 : derivatives numerically and compare to the analytically calculated
48 : derivatives.
49 :
50 : This action is normally used through the \ref driver.
51 :
52 : \par Examples
53 :
54 : In the following input we define a Legendre polynomials basis functions
55 : of order 14 over the interval -4.0 to 4.0 and output their values
56 : and derivatives to files called bfL.values.data and bfL.derivs.data.
57 : \plumedfile
58 : BF_LEGENDRE ...
59 : ORDER=14
60 : MINIMUM=-4.0
61 : MAXIMUM=4.0
62 : LABEL=bfL
63 : ... BF_LEGENDRE
64 :
65 : VES_OUTPUT_BASISFUNCTIONS ...
66 : BASIS_FUNCTIONS=bfL
67 : GRID_BINS=200
68 : FORMAT_VALUES_DERIVS=%13.6f
69 : ... VES_OUTPUT_BASISFUNCTIONS
70 : \endplumedfile
71 :
72 : This input should be run through the driver by using a command similar to the
73 : following one where the trajectory/configuration file configuration.gro is needed to
74 : trick the code to exit correctly.
75 : \verbatim
76 : plumed driver --plumed plumed.dat --igro configuration.gro
77 : \endverbatim
78 :
79 : */
80 : //+ENDPLUMEDOC
81 :
82 :
83 177 : class OutputBasisFunctions :
84 : public Action
85 : {
86 : std::vector<BasisFunctions*> bf_pntrs;
87 : public:
88 : explicit OutputBasisFunctions(const ActionOptions&);
89 : TargetDistribution* setupTargetDistPntr(std::string keyword) const;
90 0 : void calculate() {}
91 0 : void apply() {}
92 : static void registerKeywords(Keywords& keys);
93 : };
94 :
95 :
96 7474 : PLUMED_REGISTER_ACTION(OutputBasisFunctions,"VES_OUTPUT_BASISFUNCTIONS")
97 :
98 60 : void OutputBasisFunctions::registerKeywords(Keywords& keys) {
99 60 : Action::registerKeywords(keys);
100 240 : keys.add("compulsory","BASIS_FUNCTIONS","the label of the basis functions that you want to use");
101 240 : keys.add("optional","GRID_BINS","the number of bins used for the grid for writing the basis function values and derivatives. The default value is 1000.");
102 240 : keys.add("optional","GRID_MIN","the minimum of the grid for writing the basis function values and derivatives. By default it is the minimum of the interval on which the basis functions are defined.");
103 240 : keys.add("optional","GRID_MAX","the maximum of the grid for writing the basis function values and derivatives. By default it is the maximum of the interval on which the basis functions are defined.");
104 240 : keys.add("optional","FILE_VALUES","filename of the file on which the basis function values are written. By default it is BF_LABEL.values.data.");
105 240 : keys.add("optional","FILE_DERIVS","filename of the file on which the basis function derivatives are written. By default it is BF_LABEL.derivs.data.");
106 240 : keys.add("optional","FORMAT_VALUES_DERIVS","the numerical format of the basis function values and derivatives written to file. By default it is %15.8f.\n");
107 240 : keys.add("optional","FILE_TARGETDIST_AVERAGES","filename of the file on which the averages over the target distributions are written. By default it is BF_LABEL.targetdist-averages.data.");
108 240 : keys.add("optional","FORMAT_TARGETDIST_AVERAGES","the numerical format of the target distribution averages written to file. By default it is %15.8f.\n");
109 240 : keys.add("optional","FILE_TARGETDIST","filename of the files on which the target distributions are written. By default it is BF_LABEL.targetdist-#.data.");
110 240 : keys.add("numbered","TARGET_DISTRIBUTION","the target distribution to be used.");
111 180 : keys.addFlag("IGNORE_PERIODICITY",false,"if the periodicity of the basis functions should be ignored.");
112 180 : keys.addFlag("NUMERICAL_DERIVATIVES",false,"if the derivatives of the basis functions should be calculated numerically.");
113 60 : }
114 :
115 59 : OutputBasisFunctions::OutputBasisFunctions(const ActionOptions&ao):
116 : Action(ao),
117 59 : bf_pntrs(0)
118 : {
119 118 : std::vector<std::string> basisset_labels(0);
120 118 : parseVector("BASIS_FUNCTIONS",basisset_labels);
121 59 : if(basisset_labels.size()>1) {plumed_merror("Only one basis set label allowed in keyword BASIS_FUNCTIONS of "+getName());}
122 :
123 59 : std::string error_msg = "";
124 177 : bf_pntrs = VesTools::getPointersFromLabels<BasisFunctions*>(basisset_labels,plumed.getActionSet(),error_msg);
125 59 : if(error_msg.size()>0) {plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);}
126 :
127 59 : unsigned int nbins = 1000;
128 118 : parse("GRID_BINS",nbins);
129 :
130 59 : std::string min_str = bf_pntrs[0]->intervalMinStr();
131 59 : std::string max_str = bf_pntrs[0]->intervalMaxStr();
132 118 : parse("GRID_MIN",min_str);
133 118 : parse("GRID_MAX",max_str);
134 :
135 118 : std::string fname_values = bf_pntrs[0]->getLabel()+".values.data";
136 118 : parse("FILE_VALUES",fname_values);
137 118 : std::string fname_derives = bf_pntrs[0]->getLabel()+".derivs.data";
138 118 : parse("FILE_DERIVS",fname_derives);
139 118 : std::string fname_targetdist_aver = bf_pntrs[0]->getLabel()+".targetdist-averages.data";
140 118 : parse("FILE_TARGETDIST_AVERAGES",fname_targetdist_aver);
141 118 : std::string fname_targetdist = bf_pntrs[0]->getLabel()+".targetdist-.data";
142 118 : parse("FILE_TARGETDIST",fname_targetdist);
143 :
144 59 : std::string fmt_values_derivs = "%15.8f";
145 118 : parse("FORMAT_VALUES_DERIVS",fmt_values_derivs);
146 59 : std::string fmt_targetdist_aver = "%15.8f";
147 118 : parse("FORMAT_TARGETDIST_AVERAGES",fmt_targetdist_aver);
148 :
149 59 : bool ignore_periodicity = false;
150 118 : parseFlag("IGNORE_PERIODICITY",ignore_periodicity);
151 :
152 59 : bool numerical_deriv = false;
153 118 : parseFlag("NUMERICAL_DERIVATIVES",numerical_deriv);
154 :
155 : std::vector<TargetDistribution*> targetdist_pntrs;
156 118 : targetdist_pntrs.push_back(NULL);
157 59 : std::string targetdist_label="";
158 116 : for(int i=1;; i++) {
159 350 : if(!parseNumbered("TARGET_DISTRIBUTION",i,targetdist_label)) {break;}
160 116 : std::string error_msg = "";
161 232 : TargetDistribution* pntr_tmp = VesTools::getPointerFromLabel<TargetDistribution*>(targetdist_label,plumed.getActionSet(),error_msg);
162 116 : if(error_msg.size()>0) {plumed_merror("Error in keyword TARGET_DISTRIBUTION of "+getName()+": "+error_msg);}
163 116 : targetdist_pntrs.push_back(pntr_tmp);
164 116 : }
165 59 : checkRead();
166 : //
167 118 : OFile ofile_values;
168 59 : ofile_values.link(*this);
169 59 : ofile_values.enforceBackup();
170 59 : ofile_values.open(fname_values);
171 118 : OFile ofile_derivs;
172 59 : ofile_derivs.link(*this);
173 59 : ofile_derivs.enforceBackup();
174 59 : ofile_derivs.open(fname_derives);
175 59 : bf_pntrs[0]->writeBasisFunctionsToFile(ofile_values,ofile_derivs,min_str,max_str,nbins,ignore_periodicity,fmt_values_derivs,numerical_deriv);
176 59 : ofile_values.close();
177 59 : ofile_derivs.close();
178 : //
179 295 : std::vector<std::string> grid_min(1); grid_min[0]=bf_pntrs[0]->intervalMinStr();
180 295 : std::vector<std::string> grid_max(1); grid_max[0]=bf_pntrs[0]->intervalMaxStr();
181 118 : std::vector<unsigned int> grid_bins(1); grid_bins[0]=nbins;
182 59 : std::vector<Value*> arguments(1);
183 177 : arguments[0]= new Value(NULL,"arg",false);
184 118 : if(bf_pntrs[0]->arePeriodic() && !ignore_periodicity) {
185 90 : arguments[0]->setDomain(bf_pntrs[0]->intervalMinStr(),bf_pntrs[0]->intervalMaxStr());
186 : }
187 : else {
188 41 : arguments[0]->setNotPeriodic();
189 : }
190 :
191 118 : OFile ofile_targetdist_aver;
192 59 : ofile_targetdist_aver.link(*this);
193 59 : ofile_targetdist_aver.enforceBackup();
194 59 : ofile_targetdist_aver.open(fname_targetdist_aver);
195 :
196 643 : for(unsigned int i=0; i<targetdist_pntrs.size(); i++) {
197 175 : std::string is; Tools::convert(i,is);
198 : //
199 175 : if(targetdist_pntrs[i]!=NULL) {
200 116 : targetdist_pntrs[i]->setupGrids(arguments,grid_min,grid_max,grid_bins);
201 232 : plumed_massert(targetdist_pntrs[i]->getDimension()==1,"the target distribution must be one dimensional");
202 116 : targetdist_pntrs[i]->updateTargetDist();
203 : }
204 : //
205 350 : std::vector<double> bf_integrals = bf_pntrs[0]->getTargetDistributionIntegrals(targetdist_pntrs[i]);
206 525 : CoeffsVector targetdist_averages = CoeffsVector("aver.targetdist-"+is,arguments,bf_pntrs,comm,false);
207 175 : targetdist_averages.setValues(bf_integrals);
208 175 : if(fmt_targetdist_aver.size()>0) {targetdist_averages.setOutputFmt(fmt_targetdist_aver);}
209 175 : targetdist_averages.writeToFile(ofile_targetdist_aver,true);
210 175 : if(targetdist_pntrs[i]!=NULL) {
211 : Grid* targetdist_grid_pntr = targetdist_pntrs[i]->getTargetDistGridPntr();
212 116 : std::string fname = FileBase::appendSuffix(fname_targetdist,is);
213 232 : OFile ofile;
214 116 : ofile.link(*this);
215 116 : ofile.enforceBackup();
216 116 : ofile.open(fname);
217 116 : targetdist_grid_pntr->writeToFile(ofile);
218 116 : ofile.close();
219 : }
220 : }
221 59 : ofile_targetdist_aver.close();
222 59 : delete arguments[0]; arguments.clear();
223 :
224 :
225 :
226 59 : }
227 :
228 :
229 :
230 : }
231 5517 : }
|