  We present a new method of Stokes inversion of spectropolarimetric data and
evaluate it by taking the example of a SUNRISE/IMaX observation. An archive of
synthetic Stokes profiles is obtained by the spectral synthesis of
state-of-the-art magnetohydrodynamics (MHD) simulations and a realistic
degradation to the level of the observed data. The definition of a merit
function allows the archive to be searched for the synthetic Stokes profiles
that match the observed profiles best. In contrast to traditional Stokes
inversion codes, which solve the Unno-Rachkovsky equations for the polarized
radiative transfer numerically and fit the Stokes profiles iteratively, the new
technique provides the full set of atmospheric parameters. This gives us the
ability to start an MHD simulation that takes the inversion result as initial
condition. After a relaxation process of half an hour solar time we obtain
physically consistent MHD data sets with a target similar to the observation.
The new MHD simulation is used to repeat the method in a second iteration,
which further improves the match between observation and simulation, resulting
in a factor of 2.2 lower mean $\chi^2$ value. One advantage of the new
technique is that it provides the physical parameters on a geometrical height
scale. It constitutes a first step towards inversions giving results consistent
with the MHD equations.
