WarpX
BesselRoots.H
Go to the documentation of this file.
1 /* Copyright 2019 David Grote
2  *
3  * This file is part of WarpX.
4  *
5  * License: BSD-3-Clause-LBNL
6  */
7 /* -------------------------------------------------------------------------
8 ! program to calculate the first zeroes (root abscissas) of the first
9 ! kind bessel function of integer order n using the subroutine rootj.
10 ! --------------------------------------------------------------------------
11 ! sample run:
12 !
13 ! (calculate the first 10 zeroes of 1st kind bessel function of order 2).
14 !
15 ! zeroes of bessel function of order: 2
16 !
17 ! number of calculated zeroes: 10
18 !
19 ! table of root abcissas (5 items per line)
20 ! 5.135622 8.417244 11.619841 14.795952 17.959819
21  21.116997 24.270112 27.420574 30.569204 33.716520
22 !
23 ! table of error codes (5 items per line)
24 ! 0 0 0 0 0
25 ! 0 0 0 0 0
26 !
27 ! --------------------------------------------------------------------------
28 ! reference: from numath library by tuan dang trong in fortran 77
29 ! [bibli 18].
30 !
31 ! c++ release 1.0 by j-p moreau, paris
32 ! (www.jpmoreau.fr)
33 ! ------------------------------------------------------------------------ */
34 
35 using amrex::operator""_rt;
36 
37 void SecantRootFinder(int n, int nitmx, amrex::Real tol, amrex::Real *zeroj, int *ier);
38 
39 /*----------------------------------------------------------------------
40  * calculate the first nk zeroes of bessel function j(n, x)
41  * including the trivial root (when n > 0)
42  *
43  * inputs:
44  * n order of function j (integer >= 0) i*4
45  * nk number of first zeroes (integer > 0) i*4
46  * outputs:
47  * roots(nk) table of first zeroes (abcissas) r*8
48  * ier(nk) table of error codes (must be zeroes) i*4
49  *
50  * reference :
51  * abramowitz m. & stegun irene a.
52  * handbook of mathematical functions
53  */
54 void GetBesselRoots(int n, int nk, amrex::Vector<amrex::Real>& roots, amrex::Vector<int> &ier) {
55  amrex::Real zeroj;
56  int ierror, ik, k;
57 
58  const amrex::Real tol = 1e-14_rt;
59  const amrex::Real nitmx = 10;
60 
61  const amrex::Real c1 = 1.8557571_rt;
62  const amrex::Real c2 = 1.033150_rt;
63  const amrex::Real c3 = 0.00397_rt;
64  const amrex::Real c4 = 0.0908_rt;
65  const amrex::Real c5 = 0.043_rt;
66 
67  const amrex::Real t0 = 4.0_rt*n*n;
68  const amrex::Real t1 = t0 - 1.0_rt;
69  const amrex::Real t3 = 4.0_rt*t1*(7.0_rt*t0 - 31.0_rt);
70  const amrex::Real t5 = 32.0_rt*t1*((83.0_rt*t0 - 982.0_rt)*t0 + 3779.0_rt);
71  const amrex::Real t7 = 64.0_rt*t1*(((6949.0_rt*t0 - 153855.0_rt)*t0 + 1585743.0_rt)*t0 - 6277237.0_rt);
72 
73  roots.resize(nk);
74  ier.resize(nk);
75 
76  // first zero
77  if (n == 0) {
78  zeroj = c1 + c2 - c3 - c4 + c5;
79  SecantRootFinder(n, nitmx, tol, &zeroj, &ierror);
80  ier[0] = ierror;
81  roots[0] = zeroj;
82  ik = 1;
83  }
84  else {
85  // Include the trivial root
86  ier[0] = 0;
87  roots[0] = 0.;
88  const amrex::Real f1 = std::pow(n, (1.0_rt/3.0_rt));
89  const amrex::Real f2 = f1*f1*n;
90  const amrex::Real f3 = f1*n*n;
91  zeroj = n + c1*f1 + (c2/f1) - (c3/n) - (c4/f2) + (c5/f3);
92  SecantRootFinder(n, nitmx, tol, &zeroj, &ierror);
93  ier[1] = ierror;
94  roots[1] = zeroj;
95  ik = 2;
96  }
97 
98  // other zeroes
99  // k counts the nontrivial roots
100  // ik counts all roots
101  k = 2;
102  while (ik < nk) {
103 
104  // mac mahon's series for k >> n
105  const amrex::Real b0 = (k + 0.5_rt*n - 0.25_rt)*MathConst::pi;
106  const amrex::Real b1 = 8.0_rt*b0;
107  const amrex::Real b2 = b1*b1;
108  const amrex::Real b3 = 3.0_rt*b1*b2;
109  const amrex::Real b5 = 5.0_rt*b3*b2;
110  const amrex::Real b7 = 7.0_rt*b5*b2;
111 
112  zeroj = b0 - (t1/b1) - (t3/b3) - (t5/b5) - (t7/b7);
113 
114  const amrex::Real errj = std::abs(jn(n, zeroj));
115 
116  // improve solution using procedure SecantRootFinder
117  if (errj > tol) SecantRootFinder(n, nitmx, tol, &zeroj, &ierror);
118 
119  roots[ik] = zeroj;
120  ier[ik] = ierror;
121 
122  k++;
123  ik++;
124  }
125 }
126 
127 void SecantRootFinder(int n, int nitmx, amrex::Real tol, amrex::Real *zeroj, int *ier) {
128 
129  amrex::Real p0, p1, q0, q1, dp, p;
130  amrex::Real c[2];
131 
132  c[0] = 0.95_rt;
133  c[1] = 0.999_rt;
134  *ier = 0;
135 
136  for (int ntry=0 ; ntry <= 1 ; ntry++) {
137  p0 = c[ntry]*(*zeroj);
138 
139  p1 = *zeroj;
140  q0 = jn(n, p0);
141  q1 = jn(n, p1);
142  for (int it=1; it <= nitmx; it++) {
143  if (q1 == q0) break;
144  p = p1 - q1*(p1 - p0)/(q1 - q0);
145  dp = p - p1;
146  if (it > 1 && std::abs(dp) < tol) {
147  *zeroj = p;
148  return;
149  }
150  p0 = p1;
151  q0 = q1;
152  p1 = p;
153  q1 = jn(n, p1);
154  }
155  }
156  *ier = 3;
157  *zeroj = p;
158 }
void GetBesselRoots(int n, int nk, amrex::Vector< amrex::Real > &roots, amrex::Vector< int > &ier)
Definition: BesselRoots.H:54
int n
Definition: run_libensemble_on_warpx.py:68
void SecantRootFinder(int n, int nitmx, amrex::Real tol, amrex::Real *zeroj, int *ier)
Definition: BesselRoots.H:127
int it
Definition: read_lab_particles.py:11