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 809538 : Units::Units():
28 809538 : energy(1.0),
29 809538 : energyString("kj/mol"),
30 809538 : length(1.0),
31 809538 : lengthString("nm"),
32 809538 : time(1.0),
33 809538 : timeString("ps"),
34 809538 : charge(1.0),
35 809538 : chargeString("e"),
36 809538 : mass(1.0),
37 809538 : massString("amu")
38 : {
39 809538 : }
40 :
41 13 : void Units::setEnergy(const std::string &s) {
42 13 : energyString=s;
43 13 : if(s=="kj/mol") {
44 5 : energy=1.0;
45 8 : } else if(s=="kcal/mol") {
46 5 : 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 13 : }
62 :
63 33 : void Units::setLength(const std::string &s) {
64 33 : lengthString=s;
65 33 : if(s=="nm") {
66 2 : length=1.0;
67 31 : } else if(s=="A") {
68 24 : length=0.1;
69 7 : } else if(s=="um") {
70 0 : length=1000.0;
71 7 : } else if(s=="Bohr") {
72 1 : length=0.052917721067;
73 : } else {
74 6 : length=-1.0;
75 : lengthString="";
76 6 : 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 6 : plumed_massert(length>0.0,"length unit should be positive");
80 : }
81 33 : }
82 :
83 7 : void Units::setTime(const std::string &s) {
84 7 : timeString=s;
85 7 : if(s=="ps") {
86 2 : 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 7 : }
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 718 : void Units::setLength(const double s) {
137 718 : lengthString="";
138 718 : length=s;
139 718 : }
140 :
141 6 : void Units::setTime(const double s) {
142 6 : timeString="";
143 6 : time=s;
144 6 : }
145 :
146 718 : void Units::setCharge(const double s) {
147 718 : chargeString="";
148 718 : charge=s;
149 718 : }
150 :
151 718 : void Units::setMass(const double s) {
152 718 : massString="";
153 718 : mass=s;
154 718 : }
155 :
156 :
157 :
158 : }
|