ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-4/src/UseTheForce/DarkSide/electrostatic.F90
(Generate patch)

Comparing trunk/OOPSE-4/src/UseTheForce/DarkSide/electrostatic.F90 (file contents):
Revision 2129 by chrisfen, Mon Mar 21 20:51:10 2005 UTC vs.
Revision 2302 by chrisfen, Fri Sep 16 16:07:39 2005 UTC

# Line 40 | Line 40 | module electrostatic_module
40   !!
41  
42   module electrostatic_module
43 <  
43 >
44    use force_globals
45    use definitions
46    use atype_module
# Line 54 | Line 54 | module electrostatic_module
54  
55    PRIVATE
56  
57 + #define __FORTRAN90
58 + #include "UseTheForce/DarkSide/fElectrostaticSummationMethod.h"
59 +
60    !! these prefactors convert the multipole interactions into kcal / mol
61    !! all were computed assuming distances are measured in angstroms
62    !! Charge-Charge, assuming charges are measured in electrons
# Line 68 | Line 71 | module electrostatic_module
71    !! This unit is also known affectionately as an esu centi-barn.
72    real(kind=dp), parameter :: pre14 = 69.13373_dp
73  
74 +  !! variables to handle different summation methods for long-range electrostatics:
75 +  integer, save :: summationMethod = NONE
76 +  logical, save :: summationMethodChecked = .false.
77 +  real(kind=DP), save :: defaultCutoff = 0.0_DP
78 +  logical, save :: haveDefaultCutoff = .false.
79 +  real(kind=DP), save :: dampingAlpha = 0.0_DP
80 +  logical, save :: haveDampingAlpha = .false.
81 +  real(kind=DP), save :: dielectric = 0.0_DP
82 +  logical, save :: haveDielectric = .false.
83 +  real(kind=DP), save :: constERFC = 0.0_DP
84 +  real(kind=DP), save :: constEXP = 0.0_DP
85 +  logical, save :: haveDWAconstants = .false.
86 +
87 +
88 +  public :: setElectrostaticSummationMethod
89 +  public :: setElectrostaticCutoffRadius
90 +  public :: setDampedWolfAlpha
91 +  public :: setReactionFieldDielectric
92    public :: newElectrostaticType
93    public :: setCharge
94    public :: setDipoleMoment
# Line 77 | Line 98 | module electrostatic_module
98    public :: getCharge
99    public :: getDipoleMoment
100    public :: pre22
101 +  public :: destroyElectrostaticTypes
102  
103    type :: Electrostatic
104       integer :: c_ident
# Line 84 | Line 106 | module electrostatic_module
106       logical :: is_Dipole = .false.
107       logical :: is_SplitDipole = .false.
108       logical :: is_Quadrupole = .false.
109 +     logical :: is_Tap = .false.
110       real(kind=DP) :: charge = 0.0_DP
111       real(kind=DP) :: dipole_moment = 0.0_DP
112       real(kind=DP) :: split_dipole_distance = 0.0_DP
# Line 94 | Line 117 | contains
117  
118   contains
119  
120 +  subroutine setElectrostaticSummationMethod(the_ESM)
121 +
122 +    integer, intent(in) :: the_ESM    
123 +
124 +    if ((the_ESM .le. 0) .or. (the_ESM .gt. REACTION_FIELD)) then
125 +       call handleError("setElectrostaticSummationMethod", "Unsupported Summation Method")
126 +    endif
127 +
128 +  end subroutine setElectrostaticSummationMethod
129 +
130 +  subroutine setElectrostaticCutoffRadius(thisRcut)
131 +    real(kind=dp), intent(in) :: thisRcut
132 +    defaultCutoff = thisRcut
133 +    haveDefaultCutoff = .true.
134 +  end subroutine setElectrostaticCutoffRadius
135 +
136 +  subroutine setDampedWolfAlpha(thisAlpha)
137 +    real(kind=dp), intent(in) :: thisAlpha
138 +    dampingAlpha = thisAlpha
139 +    haveDampingAlpha = .true.
140 +  end subroutine setDampedWolfAlpha
141 +  
142 +  subroutine setReactionFieldDielectric(thisDielectric)
143 +    real(kind=dp), intent(in) :: thisDielectric
144 +    dielectric = thisDielectric
145 +    haveDielectric = .true.
146 +  end subroutine setReactionFieldDielectric
147 +
148    subroutine newElectrostaticType(c_ident, is_Charge, is_Dipole, &
149 <       is_SplitDipole, is_Quadrupole, status)
150 <    
149 >       is_SplitDipole, is_Quadrupole, is_Tap, status)
150 >
151      integer, intent(in) :: c_ident
152      logical, intent(in) :: is_Charge
153      logical, intent(in) :: is_Dipole
154      logical, intent(in) :: is_SplitDipole
155      logical, intent(in) :: is_Quadrupole
156 +    logical, intent(in) :: is_Tap
157      integer, intent(out) :: status
158      integer :: nAtypes, myATID, i, j
159  
160      status = 0
161      myATID = getFirstMatchingElement(atypes, "c_ident", c_ident)
162 <    
162 >
163      !! Be simple-minded and assume that we need an ElectrostaticMap that
164      !! is the same size as the total number of atom types
165  
166      if (.not.allocated(ElectrostaticMap)) then
167 <      
167 >
168         nAtypes = getSize(atypes)
169 <    
169 >
170         if (nAtypes == 0) then
171            status = -1
172            return
173         end if
174 <      
174 >
175         if (.not. allocated(ElectrostaticMap)) then
176            allocate(ElectrostaticMap(nAtypes))
177         endif
178 <      
178 >
179      end if
180  
181      if (myATID .gt. size(ElectrostaticMap)) then
182         status = -1
183         return
184      endif
185 <    
185 >
186      ! set the values for ElectrostaticMap for this atom type:
187  
188      ElectrostaticMap(myATID)%c_ident = c_ident
# Line 138 | Line 190 | contains
190      ElectrostaticMap(myATID)%is_Dipole = is_Dipole
191      ElectrostaticMap(myATID)%is_SplitDipole = is_SplitDipole
192      ElectrostaticMap(myATID)%is_Quadrupole = is_Quadrupole
193 <    
193 >    ElectrostaticMap(myATID)%is_Tap = is_Tap
194 >
195    end subroutine newElectrostaticType
196  
197    subroutine setCharge(c_ident, charge, status)
# Line 166 | Line 219 | contains
219         call handleError("electrostatic", "Attempt to setCharge of an atom type that is not a charge!")
220         status = -1
221         return
222 <    endif      
222 >    endif
223  
224      ElectrostaticMap(myATID)%charge = charge
225    end subroutine setCharge
# Line 257 | Line 310 | contains
310         status = -1
311         return
312      endif
313 <    
313 >
314      do i = 1, 3
315 <          ElectrostaticMap(myATID)%quadrupole_moments(i) = &
316 <               quadrupole_moments(i)
317 <       enddo
315 >       ElectrostaticMap(myATID)%quadrupole_moments(i) = &
316 >            quadrupole_moments(i)
317 >    enddo
318  
319    end subroutine setQuadrupoleMoments
320  
321 <  
321 >
322    function getCharge(atid) result (c)
323      integer, intent(in) :: atid
324      integer :: localError
325      real(kind=dp) :: c
326 <    
326 >
327      if (.not.allocated(ElectrostaticMap)) then
328         call handleError("electrostatic", "no ElectrostaticMap was present before first call of getCharge!")
329         return
330      end if
331 <    
331 >
332      if (.not.ElectrostaticMap(atid)%is_Charge) then
333         call handleError("electrostatic", "getCharge was called for an atom type that isn't a charge!")
334         return
335      endif
336 <    
336 >
337      c = ElectrostaticMap(atid)%charge
338    end function getCharge
339  
# Line 288 | Line 341 | contains
341      integer, intent(in) :: atid
342      integer :: localError
343      real(kind=dp) :: dm
344 <    
344 >
345      if (.not.allocated(ElectrostaticMap)) then
346         call handleError("electrostatic", "no ElectrostaticMap was present before first call of getDipoleMoment!")
347         return
348      end if
349 <    
349 >
350      if (.not.ElectrostaticMap(atid)%is_Dipole) then
351         call handleError("electrostatic", "getDipoleMoment was called for an atom type that isn't a dipole!")
352         return
353      endif
354 <    
354 >
355      dm = ElectrostaticMap(atid)%dipole_moment
356    end function getDipoleMoment
357  
358 <  subroutine doElectrostaticPair(atom1, atom2, d, rij, r2, sw, &
359 <       vpair, fpair, pot, eFrame, f, t, do_pot)
360 <    
358 >  subroutine checkSummationMethod()
359 >
360 >    if (summationMethod .eq. DAMPED_WOLF) then
361 >       if (.not.haveDWAconstants) then
362 >          
363 >          if (.not.haveDampingAlpha) then
364 >             call handleError("checkSummationMethod", "no Damping Alpha set!")
365 >          endif
366 >          
367 >          if (.not.haveDefaultCutoff) then
368 >             call handleError("checkSummationMethod", "no Default Cutoff set!")
369 >          endif
370 >
371 >          constEXP = exp(-dampingAlpha*dampingAlpha*defaultCutoff*defaultCutoff)
372 >          constERFC = erfc(dampingAlpha*defaultCutoff)
373 >          
374 >          haveDWAconstants = .true.
375 >       endif
376 >    endif
377 >
378 >    if (summationMethod .eq. REACTION_FIELD) then
379 >       if (.not.haveDielectric) then
380 >          call handleError("checkSummationMethod", "no reaction field Dielectric set!")
381 >       endif
382 >    endif
383 >
384 >    summationMethodChecked = .true.
385 >  end subroutine checkSummationMethod
386 >
387 >
388 >
389 >  subroutine doElectrostaticPair(atom1, atom2, d, rij, r2, sw, &
390 >       vpair, fpair, pot, eFrame, f, t, do_pot, corrMethod, rcuti)
391 >
392      logical, intent(in) :: do_pot
393 <    
393 >
394      integer, intent(in) :: atom1, atom2
395      integer :: localError
396 +    integer, intent(in) :: corrMethod
397  
398 <    real(kind=dp), intent(in) :: rij, r2, sw
398 >    real(kind=dp), intent(in) :: rij, r2, sw, rcuti
399      real(kind=dp), intent(in), dimension(3) :: d
400      real(kind=dp), intent(inout) :: vpair
401      real(kind=dp), intent(inout), dimension(3) :: fpair
402  
403 <    real( kind = dp ) :: pot
403 >    real( kind = dp ) :: pot, swi
404      real( kind = dp ), dimension(9,nLocal) :: eFrame
405      real( kind = dp ), dimension(3,nLocal) :: f
406      real( kind = dp ), dimension(3,nLocal) :: t
407 <    
407 >
408      real (kind = dp), dimension(3) :: ux_i, uy_i, uz_i
409      real (kind = dp), dimension(3) :: ux_j, uy_j, uz_j
410      real (kind = dp), dimension(3) :: dudux_i, duduy_i, duduz_i
# Line 327 | Line 412 | contains
412  
413      logical :: i_is_Charge, i_is_Dipole, i_is_SplitDipole, i_is_Quadrupole
414      logical :: j_is_Charge, j_is_Dipole, j_is_SplitDipole, j_is_Quadrupole
415 +    logical :: i_is_Tap, j_is_Tap
416      integer :: me1, me2, id1, id2
417      real (kind=dp) :: q_i, q_j, mu_i, mu_j, d_i, d_j
418      real (kind=dp) :: qxx_i, qyy_i, qzz_i
# Line 336 | Line 422 | contains
422      real (kind=dp) :: cx2, cy2, cz2
423      real (kind=dp) :: ct_i, ct_j, ct_ij, a1
424      real (kind=dp) :: riji, ri, ri2, ri3, ri4
425 <    real (kind=dp) :: pref, vterm, epot, dudr    
425 >    real (kind=dp) :: pref, vterm, epot, dudr, vterm1, vterm2
426      real (kind=dp) :: xhat, yhat, zhat
427      real (kind=dp) :: dudx, dudy, dudz
428 <    real (kind=dp) :: drdxj, drdyj, drdzj
429 <    real (kind=dp) :: scale, sc2, bigR
428 >    real (kind=dp) :: scale, sc2, bigR, switcher, dswitcher
429 >    real (kind=dp) :: rcuti2, rcuti3, rcuti4
430  
431      if (.not.allocated(ElectrostaticMap)) then
432         call handleError("electrostatic", "no ElectrostaticMap was present before first call of do_electrostatic_pair!")
433         return
434      end if
435  
436 +    if (.not.summationMethodChecked) then
437 +       call checkSummationMethod()
438 +    endif
439 +
440 +
441   #ifdef IS_MPI
442      me1 = atid_Row(atom1)
443      me2 = atid_Col(atom2)
# Line 363 | Line 454 | contains
454      yhat = d(2) * riji
455      zhat = d(3) * riji
456  
457 <    drdxj = xhat
458 <    drdyj = yhat
459 <    drdzj = zhat
457 >    rcuti2 = rcuti*rcuti
458 >    rcuti3 = rcuti2*rcuti
459 >    rcuti4 = rcuti2*rcuti2
460  
461 <    !! logicals
461 >    swi = 1.0d0 / sw
462  
463 +    !! logicals
464      i_is_Charge = ElectrostaticMap(me1)%is_Charge
465      i_is_Dipole = ElectrostaticMap(me1)%is_Dipole
466      i_is_SplitDipole = ElectrostaticMap(me1)%is_SplitDipole
467      i_is_Quadrupole = ElectrostaticMap(me1)%is_Quadrupole
468 +    i_is_Tap = ElectrostaticMap(me1)%is_Tap
469  
470      j_is_Charge = ElectrostaticMap(me2)%is_Charge
471      j_is_Dipole = ElectrostaticMap(me2)%is_Dipole
472      j_is_SplitDipole = ElectrostaticMap(me2)%is_SplitDipole
473      j_is_Quadrupole = ElectrostaticMap(me2)%is_Quadrupole
474 +    j_is_Tap = ElectrostaticMap(me2)%is_Tap
475  
476      if (i_is_Charge) then
477         q_i = ElectrostaticMap(me1)%charge      
478      endif
479 <    
479 >
480      if (i_is_Dipole) then
481         mu_i = ElectrostaticMap(me1)%dipole_moment
482   #ifdef IS_MPI
# Line 399 | Line 493 | contains
493         if (i_is_SplitDipole) then
494            d_i = ElectrostaticMap(me1)%split_dipole_distance
495         endif
496 <      
496 >
497      endif
498  
499      if (i_is_Quadrupole) then
# Line 432 | Line 526 | contains
526         cz_i = uz_i(1)*xhat + uz_i(2)*yhat + uz_i(3)*zhat
527      endif
528  
435
529      if (j_is_Charge) then
530         q_j = ElectrostaticMap(me2)%charge      
531      endif
532 <    
532 >
533      if (j_is_Dipole) then
534         mu_j = ElectrostaticMap(me2)%dipole_moment
535   #ifdef IS_MPI
# Line 448 | Line 541 | contains
541         uz_j(2) = eFrame(6,atom2)
542         uz_j(3) = eFrame(9,atom2)
543   #endif
544 <       ct_j = uz_j(1)*drdxj + uz_j(2)*drdyj + uz_j(3)*drdzj
544 >       ct_j = uz_j(1)*xhat + uz_j(2)*yhat + uz_j(3)*zhat
545  
546         if (j_is_SplitDipole) then
547            d_j = ElectrostaticMap(me2)%split_dipole_distance
# Line 484 | Line 577 | contains
577         cy_j = uy_j(1)*xhat + uy_j(2)*yhat + uy_j(3)*zhat
578         cz_j = uz_j(1)*xhat + uz_j(2)*yhat + uz_j(3)*zhat
579      endif
580 +  
581 + !!$    switcher = 1.0d0
582 + !!$    dswitcher = 0.0d0
583 + !!$    ebalance = 0.0d0
584 + !!$    ! weaken the dipole interaction at close range for TAP water
585 + !!$    if (j_is_Tap .and. i_is_Tap) then
586 + !!$      call calc_switch(rij, mu_i, switcher, dswitcher)
587 + !!$    endif
588  
589      epot = 0.0_dp
590      dudx = 0.0_dp
# Line 501 | Line 602 | contains
602      if (i_is_Charge) then
603  
604         if (j_is_Charge) then
504          
505          vterm = pre11 * q_i * q_j * riji
506          vpair = vpair + vterm
507          epot = epot + sw*vterm
605  
606 <          dudr  = - sw * vterm * riji
606 >          if (corrMethod .eq. 1) then
607 >             vterm = pre11 * q_i * q_j * (riji - rcuti)
608  
609 <          dudx = dudx + dudr * drdxj
610 <          dudy = dudy + dudr * drdyj
611 <          dudz = dudz + dudr * drdzj
612 <      
609 >             vpair = vpair + vterm
610 >             epot = epot + sw * vterm
611 >            
612 >             dudr  = - sw * pre11 * q_i * q_j * (riji*riji*riji - rcuti2*rcuti)
613 >            
614 >             dudx = dudx + dudr * d(1)
615 >             dudy = dudy + dudr * d(2)
616 >             dudz = dudz + dudr * d(3)
617 >
618 >          else
619 >             vterm = pre11 * q_i * q_j * riji
620 >
621 >             vpair = vpair + vterm
622 >             epot = epot + sw * vterm
623 >            
624 >             dudr  = - sw * vterm * riji
625 >            
626 >             dudx = dudx + dudr * xhat
627 >             dudy = dudy + dudr * yhat
628 >             dudz = dudz + dudr * zhat
629 >
630 >          endif
631 >
632         endif
633  
634         if (j_is_Dipole) then
635  
636 <          if (j_is_SplitDipole) then
520 <             BigR = sqrt(r2 + 0.25_dp * d_j * d_j)
521 <             ri = 1.0_dp / BigR
522 <             scale = rij * ri
523 <          else
524 <             ri = riji
525 <             scale = 1.0_dp
526 <          endif
636 >          pref = sw * pre12 * q_i * mu_j
637  
638 <          ri2 = ri * ri
639 <          ri3 = ri2 * ri
640 <          sc2 = scale * scale
638 >          if (corrMethod .eq. 1) then
639 >             ri2 = riji * riji
640 >             ri3 = ri2 * riji
641 >
642 >             vterm = - pref * ct_j * (ri2 - rcuti2)
643 >             vpair = vpair + swi*vterm
644 >             epot = epot + vterm
645              
646 <          pref = pre12 * q_i * mu_j
647 <          vterm = pref * ct_j * ri2 * scale
648 <          vpair = vpair + vterm
649 <          epot = epot + sw * vterm
646 >             !! this has a + sign in the () because the rij vector is
647 >             !! r_j - r_i and the charge-dipole potential takes the origin
648 >             !! as the point dipole, which is atom j in this case.
649 >            
650 >             dudx = dudx - pref * ( ri3*( uz_j(1) - 3.0d0*ct_j*xhat) &
651 >                  - rcuti3*( uz_j(1) - 3.0d0*ct_j*d(1)*rcuti ) )
652 >             dudy = dudy - pref * ( ri3*( uz_j(2) - 3.0d0*ct_j*yhat) &
653 >                  - rcuti3*( uz_j(2) - 3.0d0*ct_j*d(2)*rcuti ) )
654 >             dudz = dudz - pref * ( ri3*( uz_j(3) - 3.0d0*ct_j*zhat) &
655 >                  - rcuti3*( uz_j(3) - 3.0d0*ct_j*d(3)*rcuti ) )
656 >            
657 >             duduz_j(1) = duduz_j(1) - pref*( ri2*xhat - d(1)*rcuti3 )
658 >             duduz_j(2) = duduz_j(2) - pref*( ri2*yhat - d(2)*rcuti3 )
659 >             duduz_j(3) = duduz_j(3) - pref*( ri2*zhat - d(3)*rcuti3 )
660  
661 <          !! this has a + sign in the () because the rij vector is
662 <          !! r_j - r_i and the charge-dipole potential takes the origin
663 <          !! as the point dipole, which is atom j in this case.
661 >          else
662 >             if (j_is_SplitDipole) then
663 >                BigR = sqrt(r2 + 0.25_dp * d_j * d_j)
664 >                ri = 1.0_dp / BigR
665 >                scale = rij * ri
666 >             else
667 >                ri = riji
668 >                scale = 1.0_dp
669 >             endif
670 >            
671 >             ri2 = ri * ri
672 >             ri3 = ri2 * ri
673 >             sc2 = scale * scale
674 >            
675 >             vterm = - pref * ct_j * ri2 * scale
676 >             vpair = vpair + swi * vterm
677 >             epot = epot + vterm
678 >            
679 >             !! this has a + sign in the () because the rij vector is
680 >             !! r_j - r_i and the charge-dipole potential takes the origin
681 >             !! as the point dipole, which is atom j in this case.
682 >            
683 >             dudx = dudx - pref * ri3 * ( uz_j(1) - 3.0d0*ct_j*xhat*sc2)
684 >             dudy = dudy - pref * ri3 * ( uz_j(2) - 3.0d0*ct_j*yhat*sc2)
685 >             dudz = dudz - pref * ri3 * ( uz_j(3) - 3.0d0*ct_j*zhat*sc2)
686 >            
687 >             duduz_j(1) = duduz_j(1) - pref * ri2 * xhat * scale
688 >             duduz_j(2) = duduz_j(2) - pref * ri2 * yhat * scale
689 >             duduz_j(3) = duduz_j(3) - pref * ri2 * zhat * scale
690  
691 <          dudx = dudx + pref * sw * ri3 * ( uz_j(1) + 3.0d0*ct_j*xhat*sc2)
542 <          dudy = dudy + pref * sw * ri3 * ( uz_j(2) + 3.0d0*ct_j*yhat*sc2)
543 <          dudz = dudz + pref * sw * ri3 * ( uz_j(3) + 3.0d0*ct_j*zhat*sc2)
544 <
545 <          duduz_j(1) = duduz_j(1) - pref * sw * ri2 * xhat * scale
546 <          duduz_j(2) = duduz_j(2) - pref * sw * ri2 * yhat * scale
547 <          duduz_j(3) = duduz_j(3) - pref * sw * ri2 * zhat * scale
548 <          
691 >          endif
692         endif
693  
694         if (j_is_Quadrupole) then
# Line 557 | Line 700 | contains
700            cz2 = cz_j * cz_j
701  
702  
703 <          pref =  pre14 * q_i / 6.0_dp
561 <          vterm = pref * ri3 * (qxx_j * (3.0_dp*cx2 - 1.0_dp) + &
562 <               qyy_j * (3.0_dp*cy2 - 1.0_dp) + &
563 <               qzz_j * (3.0_dp*cz2 - 1.0_dp))
564 <          vpair = vpair + vterm
565 <          epot = epot + sw * vterm
703 >          pref =  sw * pre14 * q_i / 3.0_dp
704  
705 <          dudx = dudx - 5.0_dp*sw*vterm*riji*xhat - pref * sw * ri4 * ( &
706 <               qxx_j*(6.0_dp*cx_j*ux_j(1) - 2.0_dp*xhat) + &
707 <               qyy_j*(6.0_dp*cy_j*uy_j(1) - 2.0_dp*xhat) + &
708 <               qzz_j*(6.0_dp*cz_j*uz_j(1) - 2.0_dp*xhat) )
709 <          dudy = dudy - 5.0_dp*sw*vterm*riji*yhat - pref * sw * ri4 * ( &
710 <               qxx_j*(6.0_dp*cx_j*ux_j(2) - 2.0_dp*yhat) + &
711 <               qyy_j*(6.0_dp*cy_j*uy_j(2) - 2.0_dp*yhat) + &
712 <               qzz_j*(6.0_dp*cz_j*uz_j(2) - 2.0_dp*yhat) )
713 <          dudz = dudz - 5.0_dp*sw*vterm*riji*zhat - pref * sw * ri4 * ( &
714 <               qxx_j*(6.0_dp*cx_j*ux_j(3) - 2.0_dp*zhat) + &
715 <               qyy_j*(6.0_dp*cy_j*uy_j(3) - 2.0_dp*zhat) + &
716 <               qzz_j*(6.0_dp*cz_j*uz_j(3) - 2.0_dp*zhat) )
717 <          
718 <          dudux_j(1) = dudux_j(1) + pref * sw * ri3 * (qxx_j*6.0_dp*cx_j*xhat)
719 <          dudux_j(2) = dudux_j(2) + pref * sw * ri3 * (qxx_j*6.0_dp*cx_j*yhat)
720 <          dudux_j(3) = dudux_j(3) + pref * sw * ri3 * (qxx_j*6.0_dp*cx_j*zhat)
721 <
722 <          duduy_j(1) = duduy_j(1) + pref * sw * ri3 * (qyy_j*6.0_dp*cy_j*xhat)
723 <          duduy_j(2) = duduy_j(2) + pref * sw * ri3 * (qyy_j*6.0_dp*cy_j*yhat)
724 <          duduy_j(3) = duduy_j(3) + pref * sw * ri3 * (qyy_j*6.0_dp*cy_j*zhat)
725 <
726 <          duduz_j(1) = duduz_j(1) + pref * sw * ri3 * (qzz_j*6.0_dp*cz_j*xhat)
727 <          duduz_j(2) = duduz_j(2) + pref * sw * ri3 * (qzz_j*6.0_dp*cz_j*yhat)
728 <          duduz_j(3) = duduz_j(3) + pref * sw * ri3 * (qzz_j*6.0_dp*cz_j*zhat)
729 <       endif
730 <
731 <    endif
732 <  
733 <    if (i_is_Dipole) then
734 <      
735 <       if (j_is_Charge) then
736 <
737 <          if (i_is_SplitDipole) then
738 <             BigR = sqrt(r2 + 0.25_dp * d_i * d_i)
739 <             ri = 1.0_dp / BigR
740 <             scale = rij * ri
705 >          if (corrMethod .eq. 1) then
706 >             vterm1 = pref * ri3*( qxx_j * (3.0_dp*cx2 - 1.0_dp) + &
707 >                  qyy_j * (3.0_dp*cy2 - 1.0_dp) + &
708 >                  qzz_j * (3.0_dp*cz2 - 1.0_dp) )
709 >             vterm2 = pref * rcuti3*( qxx_j * (3.0_dp*cx2 - 1.0_dp) + &
710 >                  qyy_j * (3.0_dp*cy2 - 1.0_dp) + &
711 >                  qzz_j * (3.0_dp*cz2 - 1.0_dp) )
712 >             vpair = vpair + swi*( vterm1 - vterm2 )
713 >             epot = epot + ( vterm1 - vterm2 )
714 >            
715 >             dudx = dudx - (5.0_dp * &
716 >                  (vterm1*riji*xhat - vterm2*rcuti2*d(1))) + pref * ( &
717 >                  (ri4 - rcuti4)*(qxx_j*(6.0_dp*cx_j*ux_j(1)) - &
718 >                  qxx_j*2.0_dp*(xhat - rcuti*d(1))) + &
719 >                  (ri4 - rcuti4)*(qyy_j*(6.0_dp*cy_j*uy_j(1)) - &
720 >                  qyy_j*2.0_dp*(xhat - rcuti*d(1))) + &
721 >                  (ri4 - rcuti4)*(qzz_j*(6.0_dp*cz_j*uz_j(1)) - &
722 >                  qzz_j*2.0_dp*(xhat - rcuti*d(1))) )
723 >             dudy = dudy - (5.0_dp * &
724 >                  (vterm1*riji*yhat - vterm2*rcuti2*d(2))) + pref * ( &
725 >                  (ri4 - rcuti4)*(qxx_j*(6.0_dp*cx_j*ux_j(2)) - &
726 >                  qxx_j*2.0_dp*(yhat - rcuti*d(2))) + &
727 >                  (ri4 - rcuti4)*(qyy_j*(6.0_dp*cy_j*uy_j(2)) - &
728 >                  qyy_j*2.0_dp*(yhat - rcuti*d(2))) + &
729 >                  (ri4 - rcuti4)*(qzz_j*(6.0_dp*cz_j*uz_j(2)) - &
730 >                  qzz_j*2.0_dp*(yhat - rcuti*d(2))) )
731 >             dudz = dudz - (5.0_dp * &
732 >                  (vterm1*riji*zhat - vterm2*rcuti2*d(3))) + pref * ( &
733 >                  (ri4 - rcuti4)*(qxx_j*(6.0_dp*cx_j*ux_j(3)) - &
734 >                  qxx_j*2.0_dp*(zhat - rcuti*d(3))) + &
735 >                  (ri4 - rcuti4)*(qyy_j*(6.0_dp*cy_j*uy_j(3)) - &
736 >                  qyy_j*2.0_dp*(zhat - rcuti*d(3))) + &
737 >                  (ri4 - rcuti4)*(qzz_j*(6.0_dp*cz_j*uz_j(3)) - &
738 >                  qzz_j*2.0_dp*(zhat - rcuti*d(3))) )
739 >            
740 >             dudux_j(1) = dudux_j(1) + pref * (ri3*(qxx_j*6.0_dp*cx_j*xhat) - &
741 >                  rcuti4*(qxx_j*6.0_dp*cx_j*d(1)))
742 >             dudux_j(2) = dudux_j(2) + pref * (ri3*(qxx_j*6.0_dp*cx_j*yhat) - &
743 >                  rcuti4*(qxx_j*6.0_dp*cx_j*d(2)))
744 >             dudux_j(3) = dudux_j(3) + pref * (ri3*(qxx_j*6.0_dp*cx_j*zhat) - &
745 >                  rcuti4*(qxx_j*6.0_dp*cx_j*d(3)))
746 >            
747 >             duduy_j(1) = duduy_j(1) + pref * (ri3*(qyy_j*6.0_dp*cy_j*xhat) - &
748 >                  rcuti4*(qyy_j*6.0_dp*cx_j*d(1)))
749 >             duduy_j(2) = duduy_j(2) + pref * (ri3*(qyy_j*6.0_dp*cy_j*yhat) - &
750 >                  rcuti4*(qyy_j*6.0_dp*cx_j*d(2)))
751 >             duduy_j(3) = duduy_j(3) + pref * (ri3*(qyy_j*6.0_dp*cy_j*zhat) - &
752 >                  rcuti4*(qyy_j*6.0_dp*cx_j*d(3)))
753 >            
754 >             duduz_j(1) = duduz_j(1) + pref * (ri3*(qzz_j*6.0_dp*cz_j*xhat) - &
755 >                  rcuti4*(qzz_j*6.0_dp*cx_j*d(1)))
756 >             duduz_j(2) = duduz_j(2) + pref * (ri3*(qzz_j*6.0_dp*cz_j*yhat) - &
757 >                  rcuti4*(qzz_j*6.0_dp*cx_j*d(2)))
758 >             duduz_j(3) = duduz_j(3) + pref * (ri3*(qzz_j*6.0_dp*cz_j*zhat) - &
759 >                  rcuti4*(qzz_j*6.0_dp*cx_j*d(3)))
760 >        
761            else
762 <             ri = riji
763 <             scale = 1.0_dp
764 <          endif
765 <
766 <          ri2 = ri * ri
609 <          ri3 = ri2 * ri
610 <          sc2 = scale * scale
762 >             vterm = pref * ri3 * (qxx_j * (3.0_dp*cx2 - 1.0_dp) + &
763 >                  qyy_j * (3.0_dp*cy2 - 1.0_dp) + &
764 >                  qzz_j * (3.0_dp*cz2 - 1.0_dp))
765 >             vpair = vpair + swi * vterm
766 >             epot = epot + vterm
767              
768 <          pref = pre12 * q_j * mu_i
769 <          vterm = pref * ct_i * ri2 * scale
770 <          vpair = vpair + vterm
771 <          epot = epot + sw * vterm
768 >             dudx = dudx - 5.0_dp*vterm*riji*xhat + pref * ri4 * ( &
769 >                  qxx_j*(6.0_dp*cx_j*ux_j(1) - 2.0_dp*xhat) + &
770 >                  qyy_j*(6.0_dp*cy_j*uy_j(1) - 2.0_dp*xhat) + &
771 >                  qzz_j*(6.0_dp*cz_j*uz_j(1) - 2.0_dp*xhat) )
772 >             dudy = dudy - 5.0_dp*vterm*riji*yhat + pref * ri4 * ( &
773 >                  qxx_j*(6.0_dp*cx_j*ux_j(2) - 2.0_dp*yhat) + &
774 >                  qyy_j*(6.0_dp*cy_j*uy_j(2) - 2.0_dp*yhat) + &
775 >                  qzz_j*(6.0_dp*cz_j*uz_j(2) - 2.0_dp*yhat) )
776 >             dudz = dudz - 5.0_dp*vterm*riji*zhat + pref * ri4 * ( &
777 >                  qxx_j*(6.0_dp*cx_j*ux_j(3) - 2.0_dp*zhat) + &
778 >                  qyy_j*(6.0_dp*cy_j*uy_j(3) - 2.0_dp*zhat) + &
779 >                  qzz_j*(6.0_dp*cz_j*uz_j(3) - 2.0_dp*zhat) )
780 >            
781 >             dudux_j(1) = dudux_j(1) + pref * ri3*(qxx_j*6.0_dp*cx_j*xhat)
782 >             dudux_j(2) = dudux_j(2) + pref * ri3*(qxx_j*6.0_dp*cx_j*yhat)
783 >             dudux_j(3) = dudux_j(3) + pref * ri3*(qxx_j*6.0_dp*cx_j*zhat)
784 >            
785 >             duduy_j(1) = duduy_j(1) + pref * ri3*(qyy_j*6.0_dp*cy_j*xhat)
786 >             duduy_j(2) = duduy_j(2) + pref * ri3*(qyy_j*6.0_dp*cy_j*yhat)
787 >             duduy_j(3) = duduy_j(3) + pref * ri3*(qyy_j*6.0_dp*cy_j*zhat)
788 >            
789 >             duduz_j(1) = duduz_j(1) + pref * ri3*(qzz_j*6.0_dp*cz_j*xhat)
790 >             duduz_j(2) = duduz_j(2) + pref * ri3*(qzz_j*6.0_dp*cz_j*yhat)
791 >             duduz_j(3) = duduz_j(3) + pref * ri3*(qzz_j*6.0_dp*cz_j*zhat)
792 >          
793 >          endif
794 >       endif
795 >    endif
796  
797 <          dudx = dudx + pref * sw * ri3 * ( uz_i(1) - 3.0d0 * ct_i * xhat*sc2)
618 <          dudy = dudy + pref * sw * ri3 * ( uz_i(2) - 3.0d0 * ct_i * yhat*sc2)
619 <          dudz = dudz + pref * sw * ri3 * ( uz_i(3) - 3.0d0 * ct_i * zhat*sc2)
797 >    if (i_is_Dipole) then
798  
799 <          duduz_i(1) = duduz_i(1) + pref * sw * ri2 * xhat * scale
622 <          duduz_i(2) = duduz_i(2) + pref * sw * ri2 * yhat * scale
623 <          duduz_i(3) = duduz_i(3) + pref * sw * ri2 * zhat * scale
624 <       endif
799 >       if (j_is_Charge) then
800  
801 <       if (j_is_Dipole) then
801 >          pref = sw * pre12 * q_j * mu_i
802  
803 <          if (i_is_SplitDipole) then
804 <             if (j_is_SplitDipole) then
805 <                BigR = sqrt(r2 + 0.25_dp * d_i * d_i + 0.25_dp * d_j * d_j)
806 <             else
807 <                BigR = sqrt(r2 + 0.25_dp * d_i * d_i)
808 <             endif
809 <             ri = 1.0_dp / BigR
810 <             scale = rij * ri                
803 >          if (corrMethod .eq. 1) then
804 >             ri2 = riji * riji
805 >             ri3 = ri2 * riji
806 >
807 >             vterm = pref * ct_i * (ri2 - rcuti2)
808 >             vpair = vpair + swi * vterm
809 >             epot = epot + vterm
810 >            
811 >             !! this has a + sign in the () because the rij vector is
812 >             !! r_j - r_i and the charge-dipole potential takes the origin
813 >             !! as the point dipole, which is atom j in this case.
814 >            
815 >             dudx = dudx + pref * ( ri3*( uz_i(1) - 3.0d0*ct_i*xhat) &
816 >                  - rcuti3*( uz_i(1) - 3.0d0*ct_i*d(1)*rcuti ) )
817 >             dudy = dudy + pref * ( ri3*( uz_i(2) - 3.0d0*ct_i*yhat) &
818 >                  - rcuti3*( uz_i(2) - 3.0d0*ct_i*d(2)*rcuti ) )
819 >             dudz = dudz + pref * ( ri3*( uz_i(3) - 3.0d0*ct_i*zhat) &
820 >                  - rcuti3*( uz_i(3) - 3.0d0*ct_i*d(3)*rcuti ) )
821 >            
822 >             duduz_i(1) = duduz_i(1) - pref*( ri2*xhat - d(1)*rcuti3 )
823 >             duduz_i(2) = duduz_i(2) - pref*( ri2*yhat - d(2)*rcuti3 )
824 >             duduz_i(3) = duduz_i(3) - pref*( ri2*zhat - d(3)*rcuti3 )
825 >
826            else
827 <             if (j_is_SplitDipole) then
828 <                BigR = sqrt(r2 + 0.25_dp * d_j * d_j)
827 >             if (i_is_SplitDipole) then
828 >                BigR = sqrt(r2 + 0.25_dp * d_i * d_i)
829                  ri = 1.0_dp / BigR
830 <                scale = rij * ri                            
831 <             else                
830 >                scale = rij * ri
831 >             else
832                  ri = riji
833                  scale = 1.0_dp
834               endif
835 +            
836 +             ri2 = ri * ri
837 +             ri3 = ri2 * ri
838 +             sc2 = scale * scale
839 +            
840 +             vterm = pref * ct_i * ri2 * scale
841 +             vpair = vpair + swi * vterm
842 +             epot = epot + vterm
843 +            
844 +             dudx = dudx + pref * ri3 * ( uz_i(1) - 3.0d0 * ct_i * xhat*sc2)
845 +             dudy = dudy + pref * ri3 * ( uz_i(2) - 3.0d0 * ct_i * yhat*sc2)
846 +             dudz = dudz + pref * ri3 * ( uz_i(3) - 3.0d0 * ct_i * zhat*sc2)
847 +            
848 +             duduz_i(1) = duduz_i(1) + pref * ri2 * xhat * scale
849 +             duduz_i(2) = duduz_i(2) + pref * ri2 * yhat * scale
850 +             duduz_i(3) = duduz_i(3) + pref * ri2 * zhat * scale
851            endif
852 +       endif
853  
854 <          ct_ij = uz_i(1)*uz_j(1) + uz_i(2)*uz_j(2) + uz_i(3)*uz_j(3)
854 >       if (j_is_Dipole) then
855  
856 <          ri2 = ri * ri
650 <          ri3 = ri2 * ri
651 <          ri4 = ri2 * ri2
652 <          sc2 = scale * scale
856 >          pref = sw * pre22 * mu_i * mu_j
857  
858 <          pref = pre22 * mu_i * mu_j
859 <          vterm = pref * ri3 * (ct_ij - 3.0d0 * ct_i * ct_j * sc2)
860 <          vpair = vpair + vterm
861 <          epot = epot + sw * vterm
658 <          
659 <          a1 = 5.0d0 * ct_i * ct_j * sc2 - ct_ij
858 >          if (corrMethod .eq. 1) then
859 >             ri2 = riji * riji
860 >             ri3 = ri2 * riji
861 >             ri4 = ri2 * ri2
862  
863 <          dudx=dudx+pref*sw*3.0d0*ri4*scale*(a1*xhat-ct_i*uz_j(1)-ct_j*uz_i(1))
864 <          dudy=dudy+pref*sw*3.0d0*ri4*scale*(a1*yhat-ct_i*uz_j(2)-ct_j*uz_i(2))
865 <          dudz=dudz+pref*sw*3.0d0*ri4*scale*(a1*zhat-ct_i*uz_j(3)-ct_j*uz_i(3))
866 <
867 <          duduz_i(1) = duduz_i(1) + pref*sw*ri3*(uz_j(1) - 3.0d0*ct_j*xhat*sc2)
868 <          duduz_i(2) = duduz_i(2) + pref*sw*ri3*(uz_j(2) - 3.0d0*ct_j*yhat*sc2)
869 <          duduz_i(3) = duduz_i(3) + pref*sw*ri3*(uz_j(3) - 3.0d0*ct_j*zhat*sc2)
870 <
871 <          duduz_j(1) = duduz_j(1) + pref*sw*ri3*(uz_i(1) - 3.0d0*ct_i*xhat*sc2)
872 <          duduz_j(2) = duduz_j(2) + pref*sw*ri3*(uz_i(2) - 3.0d0*ct_i*yhat*sc2)
873 <          duduz_j(3) = duduz_j(3) + pref*sw*ri3*(uz_i(3) - 3.0d0*ct_i*zhat*sc2)
863 >             vterm = pref * (ri3 - rcuti3) * (ct_ij - 3.0d0 * ct_i * ct_j)
864 >             vpair = vpair + swi * vterm
865 >             epot = epot + vterm
866 >            
867 >             a1 = 5.0d0 * ct_i * ct_j - ct_ij
868 >            
869 >             dudx = dudx + pref*3.0d0*ri4 &
870 >                  *(a1*xhat-ct_i*uz_j(1)-ct_j*uz_i(1)) - &
871 >                  pref*3.0d0*rcuti4*(a1*rcuti*d(1)-ct_i*uz_j(1)-ct_j*uz_i(1))
872 >             dudy = dudy + pref*3.0d0*ri4 &
873 >                  *(a1*yhat-ct_i*uz_j(2)-ct_j*uz_i(2)) - &
874 >                  pref*3.0d0*rcuti4*(a1*rcuti*d(2)-ct_i*uz_j(2)-ct_j*uz_i(2))
875 >             dudz = dudz + pref*3.0d0*ri4 &
876 >                  *(a1*zhat-ct_i*uz_j(3)-ct_j*uz_i(3)) - &
877 >                  pref*3.0d0*rcuti4*(a1*rcuti*d(3)-ct_i*uz_j(3)-ct_j*uz_i(3))
878 >            
879 >             duduz_i(1) = duduz_i(1) + pref*(ri3*(uz_j(1) - 3.0d0*ct_j*xhat) &
880 >                  - rcuti3*(uz_j(1) - 3.0d0*ct_j*d(1)*rcuti))
881 >             duduz_i(2) = duduz_i(2) + pref*(ri3*(uz_j(2) - 3.0d0*ct_j*yhat) &
882 >                  - rcuti3*(uz_j(2) - 3.0d0*ct_j*d(2)*rcuti))
883 >             duduz_i(3) = duduz_i(3) + pref*(ri3*(uz_j(3) - 3.0d0*ct_j*zhat) &
884 >                  - rcuti3*(uz_j(3) - 3.0d0*ct_j*d(3)*rcuti))
885 >             duduz_j(1) = duduz_j(1) + pref*(ri3*(uz_i(1) - 3.0d0*ct_i*xhat) &
886 >                  - rcuti3*(uz_i(1) - 3.0d0*ct_i*d(1)*rcuti))
887 >             duduz_j(2) = duduz_j(2) + pref*(ri3*(uz_i(2) - 3.0d0*ct_i*yhat) &
888 >                  - rcuti3*(uz_i(2) - 3.0d0*ct_i*d(2)*rcuti))
889 >             duduz_j(3) = duduz_j(3) + pref*(ri3*(uz_i(3) - 3.0d0*ct_i*zhat) &
890 >                  - rcuti3*(uz_i(3) - 3.0d0*ct_i*d(3)*rcuti))
891 >          else
892 >            
893 >             if (i_is_SplitDipole) then
894 >                if (j_is_SplitDipole) then
895 >                   BigR = sqrt(r2 + 0.25_dp * d_i * d_i + 0.25_dp * d_j * d_j)
896 >                else
897 >                   BigR = sqrt(r2 + 0.25_dp * d_i * d_i)
898 >                endif
899 >                ri = 1.0_dp / BigR
900 >                scale = rij * ri                
901 >             else
902 >                if (j_is_SplitDipole) then
903 >                   BigR = sqrt(r2 + 0.25_dp * d_j * d_j)
904 >                   ri = 1.0_dp / BigR
905 >                   scale = rij * ri                            
906 >                else                
907 >                   ri = riji
908 >                   scale = 1.0_dp
909 >                endif
910 >             endif
911 >            
912 >             ct_ij = uz_i(1)*uz_j(1) + uz_i(2)*uz_j(2) + uz_i(3)*uz_j(3)
913 >            
914 >             ri2 = ri * ri
915 >             ri3 = ri2 * ri
916 >             ri4 = ri2 * ri2
917 >             sc2 = scale * scale
918 >            
919 >             vterm = pref * ri3 * (ct_ij - 3.0d0 * ct_i * ct_j * sc2)
920 >             vpair = vpair + swi * vterm
921 >             epot = epot + vterm
922 >            
923 >             a1 = 5.0d0 * ct_i * ct_j * sc2 - ct_ij
924 >            
925 >             dudx = dudx + pref*3.0d0*ri4*scale &
926 >                  *(a1*xhat-ct_i*uz_j(1)-ct_j*uz_i(1))
927 >             dudy = dudy + pref*3.0d0*ri4*scale &
928 >                  *(a1*yhat-ct_i*uz_j(2)-ct_j*uz_i(2))
929 >             dudz = dudz + pref*3.0d0*ri4*scale &
930 >                  *(a1*zhat-ct_i*uz_j(3)-ct_j*uz_i(3))
931 >            
932 >             duduz_i(1) = duduz_i(1) + pref*ri3 &
933 >                  *(uz_j(1) - 3.0d0*ct_j*xhat*sc2)
934 >             duduz_i(2) = duduz_i(2) + pref*ri3 &
935 >                  *(uz_j(2) - 3.0d0*ct_j*yhat*sc2)
936 >             duduz_i(3) = duduz_i(3) + pref*ri3 &
937 >                  *(uz_j(3) - 3.0d0*ct_j*zhat*sc2)
938 >            
939 >             duduz_j(1) = duduz_j(1) + pref*ri3 &
940 >                  *(uz_i(1) - 3.0d0*ct_i*xhat*sc2)
941 >             duduz_j(2) = duduz_j(2) + pref*ri3 &
942 >                  *(uz_i(2) - 3.0d0*ct_i*yhat*sc2)
943 >             duduz_j(3) = duduz_j(3) + pref*ri3 &
944 >                  *(uz_i(3) - 3.0d0*ct_i*zhat*sc2)
945 >          endif
946         endif
673
947      endif
948  
949      if (i_is_Quadrupole) then
950         if (j_is_Charge) then
951 <          
951 >
952            ri2 = riji * riji
953            ri3 = ri2 * riji
954            ri4 = ri2 * ri2
955            cx2 = cx_i * cx_i
956            cy2 = cy_i * cy_i
957            cz2 = cz_i * cz_i
958 <          
959 <          pref = pre14 * q_j / 6.0_dp
960 <          vterm = pref * ri3 * (qxx_i * (3.0_dp*cx2 - 1.0_dp) + &
961 <               qyy_i * (3.0_dp*cy2 - 1.0_dp) + &
962 <               qzz_i * (3.0_dp*cz2 - 1.0_dp))
963 <          vpair = vpair + vterm
964 <          epot = epot + sw * vterm
965 <          
966 <          dudx = dudx - 5.0_dp*sw*vterm*riji*xhat - pref * sw * ri4 * ( &
967 <               qxx_i*(6.0_dp*cx_i*ux_i(1) - 2.0_dp*xhat) + &
968 <               qyy_i*(6.0_dp*cy_i*uy_i(1) - 2.0_dp*xhat) + &
969 <               qzz_i*(6.0_dp*cz_i*uz_i(1) - 2.0_dp*xhat) )
970 <          dudy = dudy - 5.0_dp*sw*vterm*riji*yhat - pref * sw * ri4 * ( &
971 <               qxx_i*(6.0_dp*cx_i*ux_i(2) - 2.0_dp*yhat) + &
972 <               qyy_i*(6.0_dp*cy_i*uy_i(2) - 2.0_dp*yhat) + &
973 <               qzz_i*(6.0_dp*cz_i*uz_i(2) - 2.0_dp*yhat) )
974 <          dudz = dudz - 5.0_dp*sw*vterm*riji*zhat - pref * sw * ri4 * ( &
975 <               qxx_i*(6.0_dp*cx_i*ux_i(3) - 2.0_dp*zhat) + &
976 <               qyy_i*(6.0_dp*cy_i*uy_i(3) - 2.0_dp*zhat) + &
977 <               qzz_i*(6.0_dp*cz_i*uz_i(3) - 2.0_dp*zhat) )
978 <          
979 <          dudux_i(1) = dudux_i(1) + pref * sw * ri3 * (qxx_i*6.0_dp*cx_i*xhat)
980 <          dudux_i(2) = dudux_i(2) + pref * sw * ri3 * (qxx_i*6.0_dp*cx_i*yhat)
981 <          dudux_i(3) = dudux_i(3) + pref * sw * ri3 * (qxx_i*6.0_dp*cx_i*zhat)
982 <          
983 <          duduy_i(1) = duduy_i(1) + pref * sw * ri3 * (qyy_i*6.0_dp*cy_i*xhat)
984 <          duduy_i(2) = duduy_i(2) + pref * sw * ri3 * (qyy_i*6.0_dp*cy_i*yhat)
985 <          duduy_i(3) = duduy_i(3) + pref * sw * ri3 * (qyy_i*6.0_dp*cy_i*zhat)
986 <          
987 <          duduz_i(1) = duduz_i(1) + pref * sw * ri3 * (qzz_i*6.0_dp*cz_i*xhat)
988 <          duduz_i(2) = duduz_i(2) + pref * sw * ri3 * (qzz_i*6.0_dp*cz_i*yhat)
989 <          duduz_i(3) = duduz_i(3) + pref * sw * ri3 * (qzz_i*6.0_dp*cz_i*zhat)
958 >
959 >          pref = sw * pre14 * q_j / 3.0_dp
960 >
961 >          if (corrMethod .eq. 1) then
962 >             vterm1 = pref * ri3*( qxx_i * (3.0_dp*cx2 - 1.0_dp) + &
963 >                  qyy_i * (3.0_dp*cy2 - 1.0_dp) + &
964 >                  qzz_i * (3.0_dp*cz2 - 1.0_dp) )
965 >             vterm2 = pref * rcuti3*( qxx_i * (3.0_dp*cx2 - 1.0_dp) + &
966 >                  qyy_i * (3.0_dp*cy2 - 1.0_dp) + &
967 >                  qzz_i * (3.0_dp*cz2 - 1.0_dp) )
968 >             vpair = vpair + swi * ( vterm1 - vterm2 )
969 >             epot = epot + ( vterm1 - vterm2 )
970 >            
971 >             dudx = dudx - (5.0_dp*(vterm1*riji*xhat - vterm2*rcuti2*d(1))) + &
972 >                  pref * ( (ri4 - rcuti4)*(qxx_i*(6.0_dp*cx_i*ux_i(1)) - &
973 >                  qxx_i*2.0_dp*(xhat - rcuti*d(1))) + &
974 >                  (ri4 - rcuti4)*(qyy_i*(6.0_dp*cy_i*uy_i(1)) - &
975 >                  qyy_i*2.0_dp*(xhat - rcuti*d(1))) + &
976 >                  (ri4 - rcuti4)*(qzz_i*(6.0_dp*cz_i*uz_i(1)) - &
977 >                  qzz_i*2.0_dp*(xhat - rcuti*d(1))) )
978 >             dudy = dudy - (5.0_dp*(vterm1*riji*yhat - vterm2*rcuti2*d(2))) + &
979 >                  pref * ( (ri4 - rcuti4)*(qxx_i*(6.0_dp*cx_i*ux_i(2)) - &
980 >                  qxx_i*2.0_dp*(yhat - rcuti*d(2))) + &
981 >                  (ri4 - rcuti4)*(qyy_i*(6.0_dp*cy_i*uy_i(2)) - &
982 >                  qyy_i*2.0_dp*(yhat - rcuti*d(2))) + &
983 >                  (ri4 - rcuti4)*(qzz_i*(6.0_dp*cz_i*uz_i(2)) - &
984 >                  qzz_i*2.0_dp*(yhat - rcuti*d(2))) )
985 >             dudz = dudz - (5.0_dp*(vterm1*riji*zhat - vterm2*rcuti2*d(3))) + &
986 >                  pref * ( (ri4 - rcuti4)*(qxx_i*(6.0_dp*cx_i*ux_i(3)) - &
987 >                  qxx_i*2.0_dp*(zhat - rcuti*d(3))) + &
988 >                  (ri4 - rcuti4)*(qyy_i*(6.0_dp*cy_i*uy_i(3)) - &
989 >                  qyy_i*2.0_dp*(zhat - rcuti*d(3))) + &
990 >                  (ri4 - rcuti4)*(qzz_i*(6.0_dp*cz_i*uz_i(3)) - &
991 >                  qzz_i*2.0_dp*(zhat - rcuti*d(3))) )
992 >            
993 >             dudux_i(1) = dudux_i(1) + pref * (ri3*(qxx_i*6.0_dp*cx_i*xhat) - &
994 >                  rcuti4*(qxx_i*6.0_dp*cx_i*d(1)))
995 >             dudux_i(2) = dudux_i(2) + pref * (ri3*(qxx_i*6.0_dp*cx_i*yhat) - &
996 >                  rcuti4*(qxx_i*6.0_dp*cx_i*d(2)))
997 >             dudux_i(3) = dudux_i(3) + pref * (ri3*(qxx_i*6.0_dp*cx_i*zhat) - &
998 >                  rcuti4*(qxx_i*6.0_dp*cx_i*d(3)))
999 >            
1000 >             duduy_i(1) = duduy_i(1) + pref * (ri3*(qyy_i*6.0_dp*cy_i*xhat) - &
1001 >                  rcuti4*(qyy_i*6.0_dp*cx_i*d(1)))
1002 >             duduy_i(2) = duduy_i(2) + pref * (ri3*(qyy_i*6.0_dp*cy_i*yhat) - &
1003 >                  rcuti4*(qyy_i*6.0_dp*cx_i*d(2)))
1004 >             duduy_i(3) = duduy_i(3) + pref * (ri3*(qyy_i*6.0_dp*cy_i*zhat) - &
1005 >                  rcuti4*(qyy_i*6.0_dp*cx_i*d(3)))
1006 >            
1007 >             duduz_i(1) = duduz_i(1) + pref * (ri3*(qzz_i*6.0_dp*cz_i*xhat) - &
1008 >                  rcuti4*(qzz_i*6.0_dp*cx_i*d(1)))
1009 >             duduz_i(2) = duduz_i(2) + pref * (ri3*(qzz_i*6.0_dp*cz_i*yhat) - &
1010 >                  rcuti4*(qzz_i*6.0_dp*cx_i*d(2)))
1011 >             duduz_i(3) = duduz_i(3) + pref * (ri3*(qzz_i*6.0_dp*cz_i*zhat) - &
1012 >                  rcuti4*(qzz_i*6.0_dp*cx_i*d(3)))
1013 >
1014 >          else
1015 >             vterm = pref * ri3 * (qxx_i * (3.0_dp*cx2 - 1.0_dp) + &
1016 >                  qyy_i * (3.0_dp*cy2 - 1.0_dp) + &
1017 >                  qzz_i * (3.0_dp*cz2 - 1.0_dp))
1018 >             vpair = vpair + swi * vterm
1019 >             epot = epot + vterm
1020 >            
1021 >             dudx = dudx - 5.0_dp*vterm*riji*xhat + pref * ri4 * ( &
1022 >                  qxx_i*(6.0_dp*cx_i*ux_i(1) - 2.0_dp*xhat) + &
1023 >                  qyy_i*(6.0_dp*cy_i*uy_i(1) - 2.0_dp*xhat) + &
1024 >                  qzz_i*(6.0_dp*cz_i*uz_i(1) - 2.0_dp*xhat) )
1025 >             dudy = dudy - 5.0_dp*vterm*riji*yhat + pref * ri4 * ( &
1026 >                  qxx_i*(6.0_dp*cx_i*ux_i(2) - 2.0_dp*yhat) + &
1027 >                  qyy_i*(6.0_dp*cy_i*uy_i(2) - 2.0_dp*yhat) + &
1028 >                  qzz_i*(6.0_dp*cz_i*uz_i(2) - 2.0_dp*yhat) )
1029 >             dudz = dudz - 5.0_dp*vterm*riji*zhat + pref * ri4 * ( &
1030 >                  qxx_i*(6.0_dp*cx_i*ux_i(3) - 2.0_dp*zhat) + &
1031 >                  qyy_i*(6.0_dp*cy_i*uy_i(3) - 2.0_dp*zhat) + &
1032 >                  qzz_i*(6.0_dp*cz_i*uz_i(3) - 2.0_dp*zhat) )
1033 >            
1034 >             dudux_i(1) = dudux_i(1) + pref * ri3*(qxx_i*6.0_dp*cx_i*xhat)
1035 >             dudux_i(2) = dudux_i(2) + pref * ri3*(qxx_i*6.0_dp*cx_i*yhat)
1036 >             dudux_i(3) = dudux_i(3) + pref * ri3*(qxx_i*6.0_dp*cx_i*zhat)
1037 >            
1038 >             duduy_i(1) = duduy_i(1) + pref * ri3*(qyy_i*6.0_dp*cy_i*xhat)
1039 >             duduy_i(2) = duduy_i(2) + pref * ri3*(qyy_i*6.0_dp*cy_i*yhat)
1040 >             duduy_i(3) = duduy_i(3) + pref * ri3*(qyy_i*6.0_dp*cy_i*zhat)
1041 >            
1042 >             duduz_i(1) = duduz_i(1) + pref * ri3*(qzz_i*6.0_dp*cz_i*xhat)
1043 >             duduz_i(2) = duduz_i(2) + pref * ri3*(qzz_i*6.0_dp*cz_i*yhat)
1044 >             duduz_i(3) = duduz_i(3) + pref * ri3*(qzz_i*6.0_dp*cz_i*zhat)
1045 >          endif
1046         endif
1047      endif
1048 <      
1049 <    
1048 >
1049 >
1050      if (do_pot) then
1051   #ifdef IS_MPI
1052         pot_row(atom1) = pot_row(atom1) + 0.5d0*epot
# Line 726 | Line 1055 | contains
1055         pot = pot + epot
1056   #endif
1057      endif
1058 <        
1058 >
1059   #ifdef IS_MPI
1060      f_Row(1,atom1) = f_Row(1,atom1) + dudx
1061      f_Row(2,atom1) = f_Row(2,atom1) + dudy
1062      f_Row(3,atom1) = f_Row(3,atom1) + dudz
1063 <    
1063 >
1064      f_Col(1,atom2) = f_Col(1,atom2) - dudx
1065      f_Col(2,atom2) = f_Col(2,atom2) - dudy
1066      f_Col(3,atom2) = f_Col(3,atom2) - dudz
1067 <    
1067 >
1068      if (i_is_Dipole .or. i_is_Quadrupole) then
1069         t_Row(1,atom1)=t_Row(1,atom1) - uz_i(2)*duduz_i(3) + uz_i(3)*duduz_i(2)
1070         t_Row(2,atom1)=t_Row(2,atom1) - uz_i(3)*duduz_i(1) + uz_i(1)*duduz_i(3)
# Line 770 | Line 1099 | contains
1099      f(1,atom1) = f(1,atom1) + dudx
1100      f(2,atom1) = f(2,atom1) + dudy
1101      f(3,atom1) = f(3,atom1) + dudz
1102 <    
1102 >
1103      f(1,atom2) = f(1,atom2) - dudx
1104      f(2,atom2) = f(2,atom2) - dudy
1105      f(3,atom2) = f(3,atom2) - dudz
1106 <    
1106 >
1107      if (i_is_Dipole .or. i_is_Quadrupole) then
1108         t(1,atom1)=t(1,atom1) - uz_i(2)*duduz_i(3) + uz_i(3)*duduz_i(2)
1109         t(2,atom1)=t(2,atom1) - uz_i(3)*duduz_i(1) + uz_i(1)*duduz_i(3)
# Line 806 | Line 1135 | contains
1135      endif
1136  
1137   #endif
1138 <    
1138 >
1139   #ifdef IS_MPI
1140      id1 = AtomRowToGlobal(atom1)
1141      id2 = AtomColToGlobal(atom2)
# Line 816 | Line 1145 | contains
1145   #endif
1146  
1147      if (molMembershipList(id1) .ne. molMembershipList(id2)) then
1148 <      
1148 >
1149         fpair(1) = fpair(1) + dudx
1150         fpair(2) = fpair(2) + dudy
1151         fpair(3) = fpair(3) + dudz
# Line 825 | Line 1154 | contains
1154  
1155      return
1156    end subroutine doElectrostaticPair
1157 <  
1157 >
1158 >  !! calculates the switching functions and their derivatives for a given
1159 >  subroutine calc_switch(r, mu, scale, dscale)
1160 >
1161 >    real (kind=dp), intent(in) :: r, mu
1162 >    real (kind=dp), intent(inout) :: scale, dscale
1163 >    real (kind=dp) :: rl, ru, mulow, minRatio, temp, scaleVal
1164 >
1165 >    ! distances must be in angstroms
1166 >    rl = 2.75d0
1167 >    ru = 3.75d0
1168 >    mulow = 0.0d0 !3.3856d0 ! 1.84 * 1.84
1169 >    minRatio = mulow / (mu*mu)
1170 >    scaleVal = 1.0d0 - minRatio
1171 >    
1172 >    if (r.lt.rl) then
1173 >       scale = minRatio
1174 >       dscale = 0.0d0
1175 >    elseif (r.gt.ru) then
1176 >       scale = 1.0d0
1177 >       dscale = 0.0d0
1178 >    else
1179 >       scale = 1.0d0 - scaleVal*((ru + 2.0d0*r - 3.0d0*rl) * (ru-r)**2) &
1180 >                        / ((ru - rl)**3)
1181 >       dscale = -scaleVal * 6.0d0 * (r-ru)*(r-rl)/((ru - rl)**3)    
1182 >    endif
1183 >        
1184 >    return
1185 >  end subroutine calc_switch
1186 >
1187 >  subroutine destroyElectrostaticTypes()
1188 >
1189 >    if(allocated(ElectrostaticMap)) deallocate(ElectrostaticMap)
1190 >
1191 >  end subroutine destroyElectrostaticTypes
1192 >
1193   end module electrostatic_module

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines