Source

exafmm / mr3 / mr3.c

Full commit
  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
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <string.h>

#include "matrixMul.h"

#define MD_LJ_R2MIN 0.25f
#define MD_LJ_R2MAX 64.0f

#define COULOMB_VDW_FACTOR
static double Coulomb_vdw_factor=1.0;

#define MR3_malloc_pointer(size,string) malloc(size)
#define MR3_free_pointer(p,string)      free(p)

static double Eps2=0.0;
int Deviceid=0;


void vg_exit(int ret)
{
  exit(ret);
}


void *m3_get_unit(void)
{
  return NULL;
}


void m3_set_softening(void *unit, double eps)
{
  Eps2=eps*eps;
}


void MR3_exit(int ret)
{
  exit(ret);
}


void MR3init(void)
{
  char *s;

  s=getenv("VG_DEVICEID");
  if(s!=NULL){
    sscanf(s,"%d",&Deviceid);
    printf("VG_DEVICEID is set %d\n",Deviceid);
  }
}


void mr3init_(void)
{
  MR3init();
}


void mr3init__(void)
{
  mr3init_();
}


void MR3free(void)
{
}


void mr3free_(void)
{
  MR3free();
}


void mr3free__(void)
{
  mr3free_();
}


#if 0
void MR3SetTable(char *filename, int tblno, int flag)
{
}
#endif


void MR3calccoulomb_ij(int ni, double xi[], double qi[], double force[],
		       int nj, double xj[], double qj[],
		       double rscale, 
		       int tblno, double xmax, int periodicflag)
{
  int i,j,natchangeflag=0;

  switch(tblno){
  case 0:
    gpucoulombforce_ij_(xi,ni,qj,xj,nj,rscale,tblno,xmax,
			periodicflag,natchangeflag,force,Eps2);
    if((periodicflag & 2)!=0){
      for(i=0;i<ni;i++) for(j=0;j<3;j++) force[i*3+j]*=qi[i];
    }
    break;
  default:
    fprintf(stderr,"** error : not supported tblno = %d **\n",tblno);
    MR3_exit(1);
    break;
  }
}


void mr3calccoulomb_ij_(int *ni, double xi[], double qi[], double force[],
			int *nj, double xj[], double qj[],
			double *rscale, 
			int *tblno, double *xmax, int *periodicflag)
{
  MR3calccoulomb_ij(*ni,xi,qi,force,
		    *nj,xj,qj,
		    *rscale, 
		    *tblno,*xmax,*periodicflag);
}


void mr3calccoulomb_ij__(int *ni, double xi[], double qi[], double force[],
			 int *nj, double xj[], double qj[],
			 double *rscale, 
			 int *tblno, double *xmax, int *periodicflag)
{
  mr3calccoulomb_ij_(ni,xi,qi,force,
		     nj,xj,qj,
		     rscale, 
		     tblno,xmax,periodicflag);
}


void MR3calccoulomb(double x[], int n, double q[], double rscale,
		    int tblno, double xmax, int periodicflag,
		    int natchangeflag, double force[])
{
  int i,j;

  switch(tblno){
  case 0:
    gpucoulombforce_(x,n,q,rscale,tblno,xmax,
		     periodicflag,natchangeflag,force,Eps2);
    if((periodicflag & 2)!=0){
      for(i=0;i<n;i++) for(j=0;j<3;j++) force[i*3+j]*=q[i];
    }
    break;
  case 1:
    gpucoulombpot_(x,n,q,rscale,tblno,xmax,
		   periodicflag,natchangeflag,force);
    if((periodicflag & 2)!=0){
      for(i=0;i<n;i++) force[i*3]*=q[i];
    }
    break;
  case 6:
    gpuewreforce_(x,n,q,rscale,tblno,xmax,
		  periodicflag,natchangeflag,force);
    break;
  case 7:
    gpuewrepot_(x,n,q,rscale,tblno,xmax,
		periodicflag,natchangeflag,force);
    break;
  default:
    fprintf(stderr,"** error : not supported tblno = %d **\n",tblno);
    MR3_exit(1);
    break;
  }
}


