%% File Description. % % Name: illustrationWaveTypes.m % Author: Phillip Petcher % Creation date: 02/05/2014 % Version: 0.1020140502 % Description: Creates a nice animation (and optionally an animated gif) of the oscillations for compression, shear vertical, and shear horizontal waves. % This was only written to create a one-off diagram, so the code is not very clean. % % % Copyright (c) 2014, Phillip Anthony Petcher % All rights reserved. % % Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: % % - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. % - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. % % THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. % % %% Preliminaries. close all hidden; clear; clc; %% User input. Amplitude = 1.5; % The oscillation amplitude (1 is equal to the grid spacing, 1.5 looks good). Cycles = 1; % How many wavelengths in the pulse for the static display (1 looks good). CyclesAnimated = 5; % How many wavelengths in the pulse for the animated display (5 looks good). Extent = 16 * Cycles; % How many grid points the pulse is for the static case (16 * Cycles looks good, Extent < LengthX). ExtentAnimated = 16 * CyclesAnimated; % How many grid points the pulse is for the animated case (16 * CyclesAnimated looks good). LengthX = 32; % The number of grid points in the x-direction (the direction of wave propagation). LengthY = 8; % The number of grid points in the y-direction (the oscillation direction for shear vertical waves). LengthZ = 4; % The number of grid points in the z-direction (the oscillation direction for shear horizontal waves). AngleView = 45; % The pseudo-3D view angle (degrees, 45 looks good). FigureAnimationScale = 2; % How much to make the figure box shorter in the y-direction for the animated display (for example, 2 means that the height of the figure box is halved). DelayTime = 0.1; % The time between frames for the animated gif (10 frames a second, a delay time of 0.1, looks good). CreateGif = false; % If an animated gif should be created and saved in the current directory, set this to true, otherwise set it to false, and it is only shown on screen. %% Create the grid for the pulse to propagate over. PositionsX(:,1,1) = 0:1:(LengthX - 1); PositionsY(1,:,1) = 0:1:(LengthY - 1); PositionsZ(1,1,:) = 0:1:(LengthZ - 1); GridX = repmat(PositionsX,[1 LengthY LengthZ]); GridY = repmat(PositionsY,[LengthX 1 LengthZ]); GridZ = repmat(PositionsZ,[LengthX LengthY 1]); %% Create the shifts of the grid positions that represent the pulse. PadLength = (LengthX - Extent) / 2; Shifts(:,1) = Amplitude .* sind((0:1:(Extent - 1)) .* (360 * Cycles / (Extent - 1))); ShiftsAnimated(:,1) = Amplitude .* sind((0:1:(ExtentAnimated - 1)) .* (360 * CyclesAnimated / (ExtentAnimated - 1))); %Shifts = Shifts .* tukeywin(Extent,0.2); ShiftsAnimated = ShiftsAnimated .* tukeywin(ExtentAnimated,0.2); GridS = [zeros(floor(PadLength),1); Shifts; zeros(ceil(PadLength),1)]; BaseX = GridX + GridZ .* cosd(AngleView); BaseY = GridY - GridZ .* sind(AngleView); %% Create the static pulses. ClX = bsxfun(@plus,BaseX,GridS); ClY = BaseY; SvX = BaseX; SvY = bsxfun(@plus,BaseY,GridS); ShX = bsxfun(@plus,BaseX,GridS .* cosd(AngleView)); ShY = bsxfun(@minus,BaseY,GridS .* sind(AngleView)); %% Plot the static pulses in 2D. figure('Color','w','InvertHardcopy','off'); plot(ClX(:,:,1),ClY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ClX(:,:,1),[2 1]),permute(ClY(:,:,1),[2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); figure('Color','w','InvertHardcopy','off'); plot(SvX(:,:,1),SvY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(SvX(:,:,1),[2 1]),permute(SvY(:,:,1),[2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); figure('Color','w','InvertHardcopy','off'); plot(ShX(:,:,1),ShY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ShX(:,:,1),[2 1]),permute(ShY(:,:,1),[2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); %% Plot the static pulses in pseduo-3D. figure('Color','w','InvertHardcopy','off'); plot(ClX(:,:,1),ClY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ClX(:,:,1),[2 1 3]),permute(ClY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(ClX(:,1,:),[1 3 2]),permute(ClY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ClX(:,1,:),[3 1 2]),permute(ClY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(ClX(end,:,:),[2 3 1]),permute(ClY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ClX(end,:,:),[3 2 1]),permute(ClY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); figure('Color','w','InvertHardcopy','off'); plot(SvX(:,:,1),SvY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(SvX(:,:,1),[2 1 3]),permute(SvY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(SvX(:,1,:),[1 3 2]),permute(SvY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(SvX(:,1,:),[3 1 2]),permute(SvY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(SvX(end,:,:),[2 3 1]),permute(SvY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(SvX(end,:,:),[3 2 1]),permute(SvY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); figure('Color','w','InvertHardcopy','off'); plot(ShX(:,:,1),ShY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ShX(:,:,1),[2 1 3]),permute(ShY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(ShX(:,1,:),[1 3 2]),permute(ShY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ShX(:,1,:),[3 1 2]),permute(ShY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(ShX(end,:,:),[2 3 1]),permute(ShY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(ShX(end,:,:),[3 2 1]),permute(ShY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); %% Create and plot the animated pulses. FigureAnimation = figure('Color','w','InvertHardcopy','off'); FigurePosition = get(FigureAnimation,'Position'); set(FigureAnimation,'Position',[FigurePosition([1 2 3]) (FigurePosition(4) / FigureAnimationScale)]); % Compression. GridSExpandX = [zeros(LengthX - 1,LengthY,LengthZ); repmat(ShiftsAnimated,[1 LengthY LengthZ]); zeros(LengthX - 1,LengthY,LengthZ)]; GridSExpandY = zeros(LengthX * 2 + ExtentAnimated - 2,LengthY,LengthZ); LimitsX = [(min(min(min(BaseX))) - Amplitude) (max(max(max(BaseX)) + Amplitude))]; LimitsY = [(min(min(min(BaseY))) - Amplitude) (max(max(max(BaseY)) + Amplitude))]; IndexStart = 0; IndexEnd = LengthX - 1; figure(FigureAnimation); while IndexEnd < (LengthX * 2 + ExtentAnimated - 2) IndexStart = IndexStart + 1; IndexEnd = IndexEnd + 1; CurrentX = BaseX + GridSExpandX(IndexEnd:-1:IndexStart,:,:); CurrentY = BaseY - GridSExpandY(IndexEnd:-1:IndexStart,:,:); plot(CurrentX(:,:,1),CurrentY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,:,1),[2 1 3]),permute(CurrentY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(:,1,:),[1 3 2]),permute(CurrentY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,1,:),[3 1 2]),permute(CurrentY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(end,:,:),[2 3 1]),permute(CurrentY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(end,:,:),[3 2 1]),permute(CurrentY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); xlim(LimitsX); ylim(LimitsY); if CreateGif FigureFrame = getframe(FigureAnimation); FigureImage = frame2im(FigureFrame); [FigureImageIndexed, FigureMap] = rgb2ind(FigureImage,256); FigureMapWhiteIndex = find(all(FigureMap == 1,2)) - 1; switch IndexStart case 1 imwrite(FigureImageIndexed,FigureMap,'Compression.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'BackgroundColor',FigureMapWhiteIndex,'Loopcount',inf); otherwise imwrite(FigureImageIndexed,FigureMap,'Compression.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'WriteMode','append'); end else pause(DelayTime); end end % Shear vertical. GridSExpandX = zeros(LengthX * 2 + ExtentAnimated - 2,LengthY,LengthZ); GridSExpandY = [zeros(LengthX - 1,LengthY,LengthZ); repmat(ShiftsAnimated,[1 LengthY LengthZ]); zeros(LengthX - 1,LengthY,LengthZ)]; LimitsX = [(min(min(min(BaseX))) - Amplitude) (max(max(max(BaseX)) + Amplitude))]; LimitsY = [(min(min(min(BaseY))) - Amplitude) (max(max(max(BaseY)) + Amplitude))]; IndexStart = 0; IndexEnd = LengthX - 1; figure(FigureAnimation); while IndexEnd < (LengthX * 2 + ExtentAnimated - 2) IndexStart = IndexStart + 1; IndexEnd = IndexEnd + 1; CurrentX = BaseX + GridSExpandX(IndexEnd:-1:IndexStart,:,:); CurrentY = BaseY - GridSExpandY(IndexEnd:-1:IndexStart,:,:); plot(CurrentX(:,:,1),CurrentY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,:,1),[2 1 3]),permute(CurrentY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(:,1,:),[1 3 2]),permute(CurrentY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,1,:),[3 1 2]),permute(CurrentY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(end,:,:),[2 3 1]),permute(CurrentY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(end,:,:),[3 2 1]),permute(CurrentY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); xlim(LimitsX); ylim(LimitsY); if CreateGif FigureFrame = getframe(FigureAnimation); FigureImage = frame2im(FigureFrame); [FigureImageIndexed, FigureMap] = rgb2ind(FigureImage,256); FigureMapWhiteIndex = find(all(FigureMap == 1,2)) - 1; switch IndexStart case 1 imwrite(FigureImageIndexed,FigureMap,'ShearVertical.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'BackgroundColor',FigureMapWhiteIndex,'Loopcount',inf); otherwise imwrite(FigureImageIndexed,FigureMap,'ShearVertical.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'WriteMode','append'); end else pause(DelayTime); end end % Shear horizontal. GridSExpandX = [zeros(LengthX - 1,LengthY,LengthZ); repmat(ShiftsAnimated .* cosd(AngleView),[1 LengthY LengthZ]); zeros(LengthX - 1,LengthY,LengthZ)]; GridSExpandY = [zeros(LengthX - 1,LengthY,LengthZ); repmat(ShiftsAnimated .* sind(AngleView),[1 LengthY LengthZ]); zeros(LengthX - 1,LengthY,LengthZ)]; LimitsX = [(min(min(min(BaseX))) - Amplitude) (max(max(max(BaseX)) + Amplitude))]; LimitsY = [(min(min(min(BaseY))) - Amplitude) (max(max(max(BaseY)) + Amplitude))]; IndexStart = 0; IndexEnd = LengthX - 1; figure(FigureAnimation); while IndexEnd < (LengthX * 2 + ExtentAnimated - 2) IndexStart = IndexStart + 1; IndexEnd = IndexEnd + 1; CurrentX = BaseX + GridSExpandX(IndexEnd:-1:IndexStart,:,:); CurrentY = BaseY - GridSExpandY(IndexEnd:-1:IndexStart,:,:); plot(CurrentX(:,:,1),CurrentY(:,:,1),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,:,1),[2 1 3]),permute(CurrentY(:,:,1),[2 1 3]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(:,1,:),[1 3 2]),permute(CurrentY(:,1,:),[1 3 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(:,1,:),[3 1 2]),permute(CurrentY(:,1,:),[3 1 2]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); line(permute(CurrentX(end,:,:),[2 3 1]),permute(CurrentY(end,:,:),[2 3 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','none'); line(permute(CurrentX(end,:,:),[3 2 1]),permute(CurrentY(end,:,:),[3 2 1]),'Color',[0.8 0.8 0.8],'LineStyle','-','LineWidth',0.5,'Marker','o','MarkerEdgeColor',[0.6 0.6 0.6],'MarkerFaceColor',[0.8 0.0 0.0],'MarkerSize',4); axis('ij'); axis('image'); axis('off'); xlim(LimitsX); ylim(LimitsY); if CreateGif FigureFrame = getframe(FigureAnimation); FigureImage = frame2im(FigureFrame); [FigureImageIndexed, FigureMap] = rgb2ind(FigureImage,256); FigureMapWhiteIndex = find(all(FigureMap == 1,2)) - 1; switch IndexStart case 1 imwrite(FigureImageIndexed,FigureMap,'ShearHorizontal.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'BackgroundColor',FigureMapWhiteIndex,'Loopcount',inf); otherwise imwrite(FigureImageIndexed,FigureMap,'ShearHorizontal.gif','gif','DisposalMethod','restoreBG','DelayTime',DelayTime,'TransparentColor',FigureMapWhiteIndex,'WriteMode','append'); end else pause(DelayTime); end end