Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-2021 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 : 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() override {}
91 0 : void apply() override {}
92 : static void registerKeywords(Keywords& keys);
93 : };
94 :
95 :
96 10490 : PLUMED_REGISTER_ACTION(OutputBasisFunctions,"VES_OUTPUT_BASISFUNCTIONS")
97 :
98 72 : void OutputBasisFunctions::registerKeywords(Keywords& keys) {
99 72 : Action::registerKeywords(keys);
100 144 : keys.add("compulsory","BASIS_FUNCTIONS","the label of the basis functions that you want to use");
101 144 : 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 144 : 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 144 : 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 144 : 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 144 : 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 216 : 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. You can also use FORMAT_VALUES and FORMAT_DERIVS to give the numerical formats separately.");
107 144 : keys.add("optional","FORMAT_VALUES","the numerical format of the basis function derivatives written to file. By default it is %15.8f.\n");
108 144 : keys.add("optional","FORMAT_DERIVS","the numerical format of the basis function values written to file. By default it is %15.8f.\n");
109 216 : 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.");
110 216 : keys.add("optional","FORMAT_TARGETDIST_AVERAGES","the numerical format of the target distribution averages written to file. By default it is %15.8f.\n");
111 144 : keys.add("optional","FILE_TARGETDIST","filename of the files on which the target distributions are written. By default it is BF_LABEL.targetdist-#.data.");
112 216 : keys.add("numbered","TARGET_DISTRIBUTION","the target distribution to be used.");
113 144 : keys.addFlag("IGNORE_PERIODICITY",false,"if the periodicity of the basis functions should be ignored.");
114 144 : keys.addFlag("NUMERICAL_DERIVATIVES",false,"if the derivatives of the basis functions should be calculated numerically.");
115 72 : }
116 :
117 71 : OutputBasisFunctions::OutputBasisFunctions(const ActionOptions&ao):
118 : Action(ao),
119 71 : bf_pntrs(0)
120 : {
121 71 : std::vector<std::string> basisset_labels(0);
122 142 : parseVector("BASIS_FUNCTIONS",basisset_labels);
123 71 : if(basisset_labels.size()>1) {plumed_merror("Only one basis set label allowed in keyword BASIS_FUNCTIONS of "+getName());}
124 :
125 71 : std::string error_msg = "";
126 142 : bf_pntrs = VesTools::getPointersFromLabels<BasisFunctions*>(basisset_labels,plumed.getActionSet(),error_msg);
127 71 : if(error_msg.size()>0) {plumed_merror("Error in keyword BASIS_FUNCTIONS of "+getName()+": "+error_msg);}
128 :
129 71 : unsigned int nbins = 1000;
130 142 : parse("GRID_BINS",nbins);
131 :
132 71 : std::string min_str = bf_pntrs[0]->intervalMinStr();
133 71 : std::string max_str = bf_pntrs[0]->intervalMaxStr();
134 71 : parse("GRID_MIN",min_str);
135 142 : parse("GRID_MAX",max_str);
136 :
137 71 : std::string fname_values = bf_pntrs[0]->getLabel()+".values.data";
138 142 : parse("FILE_VALUES",fname_values);
139 71 : std::string fname_derives = bf_pntrs[0]->getLabel()+".derivs.data";
140 142 : parse("FILE_DERIVS",fname_derives);
141 71 : std::string fname_targetdist_aver = bf_pntrs[0]->getLabel()+".targetdist-averages.data";
142 142 : parse("FILE_TARGETDIST_AVERAGES",fname_targetdist_aver);
143 71 : std::string fname_targetdist = bf_pntrs[0]->getLabel()+".targetdist-.data";
144 71 : parse("FILE_TARGETDIST",fname_targetdist);
145 :
146 71 : std::string fmt_values_derivs = "%15.8f";
147 71 : parse("FORMAT_VALUES_DERIVS",fmt_values_derivs);
148 71 : std::string fmt_values = fmt_values_derivs;
149 71 : std::string fmt_derivs = fmt_values_derivs;
150 71 : parse("FORMAT_VALUES",fmt_values);
151 71 : parse("FORMAT_DERIVS",fmt_derivs);
152 :
153 71 : std::string fmt_targetdist_aver = "%15.8f";
154 71 : parse("FORMAT_TARGETDIST_AVERAGES",fmt_targetdist_aver);
155 :
156 71 : bool ignore_periodicity = false;
157 71 : parseFlag("IGNORE_PERIODICITY",ignore_periodicity);
158 :
159 71 : bool numerical_deriv = false;
160 142 : parseFlag("NUMERICAL_DERIVATIVES",numerical_deriv);
161 :
162 : std::vector<TargetDistribution*> targetdist_pntrs;
163 71 : targetdist_pntrs.push_back(NULL);
164 71 : std::string targetdist_label="";
165 176 : for(int i=1;; i++) {
166 494 : if(!parseNumbered("TARGET_DISTRIBUTION",i,targetdist_label)) {break;}
167 176 : std::string error_msg = "";
168 176 : TargetDistribution* pntr_tmp = VesTools::getPointerFromLabel<TargetDistribution*>(targetdist_label,plumed.getActionSet(),error_msg);
169 176 : if(error_msg.size()>0) {plumed_merror("Error in keyword TARGET_DISTRIBUTION of "+getName()+": "+error_msg);}
170 176 : targetdist_pntrs.push_back(pntr_tmp);
171 176 : }
172 71 : checkRead();
173 : //
174 71 : OFile ofile_values;
175 71 : ofile_values.link(*this);
176 71 : ofile_values.enforceBackup();
177 71 : ofile_values.open(fname_values);
178 71 : OFile ofile_derivs;
179 71 : ofile_derivs.link(*this);
180 71 : ofile_derivs.enforceBackup();
181 71 : ofile_derivs.open(fname_derives);
182 71 : bf_pntrs[0]->writeBasisFunctionsToFile(ofile_values,ofile_derivs,min_str,max_str,nbins,ignore_periodicity,fmt_values,fmt_derivs,numerical_deriv);
183 71 : ofile_values.close();
184 71 : ofile_derivs.close();
185 : //
186 71 : std::vector<std::string> grid_min(1); grid_min[0]=bf_pntrs[0]->intervalMinStr();
187 71 : std::vector<std::string> grid_max(1); grid_max[0]=bf_pntrs[0]->intervalMaxStr();
188 71 : std::vector<unsigned int> grid_bins(1); grid_bins[0]=nbins;
189 71 : std::vector<std::unique_ptr<Value>> arguments(1);
190 142 : arguments[0]= Tools::make_unique<Value>(nullptr,"arg",false);
191 71 : if(bf_pntrs[0]->arePeriodic() && !ignore_periodicity) {
192 46 : arguments[0]->setDomain(bf_pntrs[0]->intervalMinStr(),bf_pntrs[0]->intervalMaxStr());
193 : }
194 : else {
195 48 : arguments[0]->setNotPeriodic();
196 : }
197 :
198 71 : OFile ofile_targetdist_aver;
199 71 : ofile_targetdist_aver.link(*this);
200 71 : ofile_targetdist_aver.enforceBackup();
201 71 : ofile_targetdist_aver.open(fname_targetdist_aver);
202 :
203 318 : for(unsigned int i=0; i<targetdist_pntrs.size(); i++) {
204 247 : std::string is; Tools::convert(i,is);
205 : //
206 247 : if(targetdist_pntrs[i]!=NULL) {
207 352 : targetdist_pntrs[i]->setupGrids(Tools::unique2raw(arguments),grid_min,grid_max,grid_bins);
208 176 : plumed_massert(targetdist_pntrs[i]->getDimension()==1,"the target distribution must be one dimensional");
209 176 : targetdist_pntrs[i]->updateTargetDist();
210 : }
211 : //
212 247 : std::vector<double> bf_integrals = bf_pntrs[0]->getTargetDistributionIntegrals(targetdist_pntrs[i]);
213 494 : CoeffsVector targetdist_averages = CoeffsVector("aver.targetdist-"+is,Tools::unique2raw(arguments),bf_pntrs,comm,false);
214 247 : targetdist_averages.setValues(bf_integrals);
215 247 : if(fmt_targetdist_aver.size()>0) {targetdist_averages.setOutputFmt(fmt_targetdist_aver);}
216 247 : targetdist_averages.writeToFile(ofile_targetdist_aver,true);
217 247 : if(targetdist_pntrs[i]!=NULL) {
218 : Grid* targetdist_grid_pntr = targetdist_pntrs[i]->getTargetDistGridPntr();
219 176 : std::string fname = FileBase::appendSuffix(fname_targetdist,is);
220 176 : OFile ofile;
221 176 : ofile.link(*this);
222 176 : ofile.enforceBackup();
223 176 : ofile.open(fname);
224 176 : targetdist_grid_pntr->writeToFile(ofile);
225 176 : ofile.close();
226 176 : }
227 247 : }
228 71 : ofile_targetdist_aver.close();
229 :
230 :
231 213 : }
232 :
233 :
234 :
235 : }
236 : }
|