LCOV - code coverage report
Current view: top level - crystdistrib - RopsShortcut.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 72 78 92.3 %
Date: 2025-04-08 21:11:17 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) crystdistrib 2023-2023 The code team
       3             :    (see the PEOPLE-crystdistrib file at the root of this folder for a list of names)
       4             : 
       5             :    This file is part of crystdistrib code module.
       6             : 
       7             :    The crystdistrib code module is free software: you can redistribute it and/or modify
       8             :    it under the terms of the GNU Lesser General Public License as published by
       9             :    the Free Software Foundation, either version 3 of the License, or
      10             :    (at your option) any later version.
      11             : 
      12             :    The crystdistrib code module is distributed in the hope that it will be useful,
      13             :    but WITHOUT ANY WARRANTY; without even the implied warranty of
      14             :    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      15             :    GNU Lesser General Public License for more details.
      16             : 
      17             :    You should have received a copy of the GNU Lesser General Public License
      18             :    along with the crystdistrib code module.  If not, see <http://www.gnu.org/licenses/>.
      19             : +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
      20             : #include "core/ActionShortcut.h"
      21             : #include "core/ActionRegister.h"
      22             : #include "core/PlumedMain.h"
      23             : #include "core/ActionSet.h"
      24             : #include "core/ActionWithValue.h"
      25             : #include "tools/IFile.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace crystdistrib {
      29             : 
      30             : //+PLUMEDOC COLVAR ROPS
      31             : /*
      32             : Calculate the ROPS order parameter
      33             : 
      34             : ROPS is a shortcut to calculate the Relative-Orientational Order Parameters that are described in the paper that is referenced below. As arguments, ROPS takes a list of atoms (corresponding to molecules), a vector of quaternions, a cutoff distance, and two kernel files detailing the means, variances, and normalization factors of the probability distributions. ROPS returns a vector of order parameters.
      35             : 
      36             : The DOPS kernel file has FIELDS height, mu, and sigma corresponding to the normalization factor, mean, and variance of the gaussian distributions used in the order parameters. The SET kerneltype is gaussian.
      37             : The ROPS kernel file has FIELDS height, kappa, mu\_w, mu\_i, mu\_j, and mu\_k, which correspond to the normalization factor, reciprocal variance, and components of the mean quaternion frame of the Bipolar Watson distribution used in the order parameters. The SET kerneltype is gaussian.
      38             : 
      39             : ROPS returns one order parameter per atom given, evaluated over each atom's neighbors within the cutoff. The distribution defined by the kernel files, analogous to a radial distribution function, defined over all possible spatial orientations two molecules could take relative to one another. The domain is the set of all unit quaternions. The order parameter is obtained by evaluating the distribution at each neighbor's unit quaternion, and summing them.
      40             : 
      41             : This example file calculates the ROPS for a system of 3 molecules.
      42             : 
      43             : ```plumed
      44             : #SETTINGS INPUTFILES=regtest/crystdistrib/rt-rops-shortcut/kernels.dat,regtest/crystdistrib/rt-rops-shortcut/kernels2.dat
      45             : quat: QUATERNION ATOMS1=1,2,3 ATOMS2=4,5,6 ATOMS3=7,8,9
      46             : bops: ROPS SPECIES=1,4,7 QUATERNIONS=quat CUTOFF=100.0 KERNELFILE_DOPS=regtest/crystdistrib/rt-rops-shortcut/kernels.dat KERNELFILE_ROPS=regtest/crystdistrib/rt-rops-shortcut/kernels2.dat
      47             : ```
      48             : 
      49             : */
      50             : //+ENDPLUMEDOC
      51             : 
      52             : class RopsShortcut : public ActionShortcut {
      53             : public:
      54             :   static void registerKeywords( Keywords& keys );
      55             :   explicit RopsShortcut(const ActionOptions&);
      56             : };
      57             : 
      58             : PLUMED_REGISTER_ACTION(RopsShortcut,"ROPS")
      59             : 
      60           3 : void RopsShortcut::registerKeywords( Keywords& keys ) {
      61           3 :   ActionShortcut::registerKeywords( keys );
      62           3 :   keys.add("atoms","SPECIES","this keyword is used for colvars such as coordination number. In that context it specifies that plumed should calculate "
      63             :            "one coordination number for each of the atoms specified.  Each of these coordination numbers specifies how many of the "
      64             :            "other specified atoms are within a certain cutoff of the central atom.  You can specify the atoms here as another multicolvar "
      65             :            "action or using a MultiColvarFilter or ActionVolume action.  When you do so the quantity is calculated for those atoms specified "
      66             :            "in the previous multicolvar.  This is useful if you would like to calculate the Steinhardt parameter for those atoms that have a "
      67             :            "coordination number more than four for example");
      68           3 :   keys.add("atoms-2","SPECIESA","this keyword is used for colvars such as the coordination number.  In that context it species that plumed should calculate "
      69             :            "one coordination number for each of the atoms specified in SPECIESA.  Each of these cooordination numbers specifies how many "
      70             :            "of the atoms specifies using SPECIESB is within the specified cutoff.  As with the species keyword the input can also be specified "
      71             :            "using the label of another multicolvar");
      72           3 :   keys.add("atoms-2","SPECIESB","this keyword is used for colvars such as the coordination number.  It must appear with SPECIESA.  For a full explanation see "
      73             :            "the documentation for that keyword");
      74           3 :   keys.add("compulsory","QUATERNIONS","the label of the action that computes the quaternions that should be used");
      75           3 :   keys.add("compulsory","KERNELFILE_DOPS","the file containing the list of kernel parameters.  We expect h, mu and sigma parameters for a 1D Gaussian kernel of the form h*exp(-(x-mu)^2/2sigma^2)");
      76           3 :   keys.add("compulsory","KERNELFILE_ROPS","the file containing the list of kernel parameters.  We expect the normalization factor (height), concentration parameter (kappa), and 4 quaternion pieces of the mean for a bipolar watson distribution (mu_w,mu_i,mu_j,mu_k)): (h*exp(kappa*dot(q_mean,q))), where dot is the dot product ");
      77           3 :   keys.add("compulsory", "CUTOFF", "cutoff for the distance matrix");
      78             : //  keys.add("compulsory","SWITCH","the switching function that acts on the distances between points)");
      79           6 :   keys.setValueDescription("vector","the values of the ROPS order parameters");
      80           3 :   keys.needsAction("DISTANCE_MATRIX");
      81           3 :   keys.needsAction("QUATERNION_PRODUCT_MATRIX");
      82           3 :   keys.needsAction("ONES");
      83           3 :   keys.needsAction("CUSTOM");
      84           3 :   keys.needsAction("MATRIX_VECTOR_PRODUCT");
      85           3 :   keys.addDOI("10.1063/1.3548889");
      86           3 : }
      87             : 
      88           1 : RopsShortcut::RopsShortcut(const ActionOptions&ao):
      89             :   Action(ao),
      90           1 :   ActionShortcut(ao) {
      91             :   // Open a file and read in the kernels
      92             :   double h_dops,h_rops;
      93             :   std::string kfunc, kfunc_dops,kfunc_rops,fname_dops,fname_rops;
      94           1 :   parse("KERNELFILE_DOPS",fname_dops);
      95           1 :   parse("KERNELFILE_ROPS",fname_rops);
      96           1 :   IFile ifile_dops, ifile_rops;
      97           1 :   ifile_dops.open(fname_dops);
      98           1 :   ifile_rops.open(fname_rops);
      99          10 :   for(unsigned k=0;; ++k) {
     100          21 :     if( !ifile_dops.scanField("height",h_dops) || !ifile_rops.scanField("height",h_rops) ) {
     101             :       break;  //checks eof
     102             :     }
     103             :     std::string ktype_dops, ktype_rops;
     104          10 :     ifile_dops.scanField("kerneltype",ktype_dops);
     105          20 :     ifile_rops.scanField("kerneltype",ktype_rops);
     106          10 :     if( ktype_dops!="gaussian" ) {
     107           0 :       error("cannot process kernels of type " + ktype_dops );  //straightup error
     108             :     }
     109          10 :     if( ktype_rops!="gaussian" ) {
     110           0 :       error("cannot process kernels of type " + ktype_rops );
     111             :     }
     112             : 
     113             :     double mu_dops,mu_w, mu_i, mu_j, mu_k;
     114             :     std::string hstr_dops, hstr_rops, smu_dops,smu_w, smu_i, smu_j, smu_k, sigmastr,kappastr;
     115             : 
     116             : 
     117          10 :     Tools::convert( h_dops, hstr_dops );
     118          10 :     Tools::convert( h_rops, hstr_rops );
     119             : 
     120          10 :     ifile_dops.scanField("mu",mu_dops);
     121          10 :     Tools::convert( mu_dops, smu_dops );
     122          10 :     ifile_rops.scanField("mu_w",mu_w);
     123          10 :     Tools::convert( mu_w, smu_w );
     124          10 :     ifile_rops.scanField("mu_i",mu_i);
     125          10 :     Tools::convert( mu_i, smu_i );
     126          10 :     ifile_rops.scanField("mu_j",mu_j);
     127          10 :     Tools::convert( mu_j, smu_j );
     128          10 :     ifile_rops.scanField("mu_k",mu_k);
     129          10 :     Tools::convert( mu_k, smu_k );
     130             : 
     131             : 
     132             :     double sigma,kappa;
     133          10 :     ifile_dops.scanField("sigma",sigma);
     134          10 :     Tools::convert( sigma, sigmastr );
     135          10 :     ifile_rops.scanField("kappa",kappa);
     136          10 :     Tools::convert( kappa, kappastr );
     137             : 
     138             : 
     139             : 
     140          10 :     ifile_dops.scanField(); /*if( k==0 )*/ kfunc_dops =  hstr_dops; //else kfunc_dops += "+" + hstr;
     141          10 :     ifile_rops.scanField(); /*if( k==0 )*/ kfunc_rops =  hstr_rops; //else kfunc_rops += "+" + hstr;
     142             : 
     143          20 :     kfunc_rops += "*exp(" + kappastr + "*(w*" + smu_w + "+i*" + smu_i + "+j*" + smu_j + "+k*" + smu_k + ")^2)";
     144          20 :     kfunc_dops += "*exp(-(x-" + smu_dops +")^2/" + "(2*" + sigmastr +"*" +sigmastr + "))";
     145          10 :     if (k==0) {
     146           2 :       kfunc = kfunc_dops + "*" + kfunc_rops;
     147             :     } else {
     148          18 :       kfunc+= "+" + kfunc_dops + "*" + kfunc_rops;
     149             :     }
     150          10 :   }
     151             :   std::string sp_str, specA, specB, grpinfo;
     152             :   double cutoff;
     153           1 :   parse("SPECIES",sp_str);
     154           1 :   parse("SPECIESA",specA);
     155           1 :   parse("SPECIESB",specB);
     156           2 :   parse("CUTOFF",cutoff);
     157           1 :   if( sp_str.length()>0 ) {
     158           2 :     grpinfo="GROUP=" + sp_str;
     159             :   } else {//not sure how to use this
     160           0 :     if( specA.length()==0 || specB.length()==0 ) {
     161           0 :       error("no atoms were specified in input use either SPECIES or SPECIESA + SPECIESB");
     162             :     }
     163           0 :     grpinfo="GROUPA=" + specA + " GROUPB=" + specB;
     164             :   }
     165             :   std::string cutstr;
     166           1 :   Tools::convert( cutoff, cutstr );
     167             :   // Setup the contact matrix
     168             : //  std::string switchstr; parse("SWITCH",switchstr);
     169           2 :   readInputLine( getShortcutLabel() + "_cmat: DISTANCE_MATRIX  " + grpinfo + " CUTOFF=" + cutstr);
     170             : 
     171           1 :   if( specA.length()==0 ) {
     172             :     std::string quatstr;
     173           1 :     parse("QUATERNIONS",quatstr);
     174           2 :     readInputLine( getShortcutLabel() + "_quatprod: QUATERNION_PRODUCT_MATRIX ARG=" + quatstr + ".*," + quatstr + ".*" );
     175             :   }  else {
     176           0 :     plumed_error();
     177             :   }
     178             :   //
     179           2 :   readInputLine( getShortcutLabel() + "_kfunc: CUSTOM ARG=" + getShortcutLabel() + "_cmat,"+ getShortcutLabel() + "_quatprod.* " + "VAR=x,w,i,j,k PERIODIC=NO FUNC=" + kfunc );
     180             :   // Element wise product of cmat and kfunc
     181             : //  readInputLine( getShortcutLabel() + "_kdmat: CUSTOM ARG=" + getShortcutLabel() + "_cmat.w," + getShortcutLabel() + "_kfunc FUNC=x*y PERIODIC=NO");
     182             :   // Find the number of ones we need to multiply by
     183           1 :   ActionWithValue* av = plumed.getActionSet().selectWithLabel<ActionWithValue*>( getShortcutLabel() + "_cmat");
     184           1 :   plumed_assert( av && av->getNumberOfComponents()>0 && (av->copyOutput(0))->getRank()==2 );
     185             :   std::string size;
     186           1 :   Tools::convert( (av->copyOutput(0))->getShape()[1], size );
     187           2 :   readInputLine( getShortcutLabel() + "_ones: ONES SIZE=" + size );
     188             :   //
     189           2 :   readInputLine( getShortcutLabel() + ": MATRIX_VECTOR_PRODUCT ARG=" + getShortcutLabel() + "_kfunc," + getShortcutLabel() + "_ones");
     190           2 : }
     191             : 
     192             : }
     193             : }
     194             : 
     195             : 
     196             : 

Generated by: LCOV version 1.16