Finite difference and finite element grid optimization by the grid iteration method