跳转到内容

任务接口(API Key)

以下接口允许你通过 API Key 认证来创建和管理 TopMat 任务。所有请求都必须在 Authorization 头中携带有效的 API Key。

TopMat 任务是基于热力学数据库(TOPDB)和相场模型对材料体系进行数值计算的作业。典型的任务包括单点平衡计算、相图绘制、凝固模拟等。通过 API,你可以提交任务、查询进度、获取结果文件,并嵌入到自己的材料研发流程中。

  1. 发现并选择数据库:调用 GET /api/v2/database 浏览数据库,或用 GET /api/v2/database/by-task 按任务类型筛选,并用 POST /api/v2/database/{db_id}/phases 查询可用相,详见数据库接口
  2. 提交任务:调用 POST /api/v2/task,在 task_config 中传入具体计算类型(task_type)和数据库代码(tdb_code,即所选数据库的 code)。
  3. 等待并执行:任务依次进入 pendingqueuedrunning 状态。
  4. 查询进度:使用 GET /api/v2/task/{id} 轮询任务状态,直到进入终态(completed / failed / aborted)。
  5. 获取结果:任务完成后,调用 GET /api/v2/task/{id}/result 获取结果文件列表,或 GET /api/v2/task/{id}/previewGET /api/v2/task/{id}/vtk 获取预览和 VTK 数据。
任务类型task_type说明
单点平衡计算point_calculation在指定温度、压力和成分下计算体系的平衡相组成。
线计算line_calculation沿单一变量(如温度或成分)扫描,获取相变和相分数变化。
二元相图计算binary_equilibrium计算二元体系在温度-成分空间的相图。
热力学性质计算thermodynamic_properties_next输出定压热容、焓、熵、化学势等热力学函数。
Scheil 凝固模拟scheil_solidification基于 Scheil-Gulliver 模型模拟非平衡凝固路径。
三维相图计算binary_cpt_diagram在成分、温度、压力等条件空间中计算并展示相平衡关系。

以下任务类型尚未开放,敬请期待:

  • physical_vapor_deposition — 物理气相沉积
  • cutting_process — 切削加工
  • spinodal_decomposition — 调幅分解
  • solidification — 凝固
  • solution_treatment — 固溶处理
  • aging — 时效处理
  • directional_solidification — 定向凝固

不同任务类型的 task_config 字段略有差异,但通常包含以下共同字段:

task_config 通用结构示例
{
"task_type": "point_calculation",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"task_path": "examples/framework_demo/result/point_calculation_1",
"condition": {
"components": ["HF", "C"],
"activated_phases": ["LIQUID", "HCP_A3_HF"],
"temperature": 300,
"compositions": { "HF": 0.7, "C": 0.3 }
}
}
字段类型说明
task_typestring具体计算类型,例如 line_calculation。它与外层 engine 字段职责不同:engine 选择执行器(热力学任务为 topthermo_next),task_type 选择该执行器内的计算类型。
tdb_codestring热力学数据库代码,例如 TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB
task_pathstring结果输出路径。
conditionobject计算条件,包含成分、温度、压力、激活相、输出项等。

创建任务接口通过 tdb_code 指定数据库。服务端会根据 tdb_code 查找已启用的数据库记录,校验当前用户的订阅访问权限,然后把配置中的 tdb_code 替换为服务端管理的数据库文件路径(tdb_file)后再交给计算核心。tdb_code 的值是数据库的 code,可通过数据库接口发现和筛选。

为兼容旧客户端,缺少 tdb_code 时服务端会从 tdb_file 路径的文件名推导出数据库代码,客户端传入的文件路径本身不会被使用。

Base URL
https://api.topmaterial-tech.com/api/v2
使用 API Key 认证
curl https://api.topmaterial-tech.com/api/v2/task/list \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
创建任务接口
POST /task

为 API Key 所属用户创建一个新任务。

