Automatic mesh refinement in layered structures