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