LCOV - code coverage report
Current view: top level - generic - DumpMassCharge.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 61 63 96.8 %
Date: 2024-10-18 13:59:31 Functions: 8 11 72.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-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/ActionAtomistic.h"
      23             : #include "core/ActionPilot.h"
      24             : #include "core/ActionRegister.h"
      25             : #include "tools/File.h"
      26             : #include "core/PlumedMain.h"
      27             : 
      28             : namespace PLMD
      29             : {
      30             : namespace generic {
      31             : 
      32             : //+PLUMEDOC PRINTANALYSIS DUMPMASSCHARGE
      33             : /*
      34             : Dump masses and charges on a selected file.
      35             : 
      36             : This command dumps a file containing charges and masses.
      37             : It does so only once in the simulation (at first step).
      38             : File can be recycled in the \ref driver tool.
      39             : 
      40             : Notice that masses and charges are only written once at the beginning
      41             : of the simulation. In case no atom list is provided, charges and
      42             : masses for all atoms are written.
      43             : 
      44             : \par Examples
      45             : 
      46             : You can add the DUMPMASSCHARGE action at the end of the plumed.dat
      47             : file that you use during an MD simulations:
      48             : 
      49             : \plumedfile
      50             : c1: COM ATOMS=1-10
      51             : c2: COM ATOMS=11-20
      52             : DUMPATOMS ATOMS=c1,c2 FILE=coms.xyz STRIDE=100
      53             : 
      54             : DUMPMASSCHARGE FILE=mcfile
      55             : \endplumedfile
      56             : 
      57             : In this way, you will be able to use the same masses while processing
      58             : a trajectory from the \ref driver . To do so, you need to
      59             : add the --mc flag on the driver command line, e.g.
      60             : \verbatim
      61             : plumed driver --mc mcfile --plumed plumed.dat --ixyz traj.xyz
      62             : \endverbatim
      63             : 
      64             : With the following input you can dump only the charges for a specific
      65             : group:
      66             : 
      67             : \plumedfile
      68             : solute_ions: GROUP ATOMS=1-121,200-2012
      69             : DUMPATOMS FILE=traj.gro ATOMS=solute_ions STRIDE=100
      70             : DUMPMASSCHARGE FILE=mcfile ATOMS=solute_ions
      71             : \endplumedfile
      72             : 
      73             : */
      74             : //+ENDPLUMEDOC
      75             : 
      76             : class DumpMassCharge:
      77             :   public ActionAtomistic,
      78             :   public ActionPilot
      79             : {
      80             :   std::string file;
      81             :   bool first;
      82             :   bool second;
      83             :   bool print_masses;
      84             :   bool print_charges;
      85             : public:
      86             :   explicit DumpMassCharge(const ActionOptions&);
      87             :   ~DumpMassCharge();
      88             :   static void registerKeywords( Keywords& keys );
      89           0 :   bool actionHasForces() override { return false; }
      90             :   void prepare() override;
      91          94 :   void calculate() override {}
      92          94 :   void apply() override {}
      93             :   void update() override;
      94             : };
      95             : 
      96             : PLUMED_REGISTER_ACTION(DumpMassCharge,"DUMPMASSCHARGE")
      97             : 
      98          16 : void DumpMassCharge::registerKeywords( Keywords& keys ) {
      99          16 :   Action::registerKeywords( keys );
     100          16 :   ActionPilot::registerKeywords( keys );
     101          16 :   ActionAtomistic::registerKeywords( keys );
     102          32 :   keys.add("compulsory","STRIDE","1","the frequency with which the atoms should be output");
     103          32 :   keys.add("atoms", "ATOMS", "the atom indices whose charges and masses you would like to print out");
     104          32 :   keys.add("compulsory", "FILE", "file on which to output charges and masses.");
     105          32 :   keys.addFlag("ONLY_MASSES",false,"Only output masses to file");
     106          32 :   keys.addFlag("ONLY_CHARGES",false,"Only output charges to file");
     107          16 : }
     108             : 
     109          14 : DumpMassCharge::DumpMassCharge(const ActionOptions&ao):
     110             :   Action(ao),
     111             :   ActionAtomistic(ao),
     112             :   ActionPilot(ao),
     113          14 :   first(true),
     114          14 :   second(true),
     115          14 :   print_masses(true),
     116          14 :   print_charges(true)
     117             : {
     118             :   std::vector<AtomNumber> atoms;
     119          28 :   parse("FILE",file);
     120          14 :   if(file.length()==0) error("name of output file was not specified");
     121          14 :   log.printf("  output written to file %s\n",file.c_str());
     122             : 
     123          28 :   parseAtomList("ATOMS",atoms);
     124             : 
     125          14 :   if(atoms.size()==0) {
     126          10 :     std::vector<std::string> strvec(1); strvec[0]="@mdatoms"; interpretAtomList( strvec,atoms );
     127          10 :   }
     128             : 
     129          14 :   bool only_masses = false;
     130          14 :   parseFlag("ONLY_MASSES",only_masses);
     131          14 :   if(only_masses) {
     132           1 :     print_charges = false;
     133           1 :     log.printf("  only masses will be written to file\n");
     134             :   }
     135             : 
     136          14 :   bool only_charges = false;
     137          14 :   parseFlag("ONLY_CHARGES",only_charges);
     138          14 :   if(only_charges) {
     139           1 :     print_masses = false;
     140           1 :     log.printf("  only charges will be written to file\n");
     141             :   }
     142             : 
     143             : 
     144          14 :   checkRead();
     145             : 
     146          14 :   log.printf("  printing the following atoms:" );
     147        1224 :   for(unsigned i=0; i<atoms.size(); ++i) log.printf(" %d",atoms[i].serial() );
     148          14 :   log.printf("\n");
     149          14 :   requestAtoms(atoms);
     150             : 
     151          14 :   if(only_masses && only_charges) {
     152           0 :     plumed_merror("using both ONLY_MASSES and ONLY_CHARGES doesn't make sense");
     153             :   }
     154             : 
     155          14 : }
     156             : 
     157          94 : void DumpMassCharge::prepare() {
     158          94 :   if(!first && second) {
     159          14 :     requestAtoms(std::vector<AtomNumber>());
     160          14 :     second=false;
     161             :   }
     162          94 : }
     163             : 
     164          94 : void DumpMassCharge::update() {
     165          94 :   if(!first) return;
     166          14 :   first=false;
     167             : 
     168          14 :   OFile of;
     169          14 :   of.link(*this);
     170          14 :   of.open(file);
     171             : 
     172        1224 :   for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     173        1210 :     int ii=getAbsoluteIndex(i).index();
     174        1210 :     of.printField("index",ii);
     175        2312 :     if(print_masses) {of.printField("mass",getMass(i));}
     176        2312 :     if(print_charges) {of.printField("charge",getCharge(i));}
     177        1210 :     of.printField();
     178             :   }
     179          14 : }
     180             : 
     181          28 : DumpMassCharge::~DumpMassCharge() {
     182          28 : }
     183             : 
     184             : 
     185             : }
     186             : }

Generated by: LCOV version 1.16