Unified treatment of forward and inverse problems in the numerical simulation of tokamak plasmas