BUG: check wrong var when writing out gradient vectors