
import numpy as np
from matplotlib.pyplot import *
import matplotlib.tri as tri
from FreeFemData import FreeFemData

freeFemData = FreeFemData('filRectiligne')
						 

triang = tri.Triangulation(freeFemData.x_array,freeFemData.y_array,freeFemData.triangles)					 
						 

fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
						 

rayon = 0.02
theta = np.linspace(0,2*np.pi,1000)
x = rayon*np.cos(theta)
y = rayon*np.sin(theta)
fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot(x,y,'r-')
ax0.set_xlim(-rayon*2,rayon*2)
ax0.set_ylim(-rayon*2,rayon*2)
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
						 

data = freeFemData.readData(3) 
Az_array = data[0,:]
Bx_array = data[1,:] 
By_array = data[2,:]

fig1,ax1 = subplots(figsize=(10,10))
ax1.set_aspect('equal')
ct = ax1.tricontour(triang,Az_array,levels=40)
fig1.colorbar(ct)
ax1.set_xlabel('x',fontsize=16)
ax1.set_ylabel('y',fontsize=16)		 
						 

Bx_inter = tri.LinearTriInterpolator(triang, Bx_array)
By_inter = tri.LinearTriInterpolator(triang, By_array)
x = np.linspace(-1,1,1000)
y = np.ones(len(x))*0
By = By_inter(x,y)
figure(figsize=(8,6))
plot(x,By)
xlabel('x',fontsize=16)
ylabel('By',fontsize=16)
grid()
						   

Az_inter = tri.LinearTriInterpolator(triang, Az_array)
figure(figsize=(8,6))
plot(x,Az_inter(x,y))
xlabel('x',fontsize=16)
ylabel('Az',fontsize=16)
grid()
							

freeFemData = FreeFemData('plaqueConductriceChargee')
triang = tri.Triangulation(freeFemData.x_array,freeFemData.y_array,freeFemData.triangles)
Rdomaine = 100
e = 1 # épaisseur de la plaque
L = 50 # largeur de la plaque
e2=e/2
L2=L/2					 
fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([-e2,e2,e2,-e2,-e2],[-L2,-L2,L2,L2,-L2],'r-')
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
						 

fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([-e2,e2,e2,-e2,-e2],[-L2,-L2,L2,L2,-L2],'r-')
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
ax0.set_xlim(-10,10)
ax0.set_ylim(15,35)
						 

data = freeFemData.readData(3)
V_array = data[0,:]
Ex_array = data[1,:]
Ey_array = data[2,:]

fig1,ax1 = subplots(figsize=(10,10))
ax1.set_aspect('equal')
ct = ax1.tricontour(triang,V_array,levels=40)
fig1.colorbar(ct)
ax1.plot([-e2,e2,e2,-e2,-e2],[-L2,-L2,L2,L2,-L2],'r-')
ax1.set_xlabel('x',fontsize=16)
ax1.set_ylabel('y',fontsize=16)							 
						 

fig1,ax1 = subplots(figsize=(10,10))
ax1.set_aspect('equal')
ct = ax1.tricontour(triang,V_array,levels=40)
fig1.colorbar(ct)
ax1.plot([-e2,e2,e2,-e2,-e2],[-L2,-L2,L2,L2,-L2],'r-')
ax1.set_xlabel('x',fontsize=16)
ax1.set_ylabel('y',fontsize=16)	
ax1.set_xlim(-10,10)
ax1.set_ylim(15,35)		 
						 

Ex_inter = tri.LinearTriInterpolator(triang, Ex_array)
Ey_inter = tri.LinearTriInterpolator(triang, Ey_array)
x = np.linspace(e2,Rdomaine,1000)
y = np.ones(len(x))*0
Ex = Ex_inter(x,y)
figure(figsize=(8,6))
plot(x,Ex)
xlabel('x',fontsize=16)
ylabel('Ex',fontsize=16)
grid()
ylim(0,0.1)
						   

