ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/group/trunk/OOPSE-2.0/src/UseTheForce/DarkSide/shapes.F90
Revision: 1633
Committed: Fri Oct 22 20:22:48 2004 UTC (19 years, 8 months ago) by gezelter
File size: 49500 byte(s)
Log Message:
Added un-busticated fortran files and c/Fortran interfaces

File Contents

# User Rev Content
1 gezelter 1608 module shapes
2    
3     use force_globals
4     use definitions
5     use atype_module
6     use vector_class
7     use simulation
8     use status
9 gezelter 1633 use lj
10 gezelter 1608 #ifdef IS_MPI
11     use mpiSimulation
12     #endif
13     implicit none
14    
15     PRIVATE
16    
17     INTEGER, PARAMETER:: CHEBYSHEV_TN = 1
18     INTEGER, PARAMETER:: CHEBYSHEV_UN = 2
19     INTEGER, PARAMETER:: LAGUERRE = 3
20     INTEGER, PARAMETER:: HERMITE = 4
21     INTEGER, PARAMETER:: SH_COS = 0
22     INTEGER, PARAMETER:: SH_SIN = 1
23    
24     logical, save :: haveShapeMap = .false.
25    
26     public :: do_shape_pair
27     public :: newShapeType
28    
29    
30     type, private :: Shape
31     integer :: atid
32     integer :: nContactFuncs
33     integer :: nRangeFuncs
34     integer :: nStrengthFuncs
35     integer :: bigL
36     integer :: bigM
37     integer, pointer, dimension(:) :: ContactFuncLValue => null()
38     integer, pointer, dimension(:) :: ContactFuncMValue => null()
39     integer, pointer, dimension(:) :: ContactFunctionType => null()
40     real(kind=dp), pointer, dimension(:) :: ContactFuncCoefficient => null()
41     integer, pointer, dimension(:) :: RangeFuncLValue => null()
42     integer, pointer, dimension(:) :: RangeFuncMValue => null()
43     integer, pointer, dimension(:) :: RangeFunctionType => null()
44     real(kind=dp), pointer, dimension(:) :: RangeFuncCoefficient => null()
45     integer, pointer, dimension(:) :: StrengthFuncLValue => null()
46     integer, pointer, dimension(:) :: StrengthFuncMValue => null()
47     integer, pointer, dimension(:) :: StrengthFunctionType => null()
48     real(kind=dp), pointer, dimension(:) :: StrengthFuncCoefficient => null()
49     logical :: isLJ
50     real ( kind = dp ) :: epsilon
51     real ( kind = dp ) :: sigma
52     end type Shape
53    
54     type, private :: ShapeList
55     integer :: n_shapes = 0
56     integer :: currentShape = 0
57     type (Shape), pointer :: Shapes(:) => null()
58     integer, pointer :: atidToShape(:) => null()
59     end type ShapeList
60    
61     type(ShapeList), save :: ShapeMap
62    
63     integer :: lmax
64     real (kind=dp), allocatable, dimension(:,:) :: plm_i, dlm_i, plm_j, dlm_j
65     real (kind=dp), allocatable, dimension(:) :: tm_i, dtm_i, um_i, dum_i
66     real (kind=dp), allocatable, dimension(:) :: tm_j, dtm_j, um_j, dum_j
67    
68     contains
69    
70     subroutine newShapeType(nContactFuncs, ContactFuncLValue, &
71     ContactFuncMValue, ContactFunctionType, ContactFuncCoefficient, &
72     nRangeFuncs, RangeFuncLValue, RangeFuncMValue, RangeFunctionType, &
73     RangeFuncCoefficient, nStrengthFuncs, StrengthFuncLValue, &
74     StrengthFuncMValue, StrengthFunctionType, StrengthFuncCoefficient, &
75     myAtid, status)
76    
77     integer :: nContactFuncs
78     integer :: nRangeFuncs
79     integer :: nStrengthFuncs
80     integer :: shape_ident
81     integer :: status
82     integer :: myAtid
83     integer :: bigL
84     integer :: bigM
85     integer :: j, me, nShapeTypes, nLJTypes, ntypes, current, alloc_stat
86     integer, pointer :: MatchList(:) => null()
87    
88     integer, dimension(nContactFuncs) :: ContactFuncLValue
89     integer, dimension(nContactFuncs) :: ContactFuncMValue
90     integer, dimension(nContactFuncs) :: ContactFunctionType
91     real(kind=dp), dimension(nContactFuncs) :: ContactFuncCoefficient
92     integer, dimension(nRangeFuncs) :: RangeFuncLValue
93     integer, dimension(nRangeFuncs) :: RangeFuncMValue
94     integer, dimension(nRangeFuncs) :: RangeFunctionType
95     real(kind=dp), dimension(nRangeFuncs) :: RangeFuncCoefficient
96     integer, dimension(nStrengthFuncs) :: StrengthFuncLValue
97     integer, dimension(nStrengthFuncs) :: StrengthFuncMValue
98     integer, dimension(nStrengthFuncs) :: StrengthFunctionType
99     real(kind=dp), dimension(nStrengthFuncs) :: StrengthFuncCoefficient
100    
101     status = 0
102     ! check to see if this is the first time into this routine...
103     if (.not.associated(ShapeMap%Shapes)) then
104    
105     call getMatchingElementList(atypes, "is_Shape", .true., &
106     nShapeTypes, MatchList)
107    
108 gezelter 1633 call getMatchingElementList(atypes, "is_LennardJones", .true., &
109 gezelter 1608 nLJTypes, MatchList)
110    
111     ShapeMap%n_shapes = nShapeTypes + nLJTypes
112    
113     allocate(ShapeMap%Shapes(nShapeTypes + nLJTypes))
114    
115     ntypes = getSize(atypes)
116    
117     allocate(ShapeMap%atidToShape(ntypes))
118     end if
119    
120     ShapeMap%currentShape = ShapeMap%currentShape + 1
121     current = ShapeMap%currentShape
122    
123     call allocateShape(nContactFuncs, nRangeFuncs, nStrengthFuncs, &
124     ShapeMap%Shapes(current), stat=alloc_stat)
125     if (alloc_stat .ne. 0) then
126     status = -1
127     return
128     endif
129    
130     call getElementProperty(atypes, myAtid, "c_ident", me)
131     ShapeMap%atidToShape(me) = current
132     ShapeMap%Shapes(current)%atid = me
133     ShapeMap%Shapes(current)%nContactFuncs = nContactFuncs
134     ShapeMap%Shapes(current)%nRangeFuncs = nRangeFuncs
135     ShapeMap%Shapes(current)%nStrengthFuncs = nStrengthFuncs
136     ShapeMap%Shapes(current)%ContactFuncLValue = ContactFuncLValue
137     ShapeMap%Shapes(current)%ContactFuncMValue = ContactFuncMValue
138     ShapeMap%Shapes(current)%ContactFunctionType = ContactFunctionType
139     ShapeMap%Shapes(current)%ContactFuncCoefficient = ContactFuncCoefficient
140     ShapeMap%Shapes(current)%RangeFuncLValue = RangeFuncLValue
141     ShapeMap%Shapes(current)%RangeFuncMValue = RangeFuncMValue
142     ShapeMap%Shapes(current)%RangeFunctionType = RangeFunctionType
143     ShapeMap%Shapes(current)%RangeFuncCoefficient = RangeFuncCoefficient
144     ShapeMap%Shapes(current)%StrengthFuncLValue = StrengthFuncLValue
145     ShapeMap%Shapes(current)%StrengthFuncMValue = StrengthFuncMValue
146     ShapeMap%Shapes(current)%StrengthFunctionType = StrengthFunctionType
147     ShapeMap%Shapes(current)%StrengthFuncCoefficient = StrengthFuncCoefficient
148    
149     bigL = -1
150     bigM = -1
151    
152     do j = 1, ShapeMap%Shapes(current)%nContactFuncs
153     if (ShapeMap%Shapes(current)%ContactFuncLValue(j) .gt. bigL) then
154     bigL = ShapeMap%Shapes(current)%ContactFuncLValue(j)
155     endif
156     if (ShapeMap%Shapes(current)%ContactFuncMValue(j) .gt. bigM) then
157     bigM = ShapeMap%Shapes(current)%ContactFuncMValue(j)
158     endif
159     enddo
160     do j = 1, ShapeMap%Shapes(current)%nRangeFuncs
161     if (ShapeMap%Shapes(current)%RangeFuncLValue(j) .gt. bigL) then
162     bigL = ShapeMap%Shapes(current)%RangeFuncLValue(j)
163     endif
164     if (ShapeMap%Shapes(current)%RangeFuncMValue(j) .gt. bigM) then
165     bigM = ShapeMap%Shapes(current)%RangeFuncMValue(j)
166     endif
167     enddo
168     do j = 1, ShapeMap%Shapes(current)%nStrengthFuncs
169     if (ShapeMap%Shapes(current)%StrengthFuncLValue(j) .gt. bigL) then
170     bigL = ShapeMap%Shapes(current)%StrengthFuncLValue(j)
171     endif
172     if (ShapeMap%Shapes(current)%StrengthFuncMValue(j) .gt. bigM) then
173     bigM = ShapeMap%Shapes(current)%StrengthFuncMValue(j)
174     endif
175     enddo
176    
177     ShapeMap%Shapes(current)%bigL = bigL
178     ShapeMap%Shapes(current)%bigM = bigM
179    
180     end subroutine newShapeType
181    
182     subroutine allocateShape(nContactFuncs, nRangeFuncs, nStrengthFuncs, &
183     myShape, stat)
184    
185     integer, intent(in) :: nContactFuncs, nRangeFuncs, nStrengthFuncs
186     type(Shape), intent(inout) :: myShape
187     integer, intent(out) :: stat
188     integer :: alloc_stat
189    
190     if (associated(myShape%contactFuncLValue)) then
191     deallocate(myShape%contactFuncLValue)
192     endif
193     allocate(myShape%contactFuncLValue(nContactFuncs), stat = alloc_stat)
194     if (alloc_stat .ne. 0) then
195     stat = -1
196     return
197     endif
198     if (associated(myShape%contactFuncMValue)) then
199     deallocate(myShape%contactFuncMValue)
200     endif
201     allocate(myShape%contactFuncMValue(nContactFuncs), stat = alloc_stat)
202     if (alloc_stat .ne. 0) then
203     stat = -1
204     return
205     endif
206     if (associated(myShape%contactFunctionType)) then
207     deallocate(myShape%contactFunctionType)
208     endif
209     allocate(myShape%contactFunctionType(nContactFuncs), stat = alloc_stat)
210     if (alloc_stat .ne. 0) then
211     stat = -1
212     return
213     endif
214     if (associated(myShape%contactFuncCoefficient)) then
215     deallocate(myShape%contactFuncCoefficient)
216     endif
217     allocate(myShape%contactFuncCoefficient(nContactFuncs), stat = alloc_stat)
218     if (alloc_stat .ne. 0) then
219     stat = -1
220     return
221     endif
222    
223     if (associated(myShape%rangeFuncLValue)) then
224     deallocate(myShape%rangeFuncLValue)
225     endif
226     allocate(myShape%rangeFuncLValue(nRangeFuncs), stat = alloc_stat)
227     if (alloc_stat .ne. 0) then
228     stat = -1
229     return
230     endif
231     if (associated(myShape%rangeFuncMValue)) then
232     deallocate(myShape%rangeFuncMValue)
233     endif
234     allocate(myShape%rangeFuncMValue(nRangeFuncs), stat = alloc_stat)
235     if (alloc_stat .ne. 0) then
236     stat = -1
237     return
238     endif
239     if (associated(myShape%rangeFunctionType)) then
240     deallocate(myShape%rangeFunctionType)
241     endif
242     allocate(myShape%rangeFunctionType(nRangeFuncs), stat = alloc_stat)
243     if (alloc_stat .ne. 0) then
244     stat = -1
245     return
246     endif
247     if (associated(myShape%rangeFuncCoefficient)) then
248     deallocate(myShape%rangeFuncCoefficient)
249     endif
250     allocate(myShape%rangeFuncCoefficient(nRangeFuncs), stat = alloc_stat)
251     if (alloc_stat .ne. 0) then
252     stat = -1
253     return
254     endif
255    
256     if (associated(myShape%strengthFuncLValue)) then
257     deallocate(myShape%strengthFuncLValue)
258     endif
259     allocate(myShape%strengthFuncLValue(nStrengthFuncs), stat = alloc_stat)
260     if (alloc_stat .ne. 0) then
261     stat = -1
262     return
263     endif
264     if (associated(myShape%strengthFuncMValue)) then
265     deallocate(myShape%strengthFuncMValue)
266     endif
267     allocate(myShape%strengthFuncMValue(nStrengthFuncs), stat = alloc_stat)
268     if (alloc_stat .ne. 0) then
269     stat = -1
270     return
271     endif
272     if (associated(myShape%strengthFunctionType)) then
273     deallocate(myShape%strengthFunctionType)
274     endif
275     allocate(myShape%strengthFunctionType(nStrengthFuncs), stat = alloc_stat)
276     if (alloc_stat .ne. 0) then
277     stat = -1
278     return
279     endif
280     if (associated(myShape%strengthFuncCoefficient)) then
281     deallocate(myShape%strengthFuncCoefficient)
282     endif
283     allocate(myShape%strengthFuncCoefficient(nStrengthFuncs), stat=alloc_stat)
284     if (alloc_stat .ne. 0) then
285     stat = -1
286     return
287     endif
288    
289     end subroutine allocateShape
290    
291     subroutine init_Shape_FF(status)
292     integer :: status
293     integer :: i, j, l, m, lm, function_type
294     real(kind=dp) :: bigSigma, myBigSigma, thisSigma, coeff, Phunc, spi
295 gezelter 1633 real(kind=dp) :: costheta, cpi, theta, Pi, phi, thisDP, sigma
296 gezelter 1608 integer :: alloc_stat, iTheta, iPhi, nSteps, nAtypes, thisIP, current
297     logical :: thisProperty
298    
299     Pi = 4.0d0 * datan(1.0d0)
300    
301     status = 0
302     if (ShapeMap%currentShape == 0) then
303     call handleError("init_Shape_FF", "No members in ShapeMap")
304     status = -1
305     return
306     end if
307    
308     bigSigma = 0.0d0
309     do i = 1, ShapeMap%currentShape
310    
311     ! Scan over theta and phi to
312     ! find the largest contact in any direction....
313    
314     myBigSigma = 0.0d0
315    
316     do iTheta = 0, nSteps
317     theta = (Pi/2.0d0)*(dble(iTheta)/dble(nSteps))
318     costheta = cos(theta)
319    
320     call Associated_Legendre(costheta, ShapeMap%Shapes(i)%bigL, &
321     ShapeMap%Shapes(i)%bigM, lmax, plm_i, dlm_i)
322    
323     do iPhi = 0, nSteps
324     phi = -Pi + 2.0d0 * Pi * (dble(iPhi)/dble(nSteps))
325     cpi = cos(phi)
326     spi = sin(phi)
327    
328     call Orthogonal_Polynomial(cpi, ShapeMap%Shapes(i)%bigM, &
329     CHEBYSHEV_TN, tm_i, dtm_i)
330     call Orthogonal_Polynomial(cpi, ShapeMap%Shapes(i)%bigM, &
331     CHEBYSHEV_UN, um_i, dum_i)
332    
333     thisSigma = 0.0d0
334    
335     do lm = 1, ShapeMap%Shapes(i)%nContactFuncs
336    
337     l = ShapeMap%Shapes(i)%ContactFuncLValue(lm)
338     m = ShapeMap%Shapes(i)%ContactFuncMValue(lm)
339     coeff = ShapeMap%Shapes(i)%ContactFuncCoefficient(lm)
340     function_type = ShapeMap%Shapes(i)%ContactFunctionType(lm)
341    
342     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
343     Phunc = coeff * tm_i(m)
344     else
345     Phunc = coeff * spi * um_i(m-1)
346     endif
347    
348     thisSigma = thisSigma + plm_i(l,m)*Phunc
349     enddo
350    
351     if (thisSigma.gt.myBigSigma) myBigSigma = thisSigma
352     enddo
353     enddo
354    
355     if (myBigSigma.gt.bigSigma) bigSigma = myBigSigma
356     enddo
357    
358     nAtypes = getSize(atypes)
359    
360     if (nAtypes == 0) then
361     status = -1
362     return
363     end if
364    
365     do i = 1, nAtypes
366    
367 gezelter 1633 call getElementProperty(atypes, i, "is_LennardJones", thisProperty)
368 gezelter 1608
369     if (thisProperty) then
370    
371     ShapeMap%currentShape = ShapeMap%currentShape + 1
372     current = ShapeMap%currentShape
373    
374     call getElementProperty(atypes, i, "c_ident", thisIP)
375     ShapeMap%atidToShape(thisIP) = current
376     ShapeMap%Shapes(current)%atid = thisIP
377    
378     ShapeMap%Shapes(current)%isLJ = .true.
379    
380 gezelter 1633 ShapeMap%Shapes(current)%epsilon = getEpsilon(thisIP)
381     sigma = getSigma(thisIP)
382     ShapeMap%Shapes(current)%sigma = sigma
383     if (sigma .gt. bigSigma) bigSigma = thisDP
384 gezelter 1608
385     endif
386    
387     end do
388    
389     haveShapeMap = .true.
390    
391     end subroutine init_Shape_FF
392    
393     subroutine do_shape_pair(atom1, atom2, d, rij, r2, sw, vpair, fpair, &
394     pot, A, f, t, do_pot)
395    
396     integer, intent(in) :: atom1, atom2
397     real (kind=dp), intent(inout) :: rij, r2
398     real (kind=dp), dimension(3), intent(in) :: d
399     real (kind=dp), dimension(3), intent(inout) :: fpair
400     real (kind=dp) :: pot, vpair, sw
401     real (kind=dp), dimension(9,nLocal) :: A
402     real (kind=dp), dimension(3,nLocal) :: f
403     real (kind=dp), dimension(3,nLocal) :: t
404     logical, intent(in) :: do_pot
405    
406     real (kind=dp) :: r3, r5, rt2, rt3, rt5, rt6, rt11, rt12, rt126
407     integer :: atid1, atid2, st1, st2
408     integer :: l, m, lm, id1, id2, localError, function_type
409     real (kind=dp) :: sigma_i, s_i, eps_i, sigma_j, s_j, eps_j
410     real (kind=dp) :: coeff
411    
412     real (kind=dp) :: dsigmaidx, dsigmaidy, dsigmaidz
413     real (kind=dp) :: dsigmaidux, dsigmaiduy, dsigmaiduz
414     real (kind=dp) :: dsigmajdx, dsigmajdy, dsigmajdz
415     real (kind=dp) :: dsigmajdux, dsigmajduy, dsigmajduz
416    
417     real (kind=dp) :: dsidx, dsidy, dsidz
418     real (kind=dp) :: dsidux, dsiduy, dsiduz
419     real (kind=dp) :: dsjdx, dsjdy, dsjdz
420     real (kind=dp) :: dsjdux, dsjduy, dsjduz
421    
422     real (kind=dp) :: depsidx, depsidy, depsidz
423     real (kind=dp) :: depsidux, depsiduy, depsiduz
424     real (kind=dp) :: depsjdx, depsjdy, depsjdz
425     real (kind=dp) :: depsjdux, depsjduy, depsjduz
426    
427     real (kind=dp) :: xi, yi, zi, xj, yj, zj, xi2, yi2, zi2, xj2, yj2, zj2
428    
429     real (kind=dp) :: proji, proji3, projj, projj3
430     real (kind=dp) :: cti, ctj, cpi, cpj, spi, spj
431     real (kind=dp) :: Phunc, sigma, s, eps, rtdenom, rt
432    
433     real (kind=dp) :: dctidx, dctidy, dctidz
434     real (kind=dp) :: dctidux, dctiduy, dctiduz
435     real (kind=dp) :: dctjdx, dctjdy, dctjdz
436     real (kind=dp) :: dctjdux, dctjduy, dctjduz
437    
438     real (kind=dp) :: dcpidx, dcpidy, dcpidz
439     real (kind=dp) :: dcpidux, dcpiduy, dcpiduz
440     real (kind=dp) :: dcpjdx, dcpjdy, dcpjdz
441     real (kind=dp) :: dcpjdux, dcpjduy, dcpjduz
442    
443     real (kind=dp) :: dspidx, dspidy, dspidz
444     real (kind=dp) :: dspidux, dspiduy, dspiduz
445     real (kind=dp) :: dspjdx, dspjdy, dspjdz
446     real (kind=dp) :: dspjdux, dspjduy, dspjduz
447    
448     real (kind=dp) :: dPhuncdX, dPhuncdY, dPhuncdZ
449     real (kind=dp) :: dPhuncdUx, dPhuncdUy, dPhuncdUz
450    
451     real (kind=dp) :: dsigmadxi, dsigmadyi, dsigmadzi
452     real (kind=dp) :: dsigmaduxi, dsigmaduyi, dsigmaduzi
453     real (kind=dp) :: dsigmadxj, dsigmadyj, dsigmadzj
454     real (kind=dp) :: dsigmaduxj, dsigmaduyj, dsigmaduzj
455    
456     real (kind=dp) :: dsdxi, dsdyi, dsdzi
457     real (kind=dp) :: dsduxi, dsduyi, dsduzi
458     real (kind=dp) :: dsdxj, dsdyj, dsdzj
459     real (kind=dp) :: dsduxj, dsduyj, dsduzj
460    
461     real (kind=dp) :: depsdxi, depsdyi, depsdzi
462     real (kind=dp) :: depsduxi, depsduyi, depsduzi
463     real (kind=dp) :: depsdxj, depsdyj, depsdzj
464     real (kind=dp) :: depsduxj, depsduyj, depsduzj
465    
466     real (kind=dp) :: drtdxi, drtdyi, drtdzi
467     real (kind=dp) :: drtduxi, drtduyi, drtduzi
468     real (kind=dp) :: drtdxj, drtdyj, drtdzj
469     real (kind=dp) :: drtduxj, drtduyj, drtduzj
470    
471     real (kind=dp) :: drdxi, drdyi, drdzi
472     real (kind=dp) :: drduxi, drduyi, drduzi
473     real (kind=dp) :: drdxj, drdyj, drdzj
474     real (kind=dp) :: drduxj, drduyj, drduzj
475    
476     real (kind=dp) :: dvdxi, dvdyi, dvdzi
477     real (kind=dp) :: dvduxi, dvduyi, dvduzi
478     real (kind=dp) :: dvdxj, dvdyj, dvdzj
479     real (kind=dp) :: dvduxj, dvduyj, dvduzj
480    
481     real (kind=dp) :: fxi, fyi, fzi, fxj, fyj, fzj
482     real (kind=dp) :: txi, tyi, tzi, txj, tyj, tzj
483     real (kind=dp) :: fxii, fyii, fzii, fxij, fyij, fzij
484     real (kind=dp) :: fxji, fyji, fzji, fxjj, fyjj, fzjj
485     real (kind=dp) :: fxradial, fyradial, fzradial
486    
487     if (.not.haveShapeMap) then
488     call handleError("calc_shape", "NO SHAPEMAP!!!!")
489     return
490     endif
491    
492     !! We assume that the rotation matrices have already been calculated
493     !! and placed in the A array.
494    
495     r3 = r2*rij
496     r5 = r3*r2
497    
498     drdxi = -d(1) / rij
499     drdyi = -d(2) / rij
500     drdzi = -d(3) / rij
501    
502     drdxj = d(1) / rij
503     drdyj = d(2) / rij
504     drdzj = d(3) / rij
505    
506     ! find the atom type id (atid) for each atom:
507     #ifdef IS_MPI
508     atid1 = atid_Row(atom1)
509     atid2 = atid_Col(atom2)
510     #else
511     atid1 = atid(atom1)
512     atid2 = atid(atom2)
513     #endif
514    
515     ! use the atid to find the shape type (st) for each atom:
516    
517     st1 = ShapeMap%atidToShape(atid1)
518     st2 = ShapeMap%atidToShape(atid2)
519    
520     if (ShapeMap%Shapes(st1)%isLJ) then
521     sigma_i = ShapeMap%Shapes(st1)%sigma
522     s_i = ShapeMap%Shapes(st1)%sigma
523     eps_i = ShapeMap%Shapes(st1)%epsilon
524     dsigmaidx = 0.0d0
525     dsigmaidy = 0.0d0
526     dsigmaidz = 0.0d0
527     dsigmaidux = 0.0d0
528     dsigmaiduy = 0.0d0
529     dsigmaiduz = 0.0d0
530     dsidx = 0.0d0
531     dsidy = 0.0d0
532     dsidz = 0.0d0
533     dsidux = 0.0d0
534     dsiduy = 0.0d0
535     dsiduz = 0.0d0
536     depsidx = 0.0d0
537     depsidy = 0.0d0
538     depsidz = 0.0d0
539     depsidux = 0.0d0
540     depsiduy = 0.0d0
541     depsiduz = 0.0d0
542     else
543    
544     #ifdef IS_MPI
545     ! rotate the inter-particle separation into the two different
546     ! body-fixed coordinate systems:
547    
548     xi = A_row(1,atom1)*d(1) + A_row(2,atom1)*d(2) + A_row(3,atom1)*d(3)
549     yi = A_row(4,atom1)*d(1) + A_row(5,atom1)*d(2) + A_row(6,atom1)*d(3)
550     zi = A_row(7,atom1)*d(1) + A_row(8,atom1)*d(2) + A_row(9,atom1)*d(3)
551    
552     #else
553     ! rotate the inter-particle separation into the two different
554     ! body-fixed coordinate systems:
555    
556     xi = a(1,atom1)*d(1) + a(2,atom1)*d(2) + a(3,atom1)*d(3)
557     yi = a(4,atom1)*d(1) + a(5,atom1)*d(2) + a(6,atom1)*d(3)
558     zi = a(7,atom1)*d(1) + a(8,atom1)*d(2) + a(9,atom1)*d(3)
559    
560     #endif
561    
562     xi2 = xi*xi
563     yi2 = yi*yi
564     zi2 = zi*zi
565    
566     proji = sqrt(xi2 + yi2)
567     proji3 = proji*proji*proji
568    
569     cti = zi / rij
570     dctidx = - zi * xi / r3
571     dctidy = - zi * yi / r3
572     dctidz = 1.0d0 / rij - zi2 / r3
573     dctidux = yi / rij
574     dctiduy = -xi / rij
575     dctiduz = 0.0d0
576    
577     cpi = xi / proji
578     dcpidx = 1.0d0 / proji - xi2 / proji3
579     dcpidy = - xi * yi / proji3
580     dcpidz = 0.0d0
581     dcpidux = xi * yi * zi / proji3
582     dcpiduy = -zi * (1.0d0 / proji - xi2 / proji3)
583     dcpiduz = -yi * (1.0d0 / proji - xi2 / proji3) - (xi2 * yi / proji3)
584    
585     spi = yi / proji
586     dspidx = - xi * yi / proji3
587     dspidy = 1.0d0 / proji - yi2 / proji3
588     dspidz = 0.0d0
589     dspidux = -zi * (1.0d0 / proji - yi2 / proji3)
590     dspiduy = xi * yi * zi / proji3
591     dspiduz = xi * (1.0d0 / proji - yi2 / proji3) + (xi * yi2 / proji3)
592    
593     call Associated_Legendre(cti, ShapeMap%Shapes(st1)%bigL, &
594     ShapeMap%Shapes(st1)%bigM, lmax, plm_i, dlm_i)
595    
596     call Orthogonal_Polynomial(cpi, ShapeMap%Shapes(st1)%bigM, &
597     CHEBYSHEV_TN, tm_i, dtm_i)
598     call Orthogonal_Polynomial(cpi, ShapeMap%Shapes(st1)%bigM, &
599     CHEBYSHEV_UN, um_i, dum_i)
600    
601     sigma_i = 0.0d0
602     s_i = 0.0d0
603     eps_i = 0.0d0
604     dsigmaidx = 0.0d0
605     dsigmaidy = 0.0d0
606     dsigmaidz = 0.0d0
607     dsigmaidux = 0.0d0
608     dsigmaiduy = 0.0d0
609     dsigmaiduz = 0.0d0
610     dsidx = 0.0d0
611     dsidy = 0.0d0
612     dsidz = 0.0d0
613     dsidux = 0.0d0
614     dsiduy = 0.0d0
615     dsiduz = 0.0d0
616     depsidx = 0.0d0
617     depsidy = 0.0d0
618     depsidz = 0.0d0
619     depsidux = 0.0d0
620     depsiduy = 0.0d0
621     depsiduz = 0.0d0
622    
623     do lm = 1, ShapeMap%Shapes(st1)%nContactFuncs
624     l = ShapeMap%Shapes(st1)%ContactFuncLValue(lm)
625     m = ShapeMap%Shapes(st1)%ContactFuncMValue(lm)
626     coeff = ShapeMap%Shapes(st1)%ContactFuncCoefficient(lm)
627     function_type = ShapeMap%Shapes(st1)%ContactFunctionType(lm)
628    
629     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
630     Phunc = coeff * tm_i(m)
631     dPhuncdX = coeff * dtm_i(m) * dcpidx
632     dPhuncdY = coeff * dtm_i(m) * dcpidy
633     dPhuncdZ = coeff * dtm_i(m) * dcpidz
634     dPhuncdUz = coeff * dtm_i(m) * dcpidux
635     dPhuncdUy = coeff * dtm_i(m) * dcpiduy
636     dPhuncdUz = coeff * dtm_i(m) * dcpiduz
637     else
638     Phunc = coeff * spi * um_i(m-1)
639     dPhuncdX = coeff * (spi * dum_i(m-1) * dcpidx + dspidx *um_i(m-1))
640     dPhuncdY = coeff * (spi * dum_i(m-1) * dcpidy + dspidy *um_i(m-1))
641     dPhuncdZ = coeff * (spi * dum_i(m-1) * dcpidz + dspidz *um_i(m-1))
642     dPhuncdUx = coeff*(spi * dum_i(m-1)*dcpidux + dspidux *um_i(m-1))
643     dPhuncdUy = coeff*(spi * dum_i(m-1)*dcpiduy + dspiduy *um_i(m-1))
644     dPhuncdUz = coeff*(spi * dum_i(m-1)*dcpiduz + dspiduz *um_i(m-1))
645     endif
646    
647     sigma_i = sigma_i + plm_i(l,m)*Phunc
648    
649     dsigmaidx = dsigmaidx + plm_i(l,m)*dPhuncdX + &
650     Phunc * dlm_i(l,m) * dctidx
651     dsigmaidy = dsigmaidy + plm_i(l,m)*dPhuncdY + &
652     Phunc * dlm_i(l,m) * dctidy
653     dsigmaidz = dsigmaidz + plm_i(l,m)*dPhuncdZ + &
654     Phunc * dlm_i(l,m) * dctidz
655    
656     dsigmaidux = dsigmaidux + plm_i(l,m)* dPhuncdUx + &
657     Phunc * dlm_i(l,m) * dctidux
658     dsigmaiduy = dsigmaiduy + plm_i(l,m)* dPhuncdUy + &
659     Phunc * dlm_i(l,m) * dctiduy
660     dsigmaiduz = dsigmaiduz + plm_i(l,m)* dPhuncdUz + &
661     Phunc * dlm_i(l,m) * dctiduz
662    
663     end do
664    
665     do lm = 1, ShapeMap%Shapes(st1)%nRangeFuncs
666     l = ShapeMap%Shapes(st1)%RangeFuncLValue(lm)
667     m = ShapeMap%Shapes(st1)%RangeFuncMValue(lm)
668     coeff = ShapeMap%Shapes(st1)%RangeFuncCoefficient(lm)
669     function_type = ShapeMap%Shapes(st1)%RangeFunctionType(lm)
670    
671     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
672     Phunc = coeff * tm_i(m)
673     dPhuncdX = coeff * dtm_i(m) * dcpidx
674     dPhuncdY = coeff * dtm_i(m) * dcpidy
675     dPhuncdZ = coeff * dtm_i(m) * dcpidz
676     dPhuncdUz = coeff * dtm_i(m) * dcpidux
677     dPhuncdUy = coeff * dtm_i(m) * dcpiduy
678     dPhuncdUz = coeff * dtm_i(m) * dcpiduz
679     else
680     Phunc = coeff * spi * um_i(m-1)
681     dPhuncdX = coeff * (spi * dum_i(m-1) * dcpidx + dspidx *um_i(m-1))
682     dPhuncdY = coeff * (spi * dum_i(m-1) * dcpidy + dspidy *um_i(m-1))
683     dPhuncdZ = coeff * (spi * dum_i(m-1) * dcpidz + dspidz *um_i(m-1))
684     dPhuncdUx = coeff*(spi * dum_i(m-1)*dcpidux + dspidux *um_i(m-1))
685     dPhuncdUy = coeff*(spi * dum_i(m-1)*dcpiduy + dspiduy *um_i(m-1))
686     dPhuncdUz = coeff*(spi * dum_i(m-1)*dcpiduz + dspiduz *um_i(m-1))
687     endif
688    
689     s_i = s_i + plm_i(l,m)*Phunc
690    
691     dsidx = dsidx + plm_i(l,m)*dPhuncdX + &
692     Phunc * dlm_i(l,m) * dctidx
693     dsidy = dsidy + plm_i(l,m)*dPhuncdY + &
694     Phunc * dlm_i(l,m) * dctidy
695     dsidz = dsidz + plm_i(l,m)*dPhuncdZ + &
696     Phunc * dlm_i(l,m) * dctidz
697    
698     dsidux = dsidux + plm_i(l,m)* dPhuncdUx + &
699     Phunc * dlm_i(l,m) * dctidux
700     dsiduy = dsiduy + plm_i(l,m)* dPhuncdUy + &
701     Phunc * dlm_i(l,m) * dctiduy
702     dsiduz = dsiduz + plm_i(l,m)* dPhuncdUz + &
703     Phunc * dlm_i(l,m) * dctiduz
704    
705     end do
706    
707     do lm = 1, ShapeMap%Shapes(st1)%nStrengthFuncs
708     l = ShapeMap%Shapes(st1)%StrengthFuncLValue(lm)
709     m = ShapeMap%Shapes(st1)%StrengthFuncMValue(lm)
710     coeff = ShapeMap%Shapes(st1)%StrengthFuncCoefficient(lm)
711     function_type = ShapeMap%Shapes(st1)%StrengthFunctionType(lm)
712    
713     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
714     Phunc = coeff * tm_i(m)
715     dPhuncdX = coeff * dtm_i(m) * dcpidx
716     dPhuncdY = coeff * dtm_i(m) * dcpidy
717     dPhuncdZ = coeff * dtm_i(m) * dcpidz
718     dPhuncdUz = coeff * dtm_i(m) * dcpidux
719     dPhuncdUy = coeff * dtm_i(m) * dcpiduy
720     dPhuncdUz = coeff * dtm_i(m) * dcpiduz
721     else
722     Phunc = coeff * spi * um_i(m-1)
723     dPhuncdX = coeff * (spi * dum_i(m-1) * dcpidx + dspidx *um_i(m-1))
724     dPhuncdY = coeff * (spi * dum_i(m-1) * dcpidy + dspidy *um_i(m-1))
725     dPhuncdZ = coeff * (spi * dum_i(m-1) * dcpidz + dspidz *um_i(m-1))
726     dPhuncdUx = coeff*(spi * dum_i(m-1)*dcpidux + dspidux *um_i(m-1))
727     dPhuncdUy = coeff*(spi * dum_i(m-1)*dcpiduy + dspiduy *um_i(m-1))
728     dPhuncdUz = coeff*(spi * dum_i(m-1)*dcpiduz + dspiduz *um_i(m-1))
729     endif
730    
731     eps_i = eps_i + plm_i(l,m)*Phunc
732    
733     depsidx = depsidx + plm_i(l,m)*dPhuncdX + &
734     Phunc * dlm_i(l,m) * dctidx
735     depsidy = depsidy + plm_i(l,m)*dPhuncdY + &
736     Phunc * dlm_i(l,m) * dctidy
737     depsidz = depsidz + plm_i(l,m)*dPhuncdZ + &
738     Phunc * dlm_i(l,m) * dctidz
739    
740     depsidux = depsidux + plm_i(l,m)* dPhuncdUx + &
741     Phunc * dlm_i(l,m) * dctidux
742     depsiduy = depsiduy + plm_i(l,m)* dPhuncdUy + &
743     Phunc * dlm_i(l,m) * dctiduy
744     depsiduz = depsiduz + plm_i(l,m)* dPhuncdUz + &
745     Phunc * dlm_i(l,m) * dctiduz
746    
747     end do
748    
749     endif
750    
751     ! now do j:
752    
753     if (ShapeMap%Shapes(st2)%isLJ) then
754     sigma_j = ShapeMap%Shapes(st2)%sigma
755     s_j = ShapeMap%Shapes(st2)%sigma
756     eps_j = ShapeMap%Shapes(st2)%epsilon
757     dsigmajdx = 0.0d0
758     dsigmajdy = 0.0d0
759     dsigmajdz = 0.0d0
760     dsigmajdux = 0.0d0
761     dsigmajduy = 0.0d0
762     dsigmajduz = 0.0d0
763     dsjdx = 0.0d0
764     dsjdy = 0.0d0
765     dsjdz = 0.0d0
766     dsjdux = 0.0d0
767     dsjduy = 0.0d0
768     dsjduz = 0.0d0
769     depsjdx = 0.0d0
770     depsjdy = 0.0d0
771     depsjdz = 0.0d0
772     depsjdux = 0.0d0
773     depsjduy = 0.0d0
774     depsjduz = 0.0d0
775     else
776    
777     #ifdef IS_MPI
778     ! rotate the inter-particle separation into the two different
779     ! body-fixed coordinate systems:
780     ! negative sign because this is the vector from j to i:
781    
782     xj = -(A_Col(1,atom2)*d(1) + A_Col(2,atom2)*d(2) + A_Col(3,atom2)*d(3))
783     yj = -(A_Col(4,atom2)*d(1) + A_Col(5,atom2)*d(2) + A_Col(6,atom2)*d(3))
784     zj = -(A_Col(7,atom2)*d(1) + A_Col(8,atom2)*d(2) + A_Col(9,atom2)*d(3))
785     #else
786     ! rotate the inter-particle separation into the two different
787     ! body-fixed coordinate systems:
788     ! negative sign because this is the vector from j to i:
789    
790     xj = -(a(1,atom2)*d(1) + a(2,atom2)*d(2) + a(3,atom2)*d(3))
791     yj = -(a(4,atom2)*d(1) + a(5,atom2)*d(2) + a(6,atom2)*d(3))
792     zj = -(a(7,atom2)*d(1) + a(8,atom2)*d(2) + a(9,atom2)*d(3))
793     #endif
794    
795     xj2 = xj*xj
796     yj2 = yj*yj
797     zj2 = zj*zj
798    
799     projj = sqrt(xj2 + yj2)
800     projj3 = projj*projj*projj
801    
802     ctj = zj / rij
803     dctjdx = - zj * xj / r3
804     dctjdy = - zj * yj / r3
805     dctjdz = 1.0d0 / rij - zj2 / r3
806     dctjdux = yj / rij
807     dctjduy = -xj / rij
808     dctjduz = 0.0d0
809    
810     cpj = xj / projj
811     dcpjdx = 1.0d0 / projj - xj2 / projj3
812     dcpjdy = - xj * yj / projj3
813     dcpjdz = 0.0d0
814     dcpjdux = xj * yj * zj / projj3
815     dcpjduy = -zj * (1.0d0 / projj - xj2 / projj3)
816     dcpjduz = -yj * (1.0d0 / projj - xj2 / projj3) - (xj2 * yj / projj3)
817    
818     spj = yj / projj
819     dspjdx = - xj * yj / projj3
820     dspjdy = 1.0d0 / projj - yj2 / projj3
821     dspjdz = 0.0d0
822     dspjdux = -zj * (1.0d0 / projj - yj2 / projj3)
823     dspjduy = xj * yj * zj / projj3
824     dspjduz = xj * (1.0d0 / projj - yi2 / projj3) + (xj * yj2 / projj3)
825    
826     call Associated_Legendre(ctj, ShapeMap%Shapes(st2)%bigL, &
827     ShapeMap%Shapes(st2)%bigM, lmax, plm_j, dlm_j)
828    
829     call Orthogonal_Polynomial(cpj, ShapeMap%Shapes(st2)%bigM, &
830     CHEBYSHEV_TN, tm_j, dtm_j)
831     call Orthogonal_Polynomial(cpj, ShapeMap%Shapes(st2)%bigM, &
832     CHEBYSHEV_UN, um_j, dum_j)
833    
834     sigma_j = 0.0d0
835     s_j = 0.0d0
836     eps_j = 0.0d0
837     dsigmajdx = 0.0d0
838     dsigmajdy = 0.0d0
839     dsigmajdz = 0.0d0
840     dsigmajdux = 0.0d0
841     dsigmajduy = 0.0d0
842     dsigmajduz = 0.0d0
843     dsjdx = 0.0d0
844     dsjdy = 0.0d0
845     dsjdz = 0.0d0
846     dsjdux = 0.0d0
847     dsjduy = 0.0d0
848     dsjduz = 0.0d0
849     depsjdx = 0.0d0
850     depsjdy = 0.0d0
851     depsjdz = 0.0d0
852     depsjdux = 0.0d0
853     depsjduy = 0.0d0
854     depsjduz = 0.0d0
855    
856     do lm = 1, ShapeMap%Shapes(st2)%nContactFuncs
857     l = ShapeMap%Shapes(st2)%ContactFuncLValue(lm)
858     m = ShapeMap%Shapes(st2)%ContactFuncMValue(lm)
859     coeff = ShapeMap%Shapes(st2)%ContactFuncCoefficient(lm)
860     function_type = ShapeMap%Shapes(st2)%ContactFunctionType(lm)
861    
862     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
863     Phunc = coeff * tm_j(m)
864     dPhuncdX = coeff * dtm_j(m) * dcpjdx
865     dPhuncdY = coeff * dtm_j(m) * dcpjdy
866     dPhuncdZ = coeff * dtm_j(m) * dcpjdz
867     dPhuncdUz = coeff * dtm_j(m) * dcpjdux
868     dPhuncdUy = coeff * dtm_j(m) * dcpjduy
869     dPhuncdUz = coeff * dtm_j(m) * dcpjduz
870     else
871     Phunc = coeff * spj * um_j(m-1)
872     dPhuncdX = coeff * (spj * dum_j(m-1) * dcpjdx + dspjdx *um_j(m-1))
873     dPhuncdY = coeff * (spj * dum_j(m-1) * dcpjdy + dspjdy *um_j(m-1))
874     dPhuncdZ = coeff * (spj * dum_j(m-1) * dcpjdz + dspjdz *um_j(m-1))
875     dPhuncdUx = coeff*(spj * dum_j(m-1)*dcpjdux + dspjdux *um_j(m-1))
876     dPhuncdUy = coeff*(spj * dum_j(m-1)*dcpjduy + dspjduy *um_j(m-1))
877     dPhuncdUz = coeff*(spj * dum_j(m-1)*dcpjduz + dspjduz *um_j(m-1))
878     endif
879    
880     sigma_j = sigma_j + plm_j(l,m)*Phunc
881    
882     dsigmajdx = dsigmajdx + plm_j(l,m)*dPhuncdX + &
883     Phunc * dlm_j(l,m) * dctjdx
884     dsigmajdy = dsigmajdy + plm_j(l,m)*dPhuncdY + &
885     Phunc * dlm_j(l,m) * dctjdy
886     dsigmajdz = dsigmajdz + plm_j(l,m)*dPhuncdZ + &
887     Phunc * dlm_j(l,m) * dctjdz
888    
889     dsigmajdux = dsigmajdux + plm_j(l,m)* dPhuncdUx + &
890     Phunc * dlm_j(l,m) * dctjdux
891     dsigmajduy = dsigmajduy + plm_j(l,m)* dPhuncdUy + &
892     Phunc * dlm_j(l,m) * dctjduy
893     dsigmajduz = dsigmajduz + plm_j(l,m)* dPhuncdUz + &
894     Phunc * dlm_j(l,m) * dctjduz
895    
896     end do
897    
898     do lm = 1, ShapeMap%Shapes(st2)%nRangeFuncs
899     l = ShapeMap%Shapes(st2)%RangeFuncLValue(lm)
900     m = ShapeMap%Shapes(st2)%RangeFuncMValue(lm)
901     coeff = ShapeMap%Shapes(st2)%RangeFuncCoefficient(lm)
902     function_type = ShapeMap%Shapes(st2)%RangeFunctionType(lm)
903    
904     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
905     Phunc = coeff * tm_j(m)
906     dPhuncdX = coeff * dtm_j(m) * dcpjdx
907     dPhuncdY = coeff * dtm_j(m) * dcpjdy
908     dPhuncdZ = coeff * dtm_j(m) * dcpjdz
909     dPhuncdUz = coeff * dtm_j(m) * dcpjdux
910     dPhuncdUy = coeff * dtm_j(m) * dcpjduy
911     dPhuncdUz = coeff * dtm_j(m) * dcpjduz
912     else
913     Phunc = coeff * spj * um_j(m-1)
914     dPhuncdX = coeff * (spj * dum_j(m-1) * dcpjdx + dspjdx *um_j(m-1))
915     dPhuncdY = coeff * (spj * dum_j(m-1) * dcpjdy + dspjdy *um_j(m-1))
916     dPhuncdZ = coeff * (spj * dum_j(m-1) * dcpjdz + dspjdz *um_j(m-1))
917     dPhuncdUx = coeff*(spj * dum_j(m-1)*dcpjdux + dspjdux *um_j(m-1))
918     dPhuncdUy = coeff*(spj * dum_j(m-1)*dcpjduy + dspjduy *um_j(m-1))
919     dPhuncdUz = coeff*(spj * dum_j(m-1)*dcpjduz + dspjduz *um_j(m-1))
920     endif
921    
922     s_j = s_j + plm_j(l,m)*Phunc
923    
924     dsjdx = dsjdx + plm_j(l,m)*dPhuncdX + &
925     Phunc * dlm_j(l,m) * dctjdx
926     dsjdy = dsjdy + plm_j(l,m)*dPhuncdY + &
927     Phunc * dlm_j(l,m) * dctjdy
928     dsjdz = dsjdz + plm_j(l,m)*dPhuncdZ + &
929     Phunc * dlm_j(l,m) * dctjdz
930    
931     dsjdux = dsjdux + plm_j(l,m)* dPhuncdUx + &
932     Phunc * dlm_j(l,m) * dctjdux
933     dsjduy = dsjduy + plm_j(l,m)* dPhuncdUy + &
934     Phunc * dlm_j(l,m) * dctjduy
935     dsjduz = dsjduz + plm_j(l,m)* dPhuncdUz + &
936     Phunc * dlm_j(l,m) * dctjduz
937    
938     end do
939    
940     do lm = 1, ShapeMap%Shapes(st2)%nStrengthFuncs
941     l = ShapeMap%Shapes(st2)%StrengthFuncLValue(lm)
942     m = ShapeMap%Shapes(st2)%StrengthFuncMValue(lm)
943     coeff = ShapeMap%Shapes(st2)%StrengthFuncCoefficient(lm)
944     function_type = ShapeMap%Shapes(st2)%StrengthFunctionType(lm)
945    
946     if ((function_type .eq. SH_COS).or.(m.eq.0)) then
947     Phunc = coeff * tm_j(m)
948     dPhuncdX = coeff * dtm_j(m) * dcpjdx
949     dPhuncdY = coeff * dtm_j(m) * dcpjdy
950     dPhuncdZ = coeff * dtm_j(m) * dcpjdz
951     dPhuncdUz = coeff * dtm_j(m) * dcpjdux
952     dPhuncdUy = coeff * dtm_j(m) * dcpjduy
953     dPhuncdUz = coeff * dtm_j(m) * dcpjduz
954     else
955     Phunc = coeff * spj * um_j(m-1)
956     dPhuncdX = coeff * (spj * dum_j(m-1) * dcpjdx + dspjdx *um_j(m-1))
957     dPhuncdY = coeff * (spj * dum_j(m-1) * dcpjdy + dspjdy *um_j(m-1))
958     dPhuncdZ = coeff * (spj * dum_j(m-1) * dcpjdz + dspjdz *um_j(m-1))
959     dPhuncdUx = coeff*(spj * dum_j(m-1)*dcpjdux + dspjdux *um_j(m-1))
960     dPhuncdUy = coeff*(spj * dum_j(m-1)*dcpjduy + dspjduy *um_j(m-1))
961     dPhuncdUz = coeff*(spj * dum_j(m-1)*dcpjduz + dspjduz *um_j(m-1))
962     endif
963    
964     eps_j = eps_j + plm_j(l,m)*Phunc
965    
966     depsjdx = depsjdx + plm_j(l,m)*dPhuncdX + &
967     Phunc * dlm_j(l,m) * dctjdx
968     depsjdy = depsjdy + plm_j(l,m)*dPhuncdY + &
969     Phunc * dlm_j(l,m) * dctjdy
970     depsjdz = depsjdz + plm_j(l,m)*dPhuncdZ + &
971     Phunc * dlm_j(l,m) * dctjdz
972    
973     depsjdux = depsjdux + plm_j(l,m)* dPhuncdUx + &
974     Phunc * dlm_j(l,m) * dctjdux
975     depsjduy = depsjduy + plm_j(l,m)* dPhuncdUy + &
976     Phunc * dlm_j(l,m) * dctjduy
977     depsjduz = depsjduz + plm_j(l,m)* dPhuncdUz + &
978     Phunc * dlm_j(l,m) * dctjduz
979    
980     end do
981    
982     endif
983    
984     ! phew, now let's assemble the potential energy:
985    
986     sigma = 0.5*(sigma_i + sigma_j)
987    
988     dsigmadxi = 0.5*dsigmaidx
989     dsigmadyi = 0.5*dsigmaidy
990     dsigmadzi = 0.5*dsigmaidz
991     dsigmaduxi = 0.5*dsigmaidux
992     dsigmaduyi = 0.5*dsigmaiduy
993     dsigmaduzi = 0.5*dsigmaiduz
994    
995     dsigmadxj = 0.5*dsigmajdx
996     dsigmadyj = 0.5*dsigmajdy
997     dsigmadzj = 0.5*dsigmajdz
998     dsigmaduxj = 0.5*dsigmajdux
999     dsigmaduyj = 0.5*dsigmajduy
1000     dsigmaduzj = 0.5*dsigmajduz
1001    
1002     s = 0.5*(s_i + s_j)
1003    
1004     dsdxi = 0.5*dsidx
1005     dsdyi = 0.5*dsidy
1006     dsdzi = 0.5*dsidz
1007     dsduxi = 0.5*dsidux
1008     dsduyi = 0.5*dsiduy
1009     dsduzi = 0.5*dsiduz
1010    
1011     dsdxj = 0.5*dsjdx
1012     dsdyj = 0.5*dsjdy
1013     dsdzj = 0.5*dsjdz
1014     dsduxj = 0.5*dsjdux
1015     dsduyj = 0.5*dsjduy
1016     dsduzj = 0.5*dsjduz
1017    
1018     eps = sqrt(eps_i * eps_j)
1019    
1020     depsdxi = eps_j * depsidx / (2.0d0 * eps)
1021     depsdyi = eps_j * depsidy / (2.0d0 * eps)
1022     depsdzi = eps_j * depsidz / (2.0d0 * eps)
1023     depsduxi = eps_j * depsidux / (2.0d0 * eps)
1024     depsduyi = eps_j * depsiduy / (2.0d0 * eps)
1025     depsduzi = eps_j * depsiduz / (2.0d0 * eps)
1026    
1027     depsdxj = eps_i * depsjdx / (2.0d0 * eps)
1028     depsdyj = eps_i * depsjdy / (2.0d0 * eps)
1029     depsdzj = eps_i * depsjdz / (2.0d0 * eps)
1030     depsduxj = eps_i * depsjdux / (2.0d0 * eps)
1031     depsduyj = eps_i * depsjduy / (2.0d0 * eps)
1032     depsduzj = eps_i * depsjduz / (2.0d0 * eps)
1033    
1034     rtdenom = rij-sigma+s
1035     rt = s / rtdenom
1036    
1037     drtdxi = (dsdxi + rt * (drdxi - dsigmadxi + dsdxi)) / rtdenom
1038     drtdyi = (dsdyi + rt * (drdyi - dsigmadyi + dsdyi)) / rtdenom
1039     drtdzi = (dsdzi + rt * (drdzi - dsigmadzi + dsdzi)) / rtdenom
1040     drtduxi = (dsduxi + rt * (drduxi - dsigmaduxi + dsduxi)) / rtdenom
1041     drtduyi = (dsduyi + rt * (drduyi - dsigmaduyi + dsduyi)) / rtdenom
1042     drtduzi = (dsduzi + rt * (drduzi - dsigmaduzi + dsduzi)) / rtdenom
1043     drtdxj = (dsdxj + rt * (drdxj - dsigmadxj + dsdxj)) / rtdenom
1044     drtdyj = (dsdyj + rt * (drdyj - dsigmadyj + dsdyj)) / rtdenom
1045     drtdzj = (dsdzj + rt * (drdzj - dsigmadzj + dsdzj)) / rtdenom
1046     drtduxj = (dsduxj + rt * (drduxj - dsigmaduxj + dsduxj)) / rtdenom
1047     drtduyj = (dsduyj + rt * (drduyj - dsigmaduyj + dsduyj)) / rtdenom
1048     drtduzj = (dsduzj + rt * (drduzj - dsigmaduzj + dsduzj)) / rtdenom
1049    
1050     rt2 = rt*rt
1051     rt3 = rt2*rt
1052     rt5 = rt2*rt3
1053     rt6 = rt3*rt3
1054     rt11 = rt5*rt6
1055     rt12 = rt6*rt6
1056     rt126 = rt12 - rt6
1057    
1058     if (do_pot) then
1059     #ifdef IS_MPI
1060     pot_row(atom1) = pot_row(atom1) + 2.0d0*eps*rt126*sw
1061     pot_col(atom2) = pot_col(atom2) + 2.0d0*eps*rt126*sw
1062     #else
1063     pot = pot + 4.0d0*eps*rt126*sw
1064     #endif
1065     endif
1066    
1067     dvdxi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdxi + 4.0d0*depsdxi*rt126
1068     dvdyi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdyi + 4.0d0*depsdyi*rt126
1069     dvdzi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdzi + 4.0d0*depsdzi*rt126
1070     dvduxi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduxi + 4.0d0*depsduxi*rt126
1071     dvduyi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduyi + 4.0d0*depsduyi*rt126
1072     dvduzi = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduzi + 4.0d0*depsduzi*rt126
1073    
1074     dvdxj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdxj + 4.0d0*depsdxj*rt126
1075     dvdyj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdyj + 4.0d0*depsdyj*rt126
1076     dvdzj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtdzj + 4.0d0*depsdzj*rt126
1077     dvduxj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduxj + 4.0d0*depsduxj*rt126
1078     dvduyj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduyj + 4.0d0*depsduyj*rt126
1079     dvduzj = 24.0d0*eps*(2.0d0*rt11 - rt5)*drtduzj + 4.0d0*depsduzj*rt126
1080    
1081     ! do the torques first since they are easy:
1082     ! remember that these are still in the body fixed axes
1083    
1084     txi = dvduxi * sw
1085     tyi = dvduyi * sw
1086     tzi = dvduzi * sw
1087    
1088     txj = dvduxj * sw
1089     tyj = dvduyj * sw
1090     tzj = dvduzj * sw
1091    
1092     ! go back to lab frame using transpose of rotation matrix:
1093    
1094     #ifdef IS_MPI
1095     t_Row(1,atom1) = t_Row(1,atom1) + a_Row(1,atom1)*txi + &
1096     a_Row(4,atom1)*tyi + a_Row(7,atom1)*tzi
1097     t_Row(2,atom1) = t_Row(2,atom1) + a_Row(2,atom1)*txi + &
1098     a_Row(5,atom1)*tyi + a_Row(8,atom1)*tzi
1099     t_Row(3,atom1) = t_Row(3,atom1) + a_Row(3,atom1)*txi + &
1100     a_Row(6,atom1)*tyi + a_Row(9,atom1)*tzi
1101    
1102     t_Col(1,atom2) = t_Col(1,atom2) + a_Col(1,atom2)*txj + &
1103     a_Col(4,atom2)*tyj + a_Col(7,atom2)*tzj
1104     t_Col(2,atom2) = t_Col(2,atom2) + a_Col(2,atom2)*txj + &
1105     a_Col(5,atom2)*tyj + a_Col(8,atom2)*tzj
1106     t_Col(3,atom2) = t_Col(3,atom2) + a_Col(3,atom2)*txj + &
1107     a_Col(6,atom2)*tyj + a_Col(9,atom2)*tzj
1108     #else
1109     t(1,atom1) = t(1,atom1) + a(1,atom1)*txi + a(4,atom1)*tyi + a(7,atom1)*tzi
1110     t(2,atom1) = t(2,atom1) + a(2,atom1)*txi + a(5,atom1)*tyi + a(8,atom1)*tzi
1111     t(3,atom1) = t(3,atom1) + a(3,atom1)*txi + a(6,atom1)*tyi + a(9,atom1)*tzi
1112    
1113     t(1,atom2) = t(1,atom2) + a(1,atom2)*txj + a(4,atom2)*tyj + a(7,atom2)*tzj
1114     t(2,atom2) = t(2,atom2) + a(2,atom2)*txj + a(5,atom2)*tyj + a(8,atom2)*tzj
1115     t(3,atom2) = t(3,atom2) + a(3,atom2)*txj + a(6,atom2)*tyj + a(9,atom2)*tzj
1116     #endif
1117     ! Now, on to the forces:
1118    
1119     ! first rotate the i terms back into the lab frame:
1120    
1121     fxi = dvdxi * sw
1122     fyi = dvdyi * sw
1123     fzi = dvdzi * sw
1124    
1125     fxj = dvdxj * sw
1126     fyj = dvdyj * sw
1127     fzj = dvdzj * sw
1128    
1129     #ifdef IS_MPI
1130     fxii = a_Row(1,atom1)*fxi + a_Row(4,atom1)*fyi + a_Row(7,atom1)*fzi
1131     fyii = a_Row(2,atom1)*fxi + a_Row(5,atom1)*fyi + a_Row(8,atom1)*fzi
1132     fzii = a_Row(3,atom1)*fxi + a_Row(6,atom1)*fyi + a_Row(9,atom1)*fzi
1133    
1134     fxjj = a_Col(1,atom2)*fxj + a_Col(4,atom2)*fyj + a_Col(7,atom2)*fzj
1135     fyjj = a_Col(2,atom2)*fxj + a_Col(5,atom2)*fyj + a_Col(8,atom2)*fzj
1136     fzjj = a_Col(3,atom2)*fxj + a_Col(6,atom2)*fyj + a_Col(9,atom2)*fzj
1137     #else
1138     fxii = a(1,atom1)*fxi + a(4,atom1)*fyi + a(7,atom1)*fzi
1139     fyii = a(2,atom1)*fxi + a(5,atom1)*fyi + a(8,atom1)*fzi
1140     fzii = a(3,atom1)*fxi + a(6,atom1)*fyi + a(9,atom1)*fzi
1141    
1142     fxjj = a(1,atom2)*fxj + a(4,atom2)*fyj + a(7,atom2)*fzj
1143     fyjj = a(2,atom2)*fxj + a(5,atom2)*fyj + a(8,atom2)*fzj
1144     fzjj = a(3,atom2)*fxj + a(6,atom2)*fyj + a(9,atom2)*fzj
1145     #endif
1146    
1147     fxij = -fxii
1148     fyij = -fyii
1149     fzij = -fzii
1150    
1151     fxji = -fxjj
1152     fyji = -fyjj
1153     fzji = -fzjj
1154    
1155     fxradial = fxii + fxji
1156     fyradial = fyii + fyji
1157     fzradial = fzii + fzji
1158    
1159     #ifdef IS_MPI
1160     f_Row(1,atom1) = f_Row(1,atom1) + fxradial
1161     f_Row(2,atom1) = f_Row(2,atom1) + fyradial
1162     f_Row(3,atom1) = f_Row(3,atom1) + fzradial
1163    
1164     f_Col(1,atom2) = f_Col(1,atom2) - fxradial
1165     f_Col(2,atom2) = f_Col(2,atom2) - fyradial
1166     f_Col(3,atom2) = f_Col(3,atom2) - fzradial
1167     #else
1168     f(1,atom1) = f(1,atom1) + fxradial
1169     f(2,atom1) = f(2,atom1) + fyradial
1170     f(3,atom1) = f(3,atom1) + fzradial
1171    
1172     f(1,atom2) = f(1,atom2) - fxradial
1173     f(2,atom2) = f(2,atom2) - fyradial
1174     f(3,atom2) = f(3,atom2) - fzradial
1175     #endif
1176    
1177     #ifdef IS_MPI
1178     id1 = AtomRowToGlobal(atom1)
1179     id2 = AtomColToGlobal(atom2)
1180     #else
1181     id1 = atom1
1182     id2 = atom2
1183     #endif
1184    
1185     if (molMembershipList(id1) .ne. molMembershipList(id2)) then
1186    
1187     fpair(1) = fpair(1) + fxradial
1188     fpair(2) = fpair(2) + fyradial
1189     fpair(3) = fpair(3) + fzradial
1190    
1191     endif
1192    
1193     end subroutine do_shape_pair
1194    
1195     SUBROUTINE Associated_Legendre(x, l, m, lmax, plm, dlm)
1196    
1197     ! Purpose: Compute the associated Legendre functions
1198     ! Plm(x) and their derivatives Plm'(x)
1199     ! Input : x --- Argument of Plm(x)
1200     ! l --- Order of Plm(x), l = 0,1,2,...,n
1201     ! m --- Degree of Plm(x), m = 0,1,2,...,N
1202     ! lmax --- Physical dimension of PLM and DLM
1203     ! Output: PLM(l,m) --- Plm(x)
1204     ! DLM(l,m) --- Plm'(x)
1205     !
1206     ! adapted from the routines in
1207     ! COMPUTATION OF SPECIAL FUNCTIONS by Shanjie Zhang and Jianming Jin
1208     ! ISBN 0-471-11963-6
1209     !
1210     ! The original Fortran77 codes can be found here:
1211     ! http://iris-lee3.ece.uiuc.edu/~jjin/routines/routines.html
1212    
1213     real (kind=8), intent(in) :: x
1214     integer, intent(in) :: l, m, lmax
1215     real (kind=8), dimension(0:lmax,0:m), intent(out) :: PLM, DLM
1216     integer :: i, j, ls
1217     real (kind=8) :: xq, xs
1218    
1219     ! zero out both arrays:
1220     DO I = 0, m
1221     DO J = 0, l
1222     PLM(J,I) = 0.0D0
1223     DLM(J,I) = 0.0D0
1224     end DO
1225     end DO
1226    
1227     ! start with 0,0:
1228     PLM(0,0) = 1.0D0
1229    
1230     ! x = +/- 1 functions are easy:
1231     IF (abs(X).EQ.1.0D0) THEN
1232     DO I = 1, m
1233     PLM(0, I) = X**I
1234     DLM(0, I) = 0.5D0*I*(I+1.0D0)*X**(I+1)
1235     end DO
1236     DO J = 1, m
1237     DO I = 1, l
1238     IF (I.EQ.1) THEN
1239     DLM(I, J) = 1.0D+300
1240     ELSE IF (I.EQ.2) THEN
1241     DLM(I, J) = -0.25D0*(J+2)*(J+1)*J*(J-1)*X**(J+1)
1242     ENDIF
1243     end DO
1244     end DO
1245     RETURN
1246     ENDIF
1247    
1248     LS = 1
1249     IF (abs(X).GT.1.0D0) LS = -1
1250     XQ = sqrt(LS*(1.0D0-X*X))
1251     XS = LS*(1.0D0-X*X)
1252    
1253     DO I = 1, l
1254     PLM(I, I) = -LS*(2.0D0*I-1.0D0)*XQ*PLM(I-1, I-1)
1255     enddo
1256    
1257     DO I = 0, l
1258     PLM(I, I+1)=(2.0D0*I+1.0D0)*X*PLM(I, I)
1259     enddo
1260    
1261     DO I = 0, l
1262     DO J = I+2, m
1263     PLM(I, J)=((2.0D0*J-1.0D0)*X*PLM(I,J-1) - &
1264     (I+J-1.0D0)*PLM(I,J-2))/(J-I)
1265     end DO
1266     end DO
1267    
1268     DLM(0, 0)=0.0D0
1269    
1270     DO J = 1, m
1271     DLM(0, J)=LS*J*(PLM(0,J-1)-X*PLM(0,J))/XS
1272     end DO
1273    
1274     DO I = 1, l
1275     DO J = I, m
1276     DLM(I,J) = LS*I*X*PLM(I, J)/XS + (J+I)*(J-I+1.0D0)/XQ*PLM(I-1, J)
1277     end DO
1278     end DO
1279    
1280     RETURN
1281     END SUBROUTINE Associated_Legendre
1282    
1283    
1284     subroutine Orthogonal_Polynomial(x, m, function_type, pl, dpl)
1285    
1286     ! Purpose: Compute orthogonal polynomials: Tn(x) or Un(x),
1287     ! or Ln(x) or Hn(x), and their derivatives
1288     ! Input : function_type --- Function code
1289     ! =1 for Chebyshev polynomial Tn(x)
1290     ! =2 for Chebyshev polynomial Un(x)
1291     ! =3 for Laguerre polynomial Ln(x)
1292     ! =4 for Hermite polynomial Hn(x)
1293     ! n --- Order of orthogonal polynomials
1294     ! x --- Argument of orthogonal polynomials
1295     ! Output: PL(n) --- Tn(x) or Un(x) or Ln(x) or Hn(x)
1296     ! DPL(n)--- Tn'(x) or Un'(x) or Ln'(x) or Hn'(x)
1297     !
1298     ! adapted from the routines in
1299     ! COMPUTATION OF SPECIAL FUNCTIONS by Shanjie Zhang and Jianming Jin
1300     ! ISBN 0-471-11963-6
1301     !
1302     ! The original Fortran77 codes can be found here:
1303     ! http://iris-lee3.ece.uiuc.edu/~jjin/routines/routines.html
1304    
1305     real(kind=8), intent(in) :: x
1306     integer, intent(in):: m
1307     integer, intent(in):: function_type
1308     real(kind=8), dimension(0:m), intent(inout) :: pl, dpl
1309    
1310     real(kind=8) :: a, b, c, y0, y1, dy0, dy1, yn, dyn
1311     integer :: k
1312    
1313     A = 2.0D0
1314     B = 0.0D0
1315     C = 1.0D0
1316     Y0 = 1.0D0
1317     Y1 = 2.0D0*X
1318     DY0 = 0.0D0
1319     DY1 = 2.0D0
1320     PL(0) = 1.0D0
1321     PL(1) = 2.0D0*X
1322     DPL(0) = 0.0D0
1323     DPL(1) = 2.0D0
1324     IF (function_type.EQ.CHEBYSHEV_TN) THEN
1325     Y1 = X
1326     DY1 = 1.0D0
1327     PL(1) = X
1328     DPL(1) = 1.0D0
1329     ELSE IF (function_type.EQ.LAGUERRE) THEN
1330     Y1 = 1.0D0-X
1331     DY1 = -1.0D0
1332     PL(1) = 1.0D0-X
1333     DPL(1) = -1.0D0
1334     ENDIF
1335     DO K = 2, m
1336     IF (function_type.EQ.LAGUERRE) THEN
1337     A = -1.0D0/K
1338     B = 2.0D0+A
1339     C = 1.0D0+A
1340     ELSE IF (function_type.EQ.HERMITE) THEN
1341     C = 2.0D0*(K-1.0D0)
1342     ENDIF
1343     YN = (A*X+B)*Y1-C*Y0
1344     DYN = A*Y1+(A*X+B)*DY1-C*DY0
1345     PL(K) = YN
1346     DPL(K) = DYN
1347     Y0 = Y1
1348     Y1 = YN
1349     DY0 = DY1
1350     DY1 = DYN
1351     end DO
1352     RETURN
1353    
1354     end subroutine Orthogonal_Polynomial
1355    
1356     end module shapes
1357    
1358     subroutine makeShape(nContactFuncs, ContactFuncLValue, &
1359     ContactFuncMValue, ContactFunctionType, ContactFuncCoefficient, &
1360     nRangeFuncs, RangeFuncLValue, RangeFuncMValue, RangeFunctionType, &
1361     RangeFuncCoefficient, nStrengthFuncs, StrengthFuncLValue, &
1362     StrengthFuncMValue, StrengthFunctionType, StrengthFuncCoefficient, &
1363     myAtid, status)
1364    
1365     use definitions
1366     use shapes, only: newShapeType
1367    
1368     integer :: nContactFuncs
1369     integer :: nRangeFuncs
1370     integer :: nStrengthFuncs
1371     integer :: status
1372     integer :: myAtid
1373    
1374     integer, dimension(nContactFuncs) :: ContactFuncLValue
1375     integer, dimension(nContactFuncs) :: ContactFuncMValue
1376     integer, dimension(nContactFuncs) :: ContactFunctionType
1377     real(kind=dp), dimension(nContactFuncs) :: ContactFuncCoefficient
1378     integer, dimension(nRangeFuncs) :: RangeFuncLValue
1379     integer, dimension(nRangeFuncs) :: RangeFuncMValue
1380     integer, dimension(nRangeFuncs) :: RangeFunctionType
1381     real(kind=dp), dimension(nRangeFuncs) :: RangeFuncCoefficient
1382     integer, dimension(nStrengthFuncs) :: StrengthFuncLValue
1383     integer, dimension(nStrengthFuncs) :: StrengthFuncMValue
1384     integer, dimension(nStrengthFuncs) :: StrengthFunctionType
1385     real(kind=dp), dimension(nStrengthFuncs) :: StrengthFuncCoefficient
1386    
1387     call newShapeType(nContactFuncs, ContactFuncLValue, &
1388     ContactFuncMValue, ContactFunctionType, ContactFuncCoefficient, &
1389     nRangeFuncs, RangeFuncLValue, RangeFuncMValue, RangeFunctionType, &
1390     RangeFuncCoefficient, nStrengthFuncs, StrengthFuncLValue, &
1391     StrengthFuncMValue, StrengthFunctionType, StrengthFuncCoefficient, &
1392     myAtid, status)
1393    
1394     return
1395     end subroutine makeShape