Generalized form of the magnetic anisotropy field in micromagnetic and atomistic spin models