【参数化建模】利用Python在Abaqus里面绘制复杂曲线——以“爱心曲线”为例

【参数化建模】利用Python在Abaqus里面绘制复杂曲线——以“爱心曲线”为例

说在前面

Python在Abaqus里面可以批量处理很多事情,包括复杂曲线的绘制,这里以心形线为例。

心形线函数

这里我们选取一款比较经典的心形线:

\left\{ \begin{array}{c} y=\sqrt{1-\left( \left| x \right|-1 \right) ^2}\\ y=-3\sqrt{1-\sqrt{\frac{\left| x \right|}{2}}}\\ \end{array} \right.

Abaqus代码

# -- coding: utf-8 --
import numpy as np
from datetime import datetime


class TempTry:
    def __init__(self):
        super().__init__()
        self.current_time = datetime.now().strftime('%a %b %d %H:%M:%S %Y')
        self.fileName_Abaqus = 'draw.py'
        self.numx = 2
        self.d = 30
        self.size = 10
        self.start_mov_x = 2 * self.size
        self.start_mov_y = 3 * self.size
        self.Delta_x = 4 * self.size

    def model_abaqus(self):
        str0 = f'''
# -*- coding: mbcs -*-
#
# Abaqus/CAE Release 2020 replay file
# Internal Version: 2019_09_14-01.49.31 163176
# Run by Know634 on {self.current_time}
#

# from driverUtils import executeOnCaeGraphicsStartup
# executeOnCaeGraphicsStartup()
#: Executing "onCaeGraphicsStartup()" in the site directory ...
'''
        with open(self.fileName_Abaqus, 'w', encoding='gbk') as f:
            f.write(str0)

        str1 = f'''
from abaqus import *
from abaqusConstants import *
session.Viewport(name='Viewport: 1', origin=(0.0, 0.0), width=200.21875, 
    height=108.900466918945)
session.viewports['Viewport: 1'].makeCurrent()
session.viewports['Viewport: 1'].maximize()
from caeModules import *
from driverUtils import executeOnCaeStartup
executeOnCaeStartup()
session.viewports['Viewport: 1'].partDisplay.geometryOptions.setValues(
    referenceRepresentation=ON)
'''
        with open(self.fileName_Abaqus, 'a', encoding='gbk') as f:
            f.write(str1)

        str2 = f'''
s = mdb.models['Model-1'].ConstrainedSketch(name='__profile__', 
    sheetSize=200.0)
g, v, d, c = s.geometry, s.vertices, s.dimensions, s.constraints
s.setPrimaryObject(option=STANDALONE)
'''
        with open(self.fileName_Abaqus, 'a', encoding='gbk') as f:
            f.write(str2)

        def upper(value):
            res = self.size * np.sqrt(1 - (np.abs(value) / self.size - 1) ** 2)
            return res

        def lower(value):
            res = self.size * -3 * np.sqrt(1 - np.sqrt(np.abs(value) / (2 * self.size)))
            return res

        def genCell(num=1000, start=(0.0, 0.0)):
            x_list = np.linspace(-2 * self.size, 2 * self.size, num, endpoint=True)
            y_upper = np.array([upper(x) + start[1] for x in x_list])
            y_lower = np.array([lower(x) + start[1] for x in x_list[::-1]])
            upper_list = [f"({x_list[i] + start[0]}, {y_upper[i]})" for i in range(num)]
            lower_list = [f"({-x_list[i] + start[0]}, {y_lower[i]})" for i in range(num)]
            points = ", ".join(upper_list + lower_list)
            content = f'''
s.Spline(points=({points}))
'''
            return content

        for x in range(self.numx):
            with open(self.fileName_Abaqus, 'a', encoding='gbk') as f:
                f.write(genCell(start=(self.start_mov_x + x * self.Delta_x, self.start_mov_y)))

        str3 = f'''
p = mdb.models['Model-1'].Part(name='Part-1', dimensionality=THREE_D, 
    type=DEFORMABLE_BODY)
p = mdb.models['Model-1'].parts['Part-1']
p.BaseShellExtrude(sketch=s, depth={self.d})
s.unsetPrimaryObject()
p = mdb.models['Model-1'].parts['Part-1']
session.viewports['Viewport: 1'].setValues(displayedObject=p)
del mdb.models['Model-1'].sketches['__profile__']
'''
        with open(self.fileName_Abaqus, 'a', encoding='gbk') as f:
            f.write(str3)


if __name__ == '__main__':
    TempTry().model_abaqus()
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值