Advertisement
Guest User

Untitled

a guest
Dec 8th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.42 KB | None | 0 0
  1. def step_generator(intervals, parameter):
  2. """
  3.  
  4.  
  5.  
  6.  
  7. :param intervals: the 1- or 2-D intervals to be used
  8. :param parameter:
  9. """
  10.  
  11. intervals = np.atleast_2d(intervals)
  12.  
  13. # need to make sure the shape is right
  14. # assert self._intervals.shape
  15.  
  16.  
  17. if intervals.shape[0] > 1 and intervals.shape[1] == 2:
  18.  
  19. n_intervals = intervals.shape[0]
  20.  
  21.  
  22.  
  23.  
  24. is_2d = True
  25.  
  26. elif intervals.shape[0] == 1:
  27.  
  28. n_intervals = intervals.shape[1]
  29. intervals = intervals[0]
  30.  
  31. is_2d = False
  32.  
  33. else:
  34.  
  35. raise RuntimeError("These intervals are not yet supported")
  36.  
  37. parameter_min = parameter.min_value
  38. parameter_max = parameter.max_value
  39. initial_value = parameter.value
  40.  
  41. if is_2d:
  42.  
  43. func = StepFunctionUpper()
  44.  
  45. for i in range(n_intervals - 1):
  46.  
  47. func += StepFunctionUpper()
  48.  
  49. for i, interval in enumerate(intervals):
  50.  
  51. i=i+1
  52.  
  53. func.free_parameters['value_%d' % i].value = initial_value
  54. func.free_parameters['value_%d' % i].min_value = parameter_min
  55. func.free_parameters['value_%d' % i].max_value = parameter_max
  56.  
  57. func.parameters['upper_bound_%d' % i].value = interval[1]
  58.  
  59. func.parameters['lower_bound_%d' % i].value = interval[0]
  60.  
  61.  
  62. else:
  63.  
  64. func = DiracDelta()
  65.  
  66. for i in range(n_intervals - 1):
  67.  
  68. func += DiracDelta()
  69.  
  70. for i, interval in enumerate(intervals):
  71.  
  72. i=i+1
  73.  
  74. func.free_parameters['k_%d' % i].value = initial_value
  75. func.free_parameters['k_%d' % i].min_value = parameter_min
  76. func.free_parameters['k_%d' % i].max_value = parameter_max
  77.  
  78. func.parameters['zero_point_%d' % i].value = interval
  79.  
  80.  
  81.  
  82. return func
  83.  
  84.  
  85.  
  86. # how it would work:
  87.  
  88.  
  89. triggerName = 'bn080916009'
  90. ra = 121.8
  91. dec = -61.3
  92.  
  93.  
  94.  
  95.  
  96. data_list = DataList(nai6,nai7,nai9,bgo1,nai6_2,nai7_2,nai9_2,bgo1_2 )
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104. cpl = Cutoff_powerlaw()
  105.  
  106. syn = SynchrotronNumerical()
  107.  
  108.  
  109.  
  110.  
  111.  
  112. GRB = PointSource( triggerName, ra, dec, spectral_shape=cpl )
  113.  
  114. model = Model( GRB )
  115.  
  116.  
  117. #syn.B=1E7
  118. #syn.K = 1E25
  119. #syn.gamma_cool=7E4
  120. #syn.gamma_max = 1E6
  121. time = IndependentVariable("time",13.0, unit='s')
  122. model
  123.  
  124. law = Powerlaw(K=500,index=-1)
  125.  
  126. #law = Line(a=-0.02,b=-2.0)
  127. model.add_independent_variable(time)
  128. model.link(model.bn080916009.spectrum.main.Cutoff_powerlaw.xc,time,law)
  129.  
  130. ff = step_generator([12.5,17.5],cpl.index)
  131.  
  132.  
  133.  
  134. model.link(model.bn080916009.spectrum.main.Cutoff_powerlaw.index,time,ff)
  135.  
  136.  
  137. ff = step_generator([12.5,17.5],cpl.K)
  138.  
  139.  
  140.  
  141. model.link(model.bn080916009.spectrum.main.Cutoff_powerlaw.K,time,ff)
  142.  
  143. nai6.external_property(time,12.5)
  144. nai7.external_property(time,12.5)
  145. nai9.external_property(time,12.5)
  146. bgo1.external_property(time,12.5)
  147.  
  148.  
  149. nai6_2.external_property(time,17.5)
  150. nai7_2.external_property(time,17.5)
  151. nai9_2.external_property(time,17.5)
  152. bgo1_2.external_property(time,17.5)
  153.  
  154.  
  155.  
  156.  
  157. class StepFunctionUpper(Function1D):
  158. r"""
  159. description :
  160.  
  161. A function which is constant on the interval lower_bound - upper_bound and 0 outside the interval. The
  162. upper interval is open.
  163.  
  164. latex : $ f(x)=\begin{cases}0 & x < \text{lower_bound} \\\text{value} & \text{lower_bound} \le x \le \text{upper_bound} \\ 0 & x > \text{upper_bound} \end{cases}$
  165.  
  166. parameters :
  167.  
  168. lower_bound :
  169.  
  170. desc : Lower bound for the interval
  171. initial value : 0
  172. min : -np.inf
  173. max : np.inf
  174. fix : yes
  175.  
  176. upper_bound :
  177.  
  178. desc : Upper bound for the interval
  179. initial value : 1
  180. min : -np.inf
  181. max : np.inf
  182. fix : yes
  183.  
  184. value :
  185.  
  186. desc : Value in the interval
  187. initial value : 1.0
  188.  
  189. tests :
  190. - { x : 0.5, function value: 1.0, tolerance: 1e-20}
  191. - { x : -0.5, function value: 0, tolerance: 1e-20}
  192.  
  193. """
  194.  
  195. __metaclass__ = FunctionMeta
  196.  
  197. def _set_units(self, x_unit, y_unit):
  198. # Lower and upper bound has the same unit as x
  199. self.lower_bound.unit = x_unit
  200. self.upper_bound.unit = x_unit
  201.  
  202. # value has the same unit as y
  203. self.value.unit = y_unit
  204.  
  205. def evaluate(self, x, lower_bound, upper_bound, value):
  206. # The value * 0 is to keep the units right
  207.  
  208. result = np.zeros(x.shape) * value * 0
  209.  
  210. idx = (x >= lower_bound) & (x < upper_bound)
  211. result[idx] = value
  212.  
  213. return result
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement