LCOV - code coverage report
Current view: top level - symfunc - CylindricalHarmonic.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 36 36 100.0 %
Date: 2025-04-08 21:11:17 Functions: 4 4 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/FunctionTemplateBase.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             : //+PLUMEDOC MCOLVAR CYLINDRICAL_HARMONIC_MATRIX
      76             : /*
      77             : Calculate the cylindrical harmonic function from the elements in two input matrices
      78             : 
      79             : \par Examples
      80             : 
      81             : 
      82             : */
      83             : //+ENDPLUMEDOC
      84             : 
      85             : 
      86          12 : class CylindricalHarmonic : public function::FunctionTemplateBase {
      87             : private:
      88             :   int tmom;
      89             : public:
      90             :   void registerKeywords( Keywords& keys ) override;
      91             :   void read( ActionWithArguments* action ) override;
      92             :   void setPeriodicityForOutputs( ActionWithValue* action ) override;
      93             :   void calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const override;
      94             : };
      95             : 
      96             : typedef function::FunctionShortcut<CylindricalHarmonic> CyHarmShortcut;
      97             : PLUMED_REGISTER_ACTION(CyHarmShortcut,"CYLINDRICAL_HARMONIC")
      98             : typedef function::FunctionOfMatrix<CylindricalHarmonic> MatrixCyHarm;
      99             : PLUMED_REGISTER_ACTION(MatrixCyHarm,"CYLINDRICAL_HARMONIC_MATRIX")
     100             : 
     101           9 : void CylindricalHarmonic::registerKeywords( Keywords& keys ) {
     102           9 :   keys.add("compulsory","DEGREE","the value of the n parameter in the equation above");
     103          18 :   keys.addOutputComponent("rm","default","matrix","the real part of the cylindrical harmonic");
     104          18 :   keys.addOutputComponent("im","default","matrix","the imaginary part of the cylindrical harmonic");
     105           9 : }
     106             : 
     107           2 : void CylindricalHarmonic::read( ActionWithArguments* action ) {
     108           2 :   parse(action,"DEGREE",tmom);
     109           2 :   action->log.printf("  calculating %dth order cylindrical harmonic with %s and %s as input \n", tmom, action->getPntrToArgument(0)->getName().c_str(), action->getPntrToArgument(1)->getName().c_str() );
     110           2 :   if( action->getNumberOfArguments()==3 ) {
     111           1 :     action->log.printf("  multiplying cylindrical harmonic by weight from %s \n", action->getPntrToArgument(2)->getName().c_str() );
     112             :   }
     113           2 : }
     114             : 
     115           2 : void CylindricalHarmonic::setPeriodicityForOutputs( ActionWithValue* action ) {
     116           2 :   action->componentIsNotPeriodic("rm");
     117           2 :   action->componentIsNotPeriodic("im");
     118           2 : }
     119             : 
     120      943610 : void CylindricalHarmonic::calc( const ActionWithArguments* action, const std::vector<double>& args, std::vector<double>& vals, Matrix<double>& derivatives ) const {
     121      943610 :   double dlen2 = args[0]*args[0] + args[1]*args[1];
     122      943610 :   double dlen = sqrt( dlen2 );
     123      943610 :   double dlen3 = dlen2*dlen;
     124      943610 :   std::complex<double> com1( args[0]/dlen,args[1]/dlen );
     125             :   double weight=1;
     126      943610 :   if( args.size()==3 ) {
     127      798000 :     weight=args[2];
     128             :   }
     129      943610 :   std::complex<double> ppp = pow( com1, tmom-1 ), ii( 0, 1 );
     130             :   double real_z = real( ppp*com1 ), imag_z = imag( ppp*com1 );
     131      943610 :   std::complex<double> dp_x = static_cast<double>(tmom)*ppp*( (1.0/dlen)-(args[0]*args[0])/dlen3-ii*(args[0]*args[1])/dlen3 );
     132      943610 :   std::complex<double> dp_y = static_cast<double>(tmom)*ppp*( ii*(1.0/dlen)-(args[0]*args[1])/dlen3-ii*(args[1]*args[1])/dlen3 );
     133      943610 :   vals[0] = weight*real_z;
     134      943610 :   derivatives(0,0) = weight*real(dp_x);
     135      943610 :   derivatives(0,1) = weight*real(dp_y);
     136      943610 :   vals[1] = weight*imag_z;
     137      943610 :   derivatives(1,0) = weight*imag(dp_x);
     138      943610 :   derivatives(1,1) = weight*imag(dp_y);
     139      943610 :   if( args.size()==3 ) {
     140      798000 :     derivatives(0,2) = real_z;
     141      798000 :     derivatives(1,2) = imag_z;
     142             :   }
     143      943610 : }
     144             : 
     145             : }
     146             : }
     147             : 

Generated by: LCOV version 1.16