Skip to content

Commit

Permalink
Merge pull request #42 from squarefk/ampm
Browse files Browse the repository at this point in the history
Ampm
  • Loading branch information
yuanming-hu authored Apr 19, 2017
2 parents e6c6e3b + ac00340 commit 12c90ee
Show file tree
Hide file tree
Showing 25 changed files with 165 additions and 250 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@ before_script:
- export LD_LIBRARY_PATH=/usr/lib64:$LD_LIBRARY_PATH
- mkdir build
- cd build
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo sh ../build_scripts/linux.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo sh ../build_scripts/osx.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then travis_wait 20 sudo sh ../scripts/linux_sudo.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sudo sh ../scripts/osx_sudo.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sh ../scripts/linux_normal.sh ; fi
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then sh ../scripts/osx_normal.sh ; fi

script:
- python -c 'import taichi as tc'
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ file(GLOB TAICHI_SOURCE
"include/taichi/*/*/*/*.h" "include/taichi/*/*/*.h" "include/taichi/*/*.h" "include/taichi/*.h")

set(CORE_LIBRARY_NAME taichi_core)
add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_SOURCE} include/taichi/dynamics/mpm2d/scheduler.cpp)
add_library(${CORE_LIBRARY_NAME} SHARED ${TAICHI_SOURCE})

if (NOT WIN32)
target_link_libraries(${CORE_LIBRARY_NAME} pthread)
Expand Down
2 changes: 1 addition & 1 deletion include/taichi/dynamics/mpm2d/mpm.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

#include <memory>
#include <vector>
#include "scheduler.h"
#include "mpm_scheduler.h"
#include "mpm_grid.h"
#include <taichi/math/levelset_2d.h>
#include <taichi/math/dynamic_levelset_2d.h>
Expand Down
3 changes: 1 addition & 2 deletions include/taichi/dynamics/mpm2d/mpm_grid.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,14 @@
#pragma once

#include "mpm_utils.h"
#include <stb_image.h>
#include <algorithm>
#include <atomic>
#include <taichi/math/array_2d.h>
#include <taichi/math/array_1d.h>
#include <taichi/math/levelset_2d.h>
#include <taichi/math/dynamic_levelset_2d.h>
#include "mpm_particle.h"
#include "scheduler.h"
#include "mpm_scheduler.h"

TC_NAMESPACE_BEGIN

Expand Down
1 change: 1 addition & 0 deletions include/taichi/dynamics/mpm2d/mpm_particle.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include <taichi/math/array_2d.h>
#include "mpm_utils.h"
#include <taichi/common/util.h>
#include <taichi/math/qr_svd.h>
#include <taichi/math/levelset_2d.h>
#include <taichi/math/dynamic_levelset_2d.h>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@
the MIT license as written in the LICENSE file.
*******************************************************************************/

#include "scheduler.h"
#include "mpm_scheduler.h"

TC_NAMESPACE_BEGIN

template <typename T> using Array = Array2D<T>;
template<typename T> using Array = Array2D<T>;

