How to use the taichi.f32 function in taichi

To help you get started, we’ve selected a few taichi examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github yuanming-hu / taichi / examples / difftaichi / volume_renderer.py View on Github external
def kernel(angle: ti.f32, view_id: ti.i32):
    for pixel in range(res * res):
      for k in range(marching_steps):
        x = pixel // res
        y = pixel - x * res

        camera_origin = ti.Vector([
            camera_origin_radius * ti.sin(angle), 0,
            camera_origin_radius * ti.cos(angle)
        ])
        dir = ti.Vector([
            fov * (ti.cast(x, ti.f32) / (res_f32 / 2.0) - res_f32 / res_f32),
            fov * (ti.cast(y, ti.f32) / (res_f32 / 2.0) - 1.0), -1.0
        ])

        length = ti.sqrt(dir[0] * dir[0] + dir[1] * dir[1] + dir[2] * dir[2])
        dir /= length

        rotated_x = dir[0] * ti.cos(angle) + dir[2] * ti.sin(angle)
        rotated_z = -dir[0] * ti.sin(angle) + dir[2] * ti.cos(angle)
        dir[0] = rotated_x
        dir[2] = rotated_z
        point = camera_origin + (k + 1) * dx * dir

        # Convert to coordinates of the density grid box
        box_x = point[0] + 0.5
        box_y = point[1] + 0.5
        box_z = point[2] + 0.5
github yuanming-hu / taichi / tests / python / test_scope.py View on Github external
def test_scope():
  # In the future the following code should throw an exception at the python front end
  # instead of crashing the compiler
  return
  ti.runtime.print_preprocessed = True
  for arch in [ti.x86_64, ti.cuda]:
    # ti.reset()
    ti.cfg.arch = arch
    x = ti.var(ti.f32)

    N = 1
    @ti.layout
    def place():
      ti.root.dense(ti.i, N).place(x)

    @ti.kernel
    def func():
      if 1 > 0:
        val = 1

      ti.print(val)

    func()
github yuanming-hu / taichi / tests / python / test_mpm88.py View on Github external
n_particles = N * N
  n_grid = 128
  dx = 1 / n_grid
  inv_dx = 1 / dx
  dt = 2.0e-4
  p_vol = (dx * 0.5)**2
  p_rho = 1
  p_mass = p_vol * p_rho
  E = 400

  x = ti.Vector(dim, dt=ti.f32, shape=n_particles)
  v = ti.Vector(dim, dt=ti.f32, shape=n_particles)
  C = ti.Matrix(dim, dim, dt=ti.f32, shape=n_particles)
  J = ti.var(dt=ti.f32, shape=n_particles)
  grid_v = ti.Vector(dim, dt=ti.f32, shape=(n_grid, n_grid))
  grid_m = ti.var(dt=ti.f32, shape=(n_grid, n_grid))

  ti.cfg.arch = ti.cuda

  @ti.kernel
  def substep():
    for p in x:
      base = (x[p] * inv_dx - 0.5).cast(int)
      fx = x[p] * inv_dx - base.cast(float)
      w = [
          0.5 * ti.sqr(1.5 - fx), 0.75 - ti.sqr(fx - 1), 0.5 * ti.sqr(fx - 0.5)
      ]
      stress = -dt * p_vol * (J[p] - 1) * 4 * inv_dx * inv_dx * E
      affine = ti.Matrix([[stress, 0], [0, stress]]) + p_mass * C[p]
      for i in ti.static(range(3)):
        for j in ti.static(range(3)):
          offset = ti.Vector([i, j])
github yuanming-hu / taichi / tests / python / test_offload.py View on Github external
def test_running_loss():
  return
  steps = 16

  total_loss = ti.var(ti.f32)
  running_loss = ti.var(ti.f32)
  additional_loss = ti.var(ti.f32)

  @ti.layout
  def place():
    ti.root.place(total_loss)
    ti.root.dense(ti.i, steps).place(running_loss)
    ti.root.place(additional_loss)
    ti.root.lazy_grad()

  @ti.kernel
  def compute_loss():
    total_loss[None] = 0.0
    for i in range(steps):
      total_loss[None].atomic_add(running_loss[i] * 2)
    total_loss[None].atomic_add(additional_loss[None] * 3)
