We describe an explicit second-order finite difference code based on a
total variation diminishing scheme for self-gravitating cosmological
hydrodynamic systems. The code has been developed to follow correctly
the adiabatic changes of extremely supersonic preshock flows with a Ma
ch number larger than 100 as well as very strong shocks. In highly sup
ersonic regions, we use an entropy-like variable switching to a more c
onventional total energy variable near to and interior to shocks. The
self-gravity has been included in such a way that the numerical errors
in calculating the gravitational force term do not induce the leakage
of the gravitational energy into the thermal energy of the gas. Also,
the gravitational force term has been corrected to take account of th
e mass diffusion around the shocks so the total energy can be conserve
d. Tests for the accuracy and performance of the code without gravity
have proved that it can accurately handle supersonic flows with a Mach
number larger than 10(4). In calculations of the formation of a one-d
imensional Zel'dovich pancake, an energy accuracy of 1% is obtained fo
r 32 cells per unit wavelength, and the accuracy reaches 0.01% as the
number of cells approaching 1024. To further test the code with gravit
y, three-dimensional simulations of a purely baryonic universe but wit
h the initial cold dark matter power-spectrum have been performed. The
results have shown that shocks are well resolved and separate cleanly
the hot, dense, collapsed peaks from the cold, low-density, expanding
voids. The thermal energy in low-density regions can be orders of mag
nitude lower using this scheme than in some others due to very careful
attention given to entropy in high Mach number regions. Various numer
ical experiments have proven that the code can handle the expanding lo
w density regions very well as well as conserve the total energy very
accurately.