def initialize_f(q1, q2, v1, v2, v3, params): m_e = params.mass[0, 0] m_p = params.mass[0, 1] k = params.boltzmann_constant n_b = params.density_background T_b = params.temperature_background k = params.boltzmann_constant v1_bulk_electron = params.v1_bulk_electron v1_bulk_positron = params.v1_bulk_positron n = n_b + 0.01 * af.exp(-10 * (q1 - 5)**2) f_e = n * (m_e / (2 * np.pi * k * T_b))**(1 / 2) \ * af.exp(-m_e * (v1[:, 0] - v1_bulk_electron)**2 / (2 * k * T_b)) \ f_p = n * (m_p / (2 * np.pi * k * T_b))**(1 / 2) \ * af.exp(-m_p * (v1[:, 1] - v1_bulk_positron)**2 / (2 * k * T_b)) \ f = af.join(1, f_e, f_p) af.eval(f) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m_e = params.mass[0, 0] m_i = params.mass[0, 1] k = params.boltzmann_constant n_b_e = params.n_background_e T_b_e = params.temperature_background_e n_b_i = params.n_background_i T_b_i = params.temperature_background_i n_e = n_b_e + params.alpha * af.cos(q1) n_i = n_b_i + 0 * q1 T_e = T_b_e T_i = T_b_i f_e = n_e * np.sqrt(1 / (2 * np.pi)) * af.sqrt(m_e * T_i/m_i * T_e) \ * af.exp(-0.5 * (m_e * T_i/m_i * T_e) * (v1[:, 0])**2) f_i = n_i * np.sqrt(1 / (2 * np.pi)) \ * af.exp(-0.5 * (v1[:, 1])**2) f = af.join(1, f_e, f_i) af.eval(f) return (f)
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass_particle k = params.boltzmann_constant rho_b = params.rho_background T_b = params.temperature_background p1_bulk = params.p1_bulk_background p2_bulk = params.p2_bulk_background p3_bulk = params.p3_bulk_background pert_real = params.pert_real pert_imag = params.pert_imag k_q1 = params.k_q1 k_q2 = params.k_q2 # Calculating the perturbed density: rho = rho_b + (pert_real * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag * af.sin(k_q1 * q1 + k_q2 * q2)) f = rho * (m / (2 * np.pi * k * T_b))**(3 / 2) \ * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T_b)) \ * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T_b)) \ * af.exp(-m * (p3 - p3_bulk)**2 / (2 * k * T_b)) af.eval(f) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant q2_minus = 0.5 q2_plus = 1.5 regulator = 20 # larger value makes the transition sharper n = 1 + 0.5 * (af.tanh((q2 - q2_minus) * regulator) - af.tanh( (q2 - q2_plus) * regulator)) v1_bulk = (af.tanh((q2 - q2_minus) * regulator) - af.tanh( (q2 - q2_plus) * regulator) - 1) v2_bulk = 0.01 * af.sin(2 * np.pi * q1) *\ ( af.exp(-25 * (q2 - q2_minus)**2) + af.exp(-25 * (q2 - q2_plus )**2) ) T = (10 / n) f = n * (m / (2 * np.pi * k * T)) \ * af.exp(-m * (v1 - v1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (v2 - v2_bulk)**2 / (2 * k * T)) af.eval(f) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant n = af.select(af.abs(q2)>0.25, q1**0, 2) # Random Numbers under to seed the instability: seeding_velocities = 0.01 * (af.randu(1, 1, q1.shape[2], q1.shape[3], dtype = af.Dtype.f64 ) - 0.5 ) v1_bulk = af.select(af.abs(q2)>0.25, -0.5 - seeding_velocities, +0.5 + seeding_velocities ) v2_bulk = seeding_velocities T = (2.5 / n) f = n * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (v1 - v1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (v2 - v2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * v3**2 / (2 * k * T)) af.eval(f) return (f)
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass k = params.boltzmann_constant q2_minus = 0.5 q2_plus = 1.5 regulator = 20 # larger value makes the transition sharper rho = 1 + 0.5 * ( af.tanh(( q2 - q2_minus)*regulator) - af.tanh(( q2 - q2_plus )*regulator) ) p1_bulk = ( af.tanh(( q2 - q2_minus)*regulator) - af.tanh(( q2 - q2_plus )*regulator) - 1 ) p2_bulk = 0.5 * af.sin(2*np.pi*q1) *\ ( af.exp(-25 * (q2 - q2_minus)**2) + af.exp(-25 * (q2 - q2_plus )**2) ) T = (10 / rho) f = rho * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p3)**2 / (2 * k * T)) af.eval(f) return (f)
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass_particle k = params.boltzmann_constant pert_real = params.pert_real pert_imag = params.pert_imag k_q1 = params.k_q1 k_q2 = params.k_q2 # Calculating the perturbed density: rho = 1 + (pert_real * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag * af.sin(k_q1 * q1 + k_q2 * q2)) n_p = 0.9 / np.sqrt(2 * np.pi) n_b = 0.2 / np.sqrt(2 * np.pi) f = rho \ * ( n_p * af.exp(-0.5*p1**2) + n_b * af.exp(-0.5*((p1 - 4.5)/0.5)**2) ) af.eval(f) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant n_b = params.density_background T_b1 = params.temperature_background_1 T_b2 = params.temperature_background_2 T_b3 = params.temperature_background_3 k = params.boltzmann_constant n = n_b + params.amplitude * af.cos(params.k_q1 * q1) f1 = n * (m[0, 0] / (2 * np.pi * k * T_b1))**(1 / 2) \ * af.exp(-m[0, 0] * v1[:, 0]**2 / (2 * k * T_b1)) f2 = n * (m[0, 1] / (2 * np.pi * k * T_b2))**(1 / 2) \ * af.exp(-m[0, 1] * v1[:, 1]**2 / (2 * k * T_b2)) f3 = n * (m[0, 2] / (2 * np.pi * k * T_b3))**(1 / 2) \ * af.exp(-m[0, 2] * v1[:, 2]**2 / (2 * k * T_b3)) f = af.join(1, f1, f2, f3) af.eval(f) return (f)
def linear(self, signal_x, signal_y, Disper_cd): signal_x_fft = af.fft(signal_x) * af.exp(Disper_cd / 2) signal_x = af.ifft(signal_x_fft) signal_y_fft = af.fft(signal_y) * af.exp(Disper_cd / 2) signal_y = af.ifft(signal_y_fft) return signal_x, signal_y
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass_particle k = params.boltzmann_constant rho = af.select(af.abs(q2) > 0.25, q1**0, 2) # Seeding the instability p1_bulk = af.reorder(p1_bulk, 1, 2, 0, 3) p1_bulk += af.to_array( np.random.rand(1, q1.shape[1], q1.shape[2]) * np.random.choice([-1, 1], size=(1, q1.shape[1], q1.shape[2]))) * 0.005 p2_bulk = af.to_array( np.random.rand(1, q1.shape[1], q1.shape[2]) * np.random.choice([-1, 1], size=(1, q1.shape[1], q1.shape[2]))) * 0.005 p1_bulk = af.reorder(p1_bulk, 2, 0, 1, 3) p2_bulk = af.reorder(p2_bulk, 2, 0, 1, 3) T = (2.5 / rho) f = rho * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p3)**2 / (2 * k * T)) af.eval(f) return (f)
def f_left(f, t, q1, q2, p1, p2, p3, params): k = params.boltzmann_constant E_upper = params.E_band T = params.initial_temperature mu = params.initial_mu t = params.current_time omega = 2. * np.pi * params.AC_freq vel_drift_x_in = params.vel_drift_x_in if (params.p_space_grid == 'cartesian'): p_x = p1 p_y = p2 elif (params.p_space_grid == 'polar2D'): p_x = p1 * af.cos(p2) p_y = p1 * af.sin(p2) else: raise NotImplementedError('Unsupported coordinate system in p_space') fermi_dirac_in = (1. / (af.exp( (E_upper - vel_drift_x_in * p_x - mu) / (k * T)) + 1.)) if params.zero_temperature: fermi_dirac_in = fermi_dirac_in - 0.5 if (params.contact_geometry == "straight"): # Contacts on either side of the device q2_contact_start = params.contact_start q2_contact_end = params.contact_end cond = ((params.y >= q2_contact_start) & \ (params.y <= q2_contact_end) \ ) cond_2 = ((params.y >= 0.) & \ (params.y <= 1.) \ ) print("boundaries.py : ") f_left = cond * fermi_dirac_in + (1 - cond) * f elif (params.contact_geometry == "turn_around"): # Contacts on the same side of the device vel_drift_x_out = -params.vel_drift_x_in * np.sin(omega * t) fermi_dirac_out = (1. / (af.exp( (E_upper - vel_drift_x_out * p_x - mu) / (k * T)) + 1.)) # TODO: set these parameters in params.py cond_in = ((q2 >= 3.5) & (q2 <= 4.5)) cond_out = ((q2 >= 5.5) & (q2 <= 6.5)) f_left = cond_in*fermi_dirac_in + cond_out*fermi_dirac_out \ + (1 - cond_in)*(1 - cond_out)*f af.eval(f_left) return (f_left)
def initialize_f(q1, q2, p1, p2, p3, params): m = params.mass_particle k = params.boltzmann_constant rho = af.select(af.abs(q2) > 0.25, q1**0, 2) try: p1_bulk = af.select( af.abs(q2) > 0.25, -0.5 - 0.01 * (af.randu(1, q1.shape[1], q2.shape[2], dtype=af.Dtype.f64) - 0.5), +0.5 + 0.01 * (af.randu(1, q1.shape[1], q2.shape[2], dtype=af.Dtype.f64) - 0.5)) p2_bulk = 0.01 * ( af.randu(1, q1.shape[1], q2.shape[2], dtype=af.Dtype.f64) - 0.5) except: p1_bulk = af.select( af.abs(q2) > 0.25, -0.5 - 0.01 * (af.randu(q1.shape[0], q2.shape[1], dtype=af.Dtype.f64) - 0.5), +0.5 + 0.01 * (af.randu(q1.shape[0], q2.shape[1], dtype=af.Dtype.f64) - 0.5)) p2_bulk = 0.01 * ( af.randu(q1.shape[0], q2.shape[1], dtype=af.Dtype.f64) - 0.5) T = (2.5 / rho) f = rho * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (p1 - p1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (p2 - p2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * p3**2 / (2 * k * T)) af.eval(f) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant n_b = params.density_background T_b = params.temperature_background k = params.boltzmann_constant v1_bulk = 0 # Assigning separate bulk velocities v2_bulk = params.amplitude * -1.7450858652952794e-15 * af.cos(params.k_q1 * q1) \ - params.amplitude * 0.5123323181646575 * af.sin(params.k_q1 * q1) v3_bulk = params.amplitude * 0.5123323181646597 * af.cos(params.k_q1 * q1) \ - params.amplitude * 0 * af.sin(params.k_q1 * q1) n = n_b + 0 * q1**0 f = n * (m / (2 * np.pi * k * T_b)) \ * af.exp(-m * (v2 - v2_bulk)**2 / (2 * k * T_b)) \ * af.exp(-m * (v3 - v3_bulk)**2 / (2 * k * T_b)) af.eval(f) return (f)
def initialize_f(r, theta, rdot, thetadot, phidot, params): # Using a transformation to get the coordinates in the form used in regular cartesian coordinates: q1 = r * af.cos(theta) q2 = r * af.sin(theta) p1 = rdot * af.cos(theta) - r * af.sin(theta) * thetadot p2 = rdot * af.sin(theta) + r * af.cos(theta) * thetadot q10 = params.q10 q20 = params.q20 p10 = params.p10 p20 = params.p20 sigma_q = params.sigma_q sigma_p = params.sigma_p q_profile = (1 / sigma_q**2 / (2 * np.pi)) * \ af.exp(-0.5 * ((q1 - q10)**2 + (q2 - q20)**2) / sigma_q**2) p_profile = (1 / sigma_p**2 / (2 * np.pi)) * \ af.exp(-0.5 * ((p1 - p10)**2 + (p2 - p20)**2) / sigma_p**2) f = q_profile * p_profile af.eval(f) return (f)
def linear_prop_arrayfire(xpol, ypol, length, D): xpol_fft = af.fft(xpol) ypol_fft = af.fft(ypol) xpol_fft = xpol_fft * af.exp(D * length) ypol_fft = ypol_fft * af.exp(D * length) xpol = af.ifft(xpol_fft) ypol = af.ifft(ypol_fft) return xpol, ypol
def initialize_E(q1, q2, params): E1 = af.exp(-100 * (q2 - 0.5)**2) E2 = af.exp(-100 * (q1 - 0.5)**2) E3 = 0 * q1**0 af.eval(E1, E2, E3) return (E1, E2, E3)
def maxwell_boltzmann(rho, T, p1_b, p2_b, p3_b, p1, p2, p3): f = rho * (1 / (2 * np.pi * T))**(3 / 2) \ * af.exp(-1 * (p1 - p1_b)**2 / (2 * T)) \ * af.exp(-1 * (p2 - p2_b)**2 / (2 * T)) \ * af.exp(-1 * (p3 - p3_b)**2 / (2 * T)) af.eval(f) return (f)
def test_initialize(): obj = test() initialize(obj, params) f_background_ana = (1 / (2 * np.pi))**(3 / 2) \ * af.exp(-0.5 * obj.p1**2) \ * af.exp(-0.5 * obj.p2**2) \ * af.exp(-0.5 * obj.p3**2) assert (af.sum(af.abs(obj.f_background - f_background_ana)) < 1e-13)
def f_bottom(f, t, q1, q2, p1, p2, p3, params): k = params.boltzmann_constant E_upper = params.E_band T = params.initial_temperature mu = params.initial_mu t = params.current_time omega = 2. * np.pi * params.AC_freq q1_contact_start = params.contact_start q1_contact_end = params.contact_end contact_width = q1_contact_end - q1_contact_start q1_contact_start_2 = 0. q1_contact_end_2 = 1.73 / 2 contact_width_2 = q1_contact_end_2 - q1_contact_start_2 vel_drift_y_out = -params.vel_drift_x_out / contact_width vel_drift_y_out_2 = 10. * params.vel_drift_x_out / contact_width_2 if (params.p_space_grid == 'cartesian'): p_x = p1 p_y = p2 elif (params.p_space_grid == 'polar2D'): p_x = p1 * af.cos(p2) p_y = p1 * af.sin(p2) else: raise NotImplementedError('Unsupported coordinate system in p_space') fermi_dirac_out = (1. / (af.exp( (E_upper - vel_drift_y_out * p_y - mu) / (k * T)) + 1.)) fermi_dirac_out_2 = (1. / (af.exp( (E_upper - vel_drift_y_out_2 * p_y - mu) / (k * T)) + 1.)) if (params.contact_geometry == "straight"): # Contacts on either side of the device cond = ((q1 >= q1_contact_start) & \ (q1 <= q1_contact_end) \ ) cond_2 = ((q1 >= q1_contact_start_2) & \ (q1 <= q1_contact_end_2) \ ) f_right = cond * fermi_dirac_out + cond_2 * fermi_dirac_out_2 + ( 1 - cond_2) * (1 - cond) * f elif (params.contact_geometry == "turn_around"): # Contacts on the same side of the device f_right = f af.eval(f_right) return (f_right)
def initialize_f(q1, q2, v1, v2, v3, params): m = params.mass k = params.boltzmann_constant n_b = params.density_background T_b = params.temperature_background v1_bulk_b = params.v1_bulk_background v2_bulk_b = params.v2_bulk_background v3_bulk_b = params.v3_bulk_background pert_real_n = 1 pert_imag_n = 0 pert_real_v1 = -np.sqrt(params.gamma * T_b) / n_b \ * params.k_q1 / np.sqrt(params.k_q1**2 + params.k_q2**2) * 1j pert_imag_v1 = 0 pert_real_v2 = -np.sqrt(params.gamma * T_b) / n_b \ * params.k_q2 / np.sqrt(params.k_q1**2 + params.k_q2**2) * 1j pert_imag_v2 = 0 pert_real_T = T_b * (params.gamma - 1) / n_b pert_imag_T = 0 k_q1 = params.k_q1 k_q2 = params.k_q2 amp = params.amplitude # Introducing the perturbation amounts: # This is obtained from the Sage Worksheet(https://goo.gl/Sh8Nqt): # Plugging in the value from the Eigenvectors: # Calculating the perturbed density: n = n_b + amp * (pert_real_n * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag_n * af.sin(k_q1 * q1 + k_q2 * q2)) # Calculating the perturbed bulk velocities: v1_bulk = v1_bulk_b + amp * (pert_real_v1 * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag_v1 * af.sin(k_q1 * q1 + k_q2 * q2)) v2_bulk = v2_bulk_b + amp * (pert_real_v2 * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag_v2 * af.sin(k_q1 * q1 + k_q2 * q2)) v3_bulk = v3_bulk_b # Calculating the perturbed temperature: T = T_b + amp * (pert_real_T * af.cos(k_q1 * q1 + k_q2 * q2) - pert_imag_T * af.sin(k_q1 * q1 + k_q2 * q2)) f = n * (m / (2 * np.pi * k * T))**(3 / 2) \ * af.exp(-m * (v1 - v1_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (v2 - v2_bulk)**2 / (2 * k * T)) \ * af.exp(-m * (v3 - v3_bulk)**2 / (2 * k * T)) af.eval(f) return (f)
def initialize_B(q1, q2, params): dt = params.dt B1 = 0 * q1**0 B2 = 0 * q1**0 # B3 at n + 1/2 B3 = af.exp(-100 * (q2 - 0.5 - 0.5 * dt)**2) \ - af.exp(-100 * (q1 - 0.5 - 0.5 * dt)**2) af.eval(B1, B2, B3) return (B1, B2, B3)
def MB_dist(q1, q2, p1, p2, p3, params): # Calculating the perturbed density: rho = 1 + (0.01 * af.cos(2 * np.pi * q1 + 4 * np.pi * q2)) f = rho * (1 / (2 * np.pi))**(3 / 2) \ * af.exp(-0.5 * p1**2) \ * af.exp(-0.5 * p2**2) \ * af.exp(-0.5 * p3**2) af.eval(f) return (f)
def __init__(self): self.physical_system = type('obj', (object, ), { 'moment_exponents': moment_exponents, 'moment_coeffs': moment_coeffs }) self.p1_start = -10 self.p2_start = -10 self.p3_start = -10 self.N_p1 = 32 self.N_p2 = 32 self.N_p3 = 32 self.dp1 = (-2 * self.p1_start) / self.N_p1 self.dp2 = (-2 * self.p2_start) / self.N_p2 self.dp3 = (-2 * self.p3_start) / self.N_p3 self.N_q1 = 16 self.N_q2 = 16 self.N_ghost = 3 self.p1 = self.p1_start + (0.5 + np.arange(self.N_p1)) * self.dp1 self.p2 = self.p2_start + (0.5 + np.arange(self.N_p2)) * self.dp2 self.p3 = self.p3_start + (0.5 + np.arange(self.N_p3)) * self.dp3 self.p2, self.p1, self.p3 = np.meshgrid(self.p2, self.p1, self.p3) self.p1 = af.flat(af.to_array(self.p1)) self.p2 = af.flat(af.to_array(self.p2)) self.p3 = af.flat(af.to_array(self.p3)) self.q1 = (-self.N_ghost + 0.5 + np.arange(self.N_q1 + 2 * self.N_ghost)) / self.N_q1 self.q2 = (-self.N_ghost + 0.5 + np.arange(self.N_q2 + 2 * self.N_ghost)) / self.N_q2 self.q2, self.q1 = np.meshgrid(self.q2, self.q1) self.q1 = af.reorder(af.to_array(self.q1), 2, 0, 1) self.q2 = af.reorder(af.to_array(self.q2), 2, 0, 1) rho = (1 + 0.01 * af.sin(2 * np.pi * self.q1 + 4 * np.pi * self.q2)) T = (1 + 0.01 * af.cos(2 * np.pi * self.q1 + 4 * np.pi * self.q2)) p1_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2) p2_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2) p3_b = 0.01 * af.exp(-10 * self.q1**2 - 10 * self.q2**2) self.f = maxwell_boltzmann(rho, T, p1_b, p2_b, p3_b, self.p1, self.p2, self.p3)
def f_left(f, t, q1, q2, p1, p2, p3, params): rho = 1 * q1**0 T = 1 * q1**0 m = params.mass k = params.boltzmann_constant f = rho * af.sqrt(m / (2 * np.pi * k * T))**3 \ * af.exp(-m * p1**2 / (2 * k * T)) \ * af.exp(-m * p2**2 / (2 * k * T)) \ * af.exp(-m * p3**2 / (2 * k * T)) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): k = params.boltzmann_constant n_left = params.n_left v1_bulk_left = params.v1_bulk_left T_left = params.T_left f = q1**0 * n_left * (params.mass / (2 * np.pi * k * T_left))**(3 / 2) \ * af.exp(-params.mass * (v1 - v1_bulk_left)**2 / (2 * k * T_left)) \ * af.exp(-params.mass * v2**2 / (2 * k * T_left)) \ * af.exp(-params.mass * v3**2 / (2 * k * T_left)) af.eval(f) return (f)
def f_right(f, q1, q2, p1, p2, p3, params): rho = 0.125 * q1**0 T = 0.8 * q1**0 m = params.mass_particle k = params.boltzmann_constant f = rho * af.sqrt(m / (2 * np.pi * k * T))**3 \ * af.exp(-m * p1**2 / (2 * k * T)) \ * af.exp(-m * p2**2 / (2 * k * T)) \ * af.exp(-m * p3**2 / (2 * k * T)) return (f)
def MB_dist(q1, q2, p1, p2, p3, p_dim): # Calculating the perturbed density: rho = 1 + 0.01 * af.cos(2 * np.pi * q1) T = 1 + 0.01 * af.cos(2 * np.pi * q1) p1b = 0.01 * af.cos(2 * np.pi * q1) f = rho * (1 / (2 * np.pi * T))**(p_dim / 2) \ * af.exp(-0.5 * (p1-p1b)**2/T) \ * af.exp(-0.5 * p2**2/T) \ * af.exp(-0.5 * p3**2/T) af.eval(f) return (f)
def f_right(f, q1, q2, p1, p2, p3, params): rho = 2 / 3 * q1**0 T = 1 / 4 * q1**0 p1_b = 1.5 * q1**0 m = params.mass_particle k = params.boltzmann_constant f = rho * af.sqrt(m / (2 * np.pi * k * T))**3 \ * af.exp(-m * (p1-p1_b)**2 / (2 * k * T)) \ * af.exp(-m * p2**2 / (2 * k * T)) \ * af.exp(-m * p3**2 / (2 * k * T)) return (f)
def initialize_f(q1, q2, v1, v2, v3, params): m_e = params.mass[0, 0] m_i = params.mass[0, 1] k = params.boltzmann_constant n = params.n_background * q1**0 u_be = params.u_be u_bi = params.u_bi T = params.T_background f_e = n * (m_e / (2 * np.pi * k * T))**(3 / 2) \ * 0.5 * ( af.exp(-m_e * (v1[:, 0] - u_be)**2 / (2 * k * T)) + af.exp(-m_e * (v1[:, 0] + u_be)**2 / (2 * k * T)) ) \ * af.exp(-m_e * v2[:, 0]**2 / (2 * k * T)) \ * af.exp(-m_e * v3[:, 0]**2 / (2 * k * T)) f_i = n * (m_i / (2 * np.pi * k * T))**(3 / 2) \ * 0.5 * ( af.exp(-m_i * (v1[:, 1] - u_bi)**2 / (2 * k * T)) + af.exp(-m_i * (v1[:, 1] + u_bi)**2 / (2 * k * T)) ) \ * af.exp(-m_i * v2[:, 1]**2 / (2 * k * T)) \ * af.exp(-m_i * v3[:, 1]**2 / (2 * k * T)) f = af.join(1, f_e, f_i) af.eval(f) return (f)
def test_compute_moments(): obj = test() rho_num = compute_moments(obj, 'density') rho_ana = 1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center) error_rho = af.mean(af.abs(rho_num - rho_ana)) E_num = compute_moments(obj, 'energy') E_ana = 3/2 * (1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ * (1 + 0.01 * af.cos(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ + 3/2 * (1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ * (0.01 * af.exp(-10 * obj.q1_center**2 - 10 * obj.q2_center**2))**2 error_E = af.mean(af.abs(E_num - E_ana)) mom_p1b_num = compute_moments(obj, 'mom_v1_bulk') mom_p1b_ana = (1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ * (0.01 * af.exp(-10 * obj.q1_center**2 - 10 * obj.q2_center**2)) error_p1b = af.mean(af.abs(mom_p1b_num - mom_p1b_ana)) mom_p2b_num = compute_moments(obj, 'mom_v2_bulk') mom_p2b_ana = (1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ * (0.01 * af.exp(-10 * obj.q1_center**2 - 10 * obj.q2_center**2)) error_p2b = af.mean(af.abs(mom_p2b_num - mom_p2b_ana)) mom_p3b_num = compute_moments(obj, 'mom_v3_bulk') mom_p3b_ana = (1 + 0.01 * af.sin(2 * np.pi * obj.q1_center + 4 * np.pi * obj.q2_center)) \ * (0.01 * af.exp(-10 * obj.q1_center**2 - 10 * obj.q2_center**2)) error_p3b = af.mean(af.abs(mom_p3b_num - mom_p3b_ana)) print(error_rho) print(error_E) print(error_p1b) print(error_p2b) print(error_p3b) # print((error_rho + error_E + error_p1b + error_p2b + error_p3b) / 5) assert (error_rho < 1e-13) assert (error_E < 1e-13) assert (error_p1b < 1e-13) assert (error_p2b < 1e-13) assert (error_p3b < 1e-13)
def main(): T = 1 nT = 20 * T R_first = 1000 R = 5000000 x0 = 0 # initial log stock price v0 = 0.087**2 # initial volatility r = math.log(1.0319) # risk-free rate rho = -0.82 # instantaneous correlation between Brownian motions sigmaV = 0.14 # variance of volatility kappa = 3.46 # mean reversion speed vBar = 0.008 # mean variance k = math.log(0.95) # strike price # first run ( x, v ) = simulateHestonModel( T, nT, R_first, r, kappa, vBar, sigmaV, rho, x0, v0 ) # Price plain vanilla call option tic = time.time() ( x, v ) = simulateHestonModel( T, nT, R, r, kappa, vBar, sigmaV, rho, x0, v0 ) af.sync() toc = time.time() - tic K = math.exp(k) zeroConstant = af.constant(0, R, dtype=af.Dtype.f32) C_CPU = math.exp(-r * T) * af.mean(af.maxof(af.exp(x) - K, zeroConstant)) print("Time elapsed = {} secs".format(toc)) print("Call price = {}".format(C_CPU)) print(af.mean(v))
def black_scholes(S, X, R, V, T): # S = Underlying stock price # X = Strike Price # R = Risk free rate of interest # V = Volatility # T = Time to maturity d1 = af.log(S / X) d1 = d1 + (R + (V * V) * 0.5) * T d1 = d1 / (V * af.sqrt(T)) d2 = d1 - (V * af.sqrt(T)) cnd_d1 = cnd(d1) cnd_d2 = cnd(d2) C = S * cnd_d1 - (X * af.exp((-R) * T) * cnd_d2) P = X * af.exp((-R) * T) * (1 - cnd_d2) - (S * (1 -cnd_d1)) return (C, P)
def monte_carlo_options(N, K, t, vol, r, strike, steps, use_barrier = True, B = None, ty = af.Dtype.f32): payoff = af.constant(0, N, 1, dtype = ty) dt = t / float(steps - 1) s = af.constant(strike, N, 1, dtype = ty) randmat = af.randn(N, steps - 1, dtype = ty) randmat = af.exp((r - (vol * vol * 0.5)) * dt + vol * math.sqrt(dt) * randmat); S = af.product(af.join(1, s, randmat), 1) if (use_barrier): S = S * af.all_true(S < B, 1) payoff = af.maxof(0, S - K) return af.mean(payoff) * math.exp(-r * t)
def sigtest(x): return 1 / (1 + af.exp(-x))
af.display(af.conjg(d)) af.display(af.sinh(a)) af.display(af.cosh(a)) af.display(af.tanh(a)) af.display(af.asinh(a)) af.display(af.acosh(a)) af.display(af.atanh(a)) a = af.abs(a) b = af.abs(b) af.display(af.root(a, b)) af.display(af.pow(a, b)) af.display(af.pow2(a)) af.display(af.exp(a)) af.display(af.expm1(a)) af.display(af.erf(a)) af.display(af.erfc(a)) af.display(af.log(a)) af.display(af.log1p(a)) af.display(af.log10(a)) af.display(af.log2(a)) af.display(af.sqrt(a)) af.display(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1) af.display(af.factorial(a)) af.display(af.tgamma(a))
def simple_arith(verbose = False): display_func = _util.display_func(verbose) print_func = _util.print_func(verbose) a = af.randu(3,3,dtype=af.Dtype.u32) b = af.constant(4, 3, 3, dtype=af.Dtype.u32) display_func(a) display_func(b) c = a + b d = a d += b display_func(c) display_func(d) display_func(a + 2) display_func(3 + a) c = a - b d = a d -= b display_func(c) display_func(d) display_func(a - 2) display_func(3 - a) c = a * b d = a d *= b display_func(c * 2) display_func(3 * d) display_func(a * 2) display_func(3 * a) c = a / b d = a d /= b display_func(c / 2.0) display_func(3.0 / d) display_func(a / 2) display_func(3 / a) c = a % b d = a d %= b display_func(c % 2.0) display_func(3.0 % d) display_func(a % 2) display_func(3 % a) c = a ** b d = a d **= b display_func(c ** 2.0) display_func(3.0 ** d) display_func(a ** 2) display_func(3 ** a) display_func(a < b) display_func(a < 0.5) display_func(0.5 < a) display_func(a <= b) display_func(a <= 0.5) display_func(0.5 <= a) display_func(a > b) display_func(a > 0.5) display_func(0.5 > a) display_func(a >= b) display_func(a >= 0.5) display_func(0.5 >= a) display_func(a != b) display_func(a != 0.5) display_func(0.5 != a) display_func(a == b) display_func(a == 0.5) display_func(0.5 == a) display_func(a & b) display_func(a & 2) c = a c &= 2 display_func(c) display_func(a | b) display_func(a | 2) c = a c |= 2 display_func(c) display_func(a >> b) display_func(a >> 2) c = a c >>= 2 display_func(c) display_func(a << b) display_func(a << 2) c = a c <<= 2 display_func(c) display_func(-a) display_func(+a) display_func(~a) display_func(a) display_func(af.cast(a, af.Dtype.c32)) display_func(af.maxof(a,b)) display_func(af.minof(a,b)) display_func(af.rem(a,b)) a = af.randu(3,3) - 0.5 b = af.randu(3,3) - 0.5 display_func(af.abs(a)) display_func(af.arg(a)) display_func(af.sign(a)) display_func(af.round(a)) display_func(af.trunc(a)) display_func(af.floor(a)) display_func(af.ceil(a)) display_func(af.hypot(a, b)) display_func(af.sin(a)) display_func(af.cos(a)) display_func(af.tan(a)) display_func(af.asin(a)) display_func(af.acos(a)) display_func(af.atan(a)) display_func(af.atan2(a, b)) c = af.cplx(a) d = af.cplx(a,b) display_func(c) display_func(d) display_func(af.real(d)) display_func(af.imag(d)) display_func(af.conjg(d)) display_func(af.sinh(a)) display_func(af.cosh(a)) display_func(af.tanh(a)) display_func(af.asinh(a)) display_func(af.acosh(a)) display_func(af.atanh(a)) a = af.abs(a) b = af.abs(b) display_func(af.root(a, b)) display_func(af.pow(a, b)) display_func(af.pow2(a)) display_func(af.exp(a)) display_func(af.expm1(a)) display_func(af.erf(a)) display_func(af.erfc(a)) display_func(af.log(a)) display_func(af.log1p(a)) display_func(af.log10(a)) display_func(af.log2(a)) display_func(af.sqrt(a)) display_func(af.cbrt(a)) a = af.round(5 * af.randu(3,3) - 1) b = af.round(5 * af.randu(3,3) - 1) display_func(af.factorial(a)) display_func(af.tgamma(a)) display_func(af.lgamma(a)) display_func(af.iszero(a)) display_func(af.isinf(a/b)) display_func(af.isnan(a/a)) a = af.randu(5, 1) b = af.randu(1, 5) c = af.broadcast(lambda x,y: x+y, a, b) display_func(a) display_func(b) display_func(c) @af.broadcast def test_add(aa, bb): return aa + bb display_func(test_add(a, b))
def _sigmoid(x): return 1 / (1 + af.exp(-x))
def exp(x): if isinstance(x, afnumpy.ndarray): s = arrayfire.exp(x.d_array) return afnumpy.ndarray(x.shape, dtype=pu.typemap(s.dtype()), af_array=s) else: return numpy.exp(x)