def rotate_by_pivot(p, pivot, angle): result = [None] * 2 result[0] = (p[0] - pivot[0]) * np.cos(angle) + ( p[1] - pivot[1]) * np.sin(angle) + pivot[0] result[1] = (p[0] - pivot[0]) * -np.sin(angle) + ( p[1] - pivot[1]) * np.cos(angle) + pivot[1] return result
def ltfree(lK, Rg, alpha, beta, delta, omegat): ltx = np.cos(alpha) * np.cos(np.radians(omegat) + delta) + np.cos( beta) * lK - np.cos(np.radians(omegat)) * Rg lty = np.sin(np.radians(omegat) + delta) - np.sin(np.radians(omegat)) * Rg ltz = -np.sin(alpha) * np.cos(np.radians(omegat) + delta) + np.sin(beta) * lK return np.sqrt(ltx * ltx + lty * lty + ltz * ltz)
def ek(alpha, betas): ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) return np.array([[ca * cbs, -sbs, sa * cbs], [ca * sbs, cbs, sa * sbs], [-sa, 0, ca]])
def Mgrz(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # eaxx = ekxx * cotd + ekyx * sotd eaxy = ekxy * cotd + ekyy * sotd eaxz = ekxz * cotd # ltx = eaxx + cb * lK - cot * Rg lty = eaxy - sot * Rg ltz = eaxz + sb * lK # vcz = cot * lty - sot * ltx # return vcz / (ltx * ekzx + lty * ekzy + ltz * ekzz)
def Ftry(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # ltx = ekxx * cotd + ekyx * sotd + cb * lK - cot * Rg lty = ekxy * cotd + ekyy * sotd - sot * Rg ltz = ekxz * cotd + sb * lK # return (ltx * ekyx + lty * ekyy + ltz * ekyz) / (ltx * ekzx + lty * ekzy + ltz * ekzz)
def rotate_arrow_and_text(BarNo): alpha = p*(BarNo-1)*alpha_c / 180 * np.pi x = x_ori* np.cos(alpha) + y_ori*-np.sin(alpha) y = x_ori* np.sin(alpha) + y_ori* np.cos(alpha) pu.pyx_arrow((x,y)) pu.pyx_text(( np.sign(x)*(abs(x)-1), np.sign(y)*(abs(y)-1) ), rf'${BarNo}$', scale=1.5) print(f'alpha={alpha/np.pi*180}')
def rA(lK, R, alpha, betas, beta, delta, omegat): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cotd = np.cos(np.radians(omegat) + delta) sotd = np.sin(np.radians(omegat) + delta) rAx = (ca * cbs * cotd - sbs * sotd) * R + cb * lK rAy = (ca * sbs * cotd + cbs * sotd) * R rAz = (-sa * cotd) * R + sb * lK return np.array([rAx, rAy, rAz])
def rB(R, omegat): cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) rBx = cot * R rBy = sot * R rBz = 0 return np.array([rBx, rBy, rBz])
def derive_mm_w_pm(GP, SD): GP["mm_w_pm"].value = ( GP['mm_r_os'].value - GP["mm_d_bg_air"].value - (GP['mm_r_ri'].value + GP['mm_d_ri'].value)) / np.cos( GP['deg_alpha_vspm'].value) - GP['mm_d_pm'].value * np.tan( GP['deg_alpha_vspm'].value) return GP["mm_w_pm"].value
def rK(lK, beta): cb = np.cos(beta) sb = np.sin(beta) rKx = cb * lK rKy = 0 rKz = sb * lK return np.array([rKx, rKy, rKz])
def cosgammag(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) ltx = np.cos(alpha)*cotd + cb*lK - cot*Rg lty = sotd - sot*Rg ltz = -np.sin(alpha)*cotd + sb*lK Rwx = cot*Rg Rwy = sot*Rg Rwz = 0 lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) return (Rwx*lty-Rwy*ltx)/(lt*Rg)
def cosgammak(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) Rax = ca*cotd Ray = sotd Raz = -sa*cotd ltx = Rax + cb*lK - cot*Rg lty = Ray - sot*Rg ltz = Raz + sb*lK lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) return (sa*(Ray*ltz-Raz*lty) + ca*(Rax*lty-Ray*ltx))/lt
def vt(lK, Rg, beta, delta, omegat): alpha = 0.5*np.pi-beta cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat)+delta) sotd = np.sin(np.radians(omegat)+delta) ltx = ca*cotd + cb*lK - cot*Rg lty = sotd - sot*Rg ltz = -sa*cotd + sb*lK lt = np.sqrt(ltx*ltx+lty*lty+ltz*ltz) dltxdt = -ca*sotd + sot*Rg dltydt = cotd - cot*Rg dltzdt = sa*sotd return (ltx*dltxdt+lty*dltydt+ltz*dltzdt)/lt
def wall(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] midpoint = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] midpoint = parms[0] x0 = midpoint - thick / 2.0 th = np.deg2rad(theta) p = w * np.cos(th) Atot = w * w / np.sin(2.0 * th) out = [] for x in xvals: if (x <= (x0 - p)): val = ib #elif ((x0-p) >= (x-thick)) and ((x0+p) >= x): #Gauge volume is smaller than thickness if ((x > (x0 - p)) and (x0 - p > (x - thick)) and (x <= x0)): val = ib + i0 * ((x - (x0 - p)) * ((x - (x0 - p)) * np.tan(th))) / Atot elif ((x > x0) and (x <= x0 + p) and (x0 - p > (x - thick))): val = ib + i0 * (Atot - (x0 + p - x) * (x0 + p - x) * np.tan(th)) / Atot elif (x0 - p > (x - thick) and (x0 + p < x)): #Gauge volume is smaller than thickness val = ib + i0 elif (x0 - p < (x - thick) and (x0 + p > x)): #Gauge volume is larger than thickness A2 = (x0 + p - x) * (x0 + p - x) * np.tan(th) A3 = ((x - thick) - (x0 - p)) * (((x - thick) - (x0 - p)) * np.tan(th)) val = ib + i0 * (Atot - A2 - A3) / Atot elif ((x0 - p) < (x - thick)) and ((x0 + p) < x) and (x0 >= (x - thick)): A3 = ((x - thick) - (x0 - p)) * (((x - thick) - (x0 - p)) * np.tan(th)) val = ib + i0 * (Atot - A3) / Atot elif (x0 < (x - thick) and ((x0 + p >= (x - thick)))): A2 = (x0 + p - (x - thick)) * ((x0 + p) - (x - thick)) * np.tan(th) val = ib + i0 * (A2 / Atot) elif (x0 + p < (x - thick)): val = ib #elif ((x0-p) < (x-thick)) and ((x0+p) >= x): #Gauge volume is larger than thickness # A2 = (x0+p-x)*(x0+p-x)*np.tan(th) # A3 = ((x-thick)-(x0-p))*(((x-thick)-(x0-p))*np.tan(th)) # val = ib + i0*(Atot-A2-A3)/Atot out = out + [val] return np.array(out)
def arc(r0, R, e, n, phi0, phi): e1 = e / np.sqrt(np.sum(e * e)) # normalize en = n / np.sqrt(np.sum(n * n)) # normalize ip = np.argmax(phi > phi0) # find end index e2 = np.cross(en, e1) cp = np.cos(np.radians(phi[:ip])) sp = np.sin(np.radians(phi[:ip])) # r = cp*e1+sp*e2 r = np.zeros((3, ip)) r[0, :] = r0[0] + R * (cp * e1[0] + sp * e2[0]) r[1, :] = r0[1] + R * (cp * e1[1] + sp * e2[1]) r[2, :] = r0[2] + R * (cp * e1[2] + sp * e2[2]) return r
def Marz(lK, Rg, alpha, betas, beta, delta, omegat, ot): cb = np.cos(beta) sb = np.sin(beta) ca = np.cos(alpha) sa = np.sin(alpha) cbs = np.cos(betas) sbs = np.sin(betas) cot = np.cos(np.radians(omegat) + ot) sot = np.sin(np.radians(omegat) + ot) cotd = np.cos(np.radians(omegat) + ot + delta) sotd = np.sin(np.radians(omegat) + ot + delta) # ekxx = ca * cbs ekxy = ca * sbs ekxz = -sa # ekyx = -sbs ekyy = cbs ekyz = 0 # ekzx = sa * cbs ekzy = sa * sbs ekzz = ca # eaxx = ekxx * cotd + ekyx * sotd eaxy = ekxy * cotd + ekyy * sotd eaxz = ekxz * cotd # ltx = eaxx + cb * lK - cot * Rg lty = eaxy - sot * Rg ltz = eaxz + sb * lK # vcx = eaxy * ltz - eaxz * lty vcy = eaxz * ltx - eaxx * ltz vcz = eaxx * lty - eaxy * ltx # d = ltx * ekzx + lty * ekzy + ltz * ekzz # return np.where(d > dmin, (vcx * ekzx + vcy * ekzy + vcz * ekzz) / d, 0.0)
def transformCoords(self, points, deg_addTheta=0): # This function takes in an nx2 array of coordinates of the form # [x,y] and returns rotated and translated coordinates. The # translation and rotation are described by obj.anchor_xy and # obj.theta. The optional "addTheta" argument adds an # additional angle of "addTheta" to the obj.theta attribute. transCoords = [] for point in points: # 旋转方向和eMach是反一下的,我是Park变换。 cosT = np.cos(self.theta + (deg_addTheta * np.pi / 180)) sinT = np.sin(self.theta + (deg_addTheta * np.pi / 180)) transCoords.append([ points[0] * cosT + points[1] * sinT, points[0] * -sinT + points[1] * cosT ]) return np.array(transCoords)
def transmission(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] #x0=p[0]; i0=p[1]; w=p[2]; theta=p[3]; ib=p[4]; thick=p[5]; u=p[6]; #th = np.deg2rad(theta) #p0 = w * np.cos(th)/np.cos(2*th) #out = [] #for x in xvals: # if x < (x0 - p0): # val = ib # elif ((x>=x0-p0) and (x < x0)): # val = i0*(x-x0+p0)*(x-x0+p0)+ib # elif ((x>=x0) and (x<(x0+p0))): # val = i0*(2.0*p0*p0-(x0+p0-x)*(x0+p0-x))+ib # elif (x>=x0+p0): # val=2*i0*p0*p0 +ib # out = out + [val] x0 = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] th = np.deg2rad(theta) p = w * np.cos(th) Atot = w * w / np.sin(2.0 * th) out = [] for x in xvals: if (x <= (x0 - p)): val = ib elif ((x > (x0 - p)) and (x <= x0)): val = ib + i0 * ((x - (x0 - p)) * ((x - (x0 - p)) * np.tan(th))) / Atot elif ((x > x0) and (x <= x0 + p)): val = ib + i0 * (Atot - (x0 + p - x) * (x0 + p - x) * np.tan(th)) / Atot elif (x > x0 + p): val = ib + i0 out = out + [val] return np.array(out)
def gammak2(lK, Rg, beta, delta, omegat): alpha = 0.5 * np.pi - beta cb = np.cos(beta) sb = np.sin(beta) cot = np.cos(np.radians(omegat)) sot = np.sin(np.radians(omegat)) cotd = np.cos(np.radians(omegat) + delta) sotd = np.sin(np.radians(omegat) + delta) cotdt = np.cos(np.radians(omegat + 90) + delta) sotdt = np.sin(np.radians(omegat + 90) + delta) ltx = np.cos(alpha) * cotd + cb * lK - cot * Rg lty = sotd - sot * Rg ltz = -np.sin(alpha) * cotd + sb * lK eyrx = np.cos(alpha) * cotdt eyry = sotdt eyrz = -np.sin(alpha) * cotdt lt = np.sqrt(ltx * ltx + lty * lty + ltz * ltz) return np.degrees(np.arccos((eyrx * ltx + eyry * lty + eyrz * ltz) / lt))
def iPark(P, theta): return [ P[0] * np.cos(theta) + P[1] * -np.sin(theta), P[0] * np.sin(theta) + P[1] * np.cos(theta) ]
def ModifiedBianchi2006(self, fea_config_dict, SD, GP, OP): air_gap_flux_density_B = SD['guess_air_gap_flux_density_B'] # air_gap_flux_density_B = 0.9 stator_tooth_flux_density_B_ds = SD[ 'guess_stator_tooth_flux_density_B_ds'] # stator_tooth_flux_density_B_ds = 1.5 stator_yoke_flux_density_Bys = SD['guess_stator_yoke_flux_density_Bys'] if SD['p'] >= 2: ROTOR_STATOR_YOKE_HEIGHT_RATIO = 0.75 alpha_rm_over_alpha_rp = 1.0 # stator_yoke_flux_density_Bys = 1.2 else: # penalty for p=1 motor, i.e., large yoke height ROTOR_STATOR_YOKE_HEIGHT_RATIO = 0.5 alpha_rm_over_alpha_rp = 0.75 # stator_yoke_flux_density_Bys = 1.5 stator_outer_diameter_Dse = 0.128 # m stator_outer_radius_r_os = 0.5 * stator_outer_diameter_Dse speed_rpm = SD['ExcitationFreqSimulated'] * 60 / SD['p'] # rpm split_ratio = 0.5 # refer to 2020-MLMS-0953@Fig. 5 stator_inner_radius_r_is = stator_outer_radius_r_os * split_ratio stator_inner_diameter_Dis = stator_inner_radius_r_is * 2 mm_sleeve_length = 0.5 mm_airgap_plus_sleeve_length = SD[ 'minimum_mechanical_air_gap_length_mm'] + mm_sleeve_length rotor_outer_radius_r_or = stator_inner_radius_r_is - mm_airgap_plus_sleeve_length * 1e-3 # m rotor_outer_diameter_Dr = rotor_outer_radius_r_or * 2 # Bianchi2006@(1)--(3) stator_yoke_height_h_ys = air_gap_flux_density_B * np.pi * stator_inner_diameter_Dis * alpha_rm_over_alpha_rp / ( 2 * stator_yoke_flux_density_Bys * 2 * SD['p']) stator_tooth_height_h_ds = ( stator_outer_diameter_Dse - stator_inner_diameter_Dis) / 2 - stator_yoke_height_h_ys stator_slot_height_h_ss = stator_tooth_height_h_ds stator_tooth_width_b_ds = air_gap_flux_density_B * np.pi * stator_inner_diameter_Dis / ( stator_tooth_flux_density_B_ds * SD['Qs']) # Bianchi2006@(4) OP['stator_slot_area'] = stator_slot_area = np.pi / (4 * SD['Qs']) * ( (stator_outer_diameter_Dse - 2 * stator_yoke_height_h_ys)**2 - stator_inner_diameter_Dis** 2) - stator_tooth_width_b_ds * stator_tooth_height_h_ds # Bianchi2006@(5) slot_pitch_pps = np.pi * (stator_inner_diameter_Dis + stator_slot_height_h_ss) / SD['Qs'] kov = 1.8 # \in [1.6, 2.0] OP['end_winding_length_Lew'] = end_winding_length_Lew = np.pi * 0.5 * ( slot_pitch_pps + stator_tooth_width_b_ds ) + slot_pitch_pps * kov * (SD['coil_pitch_y'] - 1) Q = SD['Qs'] p = SD['p'] # STATOR GP['deg_alpha_st'].value = 360 / Q - 2 # deg GP['deg_alpha_so'].value = GP[ 'deg_alpha_st'].value / 2 # im_template uses alpha_so as 0. GP['mm_r_si'].value = 1e3 * stator_inner_radius_r_is # mm GP['mm_r_os'].value = 1e3 * stator_outer_diameter_Dse / 2 # mm GP['mm_d_so'].value = 1 # mm GP['mm_d_sp'].value = 1.5 * GP['mm_d_so'].value GP['mm_d_st'].value = 1e3 * ( 0.5 * stator_outer_diameter_Dse - stator_yoke_height_h_ys ) - GP['mm_r_si'].value - GP['mm_d_sp'].value # mm GP['mm_d_sy'].value = 1e3 * stator_yoke_height_h_ys # mm GP['mm_w_st'].value = 1e3 * stator_tooth_width_b_ds # mm # ROTOR GP['mm_d_sleeve'].value = mm_sleeve_length GP['mm_d_fixed_air_gap'].value = SD[ 'minimum_mechanical_air_gap_length_mm'] GP['split_ratio'].value = split_ratio GP['mm_d_pm'].value = 4 # mm GP['mm_d_ri'].value = 1e3 * ROTOR_STATOR_YOKE_HEIGHT_RATIO * stator_yoke_height_h_ys # TODO:This ratio (0.75) is randomly specified GP['mm_r_or'].value = 1e3 * rotor_outer_radius_r_or GP['mm_r_ri'].value = 1e3 * stator_inner_radius_r_is - GP[ 'mm_d_pm'].value - GP['mm_d_ri'].value - GP[ 'mm_d_sleeve'].value - GP['mm_d_fixed_air_gap'].value # Vernier specific GP["deg_alpha_vspm"].value = 20.3 GP["mm_d_bg_air"].value = 1.5 GP["mm_d_bg_magnet"].value = 1.5 GP["mm_w_pm"].value = ( GP['mm_r_os'].value - GP["mm_d_bg_air"].value - (GP['mm_r_ri'].value + GP['mm_d_ri'].value)) / np.cos( GP['deg_alpha_vspm'].value) - GP['mm_d_pm'].value * np.tan( GP['deg_alpha_vspm'].value)
def hide_annotations(self): for ann in self.annotations: ann.set_visible(False) # --------------------------------------------------------------------- t = np.linspace(0.0, 1.0, 11) s1 = t s2 = -t s3 = t**2 s4 = -(t**2) s5 = np.sin(t * 2 * np.pi) s6 = np.cos(t * 2 * np.pi) fig = figure() ax1 = fig.add_subplot(321) ax1.plot(t, s1) ax1.scatter(t, s1) ax2 = fig.add_subplot(322) ax2.plot(t, s2) ax2.scatter(t, s2) ax3 = fig.add_subplot(323) ax3.plot(t, s3) ax3.scatter(t, s3)
plt.xlabel('Smarts') plt.ylabel('Probability') # 添加标题 plt.title('Histogram of IQ') # 添加文字 plt.text(60, .025, r'$\mu=100,\ \sigma=15$') plt.axis([40, 160, 0, 0.03]) plt.grid(True) plt.show() '===========================================' ax = plt.subplot(111) t = np.arange(0.0, 5.0, 0.01) s = np.cos(2 * np.pi * t) line, = plt.plot(t, s, lw=2) plt.annotate( 'local max', xy=(2, 1), xytext=(3, 1.5), arrowprops=dict(facecolor='black', shrink=0.05), ) plt.ylim(-2, 2) plt.show() '===========================================' # 导入 matplotlib 的所有内容(nympy 可以用 np 这个名字来使用)
RgRk = Rg / Rk vtip = omega * Rko lam = omega * Rko / vw Ma = Pnom / omega S = np.pi * (Rko * Rko - Rk * Rk) alpha = np.radians(60) # Initial value betas = np.radians(0) # Initial value a, b, c, d = Marzmean(lKRk, RgRk, alpha, betas, beta, delta) alpha = a betas = b MaRF = c MgRF = d Fakz = Ma / (MaRF * Rk) sa = np.sin(alpha) ca = np.cos(alpha) sb = np.sin(betas) cb = np.cos(betas) L = Fakz * np.sqrt(sb * sb * sa * sa + ca * ca) D = Fakz * cb * sa LoD = L / D CL = 2 * L / (rho * S * vw * vw) CD = 2 * D / (rho * S * vw * vw) CM = 2 * Ma / (rho * Rko * S * vw * vw) Cp = lam * CM aeff = np.arccos(cb * ca) Ftrxam = Ftrxminmax(lKRk, RgRk, alpha, betas, beta, delta) Ftryam = Ftryminmax(lKRk, RgRk, alpha, betas, beta, delta) h = np.sin(beta) * lK - sa * Rko print "Pnom : ", Pnom
import PyX_classes, PyX_Utility import os, pyx from pylab import np ''' TEC-ISMB-2021 鼠笼相量图 ''' if __name__ == '__main__': pu = PyX_Utility.PyX_Utility() PyX_Utility.global_settings['linewidth'] = pyx.style.linewidth.THIck Q_r_prime = 8 alpha_c = 2 * np.pi / Q_r_prime for i in range(Q_r_prime): radius = 10 x = radius * np.cos(i * alpha_c) y = radius * -np.sin(i * alpha_c) pu.pyx_arrow((x, y)) radius = 9 x = radius * np.cos(i * alpha_c + 15 / 180 * np.pi) y = radius * -np.sin(i * alpha_c + 15 / 180 * np.pi) pu.pyx_text((x, y), r'$\bar U_{r%d,n}$' % (i + 1), settings=['blue'], scale=1.5) radius = 11 x = radius * np.cos(i * alpha_c + 0 / 180 * np.pi) y = radius * -np.sin(i * alpha_c + 0 / 180 * np.pi) pu.pyx_text((x, y), str(i + 1), settings=[], scale=1.5) radius = 3 x = radius * np.cos(i * alpha_c)
def rotate(_, x, y): return np.cos(_) * x + np.sin(_) * y, -np.sin(_) * x + np.cos(_) * y
def my_3d_plot_non_dominated_fronts(pop, paretoPoints, fea_config_dict, az=180, comp=[0, 1, 2], plot_option=1): """ Plots solutions to the DTLZ problems in three dimensions. The Pareto Front is also visualized if the problem id is 2,3 or 4. Args: pop (:class:`~pygmo.population`): population of solutions to a dtlz problem az (``float``): angle of view on which the 3d-plot is created comp (``list``): indexes the fitness dimension for x,y and z axis in that order Returns: ``matplotlib.axes.Axes``: the current ``matplotlib.axes.Axes`` instance on the current figure Raises: ValueError: if *pop* does not contain a DTLZ problem (veryfied by its name only) or if *comp* is not of length 3 Examples: >>> import pygmo as pg >>> udp = pg.dtlz(prob_id = 1, fdim =3, dim = 5) >>> pop = pg.population(udp, 40) >>> udp.plot(pop) # doctest: +SKIP """ from mpl_toolkits.mplot3d import axes3d import matplotlib.pyplot as plt import numpy as np # from pylab import mpl # mpl.rcParams['font.family'] = ['Times New Roman'] # mpl.rcParams['font.size'] = 16.0 # if (pop.problem.get_name()[:-1] != "DTLZ"): # raise(ValueError, "The problem seems not to be from the DTLZ suite") if (len(comp) != 3): raise ( ValueError, "The kwarg *comp* needs to contain exactly 3 elements (ids for the x,y and z axis)" ) # Create a new figure fig = plt.figure(figsize=(12, 8)) ax = fig.add_subplot(111, projection='3d') # plot the points fits = np.transpose(pop.get_f()) try: pass # ax.plot(fits[comp[0]], fits[comp[1]], fits[comp[2]], 'ro') except IndexError: print('Error. Please choose correct fitness dimensions for printing!') if False: # Plot pareto front for dtlz 1 if plot_option == 1: # (pop.problem.get_name()[-1] in ["1"]): X, Y = np.meshgrid(np.linspace(0, 0.5, 100), np.linspace(0, 0.5, 100)) Z = -X - Y + 0.5 # remove points not in the simplex for i in range(100): for j in range(100): if X[i, j] < 0 or Y[i, j] < 0 or Z[i, j] < 0: Z[i, j] = float('nan') ax.set_xlim(0, 1.) ax.set_ylim(0, 1.) ax.set_zlim(0, 1.) ax.plot_wireframe(X, Y, Z, rstride=10, cstride=10) plt.plot([0, 0.5], [0.5, 0], [0, 0]) # Plot pareto fronts for dtlz 2,3,4 if plot_option == 2: # (pop.problem.get_name()[-1] in ["2", "3", "4"]): # plot the wireframe of the known optimal pareto front thetas = np.linspace(0, (np.pi / 2.0), 30) # gammas = np.linspace(-np.pi / 4, np.pi / 4, 30) gammas = np.linspace(0, (np.pi / 2.0), 30) x_frame = np.outer(np.cos(thetas), np.cos(gammas)) y_frame = np.outer(np.cos(thetas), np.sin(gammas)) z_frame = np.outer(np.sin(thetas), np.ones(np.size(gammas))) ax.set_autoscalex_on(False) ax.set_autoscaley_on(False) ax.set_autoscalez_on(False) ax.set_xlim(0, 1.8) ax.set_ylim(0, 1.8) ax.set_zlim(0, 1.8) ax.plot_wireframe(x_frame, y_frame, z_frame) # https://stackoverflow.com/questions/37000488/how-to-plot-multi-objectives-pareto-frontier-with-deap-in-python # def simple_cull(inputPoints, dominates): # paretoPoints = set() # candidateRowNr = 0 # dominatedPoints = set() # while True: # candidateRow = inputPoints[candidateRowNr] # inputPoints.remove(candidateRow) # rowNr = 0 # nonDominated = True # while len(inputPoints) != 0 and rowNr < len(inputPoints): # row = inputPoints[rowNr] # if dominates(candidateRow, row): # # If it is worse on all features remove the row from the array # inputPoints.remove(row) # dominatedPoints.add(tuple(row)) # elif dominates(row, candidateRow): # nonDominated = False # dominatedPoints.add(tuple(candidateRow)) # rowNr += 1 # else: # rowNr += 1 # if nonDominated: # # add the non-dominated point to the Pareto frontier # paretoPoints.add(tuple(candidateRow)) # if len(inputPoints) == 0: # break # return paretoPoints, dominatedPoints # def dominates(row, candidateRow): # return sum([row[x] >= candidateRow[x] for x in range(len(row))]) == len(row) # import random # print(inputPoints) # inputPoints = [[random.randint(70,100) for i in range(3)] for j in range(500)] # print(inputPoints) # quit() # inputPoints = [(x,y,z) for x,y,z in zip(fits[comp[0]], fits[comp[1]], fits[comp[2]])] # paretoPoints, dominatedPoints = simple_cull(inputPoints, dominates) x = [coords[0] / 1000 for coords in paretoPoints] y = [coords[1] for coords in paretoPoints] z = [coords[2] for coords in paretoPoints] # from surface_fitting import surface_fitting # surface_fitting(x,y,z) # quit() if False: pass else: import pandas as pd from pylab import cm print(dir(cm)) df = pd.DataFrame({'x': x, 'y': y, 'z': z}) # 只有plot_trisurf这一个函数,输入是三个以为序列的,其他都要meshgrid得到二维数组的(即ndim=2的数组) # # https://jakevdp.github.io/PythonDataScienceHandbook/04.12-three-dimensional-plotting.html # surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.magma, linewidth=0.1, edgecolor='none') # surf = ax.plot_trisurf(x, y, z, cmap='viridis', edgecolor='none') # surf = ax.plot_trisurf(df.x, df.y, df.z, cmap=cm.magma, linewidth=0.1) surf = ax.plot_trisurf(df.x, df.y * 100, df.z, cmap=cm.Spectral, linewidth=0.1) with open('./%s_PF_points.txt' % (fea_config_dict['run_folder'][:-1]), 'w') as f: f.write('TRV,eta,OC\n') f.writelines([ '%g,%g,%g\n' % (a, b, c) for a, b, c in zip(df.x, df.y * 100, df.z) ]) # quit() fig.colorbar(surf, shrink=0.5, aspect=5) ax.set_xlabel(' \n$\\rm -TRV$ [$\\rm kNm/m^3$]') ax.set_ylabel(' \n$-\\eta$ [%]') ax.set_yticks(np.arange(-96, -93.5, 0.5)) ax.set_zlabel(r'$O_C$ [1]') # Try to export data from plot_trisurf # https://github.com/WoLpH/numpy-stl/issues/19 # print(surf.get_vector()) # plt.savefig('./plots/avgErrs_vs_C_andgamma_type_%s.png'%(k)) # plt.show() # # rotate the axes and update # for angle in range(0, 360): # ax.view_init(30, angle) # plt.draw() # plt.pause(.001) ax.view_init(azim=245, elev=15) # fig.tight_layout() # Y730 # fig.savefig(r'C:\Users\horyc\Desktop/3D-plot.png', dpi=300, layout='tight') # ax.view_init(azim=az) # ax.set_xlim(0, 1.) # ax.set_ylim(0, 1.) # ax.set_zlim(0, 10.) return ax
def hide_annotations(self): for ann in self.annotations: ann.set_visible(False) # --------------------------------------------------------------------- t = np.linspace(0.0, 1.0, 11) s1 = t s2 = -t s3 = t**2 s4 = -(t**2) s5 = np.sin(t*2*np.pi) s6 = np.cos(t*2*np.pi) fig = figure() ax1 = fig.add_subplot(321) ax1.plot(t, s1) ax1.scatter(t, s1) ax2 = fig.add_subplot(322) ax2.plot(t, s2) ax2.scatter(t, s2) ax3 = fig.add_subplot(323) ax3.plot(t, s3) ax3.scatter(t, s3)
plt.plot([1.732, 1.732], [0.0, 3.0], color='0.6', linestyle='--') # high-resolution limit calculation with delta_omega = 0.1 x = [ 1.00, 1.10, 1.20, 1.30, 1.40, 1.50, 1.60, 1.70, 1.80, 1.90, 2.00, 2.20, 2.40, 2.60 ] y = [ 1.15, 1.28, 1.38, 1.50, 1.61, 1.73, 1.84, 1.96, 2.07, 2.19, 2.31, 2.54, 2.77, 2.99 ] M = [ 1.52, 1.50, 1.42, 0.97, 1.34, 0.98, 1.27, 1.25, 1.22, 1.21, 1.20, 1.67, 1.14, 1.12 ] plt.plot(x, y, color='k', linestyle='-') plt.plot([0.0, 3.0], [0.0, 3.0 / np.cos(beta)], color='r', linestyle='--') plt.plot([1.732, 1.732], [0.0, 3.0], color='0.6', linestyle='--') plt.scatter(5, 1.25, marker='o', s=80, linewidth=1.2, edgecolor='k', facecolor='w') plt.xlim(1.0, 5.0) plt.ylim(0.0, 3.0) plt.xticks(np.arange(1.0, 5.01, 1.0)) plt.yticks(np.arange(0.0, 3.01, 1.0)) minorLocator = MultipleLocator(0.5) ax.xaxis.set_minor_locator(minorLocator) ax.yaxis.set_minor_locator(minorLocator)
def walltransmission(xvals, parms): #position=p[0]; intensity=p[1]; slit=p[2]; stth=p[3]; background=p[4]; thickness=p[5]; absorption=p[6] midpoint = parms[0] i0 = parms[1] w = parms[2] theta = parms[3] ib = parms[4] thick = parms[5] u = parms[6] midpoint = parms[0] x0 = midpoint - thick / 2.0 th = np.deg2rad(theta) p = w * np.cos(th) t = thick Atot1 = w * w / np.sin(2.0 * th) Atot = 2 * w * np.sin(th) * w * np.cos(th) out = [] baselength = 2 * p Gvol_area = 2 * w * w * np.sin(th) * np.cos(th) if baselength > thick: Ar = 0.5 * (p - thick / 2.0) * (p - thick / 2.0) * np.tan(th) maxbeam = Gvol_area - 4.0 * Ar else: maxbeam = Gvol_area for x in xvals: x1 = x x2 = x - thick if (x1 <= (x0 - p)) and (x2 <= x0 - p): val = ib elif (x0 - p) >= x2 and (x0 - p <= x1) and (x0 > x1): A = (x1 - (x0 - p)) val = ib + A * A * np.tan(th) / maxbeam * i0 elif (x0 - p <= x2) and (x0 >= x1): C = (x1 - (x0 - p)) B = (x2 - (x0 - p)) val = ib + (C * C * np.tan(th) - B * B * np.tan(th)) / maxbeam * i0 elif (x2 <= (x0 - p)) and (x0 >= x2) and (x0 <= x1) and (x1 <= (x0 + p)): A = (x1 - (x0 + p)) val = ib + (Gvol_area - A * A * np.tan(th)) / maxbeam * i0 elif (x2 <= (x0 - p)) and (x0 >= x2) and (x0 <= x1) and (x1 >= (x0 + p)): val = ib + i0 elif (x2 >= (x0 - p)) and (x0 >= x2) and (x0 <= x1) and (x1 <= (x0 + p)): A = (x1 - (x0 + p)) B = (x2 - (x0 - p)) val = ib + (Gvol_area - A * A * np.tan(th) - B * B * np.tan(th)) / maxbeam * i0 elif (x2 >= (x0 - p)) and (x0 >= x2) and (x0 <= x1) and (x1 >= (x0 + p)): B = (x2 - (x0 - p)) val = ib + (Gvol_area - B * B * np.tan(th)) / maxbeam * i0 elif (x0 + p >= x1) and (x0 <= x2): A = (x1 - (x0 + p)) C = (x2 - (x0 + p)) val = ib + (C * C * np.tan(th) - A * A * np.tan(th)) / maxbeam * i0 elif (x2 >= (x0 - p)) and (x0 <= x2) and (x0 <= x1) and ( x1 >= (x0 + p)) and (x2 <= (x0 + p)): A = (x0 + p - x2) val = ib + A * A * np.tan(th) / maxbeam * i0 elif (x0 - p) >= x2 and (x0 + p) <= x2: val = ib out = out + [val] return np.array(out)
def redraw_cross_section_outline_with_pyx(tool_tikz, no_repeat_stator, no_repeat_rotor, mm_rotor_outer_radius, mm_air_gap_length, mm_rotor_outer_steel_radius, mm_rotor_inner_radius): # PyX tool_tikz.c = pyx.canvas.canvas( ) # clear the canvas because we want to redraw 90 deg with the data tool_tikz.track_path print('Index | Path data') p_stator = None #pyx.path.path() p_rotor = None #pyx.path.path() for index, path in enumerate( tool_tikz.track_path ): # track_path is passed by reference and is changed by mirror # Failed to fill the closed path, because there is no arc-like path available. # p = pyx.path.line(4, 0, 5, 0) << pyx.path.line(5, 0, 5, 1) << pyx.path.line(5, 1, 4, 1) # p.append(path.closepath()) # tool_tikz.c.stroke(p) # tool_tikz.c.stroke(path.rect(0, 0, 1, 1), [pyx.style.linewidth.Thick, # pyx.color.rgb.red, # pyx.deco.filled([pyx.color.rgb.green])]) path_mirror = deepcopy(path) # for mirror copy (along x-axis) path_mirror[1] = path[1] * -1 path_mirror[3] = path[3] * -1 # for mirror copy (along y-axis) # path_mirror[0] = path[0]*-1 # path_mirror[2] = path[2]*-1 bool_exclude_path = False # rotate path and plot if is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): Q = no_repeat_stator else: Q = no_repeat_rotor * 2 EPS = 1e-6 if is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): # 按照Eric的要求,把不必要的线给删了。 if abs(path[1] + path[3]) < EPS: # 镜像对称线 bool_exclude_path = True if abs(path[0] - path[2]) + np.cos( 2 * np.pi / Q / 2) < EPS: # 旋转对称线(特别情况,tan(90°) = ∞ bool_exclude_path = True else: if abs( abs((path[1] - path[3]) / (path[0] - path[2])) - abs(np.tan(2 * np.pi / Q / 2))) < EPS: # 旋转对称线 bool_exclude_path = True if not is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): # 按照Eric的要求,把不必要的线给删了。 if (abs(np.sqrt(path[0]**2+path[1]**2) - mm_rotor_inner_radius)<EPS or abs(np.sqrt(path[2]**2+path[3]**2) - mm_rotor_inner_radius)<EPS) \ and (len(path)==4): # 转子铁芯内径到外径的直线(len(path)==4) bool_exclude_path = True # # 特别的是,画永磁体的时候,边界要闭合哦。 # if abs(np.sqrt(path[0]**2+path[1]**2) - mm_rotor_outer_steel_radius) < EPS or abs(np.sqrt(path[2]**2+path[3]**2) - mm_rotor_outer_steel_radius) < EPS: # bool_exclude_path = False # A trick that makes sure models with different outer diameters have the same scale. # tool_tikz.draw_arc([125,0], [-125,0], relangle=sign*180, untrack=True) tool_tikz.c.fill( pyx.path.circle(0, 0, 125), [pyx.color.transparency(1)] ) # use this if THICK is used. <- Warn: Transparency not available in PostScript, proprietary ghostscript extension code inserted. (save as eps format) # tool_tikz.c.fill(pyx.path.circle(0, 0, 125), [pyx.color.rgb.white]) # use this if THICK is used. <- this will over-write everthing... how to change zorder? _ = 2 * np.pi / Q if True: # full model for counter in range(Q): # 转子:旋转复制 if not is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): path[0], path[1] = rotate(_, path[0], path[1]) path[2], path[3] = rotate(_, path[2], path[3]) 路径 = tool_tikz.pyx_draw_path( path, sign=1, bool_exclude_path=bool_exclude_path, bool_stroke=True) if 路径 is not None: if p_rotor is None: p_rotor = 路径 else: p_rotor = p_rotor << 路径 # 定子:镜像+旋转复制 if is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): path[0], path[1] = rotate(_, path[0], path[1]) path[2], path[3] = rotate(_, path[2], path[3]) 路径 = tool_tikz.pyx_draw_path( path, sign=1, bool_exclude_path=bool_exclude_path, bool_stroke=True) # print(index, '\t|', ',\t'.join(['%g'%(el) for el in path])) if 路径 is not None: if p_stator is None: p_stator = 路径 else: p_stator = p_stator << 路径 path_mirror[0], path_mirror[1] = rotate( _, path_mirror[0], path_mirror[1]) path_mirror[2], path_mirror[3] = rotate( _, path_mirror[2], path_mirror[3]) 路径 = tool_tikz.pyx_draw_path( path_mirror, sign=-1, bool_exclude_path=bool_exclude_path, bool_stroke=True) if 路径 is not None: if p_stator is None: p_stator = 路径 else: p_stator = p_stator << 路径 # break else: # backup # 转子:旋转复制 if not is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): path[0], path[1] = rotate(0.5 * np.pi - 0.5 * 0.5 * _, path[0], path[1]) path[2], path[3] = rotate(0.5 * np.pi - 0.5 * 0.5 * _, path[2], path[3]) pyx_draw_path(tool_tikz, path, sign=1, bool_exclude_path=bool_exclude_path) # print(index, '\t|', ',\t'.join(['%g'%(el) for el in path])) # path[0], path[1] = rotate(0.5*np.pi - 0*0.5*_, path[0], path[1]) # path[2], path[3] = rotate(0.5*np.pi - 0*0.5*_, path[2], path[3]) # pyx_draw_path(tool_tikz, path, sign=1, bool_exclude_path=bool_exclude_path) # 定子:镜像+旋转复制 if is_at_stator(path, mm_rotor_outer_radius, mm_air_gap_length): path[0], path[1] = rotate(0.5 * np.pi - 0.5 * _, path[0], path[1]) path[2], path[3] = rotate(0.5 * np.pi - 0.5 * _, path[2], path[3]) pyx_draw_path(tool_tikz, path, sign=1, bool_exclude_path=bool_exclude_path) # print(index, '\t|', ',\t'.join(['%g'%(el) for el in path])) path_mirror[0], path_mirror[1] = rotate( 0.5 * np.pi - 0.5 * _, path_mirror[0], path_mirror[1]) path_mirror[2], path_mirror[3] = rotate( 0.5 * np.pi - 0.5 * _, path_mirror[2], path_mirror[3]) pyx_draw_path(tool_tikz, path_mirror, sign=-1, bool_exclude_path=bool_exclude_path) # 注意,所有 tack_path 中的 path 都已经转动了90度了! # for mirror copy (along y-axis) path[0] *= -1 path[2] *= -1 pyx_draw_path(tool_tikz, path, sign=-1, bool_exclude_path=bool_exclude_path) path_mirror[0] *= -1 path_mirror[2] *= -1 pyx_draw_path(tool_tikz, path_mirror, sign=1, bool_exclude_path=bool_exclude_path)