Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2012-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 "ActionWithVirtualAtom.h"
23 : #include "core/ActionRegister.h"
24 : #include "tools/Vector.h"
25 : #include "tools/Exception.h"
26 : #include <array>
27 :
28 : namespace PLMD {
29 : namespace vatom {
30 :
31 : //+PLUMEDOC VATOM GHOST
32 : /*
33 : Calculate the absolute position of a ghost atom with fixed coordinates in the local reference frame formed by three atoms.
34 :
35 : The computed ghost atom is stored as a virtual atom that can be accessed in
36 : an atom list through the the label for the GHOST action that creates it.
37 :
38 : When running with periodic boundary conditions, the atoms should be
39 : in the proper periodic image. This is done automatically since PLUMED 2.10,
40 : by considering the ordered list of atoms and rebuilding the molecule using a procedure
41 : that is equivalent to that done in \ref WHOLEMOLECULES . Notice that
42 : rebuilding is local to this action. This is different from \ref WHOLEMOLECULES
43 : which actually modifies the coordinates stored in PLUMED.
44 :
45 : In case you want to recover the old behavior you should use the NOPBC flag.
46 : In that case you need to take care that atoms are in the correct
47 : periodic image.
48 :
49 : \par Examples
50 :
51 : The following input instructs plumed to print the distance between the
52 : ghost atom and the center of mass for atoms 15,20:
53 : \plumedfile
54 : c1: GHOST ATOMS=1,5,10 COORDINATES=10.0,10.0,10.0
55 : c2: COM ATOMS=15,20
56 : d1: DISTANCE ATOMS=c1,c2
57 : PRINT ARG=d1
58 : \endplumedfile
59 :
60 : */
61 : //+ENDPLUMEDOC
62 :
63 :
64 : class Ghost:
65 : public ActionWithVirtualAtom {
66 : std::vector<double> coord;
67 : std::vector<Tensor> deriv;
68 : bool nopbc=false;
69 : public:
70 : explicit Ghost(const ActionOptions&ao);
71 : void calculate() override;
72 : static void registerKeywords( Keywords& keys );
73 : };
74 :
75 : PLUMED_REGISTER_ACTION(Ghost,"GHOST")
76 :
77 711 : void Ghost::registerKeywords(Keywords& keys) {
78 711 : ActionWithVirtualAtom::registerKeywords(keys);
79 1422 : keys.add("atoms","COORDINATES","coordinates of the ghost atom in the local reference frame");
80 1422 : keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
81 711 : }
82 :
83 709 : Ghost::Ghost(const ActionOptions&ao):
84 : Action(ao),
85 709 : ActionWithVirtualAtom(ao) {
86 : std::vector<AtomNumber> atoms;
87 1418 : parseAtomList("ATOMS",atoms);
88 709 : if(atoms.size()!=3) {
89 0 : error("ATOMS should contain a list of three atoms");
90 : }
91 :
92 1418 : parseVector("COORDINATES",coord);
93 709 : if(coord.size()!=3) {
94 0 : error("COORDINATES should be a list of three real numbers");
95 : }
96 :
97 709 : parseFlag("NOPBC",nopbc);
98 :
99 709 : checkRead();
100 709 : log.printf(" of atoms");
101 2836 : for(unsigned i=0; i<atoms.size(); ++i) {
102 2127 : log.printf(" %d",atoms[i].serial());
103 : }
104 709 : log.printf("\n");
105 :
106 709 : if(nopbc) {
107 4 : log<<" PBC will be ignored\n";
108 : } else {
109 705 : log<<" broken molecules will be rebuilt assuming atoms are in the proper order\n";
110 : }
111 709 : requestAtoms(atoms);
112 709 : }
113 :
114 1413 : void Ghost::calculate() {
115 :
116 1413 : if(!nopbc) {
117 1409 : makeWhole();
118 : }
119 :
120 1413 : Vector pos;
121 1413 : deriv.resize(getNumberOfAtoms());
122 1413 : std::array<Vector,3> n;
123 :
124 : // first versor
125 1413 : Vector n01 = delta(getPosition(0), getPosition(1));
126 1413 : n[0]=n01/n01.modulo();
127 :
128 : // auxiliary vector
129 1413 : Vector n02 = delta(getPosition(0), getPosition(2));
130 :
131 : // second versor
132 1413 : Vector n03 = crossProduct(n[0],n02);
133 1413 : double n03_norm = n03.modulo();
134 1413 : n[1]=n03/n03_norm;
135 :
136 : // third versor
137 1413 : n[2]=crossProduct(n[0],n[1]);
138 :
139 : // origin of the reference system
140 1413 : pos = getPosition(0);
141 :
142 5652 : for(unsigned i=0; i<3; ++i) {
143 4239 : pos += coord[i] * n[i];
144 : }
145 :
146 1413 : setPosition(pos);
147 1413 : setMass(1.0);
148 1413 : setCharge(0.0);
149 :
150 : // some useful tensors for derivatives
151 1413 : Tensor dn0d0 = (-Tensor::identity()+Tensor(n[0],n[0]))/n01.modulo();
152 1413 : Tensor dn0d1 = (+Tensor::identity()-Tensor(n[0],n[0]))/n01.modulo();
153 1413 : Tensor dn02d0 = -Tensor::identity();
154 1413 : Tensor dn02d2 = Tensor::identity();
155 :
156 : // derivative of n1 = n0 x n02
157 1413 : Tensor dn1d0, dn1d1, dn1d2;
158 1413 : Vector aux0, aux1, aux2;
159 :
160 5652 : for(unsigned j=0; j<3; ++j) {
161 : // derivative of n0 x n02 with respect to point 0, coordinate j
162 4239 : Vector tmp00 = Vector( dn0d0(j,0), dn0d0(j,1), dn0d0(j,2));
163 4239 : Vector tmp020 = Vector(dn02d0(j,0), dn02d0(j,1), dn02d0(j,2));
164 4239 : Vector tmp0 = crossProduct(tmp00,n02) + crossProduct(n[0],tmp020);
165 4239 : aux0[j] = dotProduct(tmp0,n[1]);
166 : // derivative of n0 x n02 with respect to point 1, coordinate j
167 4239 : Vector tmp01 = Vector( dn0d1(j,0), dn0d1(j,1), dn0d1(j,2));
168 4239 : Vector tmp1 = crossProduct(tmp01,n02);
169 4239 : aux1[j] = dotProduct(tmp1,n[1]);
170 : // derivative of n0 x n02 with respect to point 2, coordinate j
171 4239 : Vector tmp022 = Vector(dn02d2(j,0), dn02d2(j,1), dn02d2(j,2));
172 4239 : Vector tmp2 = crossProduct(n[0],tmp022);
173 4239 : aux2[j] = dotProduct(tmp2,n[1]);
174 : // derivative of n1 = (n0 x n02) / || (n0 x n02) ||
175 16956 : for(unsigned i=0; i<3; ++i) {
176 12717 : dn1d0(j,i) = ( tmp0[i] - aux0[j] * n[1][i] ) / n03_norm;
177 12717 : dn1d1(j,i) = ( tmp1[i] - aux1[j] * n[1][i] ) / n03_norm;
178 12717 : dn1d2(j,i) = ( tmp2[i] - aux2[j] * n[1][i] ) / n03_norm;
179 : }
180 : }
181 :
182 : // Derivative of the last versor n2 = n0 x n1 = ( n0( n0 n02 ) - n02 ) / || n0 x n02 ||
183 : // Scalar product and derivatives
184 1413 : double n0_n02 = dotProduct(n[0],n02);
185 1413 : Vector dn0_n02d0, dn0_n02d1, dn0_n02d2;
186 :
187 5652 : for(unsigned j=0; j<3; ++j) {
188 16956 : for(unsigned i=0; i<3; ++i) {
189 12717 : dn0_n02d0[j] += dn0d0(j,i)*n02[i] + n[0][i]*dn02d0(j,i);
190 12717 : dn0_n02d1[j] += dn0d1(j,i)*n02[i];
191 12717 : dn0_n02d2[j] += n[0][i]*dn02d2(j,i);
192 : }
193 : }
194 :
195 1413 : Tensor dn2d0, dn2d1, dn2d2;
196 5652 : for(unsigned j=0; j<3; ++j) {
197 16956 : for(unsigned i=0; i<3; ++i) {
198 12717 : dn2d0(j,i) = ( dn0d0(j,i) * n0_n02 + n[0][i] * dn0_n02d0[j] - dn02d0(j,i) - ( n[0][i] * n0_n02 - n02[i] ) * aux0[j] / n03_norm ) / n03_norm;
199 12717 : dn2d1(j,i) = ( dn0d1(j,i) * n0_n02 + n[0][i] * dn0_n02d1[j] - ( n[0][i] * n0_n02 - n02[i] ) * aux1[j] / n03_norm ) / n03_norm;
200 12717 : dn2d2(j,i) = ( n[0][i] * dn0_n02d2[j] - dn02d2(j,i) - ( n[0][i] * n0_n02 - n02[i] ) * aux2[j] / n03_norm ) / n03_norm;
201 : }
202 : }
203 :
204 : // Finally, the derivative tensor
205 1413 : deriv[0] = Tensor::identity() + coord[0]*dn0d0 + coord[1]*dn1d0 + coord[2]*dn2d0;
206 1413 : deriv[1] = coord[0]*dn0d1 + coord[1]*dn1d1 + coord[2]*dn2d1;
207 1413 : deriv[2] = coord[1]*dn1d2 + coord[2]*dn2d2;
208 :
209 1413 : setAtomsDerivatives(deriv);
210 :
211 : // Virial contribution
212 1413 : setBoxDerivativesNoPbc();
213 1413 : }
214 :
215 : }
216 : }
|