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 : A new unit can be set by either
35 : specifying a conversion factor from the plumed default unit or by using a string
36 : corresponding to one of the defined units given below. This directive MUST
37 : appear at the BEGINNING of the plumed.dat file. The same units must be used
38 : throughout the plumed.dat file.
39 :
40 : Notice that all input/output will then be made using the specified units.
41 : That is: all the input parameters, all the output files, etc. The only
42 : exceptions are file formats for which there is a specific convention concerning
43 : the units. For example, trajectories written in .gro format (with \ref DUMPATOMS)
44 : are going to be always in nm.
45 :
46 : The following strings can be used to specify units. Note that the strings are
47 : case sensitive.
48 : - LENGTH: nm (default), A (for Angstrom), um (for micrometer), Bohr (0.052917721067 nm)
49 : - ENERGY: kj/mol (default), j/mol, kcal/mol (4.184 kj/mol), eV (96.48530749925792 kj/mol), Ha (for Hartree, 2625.499638 kj/mol)
50 : - TIME: ps (default), fs, ns, atomic (2.418884326509e-5 ps)
51 : - MASS: amu (default)
52 : - CHARGE: e (default)
53 :
54 :
55 : \par Examples
56 :
57 : \plumedfile
58 : # this is using Angstrom - kj/mol - fs
59 : UNITS LENGTH=A TIME=fs
60 :
61 : # compute distance between atoms 1 and 4
62 : d: DISTANCE ATOMS=1,4
63 :
64 : # print time and distance on a COLVAR file
65 : PRINT ARG=d FILE=COLVAR
66 :
67 : # dump atoms 1 to 100 on a 'out.gro' file
68 : DUMPATOMS FILE=out.gro STRIDE=10 ATOMS=1-100
69 :
70 : # dump atoms 1 to 100 on a 'out.xyz' file
71 : DUMPATOMS FILE=out.xyz STRIDE=10 ATOMS=1-100
72 : \endplumedfile
73 :
74 : In the `COLVAR` file, time and distance will appear in fs and A respectively, *irrespective* of which units
75 : you are using in the host MD code. The coordinates in the `out.gro` file will be expressed in nm,
76 : since `gro` files are by convention written in nm. The coordinates in the `out.xyz` file
77 : will be written in Angstrom *since we used the UNITS command setting Angstrom units*.
78 : Indeed, within PLUMED xyz files are using internal PLUMED units and not necessarily Angstrom!
79 :
80 : If a number, x, is found instead of a string, the new unit is equal to x times the default units.
81 : Using the following command as first line of the previous example would have lead to an identical result:
82 : \plumedfile
83 : UNITS LENGTH=0.1 TIME=0.001
84 : \endplumedfile
85 :
86 : */
87 : //+ENDPLUMEDOC
88 :
89 : class Units :
90 : public virtual ActionSetup
91 : {
92 : public:
93 : static void registerKeywords( Keywords& keys );
94 : explicit Units(const ActionOptions&ao);
95 : };
96 :
97 : PLUMED_REGISTER_ACTION(Units,"UNITS")
98 :
99 36 : void Units::registerKeywords( Keywords& keys ) {
100 36 : ActionSetup::registerKeywords(keys);
101 72 : 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.");
102 72 : 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)");
103 72 : 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");
104 72 : keys.add("optional","MASS","the units of masses. Specify a conversion factor from the default, amu");
105 72 : keys.add("optional","CHARGE","the units of charges. Specify a conversion factor from the default, e");
106 72 : keys.addFlag("NATURAL",false,"use natural units");
107 36 : }
108 :
109 34 : Units::Units(const ActionOptions&ao):
110 : Action(ao),
111 34 : ActionSetup(ao)
112 : {
113 34 : PLMD::Units u;
114 :
115 : std::string s;
116 :
117 : s="";
118 68 : parse("LENGTH",s);
119 34 : if(s.length()>0) u.setLength(s);
120 66 : if(u.getLengthString().length()>0 && u.getLengthString()=="nm") {
121 8 : log.printf(" length: %s\n",u.getLengthString().c_str());
122 : }
123 50 : else if(u.getLengthString().length()>0 && u.getLengthString()!="nm") {
124 24 : log.printf(" length: %s = %g nm\n",u.getLengthString().c_str(),u.getLength());
125 : }
126 : else {
127 2 : log.printf(" length: %g nm\n",u.getLength());
128 : }
129 :
130 : s="";
131 68 : parse("ENERGY",s);
132 34 : if(s.length()>0) u.setEnergy(s);
133 66 : if(u.getEnergyString().length()>0 && u.getEnergyString()=="kj/mol") {
134 21 : log.printf(" energy: %s\n",u.getEnergyString().c_str());
135 : }
136 24 : else if(u.getEnergyString().length()>0 && u.getEnergyString()!="kj/mol") {
137 11 : log.printf(" energy: %s = %g kj/mol\n",u.getEnergyString().c_str(),u.getEnergy());
138 : }
139 : else {
140 2 : log.printf(" energy: %g kj/mol\n",u.getEnergy());
141 : }
142 :
143 : s="";
144 68 : parse("TIME",s);
145 34 : if(s.length()>0) u.setTime(s);
146 66 : if(u.getTimeString().length()>0 && u.getTimeString()=="ps") {
147 29 : log.printf(" time: %s\n",u.getTimeString().c_str());
148 : }
149 8 : else if(u.getTimeString().length()>0 && u.getTimeString()!="ps") {
150 3 : log.printf(" time: %s = %g ps\n",u.getTimeString().c_str(),u.getTime());
151 : }
152 : else {
153 2 : log.printf(" time: %g ps\n",u.getTime());
154 : }
155 :
156 : s="";
157 68 : parse("CHARGE",s);
158 34 : if(s.length()>0) u.setCharge(s);
159 66 : if(u.getChargeString().length()>0 && u.getChargeString()=="e") {
160 32 : log.printf(" charge: %s\n",u.getChargeString().c_str());
161 : }
162 2 : else if(u.getChargeString().length()>0 && u.getChargeString()!="e") {
163 0 : log.printf(" charge: %s = %g e\n",u.getChargeString().c_str(),u.getCharge());
164 : }
165 : else {
166 2 : log.printf(" charge: %g e\n",u.getCharge());
167 : }
168 :
169 : s="";
170 68 : parse("MASS",s);
171 34 : if(s.length()>0) u.setMass(s);
172 67 : if(u.getMassString().length()>0 && u.getMassString()=="amu") {
173 33 : log.printf(" mass: %s\n",u.getMassString().c_str());
174 : }
175 1 : else if(u.getMassString().length()>0 && u.getMassString()!="amu") {
176 0 : log.printf(" mass: %s = %g amu\n",u.getMassString().c_str(),u.getMass());
177 : }
178 : else {
179 1 : log.printf(" mass: %g amu\n",u.getMass());
180 : }
181 :
182 34 : bool natural=false;
183 34 : parseFlag("NATURAL",natural);
184 :
185 34 : checkRead();
186 :
187 34 : if(natural) {
188 6 : log.printf(" using natural units\n");
189 : } else {
190 28 : log.printf(" using physical units\n");
191 : }
192 34 : log.printf(" inside PLUMED, Boltzmann constant is %g\n",getKBoltzmann());
193 :
194 34 : plumed.setUnits(natural,u);
195 34 : }
196 :
197 : }
198 : }
|