All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Center.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 "ActionWithVirtualAtom.h"
23 #include "ActionRegister.h"
24 #include "core/PlumedMain.h"
25 #include "core/Atoms.h"
26 
27 using namespace std;
28 
29 namespace PLMD{
30 namespace vatom{
31 
32 //+PLUMEDOC VATOM CENTER
33 /*
34 Calculate the center for a group of atoms, with arbitrary weights.
35 
36 The computed
37 center is stored as a virtual atom that can be accessed in
38 an atom list through the label for the CENTER action that creates it.
39 Notice that the generated virtual atom has charge equal to the sum of the
40 charges and mass equal to the sum of the masses. If used with the MASS flag,
41 then it provides a result identical to \ref COM.
42 
43 \par Examples
44 
45 \verbatim
46 # a point which is on the line connecting atoms 1 and 10, so that its distance
47 # from 10 is twice its distance from 1:
48 c1: CENTER ATOMS=1,1,10
49 # this is another way of stating the same:
50 c1bis: CENTER ATOMS=1,10 WEIGHTS=2,1
51 
52 # center of mass among these atoms:
53 c2: CENTER ATOMS=2,3,4,5 MASS
54 
55 d1: DISTANCE ATOMS=c1,c2
56 
57 PRINT ARG=d1
58 \endverbatim
59 (See also \ref DISTANCE, \ref COM and \ref PRINT).
60 
61 */
62 //+ENDPLUMEDOC
63 
64 
65 class Center:
67 {
68  std::vector<double> weights;
70 public:
71  Center(const ActionOptions&ao);
72  void calculate();
73  static void registerKeywords( Keywords& keys );
74 };
75 
76 PLUMED_REGISTER_ACTION(Center,"CENTER")
77 
78 void Center::registerKeywords(Keywords& keys){
79  ActionWithVirtualAtom::registerKeywords(keys);
80  keys.add("optional","WEIGHTS","Center is computed as a weighted average.");
81  keys.addFlag("MASS",false,"If set center is mass weighted");
82 }
83 
84 Center::Center(const ActionOptions&ao):
85  Action(ao),
87  weight_mass(false)
88 {
89  vector<AtomNumber> atoms;
90  parseAtomList("ATOMS",atoms);
91  if(atoms.size()==0) error("at least one atom should be specified");
92  parseVector("WEIGHTS",weights);
93  parseFlag("MASS",weight_mass);
94  checkRead();
95  log.printf(" of atoms");
96  for(unsigned i=0;i<atoms.size();++i) log.printf(" %d",atoms[i].serial());
97  if(weight_mass){
98  log<<" mass weighted\n";
99  if(weights.size()!=0) error("WEIGHTS and MASS keywords should not be used simultaneously");
100  } else {
101  if( weights.size()==0) {
102  weights.resize( atoms.size() );
103  for(unsigned i=0;i<atoms.size();i++) weights[i] = 1.;
104  }
105  log<<" with weights";
106  if( weights.size()!=atoms.size() ) error("number of elements in weight vector does not match the number of atoms");
107  for(unsigned i=0;i<weights.size();++i) log.printf(" %f",weights[i]);
108  log.printf("\n");
109  }
110  requestAtoms(atoms);
111 }
112 
114  Vector pos;
115  double mass(0.0);
116  vector<Tensor> deriv(getNumberOfAtoms());
117  for(unsigned i=0;i<getNumberOfAtoms();i++) mass+=getMass(i);
118  if( plumed.getAtoms().chargesWereSet() ){
119  double charge(0.0);
120  for(unsigned i=0;i<getNumberOfAtoms();i++) charge+=getCharge(i);
121  setCharge(charge);
122  } else {
123  setCharge(0.0);
124  }
125  double wtot=0.0;
126  for(unsigned i=0;i<weights.size();i++) wtot+=weights[i];
127  for(unsigned i=0;i<getNumberOfAtoms();i++){
128  double w=0;
129  if(weight_mass) w=getMass(i)/mass;
130  else w=weights[i]/wtot;
131  pos+=w*getPosition(i);
132  deriv[i]=w*Tensor::identity();
133  }
134  setPosition(pos);
135  setMass(mass);
136  setAtomsDerivatives(deriv);
137 }
138 
139 }
140 }
std::vector< double > weights
Definition: Center.cpp:68
const Vector & getPosition(int) const
Get position of i-th atom.
void parseFlag(const std::string &key, bool &t)
Parse one keyword as boolean flag.
Definition: Action.cpp:104
Log & log
Reference to the log stream.
Definition: Action.h:93
void setPosition(const Vector &)
Set position of the virtual atom.
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
void error(const std::string &msg) const
Crash calculation and print documentation.
Definition: Action.cpp:195
void checkRead()
Check if Action was properly read.
Definition: Action.cpp:161
STL namespace.
void parseAtomList(const std::string &key, std::vector< AtomNumber > &t)
Parse a list of atoms without a numbered keyword.
void setMass(double)
Set its mass.
void requestAtoms(const std::vector< AtomNumber > &a)
Request atoms on which the calculation depends.
void calculate()
Calculate an Action.
Definition: Center.cpp:113
static TensorGeneric< n, n > identity()
return an identity tensor
Definition: Tensor.h:318
This class holds the keywords and their documentation.
Definition: Keywords.h:36
void setAtomsDerivatives(const std::vector< Tensor > &d)
Set the derivatives of virtual atom coordinate wrt atoms on which it dependes.
This class is used to bring the relevant information to the Action constructor.
Definition: Action.h:41
int printf(const char *fmt,...)
Formatted output with explicit format - a la printf.
Definition: OFile.cpp:82
Base class for all the input Actions.
Definition: Action.h:60
void parseVector(const std::string &key, std::vector< T > &t)
Parse one keyword as std::vector.
Definition: Action.h:311
Inherit from here if you are calculating the position of a virtual atom (eg a center of mass) ...
Provides the keyword CENTER
Definition: Center.cpp:65
void const char const char int double int double double int int double int double * w
Definition: Matrix.h:42
double getCharge(int i) const
Get charge of i-th atom.
void setCharge(double)
Set its charge.
Main plumed object.
Definition: Plumed.h:201
unsigned getNumberOfAtoms() const
Get number of available atoms.
double getMass(int i) const
Get mass of i-th atom.