A method is presented for finding the boundary conditions for computer simulations based on molecular statics models. The boundary condition is expressed as a mapping from the atoms inside the boundary to the atoms outside and it models the effect of the surrounding atoms, which are too many to be modeled explicitly. The boundary condition is computed with a variational approach using lattice statics Green's functions as test functions. As examples, these boundary conditions are applied to systems with dislocations and cracks.