# 一个新手的简单问题

taichi_elements-master
-----------------以下是python代码-----------
import taichi as ti
import numpy as np
import utils
import math
from engine.mpm_solver import MPMSolver

write_to_disk = False

ti.init(arch=ti.cuda) # Try to run on GPU

gui = ti.GUI(“Taichi Elements”, res=512, background_color=0x112F41)

mpm = MPMSolver(res=(128, 128))

for i in range(3):
mpm.add_cube(lower_corner=[0.2 + i * 0.1, 0.3 + i * 0.1],
cube_size=[0.1, 0.1],
material=MPMSolver.material_elastic)

for frame in range(500):
mpm.step(8e-3)
if frame < 500:
cube_size=[0.01, 0.05],
velocity=[1, 0],
material=MPMSolver.material_sand)
if 10 < frame < 100:
cube_size=[0.2, 0.01],
material=MPMSolver.material_water,
velocity=[math.sin(frame * 0.1), 0])
if 120 < frame < 200 and frame % 10 == 0:
lower_corner=[0.4 + frame * 0.001, 0.6 + frame // 40 * 0.02],
cube_size=[0.2, 0.1],
velocity=[-3, -1],
material=MPMSolver.material_snow)
colors = np.array([0x068587, 0xED553B, 0xEEEEF0, 0xFFFF00],
dtype=np.uint32)
particles = mpm.particle_info()
gui.circles(particles[‘position’],
color=colors[particles[‘material’]])
gui.show(f’{frame:06d}.png’ if write_to_disk else None)

## opengl不知为何不可用 我是win系统 ，这个例子很吃CPU 这是ti.init(arch=ti.cuda)的全部控制台输出

[Taichi] mode=release
[Taichi] version 0.7.15, llvm 10.0.0, commit cff542ce, win, python 3.8.8
[Taichi] Starting on arch=cuda
[Taichi] materializing…
Traceback (most recent call last):
File “demo_2d.py”, line 44, in
gui.show(f’{frame:06d}.png’ if write_to_disk else None)
File “C:\Users\Administrator\AppData\Local\Programs\Python\Python38\lib\site-packages\taichi\misc\gui.py”, line 403, in show
self.core.update()
RuntimeError: Window close button clicked, exiting… (use `while gui.running` to exit gracefully)