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: 2025-11-25 13:55:50 Functions: 2 3 66.7 %

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

Generated by: LCOV version 1.16