How to animate background flow field that an object is moving through?

Hi there!
I now have a decent code that solves an ode model of mine, using ode45. It is about a rigid plate falling freely through a fluid. It is a 2D problem, so the background flow field is pretty simple, modeled with, say, the velocity field vf = 0i + 5*j, for a uniform, vertical wind updraft that the plate can hover in. I'm also cooking up some more involved 2D flows: for instance, flows that might be periodic in time.
Currently, I wrote a live animation for the trajectory of the plate falling through the fluid, by using a while loop and also manipulating plot( ) and quiver( ) graphics objects. However, I'd like to add to this animation by also animating the flow field, in order to gain intuition about what's going on with both the plate and the flow.
How can I animate a simple flow field?
I'm thinking that using arrows could be nice; I'm also thinking of using dots that trace out the path of the flow.
Thanks!

 Respuesta aceptada

Umar
Umar el 20 de Oct. de 2024

Hi @ Noob,

After going through your comments, in order to achieve a comprehensive animation of both the falling plate and the flow field in MATLAB, you can utilize the quiver function as mentioned in your comments by using arrows representing the flow and plot for the trajectory of the plate. Below is a detailed explanation and the complete code to implement this animation. First create a grid of points in the 2D space where the flow field will be evaluated. For a uniform vertical wind updraft, the velocity field. The plate's motion will be governed by the ordinary differential equations (ODEs) that describe its falling behavior through the fluid by using ode45 to solve these equations, create a loop that updates the position of the plate and the flow field at each time step, using quiver to display the flow and plot to show the plate's trajectory. To visualize the flow path, you can store the previous positions of the flow and plot them as dots.

% Parameters
g = 9.81; % Acceleration due to gravity (m/s^2)
h = 0.1; % Height of the plate (m)
rho_f = 1.225; % Density of fluid (kg/m^3)
A = 0.1; % Cross-sectional area of the plate (m^2)
Cd = 1.0; % Drag coefficient
v_updraft = 5; % Velocity of the updraft (m/s)
% Initial conditions
y0 = [0; 0]; % Initial position [x; y]
v0 = [0; -1]; % Initial velocity [vx; vy]
% Time span
tspan = [0 10]; % Time from 0 to 10 seconds
% ODE function
odefun = @(t, y) [y(2); -g + (0.5 * rho_f * Cd * A * (v_updraft - y(2))^2) / 
(rho_f * A)];
% Solve ODE
[t, y] = ode45(odefun, tspan, y0);
% Create a grid for the flow field
[x, y_grid] = meshgrid(-5:0.5:5, -5:0.5:5);
u = zeros(size(x)); % x-component of velocity
v = v_updraft * ones(size(y_grid)); % y-component of velocity
% Create figure for animation
figure;
hold on;
axis equal;
xlim([-5 5]);
ylim([-5 5]);
title('Plate Falling Through Fluid with Flow Field');
xlabel('X Position (m)');
ylabel('Y Position (m)');
% Initialize plot objects
h_plate = plot(y(:,1), y(:,2), 'ro', 'MarkerSize', 10, 'MarkerFaceColor', 'r');
 % Plate
h_flow = quiver(x, y_grid, u, v, 'b'); % Flow field arrows
h_path = plot(nan, nan, 'g.'); % Path tracing
% Animation loop
for i = 1:length(t)
  % Update plate position
  set(h_plate, 'XData', y(i,1), 'YData', y(i,2));
    % Update flow field arrows
    set(h_flow, 'UData', u, 'VData', v);
    % Update path tracing
    set(h_path, 'XData', [get(h_path, 'XData'), y(i,1)], ...
                 'YData', [get(h_path, 'YData'), y(i,2)]);
    % Pause for animation effect
    pause(0.1);
  end
hold off;

Please see attached.

Again, utilizing quiver for the flow representation and plot for the plate's trajectory, you can gain valuable insights into the dynamics of the system.

Feel free to modify the parameters and flow characteristics to explore different scenarios!

Hope this helps.

7 comentarios

Hi Umar!
The flow field that the plate meets will change with time, let's say it's v_fluid = cos(t) * i + 0 * j. Your animation code comes after the ode45 solver solves the equations. Then you update the plate's trajectory by pulling it's x, y solution from the solution vectors that ode45 gives you. However, how do you update the flow field's movement, if the ode model was for the plate's trajectory, and not for the motion of the flow field?
I hope that makes sense.
I think you seem to be using the plate's new position as the flow's new position; is that correct to do? I already have a good animation code for the plate trajectory, using a while loop and tic and toc and drawnow; so I just want to augment it with an animation of the flow field that's changing with time t, if possible.
Thanks!

Hi @Noob,

To update the flow field in your MATLAB animation while the plate's trajectory is being computed, you can modify the flow field's velocity components based on the current time t during each iteration of your animation loop. Here is how you can achieve this:

Define the Flow Field: Before the animation loop, define the flow field as a function of time. For example:

v_fluid_x = cos(t); % x-component of the fluid velocity
v_fluid_y = zeros(size(y_grid)); % y-component remains constant

Update Flow Field in the Animation Loop: Inside your animation loop, update the flow field based on the current time t(i):

% Update flow field arrows
v_fluid_x = cos(t(i)); % Update x-component based on current time
v = v_fluid_x * ones(size(y_grid)); % Update y-component if     needed
set(h_flow, 'UData', u, 'VData', v);