请求体示例:线计算任务
{
"title": "Line-Calculation-53",
"engine": "topthermo_next",
"task_config": {
"task_type": "line_calculation",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"task_path": "",
"condition": {
"components": ["HF", "C"],
"compositions_start": { "HF": 0.8, "C": 0.2 },
"compositions_end": { "HF": 0.8, "C": 0.2 },
"temperature_start": 300,
"temperature_end": 3000,
"pressure": 101325,
"points": 101,
"activated_phases": [],
"topthermo_outputs": ["T", "P", "phase_name", "x(*)", "G", "H", "S"]
}
},
"omp_threads": 4,
"no_cache": false
}
字段类型必填说明
titlestring任务标题。
enginestringTaskMan 执行器。本文档列出的热力学计算使用 topthermo_next(默认值,可省略);具体计算类型由 task_config.task_type 指定。
task_configobject任务配置对象,必须为有效的 JSON 对象,且必须包含 task_typetdb_code
omp_threadsintegerOpenMP 线程数。
no_cachebooleantrue 时跳过计算缓存,默认 false
创建线计算任务
curl -X POST https://api.topmaterial-tech.com/api/v2/task \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef" \
-H "Content-Type: application/json" \
-d '{
"title": "Line-Calculation-53",
"engine": "topthermo_next",
"task_config": {
"task_type": "line_calculation",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"task_path": "",
"condition": {
"components": ["HF", "C"],
"compositions_start": { "HF": 0.8, "C": 0.2 },
"compositions_end": { "HF": 0.8, "C": 0.2 },
"temperature_start": 300,
"temperature_end": 3000,
"pressure": 101325,
"points": 101,
"activated_phases": [],
"topthermo_outputs": ["T", "P", "phase_name", "x(*)", "G", "H", "S"]
}
},
"omp_threads": 4,
"no_cache": false
}'
响应示例:任务创建成功
{
"id": 42,
"status": "pending",
"task_type": "line_calculation",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB"
}
字段类型说明
idinteger新建任务的 ID。
statusstring任务初始状态,通常为 pending;缓存命中时为 completed
task_typestring服务端存储的具体计算类型,取自 task_config.task_type;本例为 line_calculation
tdb_codestring | null任务使用的数据库代码。
场景消息
未提供 task_config(且无法从 description 解析)task_config or description is required
task_config 不是 JSON 对象task_config must be a JSON object
缺少 task_config.task_typetask_config.task_type is required
缺少 tdb_code(且无法从 tdb_file 推导)task_config.tdb_code or task_config.tdb_file is required
tdb_code 不存在或未启用TopDB code '<code>' was not found or is disabled
不支持的 enginetask type '<engine>' is not supported, ...

创建任务会校验当前用户对 tdb_code 对应数据库的订阅访问权限,无有效订阅时返回:

响应示例:无数据库访问权限
{
"error": "the current subscription does not allow access to TopDB code 'TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB'"
}

创建任务时会依次检查两类限制:

  1. 创建任务次数限制:按当前角色订阅套餐的 task_using_limit 执行,统计窗口为滚动 24 小时。具体额度取决于用户当前生效的套餐。
  2. 待处理任务数量限制:限制同一用户同时处于 pending 状态的任务数,具体上限由服务端部署配置决定。

超过待处理任务数量限制时,API 返回 403 Forbidden,并附带当前数量和本次生效的上限。以下示例假设部署上限为 200:

响应示例:超出待处理任务上限
{
"error": "Pending task limit exceeded. Maximum allowed pending tasks: 200",
"current_pending_tasks": 200,
"max_pending_tasks": 200
}

超过套餐的 24 小时创建任务次数限制时同样返回 403 Forbidden。响应中的 usedlimit 是当前用户的实际用量与套餐额度,例如:

响应示例:超出创建频率限制
{
"error": "Rate limit exceeded: used 100 of 100 requests in the last 24 hours"
}
列出任务接口
GET /task/list

以分页形式返回 API Key 用户拥有的任务列表。分页参数通过查询字符串(query)传递,不支持按状态或类型过滤。

参数类型必填说明
pageinteger页码,默认 1,最小 1
items_per_pageinteger每页条数,默认 10,范围 1100
sort_orderstringasc(默认)或 desc,按任务 ID 排序。
查询任务列表
curl "https://api.topmaterial-tech.com/api/v2/task/list?page=1&items_per_page=10&sort_order=asc" \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:任务列表
{
"data": [
{
"id": 42,
"title": "Line-Calculation-53",
"description": "{...}",
"status": "completed",
"task_type": "line_calculation",
"result": "Task completed successfully",
"logs": null,
"user_id": "550e8400-e29b-41d4-a716-446655440000",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"created_at": "2026-05-20 08:00:00 UTC",
"updated_at": "2026-05-20 08:05:00 UTC"
}
],
"total": 1,
"total_pages": 1,
"page": 1,
"items_per_page": 10
}
字段类型说明
dataarray任务列表,每项为 TaskStatusResponse 对象。
data[].idinteger任务 ID。
data[].titlestring任务标题。
data[].descriptionstring任务描述,为存储的 task_config 的 JSON 字符串。
data[].statusstring任务状态。
data[].task_typestring任务类型。
data[].resultstring | null任务结果摘要。
data[].logsstring | null任务日志。
data[].user_idstring所属用户 UUID。
data[].tdb_codestring | null任务使用的数据库代码。
data[].created_atstring创建时间(UTC 字符串,形如 2026-05-20 08:00:00 UTC)。
data[].updated_atstring更新时间(UTC 字符串)。
totalinteger总记录数。
total_pagesinteger总页数。
pageinteger当前页码。
items_per_pageinteger每页条数。
查看任务接口
GET /task/{id}

按 ID 获取单个任务详情。该任务必须属于 API Key 用户。