void MPMScheduler::expand(bool expand_vel, bool expand_state) {
Array<int> new_states;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class MPMScheduler {
public:
typedef MPMParticle Particle;

template <typename T> using Array = Array2D<T>;
template<typename T> using Array = Array2D<T>;

Array<int64> max_dt_int_strength;
Array<int64> max_dt_int_cfl;
Expand Down
126 changes: 0 additions & 126 deletions include/taichi/dynamics/mpm2d/mpm_utils.cpp

This file was deleted.

10 changes: 0 additions & 10 deletions include/taichi/dynamics/mpm2d/mpm_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,15 +65,5 @@ inline Vector2 dw(const Vector2 &a) {
return Vector2(dw(a.x) * w(a.y), w(a.x) * dw(a.y));
}

inline real det(const Matrix2 &m) {
return m[0][0] * m[1][1] - m[0][1] * m[1][0];
}

void polar_decomp(const Matrix2 &A, Matrix2 &r, Matrix2 &s);

void svd(Matrix2 A, Matrix2 &u, Matrix2 &sig, Matrix2 &v);
void svd(Matrix3 A, Matrix3 &u, Matrix3 &sig, Matrix3 &v);


TC_NAMESPACE_END

4 changes: 4 additions & 0 deletions include/taichi/dynamics/simulation3d.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ class Simulation3D : public Unit {
num_threads = config.get_int("num_threads");
}

virtual void add_particles(const Config &config) {
error("no impl");
}

virtual void step(real t) {
error("no impl");
}
Expand Down
8 changes: 8 additions & 0 deletions include/taichi/math/math_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ inline double pow(const double &a, const double &b) {
return ::pow(a, b);
}

inline real det(const Matrix2 &m) {
return glm::determinant(m);
}

inline real det(const Matrix3 &m) {
return glm::determinant(m);
}

//#define rand frand


Expand Down
82 changes: 52 additions & 30 deletions include/taichi/math/qr_svd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,6 @@
TC_NAMESPACE_BEGIN

void imp_svd(Matrix3 m, Matrix3 &u, Matrix3 &s, Matrix3 &v) {
/*
Eigen::Matrix<T, 3, 3> *M;
Eigen::Matrix<T, 3, 1> *S;
Eigen::Matrix<T, 3, 3> *U;
Eigen::Matrix<T, 3, 3> *V;
*/
JIXIE::singularValueDecomposition(
*(Eigen::Matrix<float, 3, 3> *)&m,
*(Eigen::Matrix<float, 3, 3> *)&u,
Expand All @@ -30,40 +24,17 @@ void imp_svd(Matrix3 m, Matrix3 &u, Matrix3 &s, Matrix3 &v) {
memset(&s[0][0] + 1, 0, sizeof(float) * 8);
s[1][1] = s_tmp[0];
s[2][2] = s_tmp[1];
/*
printf("glm\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf(" %.4f", m[j][i]);
}
printf("\n");
}
printf("\n");
printf("eigen\n");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
printf(" %.4f", (*((Eigen::Matrix<float, 3, 3> *) &m))(i, j));
}
printf("\n");
}
*/
}

// m can not be const here, otherwise JIXIE::singularValueDecomposition will cause a error due to const_cast
void imp_svd(Matrix2 m, Matrix2 &u, Matrix2 &s, Matrix2 &v) {
/*
Eigen::Matrix<T, 3, 3> *M;
Eigen::Matrix<T, 3, 1> *S;
Eigen::Matrix<T, 3, 3> *U;
Eigen::Matrix<T, 3, 3> *V;
*/
JIXIE::singularValueDecomposition(
*(Eigen::Matrix<float, 2, 2> *)&m,
*(Eigen::Matrix<float, 2, 2> *)&u,
*(Eigen::Matrix<float, 2, 1> *)&s,
*(Eigen::Matrix<float, 2, 2> *)&v
);
float s_tmp {s[0][1]};
float s_tmp{s[0][1]};
memset(&s[0][0] + 1, 0, sizeof(float) * 3);
if (s_tmp > 0) {
s[1][1] = s_tmp;
Expand All @@ -74,4 +45,55 @@ void imp_svd(Matrix2 m, Matrix2 &u, Matrix2 &s, Matrix2 &v) {
}
}

void svd(Matrix2 m, Matrix2 &u, Matrix2 &sig, Matrix2 &v) {
// TODO: what's going on ???
if (frobenius_norm2(m - Matrix2(m[0][0])) < 1e-7f) {
u = m;
sig = v = Matrix2(1);
} else {
imp_svd(m, u, sig, v);
}
}

void svd(Matrix3 A, Matrix3 &u, Matrix3 &sig, Matrix3 &v) {
if (frobenius_norm2(A - Matrix3(A[0][0])) < 1e-5f) {
u = A;
sig = v = Matrix3(1);
} else {
imp_svd(A, u, sig, v);
}
}

void polar_decomp(Matrix2 A, Matrix2 &r, Matrix2 &s) {
Matrix2 u, sig, v;
svd(A, u, sig, v);
r = u * glm::transpose(v);
s = v * sig * glm::transpose(v);
}

void polar_decomp(Matrix3 A, Matrix3 &r, Matrix3 &s) {
Matrix3 u, sig, v;
svd(A, u, sig, v);
r = u * glm::transpose(v);
s = v * sig * glm::transpose(v);
if (!is_normal(r)) {
Matrix3 m = A;
svd(m, u, sig, v);
P(A);
P(m);
P(u);
P(sig);
P(v);
P(r);
P(s);
P(glm::transpose(v));
P(u * glm::transpose(v));
r = u * glm::transpose(v);
P(r);
printf("Matrix3 m(%.30f,%.30f,%.30f,%.30f,%.30f,%.30f,%.30f,%.30f,%.30f);\n", m[0][0], m[1][0], m[2][0],
m[0][1],
m[1][1], m[2][1], m[0][2], m[1][2], m[2][2]);
}
}

TC_NAMESPACE_END
11 changes: 10 additions & 1 deletion include/taichi/math/qr_svd.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,17 @@

TC_NAMESPACE_BEGIN

void imp_svd(Matrix3 m, Matrix3 &u, Matrix3 &s, Matrix3 &v);

void imp_svd(Matrix2 m, Matrix2 &u, Matrix2 &s, Matrix2 &v);

void imp_svd(Matrix3 m, Matrix3 &u, Matrix3 &s, Matrix3 &v);

void svd(Matrix2 m, Matrix2 &u, Matrix2 &sig, Matrix2 &v);

void svd(Matrix3 m, Matrix3 &u, Matrix3 &sig, Matrix3 &v);

void polar_decomp(Matrix2 A, Matrix2 &r, Matrix2 &s);

void polar_decomp(Matrix3 A, Matrix3 &r, Matrix3 &s);

TC_NAMESPACE_END
9 changes: 6 additions & 3 deletions python/examples/simulation/3d/mpm_snow_3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@
if __name__ == '__main__':
downsample = 3
resolution = (511 / downsample, 127 / downsample, 255 / downsample)
tex = Texture('image', filename=tc.get_asset_path('/textures/taichi_words.png')) * 8

mpm = MPM3(resolution=resolution, gravity=(0, -10, 0), delta_t=0.001, num_threads=8)

tex = Texture('image', filename=tc.get_asset_path('textures/taichi_words.png')) * 8
tex = Texture('bound', tex=tex, axis=2, bounds=(0.475, 0.525), outside_val=(0, 0, 0))
mpm = MPM3(resolution=resolution, gravity=(0, -10, 0), initial_velocity=(0, 0, 0), delta_t=0.001, num_threads=8,
density_tex=tex.id)
mpm.add_particles(density_tex=tex.id, initial_velocity=(0, 0, 0))

for i in range(1000):
mpm.step(0.05)
mpm.make_video()
Expand Down
Loading

0 comments on commit 12c90ee

Please sign in to comment.