File: lal_ellipsoid_extra.h

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 (585 lines) | stat: -rw-r--r-- 26,114 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
// **************************************************************************
//                              ellipsoid_extra.h
//                             -------------------
//                           W. Michael Brown (ORNL)
//
//  Device code for Ellipsoid math routines
//
// __________________________________________________________________________
//    This file is part of the LAMMPS Accelerator Library (LAMMPS_AL)
// __________________________________________________________________________
//
//    begin                :
//    email                : brownw@ornl.gov
// ***************************************************************************/

#ifndef LAL_ELLIPSOID_EXTRA_H
#define LAL_ELLIPSOID_EXTRA_H

enum{SPHERE_SPHERE,SPHERE_ELLIPSE,ELLIPSE_SPHERE,ELLIPSE_ELLIPSE};

#if defined(NV_KERNEL) || defined(USE_HIP)
#include "lal_aux_fun1.h"
#ifndef _DOUBLE_DOUBLE
_texture( pos_tex, float4);
_texture( quat_tex,float4);
#else
_texture_2d( pos_tex,int4);
_texture_2d( quat_tex,int4);
#endif
#else
#define pos_tex x_
#define quat_tex qif
#endif

#define nbor_info_e_ss(nbor_mem, nbor_stride, t_per_atom, ii, offset,        \
                       i, numj, stride, nbor_end, nbor_begin)                \
  i=nbor_mem[ii];                                                            \
  nbor_begin=ii+nbor_stride;                                                 \
  numj=nbor_mem[nbor_begin];                                                 \
  nbor_begin+=nbor_stride;                                                   \
  nbor_end=nbor_begin+fast_mul(nbor_stride,numj);                            \
  nbor_begin+=fast_mul(offset,nbor_stride);                                  \
  stride=fast_mul(t_per_atom,nbor_stride);

#if (SHUFFLE_AVAIL == 0)

#define store_answers_t(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;                                                       \
      }                                                                     \
    }                                                                       \
    ans[ii]=f;                                                              \
    ans[ii+astride]=tor;                                                    \
  }

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

#else

#define store_answers_t(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);                             \
      if (EVFLAG) 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;                                                       \
      }                                                                     \
    }                                                                       \
    ans[ii]=f;                                                              \
    ans[ii+astride]=tor;                                                    \
  }

#define acc_answers(f, energy, virial, ii, inum, tid, t_per_atom, offset,   \
                    eflag, vflag, ans, engv)                                \
  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);                                 \
      if (EVFLAG) 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) {                                               \
    engv+=ii;                                                               \
    if (EVFLAG && eflag) {                                                  \
      *engv+=energy*(acctyp)0.5;                                            \
      engv+=inum;                                                           \
    }                                                                       \
    if (EVFLAG && vflag) {                                                  \
      for (int i=0; i<6; i++) {                                             \
        *engv+=virial[i]*(acctyp)0.5;                                       \
        engv+=inum;                                                         \
      }                                                                     \
    }                                                                       \
    acctyp4 old=ans[ii];                                                    \
    old.x+=f.x;                                                             \
    old.y+=f.y;                                                             \
    old.z+=f.z;                                                             \
    ans[ii]=old;                                                            \
  }

#endif

/* ----------------------------------------------------------------------
   dot product of 2 vectors
------------------------------------------------------------------------- */

ucl_inline numtyp gpu_dot3(const numtyp *v1, const numtyp *v2)
{
  return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
};

/* ----------------------------------------------------------------------
   cross product of 2 vectors
------------------------------------------------------------------------- */

ucl_inline void gpu_cross3(const numtyp *v1, const numtyp *v2, numtyp *ans)
{
  ans[0] = v1[1]*v2[2]-v1[2]*v2[1];
  ans[1] = v1[2]*v2[0]-v1[0]*v2[2];
  ans[2] = v1[0]*v2[1]-v1[1]*v2[0];
};

/* ----------------------------------------------------------------------
   determinant of a matrix
------------------------------------------------------------------------- */

ucl_inline numtyp gpu_det3(const numtyp m[9])
{
  numtyp ans = m[0]*m[4]*m[8] - m[0]*m[5]*m[7] -
    m[3]*m[1]*m[8] + m[3]*m[2]*m[7] +
    m[6]*m[1]*m[5] - m[6]*m[2]*m[4];
  return ans;
};

/* ----------------------------------------------------------------------
   diagonal matrix times a full matrix
------------------------------------------------------------------------- */

