File: lal_re_squared_lj.cu

package info (click to toggle)
lammps 20220106.git7586adbb6a%2Bds1-2
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 348,064 kB
  • sloc: cpp: 831,421; python: 24,896; xml: 14,949; f90: 10,845; ansic: 7,967; sh: 4,226; perl: 4,064; fortran: 2,424; makefile: 1,501; objc: 238; lisp: 163; csh: 16; awk: 14; tcl: 6
file content (787 lines) | stat: -rw-r--r-- 30,388 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
// **************************************************************************
//                               re_squared_lj.cu
//                             -------------------
//                               W. Michael Brown
//
//  Device code for RE-Squared - Lennard-Jones potential acceleration
//
// __________________________________________________________________________
//    This file is part of the LAMMPS Accelerator Library (LAMMPS_AL)
// __________________________________________________________________________
//
//    begin                : Fri May 06 2011
//    email                : brownw@ornl.gov
// ***************************************************************************

#if defined(NV_KERNEL) || defined(USE_HIP)
#include "lal_ellipsoid_extra.h"
#endif

#if (SHUFFLE_AVAIL == 0)
#define local_allocate_store_ellipse_lj local_allocate_store_ellipse
#else
#define local_allocate_store_ellipse_lj()                                   \
    __local acctyp red_acc[7][BLOCK_ELLIPSE / SIMD_SIZE];
#endif

#if (SHUFFLE_AVAIL == 0)

#define store_answers_rt(f, tor, energy, virial, ii, astride, tid,           \
                         t_per_atom, offset, eflag, vflag, ans, engv, inum)  \
  if (t_per_atom>1) {                                                        \
    red_acc[0][tid]=f.x;                                                     \
    red_acc[1][tid]=f.y;                                                     \
    red_acc[2][tid]=f.z;                                                     \
    red_acc[3][tid]=tor.x;                                                   \
    red_acc[4][tid]=tor.y;                                                   \
    red_acc[5][tid]=tor.z;                                                   \
    for (unsigned int s=t_per_atom/2; s>0; s>>=1) {                          \
      simdsync();                                                            \
      if (offset < s) {                                                      \
        for (int r=0; r<6; r++)                                              \
          red_acc[r][tid] += red_acc[r][tid+s];                              \
      }                                                                      \
    }                                                                        \
    f.x=red_acc[0][tid];                                                     \
    f.y=red_acc[1][tid];                                                     \
    f.z=red_acc[2][tid];                                                     \
    tor.x=red_acc[3][tid];                                                   \
    tor.y=red_acc[4][tid];                                                   \
    tor.z=red_acc[5][tid];                                                   \
    if (EVFLAG && (eflag || vflag)) {                                        \
      if (vflag) {                                                           \
        simdsync();                                                          \
        for (int r=0; r<6; r++)                                              \
          red_acc[r][tid]=virial[r];                                         \
        for (unsigned int s=t_per_atom/2; s>0; s>>=1) {                      \
          simdsync();                                                        \
          if (offset < s) {                                                  \
            for (int r=0; r<6; r++)                                          \
              red_acc[r][tid] += red_acc[r][tid+s];                          \
          }                                                                  \
        }                                                                    \
        for (int r=0; r<6; r++)                                              \
          virial[r]=red_acc[r][tid];                                         \
      }                                                                      \
      if (eflag) {                                                           \
        simdsync();                                                          \
        red_acc[0][tid]=energy;                                              \
        for (unsigned int s=t_per_atom/2; s>0; s>>=1) {                      \
          simdsync();                                                        \
          if (offset < s) red_acc[0][tid] += red_acc[0][tid+s];              \
        }                                                                    \
      }                                                                      \
      energy=red_acc[0][tid];                                                \
    }                                                                        \
  }                                                                          \
  if (offset==0 && ii<inum) {                                                \
    __global acctyp *ap1=engv+ii;                                            \
    if (EVFLAG && eflag) {                                                   \
      *ap1+=energy*(acctyp)0.5;                                              \
      ap1+=astride;                                                          \
    }                                                                        \
    if (EVFLAG && vflag) {                                                   \
      for (int i=0; i<6; i++) {                                              \
        *ap1+=virial[i]*(acctyp)0.5;                                         \
        ap1+=astride;                                                        \
      }                                                                      \
    }                                                                        \
    acctyp4 old=ans[ii];                                                     \
    old.x+=f.x;                                                              \
    old.y+=f.y;                                                              \
    old.z+=f.z;                                                              \
    ans[ii]=old;                                                             \
    old=ans[ii+astride];                                                     \
    old.x+=tor.x;                                                            \
    old.y+=tor.y;                                                            \
    old.z+=tor.z;                                                            \
    ans[ii+astride]=old;                                                     \
  }

