Dispersion des ondes

FondamentalDiaporama du micro-contenu (rafraichir la page si redimensionnée)

SimulationProgramme Python

1
# -*- coding: utf-8 -*-
2
"""
3
Dispersion
4
O. Thual, 21/08/2021
5
"""
6
7
#  clear all
8
for iglob in list(globals().keys()):
9
    if(iglob[0] != '_'):
10
        exec('del {}'.format(iglob))
11
# import libraries
12
import numpy as np
13
import matplotlib.pyplot as plt
14
import os
15
16
# Sous programmes 
17
18
def inifig(xaxe=0,yaxe=0,xlab='x',ylab='y'):
19
    plt.figure(2)
20
    plt.axvline(xaxe)
21
    plt.axhline(yaxe)
22
    plt.xticks(fontsize=12)
23
    plt.yticks(fontsize=12)
24
    plt.xlabel(xlab,fontsize=16 )
25
    plt.ylabel(ylab,fontsize=16)
26
    
27
def zfi(x,le=2):
28
    miss=le-len(str(x))
29
    a='0'*miss+str(x)
30
    return a
31
    
32
def reladi(k):
33
    #omega=al*k-be*k**
34
    om=2*np.pi
35
    omega=np.sqrt(om**2+k**2)
36
    return omega
37
38
def vgroup(k):
39
    #v=al-3*be*k**2
40
    om=2*np.pi
41
    omega=np.sqrt(om**2+k**2)
42
    v=k/omega
43
    return v
44
45
def gauss(k):
46
    g=np.exp(-(k-k0)**2/(2*var))
47
    return g
48
49
def signalN(x,t):
50
    psi=np.zeros((N,Nx))
51
    for n in range(0,N):
52
        kn=k[n]; omn=om[n]; 
53
        psin=np.cos(kn*x-omn*t)*gauss(kn);
54
        psi[n,:]=psin;
55
    return psi
56
57
def plotrelaN(name,kmin,kmax,k):
58
    kplt=np.linspace(0.01,kmax,501);
59
    om=reladi(k); cg=vgroup(k); cp=om/k;
60
    omplt=reladi(kplt); cgplt=vgroup(kplt); cpplt=omplt/kplt;
61
    fig=plt.figure(2,figsize=(7,4))
62
    plt.xlabel(r'$k$',fontsize=16 )
63
    plt.ylabel(r'$c_g, c_p, \omega$',fontsize=16)
64
    plt.xlim(kmin,kmax)
65
    plt.ylim(0,10)
66
    titlefig=name
67
    plt.title(titlefig,fontsize=16)
68
    plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
69
    plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
70
    plt.plot(kplt,omplt,color='black',linewidth=3)
71
    plt.plot(kplt,cgplt,color='magenta',linewidth=3)
72
    plt.plot(kplt,cpplt,color='blue',linewidth=3)
73
    plt.plot(kplt,fac*gauss(kplt),color='black',linewidth=3)
74
    # modes 
75
    plt.scatter(k,fac*amp,marker='o',color='blue',s=50)
76
    plt.scatter(k,om,marker='o',color='blue',s=50)
77
    plt.scatter(k,cg,marker='o',color='blue',s=50)
78
    plt.scatter(k,cp,marker='o',color='blue',s=50)
79
    # extremes
80
    plt.scatter(k1,fac*gauss(k1),marker='o',color='red',s=100)
81
    plt.scatter(k1,reladi(k1),marker='o',color='red',s=100)
82
    plt.scatter(k1,vgroup(k1),marker='o',color='red',s=100)
83
    plt.scatter(k1,reladi(k1)/k1,marker='o',color='red',s=100)
84
    plt.scatter(k2,fac*gauss(k2),marker='o',color='green',s=100)
85
    plt.scatter(k2,reladi(k2),marker='o',color='green',s=100)
86
    plt.scatter(k2,vgroup(k2),marker='o',color='green',s=100)
87
    plt.scatter(k2,reladi(k2)/k2,marker='o',color='green',s=100)
88
    plt.savefig(name)
89
    plt.show()
90
91
92
def reladisp(name,kmin,kmax,k):
93
    kplt=np.linspace(0.01,kmax,501);
94
    om=reladi(k); cg=vgroup(k); cp=om/k;
95
    omplt=reladi(kplt); cgplt=vgroup(kplt); cpplt=omplt/kplt;
