LCOV - code coverage report
Current view: top level - symfunc - CylindricalHarmonic.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 33 33 100.0 %
Date: 2025-12-04 11:19:34 Functions: 3 3 100.0 %

          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 intput 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;
      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             :   CylindricalHarmonic& operator=(const CylindricalHarmonic& m) {
      83             :     tmom=m.tmom;
      84             :     return *this;
      85             :   }
      86             : };
      87             : 
      88             : typedef function::FunctionShortcut<CylindricalHarmonic> CyHarmShortcut;
      89             : PLUMED_REGISTER_ACTION(CyHarmShortcut,"CYLINDRICAL_HARMONIC")
      90             : typedef function::FunctionOfMatrix<CylindricalHarmonic> MatrixCyHarm;
      91             : PLUMED_REGISTER_ACTION(MatrixCyHarm,"CYLINDRICAL_HARMONIC_MATRIX")
      92             : 
      93           9 : void CylindricalHarmonic::registerKeywords( Keywords& keys ) {
      94           9 :   keys.add("compulsory","DEGREE","the value of the n parameter in the equation above");
      95          18 :   keys.addOutputComponent("rm","default","matrix","the real part of the cylindrical harmonic");
      96          18 :   keys.addOutputComponent("im","default","matrix","the imaginary part of the cylindrical harmonic");
      97           9 : }
      98             : 
      99           2 : void CylindricalHarmonic::read( CylindricalHarmonic& func, ActionWithArguments* action, function::FunctionOptions& options ) {
     100           2 :   action->parse("DEGREE",func.tmom);
     101           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() );
     102           2 :   if( action->getNumberOfArguments()==3 ) {
     103           1 :     action->log.printf("  multiplying cylindrical harmonic by weight from %s \n", action->getPntrToArgument(2)->getName().c_str() );
     104             :   }
     105           2 :   options.derivativeZeroIfValueIsZero = (action->getNumberOfArguments()==3 && (action->getPntrToArgument(2))->isDerivativeZeroWhenValueIsZero());
     106           2 : }
     107             : 
     108     1741610 : void CylindricalHarmonic::calc( const CylindricalHarmonic& func, bool noderiv, const View<const double,helpers::dynamic_extent>& args, function::FunctionOutput& funcout ) {
     109     1741610 :   double dlen2 = args[0]*args[0] + args[1]*args[1];
     110     1741610 :   double dlen = sqrt( dlen2 );
     111     1741610 :   double dlen3 = dlen2*dlen;
     112     1741610 :   std::complex<double> com1( args[0]/dlen,args[1]/dlen );
     113             :   double weight=1;
     114     1741610 :   if( args.size()==3 ) {
     115     1596000 :     weight=args[2];
     116             :   }
     117     1741610 :   std::complex<double> ppp = pow( com1, func.tmom-1 ), ii( 0, 1 );
     118             :   double real_z = real( ppp*com1 ), imag_z = imag( ppp*com1 );
     119     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 );
     120     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 );
     121     1741610 :   funcout.values[0] = weight*real_z;
     122     1741610 :   funcout.values[1] = weight*imag_z;
     123             : 
     124     1741610 :   if( !noderiv ) {
     125      798000 :     funcout.derivs[0][0] = weight*real(dp_x);
     126      798000 :     funcout.derivs[0][1] = weight*real(dp_y);
     127      798000 :     funcout.derivs[1][0] = weight*imag(dp_x);
     128      798000 :     funcout.derivs[1][1] = weight*imag(dp_y);
     129      798000 :     if( args.size()==3 ) {
     130      798000 :       funcout.derivs[0][2] = real_z;
     131      798000 :       funcout.derivs[1][2] = imag_z;
     132             :     }
     133             :   }
     134     1741610 : }
     135             : 
     136             : }
     137             : }
     138             : 

Generated by: LCOV version 1.16