Skip to content

Commit d06b5bb

Browse files
committed
5
1 parent fd53a05 commit d06b5bb

4 files changed

Lines changed: 39 additions & 12 deletions

File tree

simulators/5_mov_dirichlet/include/SpringEnergy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ template <typename T, int dim>
1010
class SpringEnergy
1111
{
1212
public:
13-
SpringEnergy(const std::vector<T> &x, const std::vector<T> &m, const std::vector<int> &DBC, const std::vector<Eigen::Matrix<T, dim, 1>> &DBC_target, T k);
13+
SpringEnergy(const std::vector<T> &x, const std::vector<T> &m, const std::vector<int> &DBC, const std::vector<T> &DBC_v,const std::vector<T> &DBC_limit, T k,T h);
1414
SpringEnergy();
1515
~SpringEnergy();
1616
SpringEnergy(SpringEnergy &&rhs);

simulators/5_mov_dirichlet/src/SpringEnergy.cu

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ struct SpringEnergy<T, dim>::Impl
1212
DeviceBuffer<T> device_x;
1313
DeviceBuffer<T> device_m;
1414
DeviceBuffer<int> device_DBC;
15-
DeviceBuffer<Eigen::Matrix<T, dim, 1>> device_DBC_target;
15+
DeviceBuffer<T> device_DBC_target,device_DBC_v,device_DBC_limit;
1616
DeviceBuffer<T> device_grad;
1717
DeviceTripletMatrix<T, 1> device_hess;
1818
T k,h;
@@ -36,14 +36,16 @@ SpringEnergy<T, dim>::SpringEnergy(const SpringEnergy<T, dim> &rhs)
3636
: pimpl_{std::make_unique<Impl>(*rhs.pimpl_)} {}
3737

3838
template <typename T, int dim>
39-
SpringEnergy<T, dim>::SpringEnergy(const std::vector<T> &x, const std::vector<T> &m, const std::vector<int> &DBC, const std::vector<T> &DBC_target, T k,T h)
39+
SpringEnergy<T, dim>::SpringEnergy(const std::vector<T> &x, const std::vector<T> &m, const std::vector<int> &DBC, const std::vector<T> &DBC_v, const std::vector<T> &DBC_limit,T k,T h)
4040
: pimpl_{std::make_unique<Impl>()}
4141
{
4242
pimpl_->N = x.size() / dim;
4343
pimpl_->device_x.copy_from(x);
4444
pimpl_->device_m.copy_from(m);
4545
pimpl_->device_DBC.copy_from(DBC);
46-
pimpl_->device_DBC_target.copy_from(DBC_target);
46+
pimpl_->device_DBC_v.copy_from(DBC_v);
47+
pimpl_->device_DBC_limit.copy_from(DBC_limit);
48+
pimpl_->device_DBC_target.resize(DBC.size() * dim);
4749
pimpl_->k = k;
4850
pimpl_->h = h;
4951
pimpl_->device_grad.resize(pimpl_->N * dim);
@@ -60,15 +62,39 @@ void SpringEnergy<T, dim>::update_x(const DeviceBuffer<T> &x)
6062
template <typename T, int dim>
6163
void SpringEnergy<T, dim>::update_DBC_target()
6264
{
63-
// for i in range(0, len(DBC)):
64-
// if (DBC_limit[i] - x_n[DBC[i]]).dot(DBC_v[i]) > 0:
65-
// DBC_target.append(x_n[DBC[i]] + h * DBC_v[i])
66-
// else:
67-
// DBC_target.append(x_n[DBC[i]])
6865
auto &device_x = pimpl_->device_x;
6966
auto &device_DBC = pimpl_->device_DBC;
7067
auto &device_DBC_target = pimpl_->device_DBC_target;
7168
auto h = pimpl_->h;
69+
auto &device_DBC_v = pimpl_->device_DBC_v;
70+
auto &device_DBC_limit = pimpl_->device_DBC_limit;
71+
int N = device_DBC.size();
72+
device_DBC_target.fill(0);
73+
74+
ParallelFor(256).apply(N, [device_x = device_x.cviewer(), device_DBC = device_DBC.cviewer(), device_DBC_target = device_DBC_target.viewer(), device_DBC_v = device_DBC_v.cviewer(), h, device_DBC_limit = device_DBC_limit.cviewer()] __device__(int i) mutable
75+
{
76+
int idx = device_DBC(i);
77+
T d=0;
78+
for (int j = 0; j < dim; ++j)
79+
{
80+
d += (device_DBC_limit(i*dim + j) - device_x(idx * dim + j)) * (device_DBC_v(i*dim + j));
81+
}
82+
if(d>0)
83+
{
84+
for (int j = 0; j < dim; ++j)
85+
{
86+
device_DBC_target(i*dim + j) = device_x(idx * dim + j) + h * device_DBC_v(i*dim + j);
87+
}
88+
}
89+
else
90+
{
91+
for (int j = 0; j < dim; ++j)
92+
{
93+
device_DBC_target(i*dim + j) = device_x(idx*dim + j);
94+
}
95+
}
96+
}).wait();
97+
7298

7399
}
74100

simulators/5_mov_dirichlet/src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@ int main()
44
{
55
float rho = 1000, k = 4e4, initial_stretch = 1, n_seg = 8, h = 0.01, side_len = 1, tol = 0.01, mu = 0.11;
66
// printf("Running mass-spring simulator with parameters: rho = %f, k = %f, initial_stretch = %f, n_seg = %d, h = %f, side_len = %f, tol = %f\n", rho, k, initial_stretch, n_seg, h, side_len, tol);
7-
FrictionSimulator<float, 2> simulator(rho, side_len, initial_stretch, k, h, tol, mu, n_seg);
7+
MovDirichletSimulator<float, 2> simulator(rho, side_len, initial_stretch, k, h, tol, mu, n_seg);
88
simulator.run();
99
}

simulators/5_mov_dirichlet/src/simulator.cu

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ MovDirichletSimulator<T, dim>::Impl::Impl(T rho, T side_len, T initial_stretch,
9494
gravityenergy = GravityEnergy<T, dim>(N, m);
9595
barrierenergy = BarrierEnergy<T, dim>(x, ground_n, ground_o, contact_area);
9696
frictionenergy = FrictionEnergy<T, dim>(v, h, ground_n);
97+
springenergy = SpringEnergy<T, dim>(x, std::vector<T>(N, m), DBC, std::vector<T>(N * dim, 0), std::vector<T>(N * dim, 0), 0, h);
9798
DeviceBuffer<T> x_device(x);
9899
update_x(x_device);
99100
device_DBC = DeviceBuffer<int>(DBC);
@@ -192,9 +193,9 @@ void MovDirichletSimulator<T, dim>::Impl::update_v(const DeviceBuffer<T> &new_v)
192193
new_v.copy_to(v);
193194
}
194195
template <typename T, int dim>
195-
void MovDirichletSimulator<T, dim>::update_DBC_target()
196+
void MovDirichletSimulator<T, dim>::Impl::update_DBC_target()
196197
{
197-
198+
springenergy.update_DBC_target();
198199
}
199200
template <typename T, int dim>
200201
void MovDirichletSimulator<T, dim>::Impl::draw()

0 commit comments

Comments
 (0)