static struct normalized_coords accelerator_filter_x230(struct motion_filter *filter, const struct normalized_coords *unaccelerated, void *data, uint64_t time) { struct pointer_accelerator *accel = (struct pointer_accelerator *) filter; double accel_factor; /* unitless factor */ struct normalized_coords accelerated; double velocity; /* units/us */ feed_trackers(accel, unaccelerated, time); velocity = calculate_velocity(accel, time); accel_factor = calculate_acceleration(accel, data, velocity, accel->last_velocity, time); accel->last_velocity = velocity; accelerated.x = accel_factor * unaccelerated->x; accelerated.y = accel_factor * unaccelerated->y; return accelerated; }
int main(int argc, char *argv[]) { struct particle **arr; int dimensions = 1; float step = 0.1; int num_particles = 2; int num_iterations = 10; int threads = 1; if(argc < 4) { fprintf(stderr,"Usage ./particle_simulator #of_particles #of_iterations threads [one_dim / two_dim](default: one_dim)\n"); //return 1; } else { num_particles = strtoll(argv[1],NULL,10); num_iterations = strtoll(argv[2],NULL,10); threads = strtoll(argv[3],NULL,10); } if(argc > 4 && strcmp(argv[4],"two_dim") == 0) dimensions = 2; arr = generate_particles(num_particles); int i, j; //printf("Iteration, ParticleID, ParticleX, ParticleY\n"); //print_particles(num_particles,arr,0); omp_set_num_threads(threads); for(i = 1; i <= num_iterations; i++) { #pragma omp parallel for for(j = 0; j < num_particles; j++) { calculate_acceleration(arr[j],num_particles,arr,dimensions); } for(j = 0; j < num_particles; j++) { update_vel_position(arr[j],step); } //print_particles(num_particles,arr,i); } free_particles(num_particles,arr); }
static inline double calculate_acceleration_factor(struct pointer_accelerator *accel, const struct normalized_coords *unaccelerated, void *data, uint64_t time) { double velocity; /* units/us */ double accel_factor; feed_trackers(accel, unaccelerated, time); velocity = calculate_velocity(accel, time); accel_factor = calculate_acceleration(accel, data, velocity, accel->last_velocity, time); accel->last_velocity = velocity; return accel_factor; }
static struct normalized_coords accelerator_filter_x230(struct motion_filter *filter, const struct device_float_coords *raw, void *data, uint64_t time) { struct pointer_accelerator *accel = (struct pointer_accelerator *) filter; double accel_factor; /* unitless factor */ struct normalized_coords accelerated; struct device_float_coords delta_normalized; struct normalized_coords unaccelerated; double velocity; /* units/us */ /* This filter is a "do not touch me" filter. So the hack here is * just to replicate the old behavior before filters switched to * device-native dpi: * 1) convert from device-native to 1000dpi normalized * 2) run all calculation on 1000dpi-normalized data * 3) apply accel factor no normalized data */ unaccelerated = normalize_for_dpi(raw, accel->dpi); delta_normalized.x = unaccelerated.x; delta_normalized.y = unaccelerated.y; feed_trackers(accel, &delta_normalized, time); velocity = calculate_velocity(accel, time); accel_factor = calculate_acceleration(accel, data, velocity, accel->last_velocity, time); accel->last_velocity = velocity; accelerated.x = accel_factor * delta_normalized.x; accelerated.y = accel_factor * delta_normalized.y; return accelerated; }
static void accelerator_filter(struct weston_motion_filter *filter, struct weston_motion_params *motion, void *data, uint32_t time) { struct pointer_accelerator *accel = (struct pointer_accelerator *) filter; double velocity; double accel_value; feed_trackers(accel, motion->dx, motion->dy, time); velocity = calculate_velocity(accel, time); accel_value = calculate_acceleration(accel, data, velocity, time); motion->dx = accel_value * motion->dx; motion->dy = accel_value * motion->dy; apply_softening(accel, motion); accel->last_dx = motion->dx; accel->last_dy = motion->dy; accel->last_velocity = velocity; }
static struct normalized_coords accelerator_filter(struct motion_filter *filter, const struct normalized_coords *unaccelerated, void *data, uint64_t time) { struct pointer_accelerator *accel = (struct pointer_accelerator *) filter; double velocity; /* units/ms */ double accel_value; /* unitless factor */ struct normalized_coords accelerated; struct normalized_coords unnormalized; double dpi_factor = accel->dpi_factor; /* For low-dpi mice, use device units, everything else uses 1000dpi normalized */ dpi_factor = min(1.0, dpi_factor); unnormalized.x = unaccelerated->x * dpi_factor; unnormalized.y = unaccelerated->y * dpi_factor; feed_trackers(accel, &unnormalized, time); velocity = calculate_velocity(accel, time); accel_value = calculate_acceleration(accel, data, velocity, accel->last_velocity, time); accelerated.x = accel_value * unnormalized.x; accelerated.y = accel_value * unnormalized.y; accel->last = unnormalized; accel->last_velocity = velocity; return accelerated; }
void init_physics() { ions[0] = proton(); ions[1] = electron(); ions[0].location = (struct vector) {0,0,0}; ions[0].velocity=(struct vector){0,0,0}; ions[1].location = (struct vector) {0,20,0}; ions[1].velocity=(struct vector){0.15,0,0}; } /* void init_physics() { #define RANDO ((rand()%100)-50) int i; for(i=0;i<N_IONS;i++){ switch(rand()%8){ case 0: ions[i] = nuclion(rand()%9); break; default: ions[i] = electron(); break; } ions[i].location = (struct vector) {RANDO,RANDO,RANDO}; ions[i].velocity=(struct vector){0,0,0}; } } */ void dump_state() { int i; printf("--- IONS ---\n"); for(i=0;i<N_IONS;i++){ printf("[%i]\n",i); printf("charge %Lf \n",ions[i].charge); printf("mass %Lf \n",ions[i].mass); v_print("location",ions[i].location); v_printe("velocity",ions[i].velocity); v_printe("acceleration",ions[i].accel); } printf("-----------\n"); } void calculate_acceleration(struct particle * p,double dt) { struct vector fv = {0,0,0}; int i; for(i=0;i<N_IONS;i++){ fv = v_add(fv,gravitation(ions[i],*p)); fv = v_add(fv,coulombs(ions[i],*p)); // fv = v_add(fv,biotsavart(ions[i],*p)); } /* because f/m=a */ fv.x/=p->mass; fv.y/=p->mass; fv.z/=p->mass; p->accel = v_scalar_mul(dt,fv); } void apply_vel(struct particle * p) { p->velocity = v_add(p->accel,p->velocity); p->location = v_add(p->location,p->velocity); } void physics_tick(double dt) { int i; for(i=0;i<N_IONS;i++) calculate_acceleration(&ions[i],dt); for(i=0;i<N_IONS;i++) apply_vel(&ions[i]); if(keys['P']) dump_state(); }