Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 : Copyright (c) 2012-2017 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 "function/FunctionSetup.h" 23 : #include "function/FunctionShortcut.h" 24 : #include "function/FunctionOfMatrix.h" 25 : #include "core/ActionRegister.h" 26 : 27 : #include <complex> 28 : 29 : namespace PLMD { 30 : namespace symfunc { 31 : 32 : //+PLUMEDOC MCOLVAR CYLINDRICAL_HARMONIC 33 : /* 34 : Calculate the cylindrical harmonic function 35 : 36 : This action allows you to the value of the following complex function. The action outputs 37 : two components that are the real and imaginary parts of the following function: 38 : 39 : $$ 40 : z = w (\frac{x}{r} + \frac{y}{r} i )^n \qquad \textrm{where} \qquad r = \sqrt(x^2 + y^2} 41 : $$ 42 : 43 : In this expression $n$ is a parameter that is specified using the DEGREE keyword. $x$ and $y$ are the input arguments and $w$ is an optional input weight, which is set equal to 44 : one if only two arguments are provided in input. At present, the arguments for this action must be matrices. 45 : These arguments must all have the same shape as the two output components will also be matrices that are 46 : calculated by applying the function above to each of the elements of the input matrix in turn. 47 : 48 : The following input provides an example that demonstrates how this function is used: 49 : 50 : ```plumed 51 : d: DISTANCE_MATRIX GROUP=1-10 COMPONENTS 52 : c: CYLINDRICAL_HARMONIC DEGREE=6 ARG=d.x,d.y 53 : PRINT ARG=c.rm FILE=real_part 54 : PRINT ARG=c.im FILE=imaginary_part 55 : ``` 56 : 57 : The DISTANCE_MATRIX command in the above input computes 3 $10\times10$ matrices. Two of these $10\times10$ matrices are used in the input to the cylindrical harmonic command, 58 : which in turn outputs two $10\times10$ matrices that contain the real and imaginary parts when the function above is applied element-wise to the above input. These two $10\times10$ 59 : matrices are then output to two separate files. 60 : 61 : In the above example the weights for every distance is set equal to one. The following example shows how an argument can be used to set the $w$ values to use when computing the function 62 : above. 63 : 64 : ```plumed 65 : s: CONTACT_MATRIX GROUP=1-10 SWITCH={RATIONAL R_0=1.0} 66 : sc: CONTACT_MATRIX GROUP=1-10 SWITCH={RATIONAL R_0=1.0} COMPONENTS 67 : c: CYLINDRICAL_HARMONIC DEGREE=6 ARG=sc.x,sc.y,s 68 : PRINT ARG=c.rm FILE=real_part 69 : PRINT ARG=c.im FILE=imaginary_part 70 : ``` 71 : 72 : */ 73 : //+ENDPLUMEDOC 74 : 75 : 76 : class CylindricalHarmonic { 77 : public: 78 : int tmom=0; 79 : static void registerKeywords( Keywords& keys ); 80 : static void read( CylindricalHarmonic& func, ActionWithArguments* action, function::FunctionOptions& options ); 81 : static void calc( const CylindricalHarmonic& func, bool noderiv, const View<const double,helpers::dynamic_extent>& args, function::FunctionOutput& funcout ); 82 : // the assignment is customized (see below): copy construction is the default one 83 5 : CylindricalHarmonic() = default; 84 : CylindricalHarmonic(const CylindricalHarmonic&) = default; 85 : CylindricalHarmonic& operator=(const CylindricalHarmonic& m) { 86 : tmom=m.tmom; 87 : return *this; 88 : } 89 : }; 90 : 91 : typedef function::FunctionShortcut<CylindricalHarmonic> CyHarmShortcut; 92 : PLUMED_REGISTER_ACTION(CyHarmShortcut,"CYLINDRICAL_HARMONIC") 93 : typedef function::FunctionOfMatrix<CylindricalHarmonic> MatrixCyHarm; 94 : PLUMED_REGISTER_ACTION(MatrixCyHarm,"CYLINDRICAL_HARMONIC_MATRIX") 95 : 96 15 : void CylindricalHarmonic::registerKeywords( Keywords& keys ) { 97 15 : keys.add("compulsory","DEGREE","the value of the n parameter in the equation above"); 98 30 : keys.addOutputComponent("rm","default","matrix","the real part of the cylindrical harmonic"); 99 30 : keys.addOutputComponent("im","default","matrix","the imaginary part of the cylindrical harmonic"); 100 15 : } 101 : 102 3 : void CylindricalHarmonic::read( CylindricalHarmonic& func, ActionWithArguments* action, function::FunctionOptions& options ) { 103 3 : action->parse("DEGREE",func.tmom); 104 3 : if( action->getNumberOfArguments()<2 ) { 105 1 : action->error("the x and y components of the vectors (and optionally a weight) should be given with ARG"); 106 : } 107 2 : action->log.printf(" calculating %dth order cylindrical harmonic with %s and %s as input \n", func.tmom, action->getPntrToArgument(0)->getName().c_str(), action->getPntrToArgument(1)->getName().c_str() ); 108 2 : if( action->getNumberOfArguments()==3 ) { 109 1 : action->log.printf(" multiplying cylindrical harmonic by weight from %s \n", action->getPntrToArgument(2)->getName().c_str() ); 110 : } 111 2 : options.derivativeZeroIfValueIsZero = (action->getNumberOfArguments()==3 && (action->getPntrToArgument(2))->isDerivativeZeroWhenValueIsZero()); 112 2 : } 113 : 114 1741610 : void CylindricalHarmonic::calc( const CylindricalHarmonic& func, bool noderiv, const View<const double,helpers::dynamic_extent>& args, function::FunctionOutput& funcout ) { 115 1741610 : double dlen2 = args[0]*args[0] + args[1]*args[1]; 116 1741610 : double dlen = sqrt( dlen2 ); 117 1741610 : double dlen3 = dlen2*dlen; 118 1741610 : std::complex<double> com1( args[0]/dlen,args[1]/dlen ); 119 : double weight=1; 120 1741610 : if( args.size()==3 ) { 121 1596000 : weight=args[2]; 122 : } 123 1741610 : std::complex<double> ppp = pow( com1, func.tmom-1 ), ii( 0, 1 ); 124 : double real_z = real( ppp*com1 ), imag_z = imag( ppp*com1 ); 125 1741610 : std::complex<double> dp_x = static_cast<double>(func.tmom)*ppp*( (1.0/dlen)-(args[0]*args[0])/dlen3-ii*(args[0]*args[1])/dlen3 ); 126 1741610 : std::complex<double> dp_y = static_cast<double>(func.tmom)*ppp*( ii*(1.0/dlen)-(args[0]*args[1])/dlen3-ii*(args[1]*args[1])/dlen3 ); 127 1741610 : funcout.values[0] = weight*real_z; 128 1741610 : funcout.values[1] = weight*imag_z; 129 : 130 1741610 : if( !noderiv ) { 131 798000 : funcout.derivs[0][0] = weight*real(dp_x); 132 798000 : funcout.derivs[0][1] = weight*real(dp_y); 133 798000 : funcout.derivs[1][0] = weight*imag(dp_x); 134 798000 : funcout.derivs[1][1] = weight*imag(dp_y); 135 798000 : if( args.size()==3 ) { 136 798000 : funcout.derivs[0][2] = real_z; 137 798000 : funcout.derivs[1][2] = imag_z; 138 : } 139 : } 140 1741610 : } 141 : 142 : } 143 : } 144 :