Line data Source code
1 : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2 : Copyright (c) 2011-2019 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 "core/Atoms.h"
26 : #include "tools/Exception.h"
27 :
28 : using namespace std;
29 :
30 : namespace PLMD {
31 : namespace setup {
32 :
33 : //+PLUMEDOC GENERIC UNITS
34 : /*
35 : This command sets the internal units for the code. A new unit can be set by either
36 : specifying how to convert from the plumed default unit into that new unit or by using
37 : the shortcuts described below. This directive MUST appear at the BEGINNING of the
38 : plumed.dat file. The same units must be used througout 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 : \par Examples
47 :
48 : \plumedfile
49 : # this is using nm - kj/mol - fs
50 : UNITS LENGTH=A TIME=fs
51 :
52 : # compute distance between atoms 1 and 4
53 : d: DISTANCE ATOMS=1,4
54 :
55 : # print time and distance on a COLVAR file
56 : PRINT ARG=d FILE=COLVAR
57 :
58 : # dump atoms 1 to 100 on a 'out.gro' file
59 : DUMPATOMS FILE=out.gro STRIDE=10 ATOMS=1-100
60 :
61 : # dump atoms 1 to 100 on a 'out.xyz' file
62 : DUMPATOMS FILE=out.xyz STRIDE=10 ATOMS=1-100
63 : \endplumedfile
64 :
65 : In the `COLVAR` file, time and distance will appear in fs and A respectively, *irrespectively* of which units
66 : you are using the the host MD code. The coordinates in the `out.gro` file will be expressed in nm,
67 : since `gro` files are by convention written in nm. The coordinates in the `out.xyz` file
68 : will be written in Angstrom *since we used the UNITS command setting Angstrom units*.
69 : Indeed, within PLUMED xyz files are using internal PLUMED units and not necessarily Angstrom!
70 :
71 : If a number, x, is found instead of a string, the new unit is equal to x times the default units.
72 : Using the following command as first line of the previous example would have lead to an identical result:
73 : \plumedfile
74 : UNITS LENGTH=0.1 TIME=0.001
75 : \endplumedfile
76 :
77 : */
78 : //+ENDPLUMEDOC
79 :
80 26 : class Units :
81 : public virtual ActionSetup
82 : {
83 : public:
84 : static void registerKeywords( Keywords& keys );
85 : explicit Units(const ActionOptions&ao);
86 : };
87 :
88 6465 : PLUMED_REGISTER_ACTION(Units,"UNITS")
89 :
90 14 : void Units::registerKeywords( Keywords& keys ) {
91 14 : ActionSetup::registerKeywords(keys);
92 56 : keys.add("optional","LENGTH","the units of lengths. Either specify a conversion factor from the default, nm, or A (for angstroms) or um");
93 56 : keys.add("optional","ENERGY","the units of energy. Either specify a conversion factor from the default, kj/mol, or use j/mol or kcal/mol");
94 56 : keys.add("optional","TIME","the units of time. Either specify a conversion factor from the default, ps, or use ns or fs");
95 56 : keys.add("optional","MASS","the units of masses. Specify a conversion factor from the default, amu");
96 56 : keys.add("optional","CHARGE","the units of charges. Specify a conversion factor from the default, e");
97 42 : keys.addFlag("NATURAL",false,"use natural units");
98 14 : }
99 :
100 13 : Units::Units(const ActionOptions&ao):
101 : Action(ao),
102 13 : ActionSetup(ao)
103 : {
104 26 : PLMD::Units u;
105 :
106 : std::string s;
107 :
108 : s="";
109 26 : parse("LENGTH",s);
110 13 : if(s.length()>0) u.setLength(s);
111 24 : if(u.getLengthString().length()>0) log.printf(" length: %s\n",u.getLengthString().c_str());
112 2 : else log.printf(" length: %f nm\n",u.getLength());
113 :
114 : s="";
115 26 : parse("ENERGY",s);
116 13 : if(s.length()>0) u.setEnergy(s);
117 24 : if(u.getEnergyString().length()>0) log.printf(" energy: %s\n",u.getEnergyString().c_str());
118 2 : else log.printf(" energy: %f kj/mol\n",u.getEnergy());
119 :
120 : s="";
121 26 : parse("TIME",s);
122 13 : if(s.length()>0) u.setTime(s);
123 24 : if(u.getTimeString().length()>0) log.printf(" time: %s\n",u.getTimeString().c_str());
124 2 : else log.printf(" time: %f ps\n",u.getTime());
125 :
126 : s="";
127 26 : parse("CHARGE",s);
128 13 : if(s.length()>0) u.setCharge(s);
129 24 : if(u.getChargeString().length()>0) log.printf(" charge: %s\n",u.getChargeString().c_str());
130 2 : else log.printf(" charge: %f e\n",u.getCharge());
131 :
132 : s="";
133 26 : parse("MASS",s);
134 13 : if(s.length()>0) u.setMass(s);
135 25 : if(u.getMassString().length()>0) log.printf(" mass: %s\n",u.getMassString().c_str());
136 1 : else log.printf(" mass: %f amu\n",u.getMass());
137 :
138 13 : bool natural=false;
139 26 : parseFlag("NATURAL",natural);
140 26 : plumed.getAtoms().setNaturalUnits(natural);
141 :
142 13 : checkRead();
143 :
144 13 : plumed.getAtoms().setUnits(u);
145 13 : if(natural) {
146 5 : log.printf(" using natural units\n");
147 : } else {
148 8 : log.printf(" using physical units\n");
149 : }
150 26 : log.printf(" inside PLUMED, Boltzmann constant is %f\n",plumed.getAtoms().getKBoltzmann());
151 :
152 26 : plumed.getAtoms().updateUnits();
153 13 : }
154 :
155 : }
156 4839 : }
157 :
|