• No results found

5.2 Code details

5.2.1 Hydrodynamical Code

Equations of hydrodynamics

The hydrodynamical code uses a Godunov scheme to solve the equations of magentohydrodyam- ics: ∂ ρ ∂t +∇ ·[ρv] =0 (5.1a) vρ ∂t +∇ ·[ρvv+P I] = f (5.1b) ∂E ∂t +∇ ·[(E+P)v] =GL (5.1c) E= P γ−1+ ρ(vv) 2 (5.1d)

whereρis density,vis vector velocity,Ethe total energy density,Pis the gas pressure, f represents additional forces such as gravity, G represents the heating gains, Lrepresents the cooling losses,Bis the magnetic field strength, I is the identity matrix andγis the adiabatic index. A polytropic equation of state is assumed such thatPργ. Although this method is able to solve the magnetohydrodynamic equations, magnetic fields are not considered in the rest of this work, thereforeB=0. The Godunov method described below is well documented and the code used here is similar to that described by Stone et al. (2008).

The variables in each of these equations can be split in to conservative and non-conservative variables, such that conservative variables include,ρ, Mx, My, Mz,E, Bx,By, Bz whereM=

ρvis the vector momentum density. These are classed as conservative variables as they are conserved in the system, as well as over cell boundaries. Non-conservative variables therefore includevx, vy, vz, P. In this manner, equations 5.1 a and d can be thought of as conservative

forms of partial differential equations as conservative variables are used to calculate the value of each equation at each timestep. Some hydrodynamical solvers use primitive rather than conservative variables and equations in order to solve the above equations (e.g. ZEUS Stone et al. (1992)), however the calculations carried out using these methods are less accurate as it is not possible to solve the equations exactly, leading to errors associated with source and sink terms.

The Godunov method splits the solving of these equations in to three parts. First the grid must be discretised, and values of the variables reconstructed throughout the grid. The Rie- mann problem set up at the cell boundaries must then be calculated, and finally the time derivatives must be solved for fluxes and evolved. Each of these methods will be briefly dis- cussed below.

Discretisation

The code solves the equations shown in the previous section on a uniform three dimensional grid in Cartesian coordinates. The three dimensions(x,y,z)are split in to(Nx,Ny,Nz)cells with size(Lx,Ly,Lz)and the cell denoted by position(i,j,k)has its centre located at(xi,yj,zk).

The interface between cells is therefore denoted by(xi1/2,yj1/2,zk1/2)and(xi+1/2,yj+1/2,zk+1/2). Time in the simulation is split in to a number of unequal intervals between the start of the sim- ulation t0 and the endtf, as discussed below.

Equations 5.1a-d can then be discretised by integrating over the volume of a cell. For example in one dimension equation 5.1a can be written as:

∂q ∂t +

F

∂x =0 (5.2)

whereqandFare,ρandρvrespectively, with similar equations for yandzdimensions. This can then be discretised using:

qni+1=qniδt δx(F n+1/2 i+1/2 −F n+1/2 i−1/2) (5.3)

Whereqni is the cell centred volume average value ofqandFin+11//22 is the cell edge, time av- eraged flux of Fat the interface. This is an exact solution to this point, however equations 5.1b and 5.1c cannot be solved exactly in this form as the right hand side of the equations is not equal to zero. Therefore the integrals are assumed to be equal to zero and the forces, heating and cooling added as sources and sinks at the end of the calculation. In this way these calculations are also exact.

5.2. Code details

Reconstruction

