Mesh refinement based on the solution

Hello,
I want to refine a mesh structure in the following ways:

1- based on a function; the meshes are finer as the function increases and the meshes are coarser as the function decreases.
2- based on the solution u.

Thank you for your help.