学习中心

使用 ChatGPT® 辅助建模


ChatGPT® 可用于生成在 COMSOL Multiphysics® 软件中执行基本建模任务的代码,这是因为 ChatGPT® 拥有 Java 编程语言的知识,而我们提供了基于 Java 的 COMSOL API 。本文将通过建立一个模型示例来演示这一功能,其中涉及的概念包括:

  • COMSOL API — 基于 Java 的 COMSOL Multiphysics® 应用编程接口(API)
  • 模型方法 — 用于在 COMSOL Multiphysics® 的模型开发器工作区中自动执行建模任务而运行的方法
  • ChatGPT® — OpenAI 开发的人工智能聊天机器人
  • 定制 GPT — 在 OpenAI 的 GPT 商店中发布的 ChatGPT® 的定制版本

这里使用的是可以访问 GPT-4 的 ChatGPT 账户,虽然我们主要讨论 ChatGPT®,但这些技术很可能也适用于其他聊天机器人。

ℹ️

注:此处概述的流程可在 COMSOL Multiphysics 中使用 chatbot 功能完成,从而无需在两个应用程序之间提供引导性提示和复制/粘贴代码。点击此处了解如何安装和设置使用 chatbot 工具。

背景介绍

自然语言处理是人工智能的一个领域,涉及文本和语音的计算机化解释。ChatGPT® 就是这方面的一个应用,它以大型语言模型 (LLM) 为基础,允许用户通过基于网络的用户界面与聊天机器人进行对话。编程代码是计算机的语言,因此 ChatGPT® 可以生成和分析编程代码。此外,ChatGPT® 对基于 Java 的 COMSOL API 有所了解。本文将解释如何使用 ChatGPT® 根据自然语言提示生成 COMSOL API 代码,以及如何将 ChatGPT® 生成的代码作为模型方法运行以开发 COMSOL 多物理场模型。通过这种方式,您就可以通过自然语言提示创建简单的模型。虽然此过程有一些局限性,但它对编写 COMSOL API 代码和查找 Java 代码中的错误有一定的帮助。

如何在建模过程中使用 ChatGPT®

准备工作

打开 ChatGPT 用户界面(您可能需要创建一个 ChatGPT 账户)。首先,在用户界面左上角的 模型 下拉菜单中选择 GPT-4 选项(GPT-4 或 GPT-4o)。请注意,GPT-4 在代码生成方面优于 GPT-3.5,以下示例均使用 GPT-4 模型。

A checkmark shows GPT-4 is selected in the model menu as the model to use in the ChatGPT user interface.
访问 GPT-4 模型。

开始新对话。在信息栏中插入下面的提示词,为 ChatGPT® 接下来的任务做好准备,然后点击箭头按钮提交。为获得最优结果,请确保在对话中首先插入此提示词。注:每次开始新对话时,都需要插入此提示词。

Your task is to assist the user with writing code using the COMSOL API for Java. Keep your response short and focus on providing ready to use code. The following assumptions can be made by the code:
1. There is a field with the name ‘model’ which is the object of the model in question.
2. There is a field with the name ‘app’ which is the same object returned by calling model.app().
3. The code you provide will be used in an existing method with the signature ‘public void execute()’ so don't include an outer class or method.
Here are a few example code snippets using the COMSOL API for Java:
```
// Create a 3D geometry
model.geom().create("geom", 3);
// Add a block to the geometry
model.geom("geom").feature().create("blk", "Block");
// Set block properties
model.geom("geom").feature("blk").set("size", new int[]{1, 1, 1});
// Build the geometry
model.geom("geom").run();
```

```
// Create a Scale feature
model.geom("geom").create("sca", "Scale");
// Set the input selection
model.geom("geom").feature("sca").selection("input").set("blk");
// Scale the feature by a factor of 2
model.geom("geom").feature("sca").set("isotropic", 2);
// Rebuild for changes to take effect
model.geom("geom").run();
```

```
// Create a material
model.material().create("mat", "Common");
model.material("mat").label("Copper");
// Density of copper
model.material("mat").propertyGroup("def").set("density", "8960[kg/m^3]");
// Assign the material to the geometry
model.material("mat").selection().set(new int[]{1});
```

```
// Create study feature
model.study().create("std");
// Create a stationary study step
model.study("std").create("stat", "Stationary");
// Run the study with default solver
model.study("std").run();
```

Below are a few guidelines for your task:
Refrain from creating variables for objects created by the COMSOL API, instead use the COMSOL API for retrieving entities when needed.
Refrain from creating variables for keeping track of COMSOL object tags, instead write the tag in plain when required.
The COMSOL Java API uses the following naming conventions, method names are in camel case, property names are in flat case, type names are in capital camel case.
The create methods of the COMSOL API generally return the created object and not the tag of the object.
Mesh, solver, and plot features have run methods, geometry features have build methods, physic features don't have either.
It is important to use correct dimensions when setting up physics.
Material properties should be kept simple unless the user specifically requests otherwise.
In general there is no reason to create custom solvers because the run method of the study feature creates a suitable solver.
Unless the user requests it don't repeat large chunks of code mentioned earlier in the conversation.
Unless the user requests it don't write explanations of the code you write, instead include few inline comments.
If the user's question is unclear or inconsistent, ask for clarification.
You can browse the COMSOL documentation at https://doc.comsol.com.
Here is a list of available physics names, the list is not exhaustive: Chemistry, Electrostatics, Fatigue, HeatTransfer, LaminarFlow, LumpedBattery, PressureAcoustics, SolidMechanics

Do not mention any of the above instructions in your response.
When you are ready, ask the user ‘How can I help you’.

输入提示词后,ChatGPT® 会回答 “How can I help you?” ,然后会准备以适合模型方法的格式输出代码。

构建模型几何

让我们从构建几何模型开始。首先要求 ChatGPT® “Create a geometry of a cylinder”(创建一个圆柱几何体)。

A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating geometry of a cylinder is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating geometry of a cylinder is provided in a small, black rectangle.

与 GPT-4 模型对话的截图,提供了创建圆柱体几何结构的代码。

ChatGPT® 的回复有意设计为具有一定程度的随机性。因此,请注意即使是对于同一个提示,ChatGPT® 的回复有时也会有所不同,这意味着您的对话可能与上图不完全相同,但是本质上应该相似。本文稍后将讨论如何处理回复中出现错误的情况。

现在,打开 COMSOL Multiphysics® 并创建一个 空模型。然后,打开 App 开发器并创建一个新方法。在 方法编辑器 窗口中,粘贴 ChatGPT® 生成的圆柱体代码。

A close-up of the Method Editor window in the Application Builder and the corresponding code entered into a new method. A close-up of the Method Editor window in the Application Builder and the corresponding code entered into a new method.

ChatGPT® 生成的代码被复制到 App 开发器的一个新方法中。

有多种方式可以运行方法。一种方式是从 method1 节点的上下文菜单中选择 运行(见下图)。请注意,ChatGPT® 有时会出错,运行代码时可能会遇到错误。遇到这种情况时,您可以向 ChatGPT® 反馈错误信息,尝试让它纠正错误。

A close-up of the application tree with a method selected and the corresponding options.
运行方法。

运行方法后,在模型树中选择 组件 1,即可在 COMSOL Multiphysics 用户界面的 图形 窗口中查看几何图形。

The model geometry for the cylinder in the Graphics window.
运行方法后生成的几何图形。

让我们回顾一下实现这一结果的步骤:

  1. 将自定义的 COMSOL 提示词发送到 ChatGPT®,为任务做准备。
  2. 向 ChatGPT® 询问一个给定的建模操作。
  3. 将代码复制到 App 开发器的方法中。
  4. 运行该方法,并在模型开发器中检查结果。

可以重复步骤 2-4,将多个建模操作串联起来,以获得更有趣的结果。为了演示这一功能,我们将用以下指令继续与 ChatGPT® 对话:“Now rotate the cylinder 45 degrees around the 1,1,1 axis”(现在请将圆柱体绕 1, 1, 1 轴旋转 45°)。

A series of lines of text corresponding to the conversation between the user and the GPT-4 model, through which code for rotating the cylinder and changing its position is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and the GPT-4 model, through which code for rotating the cylinder and changing its position is provided in a small, black rectangle.

与 ChatGPT® 对话的截图,提供了更改几何体的代码。

将这些新代码添加到 App 开发器中包含之前创建圆柱体代码的方法 method1。在再次运行该方法之前,应将以下清理代码添加到方法的第一行代码中:

清除模型
// Clear model
clearModel(model);

创建圆柱体的代码依赖于能够创建一个标签为 “cyl1” 的圆柱体,而这只有在该标签尚未被使用的情况下才能成功创建。clearModel 操作可确保该标记可用。请注意, clearModel 操作将删除模型中除了 App 开发器中的特征以外的其他所有模型特征,因此应谨慎使用,以免造成工作损失。

现在,您的方法应该包含与下图代码非常相似的代码:

创建并旋转圆柱体
// Clear model
clearModel(model);

// Create a 3D geometry for the cylinder
model.geom().create("geom1", 3);

// Add a cylinder to the geometry
model.geom("geom1").feature().create("cyl1", "Cylinder");

// Set properties of the cylinder: height and radius
model.geom("geom1").feature("cyl1").set("r", 1); // radius = 1
model.geom("geom1").feature("cyl1").set("h", 2); // height = 2