ucl_inline void gpu_diag_times3(const numtyp4 shape, const numtyp m[9],
                              numtyp ans[9])
{
  ans[0] = shape.x*m[0];
  ans[1] = shape.x*m[1];
  ans[2] = shape.x*m[2];
  ans[3] = shape.y*m[3];
  ans[4] = shape.y*m[4];
  ans[5] = shape.y*m[5];
  ans[6] = shape.z*m[6];
  ans[7] = shape.z*m[7];
  ans[8] = shape.z*m[8];
};

/* ----------------------------------------------------------------------
   add two matrices
------------------------------------------------------------------------- */

ucl_inline void gpu_plus3(const numtyp m[9], const numtyp m2[9], numtyp ans[9])
{
  ans[0] = m[0]+m2[0];
  ans[1] = m[1]+m2[1];
  ans[2] = m[2]+m2[2];
  ans[3] = m[3]+m2[3];
  ans[4] = m[4]+m2[4];
  ans[5] = m[5]+m2[5];
  ans[6] = m[6]+m2[6];
  ans[7] = m[7]+m2[7];
  ans[8] = m[8]+m2[8];
};

/* ----------------------------------------------------------------------
   multiply the transpose of mat1 times mat2
------------------------------------------------------------------------- */

ucl_inline void gpu_transpose_times3(const numtyp m[9], const numtyp m2[9],
                                   numtyp ans[9])
{
  ans[0] = m[0]*m2[0]+m[3]*m2[3]+m[6]*m2[6];
  ans[1] = m[0]*m2[1]+m[3]*m2[4]+m[6]*m2[7];
  ans[2] = m[0]*m2[2]+m[3]*m2[5]+m[6]*m2[8];
  ans[3] = m[1]*m2[0]+m[4]*m2[3]+m[7]*m2[6];
  ans[4] = m[1]*m2[1]+m[4]*m2[4]+m[7]*m2[7];
  ans[5] = m[1]*m2[2]+m[4]*m2[5]+m[7]*m2[8];
  ans[6] = m[2]*m2[0]+m[5]*m2[3]+m[8]*m2[6];
  ans[7] = m[2]*m2[1]+m[5]*m2[4]+m[8]*m2[7];
  ans[8] = m[2]*m2[2]+m[5]*m2[5]+m[8]*m2[8];
};

/* ----------------------------------------------------------------------
   row vector times matrix
------------------------------------------------------------------------- */

ucl_inline void gpu_row_times3(const numtyp *v, const numtyp m[9], numtyp *ans)
{
  ans[0] = m[0]*v[0]+v[1]*m[3]+v[2]*m[6];
  ans[1] = v[0]*m[1]+m[4]*v[1]+v[2]*m[7];
  ans[2] = v[0]*m[2]+v[1]*m[5]+m[8]*v[2];
};

/* ----------------------------------------------------------------------
   solve Ax = b or M ans = v
   use gaussian elimination & partial pivoting on matrix
   error_flag set to 2 if bad matrix inversion attempted
------------------------------------------------------------------------- */