#else

#define store_answers_rt(f, tor, energy, virial, ii, astride, tid,           \
                         t_per_atom, offset, eflag, vflag, ans, engv, inum)  \
  if (t_per_atom>1) {                                                        \
    for (unsigned int s=t_per_atom/2; s>0; s>>=1) {                          \
        f.x += shfl_down(f.x, s, t_per_atom);                                \
        f.y += shfl_down(f.y, s, t_per_atom);                                \
        f.z += shfl_down(f.z, s, t_per_atom);                                \
        tor.x += shfl_down(tor.x, s, t_per_atom);                            \
        tor.y += shfl_down(tor.y, s, t_per_atom);                            \
        tor.z += shfl_down(tor.z, s, t_per_atom);                            \
        energy += shfl_down(energy, s, t_per_atom);                          \
    }                                                                       \
    if (EVFLAG && vflag) {                                                  \
      for (unsigned int s=t_per_atom/2; s>0; s>>=1) {                       \
          for (int r=0; r<6; r++)                                           \
            virial[r] += shfl_down(virial[r], s, t_per_atom);                \
      }                                                                     \
    }                                                                       \
  }                                                                         \
  if (offset==0 && ii<inum) {                                               \
    __global acctyp *ap1=engv+ii;                                           \
    if (EVFLAG && eflag) {                                                  \
      *ap1+=energy*(acctyp)0.5;                                             \
      ap1+=astride;                                                         \
    }                                                                       \
    if (EVFLAG && vflag) {                                                  \
      for (int i=0; i<6; i++) {                                             \
        *ap1+=virial[i]*(acctyp)0.5;                                        \
        ap1+=astride;                                                       \
      }                                                                     \
    }                                                                       \
    acctyp4 old=ans[ii];                                                    \
    old.x+=f.x;                                                             \
    old.y+=f.y;                                                             \
    old.z+=f.z;                                                             \
    ans[ii]=old;                                                            \
    old=ans[ii+astride];                                                    \
    old.x+=tor.x;                                                           \
    old.y+=tor.y;                                                           \
    old.z+=tor.z;                                                           \
    ans[ii+astride]=old;                                                    \
  }

#endif

