ComputeThole.C

Go to the documentation of this file.
00001 
00007 #include "InfoStream.h"
00008 #include "ComputeThole.h"
00009 #include "Molecule.h"
00010 #include "Parameters.h"
00011 #include "Node.h"
00012 #include "ReductionMgr.h"
00013 #include "Lattice.h"
00014 #include "PressureProfile.h"
00015 #include "Debug.h"
00016 
00017 #define CALCULATE_THOLE_CORRECTION
00018 
00019 // static initialization
00020 int TholeElem::pressureProfileSlabs = 0;
00021 int TholeElem::pressureProfileAtomTypes = 1;
00022 BigReal TholeElem::pressureProfileThickness = 0;
00023 BigReal TholeElem::pressureProfileMin = 0;
00024 
00025 void TholeElem::getMoleculePointers
00026     (Molecule* mol, int* count, int32*** byatom, Thole** structarray)
00027 {
00028 #ifdef MEM_OPT_VERSION
00029   NAMD_die("Should not be called in TholeElem::getMoleculePointers in memory optimized version!");
00030 #else
00031   *count = mol->numTholes;
00032   *byatom = mol->tholesByAtom;
00033   *structarray = mol->tholes;
00034 #endif
00035 }
00036 
00037 void TholeElem::getParameterPointers(Parameters *p, const TholeValue **v) {
00038   *v = NULL;  // parameters are stored in the structure
00039 }
00040 
00041 void TholeElem::computeForce(TholeElem *tuples, int ntuple, BigReal *reduction, 
00042                                 BigReal *pressureProfileData)
00043 {
00044  const Lattice & lattice = tuples[0].p[0]->p->lattice;
00045 
00046  //fepb BKR
00047  SimParameters *const simParams = Node::Object()->simParameters;
00048  const int step = tuples[0].p[0]->p->flags.step;
00049  const BigReal alchLambda = simParams->getCurrentLambda(step);
00050  const BigReal alchLambda2 = simParams->alchLambda2;
00051  const BigReal elec_lambda_1 = simParams->getElecLambda(alchLambda);
00052  const BigReal elec_lambda_2 = simParams->getElecLambda(1-alchLambda);
00053  const BigReal elec_lambda_12 = simParams->getElecLambda(alchLambda2);
00054  const BigReal elec_lambda_22 = simParams->getElecLambda(1-alchLambda2);
00055  Molecule *const mol = Node::Object()->molecule;
00056  //fepe
00057 
00058 
00059  for ( int ituple=0; ituple<ntuple; ++ituple ) {
00060   const TholeElem &tup = tuples[ituple];
00061   enum { size = 4 };
00062   const AtomID (&atomID)[size](tup.atomID);
00063   const int    (&localIndex)[size](tup.localIndex);
00064   TuplePatchElem * const(&p)[size](tup.p);
00065   const Real (&scale)(tup.scale);
00066   const TholeValue * const(&value)(tup.value);
00067 
00068   DebugM(3, "::computeForce() localIndex = " << localIndex[0] << " "
00069                << localIndex[1] << " " << localIndex[2] << " "
00070                << localIndex[3] << std::endl);
00071 
00072 #ifdef CALCULATE_THOLE_CORRECTION
00073   const BigReal aa = value->aa;
00074   const BigReal qq = value->qq;
00075 
00076   //  Calculate the vectors between atoms
00077   const Position & rai = p[0]->x[localIndex[0]].position;  // atom i
00078   const Position & rdi = p[1]->x[localIndex[1]].position;  // atom i's Drude
00079   const Position & raj = p[2]->x[localIndex[2]].position;  // atom j
00080   const Position & rdj = p[3]->x[localIndex[3]].position;  // atom j's Drude
00081 
00082   // r_ij = r_i - r_j
00083   Vector raa = lattice.delta(rai,raj);  // shortest vector image:  rai - raj
00084   Vector rad = lattice.delta(rai,rdj);  // shortest vector image:  rai - rdj
00085   Vector rda = lattice.delta(rdi,raj);  // shortest vector image:  rdi - raj
00086   Vector rdd = lattice.delta(rdi,rdj);  // shortest vector image:  rdi - rdj
00087 
00088   // 1/r, r = |r_ij|
00089   BigReal raa_invlen = raa.rlength();  // reciprocal of length
00090   BigReal rad_invlen = rad.rlength();
00091   BigReal rda_invlen = rda.rlength();
00092   BigReal rdd_invlen = rdd.rlength();
00093 
00094   // ar
00095   BigReal auaa = aa / raa_invlen;
00096   BigReal auad = aa / rad_invlen;
00097   BigReal auda = aa / rda_invlen;
00098   BigReal audd = aa / rdd_invlen;
00099 
00100   // exp(-ar)
00101   BigReal expauaa = exp(-auaa);
00102   BigReal expauad = exp(-auad);
00103   BigReal expauda = exp(-auda);
00104   BigReal expaudd = exp(-audd);
00105 
00106   // (1 + ar/2)
00107   BigReal polyauaa = 1 + 0.5*auaa;
00108   BigReal polyauad = 1 + 0.5*auad;
00109   BigReal polyauda = 1 + 0.5*auda;
00110   BigReal polyaudd = 1 + 0.5*audd;
00111 
00112   // U(r) = qq/r (1 - (1 + ar/2) exp(-ar))
00113   BigReal ethole = 0;
00114   ethole += qq * raa_invlen * (1 - polyauaa * expauaa);
00115   ethole += -qq * rad_invlen * (1 - polyauad * expauad);
00116   ethole += -qq * rda_invlen * (1 - polyauda * expauda);
00117   ethole += qq * rdd_invlen * (1 - polyaudd * expaudd);
00118 
00119   polyauaa = 1 + auaa*polyauaa;
00120   polyauad = 1 + auad*polyauad;
00121   polyauda = 1 + auda*polyauda;
00122   polyaudd = 1 + audd*polyaudd;
00123 
00124   BigReal raa_invlen3 = raa_invlen * raa_invlen * raa_invlen;
00125   BigReal rad_invlen3 = rad_invlen * rad_invlen * rad_invlen;
00126   BigReal rda_invlen3 = rda_invlen * rda_invlen * rda_invlen;
00127   BigReal rdd_invlen3 = rdd_invlen * rdd_invlen * rdd_invlen;
00128 
00129   // df = (1/r) (dU/dr)
00130   BigReal dfaa = qq * raa_invlen3 * (polyauaa*expauaa - 1);
00131   BigReal dfad = -qq * rad_invlen3 * (polyauad*expauad - 1);
00132   BigReal dfda = -qq * rda_invlen3 * (polyauda*expauda - 1);
00133   BigReal dfdd = qq * rdd_invlen3 * (polyaudd*expaudd - 1);
00134 
00135   Vector faa = -dfaa * raa;
00136   Vector fad = -dfad * rad;
00137   Vector fda = -dfda * rda;
00138   Vector fdd = -dfdd * rdd;
00139 
00140   //fepb - BKR scaling of alchemical drude terms
00141   //       NB: TI derivative is the _unscaled_ energy.
00142   if ( simParams->alchOn ) {
00143     // THIS ASSUMES THAT AN ATOM AND ITS DRUDE PARTICLE ARE ALWAYS IN THE SAME
00144     // PARTITION!
00145     int typeSum = 0;
00146     typeSum += (mol->get_fep_type(atomID[0]) == 2 ? -1 :\
00147         mol->get_fep_type(atomID[0]));
00148     typeSum += (mol->get_fep_type(atomID[2]) == 2 ? -1 :\
00149         mol->get_fep_type(atomID[2]));
00150     int order = (!simParams->alchDecouple ? 3 : 2);
00151 
00152     if ( 0 < typeSum && typeSum < order ) {
00153       reduction[tholeEnergyIndex_ti_1] += ethole;
00154       reduction[tholeEnergyIndex_f] += elec_lambda_12*ethole;
00155       ethole *= elec_lambda_1;
00156       faa *= elec_lambda_1;
00157       fad *= elec_lambda_1;
00158       fda *= elec_lambda_1;
00159       fdd *= elec_lambda_1; 
00160     } else if ( 0 > typeSum && typeSum > -order ) {
00161       reduction[tholeEnergyIndex_ti_2] += ethole;
00162       reduction[tholeEnergyIndex_f] += elec_lambda_22*ethole;
00163       ethole *= elec_lambda_2;
00164       faa *= elec_lambda_2;
00165       fad *= elec_lambda_2;
00166       fda *= elec_lambda_2;
00167       fdd *= elec_lambda_2;
00168     }
00169   }
00170   //fepe
00171 
00172   p[0]->f[localIndex[0]] += faa + fad;
00173   p[1]->f[localIndex[1]] += fda + fdd;
00174   p[2]->f[localIndex[2]] -= faa + fda;
00175   p[3]->f[localIndex[3]] -= fad + fdd;
00176 
00177   DebugM(3, "::computeForce() -- ending with delta energy " << ethole
00178       << std::endl);
00179   reduction[tholeEnergyIndex] += ethole;
00180 
00181   reduction[virialIndex_XX] += faa.x * raa.x + fad.x * rad.x
00182     + fda.x * rda.x + fdd.x * rdd.x;
00183   reduction[virialIndex_XY] += faa.x * raa.y + fad.x * rad.y
00184     + fda.x * rda.y + fdd.x * rdd.y;
00185   reduction[virialIndex_XZ] += faa.x * raa.z + fad.x * rad.z
00186     + fda.x * rda.z + fdd.x * rdd.z;
00187   reduction[virialIndex_YX] += faa.y * raa.x + fad.y * rad.x
00188     + fda.y * rda.x + fdd.y * rdd.x;
00189   reduction[virialIndex_YY] += faa.y * raa.y + fad.y * rad.y
00190     + fda.y * rda.y + fdd.y * rdd.y;
00191   reduction[virialIndex_YZ] += faa.y * raa.z + fad.y * rad.z
00192     + fda.y * rda.z + fdd.y * rdd.z;
00193   reduction[virialIndex_ZX] += faa.z * raa.x + fad.z * rad.x
00194     + fda.z * rda.x + fdd.z * rdd.x;
00195   reduction[virialIndex_ZY] += faa.z * raa.y + fad.z * rad.y
00196     + fda.z * rda.y + fdd.z * rdd.y;
00197   reduction[virialIndex_ZZ] += faa.z * raa.z + fad.z * rad.z
00198     + fda.z * rda.z + fdd.z * rdd.z;
00199 
00200   if (pressureProfileData) {
00201     BigReal zai = p[0]->x[localIndex[0]].position.z;
00202     BigReal zdi = p[1]->x[localIndex[1]].position.z;
00203     BigReal zaj = p[2]->x[localIndex[2]].position.z;
00204     BigReal zdj = p[3]->x[localIndex[3]].position.z;
00205     int nai = (int)floor((zai-pressureProfileMin)/pressureProfileThickness);
00206     int ndi = (int)floor((zdi-pressureProfileMin)/pressureProfileThickness);
00207     int naj = (int)floor((zaj-pressureProfileMin)/pressureProfileThickness);
00208     int ndj = (int)floor((zdj-pressureProfileMin)/pressureProfileThickness);
00209     pp_clamp(nai, pressureProfileSlabs);
00210     pp_clamp(ndi, pressureProfileSlabs);
00211     pp_clamp(naj, pressureProfileSlabs);
00212     pp_clamp(ndj, pressureProfileSlabs);
00213     int pai = p[0]->x[localIndex[0]].partition;
00214     int pdi = p[1]->x[localIndex[1]].partition;
00215     int paj = p[2]->x[localIndex[2]].partition;
00216     int pdj = p[3]->x[localIndex[3]].partition;
00217     int pn = pressureProfileAtomTypes;
00218     pp_reduction(pressureProfileSlabs, nai, naj,
00219         pai, paj, pn, faa.x * raa.x, faa.y * raa.y, faa.z * raa.z,
00220         pressureProfileData);
00221     pp_reduction(pressureProfileSlabs, nai, ndj,
00222         pai, pdj, pn, fad.x * rad.x, fad.y * rad.y, fad.z * rad.z,
00223         pressureProfileData);
00224     pp_reduction(pressureProfileSlabs, ndi, naj,
00225         pdi, paj, pn, fda.x * rda.x, fda.y * rda.y, fda.z * rda.z,
00226         pressureProfileData);
00227     pp_reduction(pressureProfileSlabs, ndi, ndj,
00228         pdi, pdj, pn, fdd.x * rdd.x, fdd.y * rdd.y, fdd.z * rdd.z,
00229         pressureProfileData);
00230   }
00231 #endif
00232 
00233  }
00234 }
00235 
00236 
00237 // The energy from the screened Coulomb correction of Thole is 
00238 // accumulated into the electrostatic potential energy.
00239 void TholeElem::submitReductionData(BigReal *data, SubmitReduction *reduction)
00240 {
00241   reduction->item(REDUCTION_ELECT_ENERGY) += data[tholeEnergyIndex];
00242   reduction->item(REDUCTION_ELECT_ENERGY_F) += data[tholeEnergyIndex_f];
00243   reduction->item(REDUCTION_ELECT_ENERGY_TI_1) += data[tholeEnergyIndex_ti_1];
00244   reduction->item(REDUCTION_ELECT_ENERGY_TI_2) += data[tholeEnergyIndex_ti_2];
00245   ADD_TENSOR(reduction,REDUCTION_VIRIAL_NORMAL,data,virialIndex);
00246 }
00247 

Generated on Sun Feb 18 01:17:12 2018 for NAMD by  doxygen 1.4.7