【问题标题】:How can I put this problem in a python for?我怎样才能把这个问题放在 python 中呢?
【发布时间】:2020-12-19 00:52:39
【问题描述】:

我正在用python解决这个问题,包括获得骑士四面体网格的总体积。

我写的这段代码获得了体积的值,但是网格的第一个四面体的值,问题是我不知道如何放置它,以便它读取所有四面体并添加每个体积一。我知道它带有一个 for 循环,但我不知道该放在哪里。

为了更好的理解,这里附上“tets”和“pts”矩阵的照片,“tets”矩阵的想法是存储“pts”行,其中4个点的坐标发现构成一个四面体。

Image 1

Image 2

这是代码:

# -- 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


    【解决方案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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-25
      • 2020-08-21
      • 2011-08-20
      • 2022-01-07
      • 2013-10-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多