运行环境
[Taichi] version 0.8.6, llvm 10.0.0, commit d5f18ffd, win, python 3.7.11
问题描述
尝试导出yml文件成功
ti.aot.start_recording('cloth.yml')
ti.init(arch=ti.cuda)
然后导出c语言代码,生成了.c 和.h 文件,但两个文件都为空
python -m taichi cc_compose cloth.yml cloth.c cloth.h
[Taichi] version 0.8.6, llvm 10.0.0, commit d5f18ffd, win, python 3.7.11
尝试导出yml文件成功
ti.aot.start_recording('cloth.yml')
ti.init(arch=ti.cuda)
然后导出c语言代码,生成了.c 和.h 文件,但两个文件都为空
python -m taichi cc_compose cloth.yml cloth.c cloth.h
Hi @yscbm, 欢迎来到太极论坛。
首先到处 C 语言代码,不能使用 CUDA 后端。具体可以参考文档:here。
import taichi as ti
ti.aot.start_recording('mpm88.yml')
ti.init(arch=ti.cc)
... # your program
我有试过ti.cc都是这个结果,文件都是为0k
你好这是我的代码
import taichi as ti
import time
ti.aot.start_recording('cloth.yml')
ti.init(arch=ti.cc)
#ti.init(arch=ti.cuda) # Alternatively, ti.init(arch=ti.cpu)
#质点数量
N = 128
cell_size = 1.0 / N
#重力
gravity = 10
#弹性系数
stiffness = 1600
#减震
damping = 2
dt = 5e-4
#球半径,中心坐标
ball_radius = 0.1
ball_center = ti.Vector.field(3, float, (1,))
#质点位置和速度
x = ti.Vector.field(3, float, (N, N))
v = ti.Vector.field(3, float, (N, N))
#三角形数量
num_triangles = (N - 1) * (N - 1) * 2
indices = ti.field(int, num_triangles * 3)
vertices = ti.Vector.field(3, float, N * N)
#初始化质点,球坐标
def init_scene():
for i, j in ti.ndrange(N, N):
x[i, j] = ti.Vector([i * cell_size - 0.5,
0.5 ,
j * cell_size - 0.5])
ball_center[0] = ti.Vector([-0.8, 0, 0])
@ti.kernel
def set_indices():
for i, j in ti.ndrange(N, N):
if i < N - 1 and j < N - 1:
square_id = (i * (N - 1)) + j
# 1st triangle of the square
indices[square_id * 6 + 0] = i * N + j
indices[square_id * 6 + 1] = (i + 1) * N + j
indices[square_id * 6 + 2] = i * N + (j + 1)
# 2nd triangle of the square
indices[square_id * 6 + 3] = (i + 1) * N + j + 1
indices[square_id * 6 + 4] = i * N + (j + 1)
indices[square_id * 6 + 5] = (i + 1) * N + j
links = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, -1], [-1, 1], [1, 1]]
links = [ti.Vector(v) for v in links]
@ti.kernel
def step():
#根据重力处理速度
for i in ti.grouped(x):
v[i].y -= gravity * dt
#受到的弹力
for i in ti.grouped(x):
force = ti.Vector([0.0,0.0,0.0])
for d in ti.static(links):
j = min(max(i + d, 0), [N-1,N-1])
relative_pos = x[j] - x[i]
current_length = relative_pos.norm()
original_length = cell_size * float(i-j).norm()
if original_length != 0:
force += stiffness * relative_pos.normalized() * (current_length - original_length) / original_length
v[i] += force * dt
#弹力衰减(模拟摩擦力等因素),判断球碰撞,根据速度更新坐标
for i in ti.grouped(x):
v[i] *= ti.exp(-damping * dt)
#下一帧碰撞速度暂停
x_norm = (x[i] + dt * v[i]-ball_center[0]).norm()
if x_norm < ball_radius+0.01:
#v[i] = ti.Vector([0.0, 0.0, 0.0])
v[i] =( x[i] + dt * v[i]-ball_center[0])/x_norm*(ball_radius+0.01 - x_norm)/dt
#if (x[i]-ball_center[0]).norm() <= ball_radius:
# v[i] = ti.Vector([0.0, 0.0, 0.0])
if i[0]==0:
v[i]=[0,0,0]
x[i] += dt * v[i]
@ti.kernel
def set_vertices():
for i, j in ti.ndrange(N, N):
vertices[i * N + j] = x[i, j]
init_scene()
set_indices()
window = ti.ui.Window("Cloth", (800, 800), vsync=True)
canvas = window.get_canvas()
scene = ti.ui.Scene()
camera = ti.ui.make_camera()
index = 0
while window.running:
#time_a = time.time()
for i in range(10):
step()
#print(time.time()-time_a)
set_vertices()
#camera.position(0.5, -0.5, 2)
#camera.lookat(0.5, -0.5, 0)
camera.position(0, 2, 2)
camera.lookat(0, 0, 0)
scene.set_camera(camera)
scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))
scene.mesh(vertices, indices=indices, color=(0.5, 0.5, 0.5), two_sided = True)
scene.particles(ball_center, radius=ball_radius, color=(0.5, 0, 0))
canvas.scene(scene)
window.show()
index+=1
if index>300:
ball_center[0].x+=0.001
#print(index)
我注释了所有ui相关的代码,并且去除了import time 但是问题还没有解决,下面是我新的代码
import taichi as ti
ti.aot.start_recording('cloth.yml')
ti.init(arch=ti.cc)
#ti.init(arch=ti.cuda) # Alternatively, ti.init(arch=ti.cpu)
#质点数量
N = 128
cell_size = 1.0 / N
#重力
gravity = 10
#弹性系数
stiffness = 1600
#减震
damping = 2
dt = 5e-4
#球半径,中心坐标
ball_radius = 0.1
ball_center = ti.Vector.field(3, float, (1,))
#质点位置和速度
x = ti.Vector.field(3, float, (N, N))
v = ti.Vector.field(3, float, (N, N))
#三角形数量
num_triangles = (N - 1) * (N - 1) * 2
indices = ti.field(int, num_triangles * 3)
vertices = ti.Vector.field(3, float, N * N)
#初始化质点,球坐标
def init_scene():
for i, j in ti.ndrange(N, N):
x[i, j] = ti.Vector([i * cell_size - 0.5,
0.5 ,
j * cell_size - 0.5])
ball_center[0] = ti.Vector([-0.8, 0, 0])
@ti.kernel
def set_indices():
for i, j in ti.ndrange(N, N):
if i < N - 1 and j < N - 1:
square_id = (i * (N - 1)) + j
# 1st triangle of the square
indices[square_id * 6 + 0] = i * N + j
indices[square_id * 6 + 1] = (i + 1) * N + j
indices[square_id * 6 + 2] = i * N + (j + 1)
# 2nd triangle of the square
indices[square_id * 6 + 3] = (i + 1) * N + j + 1
indices[square_id * 6 + 4] = i * N + (j + 1)
indices[square_id * 6 + 5] = (i + 1) * N + j
links = [[-1, 0], [1, 0], [0, -1], [0, 1], [-1, -1], [1, -1], [-1, 1], [1, 1]]
links = [ti.Vector(v) for v in links]
@ti.kernel
def step():
#根据重力处理速度
for i in ti.grouped(x):
v[i].y -= gravity * dt
#受到的弹力
for i in ti.grouped(x):
force = ti.Vector([0.0,0.0,0.0])
for d in ti.static(links):
j = min(max(i + d, 0), [N-1,N-1])
relative_pos = x[j] - x[i]
current_length = relative_pos.norm()
original_length = cell_size * float(i-j).norm()
if original_length != 0:
force += stiffness * relative_pos.normalized() * (current_length - original_length) / original_length
v[i] += force * dt
#弹力衰减(模拟摩擦力等因素),判断球碰撞,根据速度更新坐标
for i in ti.grouped(x):
v[i] *= ti.exp(-damping * dt)
#下一帧碰撞速度暂停
x_norm = (x[i] + dt * v[i]-ball_center[0]).norm()
if x_norm < ball_radius+0.01:
#v[i] = ti.Vector([0.0, 0.0, 0.0])
v[i] =( x[i] + dt * v[i]-ball_center[0])/x_norm*(ball_radius+0.01 - x_norm)/dt
#if (x[i]-ball_center[0]).norm() <= ball_radius:
# v[i] = ti.Vector([0.0, 0.0, 0.0])
if i[0]==0:
v[i]=[0,0,0]
x[i] += dt * v[i]
@ti.kernel
def set_vertices():
for i, j in ti.ndrange(N, N):
vertices[i * N + j] = x[i, j]
init_scene()
set_indices()
#window = ti.ui.Window("Cloth", (800, 800), vsync=True)
#canvas = window.get_canvas()
#scene = ti.ui.Scene()
#camera = ti.ui.make_camera()
index = 0
while True:
#time_a = time.time()
for i in range(10):
step()
#print(time.time()-time_a)
set_vertices()
print(index)
#camera.position(0.5, -0.5, 2)
#camera.lookat(0.5, -0.5, 0)
#camera.position(0, 2, 2)
#camera.lookat(0, 0, 0)
#scene.set_camera(camera)
#
#scene.point_light(pos=(0.5, 1, 2), color=(1, 1, 1))
#scene.mesh(vertices, indices=indices, color=(0.5, 0.5, 0.5), two_sided = True)
#scene.particles(ball_center, radius=ball_radius, color=(0.5, 0, 0))
#canvas.scene(scene)
#window.show()
index+=1
if index>300:
ball_center[0].x+=0.001
#print(index)
我把 while True
改成了一个 for
循环。自己的机器上跑了一下,可以正常到处 C 代码的。
你要不更新一下Taichi版本试试?
pip install -i https://test.pypi.org/simple/ taichi-nightly