LCOV - code coverage report
Current view: top level - generic - PrintNDX.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 56 62 90.3 %
Date: 2024-10-18 13:59:31 Functions: 9 13 69.2 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-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/ActionPilot.h"
      23             : #include "core/ActionWithArguments.h"
      24             : #include "core/ActionAtomistic.h"
      25             : #include "core/ActionRegister.h"
      26             : #include "tools/CheckInRange.h"
      27             : 
      28             : //+PLUMEDOC PRINTANALYSIS PRINT_NDX
      29             : /*
      30             : Print an ndx file
      31             : 
      32             : \par Examples
      33             : 
      34             : */
      35             : //+ENDPLUMEDOC
      36             : 
      37             : namespace PLMD {
      38             : namespace generic {
      39             : 
      40             : class PrintNDX :
      41             :   public ActionPilot,
      42             :   public ActionAtomistic,
      43             :   public ActionWithArguments
      44             : {
      45             :   std::string file;
      46             :   OFile ofile;
      47             :   CheckInRange bounds;
      48             : public:
      49           4 :   void calculate() override {}
      50             :   std::string writeInGraph() const override;
      51             :   explicit PrintNDX(const ActionOptions&);
      52             :   static void registerKeywords(Keywords& keys);
      53           0 :   bool actionHasForces() override { return false; }
      54           0 :   void calculateNumericalDerivatives( ActionWithValue* a=NULL ) override { plumed_error(); }
      55             :   void lockRequests() override;
      56             :   void unlockRequests() override;
      57           4 :   void apply() override {}
      58             :   void update() override;
      59          12 :   ~PrintNDX() {}
      60             : };
      61             : 
      62             : PLUMED_REGISTER_ACTION(PrintNDX,"PRINT_NDX")
      63             : 
      64           8 : void PrintNDX::registerKeywords(Keywords& keys) {
      65           8 :   Action::registerKeywords(keys);
      66           8 :   ActionPilot::registerKeywords(keys);
      67           8 :   ActionAtomistic::registerKeywords( keys );
      68           8 :   ActionWithArguments::registerKeywords(keys);
      69          16 :   keys.addInputKeyword("optional","ARG","vector","the labels of vectors that should be used when printind the NDX file");
      70          16 :   keys.add("atoms","ATOMS","the list of atoms that have the corresponding arguments");
      71          16 :   keys.add("compulsory","STRIDE","1","the frequency with which the quantities of interest should be output");
      72          16 :   keys.add("optional","FILE","the name of the file on which to output these quantities");
      73          16 :   keys.add("optional","LESS_THAN_OR_EQUAL","when printing with arguments that are vectors only print components of vectors have a value less than or equal to this value");
      74          16 :   keys.add("optional","GREATER_THAN_OR_EQUAL","when printing with arguments that are vectors only print components of vectors have a value greater than or equal to this value");
      75           8 :   keys.use("RESTART");
      76           8 :   keys.use("UPDATE_FROM");
      77           8 :   keys.use("UPDATE_UNTIL");
      78           8 : }
      79             : 
      80           4 : PrintNDX::PrintNDX(const ActionOptions&ao):
      81             :   Action(ao),
      82             :   ActionPilot(ao),
      83             :   ActionAtomistic(ao),
      84           4 :   ActionWithArguments(ao)
      85             : {
      86           4 :   ofile.link(*this);
      87           8 :   parse("FILE",file);
      88           4 :   if(file.length()>0) {
      89           4 :     ofile.open(file);
      90           4 :     log.printf("  on file %s\n",file.c_str());
      91             :   } else {
      92           0 :     log.printf("  on plumed log file\n");
      93           0 :     ofile.link(log);
      94             :   }
      95           8 :   std::vector<AtomNumber> all_atoms; parseAtomList("ATOMS",all_atoms); std::vector<std::string> argnames( getNumberOfArguments() );
      96           4 :   requestAtoms( all_atoms, false );
      97           8 :   for(unsigned i=0; i<getNumberOfArguments(); ++i) {
      98           4 :     if( getPntrToArgument(i)->getRank()!=1 || getPntrToArgument(i)->hasDerivatives() ) error("arguments for print ndx should be vector");
      99           4 :     if( getPntrToArgument(i)->getShape()[0]!=all_atoms.size() ) error("mismatch between number of arguments and number of input atoms");
     100           4 :     getPntrToArgument(i)->buildDataStore(true); argnames[i] = getPntrToArgument(i)->getName();
     101             :   }
     102           4 :   log.printf("  printing ndx file containing indices of atoms that have arguments in ranges prescribed below \n");
     103           4 :   log.printf("  full set of atom indices investigated are : ");
     104        7988 :   for(unsigned int i=0; i<all_atoms.size(); ++i) {
     105        7984 :     if ( (i+1) % 25 == 0 ) log.printf("  \n");
     106        7984 :     log.printf("  %d", all_atoms[i].serial());
     107             :   }
     108           4 :   log.printf("\n"); std::vector<std::string> str_upper, str_lower; std::string errors;
     109          12 :   parseVector("LESS_THAN_OR_EQUAL",str_upper); parseVector("GREATER_THAN_OR_EQUAL",str_lower);
     110           4 :   if( !bounds.setBounds( getNumberOfArguments(), str_lower, str_upper, errors ) ) error( errors );
     111           8 :   if( bounds.wereSet() ) log.printf("  %s \n", bounds.report( argnames ).c_str() );
     112           4 :   checkRead();
     113           8 : }
     114             : 
     115           0 : std::string PrintNDX::writeInGraph() const {
     116           0 :   return getName() + "\n" + "FILE=" + file;
     117             : }
     118             : 
     119           4 : void PrintNDX::lockRequests() {
     120             :   ActionWithArguments::lockRequests();
     121             :   ActionAtomistic::lockRequests();
     122           4 : }
     123             : 
     124           4 : void PrintNDX::unlockRequests() {
     125             :   ActionWithArguments::unlockRequests();
     126             :   ActionAtomistic::unlockRequests();
     127           4 : }
     128             : 
     129           4 : void PrintNDX::update() {
     130           4 :   unsigned n=0; std::vector<double> argvals( getNumberOfArguments() );
     131           4 :   ofile.printf("[ %s step %d ] \n", getLabel().c_str(), getStep() );
     132        7988 :   for(unsigned i=0; i<getNumberOfAtoms(); ++i) {
     133       15968 :     for(unsigned j=0; j<getNumberOfArguments(); ++j) argvals[j] = getPntrToArgument(j)->get(i);
     134        7984 :     if( bounds.check( argvals ) ) {
     135         156 :       ofile.printf("%6d", getAbsoluteIndexes()[i].serial() ); n++;
     136         156 :       if( n%15==0 ) ofile.printf("\n");
     137             :     }
     138             :   }
     139           4 :   ofile.printf("\n");
     140           4 : }
     141             : 
     142             : }
     143             : 
     144             : 
     145             : }

Generated by: LCOV version 1.16