def main():
    tc.set_gdb_trigger()
    scene = Scene()
    robot(scene)
    scene.finalize()
    for i in range(n_actuators):
        for j in range(n_sin_waves):
            weights[(i, j)] = (np.random.randn() * 0.01)
    for i in range(scene.n_particles):
        x[(0, i)] = scene.x[i]
        F[(0, i)] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
        actuator_id[i] = scene.actuator_id[i]
        particle_type[i] = scene.particle_type[i]
    fig = plt.figure()
    plt.ion()
    ax = fig.add_subplot(111, projection='3d')
    losses = []
    for iter in range(501):
        ti.clear_all_gradients()
        l = forward()
        losses.append(l)
        loss.grad[None] = 1
        backward()
        print('i=', iter, 'loss=', l)
        learning_rate = 10
        for i in range(n_actuators):
            for j in range(n_sin_waves):
                weights[(i, j)] -= (learning_rate * weights.grad[(i, j)])
            bias[i] -= (learning_rate * bias.grad[i])
        if ((iter % 50) == 0):
            print('Dumping particles...')
            for s in range(7, steps, 2):

                def to255(x):
                    return int(max(min((x * 255), 255), 0))
                (xs, ys, zs) = ([], [], [])
                (us, vs, ws) = ([], [], [])
                cs = []
                folder = 'mpm3d/iter{:04d}/'.format(iter)
                os.makedirs(folder, exist_ok=True)
                for i in range(n_particles):
                    xs.append(x[(s, i)][0])
                    ys.append(x[(s, i)][1])
                    zs.append(x[(s, i)][2])
                    us.append(v[(s, i)][0])
                    vs.append(v[(s, i)][1])
                    ws.append(v[(s, i)][2])
                    if (particle_type[i] == 0):
                        r = 0.3
                        g = 0.3
                        b = 1.0
                    elif (actuator_id[i] != (- 1)):
                        act = (actuation[(s, actuator_id[i])] * 0.5)
                        r = (0.5 - act)
                        g = (0.5 - abs(act))
                        b = (0.5 + act)
                    else:
                        (r, g, b) = (0.4, 0.4, 0.4)
                    color = (((to255(r) * 65536) + (256 * to255(g))) + to255(b))
                    cs.append(color)
                data = np.array(((((((xs + ys) + zs) + us) + vs) + ws) + cs), dtype=np.float32)
                data.tofile(open('{}/{:04}.bin'.format(folder, s), 'wb'))
            print('Particles dumped')
def main():
    tc.set_gdb_trigger()
    scene = Scene()
    robot(scene)
    scene.finalize()
    for i in range(n_actuators):
        for j in range(n_sin_waves):
            weights[(i, j)] = (np.random.randn() * 0.01)
    for i in range(scene.n_particles):
        x[(0, i)] = scene.x[i]
        F[(0, i)] = [[1, 0, 0], [0, 1, 0], [0, 0, 1]]
        actuator_id[i] = scene.actuator_id[i]
        particle_type[i] = scene.particle_type[i]
    fig = plt.figure()
    plt.ion()
    ax = fig.add_subplot(111, projection='3d')
    losses = []
    for iter in range(501):
        ti.clear_all_gradients()
        l = forward()
        losses.append(l)
        loss.grad[None] = 1
        backward()
        print('i=', iter, 'loss=', l)
        learning_rate = 10
        for i in range(n_actuators):
            for j in range(n_sin_waves):
                weights[(i, j)] -= (learning_rate * weights.grad[(i, j)])
            bias[i] -= (learning_rate * bias.grad[i])
        if ((iter % 50) == 0):
            print('Dumping particles...')
            for s in range(7, steps, 2):

                def to255(x):
                    return int(max(min((x * 255), 255), 0))
                (xs, ys, zs) = ([], [], [])
                (us, vs, ws) = ([], [], [])
                cs = []
                folder = 'mpm3d/iter{:04d}/'.format(iter)
                os.makedirs(folder, exist_ok=True)
                for i in range(n_particles):
                    xs.append(x[(s, i)][0])
                    ys.append(x[(s, i)][1])
                    zs.append(x[(s, i)][2])
                    us.append(v[(s, i)][0])
                    vs.append(v[(s, i)][1])
                    ws.append(v[(s, i)][2])
                    if (particle_type[i] == 0):
                        r = 0.3
                        g = 0.3
                        b = 1.0
                    elif (actuator_id[i] != (- 1)):
                        act = (actuation[(s, actuator_id[i])] * 0.5)
                        r = (0.5 - act)
                        g = (0.5 - abs(act))
                        b = (0.5 + act)
                    else:
                        (r, g, b) = (0.4, 0.4, 0.4)
                    color = (((to255(r) * 65536) + (256 * to255(g))) + to255(b))
                    cs.append(color)
                data = np.array(((((((xs + ys) + zs) + us) + vs) + ws) + cs), dtype=np.float32)
                data.tofile(open('{}/{:04}.bin'.format(folder, s), 'wb'))
            print('Particles dumped')