From 054477fbae4ac63e75344e32c7101936a38f6219 Mon Sep 17 00:00:00 2001 From: schoener Date: Wed, 20 Oct 2021 18:27:47 +0200 Subject: [PATCH] Updated B_tot_along_axis B_multiple_3d has error --- src/coil_class.py | 154 ++++++++++++++++++++++------------------------ 1 file changed, 73 insertions(+), 81 deletions(-) diff --git a/src/coil_class.py b/src/coil_class.py index 917537f..21a719d 100644 --- a/src/coil_class.py +++ b/src/coil_class.py @@ -281,14 +281,10 @@ class BCoil: HH = +1 --> Helmholtz configuration, HH = -1 --> Anti Helmholtz configuration """ - # z_start = self.get_zmin() + self.wire_height / 2 # (distance_coils/2 - windings * wire_height/2 + wire_height/2)*1e-3 - # R_start = self.get_R_inner() + self.wire_width / 2 # (R_inner + wire_width/2 )*1e-3 - - # Convert axis to SI units x_SI = x * 1e-3 z_SI = z * 1e-3 - if x[0] <= 0: + if x[0] <= 0: # divide array into positive and negative values x_neg = np.abs([el for el in x_SI if el < 0]) x_pos = np.array([el for el in x_SI if el > 0]) x_zero = np.array([el for el in x_SI if el == 0]) @@ -306,7 +302,7 @@ class BCoil: rastering_value = len(calc_raster[0]) I_current /= rastering_value # divide current into smaller currents for mapping the whole wire - start = time.time() + # start = time.time() for wire in range(0, self.get_N()): for ii in range(0, rastering_value): # extract position information out of raster @@ -322,8 +318,8 @@ class BCoil: B_x_neg *= -1 # B_x_neg is pointing in opposite x_direction B_x = np.concatenate((B_x_neg, B_x_zero, B_x_pos)) - end = time.time() - print(f"time = {end - start} s") + # end = time.time() + # print(f"time = {end - start} s") return B_z, B_x def B_tot_along_axis(self, I_current, x, z, raster=10): @@ -333,9 +329,6 @@ class BCoil: HH = +1 --> Helmholtz configuration, HH = -1 --> Anti Helmholtz configuration """ - z_start = self.get_zmin() + self.wire_height / 2 # (distance_coils/2 - windings * wire_height/2 + wire_height/2)*1e-3 - R_start = self.get_R_inner() + self.wire_width / 2 # (R_inner + wire_width/2 )*1e-3 - # Convert axis to SI units x_SI = x * 1e-3 z_SI = z * 1e-3 @@ -352,43 +345,40 @@ class BCoil: B_z_x = np.zeros(len(x_SI)) # dividing each wire into 10 x 10 raster + calc_raster = self.full_raster(raster) - I_current /= raster ** 2 + if self.get_N() != len(calc_raster): + log.error("N is not equal length of raster") - for xx in range(0, self.layers): - for zz in range(0, self.windings): - for xx_in in range(0, raster): - for zz_in in range(0, raster): - z_pos = z_start + zz * ( - self.wire_height + self.insulation_thickness * 2) - self.wire_height / 2 + zz_in * self.wire_height / ( - raster - 1) - R_pos = R_start + xx * ( - self.wire_width + self.insulation_thickness * 2) - self.wire_width / 2 + xx_in * self.wire_width / ( - raster - 1) - # TODO: fix z_pos, R_pos, either by raster or by spacing + rastering_value = len(calc_raster[0]) - # z-field along z-axis (x-Field always zero) - B_tot_z += BCoil.B_z_loop(I_current, R_pos, z_pos, 0, z_SI) + BCoil.B_z_loop( - self.HH * I_current, R_pos, -z_pos, 0, z_SI) + I_current /= rastering_value # divide current into smaller currents for mapping the whole wire + # start = time.time() + for wire in range(0, self.get_N()): + for ii in range(0, rastering_value): + # extract position information out of raster + z_pos = calc_raster[wire, ii, 0] + r_pos = calc_raster[wire, ii, 1] - # x-field along x-axis - B_x_pos += BCoil.B_r_loop(I_current, R_pos, z_pos, x_pos, 0) + BCoil.B_r_loop( - self.HH * I_current, R_pos, -z_pos, x_pos, 0) - B_x_neg += BCoil.B_r_loop(I_current, R_pos, z_pos, x_neg, 0) + BCoil.B_r_loop( - self.HH * I_current, R_pos, -z_pos, x_neg, 0) - # B_z along x-axis: - B_z_x += BCoil.B_z_loop(I_current, R_pos, z_pos, x_SI, 0) + BCoil.B_z_loop(self.HH * I_current, - R_pos, -z_pos, x_SI, - 0) + # z-field along z-axis (x-Field always zero) + B_tot_z += BCoil.B_z_loop(I_current, r_pos, z_pos, 0, z_SI) + BCoil.B_z_loop( + self.HH * I_current, r_pos, -z_pos, 0, z_SI) + + # x-field along x-axis + B_x_pos += BCoil.B_r_loop(I_current, r_pos, z_pos, x_pos, 0) + BCoil.B_r_loop( + self.HH * I_current, r_pos, -z_pos, x_pos, 0) + B_x_neg += BCoil.B_r_loop(I_current, r_pos, z_pos, x_neg, 0) + BCoil.B_r_loop( + self.HH * I_current, r_pos, -z_pos, x_neg, 0) + # B_z along x-axis: + B_z_x += BCoil.B_z_loop(I_current, r_pos, z_pos, x_SI, 0) + BCoil.B_z_loop(self.HH * I_current, + r_pos, -z_pos, x_SI, 0) B_x_neg *= -1 # B_x_neg is pointing in opposite x_direction # B_x_x = np.zeros(len(x_SI)) - - B_tot_x = np.sqrt(B_x_x ** 2 + B_z_x ** 2) - B_x_x = np.concatenate((B_x_neg, B_x_zero, B_x_pos)) - + B_tot_x = np.sqrt(B_x_x ** 2 + B_z_x ** 2) + B_tot_z = np.abs(B_tot_z) return B_tot_z, B_tot_x def B_multiple_3d(self, I_current, x, z, raster=4): @@ -400,38 +390,40 @@ class BCoil: B = np.zeros([len(z_SI), len(x_SI), 2]) - I_current /= raster ** 2 + calc_raster = self.full_raster(raster) - for el in range(0, len(x_SI)): - for xx in range(0, self.layers): - for zz in range(0, self.windings): - for xx_in in range(0, raster): - for zz_in in range(0, raster): - z_pos = z_start + zz * ( - self.wire_height + self.windings_spacing) - self.wire_height / 2 + zz_in * self.wire_height / ( - raster - 1) - R_pos = R_start + xx * ( - self.wire_width + self.layers_spacing) - self.wire_width / 2 + xx_in * self.wire_width / ( - raster - 1) - # TODO: fix z_pos, R_pos, either by raster or by spacing + if self.get_N() != len(calc_raster): + log.error("N is not equal length of raster") - # compute z-value of field - B[:, el, 1] += BCoil.B_z_loop(I_current, R_pos, z_pos, np.abs(x_SI[el]), - z_SI) + BCoil.B_z_loop(self.HH * I_current, R_pos, -z_pos, - np.abs(x_SI[el]), z_SI) + rastering_value = len(calc_raster[0]) + # TODO: why division by zero? - # compute x-value - if x_SI[el] < 0: - B[:, el, 0] -= BCoil.B_r_loop(I_current, R_pos, z_pos, -x_SI[el], - z_SI) + BCoil.B_r_loop(self.HH * I_current, R_pos, -z_pos, - -x_SI[el], z_SI) + I_current /= rastering_value # divide current into smaller currents for mapping the whole wire + # start = time.time() + for el in range(0,len(x_SI)): + for wire in range(0, self.get_N()): + for ii in range(0, rastering_value): + # extract position information out of raster + z_pos = calc_raster[wire, ii, 0] + r_pos = calc_raster[wire, ii, 1] - elif x_SI[el] > 0: - B[:, el, 0] += BCoil.B_r_loop(I_current, R_pos, z_pos, x_SI[el], z_SI) + BCoil.B_r_loop( - self.HH * I_current, R_pos, -z_pos, x_SI[el], z_SI) + # compute z-value of field + B[:, el, 1] += BCoil.B_z_loop(I_current, r_pos, z_pos, np.abs(x_SI[el]), + z_SI) + BCoil.B_z_loop(self.HH * I_current, r_pos, -z_pos, + np.abs(x_SI[el]), z_SI) - elif x_SI[el] == 0: - B[:, el, 0] = 0 + # compute x-value + if x_SI[el] < 0: + B[:, el, 0] -= BCoil.B_r_loop(I_current, r_pos, z_pos, -x_SI[el], + z_SI) + BCoil.B_r_loop(self.HH * I_current, r_pos, -z_pos, + -x_SI[el], z_SI) + + elif x_SI[el] > 0: + B[:, el, 0] += BCoil.B_r_loop(I_current, r_pos, z_pos, x_SI[el], z_SI) + BCoil.B_r_loop( + self.HH * I_current, r_pos, -z_pos, x_SI[el], z_SI) + + elif x_SI[el] == 0: + B[:, el, 0] = 0 return B @@ -598,22 +590,22 @@ class BCoil: def main(): - HH_Coil = BCoil(HH=-1, distance=50, radius=40, layers=4, windings=8, wire_height=0.4, wire_width=0.4, - insulation_thickness=0.1, is_round=True, winding_offset=False) - x = np.linspace(-50, 50, 200) - z = np.linspace(-50, 50, 200) + HH_Coil = BCoil(HH=1, distance=50, radius=40, layers=8, windings=8, wire_height=0.4, wire_width=0.4, + insulation_thickness=0.1, is_round=True, winding_offset=True) + x = np.linspace(-50, 50, 300) + z = np.linspace(-50, 50, 300) # Bz, Bx = HH_Coil.B_tot_along_axis(1.25, x, z) - Bz, Bx = HH_Coil.B_field(1.25, x, z) - print(Bx) - print(Bz) - plt.plot(x,Bx,label = "Bx") - plt.plot(z,Bz, label = "Bz") - plt.legend() - plt.show() - - HH_Coil.plot_raster(3) - ras = HH_Coil.full_raster(10) - + # Bz, Bx = HH_Coil.B_tot_along_axis(1.25, x, z) + # #print(Bx) + # #print(Bz) + # plt.plot(x,Bx,label = "Bx") + # plt.plot(z,Bz, label = "Bz") + # plt.legend() + # plt.show() + # + # HH_Coil.plot_raster(3) + # ras = HH_Coil.full_raster(10) + HH_Coil.plot_3d(1.25,50,50) main() # if __name__ == "__main__":