void mr3calccoulomb_(double x[], int *n, double q[], double *rscale,
		     int *tblno, double *xmax, int *periodicflag,
		     int *natchangeflag, double force[])
{
  MR3calccoulomb(x,*n,q,*rscale,*tblno,*xmax,*periodicflag,
		 *natchangeflag,force);
}


void mr3calccoulomb__(double x[], int *n, double q[], double *rscale,
		      int *tblno, double *xmax, int *periodicflag,
		      int *natchangeflag, double force[])
{
  mr3calccoulomb_(x,n,q,rscale,
		  tblno,xmax,periodicflag,
		  natchangeflag,force);
}


void MR3calcvdw_ij(int ni, double xi[], int atypei[], double force[],
		   int nj, double xj[], int atypej[],
		   int nat, double gscale[], double rscale[],
		   int tblno, double xmax, int periodicflag)
{
#if 0
  MR3calcvdw_ij_host(ni,xi,atypei,force,
		     nj,xj,atypej,
		     nat,gscale,rscale,
		     tblno,xmax,periodicflag);
#endif
}


void MR3calcvdw(double x[], int n, int atype[], int nat,
		double gscale[], double rscale[],
		int tblno, double xmax, int periodicflag,
		int natchangeflag, double force[])
{
  //  double r2mind=0.25,r2maxd=64.0;
  double r2mind=MD_LJ_R2MIN,r2maxd=MD_LJ_R2MAX;
  int i;

#if 0
  for(i=0;i<n;i++) x[i*3]=x[i*3+1]=x[i*3+2]=0.0;
  x[0]=x[5*3]=1.0;
  printf("tblno=%d nat=%d xmax=%e\n",tblno,nat,xmax);
  for(i=0;i<10;i++){
    printf("pos[%d]=%e %e %e\n",i,x[i*3],x[i*3+1],x[i*3+2]);
  }
#endif
  if((nat+1)*(nat+1)*2>THD){
    fprintf(stderr,"** too many atom types = %d **\n",nat);
    MR3_exit(1);
  }
  switch(tblno){
  case 2:
    gpuvdwforce_(x,n,atype,nat,gscale,rscale,tblno,xmax,
		 periodicflag,natchangeflag,force,r2mind,r2maxd);
    break;
  case 3:
#if 1
    gpuvdwpot_(x,n,atype,nat,gscale,rscale,tblno,xmax,
	       periodicflag,natchangeflag,force,r2mind,r2maxd);
#else
    printf("gpuvdwpot is canged\n");
    gpuvdwpot_(x,n,atype,1.0,3,xmax,periodicflag,natchangeflag,force);
#if 0
    {
      double *q;
      if((q=(double *)MR3_malloc_pointer(sizeof(double)*n,"MR3calcvdw"))==NULL){
	fprintf(stderr,"** error : can't malloc q **\n");
	MR3_exit(1);
      }
      for(i=0;i<n;i++) q[i]=0.0;
      gpuvdwpot_(x,n,q,1.0,3,xmax,periodicflag,natchangeflag,force);
      MR3_free_pointer(q,"MR3calcvdw");
    }
#endif
#endif
    break;
  default:
    fprintf(stderr,"** error : not supported tblno = %d **\n",tblno);
    MR3_exit(1);
    break;
  }
}


void mr3calcvdw_(double x[], int *n, int atype[], int *nat,
		 double gscale[], double rscale[],
		 int *tblno, double *xmax, int *periodicflag,
		 int *natchangeflag, double force[])
{
  int *atype2;
  int i;

  if((atype2=(int *)MR3_malloc_pointer(sizeof(int)*(*n),"atypei2 in mr3calcvdw_"))==NULL){
    fprintf(stderr,
	    "** error at malloc atype2 in mr3calcvdw_ **\n");
    MR3_exit(1);
  }
  for(i=0;i<*n;i++){
    if(atype[i]>0){
      atype2[i]=atype[i]-1;
    }
    else{
      printf("  warning : atype[%d]=%d should be positive\n",
	     i,atype[i]);
      atype2[i]=0;
    }
  }
  MR3calcvdw(x,*n,atype2,*nat,
	     gscale,rscale,
	     *tblno,*xmax,*periodicflag,
	     *natchangeflag,force);
  MR3_free_pointer(atype2,"atype2 in mr3calcvdw_");
}


