首页/文章/ 详情

ANSA二次开发:Pids2Mids功能解析

6月前浏览1152
ANSA官方自带脚本Pids2Mids.py(路径:..\BETA_CAE_Systems\ansa_v22.0.0\scripts\Properties)主要功能是:对材料进行重编号,使mat与prop拥有相同的标识。若某一材料被多个prop共用,则复 制该材料,确保实现prop-mat一一对应关系。源码如下:

























































































































'''==================================================*                    HEADER==================================================Developed by:Irene MakropoulouDate:26/01/2011               BETA CAE Systems                      Kato Scholari, Thessaloniki,                                GR-57500, Epanomi,                                           Greece               Tel: +302392021420 , +302311993300                               Fax: +302392021417                         E-mail: ansa@beta-cae.com                      URL: http://www.beta-cae.com              Copyright (c) 2011 BETA CAE Systems                              All rights reserved.=================================================*                   DISCLAIMER=================================================BETA CAE Systems assumes no responsibility or liability for any damages, errors, inaccuracies or data loss caused by installation or use of this software.=================================================*                    HISTORY=================================================Date:                 Updater:       Modifications:-------------------------------------------------Date:                 Updater:Modifications:-------------------------------------------------''''''Name: Pids2MidsDescription: Renumbers materials so that they have the same id with the properties.       In case one material is used by more than one properties, copies of this material       are generated in order for the 1-1 correspondence to be generated.'''import ansafrom ansa import basefrom ansa import constants@ansa.session.defbutton('PROPERTIES', 'Pids2Mids')def Pids2MidsMain():    """  Renumbers materials so that they have the same id with the properties.  In case one material is used by more than one properties, copies of this material  are generated in order for the 1-1 correspondence to be generated.  """  ptypes = base.TypesInCategory(0, "__PROPERTIES__")  props = base.CollectEntities(0, None, ptypes)  mtypes = base.TypesInCategory(0, "__MATERIALS__")  PidToMatEnt   = dict()  OrigMatEntToMid = dict()  IsMIDAllocated  = dict()    for p_ent in props:    p_mats = _CollectMatsFromProp(p_ent)    if len(p_mats) > 1:      print ("More than one mats associated with PID ", p_ent._id)    m_ent = p_mats[0]    PidToMatEnt [p_ent._id] = m_ent;    OrigMatEntToMid [m_ent] = p_mats[0]._id    IsMIDAllocated [p_mats[0]._id] = 1    respect_frozen = True  force = False  j = 0  IsAlreadyModified = dict()  for pid, mat_ent in PidToMatEnt.items():    trgt_mid = pid    orig_mid = OrigMatEntToMid[mat_ent]    # Comment ouf the following "if" in order to avoid new materials creation as copies    if mat_ent in IsAlreadyModified:      new_mat_ent = base.CopyEntity(None, mat_ent)      mat_ent = new_mat_ent      prop_ent = base.GetEntity (0, "__PROPERTIES__", pid)      _SetMaterialToProperty (prop_ent, mat_ent._id)      OrigMatEntToMid [mat_ent] = mat_ent._id      IsMIDAllocated [mat_ent._id] = 1        if trgt_mid == mat_ent._id:       continue    IsMatWithSameId = base.GetEntity(0, "__MATERIALS__", trgt_mid)    if not IsMatWithSameId:      ret = base.SetEntityId(mat_ent, trgt_mid, force, respect_frozen)      IsAlreadyModified [mat_ent] = 1    else:      is_matid_set = False      temp_id = 99999999      while not is_matid_set:        if not temp_id in IsMIDAllocated:          print('mpika')          base.SetEntityId(IsMatWithSameId, temp_id, force, respect_frozen)          OrigMatEntToMid[IsMatWithSameId] = temp_id          base.SetEntityId(mat_ent, trgt_mid, force, respect_frozen)          is_matid_set = True          IsAlreadyModified[mat_ent] = 1        else:          temp_id -= 1  del PidToMatEnt  del OrigMatEntToMid  del IsMIDAllocated  del IsAlreadyModified  print("Done")# -----------------------------------------------------------------# ----------------------------------------------------------------- def _CollectMatsFromProp(p_ent):  all_decks = {1:"MID1", 2:"MID", 3:"MID", 4:"IMAT", 5:"MID", 6:"MID"}  cur_deck = base.CurrentDeck()  ret = base.GetEntityCardValues(0, p_ent, (all_decks[cur_deck],))  m_ent = base.GetEntity(0, "__MATERIALS__", ret[all_decks[cur_deck]])  return [m_ent]# -----------------------------------------------------------------# ----------------------------------------------------------------- def _SetMaterialToProperty(p_ent, mid):  all_decks = {1:"MID1", 2:"MID", 3:"MID", 4:"IMAT", 5:"MID", 6:"MID"}  cur_deck = base.CurrentDeck()  base.SetEntityCardValues (0, p_ent, {all_decks[cur_deck]:mid})  return;
官方源码仅仅考虑了NASTRAN、LS-DYNA、PAM-CRASH、ABAQUS、RADIOSS、ANSYS 6种求解器,在实际使用过程中,像OPTISTRUCT、MARC等常用求解器均不支持,另外,官方源码实现方式冗余复杂,导致开发工程师解析困难。基于上述两点,笔者对该功能进行了重写。新代码覆盖了ANSA当前支持的所有25种求解器,并且通过对prop-mat一一对应解构并且巧用空位mid,将代码的时间复杂度由O(n2)降低为O(n)。实现源码如下:






























