参数类型说明
idinteger任务的数据库 ID(仅接受整数)。
查询任务详情
curl https://api.topmaterial-tech.com/api/v2/task/42 \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:任务详情
{
"id": 42,
"title": "Line-Calculation-53",
"description": "{...}",
"status": "completed",
"task_type": "line_calculation",
"result": "Task completed successfully",
"logs": null,
"user_id": "550e8400-e29b-41d4-a716-446655440000",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"created_at": "2026-05-20 08:00:00 UTC",
"updated_at": "2026-05-20 08:05:00 UTC"
}
字段类型说明
idinteger任务 ID。
titlestring任务标题。
descriptionstring任务描述,为存储的 task_config 的 JSON 字符串。
statusstring任务状态。
task_typestring任务类型。
resultstring | null任务结果摘要。
logsstring | null任务日志。
user_idstring所属用户 UUID。
tdb_codestring | null任务使用的数据库代码。
created_atstring创建时间(UTC 字符串,形如 2026-05-20 08:00:00 UTC)。
updated_atstring更新时间(UTC 字符串)。
状态码含义响应体
403无权访问该任务{"error": "Access denied"}
404任务不存在{"error": "Task not found"}
取消任务接口
PATCH /task/{id}/cancel

取消一个任务,将其状态设置为 aborted。只有处于 pendingqueuedrunning 状态的任务才能取消。

参数类型说明
idinteger任务的数据库 ID(仅接受整数)。
取消任务
curl -X PATCH https://api.topmaterial-tech.com/api/v2/task/42/cancel \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:任务已取消
{
"id": 42,
"title": "Line-Calculation-53",
"description": "{...}",
"status": "aborted",
"task_type": "line_calculation",
"result": null,
"logs": null,
"user_id": "550e8400-e29b-41d4-a716-446655440000",
"tdb_code": "TOPDB-B-C-Si-Zr-Hf-La-Y-Ti-O.TOPDB",
"created_at": "2026-05-20 08:00:00 UTC",
"updated_at": "2026-05-20 08:05:00 UTC"
}
字段类型说明
idinteger任务 ID。
titlestring任务标题。
descriptionstring任务描述,为存储的 task_config 的 JSON 字符串。
statusstring取消后的任务状态,为 aborted
task_typestring任务类型。
resultstring | null任务结果摘要。
logsstring | null任务日志。
user_idstring所属用户 UUID。
tdb_codestring | null任务使用的数据库代码。
created_atstring创建时间(UTC 字符串)。
updated_atstring更新时间(UTC 字符串)。
状态码含义响应体
409当前状态无法取消任务{"error": "Task cannot be stopped in current status: completed"}
删除任务接口
DELETE /task/{id}

永久删除一个任务。路径参数 {id} 接受整数数据库 ID 或任务 UUID。

如果任务仍处于可停止状态(pendingqueuedrunning),系统会先停止它,然后再删除。

删除任务
curl -X DELETE https://api.topmaterial-tech.com/api/v2/task/42 \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"

删除成功时无响应体。

状态码含义响应体
400id 既不是整数也不是 UUID{"error": "id must be an integer or uuid"}
404任务不存在{"error": "Task not found"}
获取结果文件接口
GET /task/{id}/result

返回已完成任务的结果文件列表。

获取结果文件
curl https://api.topmaterial-tech.com/api/v2/task/42/result \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:结果文件列表
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"files": [
"https://storage.example.com/results/42/file1.dat?signature=..."
],
"total_count": 1
}
字段类型说明
task_idstring任务 UUID。
filesstring[]结果文件下载 URL 列表(预签名临时 URL)。
total_countinteger文件总数。
获取预览文件接口
GET /task/{id}/preview

返回任务的预览文件列表。

获取预览文件
curl https://api.topmaterial-tech.com/api/v2/task/42/preview \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:预览文件列表
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"files": [
"https://storage.example.com/previews/42/preview.png?signature=..."
],
"total_count": 1
}
字段类型说明
task_idstring任务 UUID。
filesstring[]预览文件下载 URL 列表(预签名临时 URL)。
total_countinteger文件总数。
获取 VTK 压缩包接口
GET /task/{id}/vtk

返回任务的 VTK 压缩文件信息。

获取 VTK 压缩包
curl https://api.topmaterial-tech.com/api/v2/task/42/vtk \
-H "Authorization: Bearer tk_0123456789abcdef0123456789abcdef"
响应示例:VTK 文件列表
{
"task_id": "550e8400-e29b-41d4-a716-446655440000",
"files": [
"https://storage.example.com/vtks/42/results.zip?signature=..."
],
"total_count": 1
}
字段类型说明
task_idstring任务 UUID。
filesstring[]VTK 压缩文件下载 URL 列表(预签名临时 URL)。
total_countinteger文件总数。

完整状态枚举如下。终态为 completedfailedabortedpendingqueuedrunning 状态的任务可以取消。

状态说明
created任务已创建,尚未进入调度。
pending任务等待被接收(可取消)。
queued任务正在等待执行(可取消)。
running任务正在执行(可取消)。
uploading任务结果上传中。
completed任务成功完成(终态)。
failed任务执行失败(终态)。
aborted任务被用户取消(终态)。

状态流转关系:

  • createdqueued / aborted / failed
  • pendingqueued / running / aborted / failed
  • queuedrunning / uploading / aborted / failed
  • runninguploading / aborted / failed
  • uploadingcompleted / aborted / failed