A general numerical procedure for solidification and melting in porous media and free fluids