github yuanming-hu / taichi / examples / renderer_utils.py View on Github external
def out_dir(n):
  u = ti.Vector([1.0, 0.0, 0.0])
  if ti.abs(n[1]) < 1 - 1e-3:
    u = ti.Matrix.normalized(ti.Matrix.cross(n, ti.Vector([0.0, 1.0, 0.0])))
  v = ti.Matrix.cross(n, u)
  phi = 2 * math.pi * ti.random(ti.f32)
  r = ti.random(ti.f32)
  ay = ti.sqrt(r)
  ax = ti.sqrt(1 - r)
  return ax * (ti.cos(phi) * u + ti.sin(phi) * v) + ay * n
github yuanming-hu / taichi / examples / difftaichi / rigid_body_toi.py View on Github external
import sys

import sys
import taichi as ti
import math
import numpy as np
import os
import matplotlib.pyplot as plt
import time
from matplotlib.pyplot import cm
import taichi as tc

real = ti.f32
ti.set_default_fp(real)

max_steps = 4096
vis_interval = 4
output_vis_interval = 16
steps = 204
assert steps * 2 <= max_steps

vis_resolution = 1024

scalar = lambda: ti.var(dt=real)
vec = lambda: ti.Vector(2, dt=real)

loss = scalar()

x = vec()
github yuanming-hu / taichi / examples / difftaichi / gradient_explosion.py View on Github external
import taichi as ti
import matplotlib.pyplot as plt
import math
import sys

x = ti.global_var(dt=ti.f32)
v = ti.global_var(dt=ti.f32)
a = ti.global_var(dt=ti.f32)
loss = ti.global_var(dt=ti.f32)
damping = ti.global_var(dt=ti.f32)

max_timesteps = 1024 * 1024

dt = 0.001


@ti.layout
def place():
  ti.root.dense(ti.i, max_timesteps).place(x, v)
  ti.root.place(a, damping, loss)
  ti.root.lazy_grad()
github taichi-dev / elements / engine / mpm_solver.py View on Github external
def sand_projection(self, sigma, p):
        sigma_out = ti.Matrix.zero(ti.f32, self.dim, self.dim)
        epsilon = ti.Vector.zero(ti.f32, self.dim)
        for i in ti.static(range(self.dim)):
            epsilon[i] = ti.log(max(abs(sigma[i, i]), 1e-4))
            sigma_out[i, i] = 1
        tr = epsilon.sum() + self.Jp[p]
        epsilon_hat = epsilon - tr / self.dim
        epsilon_hat_norm = epsilon_hat.norm() + 1e-20
        if tr >= 0.0:
            self.Jp[p] = tr
        else:
            self.Jp[p] = 0.0
            delta_gamma = epsilon_hat_norm + (
                self.dim * self.lambda_0 +
                2 * self.mu_0) / (2 * self.mu_0) * tr * self.alpha
            for i in ti.static(range(self.dim)):
                sigma_out[i, i] = ti.exp(epsilon[i] - max(0, delta_gamma) /
                                         epsilon_hat_norm * epsilon_hat[i])
github yuanming-hu / taichi / examples / difftaichi / smoke_taichi.py View on Github external
import taichi as ti
import os
import math
import numpy as np
import cv2
import matplotlib
import matplotlib.pyplot as plt
from imageio import imread, imwrite

real = ti.f32
ti.set_default_fp(real)

num_iterations = 240
n_grid = 128
dx = 1.0 / n_grid
num_iterations_gauss_seidel = 10
p_dims = num_iterations_gauss_seidel + 1
steps = 100
learning_rate = 400

scalar = lambda: ti.var(dt=real)
vector = lambda: ti.Vector(2, dt=real)

v = vector()
div = scalar()
p = scalar()
github yuanming-hu / taichi / examples / difftaichi / rigid_body_toi_visualize.py View on Github external
import sys
import taichi as ti
import math
import numpy as np
import os
import matplotlib.pyplot as plt
import time
from matplotlib.pyplot import cm
import taichi as tc
import sys

real = ti.f32
ti.set_default_fp(real)

max_steps = 4096
vis_interval = 1
output_vis_interval = 1
steps = 2000
assert steps * 2 <= max_steps

vis_resolution = 1024

scalar = lambda: ti.var(dt=real)
vec = lambda: ti.Vector(2, dt=real)

loss = scalar()

x = vec()