All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Dipole.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 "Colvar.h"
23 #include "ActionRegister.h"
24 
25 #include <string>
26 #include <cmath>
27 
28 using namespace std;
29 
30 namespace PLMD{
31 namespace colvar{
32 
33 //+PLUMEDOC COLVAR DIPOLE
34 /*
35 Calcualte the dipole moment for a group of atoms.
36 
37 \par Examples
38 The following tells plumed to calculate the dipole of the group of atoms containing
39 the atoms from 1-10 and print it every 5 steps
40 \verbatim
41 d: DIPOLE GROUP=1-10
42 PRINT FILE=output STRIDE=5 ARG=5
43 \endverbatim
44 (see also \ref PRINT)
45 
46 \attention
47 If the total charge Q of the group in non zero, then a charge Q/N will be subtracted to every atom,
48 where N is the number of atoms. This implies that the dipole (which for a charged system depends
49 on the position) is computed on the geometric center of the group.
50 
51 
52 */
53 //+ENDPLUMEDOC
54 
55 class Dipole : public Colvar {
56  vector<AtomNumber> ga_lista;
57 public:
58  Dipole(const ActionOptions&);
59  virtual void calculate();
60  static void registerKeywords(Keywords& keys);
61 };
62 
63 PLUMED_REGISTER_ACTION(Dipole,"DIPOLE")
64 
65 void Dipole::registerKeywords(Keywords& keys){
66  Colvar::registerKeywords(keys);
67  keys.add("atoms","GROUP","the group of atoms we are calculating the dipole moment for");
68  keys.remove("NOPBC");
69 }
70 
71 Dipole::Dipole(const ActionOptions&ao):
73 {
74  parseAtomList("GROUP",ga_lista);
75  checkRead();
77 
78  log.printf(" of %d atoms\n",ga_lista.size());
79  for(unsigned int i=0;i<ga_lista.size();++i){
80  log.printf(" %d", ga_lista[i].serial());
81  }
82  log.printf(" \n");
84 }
85 
86 // calculator
88 {
89  double dipole=0.;
90  vector<Vector> deriv(getNumberOfAtoms());
91  Vector dipje;
92  vector<double> charges(getNumberOfAtoms());
93 
94  double ctot(0.0);
95  for(unsigned i=0;i<charges.size();++i){
96  charges[i]=getCharge(i);
97  ctot+=charges[i];
98  }
99 
100  ctot/=charges.size();
101 
102  for(unsigned i=0;i<charges.size();++i) charges[i]-=ctot;
103 
104 // deriv.resize(getPositions().size());
105 // deriv.resize(getNumberOfAtoms());
106  for(unsigned int i=0;i<ga_lista.size();i++) {
107  dipje += charges[i]*getPosition(i);
108  }
109  dipole = dipje.modulo();
110 
111  for(unsigned int i=0;i<ga_lista.size();i++) {
112  double dfunc=charges[i]/dipole;
113  deriv[i] = deriv[i] + (dfunc)*dipje;
114  }
115 
116 // for(unsigned i=0;i<getPositions().size();++i) setAtomsDerivatives(i,deriv[i]);
117  for(unsigned i=0;i<getNumberOfAtoms();++i) setAtomsDerivatives(i,deriv[i]);
118  setValue (dipole);
120 }
121 
122 }
123 }
const Vector & getPosition(int) const
Get position of i-th atom.
void setNotPeriodic()
Set your default value to have no periodicity.
Log & log
Reference to the log stream.
Definition: Action.h:93
double modulo() const
Compute the modulo.
Definition: Vector.h:325
Class implementing fixed size vectors of doubles.
Definition: Vector.h:74
void setAtomsDerivatives(int, const Vector &)
Definition: Colvar.h:97
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 addValueWithDerivatives()
Add a value with the name label that has derivatives.
void requestAtoms(const std::vector< AtomNumber > &a)
Definition: Colvar.cpp:44
#define PLUMED_COLVAR_INIT(ao)
Definition: Colvar.h:29
virtual void calculate()
Calculate an Action.
Definition: Dipole.cpp:87
This class holds the keywords and their documentation.
Definition: Keywords.h:36
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
This is the abstract base class to use for implementing new collective variables, within it there is ...
Definition: Colvar.h:39
void setValue(const double &d)
Set the default value (the one without name)
double getCharge(int i) const
Get charge of i-th atom.
void setBoxDerivativesNoPbc()
Set box derivatives automatically.
Definition: Colvar.h:107
Provides the keyword DIPOLE
Definition: Dipole.cpp:55
vector< AtomNumber > ga_lista
Definition: Dipole.cpp:56
unsigned getNumberOfAtoms() const
Get number of available atoms.
std::vector< double > charges