摘要
本文介绍了如何更改有限元软件Abaqus中的云图配色。通过Abaqus后处理界面的ContourPlot Options面板,用户可以手动指定RGB值来自定义云图颜色,但此方法繁琐且不保存自定义设置。为此,作者利用Abaqus的二次开发功能编写了自动更改云图配色的脚本程序,并集成了MATLAB后处理软件Tecplot的部分云图配色方案。用户只需在脚本中定义颜色表,并在Abaqus中运行脚本,即可成功载入自定义的云图配色方案。
正文
您是否觉得有限元软件Abaqus内置的云图颜色不够好看,或者想尝试其他风格的云图配色。如果您和我有同样的想法,那么就按照本文介绍的方法来更换Abaqus云图配色吧。
在有限元软件Abaqus中更改云图颜色非常简单,我们可以在Abaqus的后处理界面中,通过ContourPlot Options面板中的Spectrum选项来创建一个新的云图配色,如下图所示。
通过指定每一个颜色的RGB值,我们可以自由地更改Abaqus中的云图配色。但这样的处理方法非常繁琐,需要逐个输入每种颜色的RGB值。此外,当退出Abaqus后,用户自定义的云图配色不会被保存。这意味着当重新打开软件后,需要重新定义云图配色。
因此,本文基于Abaqus的二次开发功能,编写了自动更改Abaqus云图配色的脚本程序。通过该程序,可以将用户自定义的云图配色自动载入到Abaqus中。此外,笔者还将MATLAB的后处理软件Tecplot的部分云图配色方案集成在该脚本中。当然,你也可以将自己的云图配色方案集成到该脚本中。
脚本使用方法
脚本程序的完整源代码如下所示:
# -* - coding:UTF-8 -*-
# 文件名:ColorMap_Generator.py
from abaqus import *
from abaqusConstants import *
# -----------------------------定义存放颜色表的类(请勿更改)------------------------------
class ColorMap_List:
def __init__(self, ColorMap_Name, ColorMap_Value):
self.ColorMap_Name = ColorMap_Name
self.ColorMap_Value = ColorMap_Value
self.ColorMap_Validated = True
if not (2 <= len(self.ColorMap_Value) <= 24):
self.ColorMap_Validated = False
return
for Color in self.ColorMap_Value:
if len(Color) != 3:
self.ColorMap_Validated = False
return
for Value in Color:
if not (0 <= Value <= 255):
self.ColorMap_Validated = False
return
# -----------------------------------------------------------------------------------
# ------------------------定义载入颜色表至Abaqus的函数(请勿更改)--------------------------
def Abaqus_Spectrum_Generator(My_ColorMap):
# 判断当前颜色表是否有效
if My_ColorMap.ColorMap_Validated == False:
Error_Info = My_ColorMap.ColorMap_Name + "is not a valid color map"
print(Error_Info)
return
# 转换RGB颜色为16进制颜色
My_ColorMap.ColorMap_Value_Hex = []
for RGB in My_ColorMap.ColorMap_Value:
Color_Hex = '#{:02x}{:02x}{:02x}'.format(*RGB)
My_ColorMap.ColorMap_Value_Hex.append(Color_Hex)
My_ColorMap.ColorMap_Value_Hex = tuple(My_ColorMap.ColorMap_Value_Hex)
# 载入颜色表至Abaqus
session.Spectrum(name=My_ColorMap.ColorMap_Name,
colors=My_ColorMap.ColorMap_Value_Hex)
Remind_Info = My_ColorMap.ColorMap_Name + "has been loaded."
print(Remind_Info)
# ----------------------------------------------------------------------------------
# -----------------------------创建颜色表数据(支持添加颜色表)-----------------------------
# 创建MATLAB颜色表Parula
ColorMap_Name = "MATLAB_Parula"
ColorMap_Value = ([62, 38, 168], [67, 49, 200], [71, 62, 225], [72, 78, 241],
[71, 93, 250], [64, 109, 254], [49, 125, 252], [45, 140, 243],
[38, 154, 232], [30, 167, 225], [16, 178, 213], [1, 186, 196],
[32, 193, 177], [51, 199, 157], [75, 203, 132], [110, 205, 102],
[150, 202, 72], [187, 196, 47], [219, 189, 40], [245, 186, 59],
[254, 197, 55], [249, 214, 45], [245, 232, 37], [248, 248, 24],)
ColorMap_MATLAB_Parula = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Turbo
ColorMap_Name = "MATLAB_Turbo"
ColorMap_Value = ([48, 18, 59], [60, 50, 134], [68, 81, 191], [71, 110, 230],
[69, 138, 252], [56, 165, 251], [37, 192, 231], [24, 215, 202],
[31, 233, 175], [60, 245, 142], [101, 253, 105], [143, 255, 73],
[175, 250, 55], [203, 237, 52], [227, 219, 56], [245, 197, 58],
[253, 172, 52], [253, 141, 39], [246, 108, 25], [234, 78, 13],
[216, 55, 6], [193, 35, 2], [164, 19, 1], [129, 6, 2],)
ColorMap_MATLAB_Turbo = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表HSV
ColorMap_Name = "MATLAB_HSV"
ColorMap_Value = ([255, 0, 0], [255, 66, 0], [255, 131, 0], [255, 197, 0],
[247, 255, 0], [181, 255, 0], [116, 255, 0], [50, 255, 0],
[0, 255, 16], [0, 255, 82], [0, 255, 147], [0, 255, 213],
[0, 231, 255], [0, 165, 255], [0, 100, 255], [0, 34, 255],
[32, 0, 255], [98, 0, 255], [163, 0, 255], [229, 0, 255],
[255, 0, 215], [255, 0, 149], [255, 0, 84], [255, 0, 18],)
ColorMap_MATLAB_HSV = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Hot
ColorMap_Name = "MATLAB_Hot"
ColorMap_Value = ([3, 0, 0], [32, 0, 0], [61, 0, 0], [90, 0, 0],
[120, 0, 0], [149, 0, 0], [178, 0, 0], [207, 0, 0],
[236, 0, 0], [255, 11, 0], [255, 40, 0], [255, 69, 0],
[255, 98, 0], [255, 128, 0], [255, 157, 0], [255, 186, 0],
[255, 215, 0], [255, 244, 0], [255, 255, 28], [255, 255, 72],
[255, 255, 116], [255, 255, 159], [255, 255, 203], [255, 255, 247],)
ColorMap_MATLAB_Hot = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Cool
ColorMap_Name = "MATLAB_Cool"
ColorMap_Value = ([0, 255, 255], [11, 244, 255], [22, 233, 255], [33, 222, 255],
[44, 211, 255], [55, 200, 255], [66, 189, 255], [77, 178, 255],
[88, 167, 255], [99, 156, 255], [110, 145, 255], [121, 134, 255],
[132, 123, 255], [143, 112, 255], [154, 101, 255], [165, 90, 255],
[176, 79, 255], [187, 68, 255], [198, 57, 255], [209, 46, 255],
[220, 35, 255], [231, 24, 255], [242, 13, 255], [253, 2, 255],)
ColorMap_MATLAB_Cool = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Spring
ColorMap_Name = "MATLAB_Spring"
ColorMap_Value = ([255, 0, 255], [255, 11, 244], [255, 22, 233], [255, 33, 222],
[255, 44, 211], [255, 55, 200], [255, 66, 189], [255, 77, 178],
[255, 88, 167], [255, 99, 156], [255, 110, 145], [255, 121, 134],
[255, 132, 123], [255, 143, 112], [255, 154, 101], [255, 165, 90],
[255, 176, 79], [255, 187, 68], [255, 198, 57], [255, 209, 46],
[255, 220, 35], [255, 231, 24], [255, 242, 13], [255, 253, 2],)
ColorMap_MATLAB_Spring = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Summer
ColorMap_Name = "MATLAB_Summer"
ColorMap_Value = ([0, 128, 102], [11, 133, 102], [22, 139, 102], [33, 144, 102],
[44, 150, 102], [55, 155, 102], [66, 161, 102], [77, 166, 102],
[88, 172, 102], [99, 177, 102], [110, 183, 102], [121, 188, 102],
[132, 194, 102], [143, 199, 102], [154, 205, 102], [165, 210, 102],
[176, 216, 102], [187, 221, 102], [198, 227, 102], [209, 232, 102],
[220, 238, 102], [231, 243, 102], [242, 249, 102], [253, 254, 102],)
ColorMap_MATLAB_Summer = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Autumn
ColorMap_Name = "MATLAB_Autumn"
ColorMap_Value = ([255, 0, 0], [255, 11, 0], [255, 22, 0], [255, 33, 0],
[255, 44, 0], [255, 55, 0], [255, 66, 0], [255, 77, 0],
[255, 88, 0], [255, 99, 0], [255, 110, 0], [255, 121, 0],
[255, 132, 0], [255, 143, 0], [255, 154, 0], [255, 165, 0],
[255, 176, 0], [255, 187, 0], [255, 198, 0], [255, 209, 0],
[255, 220, 0], [255, 231, 0], [255, 242, 0], [255, 253, 0],)
ColorMap_MATLAB_Autumn = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Winter
ColorMap_Name = "MATLAB_Winter"
ColorMap_Value = ([0, 0, 255], [0, 11, 250], [0, 22, 244], [0, 33, 239],
[0, 44, 233], [0, 55, 227], [0, 66, 222], [0, 77, 217],
[0, 88, 211], [0, 99, 206], [0, 110, 200], [0, 121, 195],
[0, 132, 189], [0, 143, 184], [0, 154, 178], [0, 165, 173],
[0, 176, 167], [0, 187, 162], [0, 198, 156], [0, 209, 151],
[0, 220, 145], [0, 231, 140], [0, 242, 134], [0, 253, 129],)
ColorMap_MATLAB_Winter = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Gray
ColorMap_Name = "MATLAB_Gray"
ColorMap_Value = ([0, 0, 0], [11, 11, 11], [22, 22, 22], [33, 33, 33],
[44, 44, 44], [55, 55, 55], [66, 66, 66], [77, 77, 77],
[88, 88, 88], [99, 99, 99], [110, 110, 110], [121, 121, 121],
[132, 132, 132], [143, 143, 143], [154, 154, 154], [165, 165, 165],
[176, 176, 176], [187, 187, 187], [198, 198, 198], [209, 209, 209],
[220, 220, 220], [231, 231, 231], [242, 242, 242], [253, 253, 253],)
ColorMap_MATLAB_Gray = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Bone
ColorMap_Name = "MATLAB_Bone"
ColorMap_Value = ([0, 0, 0], [10, 10, 14], [19, 19, 27], [29, 29, 40],
[39, 39, 53], [48, 48, 67], [58, 58, 80], [67, 67, 93],
[77, 77, 107], [87, 88, 119], [96, 101, 128], [106, 115, 138],
[116, 128, 147], [125, 141, 157], [135, 154, 167], [144, 168, 176],
[154, 181, 186], [164, 194, 195], [177, 205, 205], [192, 215, 215],
[207, 224, 224], [222, 234, 234], [237, 244, 244], [252, 253, 253],)
ColorMap_MATLAB_Bone = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Copper
ColorMap_Name = "MATLAB_Copper"
ColorMap_Value = ([0, 0, 0], [14, 9, 5], [28, 17, 11], [41, 26, 16],
[55, 34, 22], [69, 43, 27], [83, 52, 33], [96, 60, 38],
[110, 69, 44], [124, 77, 49], [138, 86, 55], [151, 95, 60],
[165, 103, 66], [179, 112, 71], [193, 120, 77], [206, 129, 82],
[220, 137, 88], [234, 146, 93], [248, 155, 99], [255, 163, 104],
[255, 172, 109], [255, 180, 115], [255, 189, 120], [255, 198, 126],)
ColorMap_MATLAB_Copper = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Pink
ColorMap_Name = "MATLAB_Pink"
ColorMap_Value = ([15, 0, 0], [68, 43, 43], [95, 61, 61], [115, 75, 75],
[133, 86, 86], [148, 97, 97], [162, 106, 106], [175, 114, 114],
[187, 122, 122], [196, 133, 130], [201, 149, 137], [206, 163, 143],
[210, 175, 150], [214, 187, 156], [219, 199, 162], [223, 209, 167],
[227, 220, 173], [231, 229, 178], [235, 235, 190], [239, 239, 204],
[243, 243, 217], [247, 247, 230], [251, 251, 242], [254, 254, 253],)
ColorMap_MATLAB_Pink = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Sky
ColorMap_Name = "MATLAB_Sky"
ColorMap_Value = ([230, 241, 248], [220, 235, 246], [210, 230, 243], [200, 224, 241],
[190, 219, 238], [180, 214, 236], [170, 208, 233], [160, 203, 230],
[150, 197, 228], [140, 192, 225], [131, 186, 223], [121, 181, 220],
[111, 175, 218], [101, 170, 215], [91, 164, 212], [81, 159, 210],
[71, 153, 207], [61, 148, 205], [51, 142, 202], [41, 137, 200],
[32, 131, 197], [22, 126, 195], [12, 120, 192], [2, 115, 189],)
ColorMap_MATLAB_Sky = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Abyss
ColorMap_Name = "MATLAB_Abyss"
ColorMap_Value = ([9, 35, 55], [11, 40, 62], [12, 44, 69], [13, 49, 77],
[14, 53, 84], [16, 58, 91], [17, 62, 98], [18, 67, 105],
[19, 71, 112], [21, 76, 120], [22, 80, 127], [23, 85, 134],
[24, 89, 141], [25, 94, 148], [27, 98, 155], [28, 103, 162],
[29, 107, 170], [30, 112, 177], [32, 117, 184], [33, 121, 191],
[34, 126, 198], [35, 130, 205], [37, 135, 212], [38, 139, 220],)
ColorMap_MATLAB_Abyss = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建MATLAB颜色表Jet
ColorMap_Name = "MATLAB_Jet"
ColorMap_Value = ([0, 0, 131], [0, 0, 175], [0, 0, 219], [0, 8, 255],
[0, 52, 255], [0, 96, 255], [0, 139, 255], [0, 183, 255],
[0, 227, 255], [16, 255, 239], [60, 255, 195], [104, 255, 151],
[147, 255, 108], [191, 255, 64], [235, 255, 20], [255, 231, 0],
[255, 187, 0], [255, 143, 0], [255, 100, 0], [255, 56, 0],
[255, 12, 0], [223, 0, 0], [179, 0, 0], [135, 0, 0],)
ColorMap_MATLAB_Jet = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建Tecplot颜色表Rainbow1
ColorMap_Name = "Tecplot_Rainbow1"
ColorMap_Value = ([0, 0, 130], [0, 0, 255], [0, 255, 255], [255, 255, 0],
[255, 0, 0], [130, 0, 0],)
ColorMap_Tecplot_Rainbow1 = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建Tecplot颜色表Hot
ColorMap_Name = "Tecplot_Hot"
ColorMap_Value = ([2, 0, 0], [127, 0, 0], [255, 0, 0], [255, 255, 0], [255, 255, 255],)
ColorMap_Tecplot_Hot = ColorMap_List(ColorMap_Name, ColorMap_Value)
# 创建Tecplot颜色表Viridis
ColorMap_Name = "Tecplot_Viridis"
ColorMap_Value = ([68, 1, 84], [72, 31, 112], [67, 63, 133], [54, 92, 141],
[43, 117, 142], [33, 142, 141], [33, 166, 133], [64, 189, 114],
[119, 209, 83], [186, 222, 40], [253, 231, 37])
ColorMap_Tecplot_Viridis = ColorMap_List(ColorMap_Name, ColorMap_Value)
# -----------------------------------------------------------------------------------
# --------------------------------载入颜色表至Abaqus-----------------------------------
# 设置Abaqus中云图颜色数量(大于等于2且小于等于24)
session.viewports['Viewport: 1'].odbDisplay.contourOptions.setValues(numIntervals=24)
# 载入MATLAB颜色表Parula
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Parula)
# 载入MATLAB颜色表Turbo
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Turbo)
# 载入MATLAB颜色表HSV
Abaqus_Spectrum_Generator(ColorMap_MATLAB_HSV)
# 载入MATLAB颜色表Hot
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Hot)
# 载入MATLAB颜色表Cool
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Cool)
# 载入MATLAB颜色表Spring
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Spring)
# 载入MATLAB颜色表Summer
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Summer)
# 载入MATLAB颜色表Autumn
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Autumn)
# 载入MATLAB颜色表Winter
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Winter)
# 载入MATLAB颜色表Gray
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Gray)
# 载入MATLAB颜色表Bone
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Bone)
# 载入MATLAB颜色表Copper
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Copper)
# 载入MATLAB颜色表Pink
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Pink)
# 载入MATLAB颜色表Sky
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Sky)
# 载入MATLAB颜色表Abyss
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Abyss)
# 载入MATLAB颜色表Jet
Abaqus_Spectrum_Generator(ColorMap_MATLAB_Jet)
# 载入Tecplot颜色表Rainbow1
Abaqus_Spectrum_Generator(ColorMap_Tecplot_Rainbow1)
# 载入Tecplot颜色表Hot
Abaqus_Spectrum_Generator(ColorMap_Tecplot_Hot)
# 载入Tecplot颜色表Viridis
Abaqus_Spectrum_Generator(ColorMap_Tecplot_Viridis)
# -----------------------------------------------------------------------------------
在该脚本的开头,笔者定义了存放颜色表的类以及将颜色表载入至Abaqus的函数,该部分内容是不能更改的。而下图所示的颜色表数据则是允许用户自定义的内容。
其中,ColorMap_Name
代表了颜色表的名称,即在Abaqus中创建的Spectrum的名称。ColorMap_Value用于定义不同颜色的RGB值。由于Abaqus最多只允许云图存在24种颜色,因此颜色数量不能超过24,也不能小于2。ColorMap_List用于创建代表该颜色表的对象。
在脚本文件的末尾,Abaqus_Spectrum_Generator函数用于将该颜色表载入至Abaqus。
将上述源代码保存为Python文件,并在Abaqus中通过Run Script选项运行脚本。运行完成后,Abaqus的消息面板将会给出颜色表是否被载入Abaqus的提示信息,如下图所示。如果颜色表定义错误,则将给出提示信息。
运行完成后,可以发现云图选项中多了一些新的配色方案,意味着定义的颜色表载入成功。笔者非常喜欢后处理软件Tecplot中的云图配色方案,相比于Abaqus的云图色彩更为浓郁。