All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Units.cpp
Go to the documentation of this file.
1 /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
2  Copyright (c) 2013 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-code.org for more information.
6 
7  This file is part of plumed, version 2.0.
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 \verbatim
48 # this is using nm - kj/mol - fs
49 UNITS LENGTH=nm TIME=fs
50 \endverbatim
51 If a number, x, is found, the new unit is equal to x (default units)
52 \verbatim
53 # this is using nm - kj/mol - fs
54 UNITS LENGTH=nm TIME=0.001
55 \endverbatim
56 
57 
58 */
59 //+ENDPLUMEDOC
60 
61 class Units :
62  public virtual ActionSetup
63 {
64 public:
65  static void registerKeywords( Keywords& keys );
66  Units(const ActionOptions&ao);
67 };
68 
69 PLUMED_REGISTER_ACTION(Units,"UNITS")
70 
71 void Units::registerKeywords( Keywords& keys ){
72  ActionSetup::registerKeywords(keys);
73  keys.add("optional","LENGTH","the units of lengths. Either specify a conversion factor from the default, nm, or A (for angstroms) or um");
74  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");
75  keys.add("optional","TIME","the units of time. Either specify a conversion factor from the default, ps, or use ns or fs");
76  keys.addFlag("NATURAL",false,"use natural units");
77 }
78 
79 Units::Units(const ActionOptions&ao):
80 Action(ao),
81 ActionSetup(ao)
82 {
83  PLMD::Units u;
84 
85  std::string s;
86 
87  s="";
88  parse("LENGTH",s);
89  if(s.length()>0) u.setLength(s);
90  if(u.getLengthString().length()>0) log.printf(" length: %s\n",u.getLengthString().c_str());
91  else log.printf(" length: %f nm\n",u.getLength());
92 
93  s="";
94  parse("ENERGY",s);
95  if(s.length()>0) u.setEnergy(s);
96  if(u.getEnergyString().length()>0) log.printf(" energy: %s\n",u.getEnergyString().c_str());
97  else log.printf(" energy: %f kj/mol\n",u.getEnergy());
98 
99  s="";
100  parse("TIME",s);
101  if(s.length()>0) u.setTime(s);
102  if(u.getTimeString().length()>0) log.printf(" time: %s\n",u.getTimeString().c_str());
103  else log.printf(" time: %f ps\n",u.getTime());
104 
105  bool natural=false;
106  parseFlag("NATURAL",natural);
107  plumed.getAtoms().setNaturalUnits(natural);
108 
109  checkRead();
110 
111  plumed.getAtoms().setUnits(u);
112  if(natural){
113  log.printf(" using natural units\n");
114  } else {
115  log.printf(" using physical units\n");
116  }
117  log.printf(" inside PLUMED, Boltzmann constant is %f\n",plumed.getAtoms().getKBoltzmann());
118 }
119 
120 }
121 }
122 
void setTime(const std::string &)
Set time units from string.
Definition: Units.cpp:73
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
const std::string & getTimeString() const
Get time units as string.
Definition: Units.h:115
Log & log
Reference to the log stream.
Definition: Action.h:93
const std::string & getLengthString() const
Get length units as string.
Definition: Units.h:110
void setLength(const std::string &)
Set lengh units from string.
Definition: Units.cpp:57
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
Provides the keyword UNITS
Definition: Units.cpp:61
Action used to create a PLMD::Action that do something during setup only e.g.
Definition: ActionSetup.h:33
void parse(const std::string &key, T &t)
Parse one keyword as generic type.
Definition: Action.h:273
This class holds the keywords and their documentation.
Definition: Keywords.h:36
Small utility class that contains information about units.
Definition: Units.h:41
const std::string & getEnergyString() const
Get energy units as string.
Definition: Units.h:105
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
void setEnergy(const std::string &)
Set energy units from string.
Definition: Units.cpp:39
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
const double & getEnergy() const
Get energy units as double.
Definition: Units.h:90
const double & getTime() const
Get time units as double.
Definition: Units.h:100
Base class for all the input Actions.
Definition: Action.h:60
const double & getLength() const
Get length units as double.
Definition: Units.h:95
Main plumed object.
Definition: Plumed.h:201