LCOV - code coverage report
Current view: top level - setup - Units.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 57 59 96.6 %
Date: 2024-10-11 08:09:47 Functions: 5 6 83.3 %

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

Generated by: LCOV version 1.15