LCOV - code coverage report
Current view: top level - tools - Units.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 89 97 91.8 %
Date: 2026-11-05 10:19:04 Functions: 11 11 100.0 %

          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 "Units.h"
      23             : #include "Tools.h"
      24             : 
      25             : namespace PLMD {
      26             : 
      27     1623706 : Units::Units():
      28     1623706 :   energy(1.0),
      29     1623706 :   energyString("kj/mol"),
      30     1623706 :   length(1.0),
      31     1623706 :   lengthString("nm"),
      32     1623706 :   time(1.0),
      33     1623706 :   timeString("ps"),
      34     1623706 :   charge(1.0),
      35     1623706 :   chargeString("e"),
      36     1623706 :   mass(1.0),
      37     1623706 :   massString("amu") {
      38     1623706 : }
      39             : 
      40          42 : void Units::setEnergy(const std::string &s) {
      41          42 :   energyString=s;
      42          42 :   if(s=="kj/mol") {
      43          23 :     energy=1.0;
      44          19 :   } else if(s=="kcal/mol") {
      45          14 :     energy=4.184;
      46           5 :   } else if(s=="j/mol") {
      47           0 :     energy=0.001;
      48           5 :   } else if(s=="eV") {
      49           2 :     energy=96.48530749925792;
      50           3 :   } else if(s =="Ha") {
      51           1 :     energy=2625.499638;
      52             :   } else {
      53           2 :     energy=-1.0;
      54             :     energyString="";
      55           2 :     if(!Tools::convertNoexcept(s,energy)) {
      56           0 :       plumed_merror("problem with setting the energy unit, either use give an numerical value or use one of the defined units: kj/mol, kcal/mol, j/mol, eV, Ha (case sensitive)");
      57             :     }
      58           2 :     plumed_massert(energy>0.0,"energy unit should be positive");
      59             :   }
      60          42 : }
      61             : 
      62          80 : void Units::setLength(const std::string &s) {
      63          80 :   lengthString=s;
      64          80 :   if(s=="nm") {
      65           2 :     length=1.0;
      66          78 :   } else if(s=="A") {
      67          68 :     length=0.1;
      68          10 :   } else if(s=="um") {
      69           0 :     length=1000.0;
      70          10 :   } else if(s=="Bohr") {
      71           2 :     length=0.052917721067;
      72             :   } else {
      73           8 :     length=-1.0;
      74             :     lengthString="";
      75           8 :     if(!Tools::convertNoexcept(s,length)) {
      76           0 :       plumed_merror("problem with setting the length unit, either use a numerical value or use one of the defined units: nm, A, um, Bohr (case sensitive)");
      77             :     }
      78           8 :     plumed_massert(length>0.0,"length unit should be positive");
      79             :   }
      80          80 : }
      81             : 
      82          14 : void Units::setTime(const std::string &s) {
      83          14 :   timeString=s;
      84          14 :   if(s=="ps") {
      85           8 :     time=1.0;
      86           6 :   } else if(s=="ns") {
      87           0 :     time=1000.0;
      88           6 :   } else if(s=="fs") {
      89           3 :     time=0.001;
      90           3 :   } else if(s=="atomic") {
      91           1 :     time=2.418884326509e-5;
      92             :   } else {
      93           2 :     time=-1.0;
      94             :     timeString="";
      95           2 :     if(!Tools::convertNoexcept(s,time)) {
      96           0 :       plumed_merror("problem with setting the time unit, either use a numerical value or use one of the defined units: ps, fs, atomic (case sensitive)");
      97             :     }
      98           2 :     plumed_massert(time>0.0,"time unit should be positive");
      99             :   }
     100          14 : }
     101             : 
     102           4 : void Units::setCharge(const std::string &s) {
     103           4 :   chargeString=s;
     104           4 :   if(s=="e") {
     105           1 :     charge=1.0;
     106             :   } else {
     107           3 :     charge=-1.0;
     108             :     chargeString="";
     109           3 :     if(!Tools::convertNoexcept(s,charge)) {
     110           0 :       plumed_merror("problem with setting the charge unit, either use a numerical value or use one of the defined units: e (case sensitive)");
     111             :     }
     112           3 :     plumed_massert(charge>0.0,"charge unit should be positive");
     113             :   }
     114           4 : }
     115             : 
     116           3 : void Units::setMass(const std::string &s) {
     117           3 :   massString=s;
     118           3 :   if(s=="amu") {
     119           1 :     mass=1.0;
     120             :   } else {
     121           2 :     mass=-1.0;
     122             :     massString="";
     123           2 :     if(!Tools::convertNoexcept(s,mass)) {
     124           0 :       plumed_merror("problem with setting the mass unit, either use a numerical value or use one of the defined units: amu (case sensitive)");
     125             :     }
     126           2 :     plumed_massert(mass>0.0,"mass unit should be positive");
     127             :   }
     128           3 : }
     129             : 
     130          45 : void Units::setEnergy(const double s) {
     131          45 :   energyString="";
     132          45 :   energy=s;
     133          45 : }
     134             : 
     135        1347 : void Units::setLength(const double s) {
     136        1347 :   lengthString="";
     137        1347 :   length=s;
     138        1347 : }
     139             : 
     140           6 : void Units::setTime(const double s) {
     141           6 :   timeString="";
     142           6 :   time=s;
     143           6 : }
     144             : 
     145        1347 : void Units::setCharge(const double s) {
     146        1347 :   chargeString="";
     147        1347 :   charge=s;
     148        1347 : }
     149             : 
     150        1347 : void Units::setMass(const double s) {
     151        1347 :   massString="";
     152        1347 :   mass=s;
     153        1347 : }
     154             : 
     155             : 
     156             : 
     157             : }

Generated by: LCOV version 1.16