96
    fig=plt.figure(2,figsize=(7,4))
97
    plt.xlabel(r'$k$',fontsize=16 )
98
    plt.ylabel(r'$c_g, c_p, \omega$',fontsize=16)
99
    plt.xlim(kmin,kmax)
100
    plt.ylim(0,10)
101
    titlefig=name
102
    plt.title(titlefig,fontsize=16)
103
    plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
104
    plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
105
    plt.plot(kplt,omplt,color='black',linewidth=3)
106
    plt.plot(kplt,cgplt,color='magenta',linewidth=3)
107
    plt.plot(kplt,cpplt,color='blue',linewidth=3)
108
    plt.plot([0,kmax],[1,1],color='black',linestyle='-.',linewidth=3)
109
    plt.savefig(name)
110
    plt.show()
111
112
113
114
def animationN(name):
115
    t=0; 
116
    dt=Time/Nt  
117
    for i in range(0,Nt):
118
        t=dt*i; 
119
        title=name+" i="+zfi(i)
120
        titlefig=name
121
        print(title)
122
        fig=plt.figure(1,figsize=(7,4))
123
        plt.xlabel('x',fontsize=16 )
124
        plt.ylabel(r'$\theta$',fontsize=16)
125
        plt.title(titlefig,fontsize=16)
126
        # signaux
127
        psi=signalN(x,t)
128
        xp=np.zeros(M); psip=np.zeros(M)
129
        for m in range(M):
130
            xp[m]=-m*L0+v*t; psip[m]=0;    
131
        psi1=np.cos(k1*x-om1*t)*gauss(k1)
132
        psi2=np.cos(k2*x-om2*t)*gauss(k2)
133
         # 
134
        psitot=x*0;
135
        for n in range(N):
136
            psin=psi[n,:]
137
            psitot=psitot+psin/N
138
            for m in range(M):
139
                psip[m]=psip[m]+np.cos(k[n]*xp[m]-om[n]*t)*gauss(k[n])/N
140
            plt.plot(x,psin,color='blue',linewidth=1)
141
        plt.plot(x,psi1,color='red',linewidth=2)
142
        plt.plot(x,psi2,color='green',linewidth=2)
143
        plt.plot(x,2*psitot,color='black',linewidth=3)
144
        plt.scatter(xp,2*psip,marker='o',color='magenta',s=160)
145
        plt.xlim(-L,L)
146
        plt.ylim(ymin,ymax)
147
        namei=name+zfi(i)+'.png';
148
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
149
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
150
        plt.savefig(namei)
151
        plt.show()
152
        plt.close()
153
    gifanim="Anim"+name+".gif"
154
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
155
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
156
    if Flagrm: os.system('rm '+name+'*');  
157
158
159
    
160
# Main 
161
F=False; T=True
162
Flagrm=T;
163
164
165
166
# N ondes
167
# relation de dispersion
168
169
withanim=T;
170
k0=4; de=.05; dk=de*k0; k1=k0-dk; k2=k0+dk;
171
var=dk**2; fac=6;
172
for N in range(2,6):
173
    k=np.linspace(k1,k2,N); amp=gauss(k);
174
    om=reladi(k); cg=vgroup(k); cp=om/k;
175
    om1=reladi(k1); om2=reladi(k2)
176
    M=10*N # nombre de temoins de phase
177
    v=(om1+om2)/(k1+k2)
178
    name="Klein-Gordon-"+zfi(N)+"-ondes"
179
       #
180
    L0=10*4*np.pi/(k1+k2); 
181
    L=N*5*4*np.pi/(k1+k2); 
182
    Nx=1001; x=np.linspace(-L,L,Nx);
183
    Nt=(N+1)*10; Time=.5*L/vgroup(k0);  
184
    ymin=-2; ymax=2;
185
    delay="40"
186
    nom="Paquet-"+zfi(N)+"-ondes"
187
    print(nom)
188
    if withanim: animationN(nom) 
189
    name="Klein-Gordon-"+zfi(N)+"-ondes-Zoom"
190
    kmin=3.6; kmax=4.4
191
    plotrelaN(name,kmin,kmax,k)
192
    name="Klein-Gordon-"+zfi(N)+"-ondes"
193
    kmin=.01; kmax=5
194
    plotrelaN(name,kmin,kmax,k)
195
    kmin=.01; kmax=10
196
    reladisp("Klein-Gordon",kmin,kmax,k)
197