Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2018-2023 The plumed team
3 : (see the PEOPLE file at the root of the distribution for a list of names)
4 :
5 : See http://www.plumed.org for more information.
6 :
7 : This file is part of plumed, version 2.
8 :
9 : plumed 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 : plumed 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 plumed. If not, see <http://www.gnu.org/licenses/>.
21 : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
22 : #include "core/ActionShortcut.h"
23 : #include "core/PlumedMain.h"
24 : #include "core/ActionSet.h"
25 : #include "core/ActionRegister.h"
26 :
27 : namespace PLMD {
28 : namespace wham {
29 :
30 : //+PLUMEDOC REWEIGHTING WHAM_HISTOGRAM
31 : /*
32 : This can be used to output the a histogram using the weighted histogram technique
33 :
34 : This shortcut action allows you to calculate a histogram using the weighted histogram analysis technique.
35 : The following input illustrates how this is used in practise to analyze the output from a series of umbrella sampling calculations.
36 : The trajectory from each of the simulations run with the different biases should be concatenated into a
37 : single trajectory before running the following analysis script on the concatenated trajectory using PLUMED
38 : driver. The umbrella sampling simulations that will be analyzed using the script below applied a harmonic
39 : restraint that restrained the torsional angle involving atoms 5, 7, 9 and 15 to particular values. The script
40 : below calculates the reweighting weights for each of the trajectories and then applies the binless WHAM algorithm
41 : to determine a weight for each configuration in the concatenated trajectory. A histogram is then constructed from
42 : the configurations visited and their weights. This histogram is then converted into a free energy surface and output
43 : to a file called fes.dat
44 :
45 : ```plumed
46 : #SETTINGS NREPLICAS=4
47 : phi: TORSION ATOMS=5,7,9,15
48 : psi: TORSION ATOMS=7,9,15,17
49 : rp: RESTRAINT ARG=phi KAPPA=50.0 AT=@replicas:{-3.00,-1.45,0.10,1.65}
50 : hh: WHAM_HISTOGRAM ARG=phi BIAS=rp.bias TEMP=300 GRID_MIN=-pi GRID_MAX=pi GRID_BIN=50
51 : fes: CONVERT_TO_FES ARG=hh TEMP=300
52 : DUMPGRID ARG=fes FILE=fes.dat
53 : ```
54 :
55 : The script above must be run with multiple replicas using the following command:
56 :
57 : ````
58 : mpirun -np 4 plumed driver --mf_xtc alltraj.xtc --multi 4
59 : ````
60 :
61 : Notice that if you use the BANDWIDTH keyword, as in the example below, PLUMED will estimate the histogram
62 : using [kernel density estimation](https://en.wikipedia.org/wiki/Kernel_density_estimation).
63 :
64 : ```plumed
65 : #SETTINGS NREPLICAS=4
66 : phi: TORSION ATOMS=5,7,9,15
67 : psi: TORSION ATOMS=7,9,15,17
68 : rp: RESTRAINT ARG=phi KAPPA=50.0 AT=@replicas:{-3.00,-1.45,0.10,1.65}
69 : hh: WHAM_HISTOGRAM ARG=phi BIAS=rp.bias TEMP=300 GRID_MIN=-pi GRID_MAX=pi GRID_BIN=50 BANDWIDTH=0.1
70 : fes: CONVERT_TO_FES ARG=hh TEMP=300
71 : DUMPGRID ARG=fes FILE=fes.dat
72 : ```
73 :
74 :
75 : For more details on how the weights for configurations are determined using the wham method see the documentation
76 : for the [WHAM](WHAM.md) action.
77 :
78 : */
79 : //+ENDPLUMEDOC
80 :
81 : class WhamHistogram : public ActionShortcut {
82 : public:
83 : static void registerKeywords( Keywords& keys );
84 : explicit WhamHistogram( const ActionOptions& );
85 : };
86 :
87 : PLUMED_REGISTER_ACTION(WhamHistogram,"WHAM_HISTOGRAM")
88 :
89 10 : void WhamHistogram::registerKeywords( Keywords& keys ) {
90 10 : ActionShortcut::registerKeywords( keys );
91 10 : keys.add("compulsory","ARG","the arguments that you would like to make the histogram for");
92 10 : keys.add("compulsory","BIAS","*.bias","the value of the biases to use when performing WHAM");
93 10 : keys.add("compulsory","TEMP","the temperature at which the simulation was run");
94 10 : keys.add("compulsory","STRIDE","1","the frequency with which the data should be stored to perform WHAM");
95 10 : keys.add("compulsory","GRID_MIN","the minimum to use for the grid");
96 10 : keys.add("compulsory","GRID_MAX","the maximum to use for the grid");
97 10 : keys.add("compulsory","GRID_BIN","the number of bins to use for the grid");
98 10 : keys.add("optional","BANDWIDTH","the bandwidth for kernel density estimation");
99 20 : keys.setValueDescription("grid","the histogram that was generated using the WHAM weights");
100 10 : keys.needsAction("GATHER_REPLICAS");
101 10 : keys.needsAction("CONCATENATE");
102 10 : keys.needsAction("COLLECT");
103 10 : keys.needsAction("WHAM");
104 10 : keys.needsAction("KDE");
105 10 : }
106 :
107 :
108 7 : WhamHistogram::WhamHistogram( const ActionOptions& ao ) :
109 : Action(ao),
110 7 : ActionShortcut(ao) {
111 : // Input for collection of weights for WHAM
112 : std::string bias;
113 14 : parse("BIAS",bias);
114 : std::string stride;
115 7 : parse("STRIDE",stride);
116 : // Input for GATHER_REPLICAS
117 14 : readInputLine( getShortcutLabel() + "_gather: GATHER_REPLICAS ARG=" + bias );
118 : // Put all the replicas in a single vector
119 14 : readInputLine( getShortcutLabel() + "_gatherv: CONCATENATE ARG=" + getShortcutLabel() + "_gather.*");
120 : // Input for COLLECT_FRAMES
121 14 : readInputLine( getShortcutLabel() + "_collect: COLLECT TYPE=vector ARG=" + getShortcutLabel() + "_gatherv STRIDE=" + stride);
122 : // Input for WHAM
123 7 : std::string temp, tempstr="";
124 14 : parse("TEMP",temp);
125 7 : if( temp.length()>0 ) {
126 14 : tempstr="TEMP=" + temp;
127 : }
128 14 : readInputLine( getShortcutLabel() + "_wham: WHAM ARG=" + getShortcutLabel() + "_collect " + tempstr );
129 : // Input for COLLECT_FRAMES
130 : std::vector<std::string> args;
131 14 : parseVector("ARG",args);
132 : std::string argstr;
133 14 : for(unsigned i=0; i<args.size(); ++i) {
134 14 : readInputLine( getShortcutLabel() + "_data_" + args[i] + ": COLLECT ARG=" + args[i] );
135 7 : if( i==0 ) {
136 : argstr = " ARG=";
137 : } else {
138 : argstr += ",";
139 : }
140 14 : argstr += getShortcutLabel() + "_data_" + args[i];
141 : }
142 : // Input for HISTOGRAM
143 7 : std::string histo_line, bw="";
144 14 : parse("BANDWIDTH",bw);
145 7 : if( bw!="" ) {
146 0 : histo_line += " BANDWIDTH=" + bw;
147 : } else {
148 : histo_line += " KERNEL=DISCRETE";
149 : }
150 : std::string min;
151 7 : parse("GRID_MIN",min);
152 14 : histo_line += " GRID_MIN=" + min;
153 : std::string max;
154 7 : parse("GRID_MAX",max);
155 14 : histo_line += " GRID_MAX=" + max;
156 : std::string bin;
157 7 : parse("GRID_BIN",bin);
158 7 : histo_line += " GRID_BIN=" + bin;
159 14 : readInputLine( getShortcutLabel() + ": KDE " + argstr + " HEIGHTS=" + getShortcutLabel() + "_wham" + histo_line );
160 14 : }
161 :
162 : }
163 : }
|