Msc.Marc的python开发#2

有限元仿真模拟 专栏收录该内容
2 篇文章 0 订阅

Msc.Marc的python开发#2

今天接着上一篇的文章介绍。上一篇说到,利用Msc.Marc的python接口,实现自动定义材料的功能,主要用到的是Marc配置的py_mentat模块,核心思想其实很简单,那就是 一个换了python皮的procedure 文件 ,换句话说,把需要人工输入的命令汇集到python源文件中,并通过命令行和Marc进行交互,向Marc下各种命令,并获得Marc 的数据库中的各种数据。

那么今天我们要用python进行Marc的后处理,用的是py_post模块。这个模块在应用上更接近python的面向对象特点,单元、节点都有自己的类,ID、坐标都成为了类的属性,访问起来更方便更有逻辑。

开发目的

我今天要做的事情是针对一个钢筋混凝土框架,想办法调整配筋数据,使得经过某个地震波的时程分析后,只出现梁端塑性铰,而不出现柱端塑性铰,而且要尽可能接近临界状态,也就是钢筋不能超配太多。我的基础是用PKPM按中国规范分析得出的配筋数据。Marc模型是梁柱纤维梁模型,搭配课题组开发的纤维梁子程序。

流程图

整体结构

Created with Raphaël 2.2.0 开始 初始化材料特性 写入材料文件 运行分析 提取塑性铰数据,写入两个列表, 分别是总塑性铰列表和当前塑性铰列表 总塑性铰列表中 是否只有第一层柱? 结束 利用二分法更改配筋面积数据 yes no

单元结构

在这里插入图片描述

应用实例

from itertools import islice
import time
import os
from py_post import *
from py_mentat import *
HEIGHT = 4000  # height of a single story(length of a column)
LENGTH = 5000  # length of a beam
all_materials = {}
column_hinges = []  
tol = 0.05


class beam_comp(object):  # define compona_fiber beam element class
    def __init__(self, string):
        data = string.split(',')
        self.lamb_low = 1.0
        self.lamb_high = 5.0
        self.lamb_mid = 1.0
        self.Nfloor = int(data[0])
        self.Nbeam = int(data[1])
        self.name = 'F' + data[0] + 'L' + data[1]  # name
        self.Num_Rc = int(data[2])
        self.Num_S = int(data[3])
        self.dx = float(data[4])
        self.dy = float(data[5])
        self.bc = float(data[6])
        self.hc = float(data[7])
        self.Crt = float(data[8])
        self.Crb = float(data[9])
        self.Crl = float(data[10])
        self.Crr = float(data[11])
        self.Art = float(data[12])
        self.Arb = float(data[13])
        self.Arl = float(data[14])
        self.Arr = float(data[15])
        self.Ar1 = float(data[16])
        self.Ar2 = float(data[17])
        self.Ar3 = float(data[18])
        self.Ar4 = float(data[19])
        self.nf_bc = int(data[20])
        self.nf_hc = int(data[21])
        self.nrt = int(data[22])
        self.nrb = int(data[23])
        self.nrl = int(data[24])
        self.nrr = int(data[25])
        self.fc = float(data[26])
        self.kt = float(data[27])
        self.frt = float(data[28])
        self.frb = float(data[29])
        self.frl = float(data[30])
        self.frr = float(data[31])
        self.fr1 = float(data[32])
        self.fr2 = float(data[33])
        self.fr3 = float(data[34])
        self.fr4 = float(data[35])
        self.CFT = int(data[36])
        self.Sig_de = float(data[37])


