calc.py 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. from cmath import atan
  2. from multiprocessing import Queue
  3. import numpy as np
  4. def open_file(path):
  5. """depends on the format of file we open"""
  6. freq, re, im = [], [], []
  7. with open(path) as f:
  8. for line in f:
  9. temp = line[:-1].split(' ')
  10. for i in range(3):
  11. temp[i] = temp[i].replace(" ", "")
  12. freq.append(float(temp[0]))
  13. re.append(float(temp[1]))
  14. im.append(float(temp[2]))
  15. return freq, re, im
  16. def prepare_data(freq, re, im, fl=[]):
  17. """the function takes raw data and gives vectors of eq (8)"""
  18. # finding fl from the point with smallest magnitude if argument not provided
  19. if type(fl) is list and len(fl)==0:
  20. s = abs(np.array(re) + np.array(im)*1j)
  21. # frequency of loaded resonance
  22. fl = freq[list(abs(s)).index(min(abs(s)))]
  23. # frequency of unloaded resonance.
  24. f0 = fl
  25. # f0 = fl does not decrease the accuracy if Q >> 100
  26. e1, e2, e3, gamma, p = [], [], [], [], []
  27. for i in range(0, len(freq)):
  28. # filling vectors
  29. t = 2 * (freq[i] - fl) / f0
  30. g = re[i] + im[i] * 1j
  31. e1.append(t)
  32. e2.append(1)
  33. e3.append(-t * g)
  34. gamma.append(g)
  35. p.append(1 / (1 + t ** 2 * (1 + re[i] ** 2 + im[i] ** 2)))
  36. data = np.array([e1, e2, e3, gamma, p], dtype=np.cdouble)
  37. return data, fl
  38. def solution(data):
  39. """ takes projections of equation (8) on vectors e1, e2, e3 and solves the equations.
  40. It is also gives matrix of equation"""
  41. c = [] # matrix of the system
  42. b = [] # matrix extension
  43. for i in range(3):
  44. c1 = np.vdot(data[i], data[4] * data[0])
  45. c2 = np.vdot(data[i], data[4] * data[1])
  46. c3 = np.vdot(data[i], data[4] * data[2])
  47. c.append([c1, c2, c3])
  48. b.append(np.vdot(data[i], data[4] * data[3]))
  49. c = np.array(c)
  50. a = np.linalg.solve(c, b)
  51. d = np.linalg.inv(c) # inverse of matrix c
  52. return a, c, d
  53. def q_factor(a):
  54. """calculation of result"""
  55. Ql = a[2].imag # Q-factor of loaded resonator
  56. diam = abs(a[1] - a[0] / a[2]) # diameter of circle
  57. k = 1 / (2 / diam - 1)
  58. Q = Ql * (1 + k) # Q-factor = result
  59. return Ql, diam, k, Q
  60. def recalculation_of_data(data, a, c, d, error=False):
  61. """preparation data for the next iteration of solving system"""
  62. # data = np.array([e1, e2, e3, gamma, p], dtype=complex), t = e1, 1 = e2
  63. eps = np.array(a[0]*data[0] + a[1]*data[1] - a[2]*data[0]*data[3] - data[3], dtype=complex)
  64. # eps is eq(7) line's errors
  65. S2 = np.dot(abs(data[4]), abs(eps)*abs(eps)) # the weighted squared sum of errors
  66. sigma2A = [] # the square of standart deviation coefficients a
  67. temp = c[0][0]*d[0][0] + c[1][1]*d[1][1] + c[2][2]*d[2][2]
  68. for i in range(3):
  69. sigma2A.append(d[i][i] * S2 / temp)
  70. for i in range(len(data[4])): # recalculation of weight coefficients P
  71. data[4][i] = 1/(data[0][i]**2 * sigma2A[0] + sigma2A[1] + data[0][i]**2 * sigma2A[2] * (abs(data[3][i])**2))
  72. if error:
  73. return abs(np.array(sigma2A))
  74. else:
  75. return data
  76. def random_deviation(a, sigma2A, diam, k, Ql):
  77. """defines standart deviations of values"""
  78. sigmaQl = sigma2A[2]**0.5
  79. sigmaDiam = (sigma2A[0]/(abs(a[2])**2) + sigma2A[1] + abs(a[0]/a[2]/a[2])**2 * sigma2A[2])**0.5
  80. sigmaK = 2*sigmaDiam/((2-diam)**2)
  81. sigmaQ0 = ((1 + k)**2 * sigma2A[2] + Ql**2 * sigmaK**2)**0.5
  82. return sigmaQ0, sigmaQl
  83. def apply(filename):
  84. freq, re, im = open_file(filename)
  85. data = prepare_data(freq, re, im)
  86. a, c, d = solution(data)
  87. for i in range(2, 10):
  88. data = recalculation_of_data(data, a, c, d)
  89. a, c, d = solution(data)
  90. Ql, diam, k, Q = q_factor(a)
  91. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  92. sigmaQ0, sigmaQl = random_deviation(a, sigma2A, diam, k, Ql)
  93. print(f"Q = {Q} +- {sigmaQ0}, if i == {i}")
  94. def fl_fitting(freq, re, im):
  95. """providing an option to find actual fl"""
  96. data, fl = prepare_data(freq, re, im)
  97. a, c, d = solution(data)
  98. # Repeated curve fitting
  99. # 1.189 of Qfactor Matlab
  100. fl2 = 0
  101. g_d=0
  102. g_c=0
  103. for x in range(0, 3):
  104. g_c = (np.conj(a[2])*a[1]-a[0])/(np.conj(a[2])-a[2])
  105. g_d = a[0]/a[2]
  106. g_2 = 2*g_c-g_d
  107. dt = (a[1]-g_2)/(g_2*a[2]-a[0])
  108. fl2 = fl*(1 + np.real(dt)/2)
  109. data, fl = prepare_data(freq, re, im, fl2)
  110. a, c, d = solution(data)
  111. for i in range(2, 20):
  112. data = recalculation_of_data(data, a, c, d)
  113. a, c, d = solution(data)
  114. Ql, diam, k, Q = q_factor(a)
  115. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  116. sigmaQ0, sigmaQl = random_deviation(a, sigma2A, diam, k, Ql)
  117. # taking into account coupling losses on page 69 of Qfactor Matlab
  118. # to get results similar to example program
  119. if False:
  120. phi1=np.arctan(np.double(g_d.imag/g_d.real)) # 1.239
  121. phi2=np.arctan(np.double((g_c.imag-g_d.imag)/(g_c.real-g_d.real)))
  122. phi=-phi1+phi2
  123. d_s=(1-np.abs(g_d)**2)/(1-np.abs(g_d)*np.cos(phi))
  124. diam = abs(a[1] - a[0] / a[2])
  125. qk=1/(d_s/diam-1)
  126. sigma2A = recalculation_of_data(data, a, c, d, error=True)
  127. sigmaQ0 = random_deviation(a, sigma2A, diam, k, Ql)
  128. Q = Ql * (1 + qk) # Q-factor = result
  129. print(f"Q0 = {Q} +- {sigmaQ0}")
  130. return Q,sigmaQ0, Ql, sigmaQl,a