我在之前的搜索中发现了这个讨论,它展示了如何使用Python在空间中的点上对函数进行数值积分。
Looking for Python package for numerical integration over a tessellated domain
我的第一个问题是上面链接中给出的代码是否适用于现有的STL网格?我有网格的所有点和表面法线。我尝试运行此代码,它通过传递顶点和三角形的索引对我不起作用。
我在谷歌搜索中发现了这个链接,用于FEM的数值积分,并找到了一个小代码片段,用于在空间中的三角形上集成函数,如下所示:
def dJ( u, v, p1, p2, p3 ):
x1, y1 = p1
x2, y2 = p2
x3, y3 = p3
dxdu = ( (1-v)*x2 + v*x3 - x1 )
dxdv = ( u*x3 - u*x2 )
dydu = ( (1-v)*y2 + v*y3 - y1 )
dydv = ( u*y3 - u*y2 )
return np.abs( dxdu*dydv - dxdv*dydu )
def tridblquad( integrand, p1, p2, p3 ):
#http://connor-johnson.com/2014/03/09/integrals-over-arbitrary-triangular-regions-for-fem/
'''
Perform double quadtrature integration on triangular domain.
Input: function to integrate, points of triangle as tuples.
Output: integral and estimated absolute error as a tuple.
'''
x1, y1 = p1 ; x2, y2 = p2 ; x3, y3 = p3
# transformation to the unit square
g = lambda u, v, c1, c2, c3: (1-u)*c1 + u*( (1-v)*c2 + v*c3 )
# transformation for the integrand,
# including the Jacobian scaling factor
def h( u, v ):
x = g( u, v, x1, x2, x3 )
y = g( u, v, y1, y2, y3 )
I = integrand( x, y )
I *= dJ( u, v, p1, p2, p3 )
return I
# perfrom the double integration using quadrature in the transformed space
integral, error = scipy.integrate.dblquad( h, 0, 1, lambda x: 0, lambda x: 1, epsrel=1e-6, epsabs=0 )
return integral, error
但是,此代码仅适用于二维三角形。任何人都可以建议一种更好的更通用的方法来在曲面细分网格上执行函数的数值积分吗?
感谢。