class colm_comp(object):  # define compona_fiber beam element class
    def __init__(self, string):
        data = string.split(',')
        self.lamb_low = 1.0
        self.lamb_high = 5.0
        self.lamb_mid = 1.0
        self.Nfloor = int(data[0])
        self.Nbeam = int(data[1])
        self.name = 'F' + data[0] + 'C' + data[1]  # name
        self.Num_Rc = int(data[2])
        self.Num_S = int(data[3])
        self.dx = float(data[4])
        self.dy = float(data[5])
        self.bc = float(data[6])
        self.hc = float(data[7])
        self.Crt = float(data[8])
        self.Crb = float(data[9])
        self.Crl = float(data[10])
        self.Crr = float(data[11])
        self.Art = float(data[12])
        self.Arb = float(data[13])
        self.Arl = float(data[14])
        self.Arr = float(data[15])
        self.Ar1 = float(data[16])
        self.Ar2 = float(data[17])
        self.Ar3 = float(data[18])
        self.Ar4 = float(data[19])
        self.nf_bc = int(data[20])
        self.nf_hc = int(data[21])
        self.nrt = int(data[22])
        self.nrb = int(data[23])
        self.nrl = int(data[24])
        self.nrr = int(data[25])
        self.fc = float(data[26])
        self.kt = float(data[27])
        self.frt = float(data[28])
        self.frb = float(data[29])
        self.frl = float(data[30])
        self.frr = float(data[31])
        self.fr1 = float(data[32])
        self.fr2 = float(data[33])
        self.fr3 = float(data[34])
        self.fr4 = float(data[35])
        self.CFT = int(data[36])
        self.Sig_de = float(data[37])




def initate_mat(all_materials):
    with open('section_beam.csv') as file:
        for content in islice(file, 1, None):
            new_beam_comp = beam_comp(content)
            all_materials[new_beam_comp.name] = new_beam_comp
    file.close()
    with open('section_colu.csv') as file:
        for content in islice(file, 1, None):
            new_colm_comp = colm_comp(content)
            all_materials[new_colm_comp.name] = new_colm_comp
    file.close()
    return


def initate_from_info():
    pass
    return


def write_secinfo(all_materials):
    with open('secinfo.txt', 'wt') as db:
        db.write('%d\n' % len(all_materials))
        for i_floor in range(1, 5):
            for i_beam in range(1, 25):
                name = 'F%dL%d' % (i_floor, i_beam)
                db.write(
                    '%d,%d\n' %
                    (all_materials[name].Num_Rc, all_materials[name].Num_S))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].dx, all_materials[name].dy))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].bc, all_materials[name].hc))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Crt, all_materials[name].Crb,
                          all_materials[name].Crl, all_materials[name].Crr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Art, all_materials[name].Arb,
                          all_materials[name].Arl, all_materials[name].Arr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Ar1, all_materials[name].Ar2,
                          all_materials[name].Ar3, all_materials[name].Ar4))
                db.write(
                    '%d,%d\n' %
                    (all_materials[name].nf_bc, all_materials[name].nf_hc))
                db.write('%d,%d,%d,%d\n' %
                         (all_materials[name].nrt, all_materials[name].nrb,
                          all_materials[name].nrl, all_materials[name].nrr))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].fc, all_materials[name].kt))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].frt, all_materials[name].frb,
                          all_materials[name].frl, all_materials[name].frr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].fr1, all_materials[name].fr2,
                          all_materials[name].fr3, all_materials[name].fr4))
                db.write('%d\n' % all_materials[name].CFT)
                db.write('%.6f\n' % all_materials[name].Sig_de)
        for i_floor in range(1, 5):
            for i_column in range(1, 17):
                name = 'F%dC%d' % (i_floor, i_column)
                db.write(
                    '%d,%d\n' %
                    (all_materials[name].Num_Rc, all_materials[name].Num_S))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].dx, all_materials[name].dy))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].bc, all_materials[name].hc))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Crt, all_materials[name].Crb,
                          all_materials[name].Crl, all_materials[name].Crr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Art, all_materials[name].Arb,
                          all_materials[name].Arl, all_materials[name].Arr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].Ar1, all_materials[name].Ar2,
                          all_materials[name].Ar3, all_materials[name].Ar4))
                db.write(
                    '%d,%d\n' %
                    (all_materials[name].nf_bc, all_materials[name].nf_hc))
                db.write('%d,%d,%d,%d\n' %
                         (all_materials[name].nrt, all_materials[name].nrb,
                          all_materials[name].nrl, all_materials[name].nrr))
                db.write('%.6f,%.6f\n' %
                         (all_materials[name].fc, all_materials[name].kt))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].frt, all_materials[name].frb,
                          all_materials[name].frl, all_materials[name].frr))
                db.write('%.6f,%.6f,%.6f,%.6f\n' %
                         (all_materials[name].fr1, all_materials[name].fr2,
                          all_materials[name].fr3, all_materials[name].fr4))
                db.write('%d\n' % all_materials[name].CFT)
                db.write('%.6f\n' % all_materials[name].Sig_de)
    db.close()
    return


