LCOV - code coverage report
Current view: top level - generic - Ones.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 27 34 79.4 %
Date: 2026-11-05 10:19:04 Functions: 2 3 66.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2012-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 "core/ActionShortcut.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/ActionAtomistic.h"
      25             : 
      26             : namespace PLMD {
      27             : namespace generic {
      28             : 
      29             : //+PLUMEDOC COLVAR ONES
      30             : /*
      31             : Create a constant vector with all elements equal to one
      32             : 
      33             : The following input creates and outputs a constant vector with 10 elements that are all equal to one
      34             : 
      35             : ```plumed
      36             : ones: ONES SIZE=10
      37             : PRINT ARG=ones FILE=onesfile
      38             : ```
      39             : 
      40             : Notice that the ONES action is a shortcut to [CONSTANT](CONSTANT.md).
      41             : 
      42             : This action is used extensively when calculating coordination numbers in inputs similar to this one:
      43             : 
      44             : ```plumed
      45             : c1: CONTACT_MATRIX GROUP=1-7 SWITCH={RATIONAL R_0=2.6 NN=6 MM=12}
      46             : ones: ONES SIZE=7
      47             : cc: MATRIX_VECTOR_PRODUCT ARG=c1,ones
      48             : PRINT ARG=cc FILE=colvar
      49             : ```
      50             : 
      51             : For more information on why this is useful see [CONTACT_MATRIX](CONTACT_MATRIX.md)
      52             : 
      53             : */
      54             : //+ENDPLUMEDOC
      55             : 
      56             : class Ones : public ActionShortcut {
      57             : public:
      58             :   static void registerKeywords(Keywords& keys);
      59             :   explicit Ones(const ActionOptions&);
      60             : };
      61             : 
      62             : PLUMED_REGISTER_ACTION(Ones,"ONES")
      63             : 
      64         587 : void Ones::registerKeywords(Keywords& keys) {
      65         587 :   ActionShortcut::registerKeywords( keys );
      66         587 :   keys.add("compulsory","SIZE","the number of ones that you would like to create");
      67        1174 :   keys.setValueDescription("scalar/vector/matrix","a vector (or a matrix, if SIZE has two values) of ones with the required number of elements");
      68         587 :   keys.needsAction("CONSTANT");
      69         587 : }
      70             : 
      71         363 : Ones::Ones(const ActionOptions& ao):
      72             :   Action(ao),
      73         363 :   ActionShortcut(ao) {
      74             :   unsigned size;
      75         363 :   std::string matstr = "";
      76         363 :   std::vector<std::string> size_str;
      77         726 :   parseVector("SIZE",size_str);
      78         363 :   if( size_str.size()==2 ) {
      79             :     unsigned nr, nc;
      80           0 :     Tools::convert( size_str[0], nr );
      81           0 :     Tools::convert( size_str[1], nc );
      82           0 :     size = nr*nc;
      83           0 :     matstr = " NROWS=" + size_str[0] + " NCOLS=" + size_str[1];
      84         363 :   } else if( size_str[0]=="@natoms" ) {
      85           2 :     if( size_str.size()!=1 ) {
      86           0 :       error("should only be one @natoms string in input");
      87             :     }
      88           2 :     std::vector<Value*> xpos, ypos, zpos, masv, chargev;
      89           2 :     ActionAtomistic::getAtomValuesFromPlumedObject( plumed, xpos, ypos, zpos, masv, chargev );
      90           2 :     size = 0;
      91           4 :     for(unsigned i=0; i<xpos.size(); ++i ) {
      92           2 :       size += xpos[i]->getNumberOfValues();
      93             :     }
      94           2 :   } else {
      95         361 :     if( size_str.size()!=1 ) {
      96           0 :       error("size should be one or two values");
      97             :     }
      98         361 :     Tools::convert( size_str[0], size );
      99             :   }
     100         363 :   if( size<1 ) {
     101           0 :     error("size should be greater than 0");
     102             :   }
     103         363 :   std::string ones="1";
     104      148985 :   for(unsigned i=1; i<size; ++i) {
     105             :     ones +=",1";
     106             :   }
     107         726 :   readInputLine( getShortcutLabel() + ": CONSTANT NOLOG VALUES=" + ones + matstr );
     108         726 : }
     109             : 
     110             : }
     111             : }

Generated by: LCOV version 1.16