__kernel void k_resquared_ellipsoid_sphere(const __global numtyp4 *restrict x_,
                                           const __global numtyp4 *restrict q,
                                           const __global numtyp4 *restrict shape,
                                           const __global numtyp4 *restrict well,
                                           const __global numtyp *restrict splj,
                                           const __global numtyp2 *restrict sig_eps,
                                           const int ntypes,
                                           const __global int *dev_nbor,
                                           const int stride,
                                           __global acctyp4 *restrict ans,
                                           const int astride,
                                           __global acctyp *restrict engv,
                                           __global int *restrict err_flag,
                                           const int eflag, const int vflag,
                                           const int inum,
                                           const int t_per_atom) {
  int tid, ii, offset;
  atom_info(t_per_atom,ii,tid,offset);

  __local numtyp sp_lj[4];
  int n_stride;
  local_allocate_store_ellipse();

  sp_lj[0]=splj[0];
  sp_lj[1]=splj[1];
  sp_lj[2]=splj[2];
  sp_lj[3]=splj[3];

  const numtyp b_alpha=(numtyp)45.0/(numtyp)56.0;
  const numtyp cr60=ucl_cbrt((numtyp)60.0);
  const numtyp solv_f_a =
     (numtyp)3.0/((numtyp)16.0*ucl_atan((numtyp)1.0)*-(numtyp)36.0);
  const numtyp solv_f_r =
     (numtyp)3.0/((numtyp)16.0*ucl_atan((numtyp)1.0)*(numtyp)2025.0);

  acctyp4 f, tor;
  f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0;
  tor.x=(acctyp)0; tor.y=(acctyp)0; tor.z=(acctyp)0;
  acctyp energy, virial[6];
  if (EVFLAG) {
    energy=(acctyp)0;
    for (int i=0; i<6; i++) virial[i]=(acctyp)0;
  }

  if (ii<inum) {
    int nbor, nbor_end;
    int i, numj;
    nbor_info_p(dev_nbor,stride,t_per_atom,ii,offset,i,numj,
                n_stride,nbor_end,nbor);

    numtyp4 ix; fetch4(ix,i,pos_tex);
    int itype=ix.w;

    numtyp a[9];       // Rotation matrix (lab->body)
    numtyp aTe[9];     // A'*E
    numtyp lA_0[9], lA_1[9], lA_2[9]; // -A*rotation generator (x,y, or z)

    numtyp4 ishape;
    ishape=shape[itype];
    numtyp ilshape=ishape.x*ishape.y*ishape.z;

    {
      gpu_quat_to_mat_trans(q,i,a);
      gpu_transpose_times_diag3(a,well[itype],aTe);
      gpu_rotation_generator_x(a,lA_0);
      gpu_rotation_generator_y(a,lA_1);
      gpu_rotation_generator_z(a,lA_2);
    }

    numtyp factor_lj;
    for ( ; nbor<nbor_end; nbor+=n_stride) {
      int j=dev_nbor[nbor];
      factor_lj = sp_lj[sbmask(j)];
      j &= NEIGHMASK;

      numtyp4 jx; fetch4(jx,j,pos_tex);
      int jtype=jx.w;

      // Compute r12
      numtyp r[3], rhat[3];
      numtyp rnorm;
      r[0] = jx.x-ix.x;
      r[1] = jx.y-ix.y;
      r[2] = jx.z-ix.z;
      rnorm = gpu_dot3(r,r);
      rnorm = ucl_rsqrt(rnorm);
      rhat[0] = r[0]*rnorm;
      rhat[1] = r[1]*rnorm;
      rhat[2] = r[2]*rnorm;

      numtyp sigma, epsilon;
      int mtype=fast_mul(ntypes,itype)+jtype;
      sigma = sig_eps[mtype].x;
      epsilon = sig_eps[mtype].y*factor_lj;

      numtyp aTs[9];
      numtyp4 scorrect;
      numtyp half_sigma=sigma*(numtyp)0.5;
      scorrect.x = ishape.x+half_sigma;
      scorrect.y = ishape.y+half_sigma;
      scorrect.z = ishape.z+half_sigma;
      scorrect.x = scorrect.x * scorrect.x * (numtyp)0.5;
      scorrect.y = scorrect.y * scorrect.y * (numtyp)0.5;
      scorrect.z = scorrect.z * scorrect.z * (numtyp)0.5;
      gpu_transpose_times_diag3(a,scorrect,aTs);

      // energy

      numtyp gamma[9], s[3];
      gpu_times3(aTs,a,gamma);
      gpu_mldivide3(gamma,rhat,s,err_flag);

      numtyp sigma12 = ucl_rsqrt((numtyp)0.5*gpu_dot3(s,rhat));
      numtyp temp[9], w[3];
      gpu_times3(aTe,a,temp);
      temp[0] += (numtyp)1.0;
      temp[4] += (numtyp)1.0;
      temp[8] += (numtyp)1.0;
      gpu_mldivide3(temp,rhat,w,err_flag);

      numtyp h12 = ucl_recip(rnorm)-sigma12;
      numtyp chi = (numtyp)2.0*gpu_dot3(rhat,w);
      numtyp sigh = sigma/h12;
      numtyp tprod = chi*sigh;

      numtyp Ua, Ur;
      numtyp h12p3 = h12*h12*h12;
      numtyp sigmap3 = sigma*sigma*sigma;
      numtyp stemp = h12*(numtyp)0.5;
      Ua = (ishape.x+stemp)*(ishape.y+stemp)*(ishape.z+stemp)*h12p3/(numtyp)8.0;
      Ua = ((numtyp)1.0+(numtyp)3.0*tprod)*ilshape/Ua;
      Ua = epsilon*Ua*sigmap3*solv_f_a;

      stemp = h12/cr60;
      Ur = (ishape.x+stemp)*(ishape.y+stemp)*(ishape.z+stemp)*h12p3/
           (numtyp)60.0;
      Ur = ((numtyp)1.0+b_alpha*tprod)*ilshape/Ur;
      numtyp sigh6=sigh*sigh*sigh;
      sigh6*=sigh6;
      Ur = epsilon*Ur*sigmap3*sigh6*solv_f_r;

      energy+=Ua+Ur;

      // force

      numtyp fourw[3], spr[3];
      numtyp sec = sigma*chi;
      numtyp sigma12p3 = sigma12*sigma12*sigma12;
      fourw[0] = (numtyp)4.0*w[0];
      fourw[1] = (numtyp)4.0*w[1];
      fourw[2] = (numtyp)4.0*w[2];
      spr[0] = (numtyp)0.5*sigma12p3*s[0];
      spr[1] = (numtyp)0.5*sigma12p3*s[1];
      spr[2] = (numtyp)0.5*sigma12p3*s[2];

      stemp = ucl_recip(ishape.x*(numtyp)2.0+h12)+
              ucl_recip(ishape.y*(numtyp)2.0+h12)+
              ucl_recip(ishape.z*(numtyp)2.0+h12)+
              (numtyp)3.0/h12;
      numtyp hsec = ucl_recip(h12+(numtyp)3.0*sec);
      numtyp dspu = ucl_recip(h12)-hsec+stemp;
      numtyp pbsu = (numtyp)3.0*sigma*hsec;

      stemp = ucl_recip(ishape.x*cr60+h12)+
              ucl_recip(ishape.y*cr60+h12)+
              ucl_recip(ishape.z*cr60+h12)+
              (numtyp)3.0/h12;
      hsec = ucl_recip(h12+b_alpha*sec);
      numtyp dspr = (numtyp)7.0/h12-hsec+stemp;
      numtyp pbsr = b_alpha*sigma*hsec;

      #pragma unroll
      for (int i=0; i<3; i++) {
        numtyp u[3];
        u[0] = -rhat[i]*rhat[0];
        u[1] = -rhat[i]*rhat[1];
        u[2] = -rhat[i]*rhat[2];
        u[i] += (numtyp)1.0;
        u[0] *= rnorm;
        u[1] *= rnorm;
        u[2] *= rnorm;
        numtyp dchi = gpu_dot3(u,fourw);
        numtyp dh12 = rhat[i]+gpu_dot3(u,spr);
        numtyp dUa = pbsu*dchi-dh12*dspu;
        numtyp dUr = pbsr*dchi-dh12*dspr;
        numtyp force=dUr*Ur+dUa*Ua;
        if (i==0) {
          f.x+=force;
          if (EVFLAG && vflag)
            virial[0]+=-r[0]*force;
        } else if (i==1) {
          f.y+=force;
          if (EVFLAG && vflag) {
            virial[1]+=-r[1]*force;
            virial[3]+=-r[0]*force;
          }
        } else {
          f.z+=force;
          if (EVFLAG && vflag) {
            virial[2]+=-r[2]*force;
            virial[4]+=-r[0]*force;
            virial[5]+=-r[1]*force;
          }
        }

      }

      // torque on i
      numtyp fwae[3];
      gpu_row_times3(fourw,aTe,fwae);
      {
        numtyp tempv[3], p[3], lAtwo[9];
        gpu_times_column3(lA_0,rhat,p);
        gpu_times_column3(lA_0,w,tempv);
        numtyp dchi = -gpu_dot3(fwae,tempv);
        gpu_times3(aTs,lA_0,lAtwo);
        gpu_times_column3(lAtwo,spr,tempv);
        numtyp dh12 = -gpu_dot3(s,tempv);
        numtyp dUa = pbsu*dchi-dh12*dspu;
        numtyp dUr = pbsr*dchi-dh12*dspr;
        tor.x -= (dUa*Ua+dUr*Ur);
      }

      {
        numtyp tempv[3], p[3], lAtwo[9];
        gpu_times_column3(lA_1,rhat,p);
        gpu_times_column3(lA_1,w,tempv);
        numtyp dchi = -gpu_dot3(fwae,tempv);
        gpu_times3(aTs,lA_1,lAtwo);
        gpu_times_column3(lAtwo,spr,tempv);
        numtyp dh12 = -gpu_dot3(s,tempv);
        numtyp dUa = pbsu*dchi-dh12*dspu;
        numtyp dUr = pbsr*dchi-dh12*dspr;
        tor.y -= (dUa*Ua+dUr*Ur);
      }

      {
        numtyp tempv[3], p[3], lAtwo[9];
        gpu_times_column3(lA_2,rhat,p);
        gpu_times_column3(lA_2,w,tempv);
        numtyp dchi = -gpu_dot3(fwae,tempv);
        gpu_times3(aTs,lA_2,lAtwo);
        gpu_times_column3(lAtwo,spr,tempv);
        numtyp dh12 = -gpu_dot3(s,tempv);
        numtyp dUa = pbsu*dchi-dh12*dspu;
        numtyp dUr = pbsr*dchi-dh12*dspr;
        tor.z -= (dUa*Ua+dUr*Ur);
      }

    } // for nbor
  } // if ii
  store_answers_rt(f,tor,energy,virial,ii,astride,tid,t_per_atom,offset,
                   eflag,vflag,ans,engv,inum);
}