ucl_inline void gpu_mldivide3(const numtyp m[9], const numtyp *v, numtyp *ans,
                            __global int *error_flag)
{
  // create augmented matrix for pivoting

  numtyp aug[12], t;

  aug[3] = v[0];
  aug[0] = m[0];
  aug[1] = m[1];
  aug[2] = m[2];
  aug[7] = v[1];
  aug[4] = m[3];
  aug[5] = m[4];
  aug[6] = m[5];
  aug[11] = v[2];
  aug[8] = m[6];
  aug[9] = m[7];
  aug[10] = m[8];

  if (ucl_abs(aug[4]) > ucl_abs(aug[0])) {
    numtyp swapt;
    swapt=aug[0]; aug[0]=aug[4]; aug[4]=swapt;
    swapt=aug[1]; aug[1]=aug[5]; aug[5]=swapt;
    swapt=aug[2]; aug[2]=aug[6]; aug[6]=swapt;
    swapt=aug[3]; aug[3]=aug[7]; aug[7]=swapt;
  }
  if (ucl_abs(aug[8]) > ucl_abs(aug[0])) {
    numtyp swapt;
    swapt=aug[0]; aug[0]=aug[8]; aug[8]=swapt;
    swapt=aug[1]; aug[1]=aug[9]; aug[9]=swapt;
    swapt=aug[2]; aug[2]=aug[10]; aug[10]=swapt;
    swapt=aug[3]; aug[3]=aug[11]; aug[11]=swapt;
  }

  if (aug[0] != (numtyp)0.0) {
    if (0!=0) {
      numtyp swapt;
      swapt=aug[0]; aug[0]=aug[0]; aug[0]=swapt;
      swapt=aug[1]; aug[1]=aug[1]; aug[1]=swapt;
      swapt=aug[2]; aug[2]=aug[2]; aug[2]=swapt;
      swapt=aug[3]; aug[3]=aug[3]; aug[3]=swapt;
    }
  } else if (aug[4] != (numtyp)0.0) {
    if (1!=0) {
      numtyp swapt;
      swapt=aug[0]; aug[0]=aug[4]; aug[4]=swapt;
      swapt=aug[1]; aug[1]=aug[5]; aug[5]=swapt;
      swapt=aug[2]; aug[2]=aug[6]; aug[6]=swapt;
      swapt=aug[3]; aug[3]=aug[7]; aug[7]=swapt;
    }
  } else if (aug[8] != (numtyp)0.0) {
    if (2!=0) {
      numtyp swapt;
      swapt=aug[0]; aug[0]=aug[8]; aug[8]=swapt;
      swapt=aug[1]; aug[1]=aug[9]; aug[9]=swapt;
      swapt=aug[2]; aug[2]=aug[10]; aug[10]=swapt;
      swapt=aug[3]; aug[3]=aug[11]; aug[11]=swapt;
    }
  } else
    *error_flag=2;

  t = aug[4]/aug[0];
  aug[5]-=t*aug[1];
  aug[6]-=t*aug[2];
  aug[7]-=t*aug[3];
  t = aug[8]/aug[0];
  aug[9]-=t*aug[1];
  aug[10]-=t*aug[2];
  aug[11]-=t*aug[3];

  if (ucl_abs(aug[9]) > ucl_abs(aug[5])) {
    numtyp swapt;
    swapt=aug[4]; aug[4]=aug[8]; aug[8]=swapt;
    swapt=aug[5]; aug[5]=aug[9]; aug[9]=swapt;
    swapt=aug[6]; aug[6]=aug[10]; aug[10]=swapt;
    swapt=aug[7]; aug[7]=aug[11]; aug[11]=swapt;
  }

  if (aug[5] != (numtyp)0.0) {
    if (1!=1) {
      numtyp swapt;
      swapt=aug[4]; aug[4]=aug[4]; aug[4]=swapt;
      swapt=aug[5]; aug[5]=aug[5]; aug[5]=swapt;
      swapt=aug[6]; aug[6]=aug[6]; aug[6]=swapt;
      swapt=aug[7]; aug[7]=aug[7]; aug[7]=swapt;
    }
  } else if (aug[9] != (numtyp)0.0) {
    if (2!=1) {
      numtyp swapt;
      swapt=aug[4]; aug[4]=aug[8]; aug[8]=swapt;
      swapt=aug[5]; aug[5]=aug[9]; aug[9]=swapt;
      swapt=aug[6]; aug[6]=aug[10]; aug[10]=swapt;
      swapt=aug[7]; aug[7]=aug[11]; aug[11]=swapt;
    }
  }

  t = aug[9]/aug[5];
  aug[10]-=t*aug[6];
  aug[11]-=t*aug[7];

  if (aug[10] == (numtyp)0.0)
    *error_flag=2;

  ans[2] = aug[11]/aug[10];
  t = (numtyp)0.0;
  t += aug[6]*ans[2];
  ans[1] = (aug[7]-t) / aug[5];
  t = (numtyp)0.0;
  t += aug[1]*ans[1];
  t += aug[2]*ans[2];
  ans[0] = (aug[3]-t) / aug[0];
};

/* ----------------------------------------------------------------------
   compute rotation matrix from quaternion conjugate
   quat = [w i j k]
------------------------------------------------------------------------- */

