LCOV - code coverage report
Current view: top level - multicolvar - VolumeCavity.cpp (source / functions) Hit Total Coverage
Test: plumed test coverage Lines: 170 214 79.4 %
Date: 2024-10-11 08:09:47 Functions: 10 12 83.3 %

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

Generated by: LCOV version 1.15