Advertisement
Abhisek92

Demo Function

Feb 20th, 2020
382
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.39 KB | None | 0 0
  1. import numpy as np
  2. from scipy.integrate import quad
  3.  
  4. def func(x, l, kx):
  5.     return np.exp(
  6.         i * (-(((x) ** 2) / l ** 2))
  7.     ) * np.cos(2 *i * kx * x)
  8.  
  9. def sigma_vv(theta, d, h, l):
  10.     theta = np.deg2rad(theta)
  11.     k = (2 * np.pi) / l
  12.     k_z = k * np.cos(theta)
  13.     k_x = k * np.sin(theta)
  14.     rv_n = (d * np.cos(theta))-np.sqrt(d-(np.sin(theta) ** 2))
  15.     rv_d = (d * np.cos(theta)) + np.sqrt(d-(np.sin(theta) ** 2))
  16.     r_v = rv_n / rv_d
  17.     f_vv = (2 * r_v) / (np.cos(theta))
  18.     f_vv_x = (
  19.         2 * (np.sin(theta) ** 2
  20.     ) * (1 + r_v) ** 2) / np.cos(theta)
  21.     f_vv_y = (1 - (1 / d)) + (
  22.         (
  23.             d - (sin(theta) ** 2) - d * (cos(theta) ** 2)
  24.         ) / (
  25.             (d ** 2) * (cos(theta) ** 2)
  26.         )
  27.     )
  28.     f_vv_xy = f_vv_x * f_vv_y
  29.     # Assuming precision of float32
  30.     eps = np.finfo(np.float32).tiny
  31.     assert (
  32.         np.abs(
  33.             np.array(
  34.                 [
  35.                     k,
  36.                     k_z,
  37.                     k_x,
  38.                     r_v,
  39.                     f_vv,
  40.                     f_vv_xy
  41.                 ],
  42.                 dtype=np.float64
  43.             )
  44.         ) > eps
  45.     ).all()
  46.    
  47.     sigma_vv_g, i = 0.0, 0
  48.     while True:
  49.         i+=1
  50.         fact_inv = np.float64(1.0 / np.float64(np.math.factorial(i)))
  51.         if fact_inv < eps:
  52.             break
  53.         else:
  54.             # do the further calculation here
  55.             intensity_vv = (
  56.                 (
  57.                     (2 * k_z) ** i
  58.                 ) * f_vv * exp(
  59.                     (-h ** 2) * (k_z ** 2)
  60.                 )
  61.             ) + (
  62.                 ((k_z ** i) * f_vv_xy) / 2
  63.             )
  64.             w, err = quad(func=func, a=-np.inf, np.inf, args=(l, k_x))
  65.             delta_i = (
  66.                 (k ** 2) / 2
  67.             ) * np.exp(
  68.                 (-2) * (k_z ** 2) * (h ** 2)
  69.             ) * (
  70.                 (
  71.                     (
  72.                         h ** (2 * i)
  73.                     ) * (
  74.                         (I_vv) ** 2
  75.                     ) * (
  76.                         (1 / (2 * np.pi)) * w
  77.                     )
  78.                 ) * fact_inv
  79.             )
  80.             if np.abs(delta_i) < eps:
  81.                 # There is no point of proceeding further since we reached the limit of precision
  82.                 break
  83.             else:
  84.                 sigma_vv_g += delta_i
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement