-
Notifications
You must be signed in to change notification settings - Fork 0
/
GreensFunction2DRadAbs.cpp
1761 lines (1399 loc) · 60.5 KB
/
GreensFunction2DRadAbs.cpp
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
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Greens function class for 2d Green's Function for 2d annulus with radial and
// axial dependence. Inner boundary is radiative (rad) (reaction event), outer
// boundary is absorbing (abs) (escape event). Different "draw" functions
// provide a way to draw certain values from the Green's Function, e.g. an
// escape angle theta ("drawTheta" function).
//
// Based upon code from Riken Institute.
// Written by Laurens Bossen, Adapted by Martijn Wehrens. FOM Institute AMOLF.
//#define NDEBUG
//#define BOOST_DISABLE_ASSERTS
#include "compat.h"
#include <iostream>
#include <stdexcept>
#include <vector>
#include <sstream>
#include <boost/bind.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/format.hpp>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_math.h>
#include <gsl/gsl_sf_legendre.h>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_sf_lambert.h>
#include <gsl/gsl_integration.h>
#include "factorial.hpp"
#include "funcSum.hpp"
#include "findRoot.hpp"
#include "freeFunctions.hpp"
#include "CylindricalBesselGenerator.hpp"
#include "GreensFunction2DRadAbs.hpp"
namespace greens_functions
{
const Real GreensFunction2DRadAbs::TOLERANCE = 1e-8;
const Real GreensFunction2DRadAbs::MIN_T_FACTOR = 1e-8;
const Real GreensFunction2DRadAbs::L_TYPICAL = 1E-7;
const Real GreensFunction2DRadAbs::T_TYPICAL = 1E-5;
const Real GreensFunction2DRadAbs::EPSILON = 1E-12;
const Real GreensFunction2DRadAbs::SCAN_START = 0.001;
const Real GreensFunction2DRadAbs::FRACTION_SCAN_INTERVAL = .5;
const Real GreensFunction2DRadAbs::CONVERGENCE_ASSUMED = 25;
const Real GreensFunction2DRadAbs::INTERVAL_MARGIN = .33;
#ifndef WIN32_MSC
const unsigned int GreensFunction2DRadAbs::MAX_ORDER;
const unsigned int GreensFunction2DRadAbs::MAX_ALPHA_SEQ;
#endif
// This is the constructor
GreensFunction2DRadAbs::
GreensFunction2DRadAbs( const Real D,
const Real kf,
const Real r0,
const Real Sigma,
const Real a )
:
PairGreensFunction(D, kf, r0, Sigma),
h( kf / (D * 2.0 * M_PI * Sigma) ),
a( a ),
estimated_alpha_root_distance_(M_PI/(a-Sigma)) // observed convergence of
// distance roots f_alpha().
// ^: Here parent "constructors" are specified that are executed,
// also a constructor initialization list can be (and is) specified.
// These variables will be set before the contents of the constructor are
// called.
{
const Real sigma(this->getSigma());
// Check wether input makes sense, outer boundary a should be > inner
// boundary sigma.
if (a < sigma)
{
throw std::invalid_argument((boost::format("GreensFunction2DRadAbs: a >= sigma : a=%.16g, sigma=%.16g") % a % sigma).str());
}
// Clear AlphaTables
GreensFunction2DRadAbs::clearAlphaTable();
}
GreensFunction2DRadAbs::~GreensFunction2DRadAbs()
{
; // do nothing
}
//
// Alpha-related methods
//
// Resets the alpha-tables
void GreensFunction2DRadAbs::clearAlphaTable() const
{
const Real estimated_alpha_root_distance(getestimated_alpha_root_distance_());
// Clears all vectors in the alphaTable
std::for_each( this->alphaTable.begin(), this->alphaTable.end(),
boost::mem_fn( &RealVector::clear ) );
// Sets all values of the alpha_x_scan_table_ to zero.
std::fill( this->alpha_x_scan_table_.begin(),
this->alpha_x_scan_table_.end(),
SCAN_START*estimated_alpha_root_distance);
// DEBUG TODO; actual number here might
// be important for functioning of Bessel functions.
// Sets all values of the alpha_correctly_estimated_ table to zero.
std::fill( this->alpha_correctly_estimated_.begin(),
this->alpha_correctly_estimated_.end(),
0 );
}
// The method evaluates the equation for finding the alphas for given alpha. This
// is needed to find the alpha's at which the expression is zero -> alpha is the root.
Real
GreensFunction2DRadAbs::f_alpha0( const Real alpha ) const
{
const Real a( this->geta() );
const Real sigma( getSigma() );
const Real h( this->geth() );
const Real s_An( sigma * alpha );
const Real a_An( a * alpha );
// Needed? TODO
// const Real h_s( h * sigma);
const Real J0_s_An (gsl_sf_bessel_J0(s_An));
const Real J1_s_An (gsl_sf_bessel_J1(s_An));
const Real J0_a_An (gsl_sf_bessel_J0(a_An));
const Real Y0_s_An (gsl_sf_bessel_Y0(s_An));
const Real Y1_s_An (gsl_sf_bessel_Y1(s_An));
const Real Y0_a_An (gsl_sf_bessel_Y0(a_An));
// const double rho1 ( ( (h_s * J0_s_An) + (s_An * J1_s_An) ) * Y0_a_An );
// const double rho2 ( ( (h_s * Y0_s_An) + (s_An * Y1_s_An) ) * J0_a_An );
// return (rho1 - rho2);
// Sigma can be divided out, roots will remain same:
// (Note: actually double checked this).
const Real rho1 ( ( (h * J0_s_An) + (alpha * J1_s_An) ) * Y0_a_An );
const Real rho2 ( ( (h * Y0_s_An) + (alpha * Y1_s_An) ) * J0_a_An );
return rho1 - rho2;
}
// Wraps function of which we need to find roots alpha.
//
// Params contains pointer to gf object (params.gf), where f_alpha is the member
// function of which we have to find the roots. This function is thus a mere
// wrapper of f_alpha.
Real
GreensFunction2DRadAbs::f_alpha0_aux_F( const Real alpha,
const f_alpha0_aux_params* const params )
{
// create pointer to Green's Function object
const GreensFunction2DRadAbs* const gf( params->gf );
return gf->f_alpha0( alpha );
}
// f_alpha() Calculates the value of the mathematical function f_alpha(). The
// roots (y=0) of this function are constants in the Green's Functions.
Real GreensFunction2DRadAbs::f_alpha( const Real alpha,
const Integer n ) const
{
const Real a( this->geta() );
const Real sigma( getSigma() );
const Real h( this->geth() );
const Real s_An( sigma * alpha );
const Real a_An( a * alpha );
const Real realn( static_cast<Real>( n ) );
const Real h_sigma( h * sigma);
const Real Jn_s_An (gsl_sf_bessel_Jn(n,s_An));
const Real Jn1_s_An (gsl_sf_bessel_Jn(n+1,s_An));
const Real Jn_a_An (gsl_sf_bessel_Jn(n,a_An));
const Real Yn_s_An (gsl_sf_bessel_Yn(n,s_An));
const Real Yn1_s_An (gsl_sf_bessel_Yn(n+1,s_An));
const Real Yn_a_An (gsl_sf_bessel_Yn(n,a_An));
const Real rho1 ( ( (h_sigma * Jn_s_An) + (s_An * Jn1_s_An) - realn*Jn_s_An ) * Yn_a_An );
const Real rho2 ( ( (h_sigma * Yn_s_An) + (s_An * Yn1_s_An) - realn*Yn_s_An ) * Jn_a_An );
return (rho1 - rho2);
// Or..?
// const double rho1 ( ( ((h*sigma-realn) * Jn_s_An) + (s_An * Jn1_s_An) ) * Yn_a_An );
// const double rho2 ( ( ((h*sigma-realn) * Yn_s_An) + (s_An * Yn1_s_An) ) * Jn_a_An );
// return (rho1 - rho2);
}
// Simply a wrapper for f_alpha().
Real
GreensFunction2DRadAbs::f_alpha_aux_F( const Real alpha,
const f_alpha_aux_params* const params )
{
// Params contains pointer to gf object (params.gf), which has f_alpha() as
// a member.
const GreensFunction2DRadAbs* const gf( params->gf );
const Integer n( params->n );
return gf->f_alpha( alpha, n );
}
// calculates the constant part of the i-th term for the survival probability
Real
GreensFunction2DRadAbs::p_survival_i( const Real alpha) const
{
const Real a( geta() ); // get the needed parameters
const Real sigma( getSigma() );
const Real s_An (sigma*alpha);
const Real a_An (a*alpha);
const Real alpha_sq (alpha*alpha);
// calculate all the required Bessel functions
const Real J1_sAn (gsl_sf_bessel_J1(s_An));
const Real J0_aAn (gsl_sf_bessel_J0(a_An));
const Real Y0_aAn (gsl_sf_bessel_Y0(a_An));
const Real Y1_sAn (gsl_sf_bessel_Y1(s_An));
// calculate C0,n
const Real C_i_0 (calc_A_i_0(alpha));
// calculate the integral over Bn,0
const Real dB_n_0dr (J1_sAn*Y0_aAn - Y1_sAn*J0_aAn);
// this is only the part without alpha of dB0,n(sigma)/dr
const Real B_n_0_int (Real(2.0)/(M_PI*alpha_sq) - (sigma/alpha)*dB_n_0dr);
// return the total result
const Real result (C_i_0 * B_n_0_int);
return result;
}
// Calculates the factor An,0 for (for example) determination of the flux
// through the outer interface
Real
GreensFunction2DRadAbs::calc_A_i_0( const Real alpha) const
{
// Get the required parameters
const Real a( geta() );
const Real sigma( getSigma() );
const Real h (this->geth());
const Real s_An (sigma*alpha);
const Real a_An (a*alpha);
const Real r0An (r0*alpha);
// Calculate all the required Bessel functions
const Real J0_sAn (gsl_sf_bessel_J0(s_An));
const Real J1_sAn (gsl_sf_bessel_J1(s_An));
const Real J0_aAn (gsl_sf_bessel_J0(a_An));
const Real J0_r0An (gsl_sf_bessel_J0(r0An));
const Real Y0_aAn (gsl_sf_bessel_Y0(a_An));
const Real Y0_r0An (gsl_sf_bessel_Y0(r0An));
// Calculate return value
const Real alpha_sq (alpha*alpha);
const Real rho (h*J0_sAn + alpha*J1_sAn);
const Real rho_sq (rho*rho);
const Real B_n_0 (J0_r0An*Y0_aAn - Y0_r0An*J0_aAn);
const Real A_i_0 ((alpha_sq * rho_sq * B_n_0)/( rho_sq - (J0_aAn*J0_aAn)*(h*h + alpha_sq)));
return A_i_0;
}
// Calculates the n-th term of the summation for calculating the flux through
// the inner interface (reaction)
Real
GreensFunction2DRadAbs::leaves_i( const Real alpha) const
{
const Real a( geta() ); // get the needed parameters
const Real sigma( getSigma() );
const Real s_An (sigma*alpha);
const Real a_An (a*alpha);
// calculate all the required Bessel functions
const Real J1_sAn (gsl_sf_bessel_J1(s_An));
const Real Y1_sAn (gsl_sf_bessel_Y1(s_An));
const Real J0_aAn (gsl_sf_bessel_J0(a_An));
const Real Y0_aAn (gsl_sf_bessel_Y0(a_An));
// calculate An,0
const Real A_i_0 (calc_A_i_0(alpha)); // calculate the coefficient A0,n
// calculate dBn,0(sigma)/dr
const Real dB_n_0dr (-alpha*(J1_sAn*Y0_aAn - Y1_sAn*J0_aAn));
// calculate the total result
const Real result (A_i_0 * dB_n_0dr);
return result;
}
// calculates a table with all the constant factors for the survival probability
void
GreensFunction2DRadAbs::createPsurvTable( RealVector& table) const
{
const RealVector& alphaTable_0( this->getAlphaTable( 0 ) ); // get the roots for the survival probability
table.clear(); // empty the table
table.reserve( alphaTable_0.size() ); // and get the nescessary memory
std::transform( alphaTable_0.begin(), alphaTable_0.end(),
std::back_inserter( table ),
boost::bind( &GreensFunction2DRadAbs::p_survival_i,
this, _1) ); // This gets all the roots from 'begin' to 'end'
// passes them as an argument to p_survival_i and
// the result is passed to back_inserter
}
// Creates the tables with various Bessel functions used in drawR, the table is used to speed things up
void
GreensFunction2DRadAbs::createY0J0Tables( RealVector& Y0_Table,
RealVector& J0_Table,
RealVector& Y0J1J0Y1_Table,
const Real t ) const
{
const RealVector& alphaTable_0( this->getAlphaTable( 0 ) );
// get the roots for the survival probability
Y0_Table.clear(); // empty the table
J0_Table.clear();
Y0J1J0Y1_Table.clear();
Y0_Table.reserve( alphaTable_0.size() ); // and get the nescessary memory
J0_Table.reserve( alphaTable_0.size() );
Y0J1J0Y1_Table.reserve( alphaTable_0.size() );
boost::tuple<Real,Real,Real> result;
for (unsigned int count = 0; count < alphaTable_0.size(); count++)
{ result = Y0J0J1_constants(alphaTable_0[count], t);
Y0_Table.push_back (result.get<0>());
J0_Table.push_back (result.get<1>());
Y0J1J0Y1_Table.push_back (result.get<2>());
}
}
// Creates the values for in the tables Y0, J0 and Y0J1J0Y1
boost::tuple<Real,Real,Real>
GreensFunction2DRadAbs::Y0J0J1_constants ( const Real alpha,
const Real t) const
{
const Real D(this->getD());
const Real sigma(this->getSigma());
const Real a(this->geta());
const Real s_An (sigma*alpha);
const Real a_An (a*alpha);
const Real alpha_sq (alpha*alpha);
// calculate all the required Bessel functions
const Real J0_aAn (gsl_sf_bessel_J0(a_An));
const Real Y0_aAn (gsl_sf_bessel_Y0(a_An));
const Real J1_sAn (gsl_sf_bessel_J1(s_An));
const Real Y1_sAn (gsl_sf_bessel_Y1(s_An));
// calculate An,0
const Real A_i_0 (calc_A_i_0(alpha)); //_sq * rho_sq * B_n_0)/( rho_sq - J0_bAn*J0_bAn*(h*h + alpha_sq)));
// calculate the exponent with the time
const Real expT( std::exp(-D*alpha_sq*t));
// and the product
const Real Ai0_expT (A_i_0 * expT / alpha);
// calculate the large constant term in the intergral of Bn,0
const Real Y0J1_J0Y1 (Y0_aAn*sigma*J1_sAn - J0_aAn*sigma*Y1_sAn);
return boost::make_tuple (Ai0_expT*Y0_aAn, Ai0_expT*J0_aAn, Ai0_expT*Y0J1_J0Y1);
}
// =============================================================================
// Root finding algorithm for alpha function.
// Roots (y=0) are necessary to calculate
// Modified by [email protected]. nov, 2011
//
// Note mar 2012:
// The modified root finding algorithm can only work if the roots are calculated
// in a "chronological" sequence (i.e. i = 0, i = 1, i = 2 etc). Therefore roots
// are now all calculated immediately when the roots-table is expanded.
// =============================================================================
// Scans for next interval where a sign change is observed, and thus where a
// root is expected.
void GreensFunction2DRadAbs::GiveRootInterval(
Real& low, // Variable to return left boundary interval
Real& high, // Variable to return right boundary interval
const Integer n) const // Order of Bessel functions
{
// Variables for function values @ resp. left and right boundary interval.
Real f_low , f_high;
// # Get/calculate boundaries and determine width of interval to check for
// sign change.
const Real estimated_alpha_root_distance_( this->getestimated_alpha_root_distance_() );
const Real interval( FRACTION_SCAN_INTERVAL * estimated_alpha_root_distance_);
// If order (n) is zero, the offset is zero, otherwhise take n-1 offset
// value as first estimate.
// (This can be done because the offsets only get bigger, the roots
// shift to the right with the order of the Bessel functions n.)
if (alpha_x_scan_table_[n] == 0) { // which implies i == 0
if (n > 0)
{
alpha_x_scan_table_[n] = ( this->alphaTable[n-1][0] );
}
}
// Define new interval as between x1=offset ("low") and x2=(offset+interval)
// ("high"). Make sure "low" is > 0.
low = alpha_x_scan_table_[n];
high = alpha_x_scan_table_[n] + interval;
if (low <= 0) // TODO this check should be redundant
{
//low = EPSILON/L_TYPICAL;
std::cerr << "Left alpha search interval boundary < 0.\n";
throw std::exception();
}
// # Look for the sign change:
// Get the values of the function at x "low" and x "high".
// Different for n=0 because this results in a simpler function.
// (Note: This code could be optimized by duplicating all involved
// functions and removing this if-statement.
if (n == 0) {
f_low = f_alpha0(low);
f_high = f_alpha0(high);
} else {
f_low = f_alpha(low,n);
f_high = f_alpha(high,n);
}
// Continue shifting the search interval until a sign change is detected.
while( f_low * f_high > 0 )
{
low = high;
f_low = f_high;
high += interval;
f_high = f_alpha( high, n );
}
// When above loop has finished, low and high have values inbetween which
// a root of the alpha function should lie have been found. Make sure that
// scanning for the next root starts at the end of the domain [low, high]
// found here.
alpha_x_scan_table_[n] = high;
return;
}
// Simply returns an interval based upon previous root, estimated interval
// inbetween roots and INTERVAL_MARGIN (see .hpp).
void GreensFunction2DRadAbs::GiveRootIntervalSimple(
Real& low, // Variable to return left boundary interval
Real& high, // Variable to return right boundary interval
const Integer n, // Order of Bessel functions
const Real i) const // ith root
{
// Offset is simply based on previous root, the interval in which the first
// root (i=0) lies is never calculated with this function.
const Real previous_root (getAlpha(n, i-1));
// get estimated interval
const Real estimated_alpha_root_distance_( this->getestimated_alpha_root_distance_() );
// Calculates interval [low, high] where root is expected based on the
// assumption where in a converging regime, where the deviation from this
// estimate is not more than INTERVAL_MARGIN.
low = previous_root +
estimated_alpha_root_distance_ * (1 - INTERVAL_MARGIN);
high = previous_root +
estimated_alpha_root_distance_ * (1 + INTERVAL_MARGIN);
return;
}
// This function calls the GSL root finder, for roots for which n = 0. (This is
// a special case for which the function simplifies.)
Real
GreensFunction2DRadAbs::getAlphaRoot0( const Real low, // root lies between low
const Real high // .. and high
) const
{
// Reinterpret_cast converts any pointer type to any other pointer type,
// even of unrelated classes. Hence below code converts pointer to
// function ::f_alpha_aux_F() to function pointer, which then points to
// memory location of params (¶ms).
// f_alpha0_aux_params is a struct: {gf, value}
f_alpha0_aux_params params = { this, 0 }; // TODO: purpose of this zero is unclear!!!
gsl_function F =
{
reinterpret_cast<double (*)(double, void*)>
( &GreensFunction2DRadAbs::f_alpha0_aux_F ),
¶ms
};
// Define solvertype.
const gsl_root_fsolver_type* solverType( gsl_root_fsolver_brent );
// Initialize the solver.
gsl_root_fsolver* solver( gsl_root_fsolver_alloc( solverType ) );
// Call rootfinder, find root called "alpha" and return it.
const Real alpha ( findRoot( F, solver, low, high,
EPSILON/L_TYPICAL, EPSILON, "GreensFunction2DRadAbs::getAlphaRoot0" ) );
gsl_root_fsolver_free( solver );
// const Real sigma(getSigma());
return alpha;
}
// This function calls the GSL root finder, for roots for which n > 0. (n = 0 is
// a special case for which the function simplifies.)
Real
GreensFunction2DRadAbs::getAlphaRootN( const Real low, // root lies between low
const Real high, // .. and high
const Integer n // nth order Bessel
) const
{
// f_alpha_aux_params is a struct: {gf, n, value}
// n is the summation index (the order of the Bessel functions used
f_alpha_aux_params params = { this, n, 0 }; // TODO: purpose of this zero is unclear!!!
gsl_function F =
{
reinterpret_cast<double (*)(double, void*)>
( &GreensFunction2DRadAbs::f_alpha_aux_F ),
¶ms
};
// Define solvertype.
const gsl_root_fsolver_type* solverType( gsl_root_fsolver_brent );
// Initialize the solver.
gsl_root_fsolver* solver( gsl_root_fsolver_alloc( solverType ) );
// Call rootfinder, find root called "alpha" and return it.
const Real alpha ( findRoot( F, solver, low, high,
EPSILON/L_TYPICAL, EPSILON, "GreensFunction2DRadAbs::getAlphaRootN" ) );
gsl_root_fsolver_free( solver );
return alpha;
}
// Simply calls the correct function to call the rootfinder (either
// getAlphaRoot0 or getAlphaRootN).
Real
GreensFunction2DRadAbs::getAlphaRoot( const Real low, // root lies between low
const Real high, // .. and high
const Integer n // nth order Bessel
) const
{
Real alpha;
if (n == 0) {
alpha = getAlphaRoot0(low, high);
} else {
alpha = getAlphaRootN(low, high, n);
}
return alpha;
}
// Subfunction of getAlpha
//
// The function just counts the number of times the root lies in the interval
// that can be used to estimate the next root. If this happens enough, then it
// edits alpha_correctly_estimated_ to a negative value to signify that it can
// be assumed that for all next roots the distance inbetween roots will
// equal the interval +/- margin.
// TODO: This could be more sophisticated.
void GreensFunction2DRadAbs::decideOnMethod2(size_t n,
RealVector::size_type i
) const
{
// Since the function can only decide with two alpha's already calculated,
// it can't do anything if i = 0.
if (i > 0) {
const Real dx(getAlpha(n, i)-getAlpha(n, i-1)); // note the recursiveness!
const Real estimated_alpha_root_distance_( this->getestimated_alpha_root_distance_() );
// If the relative deviation from the expected difference is smaller
// than the expected margin, increase number of would-be correct
// guesses.
if (fabs(1-dx/estimated_alpha_root_distance_) < INTERVAL_MARGIN) {
++alpha_correctly_estimated_[n];
} else {
alpha_correctly_estimated_[n] = 0;
}
// If guessing would have worked for last CONVERGENCE_ASSUMED roots,
// assume it will for all following roots.
if (alpha_correctly_estimated_[n] > CONVERGENCE_ASSUMED) {
alpha_x_scan_table_[n] = -2; // permanently switch
}
}
return;
}
// This function searches for roots (y=0) of the so-called alpha-function
// (::f_alpha()). It either moves a small search interval along the x-axis to
// check for sign-change (which would indicate a root), and calls the GSL
// root finder, or directly calls the root finder if the spacing between
// roots is found to be converged.
Real GreensFunction2DRadAbs::getAlpha( size_t n, // order
RealVector::size_type i // ith root
) const
{
// const Real sigma(this->getSigma());
Real current_root_, low, high;
// # "Administration"
// Equals reading/writing to/from alphaTable to reading/writing from/to to
// this->alphaTable[n]; n being the order of the Bessel function.
// (Funky pointer operations.)
RealVector& alphaTable( this->alphaTable[n] );
// Gets it's size
const RealVector::size_type oldSize( alphaTable.size() );
// # Expansion of root table
// If doesn't contain requested value, expand table until value
if( i >= oldSize )
{
// Expand the table, temporarily fill with zeroes
alphaTable.resize( i+1, 0 );
// # Calculating the necessary roots to expand the table
for(unsigned int j = oldSize; j <= i; j++)
{
if (alphaTable[j] != 0)
{
std::cerr << "tried accessing root that's not 0. Didn't search..\n";
std::cerr << boost::format(" i = %1%, oldSize = %2%, j = %3%\n") %
i % oldSize % j;
}
else
{
// Method 1. SCANNING. If the roots are not expected to lie close enough
// to the estimate, use the "scanning" procedure to find an interval
// that contains a root. (More robust method.)
// If it is established that we can use method 2,
// alpha_x_scan_table_[n] will contain a value < 0.
if (alpha_x_scan_table_[n] >= 0)
{
// ### Gets estimate of interval by sign-change-searching
// high and low are the return-values of GiveRootInterval.
GiveRootInterval(low, high, n);
// ### Finds the root using the GSL rootfinder
current_root_ = getAlphaRoot(low, high, n);
// ### Puts the found root in the table.
alphaTable[j]= current_root_;
// Check if we can use method 2 for next roots
decideOnMethod2(n, j);
}
// Method 2. ASSUMING ROOTS AT ~FIXED INTERVAL. If next root is expected
// to lie at distance close enough to estimated distance the root finder
// can be called using a simple estimated interval.
else
{
// ### Get interval by simple extrapolation
GiveRootIntervalSimple(low, high, n, j);
// ### Finds the root using the GSL rootfinder
current_root_ = getAlphaRoot(low, high, n);
// ### Puts the found root in the table.
alphaTable[j] = current_root_;
}
}
}
}
return alphaTable[i];
}
// =============================================================================
// End modification
// =============================================================================
// calculates the ith term with exponent and time for the survival probability
Real
GreensFunction2DRadAbs::p_survival_i_exp_table( const unsigned int i,
const Real t,
const RealVector& table ) const
{
const Real alpha( this->getAlpha( 0, i ) );
return std::exp( - getD() * t * alpha * alpha ) * table[i];
}
// adds the exponential with the time to the sum. Needed for the calculation of the flux throught the outer
// interface
Real
GreensFunction2DRadAbs::leavea_i_exp( const unsigned int i,
const Real t) const
{
const Real alpha( this->getAlpha( 0,i ) );
return std::exp( - getD() * t * alpha * alpha ) * calc_A_i_0( alpha );
}
// adds the exponential with the time to the sum. Needed for the inner interface (reaction)
Real
GreensFunction2DRadAbs::leaves_i_exp( const unsigned int i,
const Real t) const
{
const Real alpha( this->getAlpha( 0, i ) );
return std::exp( - getD() * t * alpha * alpha ) * leaves_i( alpha );
}
// calculates the Bossen function for a given r
Real
GreensFunction2DRadAbs::p_int_r_i_exp_table( const unsigned int i,
const Real r,
const RealVector& Y0_aAnTable,
const RealVector& J0_aAnTable,
const RealVector& Y0J1J0Y1Table ) const
{
const Real alpha( this->getAlpha( 0, i ) ); // get the root An
const Real r_An( r*alpha);
const Real J1_rAn (gsl_sf_bessel_J1(r_An));
const Real Y1_rAn (gsl_sf_bessel_Y1(r_An));
const Real result (Y0_aAnTable[i]*r*J1_rAn - J0_aAnTable[i]*r*Y1_rAn - Y0J1J0Y1Table[i]);
return result;
}
// This tries to guess the maximum number of n iterations it needs for calculating the survival probability
// Not really sure yet how this works
unsigned int
GreensFunction2DRadAbs::guess_maxi( const Real t ) const
{
const unsigned int safety( 2 );
if( t >= std::numeric_limits<Real>::infinity() )
{
return safety;
}
const Real D( getD() );
const Real sigma( getSigma() );
const Real a( geta() );
const Real alpha0( getAlpha( 0, 0 ) );
const Real Dt( D * t );
const Real thr( ( exp( - Dt * alpha0 * alpha0 ) / alpha0 ) * this->EPSILON * 1e-1 );
const Real thrsq( thr * thr );
if( thrsq <= 0.0 )
{
return MAX_ALPHA_SEQ;
}
const Real max_alpha( 1.0 / ( sqrt( exp( gsl_sf_lambert_W0( 2 * Dt / thrsq ) ) * thrsq ) ) );
const unsigned int maxi( safety + static_cast<unsigned int>( max_alpha * ( a - sigma ) / M_PI ) );
return std::min( maxi, MAX_ALPHA_SEQ );
}
// Calculates the survival probability at a given time.
// This is a little wrapper for the p_survival_table so that you can easily calculate the survival probability
// at a given time
Real
GreensFunction2DRadAbs::p_survival( const Real t) const
{
RealVector psurvTable;
const Real p( p_survival_table( t, psurvTable ) );
return p;
}
// This actually calculates the Survival probability at time t given the particle was at r0 at time 0
// It uses the pSurvTable for efficiency (so you don't have to calculate all the constant factors all
// the time)
Real
GreensFunction2DRadAbs::p_survival_table( const Real t,
RealVector& psurvTable ) const
{
Real p;
const unsigned int maxi( guess_maxi( t ) ); // guess the maximum number of iterations required
// const unsigned int maxi( 500 ); // THIS LEADS TO BIZARRE RESULTS
// If the estimated # terms needed for convergence is bigger than number
// of terms summed over (MAX_ALPHA_SEQ), give error.
if( maxi == this->MAX_ALPHA_SEQ )
{
std::cerr << boost::format("p_survival_table (used by drawTime) "
"couldn't converge; max terms reached: %1%\n") % maxi;
}
if( psurvTable.size() < maxi + 1 ) // if the dimensions are good then this means
{ // that the table is filled
getAlpha( 0, maxi ); // this updates the table of roots
this->createPsurvTable( psurvTable); // then the table is filled with data
}
// A sum over terms is performed, where convergence is assumed. It is not
// clear if this is a just assumption.
// TODO!
p = funcSum_all( boost::bind( &GreensFunction2DRadAbs::p_survival_i_exp_table,
this,
_1, t, psurvTable ),
maxi ); // calculate the sum at time t
return p*M_PI*M_PI_2;
}
// calculates the flux leaving through the inner interface at a given moment
// FIXME: This is inaccurate for small t's!!
Real
GreensFunction2DRadAbs::leaves( const Real t) const
{
const Real sigma(this->getSigma());
const Real D(this->getD() );
const Real p( funcSum( boost::bind( &GreensFunction2DRadAbs::leaves_i_exp,
this,
_1, t),
this->MAX_ALPHA_SEQ ) );
return M_PI_2*M_PI*D*sigma*p; // The minus is not there because the flux is in the negative r
// direction, and the direction is already accounted for in the derivative of B0,n(r)
// See also leaves_i
}
// calculates the flux leaving through the outer interface at a given moment
Real
GreensFunction2DRadAbs::leavea( const Real t) const
{
const Real D(this->getD() );
const Real p( funcSum( boost::bind( &GreensFunction2DRadAbs::leavea_i_exp,
this,
_1, t),
this->MAX_ALPHA_SEQ ) );
return M_PI*D*p;
}
// calculates the sum of the sequence for drawR based upon the values in the tables and r
Real
GreensFunction2DRadAbs::p_int_r_table( const Real r,
const RealVector& Y0_aAnTable,
const RealVector& J0_aAnTable,
const RealVector& Y0J1J0Y1Table ) const
{
const Real p( funcSum( boost::bind( &GreensFunction2DRadAbs::p_int_r_i_exp_table,
this,
_1, r, Y0_aAnTable, J0_aAnTable, Y0J1J0Y1Table ),
Y0_aAnTable.size() ) );
return p*M_PI*M_PI_2;
}
// Used by drawTime
// Wrapper for p_survival_table for the interator to find the root for drawTime
Real
GreensFunction2DRadAbs::p_survival_table_F( const Real t,
const p_survival_table_params* params )
{
const GreensFunction2DRadAbs* const gf( params->gf ); // the current gf (not sure why this is here)
RealVector& table( params->table ); // table is empty but will be filled in p_survival_table
const Real rnd( params->rnd );
return rnd - gf->p_survival_table( t, table );
}
// a wrapper to make p_int_r_table available to the iterator calculating the root
Real
GreensFunction2DRadAbs::p_int_r_F( const Real r,
const p_int_r_params* params )
{
const GreensFunction2DRadAbs* const gf( params->gf );
const RealVector& Y0_aAnTable( params->Y0_aAnTable );
const RealVector& J0_aAnTable( params->J0_aAnTable );
const RealVector& Y0J1J0Y1Table( params->Y0J1J0Y1Table );
const Real rnd( params->rnd );
return gf->p_int_r_table( r, Y0_aAnTable, J0_aAnTable, Y0J1J0Y1Table) - rnd;
}
// Draws a first passage time, this could be an escape (through the outer boundary) or a reaction (through
// the inner boundary)
Real GreensFunction2DRadAbs::drawTime( const Real rnd) const
{
const Real D( this->getD() );
const Real sigma( this->getSigma() );
const Real a( this->geta() );
const Real kf( this->getkf() );
const Real r0( this->getr0() );
THROW_UNLESS( std::invalid_argument, 0.0 <= rnd && rnd < 1.0 );
THROW_UNLESS( std::invalid_argument, sigma <= r0 && r0 <= a );
Real t_guess;
if( r0 == a || a == sigma ) // when the particle is at the border or if the PD has no real size
{
return 0.0;
}
// get some initial guess for the time, dr=sqrt(2dDt) with d
// the dimensionality (2 in this case)
const Real t_Abs( gsl_pow_2( a - r0 ) / ( 4.0 * D ) );
if ( kf == 0.0 ) // if there was only one absorbing boundary
{
t_guess = t_Abs;
}
else
{
const Real t_Rad( D / gsl_pow_2( kf/(2*M_PI*a) ) + gsl_pow_2( r0 - sigma ) / D );
t_guess = std::min( t_Abs, t_Rad ); // take the shortest time to a boundary
}
t_guess *= .1;
const Real minT( std::min( sigma * sigma / D * this->MIN_T_FACTOR,
t_guess * 1e-7 ) ); // something with determining the lowest possible t
RealVector psurvTable; // this is still empty as of now->not used
p_survival_table_params params = { this, psurvTable, rnd };
gsl_function F =