__kernel void k_resquared_sphere_ellipsoid(const __global numtyp4 *restrict x_,
                                           const __global numtyp4 *restrict q,
                                           const __global numtyp4 *restrict shape,
                                           const __global numtyp4 *restrict well,
                                           const __global numtyp *restrict splj,
                                           const __global numtyp2 *restrict sig_eps,
                                           const int ntypes,
                                           const __global int *dev_nbor,
                                           const int stride,
                                           __global acctyp4 *restrict ans,
                                           __global acctyp *restrict engv,
                                           __global int *restrict err_flag,
                                           const int eflag, const int vflag,
                                           const int start, const int inum,
                                           const int t_per_atom) {
  int tid, ii, offset;
  atom_info(t_per_atom,ii,tid,offset);
  ii+=start;

  __local numtyp sp_lj[4];
  int n_stride;
  local_allocate_store_ellipse_lj();

  sp_lj[0]=splj[0];
  sp_lj[1]=splj[1];
  sp_lj[2]=splj[2];
  sp_lj[3]=splj[3];

  const numtyp b_alpha=(numtyp)45.0/(numtyp)56.0;
  const numtyp cr60=ucl_cbrt((numtyp)60.0);
  const numtyp solv_f_a =
    (numtyp)3.0/((numtyp)16.0*ucl_atan((numtyp)1.0)*-(numtyp)36.0);
  const numtyp solv_f_r =
    (numtyp)3.0/((numtyp)16.0*ucl_atan((numtyp)1.0)*(numtyp)2025.0);

  acctyp4 f;
  f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0;
  acctyp energy, virial[6];
  if (EVFLAG) {
    energy=(acctyp)0;
    for (int i=0; i<6; i++) virial[i]=(acctyp)0;
  }

  if (ii<inum) {
    int nbor, nbor_end;
    int j, numj;
    nbor_info_p(dev_nbor,stride,t_per_atom,ii,offset,j,numj,
                n_stride,nbor_end,nbor);

    numtyp4 jx; fetch4(jx,j,pos_tex);
    int jtype=jx.w;

    numtyp factor_lj;
    for ( ; nbor<nbor_end; nbor+=n_stride) {
      int i=dev_nbor[nbor];
      factor_lj = sp_lj[sbmask(i)];
      i &= NEIGHMASK;

      numtyp4 ix; fetch4(ix,i,pos_tex);
      int itype=ix.w;

      numtyp a[9];       // Rotation matrix (lab->body)
      numtyp aTe[9];     // A'*E
      numtyp4 ishape;

      ishape=shape[itype];
      gpu_quat_to_mat_trans(q,i,a);
      gpu_transpose_times_diag3(a,well[itype],aTe);

      // Compute r12
      numtyp r[3], rhat[3];
      numtyp rnorm;
      r[0] = ix.x-jx.x;
      r[1] = ix.y-jx.y;
      r[2] = ix.z-jx.z;
      rnorm = gpu_dot3(r,r);
      rnorm = ucl_rsqrt(rnorm);
      rhat[0] = r[0]*rnorm;
      rhat[1] = r[1]*rnorm;
      rhat[2] = r[2]*rnorm;

      numtyp sigma, epsilon;
      int mtype=fast_mul(ntypes,itype)+jtype;
      sigma = sig_eps[mtype].x;
      epsilon = sig_eps[mtype].y*factor_lj;

      numtyp aTs[9];
      numtyp4 scorrect;
      numtyp half_sigma=sigma * (numtyp)0.5;
      scorrect.x = ishape.x+half_sigma;
      scorrect.y = ishape.y+half_sigma;
      scorrect.z = ishape.z+half_sigma;
      scorrect.x = scorrect.x * scorrect.x * (numtyp)0.5;
      scorrect.y = scorrect.y * scorrect.y * (numtyp)0.5;
      scorrect.z = scorrect.z * scorrect.z * (numtyp)0.5;
      gpu_transpose_times_diag3(a,scorrect,aTs);

      // energy

      numtyp gamma[9], s[3];
      gpu_times3(aTs,a,gamma);
      gpu_mldivide3(gamma,rhat,s,err_flag);

      numtyp sigma12 = ucl_rsqrt((numtyp)0.5*gpu_dot3(s,rhat));
      numtyp temp[9], w[3];
      gpu_times3(aTe,a,temp);
      temp[0] += (numtyp)1.0;
      temp[4] += (numtyp)1.0;
      temp[8] += (numtyp)1.0;
      gpu_mldivide3(temp,rhat,w,err_flag);

      numtyp h12 = ucl_recip(rnorm)-sigma12;
      numtyp chi = (numtyp)2.0*gpu_dot3(rhat,w);
      numtyp sigh = sigma/h12;
      numtyp tprod = chi*sigh;

      numtyp Ua, Ur;
      numtyp h12p3 = h12*h12*h12;
      numtyp sigmap3 = sigma*sigma*sigma;
      numtyp stemp = h12/(numtyp)2.0;
      Ua = (ishape.x+stemp)*(ishape.y+stemp)*(ishape.z+stemp)*h12p3/(numtyp)8.0;
      numtyp ilshape=ishape.x*ishape.y*ishape.z;
      Ua = ((numtyp)1.0+(numtyp)3.0*tprod)*ilshape/Ua;
      Ua = epsilon*Ua*sigmap3*solv_f_a;

      stemp = h12/cr60;
      Ur = (ishape.x+stemp)*(ishape.y+stemp)*(ishape.z+stemp)*h12p3/
           (numtyp)60.0;
      Ur = ((numtyp)1.0+b_alpha*tprod)*ilshape/Ur;
      numtyp sigh6=sigh*sigh*sigh;
      sigh6*=sigh6;
      Ur = epsilon*Ur*sigmap3*sigh6*solv_f_r;

      energy+=Ua+Ur;

      // force

      numtyp fourw[3], spr[3];
      numtyp sec = sigma*chi;
      numtyp sigma12p3 = sigma12*sigma12*sigma12;
      fourw[0] = (numtyp)4.0*w[0];
      fourw[1] = (numtyp)4.0*w[1];
      fourw[2] = (numtyp)4.0*w[2];
      spr[0] = (numtyp)0.5*sigma12p3*s[0];
      spr[1] = (numtyp)0.5*sigma12p3*s[1];
      spr[2] = (numtyp)0.5*sigma12p3*s[2];

      stemp = ucl_recip(ishape.x*(numtyp)2.0+h12)+
              ucl_recip(ishape.y*(numtyp)2.0+h12)+
              ucl_recip(ishape.z*(numtyp)2.0+h12)+
              (numtyp)3.0/h12;
      numtyp hsec = ucl_recip(h12+(numtyp)3.0*sec);
      numtyp dspu = ucl_recip(h12)-hsec+stemp;
      numtyp pbsu = (numtyp)3.0*sigma*hsec;

      stemp = ucl_recip(ishape.x*cr60+h12)+
              ucl_recip(ishape.y*cr60+h12)+
              ucl_recip(ishape.z*cr60+h12)+
              (numtyp)3.0/h12;
      hsec = ucl_recip(h12+b_alpha*sec);
      numtyp dspr = (numtyp)7.0/h12-hsec+stemp;
      numtyp pbsr = b_alpha*sigma*hsec;

      #pragma unroll
      for (int i=0; i<3; i++) {
        numtyp u[3];
        u[0] = -rhat[i]*rhat[0];
        u[1] = -rhat[i]*rhat[1];
        u[2] = -rhat[i]*rhat[2];
        u[i] += (numtyp)1.0;
        u[0] *= rnorm;
        u[1] *= rnorm;
        u[2] *= rnorm;
        numtyp dchi = gpu_dot3(u,fourw);
        numtyp dh12 = rhat[i]+gpu_dot3(u,spr);
        numtyp dUa = pbsu*dchi-dh12*dspu;
        numtyp dUr = pbsr*dchi-dh12*dspr;
        numtyp force=dUr*Ur+dUa*Ua;
        if (i==0) {
          f.x+=force;
          if (EVFLAG && vflag)
            virial[0]+=-r[0]*force;
        } else if (i==1) {
          f.y+=force;
          if (EVFLAG && vflag) {
            virial[1]+=-r[1]*force;
            virial[3]+=-r[0]*force;
          }
        } else {
          f.z+=force;
          if (EVFLAG && vflag) {
            virial[2]+=-r[2]*force;
            virial[4]+=-r[0]*force;
            virial[5]+=-r[1]*force;
          }
        }
      }
    } // for nbor
  } // if ii
  store_answers(f,energy,virial,ii,inum,tid,t_per_atom,offset,eflag,vflag,
                ans,engv);
}

