LCOV - code coverage report
Current view: top level - vatom - FixedAtom.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 35 37 94.6 %
Date: 2025-03-25 09:33:27 Functions: 3 4 75.0 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2015-2023 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.org for more information.
       6             : 
       7             :    This file is part of plumed, version 2.
       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 "core/ActionRegister.h"
      24             : #include "tools/Vector.h"
      25             : #include "tools/Exception.h"
      26             : 
      27             : namespace PLMD {
      28             : namespace vatom {
      29             : 
      30             : //+PLUMEDOC VATOM FIXEDATOM
      31             : /*
      32             : Add a virtual atom in a fixed position.
      33             : 
      34             : This action creates [a virtual atom](specifying_atoms.md) at a fixed position.  The example input below illustrates
      35             : how this idea can be used to compute the angle between the vector connecting atoms 15 and 20 and the z axis and how this
      36             : quantity can then be restrained so that the angle stays close to zero.
      37             : 
      38             : ```plumed
      39             : a: FIXEDATOM AT=0,0,0
      40             : b: FIXEDATOM AT=0,0,1
      41             : an: ANGLE ATOMS=a,b,15,20
      42             : RESTRAINT ARG=an AT=0.0 KAPPA=100.0
      43             : ```
      44             : 
      45             : By default PLUMED assumes that any coordinates specified using the AT keyword specified are the cartesian coordinates of the fixed atom.
      46             : However, if you use the SCALED_COMPONENTS flag the coordinates specified using the AT keyword are interpretted as scaled coordinates.
      47             : It is also possible to assign a predefined charge or mass to the atom by using the `SET_MASS` and `SET_CHARGE` keywords.
      48             : 
      49             : > [!CAUTION]
      50             : > This action, like [POSITION](POSITION.md) is not invariant for translation of the system so adding a force on it can cause trouble.
      51             : 
      52             : The problem is that the vector connecting any atom and a virtual atom created using the FIXEDATOM atoms command is not invariant to translation.
      53             : However, if, as has been done in the following example input, one first aligns atoms to a reference using [FIT_TO_TEMPLATE](FIT_TO_TEMPLATE.md),
      54             : then it is safe to add further fixed atoms without breaking translational invariance.
      55             : 
      56             : ```plumed
      57             : #SETTINGS INPUTFILES=regtest/basic/rt63/align.pdb
      58             : FIT_TO_TEMPLATE STRIDE=1 REFERENCE=regtest/basic/rt63/align.pdb TYPE=SIMPLE
      59             : a: FIXEDATOM AT=10,20,30
      60             : d: DISTANCE ATOMS=a,20
      61             : PRINT ARG=d FILE=colvar
      62             : ```
      63             : 
      64             : */
      65             : //+ENDPLUMEDOC
      66             : 
      67             : 
      68             : class FixedAtom:
      69             :   public ActionWithVirtualAtom {
      70             :   Vector coord;
      71             :   std::vector<Tensor> deriv;
      72             :   double mass,charge;
      73             :   bool scaled_components;
      74             : public:
      75             :   explicit FixedAtom(const ActionOptions&ao);
      76             :   void calculate() override;
      77             :   static void registerKeywords( Keywords& keys );
      78             : };
      79             : 
      80             : PLUMED_REGISTER_ACTION(FixedAtom,"FIXEDATOM")
      81             : 
      82          40 : void FixedAtom::registerKeywords(Keywords& keys) {
      83          40 :   ActionWithVirtualAtom::registerKeywords(keys);
      84          40 :   keys.add("compulsory","AT","coordinates of the virtual atom");
      85          40 :   keys.add("compulsory","SET_MASS","1","mass of the virtual atom");
      86          40 :   keys.add("compulsory","SET_CHARGE","0","charge of the virtual atom");
      87          40 :   keys.addFlag("SCALED_COMPONENTS",false,"use scaled components");
      88          40 : }
      89             : 
      90          35 : FixedAtom::FixedAtom(const ActionOptions&ao):
      91             :   Action(ao),
      92          35 :   ActionWithVirtualAtom(ao) {
      93             :   std::vector<AtomNumber> atoms;
      94          70 :   parseAtomList("ATOMS",atoms);
      95          35 :   if(atoms.size()!=0) {
      96           0 :     error("ATOMS should be empty");
      97             :   }
      98             : 
      99          70 :   parseFlag("SCALED_COMPONENTS",scaled_components);
     100             : 
     101             :   std::vector<double> at;
     102          70 :   parseVector("AT",at);
     103          35 :   if(at.size()!=3) {
     104           0 :     error("AT should be a list of three real numbers");
     105             :   }
     106             : 
     107          35 :   parse("SET_MASS",mass);
     108          70 :   parse("SET_CHARGE",charge);
     109             : 
     110          35 :   coord[0]=at[0];
     111          35 :   coord[1]=at[1];
     112          35 :   coord[2]=at[2];
     113             : 
     114          35 :   checkRead();
     115          35 :   log<<"  AT position "<<coord[0]<<" "<<coord[1]<<" "<<coord[2]<<"\n";
     116          35 :   if(scaled_components) {
     117           1 :     log<<"  position is in scaled components\n";
     118             :   }
     119          35 : }
     120             : 
     121         735 : void FixedAtom::calculate() {
     122         735 :   deriv.resize(getNumberOfAtoms());
     123         735 :   if(scaled_components) {
     124           5 :     setPosition(getPbc().scaledToReal(coord));
     125             :   } else {
     126         730 :     setPosition(coord);
     127             :   }
     128         735 :   setMass(mass);
     129         735 :   setCharge(charge);
     130         735 :   setAtomsDerivatives(deriv);
     131             : // Virial contribution
     132         735 :   if(!scaled_components) {
     133         730 :     setBoxDerivativesNoPbc();
     134             :   }
     135             : // notice that with scaled components there is no additional virial contribution
     136         735 : }
     137             : 
     138             : }
     139             : }

Generated by: LCOV version 1.16