Can somebody explain in detail how a player's rank is affected when they lose against lower or upper-rank players in the different ATP tournaments? Is there a set metric for it?
ATP Rankings points are awarded based on the prestige of each tournament and which round the player reached in that tournament; the ranking of the opponent the player lost to has no impact (and neither do the rankings of the opponents the player beat). Points normally expire a year after they are gained, but this has been modified due to COVID.
For example, winning a Grand Slam tournament gains a player 2000 rankings points, while winning a "250 series" event gains the player 250 points. You now see where the names of the series comes from! Similarly, reaching the quarter-finals of a Grand Slam gains the player 360 points, while the quarter finals of a 250 series event gains the player 45 points. The full table can be found in the ATP Rankings FAQ in the "What is the points breakdown for all tournament categories?" section.