Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2013-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 "MultiColvarBase.h"
23 : #include "AtomValuePack.h"
24 : #include "tools/SwitchingFunction.h"
25 : #include "core/ActionRegister.h"
26 :
27 : #include <string>
28 : #include <cmath>
29 :
30 : namespace PLMD {
31 : namespace multicolvar {
32 :
33 : //+PLUMEDOC MCOLVAR BRIDGE
34 : /*
35 : Calculate the number of atoms that bridge two parts of a structure
36 :
37 : This quantity calculates:
38 :
39 : \f[
40 : f(x) = \sum_{ijk} s_A(r_{ij})s_B(r_{ik})
41 : \f]
42 :
43 : where the sum over \f$i\f$ is over all the ``bridging atoms" and
44 : \f$s_A\f$ and \f$s_B\f$ are \ref switchingfunction.
45 :
46 : \par Examples
47 :
48 : The following example instructs plumed to calculate the number of water molecules
49 : that are bridging between atoms 1-10 and atoms 11-20 and to print the value
50 : to a file
51 :
52 : \plumedfile
53 : w1: BRIDGE BRIDGING_ATOMS=100-200 GROUPA=1-10 GROUPB=11-20 SWITCH={RATIONAL R_0=0.2}
54 : PRINT ARG=w1 FILE=colvar
55 : \endplumedfile
56 :
57 : */
58 : //+ENDPLUMEDOC
59 :
60 : class Bridge : public MultiColvarBase {
61 : private:
62 : Vector dij, dik;
63 : SwitchingFunction sf1;
64 : SwitchingFunction sf2;
65 : public:
66 : static void registerKeywords( Keywords& keys );
67 : explicit Bridge(const ActionOptions&);
68 : // active methods:
69 : double compute( const unsigned& tindex, AtomValuePack& myatoms ) const override;
70 0 : bool isPeriodic() override { return false; }
71 : };
72 :
73 10435 : PLUMED_REGISTER_ACTION(Bridge,"BRIDGE")
74 :
75 9 : void Bridge::registerKeywords( Keywords& keys ) {
76 9 : MultiColvarBase::registerKeywords( keys );
77 18 : keys.add("atoms-2","BRIDGING_ATOMS","The list of atoms that can form the bridge between the two interesting parts "
78 : "of the structure.");
79 18 : keys.add("atoms-2","GROUPA","The list of atoms that are in the first interesting part of the structure");
80 18 : keys.add("atoms-2","GROUPB","The list of atoms that are in the second interesting part of the structure");
81 18 : keys.add("optional","SWITCH","The parameters of the two \\ref switchingfunction in the above formula");
82 18 : keys.add("optional","SWITCHA","The \\ref switchingfunction on the distance between bridging atoms and the atoms in "
83 : "group A");
84 18 : keys.add("optional","SWITCHB","The \\ref switchingfunction on the distance between the bridging atoms and the atoms in "
85 : "group B");
86 9 : }
87 :
88 8 : Bridge::Bridge(const ActionOptions&ao):
89 : Action(ao),
90 8 : MultiColvarBase(ao)
91 : {
92 : // Read in the atoms
93 : std::vector<AtomNumber> all_atoms;
94 16 : readThreeGroups("GROUPA","GROUPB","BRIDGING_ATOMS",false,true,all_atoms);
95 : // Setup the multicolvar base
96 8 : setupMultiColvarBase( all_atoms );
97 : // Setup Central atom atoms
98 8 : std::vector<bool> catom_ind(3, false); catom_ind[0]=true;
99 8 : setAtomsForCentralAtom( catom_ind );
100 :
101 16 : std::string sfinput,errors; parse("SWITCH",sfinput);
102 8 : if( sfinput.length()>0 ) {
103 8 : sf1.set(sfinput,errors);
104 8 : if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
105 8 : sf2.set(sfinput,errors);
106 8 : if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
107 : } else {
108 0 : parse("SWITCHA",sfinput);
109 0 : if(sfinput.length()>0) {
110 0 : weightHasDerivatives=true;
111 0 : sf1.set(sfinput,errors);
112 0 : if( errors.length()!=0 ) error("problem reading SWITCHA keyword : " + errors );
113 0 : sfinput.clear(); parse("SWITCHB",sfinput);
114 0 : if(sfinput.length()==0) error("found SWITCHA keyword without SWITCHB");
115 0 : sf2.set(sfinput,errors);
116 0 : if( errors.length()!=0 ) error("problem reading SWITCHB keyword : " + errors );
117 : } else {
118 0 : error("missing definition of switching functions");
119 : }
120 : }
121 8 : log.printf(" distance between bridging atoms and atoms in GROUPA must be less than %s\n",sf1.description().c_str());
122 8 : log.printf(" distance between bridging atoms and atoms in GROUPB must be less than %s\n",sf2.description().c_str());
123 :
124 : // Setup link cells
125 8 : setLinkCellCutoff( sf1.get_dmax() + sf2.get_dmax() );
126 :
127 : // And setup the ActionWithVessel
128 8 : if( getNumberOfVessels()!=0 ) error("should not have vessels for this action");
129 : std::string fake_input;
130 8 : addVessel( "SUM", fake_input, -1 ); // -1 here means that this value will be named getLabel()
131 8 : readVesselKeywords();
132 : // And check everything has been read in correctly
133 8 : checkRead();
134 8 : }
135 :
136 537 : double Bridge::compute( const unsigned& tindex, AtomValuePack& myatoms ) const {
137 : double tot=0;
138 21359 : for(unsigned i=2; i<myatoms.getNumberOfAtoms(); ++i) {
139 20822 : Vector dij=getSeparation( myatoms.getPosition(i), myatoms.getPosition(0) );
140 20822 : double dw1, w1=sf1.calculateSqr( dij.modulo2(), dw1 );
141 20822 : Vector dik=getSeparation( myatoms.getPosition(i), myatoms.getPosition(1) );
142 20822 : double dw2, w2=sf2.calculateSqr( dik.modulo2(), dw2 );
143 :
144 20822 : tot += w1*w2;
145 : // And finish the calculation
146 20822 : addAtomDerivatives( 1, 0, w2*dw1*dij, myatoms );
147 20822 : addAtomDerivatives( 1, 1, w1*dw2*dik, myatoms );
148 20822 : addAtomDerivatives( 1, i, -w1*dw2*dik-w2*dw1*dij, myatoms );
149 20822 : myatoms.addBoxDerivatives( 1, w1*(-dw2)*Tensor(dik,dik)+w2*(-dw1)*Tensor(dij,dij) );
150 : }
151 537 : return tot;
152 : }
153 :
154 : }
155 : }
|