__kernel void k_resquared_lj(const __global numtyp4 *restrict x_,
                             const __global numtyp4 *restrict lj1,
                             const __global numtyp4 *restrict lj3,
                             const int lj_types,
                             const __global numtyp *restrict gum,
                             const int stride,
                             const __global int *dev_ij,
                             __global acctyp4 *restrict ans,
                             __global acctyp *restrict engv,
                             __global int *restrict err_flag,
                             const int eflag, const int vflag, const int start,
                             const int inum, const int t_per_atom) {
  int tid, ii, offset;
  atom_info(t_per_atom,ii,tid,offset);
  ii+=start;

  __local numtyp sp_lj[4];
  int n_stride;
  local_allocate_store_ellipse();

  sp_lj[0]=gum[0];
  sp_lj[1]=gum[1];
  sp_lj[2]=gum[2];
  sp_lj[3]=gum[3];

  acctyp4 f;
  f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0;
  acctyp energy, virial[6];
  if (EVFLAG) {
    energy=(acctyp)0;
    for (int i=0; i<6; i++) virial[i]=(acctyp)0;
  }

  if (ii<inum) {
    int nbor, nbor_end;
    int i, numj;
    nbor_info_e_ss(dev_ij,stride,t_per_atom,ii,offset,i,numj,
                   n_stride,nbor_end,nbor);

    numtyp4 ix; fetch4(ix,i,pos_tex);
    int itype=ix.w;

    numtyp factor_lj;
    for ( ; nbor<nbor_end; nbor+=n_stride) {

      int j=dev_ij[nbor];
      factor_lj = sp_lj[sbmask(j)];
      j &= NEIGHMASK;

      numtyp4 jx; fetch4(jx,j,pos_tex);
      int jtype=jx.w;

      // Compute r12
      numtyp delx = ix.x-jx.x;
      numtyp dely = ix.y-jx.y;
      numtyp delz = ix.z-jx.z;
      numtyp r2inv = delx*delx+dely*dely+delz*delz;

      int ii=itype*lj_types+jtype;
      if (r2inv<lj1[ii].z && lj1[ii].w==SPHERE_SPHERE) {
        r2inv=ucl_recip(r2inv);
        numtyp r6inv = r2inv*r2inv*r2inv;
        numtyp force = r2inv*r6inv*(lj1[ii].x*r6inv-lj1[ii].y);
        force*=factor_lj;

        f.x+=delx*force;
        f.y+=dely*force;
        f.z+=delz*force;

        if (EVFLAG && eflag) {
          numtyp e=r6inv*(lj3[ii].x*r6inv-lj3[ii].y);
          energy+=factor_lj*(e-lj3[ii].z);
        }
        if (EVFLAG && vflag) {
          virial[0] += delx*delx*force;
          virial[1] += dely*dely*force;
          virial[2] += delz*delz*force;
          virial[3] += delx*dely*force;
          virial[4] += delx*delz*force;
          virial[5] += dely*delz*force;
        }
      }
    } // for nbor
  } // if ii
  acc_answers(f,energy,virial,ii,inum,tid,t_per_atom,offset,eflag,vflag,
              ans,engv);
}

