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-03-25 09:33:27 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             : The default units in the plumed input and output files are discussed [here](parsing.md). If you would like
      35             : to use units that are different from these defaults you can use the UNITS command. This directive MUST
      36             : appear at the BEGINNING of the plumed.dat file and same units must be used throughout the plumed.dat file.
      37             : 
      38             : The following input demonstrates one way that you can use the UNITS command to change the units.
      39             : 
      40             : ```plumed
      41             : # this is using Angstrom - kj/mol - fs
      42             : UNITS LENGTH=A TIME=fs
      43             : 
      44             : # compute distance between atoms 1 and 4
      45             : d: DISTANCE ATOMS=1,4
      46             : 
      47             : # print time and distance on a COLVAR file
      48             : # times and distances in this file will be in fs and A respectively
      49             : PRINT ARG=d FILE=COLVAR
      50             : 
      51             : # dump atoms 1 to 100 on a 'out.gro' file
      52             : # The positions in the gro file here will be written
      53             : # in nm because this is the convention for gro files.
      54             : DUMPATOMS FILE=out.gro STRIDE=10 ATOMS=1-100
      55             : 
      56             : # dump atoms 1 to 100 on a 'out.xyz' file
      57             : # The position in the xyz file here will be written
      58             : # in A as there is no conventional units for xyz files
      59             : # and the plumed length units has been set to Angstroms.
      60             : DUMPATOMS FILE=out.xyz STRIDE=10 ATOMS=1-100
      61             : ```
      62             : 
      63             : In the example input above strings are used to set the units. The following table details all the strings
      64             : that can be used when setting units.  Note that the strings are case sensitive.
      65             : 
      66             : | Quantity | Default | Alternatives |
      67             : |:---------|:--------|:-------------|
      68             : | Length   | nm      | A (for Angstrom), um (for micrometer), Bohr (0.052917721067 nm) |
      69             : | Energy   | kj/mol  | j/mol, kcal/mol (4.184 kj/mol), eV (96.48530749925792 kj/mol), Ha (for Hartree, 2625.499638 kj/mol) |
      70             : | Time     | ps      | fs, ns, atomic (2.418884326509e-5 ps) |
      71             : | Mass     | amu     | |
      72             : | Charge   | e       | |
      73             : 
      74             : If you want to use a unit that is not specified in the table above you can specify a factor for converting the default unit to
      75             : your chosen unit.  As an example of how this works in practice the example below converts lengths and times into A and fs by
      76             : giving explicit values for the conversion factors.
      77             : 
      78             : ```plumed
      79             : UNITS LENGTH=0.1 TIME=0.001
      80             : ```
      81             : 
      82             : The input above tells PLUMED that lengths in nm should be divided by 0.1 so as to convert them to Anstroms. Times in ps
      83             : should be divded by 0.001 to convert them from ps to fs.
      84             : 
      85             : To reitterate, the default or the units specified in the UNITS command are used in all input and output
      86             : files.  The only exceptions are file formats for which there is a specific convention concerning
      87             : the units. For example, trajectories written in .gro format (with [DUMPATOMS](DUMPATOMS.md)) will
      88             : always in nm.
      89             : 
      90             : */
      91             : //+ENDPLUMEDOC
      92             : 
      93             : class Units :
      94             :   public virtual ActionSetup {
      95             : public:
      96             :   static void registerKeywords( Keywords& keys );
      97             :   explicit Units(const ActionOptions&ao);
      98             : };
      99             : 
     100             : PLUMED_REGISTER_ACTION(Units,"UNITS")
     101             : 
     102          36 : void Units::registerKeywords( Keywords& keys ) {
     103          36 :   ActionSetup::registerKeywords(keys);
     104          36 :   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          36 :   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          36 :   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          36 :   keys.add("optional","MASS","the units of masses.  Specify a conversion factor from the default, amu");
     108          36 :   keys.add("optional","CHARGE","the units of charges.  Specify a conversion factor from the default, e");
     109          36 :   keys.addFlag("NATURAL",false,"use natural units");
     110          36 : }
     111             : 
     112          34 : Units::Units(const ActionOptions&ao):
     113             :   Action(ao),
     114          34 :   ActionSetup(ao) {
     115          34 :   PLMD::Units u;
     116             : 
     117             :   std::string s;
     118             : 
     119             :   s="";
     120          68 :   parse("LENGTH",s);
     121          34 :   if(s.length()>0) {
     122          26 :     u.setLength(s);
     123             :   }
     124          66 :   if(u.getLengthString().length()>0 && u.getLengthString()=="nm") {
     125           8 :     log.printf("  length: %s\n",u.getLengthString().c_str());
     126          50 :   } else if(u.getLengthString().length()>0 && u.getLengthString()!="nm") {
     127          24 :     log.printf("  length: %s = %g nm\n",u.getLengthString().c_str(),u.getLength());
     128             :   } else {
     129           2 :     log.printf("  length: %g nm\n",u.getLength());
     130             :   }
     131             : 
     132             :   s="";
     133          68 :   parse("ENERGY",s);
     134          34 :   if(s.length()>0) {
     135          13 :     u.setEnergy(s);
     136             :   }
     137          66 :   if(u.getEnergyString().length()>0 && u.getEnergyString()=="kj/mol") {
     138          21 :     log.printf("  energy: %s\n",u.getEnergyString().c_str());
     139          24 :   } else if(u.getEnergyString().length()>0 && u.getEnergyString()!="kj/mol") {
     140          11 :     log.printf("  energy: %s = %g kj/mol\n",u.getEnergyString().c_str(),u.getEnergy());
     141             :   } else {
     142           2 :     log.printf("  energy: %g kj/mol\n",u.getEnergy());
     143             :   }
     144             : 
     145             :   s="";
     146          68 :   parse("TIME",s);
     147          34 :   if(s.length()>0) {
     148          13 :     u.setTime(s);
     149             :   }
     150          66 :   if(u.getTimeString().length()>0 && u.getTimeString()=="ps") {
     151          29 :     log.printf("  time: %s\n",u.getTimeString().c_str());
     152           8 :   } else if(u.getTimeString().length()>0 && u.getTimeString()!="ps") {
     153           3 :     log.printf("  time: %s = %g ps\n",u.getTimeString().c_str(),u.getTime());
     154             :   } else {
     155           2 :     log.printf("  time: %g ps\n",u.getTime());
     156             :   }
     157             : 
     158             :   s="";
     159          68 :   parse("CHARGE",s);
     160          34 :   if(s.length()>0) {
     161           3 :     u.setCharge(s);
     162             :   }
     163          66 :   if(u.getChargeString().length()>0 && u.getChargeString()=="e") {
     164          32 :     log.printf("  charge: %s\n",u.getChargeString().c_str());
     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             :   } else {
     168           2 :     log.printf("  charge: %g e\n",u.getCharge());
     169             :   }
     170             : 
     171             :   s="";
     172          68 :   parse("MASS",s);
     173          34 :   if(s.length()>0) {
     174           2 :     u.setMass(s);
     175             :   }
     176          67 :   if(u.getMassString().length()>0 && u.getMassString()=="amu") {
     177          33 :     log.printf("  mass: %s\n",u.getMassString().c_str());
     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             :   } else {
     181           1 :     log.printf("  mass: %g amu\n",u.getMass());
     182             :   }
     183             : 
     184          34 :   bool natural=false;
     185          34 :   parseFlag("NATURAL",natural);
     186             : 
     187          34 :   checkRead();
     188             : 
     189          34 :   if(natural) {
     190           6 :     log.printf("  using natural units\n");
     191             :   } else {
     192          28 :     log.printf("  using physical units\n");
     193             :   }
     194          34 :   log.printf("  inside PLUMED, Boltzmann constant is %g\n",getKBoltzmann());
     195             : 
     196          34 :   plumed.setUnits(natural,u);
     197          34 : }
     198             : 
     199             : }
     200             : }

Generated by: LCOV version 1.16