from ansa import base, sessionfrom typing import List, Dict, Setclass Pids2Mids(object):  deck2MatField:Dict[int, str] = {    1: 'MID1', 2: 'MID', 3: 'MID',     4: 'IMAT', 5: 'MID', 6: 'MID',    7: 'MID', 8: 'MID', 9: 'MID',    10: 'MID', 11: 'MID', 12: 'MID',    13: 'MID', 14: 'MID', 15: 'Material',    16: 'MATNO', 17: 'MID', 18: 'MID',    19: 'MID', 20: 'MID', 21: 'MID',    22: 'MID1', 23: 'MID', 24: 'MID',    25: 'mid'  }    def __init__(self):    self.curDeck:int = base.CurrentDeck()    self.midField:str = self.deck2MatField[self.curDeck]    self.props2MatsDict:Dict[base.Entity, base.Entity] = dict()    def createPid2MidDict(self)->Set[int]:    '''    用于构造prop2Mat字典。若一个mat被多个prop应用,复 制mat重新赋予给prop,    确保prop-mat具有一一对应的关系。同时构建一个matSet用于存储当前所有mat ID    用于后续调整MID过程中ID冲突检查。    '''        matSet:Set[int] = set()    props:List[base.Entity] = base.CollectEntitiesI(self.curDeck, None, '__PROPERTIES__')    for prop in props:      mat:base.Entity = prop.get_entity_values(self.curDeck, (self.midField, ))[self.midField]      if mat._id in matSet:         mat = base.CopyEntity(None, mat)        prop.set_entity_values(self.curDeck, {self.midField: mat._id})      self.props2MatsDict[prop] = mat      matSet.add(mat._id)    return matSet    def run(self):    midSet:Set[int] = self.createPid2MidDict()        # 获取一个临时ID用于当MID于PID冲突时的临时调整    tempMid:int = 1    while tempMid in midSet: tempMid += 1        for prop, mat in self.props2MatsDict.items():      if prop._id==mat._id: continue      # 后续代码原mat的ID将被调整因此其原始ID将空出,可作为后续存储冲突ID的临时ID。      oldMid:int = mat._id      tempMat:base.Entity = base.GetEntity(self.curDeck, '__MATERIALS__', int(prop._id))      if tempMat: base.SetEntityId(tempMat, tempMid, False, True)      base.SetEntityId(mat, prop._id, False, True)      tempMid = oldMid    print('Done!!!')    return None  def Pids2MidsMain():  pidObj = Pids2Mids()  pidObj.run()  if __name__ == '__main__':  Pids2MidsMain()


