Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] Update mpm_lagrangian_force and fix Matrix constructor #1545

Merged
merged 4 commits into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions examples/mpm_lagrangian_forces.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,18 @@
mu = 1
la = 1

x = ti.Vector(dim, dt=ti.f32, shape=n_particles, needs_grad=True)
v = ti.Vector(dim, dt=ti.f32, shape=n_particles)
C = ti.Matrix(dim, dim, 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))
restT = ti.Matrix(dim, dim, dt=ti.f32, shape=n_particles, needs_grad=True)
total_energy = ti.var(ti.f32, shape=(), needs_grad=True)
vertices = ti.var(ti.i32, shape=(n_elements, 3))
x = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles, needs_grad=True)
v = ti.Vector.field(dim, dtype=ti.f32, shape=n_particles)
C = ti.Matrix.field(dim, dim, dtype=ti.f32, shape=n_particles)
grid_v = ti.Vector.field(dim, dtype=ti.f32, shape=(n_grid, n_grid))
grid_m = ti.field(dtype=ti.f32, shape=(n_grid, n_grid))
restT = ti.Matrix.field(dim,
dim,
dtype=ti.f32,
shape=n_particles,
needs_grad=True)
total_energy = ti.field(dtype=ti.f32, shape=(), needs_grad=True)
vertices = ti.field(dtype=ti.i32, shape=(n_elements, 3))


@ti.func
Expand Down
1 change: 1 addition & 0 deletions python/taichi/lang/matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ def __init__(self,
self.n = mat.n
self.m = mat.m
self.entries = mat.entries
self.grad = mat.grad

if self.n * self.m > 32:
warning(
Expand Down
16 changes: 16 additions & 0 deletions tests/python/test_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,19 @@ def test_matrix_field(n, m, dtype, shape):
assert x.dtype == dtype
assert x.n == n
assert x.m == m


@ti.host_arch_only
def test_field_needs_grad():
# Just make sure the usage doesn't crash, see https://github.com/taichi-dev/taichi/pull/1545
n = 8
m1 = ti.field(ti.f32, n, needs_grad=True)
m2 = ti.field(ti.f32, n, needs_grad=True)
gr = ti.field(ti.f32, n)

@ti.kernel
def func():
for i in range(n):
gr[i] = m1.grad[i] + m2.grad[i]

func()
16 changes: 16 additions & 0 deletions tests/python/test_matrix.py
Original file line number Diff line number Diff line change
Expand Up @@ -186,3 +186,19 @@ def func():
func()

assert np.allclose(m.to_numpy(), np.ones((5, 2, 2), np.int32) * 12)


@ti.all_archs
def test_matrix_needs_grad():
k-ye marked this conversation as resolved.
Show resolved Hide resolved
# Just make sure the usage doesn't crash, see https://github.com/taichi-dev/taichi/pull/1545
n = 8
m1 = ti.Matrix.field(2, 2, ti.f32, n, needs_grad=True)
m2 = ti.Matrix.field(2, 2, ti.f32, n, needs_grad=True)
gr = ti.Matrix.field(2, 2, ti.f32, n)

@ti.kernel
def func():
for i in range(n):
gr[i] = m1.grad[i] + m2.grad[i]

func()