PandA-2024.02
generate.c
Go to the documentation of this file.
1 #include <stdlib.h>
2 #include <stdio.h>
3 #include <string.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <fcntl.h>
7 #include <unistd.h>
8 #include <assert.h>
9 
10 #include "md.h"
11 
12 #define domainEdge 20.0
13 // LJ1=4e(s^12), LJ2=4e(s^6) -> s=1.049
14 #define van_der_Waals_thresh (1.049*1.049)
15 
16 static inline TYPE dist_sq(TYPE x1, TYPE y1, TYPE z1, TYPE x2, TYPE y2, TYPE z2) {
17  TYPE dx, dy, dz;
18  dx=x2-x1;
19  dy=y2-y1;
20  dz=z2-z1;
21  return dx*dx + dy*dy + dz*dz;
22 }
23 
24 typedef struct {
25  int index;
27 } neighbor_t;
28 
29 int neighbor_compar(const void *v_lhs, const void *v_rhs) {
30  neighbor_t lhs = *((neighbor_t *)v_lhs);
31  neighbor_t rhs = *((neighbor_t *)v_rhs);
32  return lhs.dist_sq==rhs.dist_sq ? 0 : ( lhs.dist_sq<rhs.dist_sq ? -1 : 1 );
33 }
34 
35 int main(int argc, char **argv)
36 {
37  struct bench_args_t data;
38  int i, j, reject, fd;
39  neighbor_t neighbor_list[nAtoms];
40  TYPE x, y, z;
41  const TYPE infinity = (domainEdge*domainEdge*3.)*1000;//(max length)^2 * 1000
42  struct prng_rand_t state;
43 
44  // Create random positions in the box [0,domainEdge]^3
45  prng_srand(1,&state);
46  i=0;
47  while( i<nAtoms ) {
48  // Generate a new point
49  x = domainEdge*(((TYPE)prng_rand(&state))/((TYPE)PRNG_RAND_MAX));
50  y = domainEdge*(((TYPE)prng_rand(&state))/((TYPE)PRNG_RAND_MAX));
51  z = domainEdge*(((TYPE)prng_rand(&state))/((TYPE)PRNG_RAND_MAX));
52  // Assure that it's not directly on top of another atom
53  reject = 0;
54  for( j=0; j<i; j++ ) {
55  if( dist_sq(x,y,z, data.position_x[j], data.position_y[j], data.position_z[j])<van_der_Waals_thresh ) {
56  reject=1;
57  break;
58  }
59  }
60  if(!reject) {
61  data.position_x[i] = x;
62  data.position_y[i] = y;
63  data.position_z[i] = z;
64  //printf("%lf %lf %lf\n", data.position_x[i], data.position_y[i], data.position_z[i] );
65  ++i;
66  }
67  }
68 
69  // Compute k-nearest neighbors
70  memset(data.NL, 0, nAtoms*maxNeighbors*sizeof(int32_t));
71  for( i=0; i<nAtoms; i++ ) {
72  for( j=0; j<nAtoms; j++ ) {
73  neighbor_list[j].index = j;
74  if( i==j )
75  neighbor_list[j].dist_sq = infinity;
76  else
77  neighbor_list[j].dist_sq = dist_sq(data.position_x[i], data.position_y[i], data.position_z[i], data.position_x[j], data.position_y[j], data.position_z[j]);
78  }
79  qsort(neighbor_list, nAtoms, sizeof(neighbor_t), neighbor_compar);
80  //printf("%d:", i);
81  for( j=0; j<maxNeighbors; j++ ) {
82  data.NL[i*maxNeighbors +j] = neighbor_list[j].index;
83  //printf(" {%d,%lf}", neighbor_list[j].index, neighbor_list[j].dist_sq);
84  }
85  //printf("\n\n");
86  }
87 
88  // Open and write
89  fd = open("input.data", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
90  assert( fd>0 && "Couldn't open input data file" );
91  data_to_input(fd, (void *)(&data));
92 
93  return 0;
94 }
#define domainEdge
Definition: generate.c:12
static uint64_t prng_rand(struct prng_rand_t *state)
Definition: support.h:85
int index
Definition: generate.c:25
TYPE dist_sq
Definition: generate.c:26
void qsort(void *base, size_t nel, size_t width, int(*cmp)(const void *, const void *))
void data_to_input(int fd, void *vdata)
Definition: local_support.c:34
int32_t NL[nAtoms *maxNeighbors]
Definition: md.h:38
TYPE position_z[nAtoms]
Definition: md.h:37
#define van_der_Waals_thresh
Definition: generate.c:14
#define maxNeighbors
Definition: md.h:16
TYPE position_x[nAtoms]
Definition: md.h:35
#define TYPE
Definition: backprop.h:21
static TYPE dist_sq(TYPE x1, TYPE y1, TYPE z1, TYPE x2, TYPE y2, TYPE z2)
Definition: generate.c:16
int neighbor_compar(const void *v_lhs, const void *v_rhs)
Definition: generate.c:29
TYPE position_y[nAtoms]
Definition: md.h:36
uint_fast16_t i
Definition: support.h:78
#define nAtoms
Definition: md.h:9
int main(int argc, char **argv)
Definition: generate.c:12
static void prng_srand(uint64_t seed, struct prng_rand_t *state)
Definition: support.h:106
x
Return the smallest n such that 2^n >= _x.
#define PRNG_RAND_MAX
Definition: support.h:82

Generated on Mon Feb 12 2024 13:02:49 for PandA-2024.02 by doxygen 1.8.13