Skip to content

mantaflow: Avoid computation errors by using hypot for vector norm…#14

Open
bartoszkosiorek wants to merge 2 commits intothunil:masterfrom
bartoszkosiorek:hypot
Open

mantaflow: Avoid computation errors by using hypot for vector norm…#14
bartoszkosiorek wants to merge 2 commits intothunil:masterfrom
bartoszkosiorek:hypot

Conversation

@bartoszkosiorek
Copy link

@bartoszkosiorek bartoszkosiorek commented Apr 8, 2025

…alization functions

Original fix:
https://projects.blender.org/blender/blender/pulls/136966

for Blender issues
https://projects.blender.org/blender/blender/issues/102755
and
https://projects.blender.org/blender/blender/issues/84349

In Mantaflow calculating of Vector Normalize iis using the Pythagorean addition, which is defined as:

  a ⊕ b = sqrt(a^2+b^2)

This is hypotenuse, or the length of the longest side of a right-angled triangle, that we know the other 2 sides, a and b. https://en.wikipedia.org/wiki/Pythagorean_addition

By using sqrt(a * a + b * b), for large (or small) a or b, there is a possibility of overflow (underflow), although the result itself is not that big (small) to cause overflow (underflow).

To overcome this problem, there are implementations of hypotenuse that do not use power of 2, and use other methods to calculate the result.

To calculate a ⊕ b, you can easily use hypot(a,b). https://en.cppreference.com/w/cpp/numeric/math/hypot

This patch is limiting unpredicteble behaviour of the Mantaflow, caused by computation errors.

@bartoszkosiorek bartoszkosiorek force-pushed the hypot branch 2 times, most recently from f494c54 to d4a65b8 Compare April 9, 2025 19:30
@sebbas
Copy link
Contributor

sebbas commented Apr 9, 2025

Hi, just looked into the patch and the examples you posted on b.org. It looks like a great fix!

One thing I'd do though is keep the hypot patch separate from the README & python3 updates (could go in another pull request).

@bartoszkosiorek
Copy link
Author

bartoszkosiorek commented Apr 9, 2025

Hi, just looked into the patch and the examples you posted on b.org. It looks like a great fix!

Thanks for fast feedback. I just noticed that you have your own fork of mantaflow:
https://github.com/sebbas/mantaflow

One thing I'd do though is keep the hypot patch separate from the README & python3 updates (could go in another pull request).

Sure. Will do.
Do you think I should cover it with test cases? Can you help me with that?

@sebbas
Copy link
Contributor

sebbas commented Apr 9, 2025

Yes sure, I can help with that. From the top of my head, I think the norm functions in source/util/vector4d.h would also benefit from this patch. Covering those would be great addition to this patch.

@bartoszkosiorek
Copy link
Author

bartoszkosiorek commented Apr 9, 2025

Yes sure, I can help with that. From the top of my head, I think the norm functions in source/util/vector4d.h would also benefit from this patch. Covering those would be great addition to this patch.

I already implemented vector4d on blender side, with commit (any comment is welcomed):
https://projects.blender.org/Bartosz-Kosiorek/blender/commit/79b9ac733571ae413731fa8359bad3ee433c1568

Unfortunately there is no hypot4 implementation in libstdc++, so I have to implement it myself.
https://en.cppreference.com/w/cpp/numeric/math/hypot

I would also be glad to cover vector4d norm with tests.

…alization functions

Fix for Blender issue: https://projects.blender.org/blender/blender/pulls/136966

In Mantaflow calculating of Vector Normalize iis using the Pythagorean addition,
which is defined as:

      a ⊕ b = sqrt(a^2+b^2)

This is hypotenuse, or the length of the longest side of a right-angled triangle, that we know the other 2 sides, a and b.
https://en.wikipedia.org/wiki/Pythagorean_addition

By using sqrt(a * a + b * b), for large (or small) a or b, there is a possibility of overflow (underflow),
although the result itself is not that big (small) to cause overflow (underflow).

To overcome this problem, there are implementations of hypotenuse that do not use power of 2,
and use other methods to calculate the result.

To calculate a ⊕ b, you can easily use hypot(a,b).
https://en.cppreference.com/w/cpp/numeric/math/hypot

This patch is limiting unpredicteble behaviour of the Mantaflow, caused by computation errors.
@bartoszkosiorek bartoszkosiorek changed the title mantaflow: Avoid computation errors by using hypot for 3d vector norm… mantaflow: Avoid computation errors by using hypot for vector norm… Apr 10, 2025
@bartoszkosiorek
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants