tpass by reference, not value - slidergrid - grid of elastic sliders on a frict… | |
git clone git://src.adamsgaard.dk/slidergrid | |
Log | |
Files | |
Refs | |
README | |
LICENSE | |
--- | |
commit 5956528c9e7451c2d932ce45de64d94da384150b | |
parent a86f1907864ebdac8a032659a16a356964815bd6 | |
Author: Anders Damsgaard <[email protected]> | |
Date: Tue, 22 Mar 2016 12:07:58 -0700 | |
pass by reference, not value | |
Diffstat: | |
M slidergrid/main.c | 4 ++-- | |
M slidergrid/slider.c | 105 +++++++++++++++--------------… | |
M slidergrid/slider.h | 4 ++-- | |
3 files changed, 55 insertions(+), 58 deletions(-) | |
--- | |
diff --git a/slidergrid/main.c b/slidergrid/main.c | |
t@@ -135,7 +135,7 @@ int main(int argc, char** argv) | |
// resolve slider-to-slider interaction | |
slider_neighbor_interaction( | |
- sim.sliders[i], | |
+ &sim.sliders[i], | |
sim.sliders, | |
sim.N, | |
sim.iteration); | |
t@@ -157,7 +157,7 @@ int main(int argc, char** argv) | |
#endif | |
// update slider kinematics | |
- update_kinematics(sim.sliders[i], sim.dt, sim.iteration); | |
+ update_kinematics(&sim.sliders[i], sim.dt, sim.iteration); | |
} | |
if (time_since_file >= sim.file_interval) { | |
diff --git a/slidergrid/slider.c b/slidergrid/slider.c | |
t@@ -35,63 +35,63 @@ void initialize_slider_values(slider* s) | |
/* Explicit temporal integration scheme based on three-term Taylor expansion. | |
* Truncation error O(dt^4) for positions, O(dt^3) for velocities. Accelerati… | |
* change is approximated by backwards differences. */ | |
-void update_kinematics(slider s, Float dt, long int iteration) | |
+void update_kinematics(slider* s, Float dt, long int iteration) | |
{ | |
- s.acc = divide_float3_scalar(s.force, s.mass); | |
- s.angacc = divide_float3_scalar(s.torque, s.moment_of_inertia); | |
+ s->acc = divide_float3_scalar(s->force, s->mass); | |
+ s->angacc = divide_float3_scalar(s->torque, s->moment_of_inertia); | |
Float3 acc0, angacc0; | |
if (iteration == 0) { | |
acc0 = zeroes_float3(); | |
angacc0 = zeroes_float3(); | |
} else { | |
- acc0 = s.acc; | |
- angacc0 = s.angacc; | |
+ acc0 = s->acc; | |
+ angacc0 = s->angacc; | |
} | |
const Float3 dacc_dt = make_float3( | |
- (s.acc.x - acc0.x)/dt, | |
- (s.acc.y - acc0.y)/dt, | |
- (s.acc.z - acc0.z)/dt); | |
+ (s->acc.x - acc0.x)/dt, | |
+ (s->acc.y - acc0.y)/dt, | |
+ (s->acc.z - acc0.z)/dt); | |
const Float3 dangacc_dt = make_float3( | |
- (s.angacc.x - angacc0.x)/dt, | |
- (s.angacc.y - angacc0.y)/dt, | |
- (s.angacc.z - angacc0.z)/dt); | |
+ (s->angacc.x - angacc0.x)/dt, | |
+ (s->angacc.y - angacc0.y)/dt, | |
+ (s->angacc.z - angacc0.z)/dt); | |
const Float3 dpos_dt = make_float3( | |
- s.vel.x*dt + 0.5*s.acc.x*dt*dt + 1./6.*dacc_dt.x*dt*dt*dt, | |
- s.vel.y*dt + 0.5*s.acc.y*dt*dt + 1./6.*dacc_dt.y*dt*dt*dt, | |
- s.vel.z*dt + 0.5*s.acc.z*dt*dt + 1./6.*dacc_dt.z*dt*dt*dt); | |
+ s->vel.x*dt + 0.5*s->acc.x*dt*dt + 1./6.*dacc_dt.x*dt*dt*dt, | |
+ s->vel.y*dt + 0.5*s->acc.y*dt*dt + 1./6.*dacc_dt.y*dt*dt*dt, | |
+ s->vel.z*dt + 0.5*s->acc.z*dt*dt + 1./6.*dacc_dt.z*dt*dt*dt); | |
const Float3 dangpos_dt = make_float3( | |
- s.angvel.x*dt + 0.5*s.angacc.x*dt*dt + 1./6.*dangacc_dt.x*dt*dt*dt, | |
- s.angvel.y*dt + 0.5*s.angacc.y*dt*dt + 1./6.*dangacc_dt.y*dt*dt*dt, | |
- s.angvel.z*dt + 0.5*s.angacc.z*dt*dt + 1./6.*dangacc_dt.z*dt*dt*dt… | |
+ s->angvel.x*dt + 0.5*s->angacc.x*dt*dt + 1./6.*dangacc_dt.x*dt*dt*… | |
+ s->angvel.y*dt + 0.5*s->angacc.y*dt*dt + 1./6.*dangacc_dt.y*dt*dt*… | |
+ s->angvel.z*dt + 0.5*s->angacc.z*dt*dt + 1./6.*dangacc_dt.z*dt*dt*… | |
const Float3 dvel_dt = make_float3( | |
- s.acc.x*dt + 0.5*dacc_dt.x*dt*dt, | |
- s.acc.y*dt + 0.5*dacc_dt.y*dt*dt, | |
- s.acc.z*dt + 0.5*dacc_dt.z*dt*dt); | |
+ s->acc.x*dt + 0.5*dacc_dt.x*dt*dt, | |
+ s->acc.y*dt + 0.5*dacc_dt.y*dt*dt, | |
+ s->acc.z*dt + 0.5*dacc_dt.z*dt*dt); | |
const Float3 dangvel_dt = make_float3( | |
- s.angacc.x*dt + 0.5*dangacc_dt.x*dt*dt, | |
- s.angacc.y*dt + 0.5*dangacc_dt.y*dt*dt, | |
- s.angacc.z*dt + 0.5*dangacc_dt.z*dt*dt); | |
- | |
- s.pos = add_float3(s.pos, dpos_dt); | |
- s.angpos = add_float3(s.angpos, dangpos_dt); | |
- s.vel = add_float3(s.vel, dvel_dt); | |
- s.angvel = add_float3(s.angvel, dangvel_dt); | |
+ s->angacc.x*dt + 0.5*dangacc_dt.x*dt*dt, | |
+ s->angacc.y*dt + 0.5*dangacc_dt.y*dt*dt, | |
+ s->angacc.z*dt + 0.5*dangacc_dt.z*dt*dt); | |
+ | |
+ s->pos = add_float3(s->pos, dpos_dt); | |
+ s->angpos = add_float3(s->angpos, dangpos_dt); | |
+ s->vel = add_float3(s->vel, dvel_dt); | |
+ s->angvel = add_float3(s->angvel, dangvel_dt); | |
} | |
// Find the relative displacement and velocity between two sliders | |
-void slider_displacement(slider s1, const slider s2, | |
+void slider_displacement(slider* s1, const slider s2, | |
const int idx_neighbor, const int iteration) | |
{ | |
// vector pointing from neighbor (s2) position to this slider position (s1) | |
- const Float3 dist = subtract_float3(s1.pos, s2.pos); | |
+ const Float3 dist = subtract_float3(s1->pos, s2.pos); | |
// length of inter-slider vector | |
const Float dist_norm = norm_float3(dist); | |
t@@ -100,13 +100,13 @@ void slider_displacement(slider s1, const slider s2, | |
const Float3 n = divide_float3_scalar(dist, dist_norm); | |
// relative contact interface velocity w/o rolling | |
- const Float3 vel_linear = subtract_float3(s1.vel, s2.vel); | |
+ const Float3 vel_linear = subtract_float3(s1->vel, s2.vel); | |
// relative contact interface velocity with rolling | |
/*const Float3 vel = add_float3(vel_linear, | |
add_float3( | |
multiply_scalar_float3(dist_norm/2., | |
- cross_float3(dist, s1.angvel)), | |
+ cross_float3(dist, s1->angvel)), | |
multiply_scalar_float3(dist_norm/2., | |
cross_float3(dist, s2.angvel))));*/ | |
t@@ -123,66 +123,66 @@ void slider_displacement(slider s1, const slider s2, | |
if (iteration == 0) | |
dist0 = zeroes_float3(); | |
else | |
- dist0 = s1.neighbor_distance[idx_neighbor]; | |
+ dist0 = s1->neighbor_distance[idx_neighbor]; | |
// increment in inter-slider distance | |
const Float3 ddist = subtract_float3(dist, dist0); | |
// save current inter-slider distance | |
- s1.neighbor_distance[idx_neighbor] = dist; | |
+ s1->neighbor_distance[idx_neighbor] = dist; | |
// total relative displacement in inter-slider distance | |
if (iteration == 0) | |
- s1.neighbor_relative_distance_displacement[idx_neighbor] = ddist; | |
+ s1->neighbor_relative_distance_displacement[idx_neighbor] = ddist; | |
else | |
- s1.neighbor_relative_distance_displacement[idx_neighbor] = | |
- add_float3(s1.neighbor_relative_distance_displacement[idx_neighbor… | |
+ s1->neighbor_relative_distance_displacement[idx_neighbor] = | |
+ add_float3(s1->neighbor_relative_distance_displacement[idx_neighbo… | |
ddist); | |
// total relative displacement in inter-slider distance | |
- s1.neighbor_relative_distance_velocity[idx_neighbor] = | |
+ s1->neighbor_relative_distance_velocity[idx_neighbor] = | |
multiply_float3_scalar(n, vel_n); | |
} | |
// Resolve bond mechanics between a slider and one of its neighbors based on t… | |
// incremental linear-elastic model by Potyondy and Cundall 2004 | |
-void slider_interaction(slider s1, const slider s2, const int idx_neighbor) | |
+void slider_interaction(slider* s1, const slider s2, const int idx_neighbor) | |
{ | |
// determine the bond stiffness from the harmonic mean. | |
// differs from Potyondy & Stack 2004, eq. 6. | |
const Float bond_parallel_stiffness = | |
- 2.*s1.bond_parallel_stiffness*s2.bond_parallel_stiffness/ | |
- (s1.bond_parallel_stiffness + s2.bond_parallel_stiffness); | |
+ 2.*s1->bond_parallel_stiffness*s2.bond_parallel_stiffness/ | |
+ (s1->bond_parallel_stiffness + s2.bond_parallel_stiffness); | |
// determine the bond viscosity from the harmonic mean. | |
const Float bond_parallel_viscosity = | |
- 2.*s1.bond_parallel_viscosity*s2.bond_parallel_viscosity/ | |
- (s1.bond_parallel_viscosity + s2.bond_parallel_viscosity); | |
+ 2.*s1->bond_parallel_viscosity*s2.bond_parallel_viscosity/ | |
+ (s1->bond_parallel_viscosity + s2.bond_parallel_viscosity); | |
// bond-parallel elasticity | |
const Float3 f_n_elastic = | |
multiply_scalar_float3(bond_parallel_stiffness, | |
- s1.neighbor_relative_distance_displacement[idx_neighbor]); | |
+ s1->neighbor_relative_distance_displacement[idx_neighbor]); | |
// bond-parallel viscosity | |
const Float3 f_n_viscous = | |
multiply_scalar_float3(bond_parallel_viscosity, | |
- s1.neighbor_relative_distance_displacement[idx_neighbor]); | |
+ s1->neighbor_relative_distance_displacement[idx_neighbor]); | |
// bond-parallel force, counteracts tension and compression | |
const Float3 f_n = multiply_float3(f_n_elastic, f_n_viscous); | |
// add bond-parallel force to sum of forces on slider | |
- s1.force = add_float3(s1.force, f_n); | |
+ s1->force = add_float3(s1->force, f_n); | |
} | |
// Resolve interaction between a slider and all of its neighbors | |
void slider_neighbor_interaction( | |
- slider s, | |
+ slider* s, | |
const slider* sliders, | |
const int N, | |
const int iteration) | |
t@@ -191,21 +191,18 @@ void slider_neighbor_interaction( | |
for (idx_neighbor=0; idx_neighbor<MAX_NEIGHBORS; idx_neighbor++) { | |
// reset sum of forces on slider | |
- s.force = make_float3(0., 0., 0.); | |
+ s->force = make_float3(0., 0., 0.); | |
- if (s.neighbors[idx_neighbor] != -1) { | |
+ if (s->neighbors[idx_neighbor] != -1) { | |
slider_displacement( | |
- s, sliders[s.neighbors[idx_neighbor]], | |
+ s, sliders[s->neighbors[idx_neighbor]], | |
idx_neighbor, iteration); | |
slider_interaction( | |
- s, sliders[s.neighbors[idx_neighbor]], idx_neighbor); | |
+ s, sliders[s->neighbors[idx_neighbor]], idx_neighbor); | |
} | |
} | |
} | |
- | |
- | |
- | |
diff --git a/slidergrid/slider.h b/slidergrid/slider.h | |
t@@ -50,10 +50,10 @@ void print_slider_position(slider s); | |
void initialize_slider_values(slider* s); | |
//void check_slider_values(slider s); | |
-void update_kinematics(slider s, Float dt, long int iteration); | |
+void update_kinematics(slider* s, Float dt, long int iteration); | |
void slider_neighbor_interaction( | |
- slider s, | |
+ slider* s, | |
const slider* sliders, | |
const int N, | |
const int iteration); |