Beam matching with space charge in energy recovery linacs