@@ -18,9 +18,9 @@ struct MassSpringSimulator<T, dim>::Impl
1818 InertialEnergy<T, dim> inertialenergy;
1919 MassSpringEnergy<T, dim> massspringenergy;
2020 Impl (T rho, T side_len, T initial_stretch, T K, T h_, T tol_, int n_seg);
21- void update_x (DeviceBuffer<T> &new_x);
22- void update_x_tilde (DeviceBuffer<T> &new_x_tilde);
23- void update_v (DeviceBuffer<T> &new_v);
21+ void update_x (const DeviceBuffer<T> &new_x);
22+ void update_x_tilde (const DeviceBuffer<T> &new_x_tilde);
23+ void update_v (const DeviceBuffer<T> &new_v);
2424 T IP_val ();
2525 void step_forward ();
2626 void draw ();
@@ -110,23 +110,21 @@ void MassSpringSimulator<T, dim>::Impl::step_forward()
110110 T E_last = IP_val ();
111111 DeviceBuffer<T> p = search_direction ();
112112 T residual = max_vector (p) / h;
113- // printf( "Initial residual %f\n", residual) ;
113+ std::cout << " Initial residual " << residual << " \n " ;
114114 while (residual > tol)
115115 {
116- // std::cout << "Iteration " << iter << ":\n";
117- // std::cout << "residual = " << residual << "\n";
118-
119116 // Line search
120117 T alpha = 1 ;
121118 DeviceBuffer<T> x0 = x;
122- update_x (add_vector<T>(x , p, 1.0 , alpha));
119+ update_x (add_vector<T>(x0 , p, 1.0 , alpha));
123120 while (IP_val () > E_last)
124121 {
125122 alpha /= 2 ;
126123 update_x (add_vector<T>(x0, p, 1.0 , alpha));
127124 }
128125 // std::cout << "step size = " << alpha << "\n";
129126 E_last = IP_val ();
127+ // std::cout << "Iteration " << iter << " residual " << residual << "E_last" << E_last << "\n";
130128 p = search_direction ();
131129 residual = max_vector (p) / h;
132130 iter += 1 ;
@@ -144,20 +142,20 @@ T MassSpringSimulator<T, dim>::Impl::screen_projection_y(T point)
144142 return resolution - (offset + scale * point);
145143}
146144template <typename T, int dim>
147- void MassSpringSimulator<T, dim>::Impl::update_x(DeviceBuffer<T> &new_x)
145+ void MassSpringSimulator<T, dim>::Impl::update_x(const DeviceBuffer<T> &new_x)
148146{
149147 inertialenergy.update_x (new_x);
150148 massspringenergy.update_x (new_x);
151149 new_x.copy_to (x);
152150}
153151template <typename T, int dim>
154- void MassSpringSimulator<T, dim>::Impl::update_x_tilde(DeviceBuffer<T> &new_x_tilde)
152+ void MassSpringSimulator<T, dim>::Impl::update_x_tilde(const DeviceBuffer<T> &new_x_tilde)
155153{
156154 inertialenergy.update_x_tilde (new_x_tilde);
157155 new_x_tilde.copy_to (x_tilde);
158156}
159157template <typename T, int dim>
160- void MassSpringSimulator<T, dim>::Impl::update_v(DeviceBuffer<T> &new_v)
158+ void MassSpringSimulator<T, dim>::Impl::update_v(const DeviceBuffer<T> &new_v)
161159{
162160 new_v.copy_to (v);
163161}
@@ -197,7 +195,6 @@ T MassSpringSimulator<T, dim>::Impl::IP_val()
197195template <typename T, int dim>
198196DeviceBuffer<T> MassSpringSimulator<T, dim>::Impl::IP_grad()
199197{
200-
201198 return add_vector<T>(inertialenergy.grad (), massspringenergy.grad (), 1.0 , h * h);
202199}
203200
@@ -206,14 +203,14 @@ DeviceTripletMatrix<T, 1> MassSpringSimulator<T, dim>::Impl::IP_hess()
206203{
207204 DeviceTripletMatrix<T, 1 > inertial_hess = inertialenergy.hess ();
208205 DeviceTripletMatrix<T, 1 > massspring_hess = massspringenergy.hess ();
209- DeviceTripletMatrix<T, 1 > hess;
210- hess = add_triplet<T>(inertial_hess, massspring_hess, 1.0 , h * h);
211- return inertial_hess;
206+ DeviceTripletMatrix<T, 1 > hess = add_triplet<T>(inertial_hess, massspring_hess, 1.0 , h * h);
207+ return hess;
212208}
213209template <typename T, int dim>
214210DeviceBuffer<T> MassSpringSimulator<T, dim>::Impl::search_direction()
215211{
216212 DeviceBuffer<T> dir;
213+ dir.resize (x.size ());
217214 search_dir (IP_grad (), IP_hess (), dir);
218215 return dir;
219216}
0 commit comments