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