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