LCOV - code coverage report
Current view: top level - multicolvar - VolumeCavity.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 164 207 79.2 %
Date: 2020-11-18 11:20:57 Functions: 12 14 85.7 %

          Line data    Source code
       1             : /* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
       2             :    Copyright (c) 2014-2019 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 "core/ActionRegister.h"
      23             : #include "core/PlumedMain.h"
      24             : #include "core/Atoms.h"
      25             : #include "tools/Units.h"
      26             : #include "tools/Pbc.h"
      27             : #include "ActionVolume.h"
      28             : 
      29             : //+PLUMEDOC VOLUMES CAVITY
      30             : /*
      31             : This quantity can be used to calculate functions of the distribution of collective
      32             : variables for the atoms that lie in a box defined by the positions of four atoms.
      33             : 
      34             : Each of the base quantities calculated by a multicolvar can can be assigned to a particular point in three
      35             : dimensional space. For example, if we have the coordination numbers for all the atoms in the
      36             : system each coordination number can be assumed to lie on the position of the central atom.
      37             : Because each base quantity can be assigned to a particular point in space we can calculate functions of the
      38             : distribution of base quantities in a particular part of the box by using:
      39             : 
      40             : \f[
      41             : \overline{s}_{\tau} = \frac{ \sum_i f(s_i) w(u_i,v_i,w_i) }{ \sum_i w(u_i,v_i,w_i) }
      42             : \f]
      43             : 
      44             : where the sum is over the collective variables, \f$s_i\f$, each of which can be thought to be at \f$ (u_i,v_i,z_i)\f$.
      45             : The function \f$(s_i)\f$ can be any of the usual LESS_THAN, MORE_THAN, WITHIN etc that are used in all other multicolvars.
      46             : Notice that here (at variance with what is done in \ref AROUND) we have transformed from the usual \f$(x_i,y_i,z_i)\f$
      47             : position to a position in \f$ (u_i,v_i,z_i)\f$.  This is done using a rotation matrix as follows:
      48             : 
      49             : \f[
      50             : \left(
      51             : \begin{matrix}
      52             :  u_i \\
      53             :  v_i \\
      54             :  w_i
      55             : \end{matrix}
      56             : \right) = \mathbf{R}
      57             : \left(
      58             : \begin{matrix}
      59             :  x_i - x_o \\
      60             :  y_i - y_o \\
      61             :  z_i - z_o
      62             : \end{matrix}
      63             : \right)
      64             : \f]
      65             : 
      66             : where \f$\mathbf{R}\f$ is a rotation matrix that is calculated by constructing a set of three orthonormal vectors from the
      67             : refererence positions specified by the user. The first of these unit vectors points from the first reference atom to the second.
      68             : The second is then the normal to the plane containing atoms 1,2 and 3 and the the third is the unit vector orthogonal to
      69             : these first two vectors.  \f$(x_o,y_o,z_o)\f$, meanwhile, specifies the position of the first reference atom.
      70             : 
      71             : In the previous function \f$ w(u_i,v_i,w_i) \f$ measures whether or not the system is in the subregion of interest. It
      72             : is equal to:
      73             : 
      74             : \f[
      75             : w(u_i,v_i,w_i) = \int_{0}^{u'} \int_{0}^{v'} \int_{0}^{w'} \textrm{d}u\textrm{d}v\textrm{d}w
      76             :    K\left( \frac{u - u_i}{\sigma} \right)K\left( \frac{v - v_i}{\sigma} \right)K\left( \frac{w - w_i}{\sigma} \right)
      77             : \f]
      78             : 
      79             : where \f$K\f$ is one of the kernel functions described on \ref histogrambead and \f$\sigma\f$ is a bandwidth parameter.
      80             : The vector connecting atom 1 to atom 4 is used to define the extent of the box in each of the \f$u\f$, \f$v\f$ and \f$w\f$
      81             : directions.  Essentially the vector connecting atom 1 to atom 4 is projected onto the three unit vectors
      82             : described above and the resulting projections determine the \f$u'\f$, \f$v'\f$ and \f$w'\f$ parameters in the above expression.
      83             : 
      84             : \par Examples
      85             : 
      86             : The following commands tell plumed to calculate the number of atoms in an ion chanel in a protein.
      87             : The extent of the chanel is calculated from the positions of atoms 1, 4, 5 and 11. The final value will be labeled cav.
      88             : 
      89             : \plumedfile
      90             : d1: DENSITY SPECIES=20-500
      91             : CAVITY DATA=d1 ATOMS=1,4,5,11 SIGMA=0.1 LABEL=cav
      92             : \endplumedfile
      93             : 
      94             : The following command tells plumed to calculate the coordination numbers (with other water molecules) for the water
      95             : molecules in the protein channel described above.  The average coordination number and the number of coordination
      96             : numbers more than 4 is then calculated.  The values of these two quantities are given the labels cav.mean and cav.morethan
      97             : 
      98             : \plumedfile
      99             : d1: COORDINATIONNUMBER SPECIES=20-500
     100             : CAVITY DATA=d1 ATOMS=1,4,5,11 SIGMA=0.1 MEAN MORE_THAN={RATIONAL R_0=4} LABEL=cav
     101             : \endplumedfile
     102             : 
     103             : */
     104             : //+ENDPLUMEDOC
     105             : 
     106             : namespace PLMD {
     107             : namespace multicolvar {
     108             : 
     109             : class VolumeCavity : public ActionVolume {
     110             : private:
     111             :   bool boxout;
     112             :   OFile boxfile;
     113             :   double lenunit;
     114             :   double jacob_det;
     115             :   double len_bi, len_cross, len_perp, sigma;
     116             :   Vector origin, bi, cross, perp;
     117             :   std::vector<Vector> dlbi, dlcross, dlperp;
     118             :   std::vector<Tensor> dbi, dcross, dperp;
     119             : public:
     120             :   static void registerKeywords( Keywords& keys );
     121             :   explicit VolumeCavity(const ActionOptions& ao);
     122             :   ~VolumeCavity();
     123             :   void setupRegions();
     124             :   void update();
     125             :   double calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& refders ) const ;
     126             : };
     127             : 
     128        6454 : PLUMED_REGISTER_ACTION(VolumeCavity,"CAVITY")
     129             : 
     130           3 : void VolumeCavity::registerKeywords( Keywords& keys ) {
     131           3 :   ActionVolume::registerKeywords( keys );
     132          12 :   keys.add("atoms","ATOMS","the positions of four atoms that define spatial extent of the cavity");
     133           9 :   keys.addFlag("PRINT_BOX",false,"write out the positions of the corners of the box to an xyz file");
     134          12 :   keys.add("optional","FILE","the file on which to write out the box coordinates");
     135          12 :   keys.add("optional","UNITS","( default=nm ) the units in which to write out the corners of the box");
     136           3 : }
     137             : 
     138           2 : VolumeCavity::VolumeCavity(const ActionOptions& ao):
     139             :   Action(ao),
     140             :   ActionVolume(ao),
     141             :   boxout(false),
     142             :   lenunit(1.0),
     143             :   dlbi(4),
     144             :   dlcross(4),
     145             :   dlperp(4),
     146             :   dbi(3),
     147             :   dcross(3),
     148           2 :   dperp(3)
     149             : {
     150             :   std::vector<AtomNumber> atoms;
     151           4 :   parseAtomList("ATOMS",atoms);
     152           2 :   if( atoms.size()!=4 ) error("number of atoms should be equal to four");
     153             : 
     154           2 :   log.printf("  boundaries for region are calculated based on positions of atoms : ");
     155          28 :   for(unsigned i=0; i<atoms.size(); ++i) log.printf("%d ",atoms[i].serial() );
     156           2 :   log.printf("\n");
     157             : 
     158           4 :   boxout=false; parseFlag("PRINT_BOX",boxout);
     159           2 :   if(boxout) {
     160           0 :     std::string boxfname; parse("FILE",boxfname);
     161           0 :     if(boxfname.length()==0) error("no name for box file specified");
     162           0 :     std::string unitname; parse("UNITS",unitname);
     163           0 :     if ( unitname.length()>0 ) {
     164           0 :       Units u; u.setLength(unitname);
     165           0 :       lenunit=plumed.getAtoms().getUnits().getLength()/u.getLength();
     166             :     } else {
     167             :       unitname="nm";
     168             :     }
     169           0 :     boxfile.link(*this);
     170           0 :     boxfile.open( boxfname.c_str() );
     171           0 :     log.printf("  printing box coordinates on file named %s in %s \n",boxfname.c_str(), unitname.c_str() );
     172             :   }
     173             : 
     174           2 :   checkRead();
     175           2 :   requestAtoms(atoms);
     176             :   // We have to readd the dependency because requestAtoms removes it
     177           2 :   addDependency( getPntrToMultiColvar() );
     178           2 : }
     179             : 
     180           6 : VolumeCavity::~VolumeCavity() {
     181           4 : }
     182             : 
     183        1620 : void VolumeCavity::setupRegions() {
     184             :   // Make some space for things
     185        1620 :   Vector d1, d2, d3;
     186             : 
     187             :   // Retrieve the sigma value
     188        1620 :   sigma=getSigma();
     189             :   // Set the position of the origin
     190        1620 :   origin=getPosition(0);
     191             : 
     192             :   // Get two vectors
     193        3240 :   d1 = pbcDistance(origin,getPosition(1));
     194        1620 :   double d1l=d1.modulo();
     195        3240 :   d2 = pbcDistance(origin,getPosition(2));
     196             : 
     197             :   // Find the vector connecting the origin to the top corner of
     198             :   // the subregion
     199        3240 :   d3 = pbcDistance(origin,getPosition(3));
     200             : 
     201             :   // Create a set of unit vectors
     202        1620 :   bi = d1 / d1l; len_bi=dotProduct( d3, bi );
     203        1620 :   cross = crossProduct( d1, d2 ); double crossmod=cross.modulo();
     204        1620 :   cross = cross / crossmod; len_cross=dotProduct( d3, cross );
     205        1620 :   perp = crossProduct( cross, bi ); len_perp=dotProduct( d3, perp );
     206             : 
     207             :   // Calculate derivatives of box shape with respect to atoms
     208        1620 :   double d1l3=d1l*d1l*d1l;
     209        3240 :   dbi[0](0,0) = ( -(d1[1]*d1[1]+d1[2]*d1[2])/d1l3 );   // dx/dx
     210        3240 :   dbi[0](0,1) = (  d1[0]*d1[1]/d1l3 );                 // dx/dy
     211        3240 :   dbi[0](0,2) = (  d1[0]*d1[2]/d1l3 );                 // dx/dz
     212        3240 :   dbi[0](1,0) = (  d1[1]*d1[0]/d1l3 );                 // dy/dx
     213        3240 :   dbi[0](1,1) = ( -(d1[0]*d1[0]+d1[2]*d1[2])/d1l3 );   // dy/dy
     214        3240 :   dbi[0](1,2) = (  d1[1]*d1[2]/d1l3 );
     215        3240 :   dbi[0](2,0) = (  d1[2]*d1[0]/d1l3 );
     216        3240 :   dbi[0](2,1) = (  d1[2]*d1[1]/d1l3 );
     217        3240 :   dbi[0](2,2) = ( -(d1[1]*d1[1]+d1[0]*d1[0])/d1l3 );
     218             : 
     219        3240 :   dbi[1](0,0) = ( (d1[1]*d1[1]+d1[2]*d1[2])/d1l3 );
     220        3240 :   dbi[1](0,1) = ( -d1[0]*d1[1]/d1l3 );
     221        3240 :   dbi[1](0,2) = ( -d1[0]*d1[2]/d1l3 );
     222        3240 :   dbi[1](1,0) = ( -d1[1]*d1[0]/d1l3 );
     223        3240 :   dbi[1](1,1) = ( (d1[0]*d1[0]+d1[2]*d1[2])/d1l3 );
     224        3240 :   dbi[1](1,2) = ( -d1[1]*d1[2]/d1l3 );
     225        3240 :   dbi[1](2,0) = ( -d1[2]*d1[0]/d1l3 );
     226        3240 :   dbi[1](2,1) = ( -d1[2]*d1[1]/d1l3 );
     227        3240 :   dbi[1](2,2) = ( (d1[1]*d1[1]+d1[0]*d1[0])/d1l3 );
     228        1620 :   dbi[2].zero();
     229             : 
     230        1620 :   Tensor tcderiv; double cmod3=crossmod*crossmod*crossmod; Vector ucross=crossmod*cross;
     231        3240 :   tcderiv.setCol( 0, crossProduct( d1, Vector(-1.0,0.0,0.0) ) + crossProduct( Vector(-1.0,0.0,0.0), d2 ) );
     232        3240 :   tcderiv.setCol( 1, crossProduct( d1, Vector(0.0,-1.0,0.0) ) + crossProduct( Vector(0.0,-1.0,0.0), d2 ) );
     233        3240 :   tcderiv.setCol( 2, crossProduct( d1, Vector(0.0,0.0,-1.0) ) + crossProduct( Vector(0.0,0.0,-1.0), d2 ) );
     234        3240 :   dcross[0](0,0)=( tcderiv(0,0)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dx/dx
     235        3240 :   dcross[0](0,1)=( tcderiv(0,1)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dx/dy
     236        3240 :   dcross[0](0,2)=( tcderiv(0,2)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dx/dz
     237        3240 :   dcross[0](1,0)=( tcderiv(1,0)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dy/dx
     238        3240 :   dcross[0](1,1)=( tcderiv(1,1)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dy/dy
     239        3240 :   dcross[0](1,2)=( tcderiv(1,2)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dy/dz
     240        3240 :   dcross[0](2,0)=( tcderiv(2,0)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dz/dx
     241        3240 :   dcross[0](2,1)=( tcderiv(2,1)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dz/dy
     242        3240 :   dcross[0](2,2)=( tcderiv(2,2)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dz/dz
     243             : 
     244        1620 :   tcderiv.setCol( 0, crossProduct( Vector(1.0,0.0,0.0), d2 ) );
     245        1620 :   tcderiv.setCol( 1, crossProduct( Vector(0.0,1.0,0.0), d2 ) );
     246        1620 :   tcderiv.setCol( 2, crossProduct( Vector(0.0,0.0,1.0), d2 ) );
     247        3240 :   dcross[1](0,0)=( tcderiv(0,0)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dx/dx
     248        3240 :   dcross[1](0,1)=( tcderiv(0,1)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dx/dy
     249        3240 :   dcross[1](0,2)=( tcderiv(0,2)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dx/dz
     250        3240 :   dcross[1](1,0)=( tcderiv(1,0)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dy/dx
     251        3240 :   dcross[1](1,1)=( tcderiv(1,1)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dy/dy
     252        3240 :   dcross[1](1,2)=( tcderiv(1,2)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dy/dz
     253        3240 :   dcross[1](2,0)=( tcderiv(2,0)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dz/dx
     254        3240 :   dcross[1](2,1)=( tcderiv(2,1)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dz/dy
     255        3240 :   dcross[1](2,2)=( tcderiv(2,2)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dz/dz
     256             : 
     257        1620 :   tcderiv.setCol( 0, crossProduct( d1, Vector(1.0,0.0,0.0) ) );
     258        1620 :   tcderiv.setCol( 1, crossProduct( d1, Vector(0.0,1.0,0.0) ) );
     259        1620 :   tcderiv.setCol( 2, crossProduct( d1, Vector(0.0,0.0,1.0) ) );
     260        3240 :   dcross[2](0,0)=( tcderiv(0,0)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dx/dx
     261        3240 :   dcross[2](0,1)=( tcderiv(0,1)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dx/dy
     262        3240 :   dcross[2](0,2)=( tcderiv(0,2)/crossmod - ucross[0]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dx/dz
     263        3240 :   dcross[2](1,0)=( tcderiv(1,0)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dy/dx
     264        3240 :   dcross[2](1,1)=( tcderiv(1,1)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dy/dy
     265        3240 :   dcross[2](1,2)=( tcderiv(1,2)/crossmod - ucross[1]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dy/dz
     266        3240 :   dcross[2](2,0)=( tcderiv(2,0)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,0) + ucross[1]*tcderiv(1,0) + ucross[2]*tcderiv(2,0))/cmod3 );    // dz/dx
     267        3240 :   dcross[2](2,1)=( tcderiv(2,1)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,1) + ucross[1]*tcderiv(1,1) + ucross[2]*tcderiv(2,1))/cmod3 );    // dz/dy
     268        3240 :   dcross[2](2,2)=( tcderiv(2,2)/crossmod - ucross[2]*(ucross[0]*tcderiv(0,2) + ucross[1]*tcderiv(1,2) + ucross[2]*tcderiv(2,2))/cmod3 );    // dz/dz
     269             : 
     270        3240 :   dperp[0].setCol( 0, ( crossProduct( dcross[0].getCol(0), bi ) + crossProduct( cross, dbi[0].getCol(0) ) ) );
     271        3240 :   dperp[0].setCol( 1, ( crossProduct( dcross[0].getCol(1), bi ) + crossProduct( cross, dbi[0].getCol(1) ) ) );
     272        3240 :   dperp[0].setCol( 2, ( crossProduct( dcross[0].getCol(2), bi ) + crossProduct( cross, dbi[0].getCol(2) ) ) );
     273             : 
     274        3240 :   dperp[1].setCol( 0, ( crossProduct( dcross[1].getCol(0), bi ) + crossProduct( cross, dbi[1].getCol(0) ) ) );
     275        3240 :   dperp[1].setCol( 1, ( crossProduct( dcross[1].getCol(1), bi ) + crossProduct( cross, dbi[1].getCol(1) ) ) );
     276        3240 :   dperp[1].setCol( 2, ( crossProduct( dcross[1].getCol(2), bi ) + crossProduct( cross, dbi[1].getCol(2) ) ) );
     277             : 
     278        1620 :   dperp[2].setCol( 0, ( crossProduct( dcross[2].getCol(0), bi ) ) );
     279        1620 :   dperp[2].setCol( 1, ( crossProduct( dcross[2].getCol(1), bi ) ) );
     280        1620 :   dperp[2].setCol( 2, ( crossProduct( dcross[2].getCol(2), bi ) ) );
     281             : 
     282             :   // Ensure that all lengths are positive
     283        1620 :   if( len_bi<0 ) {
     284           0 :     bi=-bi; len_bi=-len_bi;
     285           0 :     for(unsigned i=0; i<3; ++i) dbi[i]*=-1.0;
     286             :   }
     287        1620 :   if( len_cross<0 ) {
     288           0 :     cross=-cross; len_cross=-len_cross;
     289           0 :     for(unsigned i=0; i<3; ++i) dcross[i]*=-1.0;
     290             :   }
     291        1620 :   if( len_perp<0 ) {
     292           0 :     perp=-perp; len_perp=-len_perp;
     293           0 :     for(unsigned i=0; i<3; ++i) dperp[i]*=-1.0;
     294             :   }
     295        1620 :   if( len_bi<=0 || len_cross<=0 || len_bi<=0 ) plumed_merror("Invalid box coordinates");
     296             : 
     297             :   // Now derivatives of lengths
     298        1620 :   Tensor dd3( Tensor::identity() );
     299        4860 :   dlbi[0] = matmul(d3,dbi[0]) - matmul(bi,dd3);
     300        1620 :   dlbi[1] = matmul(d3,dbi[1]);
     301        1620 :   dlbi[2] = matmul(d3,dbi[2]);
     302        1620 :   dlbi[3] = matmul(bi,dd3);
     303             : 
     304        4860 :   dlcross[0] = matmul(d3,dcross[0]) - matmul(cross,dd3);
     305        1620 :   dlcross[1] = matmul(d3,dcross[1]);
     306        1620 :   dlcross[2] = matmul(d3,dcross[2]);
     307        1620 :   dlcross[3] = matmul(cross,dd3);
     308             : 
     309        4860 :   dlperp[0] = matmul(d3,dperp[0]) - matmul(perp,dd3);
     310        1620 :   dlperp[1] = matmul(d3,dperp[1]);
     311        1620 :   dlperp[2] = matmul(d3,dperp[2]);
     312        1620 :   dlperp[3] = matmul(perp,dd3);
     313             : 
     314             :   // Need to calculate the jacobian
     315        1620 :   Tensor jacob;
     316        1620 :   jacob(0,0)=bi[0]; jacob(1,0)=bi[1]; jacob(2,0)=bi[2];
     317        1620 :   jacob(0,1)=cross[0]; jacob(1,1)=cross[1]; jacob(2,1)=cross[2];
     318        1620 :   jacob(0,2)=perp[0]; jacob(1,2)=perp[1]; jacob(2,2)=perp[2];
     319        1620 :   jacob_det = fabs( jacob.determinant() );
     320        1620 : }
     321             : 
     322         120 : void VolumeCavity::update() {
     323         120 :   if(boxout) {
     324           0 :     boxfile.printf("%d\n",8);
     325           0 :     const Tensor & t(getPbc().getBox());
     326           0 :     if(getPbc().isOrthorombic()) {
     327           0 :       boxfile.printf(" %f %f %f\n",lenunit*t(0,0),lenunit*t(1,1),lenunit*t(2,2));
     328             :     } else {
     329           0 :       boxfile.printf(" %f %f %f %f %f %f %f %f %f\n",
     330           0 :                      lenunit*t(0,0),lenunit*t(0,1),lenunit*t(0,2),
     331           0 :                      lenunit*t(1,0),lenunit*t(1,1),lenunit*t(1,2),
     332           0 :                      lenunit*t(2,0),lenunit*t(2,1),lenunit*t(2,2)
     333             :                     );
     334             :     }
     335           0 :     boxfile.printf("AR %f %f %f \n",lenunit*origin[0],lenunit*origin[1],lenunit*origin[2]);
     336           0 :     Vector ut, vt, wt;
     337           0 :     ut = origin + len_bi*bi;
     338           0 :     vt = origin + len_cross*cross;
     339           0 :     wt = origin + len_perp*perp;
     340           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(ut[0]), lenunit*(ut[1]), lenunit*(ut[2]) );
     341           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(vt[0]), lenunit*(vt[1]), lenunit*(vt[2]) );
     342           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(wt[0]), lenunit*(wt[1]), lenunit*(wt[2]) );
     343           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(vt[0]+len_bi*bi[0]),
     344           0 :                    lenunit*(vt[1]+len_bi*bi[1]),
     345           0 :                    lenunit*(vt[2]+len_bi*bi[2]) );
     346           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(ut[0]+len_perp*perp[0]),
     347           0 :                    lenunit*(ut[1]+len_perp*perp[1]),
     348           0 :                    lenunit*(ut[2]+len_perp*perp[2]) );
     349           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(vt[0]+len_perp*perp[0]),
     350           0 :                    lenunit*(vt[1]+len_perp*perp[1]),
     351           0 :                    lenunit*(vt[2]+len_perp*perp[2]) );
     352           0 :     boxfile.printf("AR %f %f %f \n",lenunit*(vt[0]+len_perp*perp[0]+len_bi*bi[0]),
     353           0 :                    lenunit*(vt[1]+len_perp*perp[1]+len_bi*bi[1]),
     354           0 :                    lenunit*(vt[2]+len_perp*perp[2]+len_bi*bi[2]) );
     355             :   }
     356         120 : }
     357             : 
     358        1620 : double VolumeCavity::calculateNumberInside( const Vector& cpos, Vector& derivatives, Tensor& vir, std::vector<Vector>& rderiv ) const {
     359             :   // Setup the histogram bead
     360        4860 :   HistogramBead bead; bead.isNotPeriodic(); bead.setKernelType( getKernelType() );
     361             : 
     362             :   // Calculate distance of atom from origin of new coordinate frame
     363        1620 :   Vector datom=pbcDistance( origin, cpos );
     364             :   double ucontr, uder, vcontr, vder, wcontr, wder;
     365             : 
     366             :   // Calculate contribution from integral along bi
     367        1620 :   bead.set( 0, len_bi, sigma );
     368        1620 :   double upos=dotProduct( datom, bi );
     369        1620 :   ucontr=bead.calculate( upos, uder );
     370        1620 :   double udlen=bead.uboundDerivative( upos );
     371        1620 :   double uder2 = bead.lboundDerivative( upos ) - udlen;
     372             : 
     373             :   // Calculate contribution from integral along cross
     374        1620 :   bead.set( 0, len_cross, sigma );
     375        1620 :   double vpos=dotProduct( datom, cross );
     376        1620 :   vcontr=bead.calculate( vpos, vder );
     377        1620 :   double vdlen=bead.uboundDerivative( vpos );
     378        1620 :   double vder2 = bead.lboundDerivative( vpos ) - vdlen;
     379             : 
     380             :   // Calculate contribution from integral along perp
     381        1620 :   bead.set( 0, len_perp, sigma );
     382        1620 :   double wpos=dotProduct( datom, perp );
     383        1620 :   wcontr=bead.calculate( wpos, wder );
     384        1620 :   double wdlen=bead.uboundDerivative( wpos );
     385        1620 :   double wder2 = bead.lboundDerivative( wpos ) - wdlen;
     386             : 
     387        1620 :   Vector dfd; dfd[0]=uder*vcontr*wcontr; dfd[1]=ucontr*vder*wcontr; dfd[2]=ucontr*vcontr*wder;
     388        1620 :   derivatives[0] = (dfd[0]*bi[0]+dfd[1]*cross[0]+dfd[2]*perp[0]);
     389        1620 :   derivatives[1] = (dfd[0]*bi[1]+dfd[1]*cross[1]+dfd[2]*perp[1]);
     390        1620 :   derivatives[2] = (dfd[0]*bi[2]+dfd[1]*cross[2]+dfd[2]*perp[2]);
     391        1620 :   double tot = ucontr*vcontr*wcontr*jacob_det;
     392             : 
     393             :   // Add reference atom derivatives
     394        1620 :   dfd[0]=uder2*vcontr*wcontr; dfd[1]=ucontr*vder2*wcontr; dfd[2]=ucontr*vcontr*wder2;
     395        1620 :   Vector dfld; dfld[0]=udlen*vcontr*wcontr; dfld[1]=ucontr*vdlen*wcontr; dfld[2]=ucontr*vcontr*wdlen;
     396        8100 :   rderiv[0] = dfd[0]*matmul(datom,dbi[0]) + dfd[1]*matmul(datom,dcross[0]) + dfd[2]*matmul(datom,dperp[0]) +
     397        9720 :               dfld[0]*dlbi[0] + dfld[1]*dlcross[0] + dfld[2]*dlperp[0] - derivatives;
     398        8100 :   rderiv[1] = dfd[0]*matmul(datom,dbi[1]) + dfd[1]*matmul(datom,dcross[1]) + dfd[2]*matmul(datom,dperp[1]) +
     399        9720 :               dfld[0]*dlbi[1] + dfld[1]*dlcross[1] + dfld[2]*dlperp[1];
     400        8100 :   rderiv[2] = dfd[0]*matmul(datom,dbi[2]) + dfd[1]*matmul(datom,dcross[2]) + dfd[2]*matmul(datom,dperp[2]) +
     401        9720 :               dfld[0]*dlbi[2] + dfld[1]*dlcross[2] + dfld[2]*dlperp[2];
     402        6480 :   rderiv[3] = dfld[0]*dlbi[3] + dfld[1]*dlcross[3] + dfld[2]*dlperp[3];
     403             : 
     404        1620 :   vir.zero(); vir-=Tensor( cpos,derivatives );
     405       14580 :   for(unsigned i=0; i<4; ++i) {
     406       12960 :     vir -= Tensor( getPosition(i), rderiv[i] );
     407             :   }
     408             : 
     409        1620 :   return tot;
     410             : }
     411             : 
     412             : }
     413        4839 : }

Generated by: LCOV version 1.13