-
Notifications
You must be signed in to change notification settings - Fork 0
/
hybrid_distr.cc
679 lines (560 loc) · 18.9 KB
/
hybrid_distr.cc
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
//==============================================================================
// Start of the file "hybrid_distr.cc"
//
// Version 3d.
//
// Written by Vladimir Florinski
//==============================================================================
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include "geo_memory.hh"
#include "hybrid_distr.hh"
using namespace std;
//==============================================================================
// Standard analytic PDF expressions
//==============================================================================
// Uniform distribution, Cartesian
inline double PDF_range_0(double param1, double param2, double val)
{
return (val > param1 && val <= param2 ? 1.0 : 0.0);
};
// Uniform distribution, cylindrical
inline double PDF_range_1(double param1, double param2, double val)
{
return (val > param1 && val <= param2 ? val : 0.0);
};
// Uniform distribution, spherical
inline double PDF_range_2(double param1, double param2, double val)
{
return (val > param1 && val <= param2 ? Sqr(val) : 0.0);
};
// Gaussian distribution, Cartesian
inline double PDF_Gauss_0(double param1, double param2, double val)
{
return exp(-Sqr((val - param1) / param2));
};
// Gaussian distribution, cylindrical
inline double PDF_Gauss_1(double param1, double param2, double val)
{
return val * exp(-Sqr((val - param1) / param2));
};
// Gaussian distribution, spherical
inline double PDF_Gauss_2(double param1, double param2, double val)
{
return Sqr(val) * exp(-Sqr((val - param1) / param2));
};
//==============================================================================
// Custom distributions
//==============================================================================
const double in_ratio = 0.5; // ion to neutral ratio
// Pickup ion distribution without scattering
inline double PDF_pui_noscat(double param1, double param2, double val)
{
double st, tanpsi;
st = sqrt(1.0 - Sqr(val));
tanpsi = param1 / sqrt(1.0 - Sqr(param1));
return exp(param2 * ((1.0 - in_ratio) / st - tanpsi / val)) / val / st;
};
//==============================================================================
// "Dice roll" functions
//==============================================================================
// Constant distribution
inline double Roll_singular(double param1, double param2)
{
return param1;
};
// Uniform distribution, Cartesian
inline double Roll_range_0(double param1, double param2)
{
return param1 + drand48() * (param2 - param1);
};
// Uniform distribution, cylindrical
inline double Roll_range_1(double param1, double param2)
{
return sqrt(Sqr(param1) + drand48() * (Sqr(param2) - Sqr(param1)));
};
// Uniform distribution, spherical
inline double Roll_range_2(double param1, double param2)
{
return pow(Cube(param1) + drand48() * (Cube(param2) - Cube(param1)), 1.0 / 3.0);
};
// Gaussian distribution, Cartesian
inline double Roll_Gauss_0(double param1, double param2)
{
return param1 + param2 * sqrt(-log(drand48())) * cos(2.0 * M_PI * drand48());
};
// Gaussian distribution, cylindrical
inline double Roll_Gauss_1(double param1, double param2)
{
return param1 + param2 * sqrt(-log(drand48()));
};
// Gaussian distribution truncated to [-1,1]
inline double Roll_Gauss_R(double param1, double param2)
{
double gauss_trial = -2.0;
while(gauss_trial < -1.0 || gauss_trial > 1.0) {
gauss_trial = param1 + param2 * sqrt(-log(drand48())) * cos(2.0 * M_PI * drand48());
};
return gauss_trial;
};
//==============================================================================
// The distribution_t class private methods
//==============================================================================
//------------------------------------------------------------------------------
// Binary search of the forward CDF table
//------------------------------------------------------------------------------
// Input | table | which table to use - first or second
// Input | val | random number giving the value of the CDF
//------------------------------------------------------------------------------
double distribution_t::SearchForwardCDF(int table, double val)
{
int i1, i2, i3;
double var_min, var_max, dvar, *cdf_table;
// determine which table we need
if(table == 1) {
var_min = limits[0];
var_max = limits[1];
cdf_table = cdf_table_f1;
}
else if(table == 2) {
var_min = limits[2];
var_max = limits[3];
cdf_table = cdf_table_f2;
}
else {
cerr << "# distribution_t: Invalid table request\n";
return 0.0;
};
// Bisection search (slow)
dvar = (var_max - var_min) / table_size;
i1 = 0;
i2 = table_size;
while(i2 - i1 > 1) {
i3 = (i2 + i1) / 2;
if(val < cdf_table[i3]) i2 = i3;
else i1 = i3;
};
return var_min + (i1 * dvar * (cdf_table[i2] - val)
+ i2 * dvar * (val - cdf_table[i1]))
/ (cdf_table[i2] - cdf_table[i1]);
};
//------------------------------------------------------------------------------
// Lookup from the inverse CDF table
//------------------------------------------------------------------------------
// Input | table | which table to use - first or second
// Input | val | random number giving the value of the CDF
//------------------------------------------------------------------------------
double distribution_t::SearchInverseCDF(int table, double val)
{
int i;
double dcdf, *cdf_table;
// determine which table we need
if(table == 1) cdf_table = cdf_table_i1;
else if(table == 2) cdf_table = cdf_table_i2;
else {
cerr << "# distribution_t: Invalid table request\n";
return 0.0;
};
// Linear interpolation (fast)
dcdf = 1.0 / table_size;
i = val / dcdf;
return cdf_table[i] * (i + 1.0 - val / dcdf)
+ cdf_table[i + 1] * (val / dcdf - i);
};
//------------------------------------------------------------------------------
// Generates the forward CDF lookup tables
//------------------------------------------------------------------------------
// Input | table | which table to generate - first or second
//------------------------------------------------------------------------------
void distribution_t::GenerateForwardTables(int table)
{
int i;
double var, dvar, var_min, var_max, param1, param2, pdf_l, pdf_r;
double *cdf_table, *norm;
double (*PDF_func)(double param1, double param2, double val);
// determine which table we need
if(table == 1) {
var_min = limits[0];
var_max = limits[1];
param1 = params[0];
param2 = params[1];
norm = &norm_f1;
PDF_func = PDF_func1;
}
else if(table == 2) {
var_min = limits[2];
var_max = limits[3];
param1 = params[2];
param2 = params[3];
norm = &norm_f2;
PDF_func = PDF_func2;
}
else {
cerr << "# distribution_t: Invalid table request\n";
return;
};
// Singular PDFs can not be generated
if(!PDF_func) return;
// Allocate memory for tables
if(table == 1) {
if(!cdf_table_f1) cdf_table_f1 = new double[table_size + 1];
cdf_table = cdf_table_f1;
}
else {
if(!cdf_table_f2) cdf_table_f2 = new double[table_size + 1];
cdf_table = cdf_table_f2;
};
dvar = (var_max - var_min) / table_size;
cdf_table[0] = 0.0;
pdf_r = PDF_func(param1, param2, var_min);
// Integrate the PDF
for(i = 1; i <= table_size; i++) {
var = var_min + i * dvar;
pdf_l = pdf_r;
pdf_r = PDF_func(param1, param2, var);
cdf_table[i] = cdf_table[i - 1] + 0.5 * dvar * (pdf_l + pdf_r);
};
// Normalize to unity
*norm = cdf_table[table_size];
for(i = 1; i <= table_size; i++) cdf_table[i] /= *norm;
};
//------------------------------------------------------------------------------
// Generates the inverse CDF lookup tables
//------------------------------------------------------------------------------
// Input | table | which table to generate - first or second
//------------------------------------------------------------------------------
void distribution_t::GenerateInverseTables(int table)
{
int i, n1;
double var, dvar, var_min, var_max, param1, param2, pdf_l, pdf_r, cdf, dcdf;
double *cdf_table, *norm;
double (*PDF_func)(double param1, double param2, double val);
// determine which table we need
if(table == 1) {
var_min = limits[0];
var_max = limits[1];
param1 = params[0];
param2 = params[1];
norm = &norm_i1;
PDF_func = PDF_func1;
}
else if(table == 2) {
var_min = limits[2];
var_max = limits[3];
param1 = params[2];
param2 = params[3];
norm = &norm_i2;
PDF_func = PDF_func2;
}
else {
cerr << "# distribution_t: Invalid table request\n";
return;
};
// Singular PDFs can not be generated
if(!PDF_func) return;
// Allocate memory for tables
if(table == 1) {
if(!cdf_table_i1) cdf_table_i1 = new double[table_size + 1];
cdf_table = cdf_table_i1;
}
else {
if(!cdf_table_i2) cdf_table_i2 = new double[table_size + 1];
cdf_table = cdf_table_i2;
};
// Compute the normalization
dvar = (var_max - var_min) / table_size;
pdf_r = PDF_func(param1, param2, var_min);
*norm = 0.0;
for(i = 1; i <= table_size; i++) {
var = var_min + i * dvar;
pdf_l = pdf_r;
pdf_r = PDF_func(param1, param2, var);
*norm += 0.5 * dvar * (pdf_l + pdf_r);
};
// Integrate with a smaller step, recording values at regular intervals on the
// CDF axis
n1 = table_size * icdf_steps;
dcdf = 1.0 / table_size;
pdf_r = PDF_func(param1, param2, var_min) / (*norm);
dvar = (var_max - var_min) / n1;
cdf = cdf_table[0] = 0.0;
var = var_min;
for(i = 1; i < table_size; i++) {
while(cdf < i * dcdf) {
var += dvar;
pdf_l = pdf_r;
pdf_r = PDF_func(param1, param2, var) / (*norm);
cdf += 0.5 * dvar * (pdf_l + pdf_r);
};
cdf_table[i] = var;
};
cdf_table[table_size] = var_max;
};
//------------------------------------------------------------------------------
// Initialize a cylindrical distribution
//------------------------------------------------------------------------------
// Input | p_inp[4] | parameters of the distribution
//------------------------------------------------------------------------------
void distribution_t::InitCyl(double *p_inp)
{
memcpy(params, p_inp, 4 * sizeof(double));
// Singular v_perp - no PDF
if(type1 == PDF_SINGULAR ) {
limits[0] = limits[1] = params[0];
PDF_func1 = NULL;
Roll_func1 = Roll_singular;
}
// Uniform v_perp - both PDF and roll function exist
else if(type1 == PDF_RANGE) {
limits[0] = params[0];
limits[1] = params[1];
PDF_func1 = PDF_range_1;
Roll_func1 = Roll_range_1;
}
// Gauss v_perp - no known roll function
else if(type1 == PDF_GAUSS) {
limits[0] = 0.0;
limits[1] = params[0] + gauss_width * params[1];
PDF_func1 = PDF_Gauss_1;
Roll_func1 = NULL;
}
// Centered Gaussian v_perp - both PDF and roll function exist
else if(type1 == PDF_CGAUSS) {
limits[0] = params[0] = 0.0;
limits[1] = gauss_width * params[1];
PDF_func1 = PDF_Gauss_1;
Roll_func1 = Roll_Gauss_1;
}
// Custom v_perp - not implemented
else if(type1 == PDF_CUSTOM1) {
limits[0] = 0.0;
limits[1] = 0.0;
PDF_func1 = NULL;
Roll_func1 = NULL;
}
// Either type zero or invalid input (beam default)
else {
type1 = PDF_ZERO;
params[0] = params[1] = 0.0;
limits[0] = limits[1] = 0.0;
PDF_func1 = NULL;
Roll_func1 = Roll_singular;
};
//------------------------------------------------------------------------------
// Singular v_para - no PDF
if(type2 == PDF_SINGULAR) {
limits[2] = limits[3] = params[2];
PDF_func2 = NULL;
Roll_func2 = Roll_singular;
}
// Uniform v_para - both PDF and roll function exist
else if(type2 == PDF_RANGE) {
limits[2] = params[2];
limits[3] = params[3];
PDF_func2 = PDF_range_0;
Roll_func2 = Roll_range_0;
}
// Gauss v_para - both PDF and roll function exist
else if(type2 == PDF_GAUSS) {
limits[2] = params[2] - gauss_width * params[3];
limits[3] = params[2] + gauss_width * params[3];
PDF_func2 = PDF_Gauss_0;
Roll_func2 = Roll_Gauss_0;
}
// Custom v_para - not implemented
else if(type2 == PDF_CUSTOM1) {
limits[2] = 0.0;
limits[3] = 0.0;
PDF_func2 = NULL;
Roll_func2 = NULL;
}
// Defaults to nondrifting annulus
else {
type2 = PDF_ZERO;
params[2] = params[3] = 0.0;
limits[2] = limits[3] = 0.0;
PDF_func2 = NULL;
Roll_func2 = Roll_singular;
};
};
//------------------------------------------------------------------------------
// Initialize a spherical distribution
//------------------------------------------------------------------------------
// Input | p_inp[4] | parameters of the distribution
//------------------------------------------------------------------------------
void distribution_t::InitSph(double *p_inp)
{
memcpy(params, p_inp, 4 * sizeof(double));
// Singular v - no PDF
if(type1 == PDF_SINGULAR) {
limits[0] = limits[1] = params[0];
PDF_func1 = NULL;
Roll_func1 = Roll_singular;
}
// Uniform v - both PDF and roll function exist
else if(type1 == PDF_RANGE) {
limits[0] = params[0];
limits[1] = params[1];
PDF_func1 = PDF_range_2;
Roll_func1 = Roll_range_2;
}
// Gauss v - roll function unavailable
else if(type1 == PDF_GAUSS) {
limits[0] = 0.0;
limits[1] = params[0] + gauss_width * params[1];
PDF_func1 = PDF_Gauss_2;
Roll_func1 = NULL;
}
// Defaults to stationary
else {
type1 = PDF_ZERO;
params[0] = params[1] = 0.0;
limits[0] = limits[1] = 0.0;
PDF_func1 = NULL;
Roll_func1 = Roll_singular;
};
//------------------------------------------------------------------------------
// Singular mu - no PDF
if(type2 == PDF_SINGULAR) {
limits[2] = limits[3] = params[0];
PDF_func2 = NULL;
Roll_func2 = Roll_singular;
}
// Uniform mu - both PDF and roll function exist
else if(type2 == PDF_RANGE) {
limits[2] = params[0];
limits[3] = params[1];
PDF_func2 = PDF_range_0;
Roll_func2 = Roll_range_0;
}
// Gauss mu - use truncated Gaussian roll
else if(type2 == PDF_GAUSS) {
limits[2] = -1.0;
limits[3] = 1.0;
PDF_func2 = PDF_Gauss_0;
Roll_func2 = Roll_Gauss_R;
}
// Custom mu (1)
else if(type2 == PDF_CUSTOM1) {
limits[2] = 1.0E-7;
limits[3] = params[2];
PDF_func2 = PDF_pui_noscat;
Roll_func2 = NULL;
}
// Defaults to nondrifting annulus
else {
type2 = PDF_ZERO;
params[0] = params[1] = 0.0;
limits[2] = limits[3] = 0.0;
PDF_func2 = NULL;
Roll_func2 = Roll_singular;
};
};
//==============================================================================
// The distribution_t class public methods
//==============================================================================
//------------------------------------------------------------------------------
// Default constructor (cold stationary distribution)
//------------------------------------------------------------------------------
distribution_t::distribution_t()
{
geom = false;
type1 = 0;
type2 = 1;
memset(params, 0, n_par * sizeof(double));
PDF_func1 = NULL;
PDF_func2 = NULL;
Roll_func1 = Roll_singular;
Roll_func2 = Roll_singular;
cdf_table_f1 = cdf_table_f2 = NULL;
cdf_table_i1 = cdf_table_i2 = NULL;
};
//------------------------------------------------------------------------------
// Allocate memory, assign the PDF functions and compute the CDF tables
//------------------------------------------------------------------------------
// Input | type | type of distribution
// Input | p_inp[4] | parameters of the distribution
//------------------------------------------------------------------------------
void distribution_t::Activate(int type, double *p_inp)
{
geom = type / 100;
type1 = (type - 100 * geom) / 10;
type2 = (type - 100 * geom - 10 * type1);
// Initialize the parameter set
if(geom == GEOM_CYL) InitCyl(p_inp);
else if(geom == GEOM_SPH) InitSph(p_inp);
else {
cerr << "# distribution_t: Invalid geometry\n";
return;
};
// Generate the CDF tables
GenerateForwardTables(1);
GenerateForwardTables(2);
GenerateInverseTables(1);
GenerateInverseTables(2);
};
//------------------------------------------------------------------------------
// Complete class constructor with initialization
//------------------------------------------------------------------------------
// Input | type | type of distribution
// Input | p_inp[4] | parameters of the distribution
//------------------------------------------------------------------------------
distribution_t::distribution_t(int type, double *p_inp)
{
Activate(type, p_inp);
};
//------------------------------------------------------------------------------
// Generates an instance of a three-dimensional velocity vector
//------------------------------------------------------------------------------
// Output | v[3] | velocity vector
//------------------------------------------------------------------------------
void distribution_t::RollOne(double *v)
{
double v1, v2, phi, rn1, rn2, st;
// stationary distribution as default
memset(v, 0, 3 * sizeof(double));
// Select the best method to generate the distribution. Roll function is
// preferred, followed by inverse CDF, and finally forward CDF.
rn1 = drand48();
if(!Roll_func1 && !PDF_func1) return;
else if(!Roll_func1 || (PDF_func1 && prefer_cdf)) {
if(use_forward) v1 = SearchForwardCDF(1, rn1);
else v1 = SearchInverseCDF(1, rn1);
}
else v1 = Roll_func1(params[0], params[1]);
rn2 = drand48();
if(!Roll_func2 && !PDF_func2) return;
else if(!Roll_func2 || (PDF_func2 && prefer_cdf)) {
if(use_forward) v2 = SearchForwardCDF(2, rn2);
else v2 = SearchInverseCDF(2, rn2);
}
else v2 = Roll_func2(params[2], params[3]);
// Convert to Cartesian frame
if(geom == GEOM_CYL) {
if(type1 != PDF_ZERO) {
phi = 2.0 * M_PI * drand48();
v[0] = v1 * cos(phi);
v[1] = v1 * sin(phi);
};
v[2] = v2;
}
else if(geom == GEOM_SPH && type1 != PDF_ZERO) {
phi = 2.0 * M_PI * drand48();
st = sqrt(1.0 - Sqr(v2));
v[0] = v1 * st * cos(phi);
v[1] = v1 * st * sin(phi);
v[2] = v1 * v2;
};
};
//------------------------------------------------------------------------------
// Class destructor
//------------------------------------------------------------------------------
distribution_t::~distribution_t()
{
if(cdf_table_f1) delete[] cdf_table_f1;
if(cdf_table_f2) delete[] cdf_table_f2;
if(cdf_table_i1) delete[] cdf_table_i1;
if(cdf_table_i2) delete[] cdf_table_i2;
};