来源:檐苔
LS-DYNASystemRADIOSSOptiStructNastran二次开发材料
著作权归作者所有,欢迎分享,未经许可,不得转载
首次发布时间:2026-03-18
最近编辑:6月前
青瓦松
硕士 签名征集中
获赞 46粉丝 13文章 63课程 0
点赞
收藏
作者推荐

机器学习与深度学习引言学习笔记

一. 引言传统编程的局限性传统程序依赖人工设计的业务逻辑,需考虑所有边界情况(如购物车功能)。复杂任务(如天气预测、图像识别)无法通过规则实现,需数据驱动方案。机器学习的定义目标:从经验(数据)中学习,提高任务性能。核心流程:定义模型 → 用数据集调整参数 → 优化目标函数(如损失函数)。日常生活中的应用语音助手(如“Hey Siri”唤醒词识别):音频特征→标签映射。图像分类:模型通过调整参数识别特征(如猫狗分类)。二. 机器学习的关键组件数据特征与标签:样本由特征向量(如像素值)和标签(如类别)组成。数据质量:需避免偏差(如医疗数据缺乏某些人群样本)。数据规模:深度学习高度依赖大数据,但需注意分布偏移(训练与测试数据差异)。模型表示学习:深度学习通过多层非线性转换自动提取特征。模型族:如神经网络,参数调整生成不同行为(如不同唤醒词识别)。目标函数损失函数:量化模型有效性(如平方误差用于回归,交叉熵用于分类)。过拟合:模型在训练集表现好,但测试集性能差(需验证泛化能力)。优化算法梯度下降:通过计算损失梯度调整参数,逐步逼近最优解。训练步骤:随机初始化→数据采样→参数更新→重复优化。三. 机器学习的核心问题类型监督学习回归:预测连续值(如房价、降雨量),使用平方误差损失。分类:预测离散类别(如二分类:垃圾邮件检测;多分类:手写数字识别)。多标签分类:样本同时属于多个类别(如文章标签)。推荐系统:个性化推荐(如亚马逊商品推荐),可能受反馈循环影响。序列学习:处理可变长度输入/输出(如语音识别、机器翻译)。无监督学习聚类:无标签数据分组(如客户行为分析)。主成分分析(PCA):降维提取关键特征。生成对抗网络(GANs):合成数据(如生成逼真图像)。强化学习定义:智能体与环境交互,通过奖励调整策略(如AlphaGo)。关键挑战:信用分配(确定哪些动作影响奖励)、部分可观测性(如自动驾驶感知)。应用场景:游戏AI、机器人控制。四. 机器学习的起源与发展早期理论基础统计与优化:高斯分布、费舍尔的线性判别分析。信息论:香农的信息度量,图灵的“机器思考”问题。神经网络的发展早期模型:赫布学习(神经元强化机制)、感知机。深度学习复兴:大数据(如ImageNet)与GPU算力推动。深度学习的突破关键技术:Dropout正则化、注意力机制、端到端训练。框架演进:TensorFlow、PyTorch等简化模型开发。五. 深度学习的成功案例语音与视觉语音识别:错误率接近人类(如Siri、Alexa)。图像分类:ImageNet竞赛Top-5错误率从28%降至2.25%。游戏与决策AlphaGo:结合深度网络与蒙特卡洛树搜索击败人类冠军。自动驾驶:通过感知与推理实现部分自主(如特斯拉)。生成模型GANs应用:生成逼真图像(如人脸合成)、风格迁移。六. 深度学习的特点与挑战核心特点端到端学习:替代人工特征工程(如传统Canny边缘检测→自动学习滤波器)。非参数模型:大数据驱动灵活建模(如深度网络)。挑战与伦理数据偏见:模型可能继承历史偏见(如招聘算法偏好特定群体)。隐私与安全:大规模数据采集的隐私风险。自动化风险:职业替代(如司机、客服)。来源:檐苔

未登录
还没有评论
课程
培训
服务
行家
VIP会员 学习计划 福利任务
下载APP
联系我们
帮助与反馈