write simulation code