1. A method of managing hydrocarbons, comprising: obtaining, with a computer, an initial geophysical model; modeling, with the computer, a forward wavefield with viscoacoustic or viscoelastic wave equations, wherein the forward wavefield with viscoacoustic wave equations is based on a first auxiliary variable that is a function of both pressure and a memory variable or the forward wavefield with viscoelastic wave equations is based on a second auxiliary variable that is a function of both stress and a memory variable, respectively, and the first or second auxiliary variable is chosen to eliminate a velocity term from the forward wavefield with viscoacoustic or viscoelastic wave equations; modeling, with the computer, an adjoint wavefield with adjoint viscoacoustic or adjoint viscoelastic wave equations, which are derived from the forward viscoacoustic or the forward viscoelastic wave equations, respectively, and are based on an adjoint of the first or second auxiliary variable, respectively, wherein the modeling includes causing a spatial derivative of the memory variable to not be present in the adjoint viscoacoustic or viscoelastic wave equations by a change of variables comprising replacing the memory variable with the first or second auxiliary variable in the viscoacoustic or viscoelastic wave equations, respectively, thereby causing the adjoint viscoacoustic or viscoelastic wave equations to include spatially-pointwise operations of the first or second auxiliary variable, respectively, instead of the spatial derivative operations of the memory variable; obtaining, with the computer, a gradient of a cost function based on a combination of a model of the forward wavefield and a model of the adjoint wavefield; updating, with the computer, the initial geophysical model with the gradient of the cost function, and obtaining and displaying an updated geophysical model; and managing hydrocarbons using the updated geophysical model.