y = np.linspace(L2,Rdomaine,1000)
x = np.ones(len(y))*0
Ey = Ey_inter(x,y)
figure(figsize=(8,6))
plot(y,Ey)
xlabel('y',fontsize=16)
ylabel('Ey',fontsize=16)
grid()
ylim(0,0.1)
						   

freeFemData = FreeFemData('conducteursAxial')
triang = tri.Triangulation(freeFemData.x_array,freeFemData.y_array,freeFemData.triangles)
Zm=100
Rm=100
e=1
e2=e/2
R=25
z1=-5-e2
z2=5+e2	 
fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([z1-e2,z1+e2,z1+e2,z1-e2,z1-e2],[-R,-R,R,R,-R],'r-')
ax0.plot([z2-e2,z2+e2,z2+e2,z2-e2,z2-e2],[-R,-R,R,R,-R],'r-')
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
						 

fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([z1-e2,z1+e2,z1+e2,z1-e2,z1-e2],[-R,-R,R,R,-R],'r-')
ax0.plot([z2-e2,z2+e2,z2+e2,z2-e2,z2-e2],[-R,-R,R,R,-R],'r-')
ax0.set_xlabel('x',fontsize=16)
ax0.set_ylabel('y',fontsize=16)
ax0.set_xlim(-10,10)
ax0.set_ylim(15,35)
						 

data = freeFemData.readData(3)
V_array = data[0,:]
Ex_array = data[1,:]
Ey_array = data[2,:]

fig1,ax1 = subplots(figsize=(10,10))
ax1.set_aspect('equal')
ct = ax1.tricontour(triang,V_array,levels=20)
fig1.colorbar(ct)
ax1.plot([z1-e2,z1+e2,z1+e2,z1-e2,z1-e2],[-R,-R,R,R,-R],'r-')
ax1.plot([z2-e2,z2+e2,z2+e2,z2-e2,z2-e2],[-R,-R,R,R,-R],'r-')
ax1.set_xlabel('z',fontsize=16)
ax1.set_ylabel('r',fontsize=16)
ax1.set_xlim(-50,50)
ax1.set_ylim(-50,50)						 
						 

Ex_inter = tri.LinearTriInterpolator(triang, Ex_array)
Ey_inter = tri.LinearTriInterpolator(triang, Ey_array)
x = np.linspace(-Zm,Zm,1000)
y = np.ones(len(x))*0
Ex = Ex_inter(x,y)
figure(figsize=(8,6))
plot(x,Ex)
xlabel('z',fontsize=16)
ylabel('Ez',fontsize=16)
grid()
						   

freeFemData = FreeFemData('bobineAxial')
triang = tri.Triangulation(freeFemData.x_array,freeFemData.y_array,freeFemData.triangles)  
Zm=100
Rm=100 
a=8 
e=4
e2=e/2
L=50
L2=L/2
fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([-L2,L2,L2,-L2,-L2],[a-e2,a-e2,a+e2,a+e2,a-e2],'r-')
ax0.plot([-L2,L2,L2,-L2,-L2],[-a-e2,-a-e2,-a+e2,-a+e2,-a-e2],'r-')
ax0.set_xlabel('z',fontsize=16)
ax0.set_ylabel('r',fontsize=16)
						 

fig0,ax0 = subplots(figsize=(10,10))
ax0.set_aspect('equal')
ax0.triplot(triang,color='0.8')
ax0.plot([-L2,L2,L2,-L2,-L2],[a-e2,a-e2,a+e2,a+e2,a-e2],'r-')
ax0.plot([-L2,L2,L2,-L2,-L2],[-a-e2,-a-e2,-a+e2,-a+e2,-a-e2],'r-')
ax0.set_xlabel('z',fontsize=16)
ax0.set_ylabel('r',fontsize=16)
ax0.set_xlim(-L,L)  
ax0.set_ylim(-L,L)
						 

data = freeFemData.readData(3)
Atheta_array = data[0,:]
Br_array = data[1,:] 
Bz_array = data[2,:] 