def get_name_postel(pObj, ind_elem):
    # obtain the name 'FXXX' for a post element
    element = pObj.element(ind_elem)
    nodes = element.items
    node1 = pObj.node(nodes[0] - 1)
    node2 = pObj.node(nodes[1] - 1)
    if (node1.z == node2.z):  # it's a beam
        # we should allocate ID to the beam according the rule of serial number
        # which is
        # 1. beams parallel to the x axis come first(left to right)
        # 2. then beams parallel to the y axis(left to right)
        Nfloor = int(node1.z / HEIGHT)
        if (node1.x == node2.x):  # if the beam is parallel to the y axis.
            Nbeam = 4 + int(node1.x / LENGTH) + int(
                (node1.y + node2.y) / (2 * LENGTH)) * 7
        else:  # if the beam is parallel to the x axis
            Nbeam = 1 + int(node1.y / LENGTH) * 7 + int(
                (node1.x + node2.x) / (2 * LENGTH))
        name = 'F%dL%d' % (Nfloor, Nbeam)  # assemble the name of beam element
    else:  # it's a column
        Nfloor = int((node1.z + node2.z) / (2 * HEIGHT)) + 1
        Ncolu = int(node1.x / LENGTH) + 4 * int(node1.y / LENGTH) + 1
        name = 'F%dC%d' % (Nfloor, Ncolu)
    return name


def tell_if_colu(pObj, ind_elem):
    element = pObj.element(ind_elem)
    nodes = element.items
    node1 = pObj.node(nodes[0] - 1)
    node2 = pObj.node(nodes[1] - 1)
    if (node1.z == node2.z):  # it's a beam
        return False
    else:
        return True