ucl_inline void gpu_quat_to_mat_trans(__global const numtyp4 *qif, const int qi,
                                    numtyp mat[9])
{
  numtyp4 q; fetch4(q,qi,quat_tex);

  numtyp w2 = q.x*q.x;
  numtyp i2 = q.y*q.y;
  numtyp j2 = q.z*q.z;
  numtyp k2 = q.w*q.w;
  numtyp twoij = (numtyp)2.0*q.y*q.z;
  numtyp twoik = (numtyp)2.0*q.y*q.w;
  numtyp twojk = (numtyp)2.0*q.z*q.w;
  numtyp twoiw = (numtyp)2.0*q.y*q.x;
  numtyp twojw = (numtyp)2.0*q.z*q.x;
  numtyp twokw = (numtyp)2.0*q.w*q.x;

  mat[0] = w2+i2-j2-k2;
  mat[3] = twoij-twokw;
  mat[6] = twojw+twoik;

  mat[1] = twoij+twokw;
  mat[4] = w2-i2+j2-k2;
  mat[7] = twojk-twoiw;

  mat[2] = twoik-twojw;
  mat[5] = twojk+twoiw;
  mat[8] = w2-i2-j2+k2;
};

/* ----------------------------------------------------------------------
   transposed matrix times diagonal matrix
------------------------------------------------------------------------- */

ucl_inline void gpu_transpose_times_diag3(const numtyp m[9],
                                        const numtyp4 d, numtyp ans[9])
{
  ans[0] = m[0]*d.x;
  ans[1] = m[3]*d.y;
  ans[2] = m[6]*d.z;
  ans[3] = m[1]*d.x;
  ans[4] = m[4]*d.y;
  ans[5] = m[7]*d.z;
  ans[6] = m[2]*d.x;
  ans[7] = m[5]*d.y;
  ans[8] = m[8]*d.z;
};

/* ----------------------------------------------------------------------
   multiply mat1 times mat2
------------------------------------------------------------------------- */

ucl_inline void gpu_times3(const numtyp m[9], const numtyp m2[9],
                         numtyp ans[9])
{
  ans[0] = m[0]*m2[0] + m[1]*m2[3] + m[2]*m2[6];
  ans[1] = m[0]*m2[1] + m[1]*m2[4] + m[2]*m2[7];
  ans[2] = m[0]*m2[2] + m[1]*m2[5] + m[2]*m2[8];
  ans[3] = m[3]*m2[0] + m[4]*m2[3] + m[5]*m2[6];
  ans[4] = m[3]*m2[1] + m[4]*m2[4] + m[5]*m2[7];
  ans[5] = m[3]*m2[2] + m[4]*m2[5] + m[5]*m2[8];
  ans[6] = m[6]*m2[0] + m[7]*m2[3] + m[8]*m2[6];
  ans[7] = m[6]*m2[1] + m[7]*m2[4] + m[8]*m2[7];
  ans[8] = m[6]*m2[2] + m[7]*m2[5] + m[8]*m2[8];
};

/* ----------------------------------------------------------------------
   Apply principal rotation generator about x to rotation matrix m
------------------------------------------------------------------------- */

ucl_inline void gpu_rotation_generator_x(const numtyp m[9], numtyp ans[9])
{
  ans[0] = 0;
  ans[1] = -m[2];
  ans[2] = m[1];
  ans[3] = 0;
  ans[4] = -m[5];
  ans[5] = m[4];
  ans[6] = 0;
  ans[7] = -m[8];
  ans[8] = m[7];
};

/* ----------------------------------------------------------------------
   Apply principal rotation generator about y to rotation matrix m
------------------------------------------------------------------------- */

ucl_inline void gpu_rotation_generator_y(const numtyp m[9], numtyp ans[9])
{
  ans[0] = m[2];
  ans[1] = 0;
  ans[2] = -m[0];
  ans[3] = m[5];
  ans[4] = 0;
  ans[5] = -m[3];
  ans[6] = m[8];
  ans[7] = 0;
  ans[8] = -m[6];
};

/* ----------------------------------------------------------------------
   Apply principal rotation generator about z to rotation matrix m
------------------------------------------------------------------------- */

ucl_inline void gpu_rotation_generator_z(const numtyp m[9], numtyp ans[9])
{
  ans[0] = -m[1];
  ans[1] = m[0];
  ans[2] = 0;
  ans[3] = -m[4];
  ans[4] = m[3];
  ans[5] = 0;
  ans[6] = -m[7];
  ans[7] = m[6];
  ans[8] = 0;
};

/* ----------------------------------------------------------------------
   matrix times vector
------------------------------------------------------------------------- */

ucl_inline void gpu_times_column3(const numtyp m[9], const numtyp v[3],
                                numtyp ans[3])
{
  ans[0] = m[0]*v[0] + m[1]*v[1] + m[2]*v[2];
  ans[1] = m[3]*v[0] + m[4]*v[1] + m[5]*v[2];
  ans[2] = m[6]*v[0] + m[7]*v[1] + m[8]*v[2];
};

#endif