special-functions-test-generator.py 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. #!/usr/bin/env python3
  2. import mpmath as mp
  3. import numpy as np
  4. Du_test = [
  5. # // x, [Re(m), Im(m)], Qext, Qsca, test_name
  6. [0.099, [0.75,0], 7.417859e-06, 7.417859e-06, 'a'],
  7. [0.101, [0.75,0], 8.033538e-06, 8.033538e-06, 'b'],
  8. [10, [0.75,0], 2.232265, 2.232265, 'c'],
  9. [1000, [0.75,0], 1.997908, 1.997908, 'd'],
  10. [100, [1.33,-1e-5], 2.101321, 2.096594, 'e'],
  11. [10000, [1.33,-1e-5], 2.004089, 1.723857, 'f'],
  12. [0.055, [1.5, -1], 0.101491, 1.131687e-05, 'g'],
  13. [0.056, [1.5, -1], 0.1033467, 1.216311e-05, 'h'],
  14. [100, [1.5, -1], 2.097502, 1.283697, 'i'],
  15. [10000, [1.5, -1], 2.004368, 1.236574, 'j'],
  16. [1, [10, -10], 2.532993, 2.049405, 'k'],
  17. [100, [10, -10,], 2.071124, 1.836785, 'l'],
  18. [10000, [10, -10], 2.005914, 1.795393, 'm'],
  19. [80, [1.05, 1], 0, 0, 'Yang'],
  20. ]
  21. # // Dtest refractive index is m={1.05,1}, the size parameter is x = 80
  22. n_list = [0,1,30,50,60,70,75,80,85,90,99,116,130];
  23. def get_z_values(du_list):
  24. zlist = []
  25. for record in du_list:
  26. x = mp.mpf(str(record[0]))
  27. m = mp.mpc(str(record[1][0]), str(record[1][1]))
  28. z = x*m
  29. zlist.append(z)
  30. return zlist
  31. # APPLIED OPTICS / Vol. 53, No. 31 / 1 November 2014, eq(13)
  32. def LeRu_cutoff(z):
  33. x = mp.fabs(z)
  34. return int(x + 11 * x**(1/3) + 1)
  35. def get_n_list(z, max_number_of_elements = 10):
  36. nmax = LeRu_cutoff(z)
  37. factor = nmax**(1/(max_number_of_elements-2))
  38. n_list = [int(factor**i) for i in range(max_number_of_elements-1) ]
  39. n_list.append(0)
  40. n_set = set(n_list)
  41. return sorted(n_set)
  42. # Riccati-Bessel z*j_n(z)
  43. def psi(n,z):
  44. return mp.sqrt( (mp.pi * z)/2 ) * mp.autoprec(mp.besselj)(n+1/2,z)
  45. # to compare r(n,z) with Wolfram Alpha
  46. # n=49, z=1.3-2.1i, SphericalBesselJ[n-1,z]/SphericalBesselJ[n,z]
  47. def r(n,z):
  48. return psi(n-1,z)/psi(n,z)
  49. def D1(n,z):
  50. return r(n,z) - n/z
  51. def main ():
  52. mp.mp.dps = 15
  53. output_dps = 10
  54. elements_of_n_list = 15
  55. out_filename = 'test_spec_functions_data.h'
  56. zlist = get_z_values(Du_test)
  57. output_str = ('// complex(z), n, complex(D1_n(z)), abs_err_real, abs_err_imag\n'+
  58. 'D1_test_'+str(output_dps)+'digits = {\n')
  59. for z in zlist:
  60. n_list = get_n_list(z, elements_of_n_list)
  61. print(z, n_list)
  62. for n in n_list:
  63. try:
  64. D1nz = D1(n,z)
  65. except:
  66. pass
  67. print(z, n)
  68. output_str+=('{{'+
  69. mp.nstr(z.real, output_dps)+','+
  70. mp.nstr(z.imag, output_dps)+'},'+
  71. str(n)+',{'+
  72. mp.nstr(D1nz.real, output_dps)+','+
  73. mp.nstr(D1nz.imag, output_dps)+'},'+
  74. mp.nstr(mp.fabs(D1nz.real* 10**-output_dps),2)+',' +
  75. mp.nstr(mp.fabs(D1nz.imag* 10**-output_dps),2)+
  76. '},\n')
  77. output_str += '};\n'
  78. with open(out_filename, 'w') as out_file:
  79. out_file.write(output_str)
  80. main()