__kernel void k_resquared_lj_fast(const __global numtyp4 *restrict x_,
                                  const __global numtyp4 *restrict lj1_in,
                                  const __global numtyp4 *restrict lj3_in,
                                  const __global numtyp *restrict gum,
                                  const int stride,
                                  const __global int *dev_ij,
                                  __global acctyp4 *restrict ans,
                                  __global acctyp *restrict engv,
                                  __global int *restrict err_flag,
                                  const int eflag, const int vflag,
                                  const int start, const int inum,
                                  const int t_per_atom) {
  int tid, ii, offset;
  atom_info(t_per_atom,ii,tid,offset);
  ii+=start;

  __local numtyp sp_lj[4];
  __local numtyp4 lj1[MAX_SHARED_TYPES*MAX_SHARED_TYPES];
  __local numtyp4 lj3[MAX_SHARED_TYPES*MAX_SHARED_TYPES];
  int n_stride;
  local_allocate_store_ellipse();

  if (tid<4)
    sp_lj[tid]=gum[tid];
  if (tid<MAX_SHARED_TYPES*MAX_SHARED_TYPES) {
    lj1[tid]=lj1_in[tid];
    if (EVFLAG && eflag)
      lj3[tid]=lj3_in[tid];
  }

  acctyp4 f;
  f.x=(acctyp)0; f.y=(acctyp)0; f.z=(acctyp)0;
  acctyp energy, virial[6];
  if (EVFLAG) {
    energy=(acctyp)0;
    for (int i=0; i<6; i++) virial[i]=(acctyp)0;
  }

  __syncthreads();

  if (ii<inum) {
    int nbor, nbor_end;
    int i, numj;
    nbor_info_e_ss(dev_ij,stride,t_per_atom,ii,offset,i,numj,
                   n_stride,nbor_end,nbor);

    numtyp4 ix; fetch4(ix,i,pos_tex);
    int iw=ix.w;
    int itype=fast_mul((int)MAX_SHARED_TYPES,iw);

    numtyp factor_lj;
    for ( ; nbor<nbor_end; nbor+=n_stride) {

      int j=dev_ij[nbor];
      factor_lj = sp_lj[sbmask(j)];
      j &= NEIGHMASK;

      numtyp4 jx; fetch4(jx,j,pos_tex);
      int mtype=itype+jx.w;

      // Compute r12
      numtyp delx = ix.x-jx.x;
      numtyp dely = ix.y-jx.y;
      numtyp delz = ix.z-jx.z;
      numtyp r2inv = delx*delx+dely*dely+delz*delz;

      if (r2inv<lj1[mtype].z && lj1[mtype].w==SPHERE_SPHERE) {
        r2inv=ucl_recip(r2inv);
        numtyp r6inv = r2inv*r2inv*r2inv;
        numtyp force = factor_lj*r2inv*r6inv*(lj1[mtype].x*r6inv-lj1[mtype].y);

        f.x+=delx*force;
        f.y+=dely*force;
        f.z+=delz*force;

        if (EVFLAG && eflag) {
          numtyp e=r6inv*(lj3[mtype].x*r6inv-lj3[mtype].y);
          energy+=factor_lj*(e-lj3[mtype].z);
        }
        if (EVFLAG && vflag) {
          virial[0] += delx*delx*force;
          virial[1] += dely*dely*force;
          virial[2] += delz*delz*force;
          virial[3] += delx*dely*force;
          virial[4] += delx*delz*force;
          virial[5] += dely*delz*force;
        }
      }

    } // for nbor
  } // if ii
  acc_answers(f,energy,virial,ii,inum,tid,t_per_atom,offset,eflag,vflag,
              ans,engv);
}