All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
COM.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 COM
33 /*
34 Calculate the center of mass for a group of atoms.
35 
36 The computed
37 center of mass is stored as a virtual atom that can be accessed in
38 an atom list through the label for the COM action that creates it.
39 
40 For arbitrary weights (e.g. geometric center) see \ref CENTER.
41 
42 \par Examples
43 
44 The following input instructs plumed to print the distance between the
45 center of mass for atoms 1,2,3,4,5,6,7 and that for atoms 15,20:
46 \verbatim
47 COM ATOMS=1-7 LABEL=c1
48 COM ATOMS=15,20 LABEL=c2
49 DISTANCE ATOMS=c1,c2 LABEL=d1
50 PRINT ARG=d1
51 \endverbatim
52 (See also \ref DISTANCE and \ref PRINT).
53 
54 */
55 //+ENDPLUMEDOC
56 
57 
58 class COM:
60 {
61 public:
62  COM(const ActionOptions&ao);
63  void calculate();
64  static void registerKeywords( Keywords& keys );
65 };
66 
67 PLUMED_REGISTER_ACTION(COM,"COM")
68 
69 void COM::registerKeywords(Keywords& keys){
70  ActionWithVirtualAtom::registerKeywords(keys);
71 }
72 
73 COM::COM(const ActionOptions&ao):
74  Action(ao),
76 {
77  vector<AtomNumber> atoms;
78  parseAtomList("ATOMS",atoms);
79  if(atoms.size()==0) error("at least one atom should be specified");
80  checkRead();
81  log.printf(" of atoms");
82  for(unsigned i=0;i<atoms.size();++i) log.printf(" %d",atoms[i].serial());
83  log.printf("\n");
84  requestAtoms(atoms);
85 }
86 
88  Vector pos;
89  double mass(0.0);
90  vector<Tensor> deriv(getNumberOfAtoms());
91  for(unsigned i=0;i<getNumberOfAtoms();i++) mass+=getMass(i);
92  if( plumed.getAtoms().chargesWereSet() ){
93  double charge(0.0);
94  for(unsigned i=0;i<getNumberOfAtoms();i++) charge+=getCharge(i);
95  setCharge(charge);
96  } else {
97  setCharge(0.0);
98  }
99  for(unsigned i=0;i<getNumberOfAtoms();i++){
100  pos+=(getMass(i)/mass)*getPosition(i);
101  deriv[i]=(getMass(i)/mass)*Tensor::identity();
102  }
103  setPosition(pos);
104  setMass(mass);
105  setAtomsDerivatives(deriv);
106 }
107 
108 }
109 }
const Vector & getPosition(int) const
Get position of i-th atom.
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.
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
Inherit from here if you are calculating the position of a virtual atom (eg a center of mass) ...
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.
void calculate()
Calculate an Action.
Definition: COM.cpp:87
Provides the keyword COM
Definition: COM.cpp:58