LCOV - code coverage report
Current view: top level - generic - DumpMassCharge.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 66 68 97.1 %
Date: 2026-03-30 13:16:06 Functions: 11 13 84.6 %

          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             : #include "core/Atoms.h"
      28             : 
      29             : namespace PLMD {
      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             :   std::string file;
      80             :   bool first;
      81             :   bool second;
      82             :   bool print_masses;
      83             :   bool print_charges;
      84             : public:
      85             :   explicit DumpMassCharge(const ActionOptions&);
      86             :   ~DumpMassCharge();
      87             :   static void registerKeywords( Keywords& keys );
      88             :   void prepare() override;
      89          84 :   void calculate() override {}
      90          84 :   void apply() override {}
      91             :   void update() override;
      92             : };
      93             : 
      94       13809 : PLUMED_REGISTER_ACTION(DumpMassCharge,"DUMPMASSCHARGE")
      95             : 
      96          16 : void DumpMassCharge::registerKeywords( Keywords& keys ) {
      97          16 :   Action::registerKeywords( keys );
      98          16 :   ActionPilot::registerKeywords( keys );
      99          16 :   ActionAtomistic::registerKeywords( keys );
     100          32 :   keys.add("compulsory","STRIDE","1","the frequency with which the atoms should be output");
     101          32 :   keys.add("atoms", "ATOMS", "the atom indices whose charges and masses you would like to print out");
     102          32 :   keys.add("compulsory", "FILE", "file on which to output charges and masses.");
     103          32 :   keys.addFlag("ONLY_MASSES",false,"Only output masses to file");
     104          32 :   keys.addFlag("ONLY_CHARGES",false,"Only output charges to file");
     105          16 : }
     106             : 
     107          12 : DumpMassCharge::DumpMassCharge(const ActionOptions&ao):
     108             :   Action(ao),
     109             :   ActionAtomistic(ao),
     110             :   ActionPilot(ao),
     111          12 :   first(true),
     112          12 :   second(true),
     113          12 :   print_masses(true),
     114          12 :   print_charges(true) {
     115             :   std::vector<AtomNumber> atoms;
     116          24 :   parse("FILE",file);
     117          12 :   if(file.length()==0) {
     118           0 :     error("name of output file was not specified");
     119             :   }
     120          12 :   log.printf("  output written to file %s\n",file.c_str());
     121             : 
     122          24 :   parseAtomList("ATOMS",atoms);
     123             : 
     124          12 :   if(atoms.size()==0) {
     125         720 :     for(int i=0; i<plumed.getAtoms().getNatoms(); i++) {
     126         712 :       atoms.push_back(AtomNumber::index(i));
     127             :     }
     128             :   }
     129             : 
     130          12 :   bool only_masses = false;
     131          12 :   parseFlag("ONLY_MASSES",only_masses);
     132          12 :   if(only_masses) {
     133           1 :     print_charges = false;
     134           1 :     log.printf("  only masses will be written to file\n");
     135             :   }
     136             : 
     137          12 :   bool only_charges = false;
     138          12 :   parseFlag("ONLY_CHARGES",only_charges);
     139          12 :   if(only_charges) {
     140           1 :     print_masses = false;
     141           1 :     log.printf("  only charges will be written to file\n");
     142             :   }
     143             : 
     144             : 
     145          12 :   checkRead();
     146             : 
     147          12 :   log.printf("  printing the following atoms:" );
     148        1006 :   for(unsigned i=0; i<atoms.size(); ++i) {
     149         994 :     log.printf(" %d",atoms[i].serial() );
     150             :   }
     151          12 :   log.printf("\n");
     152          12 :   requestAtoms(atoms);
     153             : 
     154          12 :   if(only_masses && only_charges) {
     155           0 :     plumed_merror("using both ONLY_MASSES and ONLY_CHARGES doesn't make sense");
     156             :   }
     157             : 
     158          12 : }
     159             : 
     160          84 : void DumpMassCharge::prepare() {
     161          84 :   if(!first && second) {
     162          12 :     requestAtoms(std::vector<AtomNumber>());
     163          12 :     second=false;
     164             :   }
     165          84 : }
     166             : 
     167          84 : void DumpMassCharge::update() {
     168          84 :   if(!first) {
     169          72 :     return;
     170             :   }
     171          12 :   first=false;
     172             : 
     173          12 :   OFile of;
     174          12 :   of.link(*this);
     175          12 :   of.open(file);
     176             : 
     177        1006 :   for(unsigned i=0; i<getNumberOfAtoms(); i++) {
     178         994 :     int ii=getAbsoluteIndex(i).index();
     179         994 :     of.printField("index",ii);
     180         994 :     if(print_masses) {
     181        1772 :       of.printField("mass",getMass(i));
     182             :     }
     183         994 :     if(print_charges) {
     184        1772 :       of.printField("charge",getCharge(i));
     185             :     }
     186         994 :     of.printField();
     187             :   }
     188          12 : }
     189             : 
     190          24 : DumpMassCharge::~DumpMassCharge() {
     191          24 : }
     192             : 
     193             : 
     194             : }
     195             : }

Generated by: LCOV version 1.16