Solutions de l'équation de D'Alembert

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

SimulationProgramme Python

1
# -*- coding: utf-8 -*-
2
"""
3
D'Alembert
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
17
def inifig(xaxe=0,yaxe=0,xlab='x',ylab='y'):
18
    plt.figure(2)
19
    plt.axvline(xaxe)
20
    plt.axhline(yaxe)
21
    plt.xticks(fontsize=12)
22
    plt.yticks(fontsize=12)
23
    plt.xlabel(xlab,fontsize=16 )
24
    plt.ylabel(ylab,fontsize=16)
25
    
26
def zfi(x,le=2):
27
    miss=le-len(str(x))
28
    a='0'*miss+str(x)
29
    return a
30
31
def animation(name):
32
    a=np.linspace(0,L,N+1);
33
    acont=np.linspace(0,L,501);
34
    t=0; 
35
    dt=T/Nt
36
    for i in range(0,Nt+1):
37
        title=name+" i="+zfi(i)
38
        titlefig=name
39
        print(title)
40
        fig=plt.figure(N,figsize=(7,4))
41
        plt.xlabel('x',fontsize=16 )
42
        plt.ylabel(r'$\psi$',fontsize=16)
43
        plt.title(titlefig,fontsize=16)
44
        # signal
45
        t=dt*i;
46
        xi=signal(a,t)
47
        xicont=signal(acont,t)
48
        plt.plot(acont,xicont,color='black',linewidth=3)
49
        plt.xlim(0,L)
50
        plt.ylim(ymin,ymax)
51
        namei=name+zfi(i)+'.png';
52
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
53
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
54
        plt.savefig(namei)
55
        plt.show()
56
        plt.close()
57
    gifanim="Anim"+name+".gif"
58
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
59
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);    
60
    if flagp: os.system('rm '+name+'*');   
61
62
def animationboth(name):
63
    a=np.linspace(0,L,N+1);
64
    acont=np.linspace(0,L,501);
65
    t=0; 
66
    dt=T/Nt
67
    for i in range(0,Nt+1):
68
        title=name+" i="+zfi(i)
69
        titlefig=name
70
        print(title)
71
        fig=plt.figure(N,figsize=(7,4))
72
        plt.xlabel('x',fontsize=16 )
73
        plt.ylabel(r'$\psi$',fontsize=16)
74
        plt.title(titlefig,fontsize=16)
75
        # signal
76
        t=dt*i;
77
        xicont=signal(acont,t)
78
        xileft=signalleft(acont,t)
79
        xiright=signalright(acont,t)
80
        plt.plot(acont,xicont,color='black',linewidth=3)
81
        plt.plot(acont,xileft,color='green',linewidth=3)
82
        plt.plot(acont,xiright,color='red',linewidth=3)
83
        plt.xlim(0,L)
84
        plt.ylim(ymin,ymax)
85
        namei=name+zfi(i)+'.png';
86
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
87
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
88
        plt.savefig(namei)
89
        plt.show()
90
        plt.close()
91
    gifanim="Anim"+name+".gif"
92
    os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
93
    #os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
94
    if flagp: os.system('rm '+name+'*');  
95
96
def animationbothwith(name):
97
    a=np.linspace(0,L,N+1);
98
    acont=np.linspace(0,L,501);
99
    t=0; 
100
    dt=T/Nt
101
    for i in range(0,Nt+1):
102
        title=name+" i="+zfi(i)
103
        titlefig=name
104
        print(title)
105
        fig=plt.figure(N,figsize=(7,4))
106
        plt.xlabel('x',fontsize=16 )
107
        plt.ylabel(r'$\psi$',fontsize=16)
108
        plt.title(titlefig,fontsize=16)
109
        # signal
110
        t=dt*i;
111
        xicont=signal(acont,t)
112
        xileft=signalleft(acont,t)
113
        xiright=signalright(acont,t)
114
        xinit=signalinit(acont)
115
        plt.plot(acont,xinit,color='grey',linewidth=3)
116
        plt.plot(acont,xicont,color='black',linewidth=3)
117
        plt.plot(acont,xileft,color='green',linewidth=3)
118
        plt.plot(acont,xiright,color='red',linewidth=3)
119
        plt.xlim(0,L)
120
        plt.ylim(ymin,ymax)
121
        namei=name+zfi(i)+'.png';
122
        plt.grid(color='black', axis='y', linestyle='-', linewidth=1)        
123
        plt.grid(color='black', axis='x', linestyle='-', linewidth=1)        
124
        plt.savefig(namei)
125
        plt.show()
126
        plt.close()
127
    gifanim="Anim"+name+".gif"
128
    #os.system('/opt/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
129
    os.system('/usr/local/bin/convert -set delay '+delay+' '+name+'* '+gifanim);
130
    if flagp: os.system('rm '+name+'*');  
131
132
133
# Main 
134
F=False; T=True
135
flagp=F; flagall=F;
136
137
def pulse(a,d):
138
    al=0*a[a<-d]; ac=a[np.abs(a)<=d]; ar=0*a[a>d];
139
    k=np.pi/d; fc=.5*(1+np.cos(k*ac));
140
    f=np.concatenate((al,fc,ar))
141
    return f
142
143
def leftsin(a,k):
144
    al=a[a<=0]; ar=0*a[a>0];
145
    fl=-np.sin(k*al);
146
    f=np.concatenate((fl,ar))
147
    return f
148
149
def rightsin(a,k):
150
    al=0*a[a<=0]; ar=a[a>0];
151
    fr=np.sin(k*ar);
152
    f=np.concatenate((al,fr))
153
    return f
154
155
def statio(a,d):
156
    k=np.pi/d; fc=.5*(1+np.cos(k*ac));
157
    f=np.cos
158
    return f
159
160
161
# Condition initiale
162
if F or flagall: 
163
    N=20; L=10; d=2; c=1; 
164
    T=.7*L/c; Nt=20; sc=.9*L/N # scaling 
165
    k=4*np.pi/L; omega=k*c;
166
    ymin=-.2; ymax=1.1;
167
    delay="80"
168
    # signal 
169
    def signalinit(a):
170
        xi=pulse(-L/2+a,d)
171
        return xi
172
    def signal(a,t):
173
        xi=.5*pulse(-L/2+a-c*t,d)+.5*pulse(-L/2+a+c*t,d)
174
        return xi
175
    def signalleft(a,t):
176
        xi=.5*pulse(-L/2+a+c*t,d)
177
        return xi
178
    def signalright(a,t):
179
        xi=.5*pulse(-L/2+a-c*t,d)
180
        return xi
181
    animationbothwith("Condition-initiale") 
182
183
184
# Deux pulses
185
if T or flagall: 
186
    N=20; L=10; d=2; c=1; 
187
    T=(L+2*d)/c; Nt=20; sc=.9*L/N # scaling 
188
    k=np.pi/d;
189
    ymin=-.2; ymax=1;
190
    duprange=np.linspace(-.2,1,7)
191
    delay="80"
192
    # signal 
193
    def signal(a,t):
194
        xi=.5*pulse(a+d-c*t,d)+.5*pulse(a-L-d+c*t,d)
195
        return xi
196
    def signalleft(a,t):
197
        xi=.5*pulse(a-L-d+c*t,d)
198
        return xi
199
    def signalright(a,t):
200
        xi=.5*pulse(a+d-c*t,d)
201
        return xi
202
    animationboth("Deux-pulses") 
203
204
205
# Deux pulses inegaux
206
if F or flagall: 
207
    N=20; L=10; d=2; c=1; 
208
    T=(L+2*d)/c; Nt=20; sc=.9*L/N # scaling 
209
    k=np.pi/d;
210
    ymin=-.2; ymax=1;
211
    duprange=np.linspace(-.2,1,7)
212
    delay="80"
213
    # signal 
214
    def signal(a,t):
215
        xi=.7*pulse(a+d-c*t,d)+.3*pulse(a-L-d+c*t,d)
216
        return xi
217
    def signalleft(a,t):
218
        xi=.3*pulse(a-L-d+c*t,d)
219
        return xi
220
    def signalright(a,t):
221
        xi=.7*pulse(a+d-c*t,d)
222
        return xi
223
    animationboth("Deux-pulses-inegaux") 
224
225
226
 # Progessive-cos
227
if T or flagall: 
228
    N=20; L=10; c=1; 
229
    T=2*L/c; Nt=20; sc=.9*L/N # scaling 
230
    k=2*np.pi/L; omega=k*c;
231
    ymin=-1; ymax=1;
232
    duprange=np.linspace(-1,1,9)
233
    delay="20"
234
    # signal 
235
    def signal(a,t):
236
        xi=np.cos(n*k*a-omega*t)
237
        return xi
238
    m=20;n=m/10; 
239
    animation("Progressive-cos-10xn="+zfi(m)) 
240
241
242
 # Onde stationnaire
243
if T or flagall: 
244
    N=20; L=10; c=1; 
245
    T=2*L/c; Nt=82; sc=.9*L/N # scaling 
246
    k=4*np.pi/L; omega=k*c;
247
    ymin=-1; ymax=1;
248
    delay="20"
249
    # signal 
250
    def signal(a,t):
251
        xi=.5*rightsin(-L+a+c*t,k)+.5*leftsin(a-c*t,k)
252
        return xi
253
    def signalleft(a,t):
254
        xi=.5*rightsin(-L+a+c*t,k)
255
        return xi
256
    def signalright(a,t):
257
        xi=.5*leftsin(a-c*t,k)
258
        return xi
259
    animationboth("Onde-stationnaire") 
260
261
 
262
 # Stationnaire-cos
263
if F or flagall: 
264
    N=20; L=10; c=1; 
265
    T=2*L/c; Nt=20; sc=.9*L/N # scaling 
266
    k=np.pi/L; omega=k*c;
267
    ymin=-1; ymax=1;
268
    #duprange=np.linspace(-1,1,9)
269
    duprange=np.linspace(-2,-1,1)
270
    delay="20"
271
    # signal 
272
    def signal(a,t):
273
        xi=np.cos(n*k*a)*np.cos(omega*t+np.pi/2)
274
        return xi
275
    for m in range(5,55,5):
276
        print("Stationaire-sin-10xn="+zfi(m))
277
        n=m/10; animation("Stationaire-cos-10xn="+zfi(m)) 
278
279
 
280
 # Stationnaire-sin
281
if F or flagall: 
282
    N=20; L=10; c=1; 
283
    T=2*L/c; Nt=20; sc=.9*L/N # scaling 
284
    k=np.pi/L; omega=k*c;
285
    ymin=-1; ymax=1;
286
    #duprange=np.linspace(-1,1,9)
287
    duprange=np.linspace(-2,-1,1)
288
    delay="20"
289
    # signal 
290
    def signal(a,t):
291
        xi=np.sin(n*k*a)*np.cos(omega*t+np.pi/2)
292
        return xi
293
    #for m in np.linspace(5,50,11):
294
    for m in range(5,55,5):
295
        print("Stationaire-sin-10xn="+zfi(m))
296
        n=m/10; animation("Stationaire-sin-10xn="+zfi(m)) 
297