void mr3calcvdw__(double x[], int *n, int atype[], int *nat,
		  double gscale[], double rscale[],
		  int *tblno, double *xmax, int *periodicflag,
		  int *natchangeflag, double force[])
{
  mr3calcvdw_(x,n,atype,nat,
	      gscale,rscale,
	      tblno,xmax,periodicflag,
	      natchangeflag,force);
}


void MR3calcewald(int *k, int knum_org, double *x, int n, double *chg,
		  double alpha, double epsilon, double cell[3][3],
		  double *force, double *tpot, double stress[3][3])
{
  double *gvec,cell_1[3],*xq,vol1,eps1,alpha4,kvtmp,r2;
  int knum,i,j;
  
  knum=(knum_org>0 ? knum_org:-knum_org);
  if((gvec=(double *)MR3_malloc_pointer(sizeof(double)*knum*4,"gvec in MR3calcewald"))==NULL){
    fprintf(stderr,"** can't malloc gvec in MR3calcewald **\n");
    MR3_exit(1);
  }
  if((xq=(double *)MR3_malloc_pointer(sizeof(double)*n*4,"xq in MR3calcewald"))==NULL){
    fprintf(stderr,"** can't malloc xq in MR3calcewald **\n");
    MR3_exit(1);
  }
  for(i=0;i<3;i++) cell_1[i]=1.0/cell[i][i];
  for(i=0;i<n;i++){
    for(j=0;j<3;j++) xq[i*4+j]=x[i*3+j];
    xq[i*4+3]=chg[i];
  }
  for(i=0,vol1=1.0;i<3;i++) vol1*=cell_1[i];
  eps1=1.0/epsilon;
  alpha4=1.0/(4.0*alpha*alpha);
  for(i=0;i<knum;i++){
    r2=0.0;
    for(j=0;j<3;j++){
      kvtmp=gvec[i*4+j]=k[i*3+j]*2.0*M_PI*cell_1[j];
      r2+=kvtmp*kvtmp;
    }
    gvec[i*4+3]=2.0*eps1*vol1*exp(-r2*alpha4)/r2;
  }
  if(knum_org>0){
    gpuewwvforce_(xq,n,gvec,knum_org,force,tpot);
  }
  else if(knum_org<0){
    gpuewwvpot_(xq,n,gvec,-knum_org,force,tpot);
    *tpot=0.0;
    for(i=0;i<n;i++) *tpot+=force[i*3];
    *tpot*=0.5;
  }
  MR3_free_pointer(gvec,"gvec in MR3calcewald");
  MR3_free_pointer(xq,"xq in MR3calcewald");
}


void mr3calcewald_(int *k, int *knum, double *x, int *n,
		   double *chg, double *alpha, double *epsilon,
		   double cell[3][3], double *force,
		   double *tpot, double stress[3][3])
{
    MR3calcewald(k,*knum,x,*n,chg,*alpha,*epsilon,cell,
		 force,tpot,stress);
}


void mr3calcewald__(int *k, int *knum, double *x, int *n,
		    double *chg, double *alpha, double *epsilon,
		    double cell[3][3], double *force,
		    double *tpot, double stress[3][3])
{
    mr3calcewald_(k,knum,x,n,chg,alpha,epsilon,cell,
		  force,tpot,stress);
}


void MR3calcnacl(void)
{
  fprintf(stderr,"** error : MR3calcnacl is not supported **\n");
}


void vtgrape_force(double xj[][3], // position of j-th particles
		   double mj[],    // mass of j-th particles
		   double xi[][3], // position of i-th particles
		   double eps2,    // softening parameter
		   double a[][3], // force of i-th particles
		   int ni,         // number of i-th particles
		   int nj)
{
  double size=10.0;
  static int ini=0;

  if(ini==0){
    MR3init();
    ini=1;
  }
  gpucoulombforce_ij_(xi,ni,mj,xj,nj,1.0,0,size,
		      0,0,a,eps2);
}
#include "mr3_host.c"


void vg_get_r1result(int n, float r[], float r1[])
{
  printf("** warning : this routine is not supported **\n");
}