LCOV - code coverage report
Current view: top level - setup - Units.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 59 61 96.7 %
Date: 2021-11-18 15:22:58 Functions: 8 10 80.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2011-2020 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.
      36             : 
      37             : A new unit can be set by either
      38             : specifying a conversion factor from the plumed default unit or by using a string
      39             : corresponding to one of the defined units given below.  This directive MUST
      40             : appear at the BEGINNING of the plumed.dat file.  The same units must be used
      41             : throughout the plumed.dat file.
      42             : 
      43             : Notice that all input/output will then be made using the specified units.
      44             : That is: all the input parameters, all the output files, etc. The only
      45             : exceptions are file formats for which there is a specific convention concerning
      46             : the units. For example, trajectories written in .gro format (with \ref DUMPATOMS)
      47             : are going to be always in nm.
      48             : 
      49             : The following strings can be used to specify units. Note that the strings are
      50             : case sensitive.
      51             : - LENGTH: nm (default), A (for Angstrom), um (for micrometer), Bohr (0.052917721067 nm)
      52             : - ENERGY: kj/mol (default), j/mol, kcal/mol (4.184 kj/mol), eV (96.48530749925792 kj/mol), Ha (for Hartree, 2625.499638 kj/mol)
      53             : - TIME: ps (default), fs, ns, atomic (2.418884326509e-5 ps)
      54             : - MASS: amu (default)
      55             : - CHARGE: e (default)
      56             : 
      57             : 
      58             : \par Examples
      59             : 
      60             : \plumedfile
      61             : # this is using Angstrom - kj/mol - fs
      62             : UNITS LENGTH=A TIME=fs
      63             : 
      64             : # compute distance between atoms 1 and 4
      65             : d: DISTANCE ATOMS=1,4
      66             : 
      67             : # print time and distance on a COLVAR file
      68             : PRINT ARG=d FILE=COLVAR
      69             : 
      70             : # dump atoms 1 to 100 on a 'out.gro' file
      71             : DUMPATOMS FILE=out.gro STRIDE=10 ATOMS=1-100
      72             : 
      73             : # dump atoms 1 to 100 on a 'out.xyz' file
      74             : DUMPATOMS FILE=out.xyz STRIDE=10 ATOMS=1-100
      75             : \endplumedfile
      76             : 
      77             : In the `COLVAR` file, time and distance will appear in fs and A respectively, *irrespective* of which units
      78             : you are using in the host MD code. The coordinates in the `out.gro` file will be expressed in nm,
      79             : since `gro` files are by convention written in nm. The coordinates in the `out.xyz` file
      80             : will be written in Angstrom *since we used the UNITS command setting Angstrom units*.
      81             : Indeed, within PLUMED xyz files are using internal PLUMED units and not necessarily Angstrom!
      82             : 
      83             : If a number, x, is found instead of a string, the new unit is equal to x times the default units.
      84             : Using the following command as first line of the previous example would have lead to an identical result:
      85             : \plumedfile
      86             : UNITS LENGTH=0.1 TIME=0.001
      87             : \endplumedfile
      88             : 
      89             : */
      90             : //+ENDPLUMEDOC
      91             : 
      92          30 : class Units :
      93             :   public virtual ActionSetup
      94             : {
      95             : public:
      96             :   static void registerKeywords( Keywords& keys );
      97             :   explicit Units(const ActionOptions&ao);
      98             : };
      99             : 
     100        7386 : PLUMED_REGISTER_ACTION(Units,"UNITS")
     101             : 
     102          16 : void Units::registerKeywords( Keywords& keys ) {
     103          16 :   ActionSetup::registerKeywords(keys);
     104          64 :   keys.add("optional","LENGTH","the units of lengths.  Either specify a conversion factor from the default, nm, or use one of the defined units, A (for angstroms), um (for micrometer), and Bohr.");
     105          64 :   keys.add("optional","ENERGY","the units of energy.  Either specify a conversion factor from the default, kj/mol, or use one of the defined units, j/mol, kcal/mol and Ha (for Hartree)");
     106          64 :   keys.add("optional","TIME","the units of time.  Either specify a conversion factor from the default, ps, or use one of the defined units, ns, fs, and atomic");
     107          64 :   keys.add("optional","MASS","the units of masses.  Specify a conversion factor from the default, amu");
     108          64 :   keys.add("optional","CHARGE","the units of charges.  Specify a conversion factor from the default, e");
     109          48 :   keys.addFlag("NATURAL",false,"use natural units");
     110          16 : }
     111             : 
     112          15 : Units::Units(const ActionOptions&ao):
     113             :   Action(ao),
     114          15 :   ActionSetup(ao)
     115             : {
     116          30 :   PLMD::Units u;
     117             : 
     118             :   std::string s;
     119             : 
     120             :   s="";
     121          30 :   parse("LENGTH",s);
     122          15 :   if(s.length()>0) u.setLength(s);
     123          28 :   if(u.getLengthString().length()>0 && u.getLengthString()=="nm") {
     124          16 :     log.printf("  length: %s\n",u.getLengthString().c_str());
     125             :   }
     126          12 :   else if(u.getLengthString().length()>0 && u.getLengthString()!="nm") {
     127          10 :     log.printf("  length: %s = %g nm\n",u.getLengthString().c_str(),u.getLength());
     128             :   }
     129             :   else {
     130           2 :     log.printf("  length: %g nm\n",u.getLength());
     131             :   }
     132             : 
     133             :   s="";
     134          30 :   parse("ENERGY",s);
     135          15 :   if(s.length()>0) u.setEnergy(s);
     136          28 :   if(u.getEnergyString().length()>0 && u.getEnergyString()=="kj/mol") {
     137          24 :     log.printf("  energy: %s\n",u.getEnergyString().c_str());
     138             :   }
     139           4 :   else if(u.getEnergyString().length()>0 && u.getEnergyString()!="kj/mol") {
     140           2 :     log.printf("  energy: %s = %g kj/mol\n",u.getEnergyString().c_str(),u.getEnergy());
     141             :   }
     142             :   else {
     143           2 :     log.printf("  energy: %g kj/mol\n",u.getEnergy());
     144             :   }
     145             : 
     146             :   s="";
     147          30 :   parse("TIME",s);
     148          15 :   if(s.length()>0) u.setTime(s);
     149          28 :   if(u.getTimeString().length()>0 && u.getTimeString()=="ps") {
     150          24 :     log.printf("  time: %s\n",u.getTimeString().c_str());
     151             :   }
     152           4 :   else if(u.getTimeString().length()>0 && u.getTimeString()!="ps") {
     153           2 :     log.printf("  time: %s = %g ps\n",u.getTimeString().c_str(),u.getTime());
     154             :   }
     155             :   else {
     156           2 :     log.printf("  time: %g ps\n",u.getTime());
     157             :   }
     158             : 
     159             :   s="";
     160          30 :   parse("CHARGE",s);
     161          15 :   if(s.length()>0) u.setCharge(s);
     162          28 :   if(u.getChargeString().length()>0 && u.getChargeString()=="e") {
     163          26 :     log.printf("  charge: %s\n",u.getChargeString().c_str());
     164             :   }
     165           2 :   else if(u.getChargeString().length()>0 && u.getChargeString()!="e") {
     166           0 :     log.printf("  charge: %s = %g e\n",u.getChargeString().c_str(),u.getCharge());
     167             :   }
     168             :   else {
     169           2 :     log.printf("  charge: %g e\n",u.getCharge());
     170             :   }
     171             : 
     172             :   s="";
     173          30 :   parse("MASS",s);
     174          15 :   if(s.length()>0) u.setMass(s);
     175          29 :   if(u.getMassString().length()>0 && u.getMassString()=="amu") {
     176          14 :     log.printf("  mass: %s\n",u.getMassString().c_str());
     177             :   }
     178           1 :   else if(u.getMassString().length()>0 && u.getMassString()!="amu") {
     179           0 :     log.printf("  mass: %s = %g amu\n",u.getMassString().c_str(),u.getMass());
     180             :   }
     181             :   else {
     182           1 :     log.printf("  mass: %g amu\n",u.getMass());
     183             :   }
     184             : 
     185          15 :   bool natural=false;
     186          30 :   parseFlag("NATURAL",natural);
     187          30 :   plumed.getAtoms().setNaturalUnits(natural);
     188             : 
     189          15 :   checkRead();
     190             : 
     191          15 :   plumed.getAtoms().setUnits(u);
     192          15 :   if(natural) {
     193           6 :     log.printf("  using natural units\n");
     194             :   } else {
     195           9 :     log.printf("  using physical units\n");
     196             :   }
     197          30 :   log.printf("  inside PLUMED, Boltzmann constant is %g\n",plumed.getAtoms().getKBoltzmann());
     198             : 
     199          30 :   plumed.getAtoms().updateUnits();
     200          15 : }
     201             : 
     202             : }
     203        5517 : }

Generated by: LCOV version 1.14