Correction TP 5
Transcription
Correction TP 5
Correction TP 5 //Schéma saute mouton pour l’eq des ondes sur un carré // d2t(u)-c2.delta(u)=f // u=0 au bord function [z]=fonc_temps(t,a,t0) z=exp(-a*(t-t0).*(t-t0)); endfunction clear all; c=1.; //célérité du son n=201; //nombre de points en x et y h=1./(n-1); //pas de maillage tf=5; //temps final lambda=0.99; //cfl (1 limite de stabilité) dt=lambda*h/(sqrt(2)*c); //pas de temps calculé //donnée de la gaussienne en temps a=10000; //rampe de la gaussienne t0=0.04; //centrage de la gaussienne en temps cfl=c*dt/h //CFL cfl2=cfl*cfl; dt2=dt*dt; tcfl=[’\lambda = ’ string(lambda)]; th=[’h = 1/’ string(n-1)]; tdt=[’\Deltat = ’ msprintf(’%6.2g’,dt)] uk=zeros(n,n); ukm1=zeros(n,n); ukp1=zeros(n,n); v=zeros(n,n); //vecteur //vecteur //vecteur //vecteur solution à l’instant tk solution à l’instant tk-1 solution à l’instant tk+1 temporaire //initialisation à l’instant 0 et dt tk=2*dt; //temps courant k=2; //source dirac au centre du carré (n impair !) (50000 pour f, 40 pour f") f=zeros(n,n); //f((n+1)/2,(n+1)/2)=80000.; f(10,10)=900000; //initialisation pour le graphique scf(1);clf();hf=gcf(); hf.pixmap = "on"; hf.figure_size = [600 600]; hf.children.axes_visible=[’off’,’off’,’off’] ; hf.children.isoview=’on’; hf.children.tight_limits = "on" hf.children.margins = [0,0,0,0]; hf.pixel_drawing_mode=’xor’ x=0:h:1; xset("colormap",oceancolormap(128)); drawlater() Sgrayplot(x’,x’,uk, strf="040") drawnow() 1 drawlater() //boucle itérative en temps, schéma saute-mouton while(tk<tf) fk=f*fonc_temps(tk,a,t0); v(2:n-1,2:n-1)=uk(3:n,2:n-1)+uk(1:n-2,2:n-1)+uk(2:n-1,3:n)+uk(2:n-1,1:n-2)-4*uk(2:n-1,2:n-1); ukp1=2*uk-ukm1+cfl2*v+dt2*fk; ukm1=uk; uk=ukp1; drawlater() Sgrayplot(x’,x’,uk, strf="040") tk=tk+dt; drawnow() drawlater() end, h.pixel_drawing_mode=’copy’ drawnow() On illustre ci-après la façon dont on peut inclure cette simulation dans une interface utilisateur (gui). 2 function [z]=fonc_temps(t,a,tau) z=exp(-a*(t-tau).*(t-tau)); endfunction function compute() ht0 = findobj("tag", "t0");t0=evstr(ht0.string); htf = findobj("tag", "tf"); tf=evstr(htf.string); hn = findobj("tag", "n");n=evstr(hn.string); hdt = findobj("tag", "dt");dt=evstr(hdt.string); hc = findobj("tag", "c");c=evstr(hc.string); htau = findobj("tag", "tau");tau=evstr(htau.string); ha = findobj("tag", "a");a=evstr(ha.string); hsi = findobj("tag", "source i");si=evstr(hsi.string); hsj = findobj("tag", "source j");sj=evstr(hsj.string); disp(t0,tf,n,dt,c,tau,a,si,sj) ht = findobj("tag", "t"); hlt = findobj("tag", "lt"); hbtn = findobj("tag", "btn1");btn=hbtn.string; if(btn=="Stop") set(hbtn,’string’,’Calcul’); set(ht,’visible’,’off’); set(ht,’string’,’0’); set(hlt,’visible’,’off’); m_axes = gca(); m_axes.visible="off"; resume; else set(hbtn,’string’,’Stop’); set(ht,’visible’,’on’); set(hlt,’visible’,’on’); end, h=1./(n-1); x=0:h:1; lambda=0.9999; dt=lambda*h/(sqrt(2)*c); set(hdt,"string",string(dt)); cfl=c*dt/h; //CFL hcfl = findobj("tag", "cfl"); set(hcfl,"string",string(cfl)) cfl2=cfl*cfl; dt2=dt*dt; uk=zeros(n,n); ukm1=zeros(n,n); ukp1=zeros(n,n); v=zeros(n,n); //vecteur //vecteur //vecteur //vecteur //pas de maillage //pas de temps calculé solution à l’instant tk solution à l’instant tk-1 solution à l’instant tk+1 temporaire //initialisation à l’instant 0 et dt tk=2*dt; //temps courant k=2; //source dirac au centre du carré (n impair !) (50000 pour f, 40 pour f") f=zeros(n,n); //f((n+1)/2,(n+1)/2)=80000.; f(si,sj)=900000; m_axes = gca(); 3 m_axes.axes_bounds = [0.25,0.05,0.75,0.95]; m_axes.margins = [0.05 0.05 0.05 0.05]; m_axes.auto_scale="off"; m_axes.visible="on"; xset("colormap",oceancolormap(256)); drawlater() Sgrayplot(x’,x’,uk, strf="040",zminmax=[-1,1]) drawnow() drawlater() //boucle itérative en temps, schéma saute-mouton while(tk<tf) set(ht,"string",string(tk)); fk=f*fonc_temps(tk,a,tau); v(2:n-1,2:n-1)=uk(3:n,2:n-1)+uk(1:n-2,2:n-1)+uk(2:n-1,3:n)+uk(2:n-1,1:n-2)-4*uk(2:n-1,2:n-1); ukp1=2*uk-ukm1+cfl2*v+dt2*fk; ukm1=uk; uk=ukp1; zmin=min(uk);zmax=max(uk);zmax=0.9*(zmax-zmin)/2;zmin=-zmax; drawlater() Sgrayplot(x’,x’,uk, strf="040",zminmax=[zmin,zmax]) tk=tk+dt; drawnow() end, endfunction // Window Parameters initialization frame_w = 200; frame_h = 600; plot_w = 600; plot_h = frame_h; margin_x = 15; margin_y = 15; defaultfont = "arial"; axes_w = 3*margin_x + frame_w + plot_w; axes_h = 2*margin_y + frame_h; gui_onde = scf(100001); gui_onde.background = -2; gui_onde.figure_position = [100 100]; gui_onde.figure_name = gettext("GUI Onde"); gui_onde.axes_size = [axes_w axes_h]; // // // // // // // // Frame width and height Plot width and heigh Horizontal and vertical margin for elements Default Font axes width axes height (100 =>toolbar height) Create window with id=100001 and make it the current one Background and text // Change dimensions of the figure delmenu(gui_onde.figure_id,gettext("&File")); // Remove Scilab graphics menus & toolbar delmenu(gui_onde.figure_id,gettext("&Tools")); delmenu(gui_onde.figure_id,gettext("&Edit")); delmenu(gui_onde.figure_id,gettext("&?")); toolbar(gui_onde.figure_id,"off"); title = uicontrol("parent",gui_onde, "style","text", ... "string","Simulation de l’’équation des ondes dans un carré", "units","pixels", ... "position",[0 630 500 40],... "fontname",defaultfont, "fontunits","points", ... "fontsize",20, "horizontalalignment","center", ... "background",[1 1 1], "tag","title_control"); // Frame parameters my_frame = uicontrol("parent",gui_onde, "relief","groove", ... "style","frame", "units","pixels", ... "position",[ margin_x margin_y frame_w frame_h], ... "horizontalalignment","center", "background",[1 1 1], ... "tag","frame_control"); // Frame title parameter my_frame_title = uicontrol("parent",gui_onde, "style","text", ... "string","paramètres", "units","pixels", ... 4 "position",[40+margin_x margin_y+frame_h-10 100 20],... "fontname",defaultfont, "fontunits","points", ... "fontsize",16, "horizontalalignment","center", ... "background",[1 1 1], "tag","title_frame_control"); // Adding model parameters h1 = frame_w; h1o = 350; // ordered list of labels labels1 = ["c","t0", "tf", "n", "dt", "cfl","a","tau","source i", "source j"]; tips = ["vitesse du son","temps initial","temps final","découpage suivant x et y", ... "pas de temps","cfl","rampe de la source gausienne","temps où la source gaussienne est maximale",... "abcisse de la source (indice dans la grille)","ordonnée de la source (indice dans la grille)"]; // ordered list of default values values1 = [1, 0, 5, 201, 0.01, 0, 10000, 0.04, 10, 10]; // positioning l1 = 25; l2 = 100; l3 = 80; drawlater for k=1:size(labels1,2) uicontrol("parent",gui_onde,"style","text",... "string",labels1(k), "position",[l1,h1-k*20+h1o,l2,20], ... "horizontalalignment","left", "fontsize",14, ... "background",[1 1 1],’TooltipString’,tips(k)); guientry1(k) = uicontrol("parent",gui_onde, "style","edit",... "string",string(values1(k)), "position",[l3,h1-k*20+h1o,120,20], ... "horizontalalignment","left", "fontsize",14, ... "background",[.9 .9 .9], "tag",labels1(k),’TooltipString’,tips(k)); end uicontrol("parent",gui_onde,"style","text",... "string",’t=’, "position",[55 60 80 20], ... "horizontalalignment","left", "fontsize",14, ... ’visible’,’off’,’tag’,’lt’,"background",[1 1 1]); uicontrol("parent",gui_onde, "style","edit",... "string",’0’, "position",[70 60 80 20], ... "horizontalalignment","left", "fontsize",14, ... "background",[.9 .9 .9],’visible’,’off’, "tag",’t’); // Adding button huibutton = uicontrol(gui_onde, "style","pushbutton", ... "Position",[70 100 80 20], "String","Calcul", ... "BackgroundColor",[.9 .9 .9], "fontsize",14, ... "tag","btn1","Callback","compute"); drawnow 5