Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/355.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我如何将这个问题放到python中?_Python_Numpy_For Loop_Mesh_Numerical Methods - Fatal编程技术网

我如何将这个问题放到python中?

我如何将这个问题放到python中?,python,numpy,for-loop,mesh,numerical-methods,Python,Numpy,For Loop,Mesh,Numerical Methods,我正在用python解决这个问题,其中包括获取骑士四面体网格的总体积 我写的这段代码获得了网格第一个四面体的体积值,问题是我不知道如何放置它,以便它读取所有四面体,并将这个四面体的每个体积相加。我知道它有一个for循环,但我不知道放在哪里 为了更好地理解,“tets”和“pts”矩阵,这里附上了一张照片,“tets”矩阵的想法是存储“pts”行,其中发现了构成四面体的4个点中的每个点的坐标 这是代码: # -- coding: utf-8 -- """ Edit

我正在用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])))

做一个程序来计算一个四面体的体积

def tetra_vol(nodes):
    M = np.array( [ 1,*pts[node]] for node in nodes)
    return abs(np.linalg.det(M))/6
您可以用计算完全相同的行列式值的单个步骤来替换它。您使用的操作是行操作,可用于将4x4行列式简化为3x3行列式,其中您使用了显式公式

然后计算所有四面体的和

Vol = sum(tetra_vol(tet) for tet in tets)