LCOV - code coverage report
Current view: top level - setup - Units.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 51 51 100.0 %
Date: 2019-08-13 10:39:37 Functions: 9 10 90.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2019 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/ActionSetup.h"
      23             : #include "core/ActionRegister.h"
      24             : #include "core/PlumedMain.h"
      25             : #include "core/Atoms.h"
      26             : #include "tools/Exception.h"
      27             : 
      28             : using namespace std;
      29             : 
      30             : namespace PLMD {
      31             : namespace setup {
      32             : 
      33             : //+PLUMEDOC GENERIC UNITS
      34             : /*
      35             : This command sets the internal units for the code.  A new unit can be set by either
      36             : specifying how to convert from the plumed default unit into that new unit or by using
      37             : the shortcuts described below.  This directive MUST appear at the BEGINNING of the
      38             : plumed.dat file.  The same units must be used througout the plumed.dat file.
      39             : 
      40             : Notice that all input/output will then be made using the specified units.
      41             : That is: all the input parameters, all the output files, etc. The only
      42             : exceptions are file formats for which there is a specific convention concerning
      43             : the units. For example, trajectories written in .gro format (with \ref DUMPATOMS)
      44             : are going to be always in nm.
      45             : 
      46             : \par Examples
      47             : 
      48             : \plumedfile
      49             : # this is using nm - kj/mol - fs
      50             : UNITS LENGTH=A TIME=fs
      51             : 
      52             : # compute distance between atoms 1 and 4
      53             : d: DISTANCE ATOMS=1,4
      54             : 
      55             : # print time and distance on a COLVAR file
      56             : PRINT ARG=d FILE=COLVAR
      57             : 
      58             : # dump atoms 1 to 100 on a 'out.gro' file
      59             : DUMPATOMS FILE=out.gro STRIDE=10 ATOMS=1-100
      60             : 
      61             : # dump atoms 1 to 100 on a 'out.xyz' file
      62             : DUMPATOMS FILE=out.xyz STRIDE=10 ATOMS=1-100
      63             : \endplumedfile
      64             : 
      65             : In the `COLVAR` file, time and distance will appear in fs and A respectively, *irrespectively* of which units
      66             : you are using the the host MD code. The coordinates in the `out.gro` file will be expressed in nm,
      67             : since `gro` files are by convention written in nm. The coordinates in the `out.xyz` file
      68             : will be written in Angstrom *since we used the UNITS command setting Angstrom units*.
      69             : Indeed, within PLUMED xyz files are using internal PLUMED units and not necessarily Angstrom!
      70             : 
      71             : If a number, x, is found instead of a string, the new unit is equal to x times the default units.
      72             : Using the following command as first line of the previous example would have lead to an identical result:
      73             : \plumedfile
      74             : UNITS LENGTH=0.1 TIME=0.001
      75             : \endplumedfile
      76             : 
      77             : */
      78             : //+ENDPLUMEDOC
      79             : 
      80          24 : class Units :
      81             :   public virtual ActionSetup
      82             : {
      83             : public:
      84             :   static void registerKeywords( Keywords& keys );
      85             :   explicit Units(const ActionOptions&ao);
      86             : };
      87             : 
      88        4833 : PLUMED_REGISTER_ACTION(Units,"UNITS")
      89             : 
      90          13 : void Units::registerKeywords( Keywords& keys ) {
      91          13 :   ActionSetup::registerKeywords(keys);
      92          13 :   keys.add("optional","LENGTH","the units of lengths.  Either specify a conversion factor from the default, nm, or A (for angstroms) or um");
      93          13 :   keys.add("optional","ENERGY","the units of energy.  Either specify a conversion factor from the default, kj/mol, or use j/mol or kcal/mol");
      94          13 :   keys.add("optional","TIME","the units of time.  Either specify a conversion factor from the default, ps, or use ns or fs");
      95          13 :   keys.add("optional","MASS","the units of masses.  Specify a conversion factor from the default, amu");
      96          13 :   keys.add("optional","CHARGE","the units of charges.  Specify a conversion factor from the default, e");
      97          13 :   keys.addFlag("NATURAL",false,"use natural units");
      98          13 : }
      99             : 
     100          12 : Units::Units(const ActionOptions&ao):
     101             :   Action(ao),
     102          12 :   ActionSetup(ao)
     103             : {
     104          12 :   PLMD::Units u;
     105             : 
     106          24 :   std::string s;
     107             : 
     108          12 :   s="";
     109          12 :   parse("LENGTH",s);
     110          12 :   if(s.length()>0) u.setLength(s);
     111          12 :   if(u.getLengthString().length()>0) log.printf("  length: %s\n",u.getLengthString().c_str());
     112           2 :   else                               log.printf("  length: %f nm\n",u.getLength());
     113             : 
     114          12 :   s="";
     115          12 :   parse("ENERGY",s);
     116          12 :   if(s.length()>0) u.setEnergy(s);
     117          12 :   if(u.getEnergyString().length()>0) log.printf("  energy: %s\n",u.getEnergyString().c_str());
     118           2 :   else                               log.printf("  energy: %f kj/mol\n",u.getEnergy());
     119             : 
     120          12 :   s="";
     121          12 :   parse("TIME",s);
     122          12 :   if(s.length()>0) u.setTime(s);
     123          12 :   if(u.getTimeString().length()>0) log.printf("  time: %s\n",u.getTimeString().c_str());
     124           2 :   else                             log.printf("  time: %f ps\n",u.getTime());
     125             : 
     126          12 :   s="";
     127          12 :   parse("CHARGE",s);
     128          12 :   if(s.length()>0) u.setCharge(s);
     129          12 :   if(u.getChargeString().length()>0) log.printf("  charge: %s\n",u.getChargeString().c_str());
     130           2 :   else                               log.printf("  charge: %f e\n",u.getCharge());
     131             : 
     132          12 :   s="";
     133          12 :   parse("MASS",s);
     134          12 :   if(s.length()>0) u.setMass(s);
     135          12 :   if(u.getMassString().length()>0) log.printf("  mass: %s\n",u.getMassString().c_str());
     136           1 :   else                             log.printf("  mass: %f amu\n",u.getMass());
     137             : 
     138          12 :   bool natural=false;
     139          12 :   parseFlag("NATURAL",natural);
     140          12 :   plumed.getAtoms().setNaturalUnits(natural);
     141             : 
     142          12 :   checkRead();
     143             : 
     144          12 :   plumed.getAtoms().setUnits(u);
     145          12 :   if(natural) {
     146           5 :     log.printf("  using natural units\n");
     147             :   } else {
     148           7 :     log.printf("  using physical units\n");
     149             :   }
     150          24 :   log.printf("  inside PLUMED, Boltzmann constant is %f\n",plumed.getAtoms().getKBoltzmann());
     151          12 : }
     152             : 
     153             : }
     154        4821 : }
     155             : 

Generated by: LCOV version 1.13