The value of each parameter is calculated at the cell centre, while fluxes are calculated at cell borders. It is therefore necessary to calculate the interface values by reconstructing the simulation grid. There are several different interpolation methods that can be used to do this. The simplest of these is a first order, piecewise linear reconstruction. In this case, it is assumed that the variables are constant throughout the cell, such that the value at each boundary is the same as the value at the cell centre. This is the least accurate method as it does not account for variation within the cell and may lead to large differences between values at cell interfaces. It is also possible to assume that the variables vary in a linear fashion throughout the cell. In this case, the interface values are determined by the value of the variables in neighbouring cells. Finally, a parabolic function may be chosen which again uses the neighbouring values to determine the cell interface values. In this way, the variables are interpolated to determine the interface values. It is important to determine the cell edge values as accurately as possible as these values set up a Riemann problem at the interfaces. As will be discussed further below, it is also necessary to determine the location of any shocks or physical discontinuities at this stage. The largest numerical errors can be introduced at this stage, as a lack of accuracy in determination of the value of each variable at the cell walls can lead to unrealistic fluxes as will be discussed below.

Reimann solvers

Figure 5.1 shows an example of the problem set up at each cell boundary, it is clear from this that there is a discontinuity formed at the interface, known as a Riemann problem. The next stage in the Godonuv method is therefore to determine the value of the fluxes of mass, momentum and energy, over the boundaries using a Riemann solver to smooth out the dis- continuity. The simplest Riemann solvers involve averaging the values at the cell boundaries, although this can be inaccurate. It is more accurate to take in to account the accuracy of the interpolation and weight the fluxes from each side accordingly. The method used here is an approximate solver as full Riemann solvers can be too computationally expensive.

In this approximation, the time averaged fluxes are calculated based on left and rightward moving sound wave velocities. In this case, the state to the left of the boundary is assumed to beqi and the rightqi+1and the flux across the boundary is assumed to be given by:

i-1 i-1/2 i

qi-1 qL,i-1/2

qR,i-1/2 qi

Figure 5.1: An example of a Riemann problem.qis the variable being advected over the cell boundary.

Fi1/2=

b+fL,i1/2bfR,i1/2

b+−b− +

b+b

b+−b−(qiqi−1) (5.4) (Stone et al., 2008) wherefL,i−1/2 andfR,i−1/2 are fluxes calculated using the left and right

states,b+=vR+cR,b−=vL+cLwherevRandvLare the velocities normal to the interface and

cRandcLare the sound speeds. This is therefore a weighted average based on the velocities in each cell. At this stage numerical errors may be introduced to the simulation, although these are largely based on the accuracy of the interpolation, as the equation above is a weighted average of the flux of the states determined by the reconstruction of the simulation grid.

Shocks

As discussed in the previous section, the value of fluxes across cell boundaries sets up a nu- merical Riemann problem in these areas. However there are cases where the discontinuity in variables over cell boundaries is a physical (real) property that results from shocks. It is therefore necessary to be careful when using a Riemann solver to reduce the gap between the variables on either side of the boundary. The solvers used here, and discussed above take the sound speed in the gas into account to determine the discontinuity that should result naturally from shocks, and then ensure that this physical jump is not smoothed out completely.

5.2. Code details

CFL condition

It is then necessary to determine a new timestep for the simulation. The choice of timestep is important because if the timestep is too long, it is possible that all the material in a cell will be advected though the simulation cell in a single timestep before the fluxes are calculated resulting in negative pressures or densities. To avoid this, it is necessary that the new timestep must obey the CFL condition (Courant, Friedrichs & Lewy, 1928):

d tC F L< ∆x

maxspeed (5.5)

where maxspeedis the maximum speed in the simulation domain, and could be either the veloc-

ity of the gas, the sound speed or the Alfven speed. In the case of a three dimensional simula- tion the yandzcell sizes must also be considered. Decreasing the timestep size gives a better discretisation of the simulation time, resulting in higher precision although this increases the computational run time of the simulation. A compromise must therefore be reached between the precision of the simulation and the running time.

Time integration

The simulation is then evolved in time using an Euler or “Runge-Kutta” integrator such that an approximation to the conservative variables are determined at time tn+1 (see equation 5.3). This method increments the timestep to calculate the value of each variable at each increment and then uses weighted averages of the increments to determine the value at the end of the timestep. At this stage the user has the option to use first to fourth order approximations, with increasing orders leading to greater accuracy. This integration is another source of numerical errors in this scheme, although these are smaller than those that arise from the reconstruction and interpolation.