We presents a numerical method for following the hydrodynamics of a gaseous component in 3D cosmological simulations containing both gas and collisionless components. The code is the merged product of two existing approaches – the high-resolution P3M N-body program developed by Efstathiou & Eastwood, and SPH, the smoothed particle hydrodynamics approach of Monaghan & Gingold. The gas physics incorporated within the model includes self-gravity, the gravity from additional collisionless components, adiabatic heating, heating due to shocks, and radiative cooling. The use of variable spatial resolution allows reliable definition of a wide dynamic range. This makes the code optimal for tackling the general problem of clustering from an initially Gaussian random field. Tests of the method are presented, with emphasis on following the collapse and shock-heating of initially cold clouds. This is done both for self-gravitating evolution and for the case of a spherical rich cluster in an Ω = 1 universe with 10 per cent gas and 90 per cent dark matter. Resolution limits for applications to the evolution of X-ray gas in rich clusters and to the galaxy formation problem are estimated to demonstrate that interesting problems can be tackled with even moderate micro-computing capacity.