跳转至主要内容
Version: v1.3.0

常见问题

Can I enable auto compeletion for Taichi?

Yes, Taichi's Python user-facing APIs should work natively with any language server for Python.

Take VSCode as an example, you can install Python or Pylance extensions to get language support like signature help with type information, code completion etc.

If it doesn't work out of box after installing the extension, please make sure the right Python interpreter is selected by:

  • invoke command palette (Shift + Command + P (Mac) / Ctrl + Shift + P (Windows/Linux))
  • find Python: Select Interpreter
  • make sure you select the path to the Python interpreter you're using with a taichi package installed

为什么在安装太极时,我的 pip 会报错 package not found

您可能使用的是太极不支持的 Python 编译器版本。 目前,Taichi 仅支持 Python 3.7/3.8/3.9/3.10 (64-bit) 。 欲了解更多有关安装问题的信息,请查阅 Installation Troubleshooting.。

太极是否提供内置常量,例如 ti.pi

太极没有提供诸如 pi 的内置常量。 我们建议直接使用 math.pi

太极内核的最外层循环默认并行。 我该如何串行它们呢?

一个可行的解决方案是在想串行的循环外加入一个额外的 幽灵 循环。

for _ in range(1):  # This "ghost" loop will be "parallelized", but with only one thread. Therefore, the containing loop below is serialized.
for i in range(100): # The loop you want to serialize
...

Taichi fields 加载图像最便捷的方式是什么?

一个可行的解决方案是 field.from_numpy(ti.tools.imread('filename.png'))

太极能否与诸如 matplotlib其他 Python packages 相互兼容呢?

太极没有提供诸如 pi 的内置常量。 我们建议直接使用 math.pi

import taichi as ti
pixels = ti.field(ti.f32, (1024, 512))
import numpy as np
arr = np.random.rand(1024, 512)
pixels.from_numpy(arr) # load numpy data into taichi fields
import matplotlib.pyplot as plt
arr = pixels.to_numpy() # store taichi data into numpy arrays
plt.imshow(arr)
plt.show()
import matplotlib.cm as cm
cmap = cm.get_cmap('magma')
gui = ti.GUI('Color map')
while gui.running:
render_pixels()
arr = pixels.to_numpy()
gui.set_image(cmap(arr))
gui.show()

此外,您还可以将 NumPy 数组或 torch tensor 传入 Taichi kernel 作为参数。 更多细节可参阅与外部数组交互

如何声明一个 动态长度 的 field?

The dynamic SNode supports variable-length fields. 它的行为类似于 C++ 中的 std::vector 或者 Python 中的 list

tip

An alternative solution is to allocate a large enough dense field, with a corresponding 0-D field field_len[None] tracking its length. Taichi 提供了诸如 from_numpyto_numpy 等辅助函数来在 Taichi 场和 NumPy 数组之间传输数据。这样你就可以将最喜欢的 Python 安装包(例如 numpypytorchmatplotlib 等)与 Taichi 一起使用。

如何在太极中使用非结构化的数据结构,例如图和四面体网格?

此外,您还可以将 NumPy 数组或 torch tensor 传入 Taichi kernel 作为参数。 更多细节可参阅与外部数组交互 。 接着您就可以通过 for v in verticesfor v in range(n) 遍历元素。

How to install Taichi on a server without Internet access?

Follow these steps to install Taichi on a server without Internet access.

  1. From a computer with Internet access, pip download Taichi, ensuring that this computer has the same operating system as the target server:
pip download taichi

This command downloads the wheel package of Taichi and all its dependencies.

  1. Copy the downloaded .whl packages to your local server and install each with the following command. Note that you must* complete all dependency installation before installing Taichi.
python -m pip install xxxx.whl

Can I integrate Taichi and Houdini?

这些数据结构必须分解为一维的 Taichi fields。 例如表示图时,您可以分配两个 fields:一个用于表示顶点,另一个用于代表边。

You can follow the instructions provided here.

How do I accurately initialize a vector or matrix with f64 precision when my default floating-point precision (default_fp) is f32?

To better understand the question, look at the program below:

import taichi as ti

ti.init()

@ti.kernel
def foo():
A = ti.Vector([0.2, 0.0], ti.f64)
print('A =', A)

B = ti.Vector([ti.f64(0.2), 0.0], ti.f64)
print('B =', B)

foo()

You get the following output:

A = [0.200000002980, 0.000000000000]
B = [0.200000000000, 0.000000000000]

You may notice the value of A is slightly different from [0.2, 0]. This is because, by default, your float literals are converted to ti.f32, and 0.2 in ti.f32 precision becomes 0.200000002980. If you expect A and B to have ti.f64 precision, use ti.f64(0.2) to preserve more effective digits here so that 0.2 keeps its ti.f64 type.

Alternatively, if you can afford having all floating-point operations in f64 precision, you can directly initialize Taichi with ti.init(..., default_fp=ti.f64).

Why does it always return an error when I pass a list from the Python scope to a Taichi kernel?

A Taichi kernel cannot take a Python list directly. You need to use NumPy arrays as a bridge.

For example, the following code snippet does not work:

import taichi as ti
import numpy as np
ti.init()
x = ti.field(ti.i32, shape=3)
array = [10, 20, 30]

@ti.kernel
def test(arr: list):
for i in range(3):
x[i] = arr[i]
test(array)

You need to import NumPy:

import taichi as ti
import numpy as np
ti.init(arch=ti.cpu)
x = ti.field(ti.i32, shape=3)
array = np.array([10, 20, 30])
@ti.kernel
def test(arr: ti.types.ndarray()):
for i in range(3):
x[i] = arr[i]
test(array)