All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
Colvar.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 <vector>
24 #include <string>
25 
26 using namespace std;
27 namespace PLMD{
28 
29 Colvar::Colvar(const ActionOptions&ao):
30 Action(ao),
31 ActionAtomistic(ao),
32 ActionWithValue(ao),
33 isEnergy(false)
34 {
35 }
36 
41  keys.addFlag("NOPBC",false,"ignore the periodic boundary conditions when calculating distances");
42 }
43 
44 void Colvar::requestAtoms(const vector<AtomNumber> & a){
45  plumed_massert(!isEnergy,"request atoms should not be called if this is energy");
46 // Tell actionAtomistic what atoms we are getting
48 // Resize the derivatives of all atoms
49  for(int i=0;i<getNumberOfComponents();++i) getPntrToComponent(i)->resizeDerivatives(3*a.size()+9);
50 // Set the size of the forces array
51  forces.resize(3*getNumberOfAtoms()+9);
52 }
53 
55  vector<Vector>& f(modifyForces());
56  Tensor& v(modifyVirial());
57  unsigned nat=getNumberOfAtoms();
58 
59  for(unsigned i=0;i<f.size();i++){
60  f[i][0]=0.0;
61  f[i][1]=0.0;
62  f[i][2]=0.0;
63  }
64  v.zero();
65 
66  if(!isEnergy){
67  for(int i=0;i<getNumberOfComponents();++i){
69  for(unsigned j=0;j<nat;++j){
70  f[j][0]+=forces[3*j+0];
71  f[j][1]+=forces[3*j+1];
72  f[j][2]+=forces[3*j+2];
73  }
74  v(0,0)+=forces[3*nat+0];
75  v(0,1)+=forces[3*nat+1];
76  v(0,2)+=forces[3*nat+2];
77  v(1,0)+=forces[3*nat+3];
78  v(1,1)+=forces[3*nat+4];
79  v(1,2)+=forces[3*nat+5];
80  v(2,0)+=forces[3*nat+6];
81  v(2,1)+=forces[3*nat+7];
82  v(2,2)+=forces[3*nat+8];
83  }
84  }
85  } else if( isEnergy ){
86  forces.resize(1);
87  if( getPntrToComponent(0)->applyForce( forces ) ) modifyForceOnEnergy()+=forces[0];
88  }
89 }
90 
92  Tensor virial;
93  unsigned nat=getNumberOfAtoms();
94  for(unsigned i=0;i<nat;i++) virial-=Tensor(getPosition(i),
95  Vector(v->getDerivative(3*i+0),
96  v->getDerivative(3*i+1),
97  v->getDerivative(3*i+2)));
98  setBoxDerivatives(v,virial);
99 }
100 }
void zero()
set it to zero
Definition: Tensor.h:198
const Vector & getPosition(int) const
Get position of i-th atom.
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
static void registerKeywords(Keywords &keys)
bool applyForce(std::vector< double > &forces) const
Apply the forces to the derivatives using the chain rule (if there are no forces this routine returns...
Definition: Value.cpp:80
Class implementing fixed size matrices of doubles.
Definition: Tensor.h:70
Tensor & modifyVirial()
Get a reference to virial array.
A class for holding the value of a function together with its derivatives.
Definition: Value.h:46
bool isEnergy
Definition: Colvar.h:47
STL namespace.
void requestAtoms(const std::vector< AtomNumber > &a)
Definition: Colvar.cpp:44
void setBoxDerivatives(const Tensor &)
Definition: Colvar.h:102
Used to create a PLMD::Action that has some scalar or vectorial output that may or may not have some ...
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
Action used to create objects that access the positions of the atoms from the MD code.
Base class for all the input Actions.
Definition: Action.h:60
static void registerKeywords(Keywords &keys)
Register all the relevant keywords for the action.
Definition: Action.cpp:49
void resizeDerivatives(int n)
Set the number of derivatives.
Definition: Value.h:218
static void registerKeywords(Keywords &keys)
Definition: Colvar.cpp:37
void setBoxDerivativesNoPbc()
Set box derivatives automatically.
Definition: Colvar.h:107
void requestAtoms(const std::vector< AtomNumber > &a)
Request an array of atoms.
int getNumberOfComponents() const
Returns the number of values defined.
std::vector< double > forces
This is used by apply to retrive the forces on the atoms.
Definition: Colvar.h:45
Value * getPntrToComponent(int i)
Return a pointer to the component by index.
std::vector< Vector > & modifyForces()
Get a reference to forces array.
Tensor3d Tensor
Definition: Tensor.h:425
unsigned getNumberOfAtoms() const
Get number of available atoms.
void const char const char int double * a
Definition: Matrix.h:42
void apply()
Apply an Action.
Definition: Colvar.cpp:54
double & modifyForceOnEnergy()
Get a reference to force on energy.
Vector3d Vector
Alias for three dimensional vectors.
Definition: Vector.h:351
double getDerivative(const unsigned n) const
Get the derivative with respect to component n.
Definition: Value.h:207
void addFlag(const std::string &k, const bool def, const std::string &d)
Add a falg with name k that is by default on if def is true and off if def is false. d should provide a description of the flag.
Definition: Keywords.cpp:193