如何计算非均匀球体的质量?

发布于 2024-10-07 09:13:41 字数 291 浏览 2 评论 0原文

我想根据三维离散不均匀密度分布计算球体的质量。假设一组不同密度的 3x3x3 立方体内接于一个球体。使用 Python 求和分区质量的最快方法是什么?

我尝试使用 scipy.integrate.dblquad 计算球体数学方程下的体积: x^2 +y^2 +z^2 = R^2 对于其中一个立方体的范围。 然而,只有当边界小于球体的半径时,结果才有效,并且假设 50,000 个球体(每个球体有 27 个立方体)的重复计算会非常慢。

另一方面,在我看来,由于质量分布相当粗糙和离散,因此无法使用 CoM 计算的常用方程。

I want to calculated the mass of a sphere based on a three-dimensional discrete inhomogeneous density distribution. Lets say a set of 3x3x3 cubes of different densities is inscribed by a sphere. What is the fastest way to sum up the partitioned masses using Python?

I tried to calculate the volume under the mathematical equation for a sphere: x^2 +y^2 +z^2 = R^2 for the range of one of the cubes using scipy.integrate.dblquad.
However, the result is only valid if the boundaries are smaller than the radius of the sphere and repetitive calculation for let's say 50,000 spheres with 27 cubes each would be quite slow.

On the other hand, the usual equation for CoM calculations couldn't be used in my opinion, due to the rather coarse and discrete mass distribution.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

妖妓 2024-10-14 09:13:41

时序实验

您没有指定时序约束,因此我使用一个很好的集成包做了一个小实验。

如果不进行优化,如果立方体密度是简单函数,则可以在标准笔记本电脑中在 0.005 秒内计算出球坐标中的每个积分。

作为参考,这是 Mathematica 中的程序:

Clear@f;
(* Define a cuboid as density function *)
iP = IntegerPart;
f[{x_, y_, z_}, {lx_, ly_, lz_}] :=   iP[x - lx] + iP[y - ly] + iP[z - lz] /; 
   lx <= x <= lx + 3 && ly <= y <= ly + 3 && lz <= z <= lz + 3;

f[{x_, y_, z_}, {lx_, ly_, lz_}] := Break[] /; True;

Timing[Table[s = RandomReal[{0, 3}, 3]; (*sphere center random*)
   sphereRadius = Min[Union[s, 3 - s]]; (*max radius inside cuboid *)
   NIntegrate[(f[{x, y, z} - s, -s] /.  (*integrate in spherical coords *)
       {x -> r Cos@th Sin@phi, 
        y -> r Sin@th Sin@phi, 
        z -> r Cos@phi}) r^2 Sin@phi,
       {r, 0, sphereRadius}, {th, 0, 2 Pi}, {phi, 0, Pi}], 
         {10000}]][[1]]  

结果是52 秒进行 10^4 次迭代。

所以也许您不需要优化太多......

Timing Experiment

You didn't specify your timing constraints, so I've done a little experiment with a nice integration package.

Without optimization, each integral in spherical coordinates can be evaluated in 0.005 secs in a standard laptop if the cubes densities are straightforward functions.

Just as a reference, this is the program in Mathematica:

Clear@f;
(* Define a cuboid as density function *)
iP = IntegerPart;
f[{x_, y_, z_}, {lx_, ly_, lz_}] :=   iP[x - lx] + iP[y - ly] + iP[z - lz] /; 
   lx <= x <= lx + 3 && ly <= y <= ly + 3 && lz <= z <= lz + 3;

f[{x_, y_, z_}, {lx_, ly_, lz_}] := Break[] /; True;

Timing[Table[s = RandomReal[{0, 3}, 3]; (*sphere center random*)
   sphereRadius = Min[Union[s, 3 - s]]; (*max radius inside cuboid *)
   NIntegrate[(f[{x, y, z} - s, -s] /.  (*integrate in spherical coords *)
       {x -> r Cos@th Sin@phi, 
        y -> r Sin@th Sin@phi, 
        z -> r Cos@phi}) r^2 Sin@phi,
       {r, 0, sphereRadius}, {th, 0, 2 Pi}, {phi, 0, Pi}], 
         {10000}]][[1]]  

The result is 52 secs for 10^4 iterations.

So perhaps you don't need to optimize a lot ...

你另情深 2024-10-14 09:13:41

我无法理解你所说的“被球体内切”的确切含义。我还没有尝试过 scipy.integrate。不过,这里有一些:

将 3x3x3 立方体设置为单位密度。然后分别对每个立方体进行积分,因此这里应该有体积立方体 V_ijk。现在,对于每个球体,您可以通过对 V_ijk*D_ijk 求和来获得每个球体的质量,其中 D_ijk 是球体的密度。

它应该快得多,因为您现在不需要进行集成。

I cannot get your exact meaning of inscribed by a sphere. Also I havent tried the scipy.integrate. However, here are some though:

Set a 3x3x3 cube to unit density. Then take the integration for each cube respectively, so you should have the volume cube V_ijk here. Now for each of sphere, you can get the mass of each sphere by summing V_ijk*D_ijk, where the D_ijk is the density of the sphere.

It should be much faster because you do not need to do integration now.

自找没趣 2024-10-14 09:13:41

您可以获得立方体(或长方体)和球体之间相交体积的解析公式。这并不容易,但应该是可能的。我已经对二维的任意三角形和圆进行了此操作。基本思想是将交集分解为更简单的部分,例如四面体和体积球面三角形扇形,相对简单的体积公式是已知的。主要的困难在于考虑所有可能的交叉情况。幸运的是,这两个对象都是凸的,因此可以保证有一个凸的相交体积。

一种近似方法可能是简单地细分立方体,直到近似数值积分算法起作用为止;这应该还是比较快的。你知道皮克定理吗?这仅适用于 2D,但我相信,3D 概括

You can obtain an analytic formula for the intersecting volume between a cube (or rectangular prism) and a sphere. It won't be easy, but it should be possible. I have done it for an arbitrary triangle and circle in 2D. The basic idea is to decompose the intersection into simpler pieces, like tetrahedra and volumetric spherical triangle sectors, for which relatively simple volume formulas are known. The main difficult is in considering all the possible cases of intersections. Luckily both objects are convex, so you are guaranteed a single convex intersection volume.

An approximate method might be to simply subdivide the cubes until your approximate numerical integration algorithm does work; this should still be relatively fast. Do you know about Pick's Theorem? That only works in 2D, but there are, I believe, 3D generalizations.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文