How to know if modeled volumes work?

Im wondering how could you pre check that a 3D geometry like gears will work before 3d printing it.
Like take for examples this picture:

How would you know this kind of joint will work, that the spacing left is optimal?
Or for example this picture:

Are the gears going to work smoothly between each other? Is there a way to simulate it or test it before printing and wasting money and material?
This is not mine, but it represents my curiosity on how this was calculated to work perfectly when printed, instead of having to print multiple times before reaching the optimal solution.

usually that kind of simulation is being done with software like solidworks, which also simulates the stress on the material. but i have found that grasshopper and kangaroo can be used for this to some extent depending on your ideas how to implement it, it does physical collision simulation so that could be a start.

or here another plugin which is maybe more specific called gear

The clearances you need between parts for different sorts of “fits” for gears, bearings, things that are supposed to move easily or not, there are engineering rules of thumb that you can basically look up. How well those numbers translate into 3D prints will depend on the particulars of the machine you’re using and will probably require some expertise from an experienced operator and some trial and error.