Water hammer is caused in pipe lines when