【python】【材料力学】计算任意界面的质心坐标及惯性矩

背景

etabs自定义截面

公式

This code uses the following formulas:

Area of the polygon:

A = 1 2 ∑ i = 1 n ( x i y i + 1 − x i + 1 y i ) A = \frac{1}{2} \sum_{i=1}^{n} (x_i y_{i+1} - x_{i+1} y_i) A=21i=1n(xiyi+1xi+1yi)

Centroid coordinates:

C x = 1 6 A ∑ i = 1 n ( x i + x i + 1 ) ( x i y i + 1 − x i + 1 y i ) C_x = \frac{1}{6A} \sum_{i=1}^{n} (x_i + x_{i+1})(x_i y_{i+1} - x_{i+1} y_i) Cx=6A1i=1n(xi+xi+1)(xiyi+1xi+1yi)

C y = 1 6 A ∑ i = 1 n ( y i + y i + 1 ) ( x i y i + 1 − x i + 1 y i ) C_y = \frac{1}{6A} \sum_{i=1}^{n} (y_i + y_{i+1})(x_i y_{i+1} - x_{i+1} y_i) Cy=6A1i=1n(yi+yi+1)(xiyi+1xi+1yi)

Moment of inertia:

I x = 1 12 ∑ i = 0 n − 1 ( y i 2 + y i y i + 1 + y i + 1 2 ) ( x i y i + 1 − x i + 1 y i ) Ix = \frac{1}{12}\sum{i=0}^{n-1} (yi^2 + yiy{i+1} + y{i+1}^2)(xi y{i+1} - x{i+1} yi) Ix=121i=0n1(yi2+yiyi+1+yi+12)(xiyi+1xi+1yi)
I y = 1 12 ∑ i = 0 n − 1 ( x i 2 + x i x i + 1 + x i + 1 2 ) ( x i y i + 1 − x i + 1 y i ) Iy = \frac{1}{12}\sum{i=0}^{n-1} (xi^2 + xi x{i+1} + x{i+1}^2)(xi y{i+1} - x{i+1} yi) Iy=121i=0n1(xi2+xixi+1+xi+12)(xiyi+1xi+1yi)
I x y = 1 24 ∑ i = 0 n − 1 ( 2 x i y i + x i + 1 y i + 2 x i + 1 y i + 1 + x i y i + 1 ) ( x i y i + 1 − x i + 1 y i ) I{xy} = \frac{1}{24}\sum{i=0}^{n-1} (2xi yi + x{i+1} yi + 2x{i+1} y{i+1} + xi y{i+1})(xi y{i+1} - x{i+1} yi) Ixy=241i=0n1(2xiyi+xi+1yi+2xi+1yi+1+xiyi+1)(xiyi+1xi+1yi)
其中, n n n 是多边形的顶点数, ( x i , y i ) (xi,yi) (xi,yi) 表示多边形中第 i i i 个顶点的坐标, ( x i + 1 , y i + 1 ) (x{i+1},y{i+1}) (xi+1,yi+1) 表示多边形中第 i + 1 i+1 i+1 个顶点的坐标, I x Ix Ix 代表多边形相对于 x x x 轴的惯性矩, I y Iy Iy 代表多边形相对于 y y y 轴的惯性矩, I x y I_{xy} Ixy 代表多边形相对于 x x x y y y 轴的二次惯性矩。

代码


import matplotlib.pyplot as plt

