【发布时间】:2020-12-19 00:52:39
【问题描述】:
我正在用python解决这个问题,包括获得骑士四面体网格的总体积。
我写的这段代码获得了体积的值,但是网格的第一个四面体的值,问题是我不知道如何放置它,以便它读取所有四面体并添加每个体积一。我知道它带有一个 for 循环,但我不知道该放在哪里。
为了更好的理解,这里附上“tets”和“pts”矩阵的照片,“tets”矩阵的想法是存储“pts”行,其中4个点的坐标发现构成一个四面体。
这是代码:
# -- coding: utf-8 --
"""
Editor de Spyder
Este es un archivo temporal.
"""
import numpy as np
import meshio
mesh = meshio.read("knight.msh")#read the mesh
pts = mesh.points #stores the mesh points in an array
tets = mesh.cells[0].data #"tets" stores the number of the row of "pts" in which the coordinates of the tetrahedron are found
#VERTICE A OF THE TETRAHEDRO
kA_i = tets[0,0] #Take from the "test" matrix the value [n, 0] that corresponds to the place of "pts" #where the x, y, z coordinates of the point are stored
A = (pts[kA_i,0], pts[kA_i,1],pts[kA_i,2]) #Stores the coordinates of vertice A
#VERTICE B OF THE TETRAHEDRO
kB_i = tets[0,1]
B = (pts[kB_i,0],pts[kB_i,1],pts[kB_i,2])
#VERTICE C OF THE TETRAHEDRO
kC_i = tets[0,2]
C = (pts[kC_i,0],pts[kC_i,1],pts[kC_i,2])
#VERTICE D OF THE TETRAHEDRO
kD_i = tets[0,3]
D = (pts[kD_i,0],pts[kD_i,1],pts[kD_i,2])
#CALCULATION OF THE TETRAHEDRAL SEGMENTS
SAB = (B[0]-A[0],B[*1*]-A[*1*],B[*2*]-A[*2*]) #SEGMENT AB
U = tuple(SAB) #Directional vector U
SAC = (C[0]-A[0],C[*1*]-A[*1*],C[*2*]-A[*2*]) #SEGMENT AC
V = tuple(SAC) #Directional vector V
SAD = (D[0]-A[0],D[*1*]-A[*1*],D[*2*]-A[*2*]) #SEGMENT AD
W = tuple(SAD) #Directional vector W
#MATRIX GENERATION
M = np.array((U,V,W))
#CALCULATION OF THE VOLUME OF THE TETRAHEDRON:
Vol = (1/6)*np.abs((M[0,0]*(M[1,1]*M[2,2] - M[2,1]*M[1,2]) - M[0,1]*(M[1,0]*M[2,2] - M[2,0]*M[1,2]) + M[0,2]*(M[1,0]*M[2,1] - M[2,0]*M[1,1])))
【问题讨论】:
标签: python numpy for-loop mesh numerical-methods