Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2015-2019 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/ActionRegister.h"
23 : #include "tools/Pbc.h"
24 : #include "tools/SwitchingFunction.h"
25 : #include "ActionVolume.h"
26 :
27 : //+PLUMEDOC VOLUMES INCYLINDER
28 : /*
29 : This quantity can be used to calculate functions of the distribution of collective
30 : variables for the atoms that lie in a particular, user-specified part of of the cell.
31 :
32 : Each of the base quantities calculated by a multicolvar can can be assigned to a particular point in three
33 : dimensional space. For example, if we have the coordination numbers for all the atoms in the
34 : system each coordination number can be assumed to lie on the position of the central atom.
35 : Because each base quantity can be assigned to a particular point in space we can calculate functions of the
36 : distribution of base quantities in a particular part of the box by using:
37 :
38 : \f[
39 : \overline{s}_{\tau} = \frac{ \sum_i f(s_i) \sigma(r_{xy}) }{ \sum_i \sigma(r_{xy}) }
40 : \f]
41 :
42 : where the sum is over the collective variables, \f$s_i\f$, each of which can be thought to be at \f$ (x_i,y_i,z_i)\f$.
43 : The function \f$\sigma\f$ is a \ref switchingfunction that acts on the distance between the point at which the
44 : collective is located \f$(x_i,y_i,z_i)\f$ and the position of the atom that was specified using the ORIGIN keyword
45 : projected in the xy plane if DIRECTION=z is used. In other words:
46 : \f[
47 : r_{xy} = sqrt{ ( x_i - x_0)^2 + ( y_i - y_0)^2 }
48 : \f]
49 : In short this function, \f$\sigma(r_{xy})\f$, measures whether or not the CV is within a cylinder that
50 : runs along the axis specified using the DIRECTION keyword and that is centered on the position of the atom specified using
51 : ORIGIN.
52 :
53 : The function \f$(s_i)\f$ can be any of the usual LESS_THAN, MORE_THAN, WITHIN etc that are used in all other multicolvars.
54 :
55 : When INCYLINDER is used with the \ref DENSITY action the number of atoms in the specified region is calculated
56 :
57 : \par Examples
58 :
59 : The input below can be use to calculate the average coordination numbers for those atoms that are within a cylindrical tube
60 : of radius 1.5 nm that is centered on the position of atom 101 and that has its long axis parallel to the z-axis.
61 :
62 : \plumedfile
63 : c1: COORDINATIONNUMBER SPECIES=1-100 SWITCH={RATIONAL R_0=0.1}
64 : d2: INCYLINDER ATOM=101 DATA=d1 DIRECTION=Z RADIUS={TANH R_0=1.5} SIGMA=0.1 LOWER=-0.1 UPPER=0.1 MEAN
65 : PRINT ARG=d2.* FILE=colvar
66 : \endplumedfile
67 :
68 : */
69 : //+ENDPLUMEDOC
70 :
71 : namespace PLMD {
72 : namespace multicolvar {
73 :
74 3 : class VolumeInCylinder : public ActionVolume {
75 : private:
76 : bool docylinder;
77 : Vector origin;
78 : HistogramBead bead;
79 : std::vector<unsigned> dir;
80 : SwitchingFunction switchingFunction;
81 : public:
82 : static void registerKeywords( Keywords& keys );
83 : explicit VolumeInCylinder (const ActionOptions& ao);
84 : void setupRegions();
85 : double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const ;
86 : };
87 :
88 6453 : PLUMED_REGISTER_ACTION(VolumeInCylinder,"INCYLINDER")
89 :
90 2 : void VolumeInCylinder::registerKeywords( Keywords& keys ) {
91 2 : ActionVolume::registerKeywords( keys );
92 8 : keys.add("atoms","ATOM","the atom whose vicinity we are interested in examining");
93 8 : keys.add("compulsory","DIRECTION","the direction of the long axis of the cylinder. Must be x, y or z");
94 8 : keys.add("compulsory","RADIUS","a switching function that gives the extent of the cyclinder in the plane perpendicular to the direction");
95 10 : keys.add("compulsory","LOWER","0.0","the lower boundary on the direction parallel to the long axis of the cylinder");
96 10 : keys.add("compulsory","UPPER","0.0","the upper boundary on the direction parallel to the long axis of the cylinder");
97 6 : keys.reset_style("SIGMA","optional");
98 2 : }
99 :
100 1 : VolumeInCylinder::VolumeInCylinder(const ActionOptions& ao):
101 : Action(ao),
102 : ActionVolume(ao),
103 2 : docylinder(false)
104 : {
105 : std::vector<AtomNumber> atom;
106 2 : parseAtomList("ATOM",atom);
107 1 : if( atom.size()!=1 ) error("should only be one atom specified");
108 2 : log.printf(" center of cylinder is at position of atom : %d\n",atom[0].serial() );
109 :
110 2 : std::string sdir; parse("DIRECTION",sdir);
111 1 : if( sdir=="X") {dir.push_back(1); dir.push_back(2); dir.push_back(0); }
112 1 : else if( sdir=="Y") {dir.push_back(0); dir.push_back(2); dir.push_back(1); }
113 4 : else if( sdir=="Z") {dir.push_back(0); dir.push_back(1); dir.push_back(2); }
114 0 : else { error(sdir + "is not a valid direction. Should be X, Y or Z"); }
115 2 : log.printf(" cylinder's long axis is along %s axis\n",sdir.c_str() );
116 :
117 2 : std::string sw, errors; parse("RADIUS",sw);
118 1 : if(sw.length()==0) error("missing RADIUS keyword");
119 1 : switchingFunction.set(sw,errors);
120 1 : if( errors.length()!=0 ) error("problem reading RADIUS keyword : " + errors );
121 3 : log.printf(" radius of cylinder is given by %s \n", ( switchingFunction.description() ).c_str() );
122 :
123 3 : double min, max; parse("LOWER",min); parse("UPPER",max);
124 1 : if( min!=0.0 || max!=0.0 ) {
125 1 : if( min>max ) error("minimum of cylinder should be less than maximum");
126 1 : docylinder=true;
127 2 : log.printf(" cylinder extends from %f to %f along the %s axis\n",min,max,sdir.c_str() );
128 3 : bead.isNotPeriodic(); bead.setKernelType( getKernelType() ); bead.set( min, max, getSigma() );
129 : }
130 :
131 1 : checkRead(); requestAtoms(atom);
132 1 : }
133 :
134 20 : void VolumeInCylinder::setupRegions() { }
135 :
136 4000 : double VolumeInCylinder::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const {
137 : // Calculate position of atom wrt to origin
138 8000 : Vector fpos=pbcDistance( getPosition(0), cpos );
139 :
140 : double vcylinder, dcylinder;
141 4000 : if( docylinder ) {
142 8000 : vcylinder=bead.calculate( fpos[dir[2]], dcylinder );
143 : } else {
144 0 : vcylinder=1.0; dcylinder=0.0;
145 : }
146 :
147 4000 : const double dd = fpos[dir[0]]*fpos[dir[0]] + fpos[dir[1]]*fpos[dir[1]];
148 4000 : double dfunc, vswitch = switchingFunction.calculateSqr( dd, dfunc );
149 4000 : derivatives.zero(); double value=vswitch*vcylinder;
150 8000 : derivatives[dir[0]]=vcylinder*dfunc*fpos[dir[0]];
151 8000 : derivatives[dir[1]]=vcylinder*dfunc*fpos[dir[1]];
152 8000 : derivatives[dir[2]]=vswitch*dcylinder;
153 : // Add derivatives wrt to position of origin atom
154 4000 : refders[0] = -derivatives;
155 : // Add virial contribution
156 4000 : vir -= Tensor(fpos,derivatives);
157 4000 : return value;
158 : }
159 :
160 : }
161 4839 : }
|