Numerical Simulation Of Astrophysical Gas Dynamics, And Application To The Gravitational Stability Of Protostellar Discs