Simulation of ion optics using particle-in-cell and treecode methods