1. A method, comprising: obtaining, with a computer, an initial geophysical model; modeling, with the computer, a forward wavefield with viscoacoustic or viscoelastic wave equations; modeling, with the computer, an adjoint wavefield with adjoint viscoacoustic or adjoint viscoelastic wave equations, wherein the adjoint viscoacoustic wave equations are based on an auxiliary variable that is a function of pressure and a memory variable or the adjoint viscoelastic wave equations are based on a combination of stress and a memory variable, respectively; 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; and using, with the computer, the gradient of the cost function to update the initial geophysical model and obtain an updated geophysical model.