# Unsteady simulations

![Unsteady simulation of a NACA4412 wing](https://81269019-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MV8htLPXN-_fWc_anY_%2F-MVX86KbflOuXvUcmDLe%2F-MVX8XClSgLnVkHkLy7V%2Fnaca4412_cp_unsteady.png?alt=media\&token=5dba9ea8-7425-43b1-a10a-b92f86659fa0)

For unsteady simulations the APM Solver assumes that the wake is free, i.e. the wake is allowed to deform and interact with itself and the body. The following options in the **.conf** file are changed:

* `dt` - the time step size in seconds&#x20;
* `N_timesteps` - the number of timesteps&#x20;
* `output_frequency` - the results output frequency

Two of the three options are identical to the ones for steady simulations, however, their values differ. The time step, `dt`, should be selected such as to allow proper development of the wake. As an initial estimate `dt` should be selected as the time it takes the flow to travel one chord length. If the airspeed is `norm_V_ref=10` m/s and the `c_ref=1` m, `dt` should be approximately 0.1s. The number of time steps `N_timesteps` should be sufficiently high to allow the aerodynamic loads to stabilise. The `output_frequency` option instructs APM how often to output results.&#x20;

{% hint style="info" %}
If the output\_frequency option is equal to 10, the APM Solver will output results every 10 timesteps. The values for airspeed and chord are arbitrary and are used for illustrative purposes.
{% endhint %}

## Custom motion

![Unsteady simulation of a NACA4412 wing with custom motion](https://81269019-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MV8htLPXN-_fWc_anY_%2F-MVX86KbflOuXvUcmDLe%2F-MVX8a3r8IOv1JvrFm5W%2Fnaca4412_cp_unsteady_motion.png?alt=media\&token=0fca715d-6b9f-47e1-a14b-532b71948cf3)

Custom motion can be specified, if required, during unsteady simulations. If a **.motion** file is created in the current working directory the APM solver will ignore the `dt`and `N_timesteps`options specified in the **.conf** file and will use the time step and number of time steps specified in the **.motion** file. The unsteady motion file contains 10 variable per row - $$t$$,$$u$$,$$v$$,$$w$$,$$p$$,$$q$$,$$r$$,$$\phi$$,$$\theta$$, and $$\psi$$. The variables represent the velocity components, rotational rates, and kinematic angles at each time step. An example motion is a sinusoidal motion with a variation of the angle of attack given by:&#x20;

$$
\alpha=Asin(\omega t)\text{,}\\\omega=2\pi f\text{,}
$$

where $$f$$ is the frequency of the motion and $$A$$ is the amplitude of the angle of attack. The $$u$$and$$w$$ variables can be determined from the equation for $$\alpha$$:

$$
tan(\alpha)=\frac{u}{w}\\|V\_{ref}|=\sqrt{u^2+w^2}\ v=p=q=r=\phi=\theta=\psi=0
$$

The image above shows an unsteady simulation of a NACA4412 wing with custom **.motion** file. The structure of an example **.motion** file is shown below:

```
0.000000 1.000000 0.000000 0.000000 1.370778 0.000000 0.000000 0.000000 0.000000 0.000000
0.010000 1.000000 0.000000 0.000000 1.353902 0.000000 0.000000 0.013651 0.000000 0.000000
0.020000 1.000000 0.000000 0.000000 1.303688 0.000000 0.000000 0.026967 0.000000 0.000000
0.030000 1.000000 0.000000 0.000000 1.221372 0.000000 0.000000 0.039618 0.000000 0.000000
0.040000 1.000000 0.000000 0.000000 1.108983 0.000000 0.000000 0.051294 0.000000 0.000000
...
```

Below is an example MATLAB file which can be used to generate a **.motion** file.

```
clear all;
close all;
clc;

V_ref=1; %freestream velocity (m/s)
c_ref=1; %reference chord
t_max=c_ref*100/V_ref; %maximum simulation time
N_cycles=3; %number of oscillation cycles
N_timesteps=50; %number of timesteps
dt=t_max/N_timesteps; %timestep size (s)
t=0:dt:t_max;
V_ref=1; % reference velocity
A=deg2rad(5);  %oscillation amplitude (deg)
f=N_cycles/t_max; %oscillation frequency (Hz)
omega=2*pi*f;

phi=A*sin(omega*t);
theta=A*sin(omega*t);
psi=A*sin(omega*t);
p=A*cos(omega*t)*omega;
q=A*cos(omega*t)*omega;
r=A*cos(omega*t)*omega;


motion_file=fopen('rolling.motion','w');

for  n=1:length(t)
    
    t_out=t(n);
    u_out=V_ref;
    v_out=0;
    w_out=0;
    p_out=p(n);
    q_out=0;
    r_out=0;
    phi_out=phi(n);
    theta_out=0;
    psi_out=0;
   
    fprintf(motion_file,'%0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f\n',t_out,u_out,v_out,w_out,p_out,q_out,r_out,phi_out,theta_out,psi_out);
    
end

fclose(motion_file);

motion_file=fopen('pitching.motion','w');

for  n=1:length(t)
    
    u=V_ref*1/sqrt(1+tan(theta(n))^2);
    w=u*tan(theta(n));
    
    t_out=t(n);
    u_out=u;
    v_out=0;
    w_out=w;
    p_out=0;
    q_out=q(n);
    r_out=0;
    phi_out=0;
    theta_out=theta(n);
    psi_out=0;
   
    fprintf(motion_file,'%0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f\n',t_out,u_out,v_out,w_out,p_out,q_out,r_out,phi_out,theta_out,psi_out);
    
end

fclose(motion_file);

motion_file=fopen('yawing.motion','w');

for  n=1:length(t)
    
    v=V_ref*sin(-psi(n));
    w=0;
    u=sqrt(V_ref^2-v^2-w^2);
    
    t_out=t(n);
    u_out=u;
    v_out=v;
    w_out=w;
    p_out=0;
    q_out=0;
    r_out=r(n);
    phi_out=0;
    theta_out=0;
    psi_out=psi(n);
   
    fprintf(motion_file,'%0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f %0.6f\n',t_out,u_out,v_out,w_out,p_out,q_out,r_out,phi_out,theta_out,psi_out);
    
end

fclose(motion_file);
```
