Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2016-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 "CubicHarmonicBase.h"
23 : #include "tools/SwitchingFunction.h"
24 :
25 : #include <string>
26 : #include <cmath>
27 :
28 : using namespace std;
29 :
30 : namespace PLMD {
31 : namespace crystallization {
32 :
33 9 : void CubicHarmonicBase::registerKeywords( Keywords& keys ) {
34 9 : multicolvar::MultiColvarBase::registerKeywords( keys );
35 27 : keys.use("SPECIES"); keys.use("SPECIESA"); keys.use("SPECIESB");
36 18 : keys.add("compulsory","NN","6","The n parameter of the switching function ");
37 18 : keys.add("compulsory","MM","0","The m parameter of the switching function; 0 implies 2*NN");
38 18 : keys.add("compulsory","D_0","0.0","The d_0 parameter of the switching function");
39 18 : keys.add("compulsory","R_0","The r_0 parameter of the switching function");
40 18 : keys.add("optional","SWITCH","This keyword is used if you want to employ an alternative to the continuous switching function defined above. "
41 : "The following provides information on the \\ref switchingfunction that are available. "
42 : "When this keyword is present you no longer need the NN, MM, D_0 and R_0 keywords.");
43 18 : keys.add("compulsory","PHI","0.0","The Euler rotational angle phi");
44 18 : keys.add("compulsory","THETA","0.0","The Euler rotational angle theta");
45 18 : keys.add("compulsory","PSI","0.0","The Euler rotational angle psi");
46 18 : keys.addFlag("UNORMALIZED",false,"calculate the sum of the components of the vector rather than the mean");
47 : // Use actionWithDistributionKeywords
48 36 : keys.use("MEAN"); keys.use("MORE_THAN"); keys.use("LESS_THAN"); keys.use("MAX");
49 36 : keys.use("MIN"); keys.use("BETWEEN"); keys.use("HISTOGRAM"); keys.use("MOMENTS");
50 27 : keys.use("ALT_MIN"); keys.use("LOWEST"); keys.use("HIGHEST");
51 9 : }
52 :
53 6 : CubicHarmonicBase::CubicHarmonicBase(const ActionOptions&ao):
54 : Action(ao),
55 6 : MultiColvarBase(ao)
56 : {
57 : // Read in the switching function
58 12 : std::string sw, errors; parse("SWITCH",sw);
59 6 : if(sw.length()>0) {
60 6 : switchingFunction.set(sw,errors);
61 6 : if( errors.length()!=0 ) error("problem reading SWITCH keyword : " + errors );
62 : } else {
63 0 : double r_0=-1.0, d_0; int nn, mm;
64 0 : parse("NN",nn); parse("MM",mm);
65 0 : parse("R_0",r_0); parse("D_0",d_0);
66 0 : if( r_0<0.0 ) error("you must set a value for R_0");
67 0 : switchingFunction.set(nn,mm,r_0,d_0);
68 : }
69 :
70 18 : double phi, theta, psi; parse("PHI",phi); parse("THETA",theta); parse("PSI",psi);
71 6 : log.printf(" creating rotation matrix with Euler angles phi=%f, theta=%f and psi=%f\n",phi,theta,psi);
72 : // Calculate the rotation matrix http://mathworld.wolfram.com/EulerAngles.html
73 6 : rotationmatrix[0][0]=std::cos(psi)*std::cos(phi)-std::cos(theta)*std::sin(phi)*std::sin(psi);
74 6 : rotationmatrix[0][1]=std::cos(psi)*std::sin(phi)+std::cos(theta)*std::cos(phi)*std::sin(psi);
75 6 : rotationmatrix[0][2]=std::sin(psi)*std::sin(theta);
76 :
77 6 : rotationmatrix[1][0]=-std::sin(psi)*std::cos(phi)-std::cos(theta)*std::sin(phi)*std::cos(psi);
78 6 : rotationmatrix[1][1]=-std::sin(psi)*std::sin(phi)+std::cos(theta)*std::cos(phi)*std::cos(psi);
79 6 : rotationmatrix[1][2]=std::cos(psi)*std::sin(theta);
80 :
81 6 : rotationmatrix[2][0]=std::sin(theta)*std::sin(phi);
82 6 : rotationmatrix[2][1]=-std::sin(theta)*std::cos(phi);
83 6 : rotationmatrix[2][2]=std::cos(theta);
84 :
85 :
86 6 : log.printf(" measure crystallinity around central atom. Includes those atoms within %s\n",( switchingFunction.description() ).c_str() );
87 6 : parseFlag("UNORMALIZED",unormalized);
88 6 : if( unormalized ) log.printf(" output sum of vector functions \n");
89 6 : else log.printf(" output mean of vector functions \n");
90 : // Set the link cell cutoff
91 6 : rcut2 = switchingFunction.get_dmax()*switchingFunction.get_dmax();
92 6 : setLinkCellCutoff( switchingFunction.get_dmax() );
93 : // And setup the ActionWithVessel
94 6 : std::vector<AtomNumber> all_atoms; setupMultiColvarBase( all_atoms );
95 6 : }
96 :
97 26142 : double CubicHarmonicBase::compute( const unsigned& tindex, multicolvar::AtomValuePack& myatoms ) const {
98 26142 : double dfunc; Vector rotatedis;
99 :
100 : // Calculate the coordination number
101 26142 : Vector myder, rotateder, fder; unsigned nat=myatoms.getNumberOfAtoms();
102 :
103 2517483 : for(unsigned i=1; i<nat; ++i) {
104 : Vector& distance=myatoms.getPosition(i);
105 :
106 : double d2;
107 3953726 : if ( (d2=distance[0]*distance[0])<rcut2 &&
108 1462385 : (d2+=distance[1]*distance[1])<rcut2 &&
109 3193615 : (d2+=distance[2]*distance[2])<rcut2 &&
110 : d2>epsilon ) {
111 :
112 328678 : double sw = switchingFunction.calculateSqr( d2, dfunc );
113 :
114 328678 : rotatedis[0]=rotationmatrix[0][0]*distance[0]
115 328678 : +rotationmatrix[0][1]*distance[1]
116 328678 : +rotationmatrix[0][2]*distance[2];
117 328678 : rotatedis[1]=rotationmatrix[1][0]*distance[0]
118 328678 : +rotationmatrix[1][1]*distance[1]
119 328678 : +rotationmatrix[1][2]*distance[2];
120 328678 : rotatedis[2]=rotationmatrix[2][0]*distance[0]
121 328678 : +rotationmatrix[2][1]*distance[1]
122 328678 : +rotationmatrix[2][2]*distance[2];
123 :
124 328678 : double tmp = calculateCubicHarmonic( rotatedis, d2, rotateder );
125 :
126 328678 : myder[0]=rotationmatrix[0][0]*rotateder[0]
127 328678 : +rotationmatrix[1][0]*rotateder[1]
128 328678 : +rotationmatrix[2][0]*rotateder[2];
129 328678 : myder[1]=rotationmatrix[0][1]*rotateder[0]
130 328678 : +rotationmatrix[1][1]*rotateder[1]
131 328678 : +rotationmatrix[2][1]*rotateder[2];
132 328678 : myder[2]=rotationmatrix[0][2]*rotateder[0]
133 328678 : +rotationmatrix[1][2]*rotateder[1]
134 328678 : +rotationmatrix[2][2]*rotateder[2];
135 :
136 328678 : fder = (+dfunc)*tmp*distance + sw*myder;
137 :
138 328678 : accumulateSymmetryFunction( 1, i, sw*tmp, fder, Tensor(distance,-fder), myatoms );
139 328678 : accumulateSymmetryFunction( -1, i, sw, (+dfunc)*distance, (-dfunc)*Tensor(distance,distance), myatoms );
140 : }
141 : }
142 : // values -> der of... value [0], weight[1], x coord [2], y, z... [more magic]
143 26142 : updateActiveAtoms( myatoms );
144 26142 : if( !unormalized ) myatoms.getUnderlyingMultiValue().quotientRule( 1, 1 );
145 26142 : return myatoms.getValue(1); // this is equivalent to getting an "atomic" CV
146 : }
147 :
148 : }
149 : }
150 :
|