LCOV - code coverage report
Current view: top level - setup - Units.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 62 64 96.9 %
Date: 2026-03-30 13:16:06 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             : public:
      93             :   static void registerKeywords( Keywords& keys );
      94             :   explicit Units(const ActionOptions&ao);
      95             : };
      96             : 
      97       13825 : PLUMED_REGISTER_ACTION(Units,"UNITS")
      98             : 
      99          24 : void Units::registerKeywords( Keywords& keys ) {
     100          24 :   ActionSetup::registerKeywords(keys);
     101          48 :   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.");
     102          48 :   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)");
     103          48 :   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");
     104          48 :   keys.add("optional","MASS","the units of masses.  Specify a conversion factor from the default, amu");
     105          48 :   keys.add("optional","CHARGE","the units of charges.  Specify a conversion factor from the default, e");
     106          48 :   keys.addFlag("NATURAL",false,"use natural units");
     107          24 : }
     108             : 
     109          20 : Units::Units(const ActionOptions&ao):
     110             :   Action(ao),
     111          20 :   ActionSetup(ao) {
     112          20 :   PLMD::Units u;
     113             : 
     114             :   std::string s;
     115             : 
     116             :   s="";
     117          40 :   parse("LENGTH",s);
     118          20 :   if(s.length()>0) {
     119          12 :     u.setLength(s);
     120             :   }
     121          38 :   if(u.getLengthString().length()>0 && u.getLengthString()=="nm") {
     122           8 :     log.printf("  length: %s\n",u.getLengthString().c_str());
     123          22 :   } else if(u.getLengthString().length()>0 && u.getLengthString()!="nm") {
     124          10 :     log.printf("  length: %s = %g nm\n",u.getLengthString().c_str(),u.getLength());
     125             :   } else {
     126           2 :     log.printf("  length: %g nm\n",u.getLength());
     127             :   }
     128             : 
     129             :   s="";
     130          40 :   parse("ENERGY",s);
     131          20 :   if(s.length()>0) {
     132           7 :     u.setEnergy(s);
     133             :   }
     134          38 :   if(u.getEnergyString().length()>0 && u.getEnergyString()=="kj/mol") {
     135          13 :     log.printf("  energy: %s\n",u.getEnergyString().c_str());
     136          12 :   } else if(u.getEnergyString().length()>0 && u.getEnergyString()!="kj/mol") {
     137           5 :     log.printf("  energy: %s = %g kj/mol\n",u.getEnergyString().c_str(),u.getEnergy());
     138             :   } else {
     139           2 :     log.printf("  energy: %g kj/mol\n",u.getEnergy());
     140             :   }
     141             : 
     142             :   s="";
     143          40 :   parse("TIME",s);
     144          20 :   if(s.length()>0) {
     145           7 :     u.setTime(s);
     146             :   }
     147          38 :   if(u.getTimeString().length()>0 && u.getTimeString()=="ps") {
     148          15 :     log.printf("  time: %s\n",u.getTimeString().c_str());
     149           8 :   } else if(u.getTimeString().length()>0 && u.getTimeString()!="ps") {
     150           3 :     log.printf("  time: %s = %g ps\n",u.getTimeString().c_str(),u.getTime());
     151             :   } else {
     152           2 :     log.printf("  time: %g ps\n",u.getTime());
     153             :   }
     154             : 
     155             :   s="";
     156          40 :   parse("CHARGE",s);
     157          20 :   if(s.length()>0) {
     158           3 :     u.setCharge(s);
     159             :   }
     160          38 :   if(u.getChargeString().length()>0 && u.getChargeString()=="e") {
     161          18 :     log.printf("  charge: %s\n",u.getChargeString().c_str());
     162           2 :   } else if(u.getChargeString().length()>0 && u.getChargeString()!="e") {
     163           0 :     log.printf("  charge: %s = %g e\n",u.getChargeString().c_str(),u.getCharge());
     164             :   } else {
     165           2 :     log.printf("  charge: %g e\n",u.getCharge());
     166             :   }
     167             : 
     168             :   s="";
     169          40 :   parse("MASS",s);
     170          20 :   if(s.length()>0) {
     171           2 :     u.setMass(s);
     172             :   }
     173          39 :   if(u.getMassString().length()>0 && u.getMassString()=="amu") {
     174          19 :     log.printf("  mass: %s\n",u.getMassString().c_str());
     175           1 :   } else if(u.getMassString().length()>0 && u.getMassString()!="amu") {
     176           0 :     log.printf("  mass: %s = %g amu\n",u.getMassString().c_str(),u.getMass());
     177             :   } else {
     178           1 :     log.printf("  mass: %g amu\n",u.getMass());
     179             :   }
     180             : 
     181          20 :   bool natural=false;
     182          20 :   parseFlag("NATURAL",natural);
     183          20 :   plumed.getAtoms().setNaturalUnits(natural);
     184             : 
     185          20 :   checkRead();
     186             : 
     187          20 :   plumed.getAtoms().setUnits(u);
     188          20 :   if(natural) {
     189           6 :     log.printf("  using natural units\n");
     190             :   } else {
     191          14 :     log.printf("  using physical units\n");
     192             :   }
     193          20 :   log.printf("  inside PLUMED, Boltzmann constant is %g\n",plumed.getAtoms().getKBoltzmann());
     194             : 
     195          20 :   plumed.getAtoms().updateUnits();
     196          20 : }
     197             : 
     198             : }
     199             : }

Generated by: LCOV version 1.16