fig1,ax1 = subplots(figsize=(10,10))
ax1.set_aspect('equal')
ct = ax1.tricontour(triang,Atheta_array,levels=10)
fig1.colorbar(ct)
ax1.plot([-L2,L2,L2,-L2,-L2],[a-e2,a-e2,a+e2,a+e2,a-e2],'r-')
ax1.plot([-L2,L2,L2,-L2,-L2],[-a-e2,-a-e2,-a+e2,-a+e2,-a-e2],'r-')
ax1.set_xlabel('z',fontsize=16)
ax1.set_ylabel('r',fontsize=16)
ax1.set_xlim(-L,L)
ax1.set_ylim(-L,L)
						 

Br_inter = tri.LinearTriInterpolator(triang, Br_array)
Bz_inter = tri.LinearTriInterpolator(triang, Bz_array)
x = np.linspace(-Zm,Zm,1000)
y = np.ones(len(x))*1
Bz = Bz_inter(x,y)
figure(figsize=(8,6))
plot(x,Bz) 
xlabel('z',fontsize=16)
ylabel('Bz',fontsize=16)
grid()
						 

y = np.linspace(-Rm,Rm,1000)
x = np.ones(len(y))*0
Bz = Bz_inter(x,y)
figure(figsize=(8,6))
plot(y,Bz)
xlabel('r',fontsize=16)
ylabel('Bz',fontsize=16)
xlim(-40,40)
plot([a-e2,a-e2],[0,4],'r-')
plot([a+e2,a+e2],[0,4],'r-')
plot([-a-e2,-a-e2],[0,4],'r-')
plot([-a+e2,-a+e2],[0,4],'r-')
grid()
						 

Atheta_inter = tri.LinearTriInterpolator(triang, Atheta_array)
y = np.linspace(-Rm,Rm,500)
x = np.ones(len(y))*0
(Gz,Gr) = Atheta_inter.gradient(x,y)
Atheta = Atheta_inter(x,y)
Bz = Atheta/y + Gr
figure(figsize=(8,6))
plot(y,Bz)
xlim(-40,40)
plot([a-e2,a-e2],[0,4],'r-')
plot([a+e2,a+e2],[0,4],'r-')
plot([-a-e2,-a-e2],[0,4],'r-')
plot([-a+e2,-a+e2],[0,4],'r-')
grid()
xlabel('r',fontsize=16)
ylabel('Bz',fontsize=16)
					 	  


x = np.linspace(-Zm,Zm,1000)
y = np.ones(len(x))*1
(Gz,Gr) = Atheta_inter.gradient(x,y)
Atheta = Atheta_inter(x,y)
Bz = Atheta/y + Gr
figure(figsize=(8,6))
plot(x,Bz)
xlim(-100,100)
grid()
xlabel('z',fontsize=16)
ylabel('Bz',fontsize=16)
			 	  

figure(figsize=(8,6))
y = np.linspace(-Rm,Rm,500)
x = np.ones(len(y))*0
Atheta = Atheta_inter(x,y)
figure(figsize=(8,6))
plot(y,Atheta)
xlabel('r',fontsize=16)
ylabel(r"$A_{\theta}$",fontsize=16)
grid()
						  

from scipy.signal import convolve
dy = y[1]-y[0]
dAtheta_dr = convolve(Atheta,[1,-1],mode='same') / dy
figure(figsize=(8,6))
plot(y,dAtheta_dr)
xlabel('r',fontsize=16)
ylabel(r"$\frac{\partial A_{\theta}}{\partial r}$",fontsize=16)
grid()
						 

Bz = Atheta/y + dAtheta_dr
figure(figsize=(8,6))
plot(y,Bz)
xlim(-40,40)
plot([a-e2,a-e2],[0,4],'r-')
plot([a+e2,a+e2],[0,4],'r-')
plot([-a-e2,-a-e2],[0,4],'r-')
plot([-a+e2,-a+e2],[0,4],'r-')
grid()
xlabel('r',fontsize=16)
ylabel('Bz',fontsize=16)
						 
