Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2017-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/ActionRegister.h"
23 : #include "tools/Pbc.h"
24 : #include "tools/KernelFunctions.h"
25 : #include "tools/SwitchingFunction.h"
26 : #include "ActionVolume.h"
27 : #include <memory>
28 :
29 : //+PLUMEDOC VOLUMES INENVELOPE
30 : /*
31 : This quantity can be used to calculate functions of the distribution of collective variables for the atoms that lie in a region where the density of a certain type of atom is high.
32 :
33 : This collective variable can be used to determine whether colvars are within region where the density
34 : of a particular atom is high. This is achieved by calculating the following function at the point where
35 : the atom is located \f$(x,y,z)\f$:
36 :
37 : \f[
38 : w_j = 1 - \sigma\left[ \sum_{i=1}^N K\left( \frac{x-x_i}{\sigma_x},\frac{y-y_i}{\sigma_y},\frac{z-z_i}{\sigma_z} \right) \right]
39 : \f]
40 :
41 : Here \f$\sigma\f$ is a \ref switchingfunction and \f$K\f$ is a \ref kernelfunctions. The sum runs over the atoms
42 : specified using the ATOMS keyword and a \f$w_j\f$ value is calculated for each of the central atoms of the input
43 : multicolvar.
44 :
45 : \par Examples
46 :
47 : The input below calculates a density field from the positions of atoms 1-14400. The number of the atoms
48 : that are specified in the DENSITY action that are within a region where the density field is greater than
49 : 2.0 is then calculated.
50 :
51 : \plumedfile
52 : d1: DENSITY SPECIES=14401-74134:3 LOWMEM
53 : fi: INENVELOPE DATA=d1 ATOMS=1-14400 CONTOUR={RATIONAL D_0=2.0 R_0=1.0} BANDWIDTH=0.1,0.1,0.1 LOWMEM
54 : PRINT ARG=fi FILE=colvar
55 : \endplumedfile
56 :
57 : */
58 : //+ENDPLUMEDOC
59 :
60 : namespace PLMD {
61 : namespace multicolvar {
62 :
63 : class VolumeInEnvelope : public ActionVolume {
64 : private:
65 : LinkCells mylinks;
66 : std::unique_ptr<KernelFunctions> kernel;
67 : std::vector<std::unique_ptr<Value>> pos;
68 : std::vector<Vector> ltmp_pos;
69 : std::vector<unsigned> ltmp_ind;
70 : SwitchingFunction sfunc;
71 : public:
72 : static void registerKeywords( Keywords& keys );
73 : explicit VolumeInEnvelope(const ActionOptions& ao);
74 : void setupRegions() override;
75 : double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const override;
76 : };
77 :
78 13785 : PLUMED_REGISTER_ACTION(VolumeInEnvelope,"INENVELOPE")
79 :
80 4 : void VolumeInEnvelope::registerKeywords( Keywords& keys ) {
81 4 : ActionVolume::registerKeywords( keys );
82 4 : keys.remove("SIGMA");
83 8 : keys.add("atoms","ATOMS","the atom whose positions we are constructing a field from");
84 8 : keys.add("compulsory","BANDWIDTH","the bandwidths for kernel density estimation");
85 8 : keys.add("compulsory","CONTOUR","a switching function that tells PLUMED how large the density should be");
86 4 : }
87 :
88 0 : VolumeInEnvelope::VolumeInEnvelope(const ActionOptions& ao):
89 : Action(ao),
90 : ActionVolume(ao),
91 0 : mylinks(comm) {
92 : std::vector<AtomNumber> atoms;
93 0 : parseAtomList("ATOMS",atoms);
94 0 : log.printf(" creating density field from atoms : ");
95 0 : for(unsigned i=0; i<atoms.size(); ++i) {
96 0 : log.printf("%d ",atoms[i].serial() );
97 : }
98 0 : log.printf("\n");
99 0 : ltmp_ind.resize( atoms.size() );
100 0 : ltmp_pos.resize( atoms.size() );
101 0 : for(unsigned i=0; i<atoms.size(); ++i) {
102 0 : ltmp_ind[i]=i;
103 : }
104 :
105 : std::string sw, errors;
106 0 : parse("CONTOUR",sw);
107 0 : if(sw.length()==0) {
108 0 : error("missing CONTOURkeyword");
109 : }
110 0 : sfunc.set(sw,errors);
111 0 : if( errors.length()!=0 ) {
112 0 : error("problem reading RADIUS keyword : " + errors );
113 : }
114 0 : log.printf(" density at atom must be larger than %s \n", ( sfunc.description() ).c_str() );
115 :
116 0 : std::vector<double> pp(3,0.0), bandwidth(3);
117 0 : parseVector("BANDWIDTH",bandwidth);
118 0 : log.printf(" using %s kernel with bandwidths %f %f %f \n",getKernelType().c_str(),bandwidth[0],bandwidth[1],bandwidth[2] );
119 0 : kernel=Tools::make_unique<KernelFunctions>( pp, bandwidth, getKernelType(), "DIAGONAL", 1.0 );
120 0 : for(unsigned i=0; i<3; ++i) {
121 0 : pos.emplace_back(Tools::make_unique<Value>());
122 0 : pos[i]->setNotPeriodic();
123 : }
124 0 : std::vector<double> csupport( kernel->getContinuousSupport() );
125 0 : double maxs = csupport[0];
126 0 : for(unsigned i=1; i<csupport.size(); ++i) {
127 0 : if( csupport[i]>maxs ) {
128 0 : maxs = csupport[i];
129 : }
130 : }
131 0 : checkRead();
132 0 : requestAtoms(atoms);
133 0 : mylinks.setCutoff( maxs );
134 0 : }
135 :
136 0 : void VolumeInEnvelope::setupRegions() {
137 0 : for(unsigned i=0; i<ltmp_ind.size(); ++i) {
138 0 : ltmp_pos[i]=getPosition(i);
139 : }
140 0 : mylinks.buildCellLists( ltmp_pos, ltmp_ind, getPbc() );
141 0 : }
142 :
143 0 : double VolumeInEnvelope::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const {
144 0 : unsigned ncells_required=0, natoms=1;
145 0 : std::vector<unsigned> cells_required( mylinks.getNumberOfCells() ), indices( 1 + getNumberOfAtoms() );
146 0 : mylinks.addRequiredCells( mylinks.findMyCell( cpos ), ncells_required, cells_required );
147 0 : indices[0]=getNumberOfAtoms();
148 0 : mylinks.retrieveAtomsInCells( ncells_required, cells_required, natoms, indices );
149 : double value=0;
150 0 : std::vector<double> der(3);
151 0 : Vector tder;
152 :
153 : // convert pointer once
154 0 : auto pos_ptr=Tools::unique2raw(pos);
155 :
156 0 : for(unsigned i=1; i<natoms; ++i) {
157 0 : Vector dist = getSeparation( cpos, getPosition( indices[i] ) );
158 0 : for(unsigned j=0; j<3; ++j) {
159 0 : pos[j]->set( dist[j] );
160 : }
161 0 : value += kernel->evaluate( pos_ptr, der, true );
162 0 : for(unsigned j=0; j<3; ++j) {
163 0 : derivatives[j] -= der[j];
164 0 : refders[ indices[i] ][j] += der[j];
165 0 : tder[j]=der[j];
166 : }
167 0 : vir -= Tensor( tder, dist );
168 : }
169 0 : double deriv, fval = sfunc.calculate( value, deriv );
170 0 : derivatives *= -deriv*value;
171 0 : vir *= -deriv*value;
172 0 : for(unsigned i=1; i<natoms; ++i) {
173 0 : refders[ indices[i] ] *= -deriv*value;
174 : }
175 0 : return 1.0 - fval;
176 : }
177 :
178 : }
179 : }
|