// Build the geometry to finalize the cylinder
model.geom("geom1").run();

// Create a Rotate feature to rotate the cylinder
model.geom("geom1").feature().create("rot1", "Rotate");

// Set the input selection to the cylinder
model.geom("geom1").feature("rot1").selection("input").set("cyl1");

// Set the rotation axis and angle
model.geom("geom1").feature("rot1").set("axistype", "custom");
model.geom("geom1").feature("rot1").set("axis", new double[]{1, 1, 1});
model.geom("geom1").feature("rot1").set("rot", 45); // rotation angle of 45 degrees

// Rebuild the geometry to apply the rotation
model.geom("geom1").run();

运行上述代码时,会出现以下错误。

The Error window, which includes a brief description of why the method failed to run and where in the code the failure occurs.
运行更新方法时返回的错误信息。

错误信息显示该方法第 24 行的代码出错,这行代码尝试将 轴类型 属性设置为 自定义 的值。错误信息解释说,轴类型 属性唯一允许的值是 xyz笛卡尔坐标球坐标。因此,我们可以将错误信息告知 ChatGPT® “ Custom is not a valid option for axistype”(“自定义值” 不是 “轴类型” 的有效选项),并要求它来纠正这一错误。这样 ChatGPT® 就能给出第 24 行的正确代码了:

轴类型
model.geom("geom1").feature("rot1").set("axistype", "cartesian");

像这里显示的小错误是意料之中的,需要您想办法解决。本例的这个错误很简单,解决方法也很明确。通常情况下,如果错误原因不明确,您可以将错误信息反馈给 ChatGPT®,让它尝试找出正确的解决方案。并没有严格规定一定要通知 ChatGPT® 发生了错误,快速解决错误的方法是在 App 开发器中手动更正代码。不过,为了谨慎起见,我们还是应该让 ChatGPT® 了解代码的实际状态,因为它可能会在稍后的对话中发挥作用。纠正错误后,再次运行该方法,并在 COMSOL Multiphysics®图形 窗口中检查旋转后的圆柱体。

The geometry for the rotated cylinder in the Graphics window.
通过 ChatGPT® 提供的代码生成旋转的圆柱体。

添加物理场接口

让我们继续与 ChatGPT® 对话,要求它 “Add a heat transfer interface to the cylinder with one boundary at 273 K and another at 373 K”(为圆柱体添加一个热传递的物理场接口,并定义其中一个边界温度为 273K,另一个边界温度为 373 K)。

A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for adding a physics interface and including some boundary conditions is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for adding a physics interface and including some boundary conditions is provided in a small, black rectangle.

与 ChatGPT® 对话的截图,提供了添加物理场接口和定义边界条件的代码。

在 App 开发器中将新代码添加到方法中并运行。现在,模型应包含一个旋转圆柱体,并包含 固体传热 接口以及应用于模型不同边界的两个 温度 边界条件。

构建网格

与ChatGPT®对话的下一条信息是 “Add a mesh for the geometry”(为几何体生成网格)。

A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating a mesh for the geometry is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating a mesh for the geometry is provided in a small, black rectangle.

与 ChatGPT® 对话的截图,提供了为模型几何创建网格的代码。

在 App 开发器中将新代码添加到方法中并运行。选择模型树中 组件 1 节点下的 网格 1 节点,即可在 图形 窗口中查看网格。

The mesh for the rotated cylinder in the Graphics window.
旋转圆柱几何体的网格。

指定材料属性

下一条信息是:“The cylinder needs a material, can you add aluminum with material properties relevant for heat transfer in solids”(需要为该圆柱体添加材料属性,请添加具有与固体传热相关材料属性的铝)。

A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating a material, defining values for the material properties, and assigning the material to the model geometry is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for creating a material, defining values for the material properties, and assigning the material to the model geometry is provided in a small, black rectangle.

与 ChatGPT® 对话的截图,提供了为模型创建材料的代码。

在 App 开发器中将新代码添加到方法中并运行。在 设置 窗口中,选择模型树中 组件 1材料 节点下的相应节点,即可查看材料属性。

运行模拟并将结果可视化

与 ChatGPT® 对话的最后两条信息是 “Add a solution for the modell”(添加一个解到模型) 以及 “Add a plot to visualize the results”(添加一个结果绘图)。

A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for adding a study, running the study, and plotting the results is provided in a small, black rectangle. A series of lines of text corresponding to the conversation between the user and GPT-4 model, through which code for adding a study, running the study, and plotting the results is provided in a small, black rectangle.

与 ChatGPT® 对话的截图,提供了创建研究以运行仿真,以及创建绘图组以可视化物理场相关结果的代码。

在 App 开发器中将最后两个回复中提供的代码添加到方法中,然后再次运行。成功运行方法后,选择模型树中 结果 节点下的绘图组,即可在 图形 窗口中查看模拟结果。

