bool Accelerometer::update(float dt) { bool has_been_flipped = false; if (calibrating) { calibrate(); } else { if (accel.available()) accel.read(); if (compute_acceleration() && has_moved()) { has_been_flipped = is_flipped(); String log = (has_been_flipped ? "FLIPPED" : "MOVED"); log += " (" + String(acc.x) + ", " + String(acc.y) + ", " + String(acc.z) + ")"; if (has_been_flipped) { Particle.publish("flip", log); } } } return has_been_flipped; }
//main simulation loop void run(const FLOAT t_max, const FLOAT eps, const FLOAT ErrTolIntAccuracy, struct Particle *particles_host, icl_buffer* particles_device, const UINT nParticles, icl_device* dev, struct Tree *tree, struct Particle *ref) { UINT k = 0; UINT treeHeight = 0; //FLOAT dt=1.5e-6; //FLOAT dt=1.220703125e-5; FLOAT dt=3.05176e-06; FLOAT current_time = 0.0; //time before current full timestep (drift), kicks are at current_time-+dt/2.0 FLOAT timeBetSnapshot = 1e-3; FLOAT timeLastSnapshot = 0.0; /* tree->nodelist[0].center_of_mass.x = 1; tree->nodelist[0].center_of_mass.y = 2; tree->nodelist[0].center_of_mass.z = 3; tree->nodelist[0].center_geometric.x = 1.3; tree->nodelist[0].center_geometric.y = 2.2; tree->nodelist[0].center_geometric.z = 3.1; tree->nodelist[0].mass = 77.0; tree->nodelist[0].l = 42.7; tree->nodelist[0].bounding_box.box[0].x = -3.0; tree->nodelist[0].bounding_box.box[0].y = -3.2; tree->nodelist[0].bounding_box.box[0].z = -3.3; tree->nodelist[0].bounding_box.box[1].x = 3.0; tree->nodelist[0].bounding_box.box[1].y = 3.2; tree->nodelist[0].bounding_box.box[1].z = 3.3; tree->nodelist[0].size = 8; tree->nodelist[0].level = 7; tree->nodelist[0].address = 17; tree->nodelist[0].left_child = 1; tree->nodelist[0].right_child = 2; tree->nodelist[0].split_dim = 3; */ // create root node in nodelist tree->nodelist[0].particlesLow = 0; tree->nodelist[0].particlesHigh = nParticles; tree->nodelist[0].level = 0; //tree->nodelist[0].bounding_box = bounding_box; tree->nodelist[0].address = 0; icl_buffer* nodelist = icl_create_buffer(dev, CL_MEM_READ_WRITE, sizeof(struct Node) * (nParticles * 2 - 1)); // copy root node to ocl device icl_write_buffer(nodelist, CL_TRUE, sizeof(struct Node), &(tree->nodelist[0]), NULL, NULL); //snapshot_000 IC with computed code properties (acceleration...) // out_snapshot(particles_host, particles_device, nParticles, dev, 0.0); // TODO particles have been resorted during tree construction. Upload them in original sorting for comparison, not needed for correctness REMOVE IT! // icl_write_buffer(particles_device, CL_TRUE, sizeof(struct Particle) * nParticles, &particles_host[0], NULL, NULL); //just for the first time, kick to half timestep treeHeight = compute_acceleration(1, nodelist, particles_device, nParticles, eps, treeHeight, dev, ref); #if timing == 1 return; #endif //dt = calcTimestep(eps, ErrTolIntAccuracy, particles, nParticles); kick(dt/2.0, particles_device, nParticles, dev); // out_snapshot(particles_host, particles_device, nParticles, dev, 0.0); //TEST icl_read_buffer(particles_device, CL_TRUE, sizeof(struct Particle) * nParticles, particles_host, NULL, NULL); printf("\tid: %d pos: %g %g %g vel: %g %g %g\n", particles_host[0].id, particles_host[0].pos.x, particles_host[0].pos.y, particles_host[0].pos.z, particles_host[0].vel.x, particles_host[0].vel.y, particles_host[0].vel.z); printf("\tacc: %g %g %g\n", particles_host[0].acc.x, particles_host[0].acc.y, particles_host[0].acc.z); // while(current_time < t_max) { current_time += dt; printf("___step: %d time: %g\n", k++, current_time); // printf("\tid: %d pos: %g %g %g vel: %g %g %g\n", ) //drift to next full timestep at current_time drift(dt, particles_device, nParticles, dev); //get new accelerations treeHeight = compute_acceleration(2, nodelist, particles_device, nParticles, eps, treeHeight, dev, ref); //TODO: mode 2: implement dynamic tree update //kick particles to current_time+dt/2.0 kick(dt, particles_device, nParticles, dev); //output & energy statistic if(current_time-timeLastSnapshot > timeBetSnapshot) { out_snapshot(particles_host, particles_device, nParticles, dev, current_time); timeLastSnapshot = current_time; } //TEST icl_read_buffer(particles_device, CL_TRUE, sizeof(struct Particle) * nParticles, particles_host, NULL, NULL); printf("\tid: %d pos: %g %g %g vel: %g %g %g\n", particles_host[0].id, particles_host[0].pos.x, particles_host[0].pos.y, particles_host[0].pos.z, particles_host[0].vel.x, particles_host[0].vel.y, particles_host[0].vel.z); printf("\tacc: %g %g %g\n", particles_host[0].acc.x, particles_host[0].acc.y, particles_host[0].acc.z); } out_snapshot(particles_host, particles_device, nParticles, dev, current_time); //write a snapshot also for the final time printf("final time reached: %g\n", current_time); compute_acceleration(0, nodelist, particles_device, nParticles, eps, treeHeight, dev, ref); //clean up icl_release_buffer(nodelist); }