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

Generated by: LCOV version 1.15