def polygon_area(vertices):
    n = len(vertices)
    area = 0
    for i in range(n):
        area += (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
    return 0.5 * abs(area)

def centroid(vertices):
    n = len(vertices)
    cx, cy = 0, 0
    area = polygon_area(vertices)
    for i in range(n):
        factor = (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
        cx += (vertices[i][0] + vertices[(i + 1) % n][0]) * factor
        cy += (vertices[i][1] + vertices[(i + 1) % n][1]) * factor
    return (1 / (6 * area)) * cx, (1 / (6 * area)) * cy

def moment_of_inertia(vertices):
    n = len(vertices)
    Ix, Iy, Ixy = 0, 0, 0
    area = polygon_area(vertices)
    for i in range(n):
        factor = (vertices[i][0] * vertices[(i + 1) % n][1] - vertices[(i + 1) % n][0] * vertices[i][1])
        Ix += (vertices[i][1]**2 + vertices[i][1]*vertices[(i + 1) % n][1] + vertices[(i + 1) % n][1]**2) * factor
        Iy += (vertices[i][0]**2 + vertices[i][0]*vertices[(i + 1) % n][0] + vertices[(i + 1) % n][0]**2) * factor
        Ixy += (vertices[i][0]*vertices[(i + 1) % n][1] + 2*vertices[i][0]*vertices[i][1] + 2*vertices[(i + 1) % n][0]*vertices[(i + 1) % n][1] + vertices[(i + 1) % n][0]*vertices[i][1]) * factor
    return (1 / 12) * abs(Ix), (1 / 12) * abs(Iy), (1 / 24) * abs(Ixy)

def plot_polygon(vertices, centroid):
    polygon = plt.Polygon(vertices, fill=None, edgecolor='r', linewidth=2)
    plt.gca().add_patch(polygon)
    plt.scatter(*centroid, color='blue', marker='o', s=100, label='Centroid')
    plt.axis('equal')
    plt.legend()
    plt.show()

# 可增加,从cad中读取
vertices = [(0, 0), (30, 0), (30, 30), (0, 30), (-20, 20)]

area = polygon_area(vertices)
centroid_coordinates = centroid(vertices)
Ix, Iy, Ixy = moment_of_inertia(vertices)

print("Area:", area)
print("Centroid:", centroid_coordinates)
print("Moment of Inertia: Ix =", Ix, "Iy =", Iy, "Ixy =", Ixy)

plot_polygon(vertices, centroid_coordinates)

总结

常用的markdown编辑器可以渲染latex格式代码
常用公式可以编程

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论
计算阴阳离子质心之间的RDF需要用到Python中的科学计算库和可视化库。以下是一个基本的Python脚本示例: ```python import numpy as np import matplotlib.pyplot as plt #读取质心坐标文件 coords = np.loadtxt("centers.txt") #定义模拟框架大小和分辨率 boxsize = 10.0 nbins = 100 binwidth = boxsize / nbins #将质心分为阳离子和阴离子 nions = len(coords) nCation = int(nions/2) nAnion = nions - nCation cationCoords = coords[:nCation] anionCoords = coords[nCation:] #计算阴阳离子之间的距离 rij = [] for i in range(nCation): for j in range(nAnion): dr = anionCoords[j] - cationCoords[i] dr -= np.round(dr / boxsize) * boxsize r = np.sqrt(np.sum(dr**2)) rij.append(r) #计算RDF rdf, edges = np.histogram(rij, bins=nbins, range=(0, boxsize)) rdf /= np.sum(rdf) #计算每个bin的体积 volumes = np.array([4.0/3.0*np.pi*((r+binwidth)**3-r**3) for r in edges[:-1]]) #计算密度 density = nions / boxsize**3 #归一化RDF rdf /= density*volumes #绘制RDF曲线 plt.plot(edges[:-1], rdf) plt.xlabel("Distance (A)") plt.ylabel("RDF") plt.show() ``` 在此示例中,我们假设坐标文件中的偶数行为阳离子质心,奇数行为阴离子质心。程序将模拟框架大小定义为`10.0`,分辨率定义为`100`。程序将质心分为阳离子和阴离子,并计算它们之间的距离。程序使用`numpy.histogram`函数计算RDF,并使用体积和密度归一化,得到归一化RDF曲线。 运行脚本后,将生成一个RDF曲线图。需要注意的是,此脚本仅适用于特定的质心坐标文件格式,如果您的坐标文件格式不同,可能需要对脚本进行修改。同时,此脚本也只是最基本的阴阳离子RDF计算示例,您可能需要根据需要进行修改和优化。 如果您需要更具体的帮助,请提供更详细的问题描述。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hmywillstronger

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值