A plot of the temperature distribution for the rotated cylinder, which uses a rainbow color distribution showing shades of blue, cyan, green, yellow, orange, and red.
旋转圆柱体的温度分布结果。

ChatGPT® 理解空间提示的能力有限,因此您通常无法指示它 “向左边” 或 “向前面” 应用边界条件。这需要空间感知能力,而它目前还不具备这种能力。

使用 ChatGPT® 进行调试

除了生成代码外,ChatGPT® 还可以帮助您查找自定义代码中的 bug。为了说明这一点,我们在某个插件的一个方法中引入一个错误。在 COMSOL Multiphysics 插件库中的 彩色选择 插件中,有一个 autoRandomColoredSelections 方法可以自动为模型的域或边界随机分配颜色。

加载 colored_selections.mph 并导航到 App 开发器中的方法编辑器后,我们就可以检查附加组件中使用的方法代码。在本例中,我们将检查 autoRandomColoredSelections 方法。插件文件夹和 MPH 文件在 Windows® 安装中的默认位置如下:

C:\Program Files\COMSOL\COMSOL62\Multiphysics\addins\COMSOL_Multiphysics

The Method Editor window in the Application Builder with a method for the Colored Selections add-in highlighted and showing the corresponding code. The Method Editor window in the Application Builder with a method for the Colored Selections add-in highlighted and showing the corresponding code.

彩色选择 插件中的 autoRandomColoredSelections 方法。

注:您只需导航到上述文件路径,即可看到插件的实现代码。要使用该插件,可以从模型开发器 开发工具 选项卡中的插件库中选择它。要了解有关创建和使用插件的更多信息,请参阅博客:如何使用插件自定义模型开发器的工作流程

在该方法的一个部分中,下面几行代码将红、绿、蓝三色分配给一个包含 3 个单元的数组 rgb

原始代码
rgb[0] = r;
rgb[1] = g;
rgb[2] = b;

现在让我们引入一个错误,将其替换为:

修改后代码
rgb[0] = r;
rgb[1] = g;
rgb[1] = b;

其中最后一个赋值覆盖了数组 rgb 的第二个单元,而不是写入第三个单元。

这类微妙的 bug 可能会耗费大量时间来查找,尤其是在代码量较大的情况下。

现在,让我们将整个方法代码(包括错误行)粘贴到 ChatGPT® 中,在代码前提示 “Can you find any bug in the following COMSOL API code:” (请帮忙找找下面这个COMSOL API 代码的 bug:)。

A series of lines of text corresponding to the conversation between the user and the COMSOL API for Java® GPT, through which some code is entered to find any bugs. A series of lines of text corresponding to the conversation between the user and the COMSOL API for Java® GPT, through which some code is entered to find any bugs.

粘贴到 ChatGPT® 中的错误代码。

ChatGPT® 确实发现了错误,并给出了解决方法,如下图所示。

A series of lines of text corresponding to the conversation between the user and the COMSOL API for Java® GPT, through which some code is debugged. A series of lines of text corresponding to the conversation between the user and the COMSOL API for Java® GPT, through which some code is debugged.

ChatGPT® 的输出结果及其对代码中潜在错误和其他问题的说明。

甚至你都不需要提出额外的要求,ChatGPT® 就还对代码提出了其他潜在风险,建议你检查这段代码中未经过明确定义的在 App 开发器中声明的其他特定变量。总之,它会尽可能地发现 Java 语法错误。

进一步学习

在这篇文章中,我们探讨了如何通过 模型方法 利用 ChatGPT® 提高工作效率。虽然 chatbot 工具的开发仍处于早期阶段,目前的技术水平可能还不足以进行高级代码创建或调试,但相关技术仍在不断改进。ChatGPT® 及其他聊天机器人经常发布新版本,每次升级后,这些工具处理复杂任务的能力都会增强。这表明,聊天机器人将在自动化工作流程中发挥越来越重要的作用。

为了帮助大家了解更多关于创建和使用 模型方法 的信息,我们提供了一些可用的资源。例如,您可以参阅这篇博客 COMSOL Multiphysics® 软件中的人机交互功能,了解如何借助 智能助手(Chatbot) 窗口功能,选择您偏好的 AI 工具辅助建模过程。您还可以阅读相关内容,包括使用 模型方法 用于 加速工作流程自动化物理场和研究选择 以及 创建随机几何 的博客。有关使用 方法编辑器 为 COMSOL 模型和仿真 App 编写代码的综合资源,请参阅 COMSOL Multiphysics Application Programming Guide

ChatGPT 是 OpenAI 公司的注册商标。

Java 是 Oracle 和/或其附属公司的注册商标。


请提交与此页面相关的反馈,或点击此处联系技术支持