-
Notifications
You must be signed in to change notification settings - Fork 0
/
df.c
133 lines (125 loc) · 3.48 KB
/
df.c
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
#include "stdio.h"
#include "stdlib.h"
#include "linux/limits.h"
#include "string.h"
#include "math.h"
#include "omp.h"
void usage()
{
printf("df: an utility to construct distribution function using dump files.\n");
printf("usage: df savedir savefile dumpdir dumpfile startcell stopcell nvsteps nelectrons ntpoints\n");
printf("savedir - the dir where result shall be saved\n");
printf("savefile - the file name template for the results\n");
printf("dumpdir - the dir where dump files are saved\n");
printf("dumpfile - the file name template of the dummp files\n");
printf("startcell, stopcell - the bounds of the region where the df shall be calculated\n");
printf("nvsteps - the total number of speed steps for calculation of the df\n");
printf("nelectrons - the total number of particles saved in a dump file\n");
printf("ntpoints - the total number of timeponts saved in the dump files\n");
}
unsigned int select_particles(float* from, float* to, unsigned int* ass,
unsigned int nstart, unsigned int nstop,
unsigned int nelectrons)
{
unsigned int i;
unsigned int k = 0;
#pragma omp parallel for
for(i = 0; i < nelectrons; ++i)
{
if((ass[i] >= nstart) && (ass[i] <= nstop))
{
#pragma omp critical
{
to[k]=from[i];
k++;
}
}
}
return k;
}
int comp(const void* m1, const void* m2)
{
if (*(float*)m1 > *(float*)m2)
return 1;
if (*(float*)m1 < *(float*)m2)
return -1;
return 0;
}
void gdf(float* vsel, unsigned int* f, float* vf, unsigned int nv, unsigned int nvsteps)
{
qsort(vsel, nv, sizeof(float), comp);
float vmin = vsel[0];
float vmax = vsel[nv-1];
float dv = (vmax - vmin)/nvsteps;
unsigned int i;
unsigned int k;
memset(f, 0, sizeof(unsigned int)*nvsteps);
float vt = 0;
for (i = 0; i < nvsteps; ++i)
{
vt = vmin + i*dv;
vf[i] = vt + 0.5*dv;
for (k = 0; k < nv; ++k)
{
if(fabs(vsel[k]-vt) < dv/2)
{
f[i]++;
}
}
}
}
int main(int argc, char** argv)
{
if (0 == strcmp(argv[1], "help"))
{
usage();
return 0;
}
char* savedir = argv[1];
char* savefile = argv[2];
char* dumpdir = argv[3];
char* dumpfile = argv[4];
unsigned int nstart = atoi(argv[5]);
unsigned int nstop = atoi(argv[6]);
unsigned int n = nstart - nstop + 1;
unsigned int nvsteps = atoi(argv[7]);
unsigned int nelectrons = atoi(argv[8]);
unsigned int ntpoints = atoi(argv[9]);
unsigned int i;
FILE* from;
FILE* to;
char fullsavename[PATH_MAX];
char fulldumpname[PATH_MAX];
float* v = (float*)malloc(sizeof(float)*nelectrons);
float* vf = (float*)malloc(sizeof(float)*nvsteps);
unsigned int* f = (unsigned int*)malloc(sizeof(unsigned int)*nvsteps);
unsigned int nvf;
float* vsel = (float*)malloc(sizeof(float)*nelectrons);
unsigned int* ass = (unsigned int*)malloc(sizeof(unsigned int)*nelectrons);
unsigned int j;
for (i = 0; i < ntpoints; ++i)
{
sprintf(fulldumpname, "%s/%s_%d_vperp.dat", dumpdir, dumpfile, i);
from = fopen(fulldumpname, "r");
fread(v, sizeof(float), nelectrons, from);
fclose(from);
sprintf(fulldumpname, "%s/%s_%d_association.dat", dumpdir, dumpfile, i);
from = fopen(fulldumpname, "r");
fread(ass, sizeof(unsigned int), nelectrons, from);
fclose(from);
nvf = select_particles(v, vsel, ass, nstart, nstop, nelectrons);
gdf(vsel, f, vf, nvf, nvsteps);
sprintf(fulldumpname, "%s/%s_%d.dat", savedir, savefile, i);
to = fopen(fulldumpname, "w");
for(j = 0; j < nvsteps; ++j)
{
fprintf(to, "%d\t%e\n", f[j], vf[j]);
}
fclose(to);
}
free(ass);
free(v);
free(vsel);
free(vf);
free(f);
}