Use drawnow: To ensure the animation updates correctly, use drawnow at the end of each loop iteration. Here’s a snippet of how your animation loop might look:

for i = 1:length(t)
  % Update plate position
  set(h_plate, 'XData', y(i,1), 'YData', y(i,2));
    % Update flow field based on current time
    v_fluid_x = cos(t(i));
    v = v_fluid_x * ones(size(y_grid));
    set(h_flow, 'UData', u, 'VData', v);
    % Update path tracing
    set(h_path, 'XData', [get(h_path, 'XData'), y(i,1)], ...
                 'YData', [get(h_path, 'YData'), y(i,2)]);
    % Pause for animation effect
    pause(0.1);
    drawnow; % Ensure the figure updates
  end

This approach will allow you to visualize how the flow field changes over time while the plate's trajectory is being animated.

Hope this helps.

Noob
Noob el 20 de Oct. de 2024
Editada: Noob el 20 de Oct. de 2024
Hi Umar,
But won't cos(t) and then the zeros(size(y_grid)) then cause incompatible sizes for the meshgrid to form, showing the flow in the background? And, I get those incompatible size errors with the quiver( ) function, something about the size of x needing to match that of u, and the size of y needing to match that of v.
For instance, what if there are 1,000 time points t, but your grid is 250 x 250?
Noob
Noob el 20 de Oct. de 2024
Editada: Noob el 20 de Oct. de 2024
Hi Umar,
If I'm not mistaken, your approach would show one flow vector at one instant in time. I'm afraid I can't ask a better question at the moment. So, whatever you can say, I'll consider it and try it. The trajectory animation was relatively easy. Animating the flow seems much harder?
Thanks for all of your help!

Hi @Noob,

To effectively animate the flow field alongside the plate's trajectory, you will need to ensure that the dimensions of your grid and the flow vectors align properly. Here is a step-by-step approach to address your concerns:

Define Your Mesh Grid: Before entering your animation loop, create a mesh grid that will serve as the basis for your flow field visualization.

   [x_grid, y_grid] = meshgrid(linspace(-5, 5, 250), linspace(-5, 
   5, 250)); 
   % Adjust ranges as needed

Initialize Flow Field Components: Create variables for the flow field components based on the mesh grid dimensions.

   v_fluid_x = cos(0) * ones(size(x_grid)); % Initialize with t=0
   v_fluid_y = zeros(size(y_grid)); % Remains constant

Animation Loop: In your animation loop, update both the plate's position and the flow field vectors at each time step. Here is how you can structure this:

   for i = 1:length(t)
       % Update plate position
       set(h_plate, 'XData', y(i,1), 'YData', y(i,2));
       % Update flow field based on current time
       v_fluid_x = cos(t(i)) * ones(size(x_grid)); 
       % Update x-component
       v_fluid_y = zeros(size(y_grid)); % Keep y-component constant
       % Update quiver plot (ensure u and v match grid sizes)
       set(h_flow, 'UData', v_fluid_x, 'VData', v_fluid_y);
       % Update path tracing
       set(h_path, 'XData', [get(h_path, 'XData'), y(i,1)], ...
                    'YData', [get(h_path, 'YData'), y(i,2)]);
       % Pause for animation effect
       pause(0.1);
       drawnow; % Ensure figure updates
   end

Here are some insights to share that can help further.

Size Compatibility: To avoid dimension mismatch errors with quiver(), ensure that v_fluid_x and v_fluid_y are both matrices of size equal to that of your mesh grid (250x250 in this case). The use of ones(size(x_grid)) ensures this compatibility.

Flow Visualization: If you want to visualize how the flow changes over time more dynamically, consider adding a color gradient or varying arrow lengths based on the velocity magnitude.

Performance Considerations: If your animation becomes slow due to rendering many arrows at each frame, consider reducing the number of arrows displayed or using a subset of points for visualization.

Debugging Dimension Errors: Always check the dimensions of your variables right before passing them to functions like quiver(). Use MATLAB’s size() function to confirm they match.

By following this structured approach, you should be able to create an engaging and informative animation that accurately represents both the plate's trajectory and the changing flow field over time.

If you encounter further issues or need clarification on specific parts of this process, feel free to ask!

Noob
Noob el 21 de Oct. de 2024
Editada: Noob el 21 de Oct. de 2024
Hi Umar!
I think I'll hold off on this, for now, and keep focus on the mathematical modeling and simulations, since there is so much to do there (and animating a flow field seems hard to do). So, for now, plate trajectories using live animation is sufficient, I think. Thanks so much for your time and patience!
Hi @Noob,
Thank you for your thoughtful feedback regarding the project direction. I completely understand your decision to prioritize mathematical modeling and simulations at this stage, as it is indeed a crucial area with much to explore. Focusing on plate trajectories using live animation sounds like a practical approach, and I appreciate your clarity in outlining the current priorities. Should you need any assistance or further discussion on this topic, please do not hesitate to reach out. Thank you once again for your insights and collaboration.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Animation en Centro de ayuda y File Exchange.

Productos

Versión

R2024b

Etiquetas

Preguntada:

el 20 de Oct. de 2024

Comentada:

el 22 de Oct. de 2024

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by