Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2014-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 "MetainferenceBase.h"
23 : #include "core/ActionRegister.h"
24 : #include "tools/NeighborList.h"
25 : #include "tools/Pbc.h"
26 : #include <memory>
27 :
28 : namespace PLMD {
29 : namespace isdb {
30 :
31 : //+PLUMEDOC ISDB_COLVAR NOE
32 : /*
33 : Calculates NOE intensities as sums of 1/r^6, also averaging over multiple equivalent atoms
34 : or ambiguous NOE.
35 :
36 : Each NOE is defined by two groups containing the same number of atoms, distances are
37 : calculated in pairs, transformed in 1/r^6, summed and saved as components.
38 :
39 : \f[
40 : NOE() = (\frac{1}{N_{eq}}\sum_j^{N_{eq}} (\frac{1}{r_j^6}))
41 : \f]
42 :
43 : NOE can be used to calculate a Metainference score over one or more replicas using the intrinsic implementation
44 : of \ref METAINFERENCE that is activated by DOSCORE.
45 :
46 : \par Examples
47 : In the following examples three noes are defined, the first is calculated based on the distances
48 : of atom 1-2 and 3-2; the second is defined by the distance 5-7 and the third by the distances
49 : 4-15,4-16,8-15,8-16. \ref METAINFERENCE is activated using DOSCORE.
50 :
51 : \plumedfile
52 : NOE ...
53 : GROUPA1=1,3 GROUPB1=2,2 NOEDIST1=0.6
54 : GROUPA2=5 GROUPB2=7 NOEDIST2=0.6
55 : GROUPA3=4,4,8,8 GROUPB3=15,16,15,16 NOEDIST3=0.6
56 : DOSCORE
57 : SIGMA_MEAN0=1
58 : LABEL=noes
59 : ... NOE
60 :
61 : PRINT ARG=noes.* FILE=colvar
62 : \endplumedfile
63 :
64 : */
65 : //+ENDPLUMEDOC
66 :
67 : class NOE :
68 : public MetainferenceBase
69 : {
70 : private:
71 : bool pbc;
72 : std::vector<unsigned> nga;
73 : std::unique_ptr<NeighborList> nl;
74 : unsigned tot_size;
75 : public:
76 : static void registerKeywords( Keywords& keys );
77 : explicit NOE(const ActionOptions&);
78 : void calculate() override;
79 : void update() override;
80 : };
81 :
82 : PLUMED_REGISTER_ACTION(NOE,"NOE")
83 :
84 13 : void NOE::registerKeywords( Keywords& keys ) {
85 13 : MetainferenceBase::registerKeywords(keys);
86 26 : keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
87 26 : keys.add("numbered","GROUPA","the atoms involved in each of the contacts you wish to calculate. "
88 : "Keywords like GROUPA1, GROUPA2, GROUPA3,... should be listed and one contact will be "
89 : "calculated for each ATOM keyword you specify.");
90 26 : keys.add("numbered","GROUPB","the atoms involved in each of the contacts you wish to calculate. "
91 : "Keywords like GROUPB1, GROUPB2, GROUPB3,... should be listed and one contact will be "
92 : "calculated for each ATOM keyword you specify.");
93 26 : keys.reset_style("GROUPA","atoms");
94 26 : keys.reset_style("GROUPB","atoms");
95 26 : keys.add("numbered","NOEDIST","Add an experimental value for each NOE.");
96 26 : keys.addOutputComponent("noe","default","scalar","the # NOE");
97 26 : keys.addOutputComponent("exp","NOEDIST","scalar","the # NOE experimental distance");
98 13 : }
99 :
100 11 : NOE::NOE(const ActionOptions&ao):
101 : PLUMED_METAINF_INIT(ao),
102 11 : pbc(true)
103 : {
104 11 : bool nopbc=!pbc;
105 11 : parseFlag("NOPBC",nopbc);
106 11 : pbc=!nopbc;
107 :
108 : // Read in the atoms
109 : std::vector<AtomNumber> t, ga_lista, gb_lista;
110 22 : for(int i=1;; ++i ) {
111 66 : parseAtomList("GROUPA", i, t );
112 33 : if( t.empty() ) break;
113 55 : for(unsigned j=0; j<t.size(); j++) ga_lista.push_back(t[j]);
114 22 : nga.push_back(t.size());
115 22 : t.resize(0);
116 22 : }
117 : std::vector<unsigned> ngb;
118 22 : for(int i=1;; ++i ) {
119 66 : parseAtomList("GROUPB", i, t );
120 33 : if( t.empty() ) break;
121 55 : for(unsigned j=0; j<t.size(); j++) gb_lista.push_back(t[j]);
122 22 : ngb.push_back(t.size());
123 22 : if(ngb[i-1]!=nga[i-1]) error("The same number of atoms is expected for the same GROUPA-GROUPB couple");
124 22 : t.resize(0);
125 22 : }
126 11 : if(nga.size()!=ngb.size()) error("There should be the same number of GROUPA and GROUPB keywords");
127 : // Create neighbour lists
128 22 : nl=Tools::make_unique<NeighborList>(ga_lista,gb_lista,false,true,pbc,getPbc(),comm);
129 :
130 : // Optionally add an experimental value (like with RDCs)
131 : std::vector<double> noedist;
132 11 : noedist.resize( nga.size() );
133 : unsigned ntarget=0;
134 29 : for(unsigned i=0; i<nga.size(); ++i) {
135 40 : if( !parseNumbered( "NOEDIST", i+1, noedist[i] ) ) break;
136 18 : ntarget++;
137 : }
138 : bool addexp=false;
139 11 : if(ntarget!=nga.size() && ntarget!=0) error("found wrong number of NOEDIST values");
140 11 : if(ntarget==nga.size()) addexp=true;
141 11 : if(getDoScore()&&!addexp) error("with DOSCORE you need to set the NOEDIST values");
142 :
143 : // Output details of all contacts
144 : unsigned index=0;
145 33 : for(unsigned i=0; i<nga.size(); ++i) {
146 22 : log.printf(" The %uth NOE is calculated using %u equivalent couples of atoms\n", i, nga[i]);
147 55 : for(unsigned j=0; j<nga[i]; j++) {
148 33 : log.printf(" couple %u is %d %d.\n", j, ga_lista[index].serial(), gb_lista[index].serial() );
149 33 : index++;
150 : }
151 : }
152 11 : tot_size = index;
153 :
154 11 : if(pbc) log.printf(" using periodic boundary conditions\n");
155 0 : else log.printf(" without periodic boundary conditions\n");
156 :
157 22 : log << " Bibliography" << plumed.cite("Bonomi, Camilloni, Bioinformatics, 33, 3999 (2017)") << "\n";
158 :
159 11 : if(!getDoScore()) {
160 21 : for(unsigned i=0; i<nga.size(); i++) {
161 14 : std::string num; Tools::convert(i,num);
162 28 : addComponentWithDerivatives("noe-"+num);
163 28 : componentIsNotPeriodic("noe-"+num);
164 : }
165 7 : if(addexp) {
166 15 : for(unsigned i=0; i<nga.size(); i++) {
167 10 : std::string num; Tools::convert(i,num);
168 20 : addComponent("exp-"+num);
169 10 : componentIsNotPeriodic("exp-"+num);
170 10 : Value* comp=getPntrToComponent("exp-"+num);
171 10 : comp->set(noedist[i]);
172 : }
173 : }
174 : } else {
175 12 : for(unsigned i=0; i<nga.size(); i++) {
176 8 : std::string num; Tools::convert(i,num);
177 16 : addComponent("noe-"+num);
178 16 : componentIsNotPeriodic("noe-"+num);
179 : }
180 12 : for(unsigned i=0; i<nga.size(); i++) {
181 8 : std::string num; Tools::convert(i,num);
182 16 : addComponent("exp-"+num);
183 8 : componentIsNotPeriodic("exp-"+num);
184 8 : Value* comp=getPntrToComponent("exp-"+num);
185 8 : comp->set(noedist[i]);
186 : }
187 : }
188 :
189 11 : requestAtoms(nl->getFullAtomList(), false);
190 11 : if(getDoScore()) {
191 4 : setParameters(noedist);
192 4 : Initialise(nga.size());
193 : }
194 11 : setDerivatives();
195 11 : checkRead();
196 11 : }
197 :
198 456 : void NOE::calculate()
199 : {
200 456 : const unsigned ngasz=nga.size();
201 456 : std::vector<Vector> deriv(tot_size, Vector{0,0,0});
202 :
203 456 : #pragma omp parallel for num_threads(OpenMP::getNumThreads())
204 : for(unsigned i=0; i<ngasz; i++) {
205 : Tensor dervir;
206 : double noe=0;
207 : unsigned index=0;
208 : for(unsigned k=0; k<i; k++) index+=nga[k];
209 : std::string num; Tools::convert(i,num);
210 : Value* val=getPntrToComponent("noe-"+num);
211 : // cycle over equivalent atoms
212 : for(unsigned j=0; j<nga[i]; j++) {
213 : const unsigned i0=nl->getClosePair(index+j).first;
214 : const unsigned i1=nl->getClosePair(index+j).second;
215 :
216 : Vector distance;
217 : if(pbc) distance=pbcDistance(getPosition(i0),getPosition(i1));
218 : else distance=delta(getPosition(i0),getPosition(i1));
219 :
220 : const double ir2=1./distance.modulo2();
221 : const double ir6=ir2*ir2*ir2;
222 : const double ir8=6*ir6*ir2;
223 :
224 : noe += ir6;
225 : deriv[index+j] = ir8*distance;
226 : if(!getDoScore()) {
227 : dervir += Tensor(distance, deriv[index+j]);
228 : setAtomsDerivatives(val, i0, deriv[index+j]);
229 : setAtomsDerivatives(val, i1, -deriv[index+j]);
230 : }
231 : }
232 : val->set(noe);
233 : if(!getDoScore()) {
234 : setBoxDerivatives(val, dervir);
235 : } else setCalcData(i, noe);
236 : }
237 :
238 456 : if(getDoScore()) {
239 : /* Metainference */
240 48 : Tensor dervir;
241 48 : double score = getScore();
242 48 : setScore(score);
243 :
244 : /* calculate final derivatives */
245 48 : Value* val=getPntrToComponent("score");
246 144 : for(unsigned i=0; i<ngasz; i++) {
247 : unsigned index=0;
248 144 : for(unsigned k=0; k<i; k++) index+=nga[k];
249 : // cycle over equivalent atoms
250 240 : for(unsigned j=0; j<nga[i]; j++) {
251 144 : const unsigned i0=nl->getClosePair(index+j).first;
252 144 : const unsigned i1=nl->getClosePair(index+j).second;
253 :
254 144 : Vector distance;
255 144 : if(pbc) distance=pbcDistance(getPosition(i0),getPosition(i1));
256 0 : else distance=delta(getPosition(i0),getPosition(i1));
257 :
258 144 : dervir += Tensor(distance,deriv[index+j]*getMetaDer(i));
259 144 : setAtomsDerivatives(val, i0, deriv[index+j]*getMetaDer(i));
260 144 : setAtomsDerivatives(val, i1, -deriv[index+j]*getMetaDer(i));
261 : }
262 : }
263 48 : setBoxDerivatives(val, dervir);
264 : }
265 456 : }
266 :
267 132 : void NOE::update() {
268 : // write status file
269 132 : if(getWstride()>0&& (getStep()%getWstride()==0 || getCPT()) ) writeStatus();
270 132 : }
271 :
272 : }
273 : }
|