int main(){ srand(time(0)); typedef SCHEME<P> pkc; uint8 * pk; pk = new uint8 [pkc::sizeof_pub_key()]; uint8 * sk; sk = new uint8 [pkc::sizeof_pri_key()]; printf("pk: %d , sk: %d\n" , (int)pkc::sizeof_pub_key() , (int)pkc::sizeof_pri_key()); pkc::gen_key( pk , sk ); VEC<31, L_MSG> vec0;//, vec2;//, vec4; VEC<31, L_SEC> vec1, vec3;//, vec5; rand_vec( &vec1 ); //zero_vec( &vec1 ); vec1.dump( stdout ); pkc::pri_map( &vec0 , sk , &vec1 ); //zero_vec( & vec0 ); vec0.dump( stdout ); pkc::pub_map( &vec3 , pk , &vec0 ); vec3.dump( stdout ); return 0; }
void get_light(t_tmp *tmp, t_obj obc, t_cod cor, t_sph *obj_a) { t_sph *tm; t_sph *obj_hit; double old_lx[3]; double old_n[3]; double r; double d; tm = obj_a; obj_hit = obc.nt; init_light(&cor, old_lx, old_n); while (obj_a) { if (obj_a->bri > 0) { set_r_d(&r, &d, &obc, obj_a); set_ray(&cor, obj_a, &obc); rand_vec(&(cor.l_x), (double)AR(atan(r / d))); calc_pt(tmp, &obc, &cor, tm); obc.bounce -= 80; do_lum(&obc, &cor, obj_hit, (2 * M_PI * (1 - (d / (sqrt(pow(d, 2) + pow(r, 2))))))); } obj_a = obj_a->nt; } de_init_light(&cor, old_lx, old_n); }
int main(int argc, char* argv[]) { int dim, nthreads; double **a, *b; double start_time; double total_time; if (argc != 2) { fprintf(stderr, "Usage: ./matmul dim\n"); exit(1); } dim = atoi(argv[1]); a = rand_mat(dim, dim); b = rand_vec(dim); omp_set_dynamic(0); printf("dim,nthreads,time\n"); for (nthreads = 1; nthreads <= MAX_THREADS; nthreads++) { start_time = omp_get_wtime(); #pragma omp parallel num_threads(nthreads) { mat_mul_vec(a, b, dim, dim); } total_time = omp_get_wtime() - start_time; printf("%d,%d,%f\n", dim, nthreads, total_time); } return 0; }
// helper to set up particle attributes static particle* make_particle(generator_data *data, vector pos, double angle) { double angle1 = angle - data->spawn_arc / 2; double angle2 = angle + data->spawn_arc / 2; particle *p = malloc(sizeof(particle)); p->position = pos; p->velocity = rand_vec(angle1, angle2, data->spawn_velocity, data->max_velocity); p->time_alive = 0; p->time_to_live = randd(data->min_duration, data->max_duration); p->radius = data->start_radius; p->color = data->start_color; p->data = data; return p; }
void Camera::updateShake(const float dt) { if (shakeDuration > 0.0f) { shakeDuration -= dt; float dt_step = 0.03f; for (float c_dt = 0.0f ; c_dt < dt ; c_dt += dt_step) { float rdt = Math::Min(dt_step, dt - c_dt); Vector3D rand_vec( float((TA3D_RAND() % 2001) - 1000) * 0.001f * shakeMagnitude, float((TA3D_RAND() % 2001) - 1000) * 0.001f * shakeMagnitude, float((TA3D_RAND() % 2001) - 1000) * 0.001f * shakeMagnitude ); shakeVector += -rdt * 10.0f * shakeVector; shakeVector += rand_vec; if (shakeVector.x < -20.0f) shakeVector.x = -20.0f; else if(shakeVector.x > 20.0f) shakeVector.x = 20.0f; if (shakeVector.y < -20.0f ) shakeVector.y = -20.0f; else if(shakeVector.y > 20.0f) shakeVector.y = 20.0f; if (shakeVector.z < -20.0f) shakeVector.z = -20.0f; else if(shakeVector.z > 20.0f) shakeVector.z = 20.0f; } } else { float dt_step = 0.03f; for (float c_dt = 0.0f; c_dt < dt; c_dt += dt_step) { float rdt = Math::Min(dt_step, dt - c_dt); shakeVector += -rdt * 10.0f * shakeVector; if( shakeVector.x < -20.0f ) shakeVector.x = -20.0f; else if( shakeVector.x > 20.0f) shakeVector.x = 20.0f; if (shakeVector.y < -20.0f) shakeVector.y = -20.0f; else if( shakeVector.y > 20.0f) shakeVector.y = 20.0f; if (shakeVector.z < -20.0f) shakeVector.z = -20.0f; else if( shakeVector.z > 20.0f) shakeVector.z = 20.0f; } } }
Vec3f RayTracer::reflections(const Ray &ray, const Hit &hit, int bounce_count, double roughness) const { if (bounce_count <= 0) return Vec3f(0, 0, 0); const Vec3f point = ray.pointAtParameter(hit.getT()); /* Get mirror direction */ const Vec3f orig_dir = ray.getDirection(); Vec3f norm = hit.getNormal(); norm.Normalize(); const Vec3f new_dir = orig_dir - 2 * orig_dir.Dot3(norm) * norm; const Ray new_ray(point, new_dir); Vec3f rand_vec(0, 0, 0); double answerx = 0, answery = 0, answerz = 0; /* sphere projection, what if the center of the sphere misses the * object? */ { for (int i = 0; i <= args->num_glossy_samples; ++i) { /* Getting gloss ray */ Ray start_ray(new_ray.getOrigin(), new_ray.getDirection() + rand_vec); const Vec3f answer(reflection(start_ray, bounce_count - 1)); answerx += answer.x(); answery += answer.y(); answerz += answer.z(); rand_vec = Vec3f(roughness * (static_cast<double>(rand()) / RAND_MAX), roughness * (static_cast<double>(rand()) / RAND_MAX), roughness * (static_cast<double>(rand()) / RAND_MAX)); } } Vec3f answer = Vec3f(answerx, answery, answerz); answer *= static_cast<double>(1)/(args->num_glossy_samples + 1); return answer; }
void range_sort_test() { std::vector<int> v; typedef std::vector<int>::iterator itor; // iterator checks rand_vec(v); tbb::parallel_sort(v.begin(), v.end()); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a <= *(a+1), "v not sorted"); v.clear(); rand_vec(v); tbb::parallel_sort(v.begin(), v.end(), std::greater<int>()); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a >= *(a+1), "v not sorted"); v.clear(); // range checks rand_vec(v); tbb::parallel_sort(v); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a <= *(a+1), "v not sorted"); v.clear(); rand_vec(v); tbb::parallel_sort(v, std::greater<int>()); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a >= *(a+1), "v not sorted"); v.clear(); // const range checks rand_vec(v); tbb::parallel_sort(tbb::blocked_range<std::vector<int>::iterator>(v.begin(), v.end())); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a <= *(a+1), "v not sorted"); v.clear(); rand_vec(v); tbb::parallel_sort(tbb::blocked_range<std::vector<int>::iterator>(v.begin(), v.end()), std::greater<int>()); for(itor a=v.begin(); a<v.end()-1; ++a) ASSERT(*a >= *(a+1), "v not sorted"); v.clear(); // array tests int arr[elements]; for(int i=0; i<elements; ++i) arr[i] = rand()%(elements*10); tbb::parallel_sort(arr); for(int i=0; i<elements-1; ++i) ASSERT(arr[i] <= arr[i+1], "arr not sorted"); }
int main(){ // new random seed each time, for velocities and placement seed(); const flt Vs = Natoms * pow(sigma, NDIM) * M_PI_2 / NDIM; const flt L = pow(Vs / phi, OVERNDIM); cout << "Using L = " << L << "\n"; // Create the bounding box (sides of length L), and a "vector" of Natoms atoms boost::shared_ptr<OriginBox> obox(new OriginBox(L)); boost::shared_ptr<AtomVec> atomptr(new AtomVec(Natoms, 1.0)); AtomVec & atoms = *atomptr; // LJ Interaction // We'll cut it off at 2.5σ, and have a NeighborList out to 1.4 times that boost::shared_ptr<NListed<EpsSigCutAtom, LennardJonesCutPair> > LJ(new NListed<EpsSigCutAtom, LennardJonesCutPair>(obox, atomptr, 0.4*(sigcut*sigma))); boost::shared_ptr<NeighborList> nl = LJ->neighbor_list(); // ^ this is the Interaction // Note that NListed is a class template; its an Interaction that // takes various structs as template parameters, and turns them into // a neighbor Interaction // See Interaction.hpp for a whole bunch of them // Also note that the NeighborList is not the same as the // "neighborlisted" Interaction; multiple interactions can use the // same NeighborList // Now we run through all the atoms, set their positions / velocities, // and add them to the LJ Interaction (i.e., to the neighbor list) for (uint i=0; i < atoms.size(); i++){ if((i+1) % 50 == 0) cout << (Natoms - (i+1)) / 50 << ", "; cout.flush(); // we track energy to see if things are overlapping flt E0 = LJ->energy(*obox); atoms[i].x = obox->rand_loc(); // random location in the box atoms[i].v = rand_vec(); // from a Gaussian atoms[i].f = Vec::Zero(); atoms[i].a = Vec::Zero(); // Add it to the Lennard-Jones potential LJ->add(EpsSigCutAtom(atoms.get_id(i), epsilon, sigma, sigcut)); // force an update the NeighborList, so we can get an accurate energy nl->update_list(true); // If we're overlapping too much, try a new location while(LJ->energy(*obox) > E0 + epsilon/2.0){ atoms[i].x = obox->rand_loc(); // random location in the box nl->update_list(true); } } cout << "Starting. Neighborlist contains " << nl->numpairs() << " / " << (atoms.size()*(atoms.size()-1))/2 << " pairs.\n"; //Now we make our "Collection" CollectionVerlet collec = CollectionVerlet(boost::static_pointer_cast<Box>(obox), atomptr, dt); // This is very important! Otherwise the NeighborList won't update! collec.add_tracker(nl); // And add the Interaction collec.add_interaction(LJ); // subtract off center of mass velocity, and set a total energy collec.reset_com_velocity(); collec.scale_velocities_to_energy(Natoms / 4.0); // We'll put the energies to stdout and the coordinates to a new file. // VMD is good for 3D visualization purposes, and it can read .xyz files // see 'writefile' function ofstream outfile; outfile.open("LJatoms.xyz", ios::out); writefile(outfile, atoms, *obox); //Print out total energy, kinetic_energy energy, and potential energy cout << "E: " << collec.energy() << " K: " << collec.kinetic_energy() << " U: " << LJ->energy(*obox) << "\n"; // Run the simulation! And every _ steps, write a frame to the .xyz // file and print out the energies again for(uint i=0; i<500; i++){ for(uint j=0; j<1000; j++){ collec.timestep(); } writefile(outfile, atoms, *obox); cout << (500-i) << " E: " << collec.energy() << " K: " << collec.kinetic_energy() << " U: " << LJ->energy(*obox) << "\n"; } // Unnecessary extra: // Write a "tcl" file with the box boundaries // the "tcl" file is made specifically for VMD ofstream pbcfile; pbcfile.open("LJatoms-pbc.tcl", ios::out); pbcfile << "set cell [pbc set {"; for(uint j=0; j<NDIM; j++){ pbcfile << obox->box_shape()[j] << " "; } pbcfile << "} -all];\n"; pbcfile << "pbc box -toggle -center origin -color red;\n"; // Now you should be able to run "vmd -e LJatoms-pbc.tcl LJatoms.xyz" // and it will show you the movie and also the bounding box // if you have .vmdrc in that same directory, you should also be able // to toggle the box with the "b" button cout << "Done. Neighborlist contains " << nl->numpairs() << " / " << (atoms.size()*(atoms.size()-1))/2 << " pairs.\n"; };