def get_last_line(inputfile):
    filesize = os.path.getsize(inputfile)
    blocksize = 1024
    with open(inputfile, 'rb') as f:
        last_line = ""
        if filesize > blocksize:
            maxseekpoint = (filesize // blocksize)
            f.seek((maxseekpoint - 1) * blocksize)
        elif filesize:
            f.seek(0, 0)
        lines = f.readlines()
        if lines:
            linenum = 1
            while last_line == "":
                last_line = lines[-linenum].strip()
                linenum += 1
    f.close()
    return last_line


def run_job():
    py_send('*edit_job Time-history')
    py_send('*submit_job 1 *monitor_job')
    print('\tjob is running')
    time.sleep(420)
    while True:
        print('looping...')
        last_line = get_last_line('model1_Time-history.log')
        if last_line[-4:] == '3004':
            break
        else:
            print('\tjob is running')
            time.sleep(30)
    return 1


def post_extract():
    global column_hinges
    pObj = post_open("model1_Time-history.t16")
    # move to the last increment
    ninc = pObj.increments()  # the number total increments
    pObj.moveto(ninc - 1)
    # check each element for the user defined variable 3,
    # if it's 1, collect it in a library
    nelem = pObj.elements()  # the number of total elements
    nes = pObj.element_scalars()  # number of scalars acquired for each element
    for i in range(nes):
        if pObj.element_scalar_label(i) == 'User Defined Variable 3':
            ind_UDV3 = i
    curr_colu_hinges = []
    for i_elem in range(nelem):
        slist = pObj.element_scalar(i_elem, ind_UDV3)
        if (slist[0].value == 1 or slist[1].value == 1):
            name = get_name_postel(pObj, i_elem)
            if tell_if_colu(pObj, i_elem):
                column_hinges.append(name)
                curr_colu_hinges.append(name)
    column_hinges = undupli(column_hinges)
    curr_colu_hinges = undupli(curr_colu_hinges)
    pObj.close()

    return curr_colu_hinges


def undupli(input_list):
    output_list = []
    for item in input_list:
        if item in output_list:
            continue
        else:
            output_list.append(item)
    output_list.sort()
    return output_list


def tell_first_floor(column_hinges):
    # if all elements in column_hinges are on first floor
    # then return true, else return false
    logic_flag = True
    for name in column_hinges:
        logic_flag = logic_flag and (name[1]=='1')
    return logic_flag


def adjust_area(all_materials, column_hinges, curr_colu_hinges):
    if len(column_hinges) == 0 or tell_first_floor(column_hinges):
        return False
    else:
        for name in column_hinges:
            column = all_materials[name]
            if abs(column.lamb_high - column.lamb_low) < tol:
                lamb_pre = column.lamb_mid
                column.lamb_mid = column.lamb_high
                # prepare for a restart
                column.lamb_high = column.lamb_mid + 1
                column.lamb_low = max(column.lamb_mid-1, column.lamb_low)
                column.Art = column.lamb_mid/lamb_pre * column.Art
                column.Arb = column.lamb_mid/lamb_pre * column.Arb
                column.Arl = column.lamb_mid/lamb_pre * column.Arl
                column.Arr = column.lamb_mid/lamb_pre * column.Arr
                column.Ar1 = column.lamb_mid/lamb_pre * column.Ar1
                column.Ar2 = column.lamb_mid/lamb_pre * column.Ar2
                column.Ar3 = column.lamb_mid/lamb_pre * column.Ar3
                column.Ar4 = column.lamb_mid/lamb_pre * column.Ar4
                all_materials[name] = column
                column_hinges.remove(name)
            else:
                if name in curr_colu_hinges:
                    column.lamb_low = column.lamb_mid
                else:
                    column.lamb_high = column.lamb_mid

                lamb_pre = column.lamb_mid
                column.lamb_mid = (column.lamb_low + column.lamb_high) / 2.0
                column.Art = column.lamb_mid/lamb_pre * column.Art
                column.Arb = column.lamb_mid/lamb_pre * column.Arb
                column.Arl = column.lamb_mid/lamb_pre * column.Arl
                column.Arr = column.lamb_mid/lamb_pre * column.Arr
                column.Ar1 = column.lamb_mid/lamb_pre * column.Ar1
                column.Ar2 = column.lamb_mid/lamb_pre * column.Ar2
                column.Ar3 = column.lamb_mid/lamb_pre * column.Ar3
                column.Ar4 = column.lamb_mid/lamb_pre * column.Ar4
                all_materials[name] = column
        return True
    


def main():
    global all_materials
    global column_hinges

    log = open('python.log', 'wt')
    print('\tlog has been opened')

    initate_mat(all_materials)

    print('\tmaterials has been initated')
    iteration = 1
    print('iteration %d' % iteration)

    write_secinfo(all_materials)

    print('\tsecinfo has been writen')

    run_job()

    print('\tjob has been run')

    curr_colu_hinges = post_extract()

    print('\tlenth of column_hinges list: %d' % len(column_hinges))
    for name in column_hinges:
        log.write('\t%s has lambda %f\n' % (name, all_materials[name].lamb_mid))
        print('\t%s has lambda %f' % (name, all_materials[name].lamb_mid))
    print('\titeration completed')

    while adjust_area(all_materials, column_hinges, curr_colu_hinges):
        iteration += 1
        print('iteration %d' % iteration)
        log.write('\niteration %d\n' % iteration)

        write_secinfo(all_materials)

        print('\tsecinfo has been writen')

        run_job()

        print('\tjob has been run')

        curr_colu_hinges = post_extract()

        print('\tlenth of column_hinges list: %d' % len(column_hinges))
        log.write('\ncolumn_hinges\n')
        for name in column_hinges:
            log.write('\t%s has lambda %f \n' % (name, all_materials[name].lamb_mid))
            print('\t%s has lambda %f' % (name, all_materials[name].lamb_mid))
        print('\tcurrent_hinges')
        log.write('\tcurrent_hinges\n')
        for name in curr_colu_hinges:
            log.write('\t%s has lambda %f \n' % (name, all_materials[name].lamb_mid))
            print('\t%s has lambda %f' % (name, all_materials[name].lamb_mid))
        print('\niteration completed\n')

    for key in all_materials:
        log.write('%s: %f \n' % (all_materials[key].name, all_materials[key].lamb_mid))
    
    
    print('finish')
    log.write('\nfinish\n')
    log.close()
    return

if __name__ == '__main__':
    py_connect("", 40007)
    main()
    py_disconnect()


  • 0
    点赞
  • 0
    评论
  • 0
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

一共十五章,PDF格式…… 1 目录 第一章介绍 MENTAT与MARC程序的关系 MENTAT的文档 MENTAT的安装目录 MENTAT程序的屏幕布局 MENTAT与用户的通讯 MENTAT菜单光钮的分类 第二章网格生成 与有限元分析相关的常用词 与几何实体相关的常用词 网格生成方法1(网格的直接定义) 格栅的显示 格栅尺寸的显示 节点的生成 单元生成法 节点位置的修正 单元的删除 2 节点的删除 全部网格删除 网格生成方法2(由几何实体转化为网格) 控制点的生成 线的生成 CUBIC SPLINE(三次样条曲线)的生成 POLY LINE(多折线)的生成 Bezier 曲线的生成 圆弧、圆的生成 其它曲线 面的生成 四边形的生成 Bezier 曲面的生成 DRIVEN SURFACE(驱动面)的生成 RULER SUREACE(直纹曲面)的生成 SPHERE(球面)的生成 CYLINDER(圆柱、圆锥)面的生成 SWEPT SURFACE(扫描面)的生成 NURB SURFACE (NURB 曲面)的生成 3 实体的生成 几何实体与网格的转换 操作示范 单元表的指定方法 网格生成方法3(从几何实体自动生成网格) 其它前处理功能 ATTACH CHANGE CLASS CHECK CONVERT DUPLICATE EXPAND MOVE RELAX RENUMBER REVOLVE SOLIDS STRETCH SUBDIVIDE 4 SWEEP SYMMETRY 简例 (例题1)单元的直接定义 (例题2)几何实体转化为网格 (例题3)OVERLAY 网格划分 (例题4)ADVANCING FRONT 自动网格划分 第三章边界条件的定义(BOUNDARY CONDITIONS) BOUNDRAY CONDITIONS的 子菜单 MECHANICAL 边界条件名的定义 热传导边界条件的定义 TRANSFORMS(局部坐标系的定义) 边界条件定义举例(应力分析) 第四章初始条件的定义(INITIAL CONDITIONS) INITIAL CONDITIONS的子菜单 MECHANICAL(应力分析) 5 初始条件的定义 THERMAL (热传导分析) 第五章材料特性的定义 材料特性名的定义 MENTAT定义的材料特性 MECHANICAL MATERIAL OTHER MATERIAL(其它材料类型) 与材料特性相关的其它定义 材料常数的定义方法 材料特性定义举例 第六章选择功能(SELECT) 第七章表格功能的使用(TABLE) 第八章几何特性的定义(GEOMETRIC PROPERTIES) 3D单元几何特性的定义 轴对称单元几何特性的定义 平面单元几何特性的定义 接触/摩擦单元几何特性的定义 6 热传导单元的几何特性定义 几何特性定义举例(应力分析) 第九章接触条件的定义(CONTACT) 接触体的生成 CONTACT BODIES CONTACT TABLE CONTACT AREAS 第十章载荷工况的定义 分析类型的选择 LOAD CASE的定义 热传导分析时载荷工况的定义 第十一章定义作业参数并提交运行 JOBS PROCESSOR LOAD CASES ANALYSIS DIMENSION ANALYSIS OPTIONS CREEP TIME MODAL SOLUTION METHOD 7 DYNAMIC OPERATOR DYNAMIC HARMONIC VISCOELASTICITY JOB RESULTS INITIAL LOADS JOB PARAMETERS CONTACT CONTROL SOIL CONTROL ADAPTIVE MESHING CRITERIA ADDITIONAL PRINT ELEMENT TYPE RUN JOB JOBS操作举例 第十二章后处理(RESULTS) FILE SCALAR DEFORMED SHARP PLOT STYLE 8 PATH PLOT HISTORY PLOT ANIMATE ISOLATE ELEMS 第十三章静态菜单命令 静态菜单命令介绍 FILES菜单 PLOT菜单 VIEW菜单 第十四章练习 第十五章MENTAT的其它界面 MENTAT调用的外部程序 MARC Jobs 图形输出界面
©️2021 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值