Exemple #1
0
// ----------------------------------------
// #update(delta)
VALUE Ashton_ParticleEmitter_update(VALUE self, VALUE delta)
{
    EMITTER();

    float _delta = NUM2DBL(delta);
    if(_delta < 0.0) rb_raise(rb_eArgError, "delta must be >= 0");

    if(emitter->count > 0)
    {
        Particle* particle = emitter->particles;
        Particle* last = &emitter->particles[emitter->max_particles - 1];
        for(; particle <= last; particle++)
        {
            // Ignore particles that are already dead.
            if(particle->time_to_live > 0)
            {
                update_particle(emitter, particle, _delta);
            }
        }
    }

    // Time to emit one (or more) new particles?
    emitter->time_until_emit -= _delta;
    while(emitter->time_until_emit <= 0)
    {
        rb_funcall(self, rb_intern("emit"), 0);
        emitter->time_until_emit += deviate(&emitter->interval);
    }

    // Copy all the current data onto the graphics card.
    if(emitter->count > 0) update_vbo(emitter);

    return Qnil;
}
void update_particles_layer(Layer *me, GContext* ctx) {
  (void)me;
  graphics_context_set_fill_color(ctx, GColorWhite);
  for(int i=0;i<NUM_PARTICLES;i++) {
    update_particle(i);
    draw_particle(ctx, i);
  }
}
Exemple #3
0
int main(int argc, char* argv[]) {
	particle p_list[NUM_PARTICLES];
	png_file png;
	float t;
	int i;

	int num_nodes,id;
	MPI_Status status;

	/* init MPI */
    MPI_Init(&argv,&argc);
    MPI_Comm_rank(MPI_COMM_WORLD,&id);
    MPI_Comm_size(MPI_COMM_WORLD,&num_nodes);

	/* create datatypes */


	/* init the particle list */
	for(i=0; i < NUM_PARTICLES; i++) {
		init_particle(&p_list[i],0,0,0);
		set_x_pos_fnt(&p_list[i],&position_x);
		set_y_pos_fnt(&p_list[i],&position_y);
		set_z_pos_fnt(&p_list[i],&position_z);
		p_list[i].parm_list = (float*)malloc(sizeof(float) * NUM_PARMS);
		p_list[i].parm_list[MASS] = 1.0F;  /* kg */
		p_list[i].parm_list[X_V0] = 15.0F; /* m/s */
		p_list[i].parm_list[Y_V0] = 0.0F;  /* m/s */
		p_list[i].parm_list[Z_V0] = 0.0F;  /* m/s */
	}

	/* open the png file */
	open_file(&png,"test.png",1024,1024,0,2000,-2000,0);

	/* do some work */
	for(t=0.0F; t < 20.0F; t+=0.05F) {
		for(i=0; i < NUM_PARTICLES; i++) {
			update_particle(&p_list[i],t);
			printf("%F %F %F\n",p_list[i].x,p_list[i].y,p_list[i].z);
			plot_point(&png,(int)p_list[i].x,(int)p_list[i].y,255-i,255-i,255-i);
		}
	}

	/* write out the png file */
	write_file(&png);

	/* close the png file */
	close_file(&png);

	/* destory particle list */
	for(i=0; i < NUM_PARTICLES; i++) {
		free(p_list[i].parm_list);
	}

	/* shutdown MPI */
	MPI_Finalize();

	return(0);
}
void Particle::draw_rain(){
	//Only draw the visible particle rain if we draw snow , then use glut?
	update_particle();	
	glBegin(GL_LINES);
	for(int i = 0;i<MAX_PARTICLE_COUNT;i+=1){
		
		glVertex3d(particle_array[i][0],particle_array[i][1],particle_array[i][2]);
		glVertex3d(particle_array[i][0],particle_array[i][1],particle_array[i][2]-PARTICLE_HEIGHT);
		
	}
	glEnd();
}
Exemple #5
0
// updates particle with map
void update_particle_with_map(Tparticle *p, Tmap *m) {
	update_particle(p);

	/* bouncing algo removed 
	p->y -= p->dy;
	p->x -= p->dx;
	if (is_ground(m, (int)p->x, (int)p->y)) {
		p->dy = - 0.9 * p->dy;
		//p->dx = - p->dx;
	}
	*/

}
void Particle::draw_snow(){
	//Only draw the visible particle rain if we draw snow , then use glut?
	update_particle();	
	glBegin(GL_QUADS);
	for(int i = 0;i<MAX_PARTICLE_COUNT;i+=1){
		glNormal3d(0,1,1);
		glVertex3d(particle_array[i][0],particle_array[i][1],particle_array[i][2]+SNOW_HEIGHT);
		glVertex3d(particle_array[i][0]+SNOW_HEIGHT,particle_array[i][1],particle_array[i][2]);
		glVertex3d(particle_array[i][0],particle_array[i][1],particle_array[i][2]-SNOW_HEIGHT);
		glVertex3d(particle_array[i][0]-SNOW_HEIGHT,particle_array[i][1],particle_array[i][2]);
		
	}
	glEnd();
}
Exemple #7
0
void update_particles(double time) {
  time_elapsed = time;
  list_node *pnode = particle_list->head;
  while (pnode != NULL) {
    particle *p = (particle*)(pnode->value); // particle struct in node
    if (p->time_alive > p->time_to_live) {   // has particle expired?
      pnode = list_remove(particle_list, pnode, free); // remove
    }
    else {                 // not expired - perform update
      update_particle(p);  // update particle attributes
      pnode = pnode->next; // move to next particle
    }
  }
}
static void particle_engine(double t, float dt)
{
    int i;
    float dt2;

    // Update particles (iterated several times per frame if dt is too large)
    while (dt > 0.f)
    {
        // Calculate delta time for this iteration
        dt2 = dt < MIN_DELTA_T ? dt : MIN_DELTA_T;

        for (i = 0;  i < MAX_PARTICLES;  i++)
            update_particle(&particles[i], dt2);

        min_age += dt2;

        // Should we create any new particle(s)?
        while (min_age >= BIRTH_INTERVAL)
        {
            min_age -= BIRTH_INTERVAL;

            // Find a dead particle to replace with a new one
            for (i = 0;  i < MAX_PARTICLES;  i++)
            {
                if (!particles[i].active)
                {
                    init_particle(&particles[i], t + min_age);
                    update_particle(&particles[i], min_age);
                    break;
                }
            }
        }

        dt -= dt2;
    }
}
Exemple #9
0
void ParticleEmitter::update(float passedTime)
{
  for( auto it = mParticles.begin(); it != mParticles.end(); )
  {
    if(it->timeToLive > passedTime)
    { 
      update_particle(*this, *it, passedTime, mType);
      ++it;
    } 
    else
      it = mParticles.erase(it);
  }

  emit_particles(*this, passedTime);
}
Exemple #10
0
int main(int argc, char* argv[]) {
	particle p_list[NUM_PARTICLES];
	png_file png;
	float t;
	int i;

	/* init the particle engine */
	init_particle_engine();
	set_x_pos_fnt(&p_list[i],&position_x);
	set_y_pos_fnt(&p_list[i],&position_y);
	set_z_pos_fnt(&p_list[i],&position_z);


	/* init the particle list */
	for(i=0; i < NUM_PARTICLES; i++) {
		init_particle(&p_list[i],0,0,0);
		p_list[i].parm_list[MASS] = 1.0F;  /* kg */
		p_list[i].parm_list[X_V0] = 15.0F; /* m/s */
		p_list[i].parm_list[Y_V0] = 0.0F;  /* m/s */
		p_list[i].parm_list[Z_V0] = 0.0F;  /* m/s */
	}

	/* open the png file */
	open_file(&png,"test.png",1024,1024,0,2000,-2000,0);

	/* do some work */
	for(t=0.0F; t < 20.0F; t+=0.05F) {
		for(i=0; i < NUM_PARTICLES; i++) {
			update_particle(&p_list[i],t);
			plot_point(&png,(int)p_list[i].x,(int)p_list[i].y,255,255,255);
		}
	}

	//plot_point(&png,0,0,255,255,255);

	/* write out the png file */
	write_file(&png);

	/* close the png file */
	close_file(&png);

	return(0);
}
Exemple #11
0
void Cconfig::fread(Cin_out where_to_read)
{
	where_to_read.set_file_name();
	where_to_read.check_file();

    P.clear(); C.clear();

	ifstream file;

	file.open(where_to_read.save_file[0].c_str());  // read particles
	while(1<2)
	{
		Cparticle part;
		file>>part;
		if(!file.eof()) P.push_back(part);
		else break;
	}
	file.close();
	
	file.open(where_to_read.save_file[1].c_str());  // read contact
	while(1<2)
	{
		Ccontact cont;
		file>> cont;
		if(!file.eof()) C.push_back(cont);
		else break;
	}
	file.close();


	file.open(where_to_read.save_file[2].c_str());//save parameter
	file>>t;
	file>>parameter; 
	file.close();

	file.open(where_to_read.save_file[3].c_str()); //save cell
	file>>cell; 
	file.close();
	
	
	//bluild the pointers
	for(int ip=0;ip<P.size();ip++)//cell knows which particle is a plan
	{
		if(P[ip].AM_I_BOUNDARY==-2 ) cell.plan_bottom=&P[ip];
		if(P[ip].AM_I_BOUNDARY==2 )  cell.plan_top=&P[ip];
		P[ip].id=ip;
	}

	for(int ic=0;ic<C.size();ic++)
		{ 
			C[ic].pA = &P[C[ic].A]; 
			C[ic].pB = &P[C[ic].B];
			C[ic].cell = &cell;
			C[ic].parameter = &parameter;
			P[C[ic].A].contact.push_back(&C[ic]);
			if(INIT_BOND != 0) C[ic].deltaNB = INIT_BOND;
		} 

	update_particle();
	parameter.dimensionless_number(cell,P);	
	iterate(0.0);//use here to recover all the data that haven't been saved, but which derive from saved data

}
Exemple #12
0
//TODO: Add function to reset simulation statistics (av_momentum_transfer).
int main(int argc, char *argv[]){

    Simulation* sim;
    const double output_delta = 10.0;
    double output_start_time = 0.01;

    const auto& directory = argv[3];

    if(strcmp(argv[1], "-r") == 0){
        FILE* fp = fopen(argv[2], "rb");
        fseek(fp, 0l, SEEK_END);
        size_t file_size = ftell(fp);
        fseek(fp, 0l, SEEK_SET);
        char* data = reinterpret_cast<char*>(malloc(file_size));
        fread(data, file_size, 1, fp);
        fclose(fp);

        Archive ar(data, file_size);
        sim = new Simulation();
        deserialize(ar, sim);

        free(data);

        output_start_time = sim->time() + output_delta;
    }
    else{
        Configuration config;
        xml_load_config(argv[1], config);

        //Scale box and positions for reaching target_pf
        {
            double target_pf = atof(argv[2]);
            double pf = packing_fraction(config);
            double scale = pow(pf / target_pf, 1.0 / 3.0);
            config.pbc_.setSize(scale * config.pbc_.getSize());
            for(auto& particle: config.particles_) particle.xform.pos_ = scale * particle.xform.pos_;
        }

        sim = new Simulation(config);
    }

    double pf = packing_fraction(sim->configuration());

    PeriodicCallback output(output_start_time);
    output.setNextFunction([output_delta](double time){
        return time + output_delta;
    });

    FILE* pressure_fp;
    {
        char fp_buff[64];
        sprintf(fp_buff, "%s/pressure.pf%.3f.pid%u.dat", directory, pf, getpid());
        pressure_fp = fopen(fp_buff, "w");
    }

    output.setCallback([sim, pf, pressure_fp, directory](double time) -> bool {
        static int nFiles = 0;
        printf("%f\n", time);

        //Check for overlaps
        if(sim->check_overlaps()){
            printf("Overlaps found!\n");

            //Kind of a hack >_<
            sim->~Simulation();
            new (sim) Simulation();

            char buff[64];
            sprintf(buff, "%s/archive.pf%.3f.pid%u.bin", directory, pf, getpid());
            printf("Resetting...\n");
            FILE* fp = fopen(buff, "rb");
            size_t file_size = get_file_size(fp);
            char* data = reinterpret_cast<char*>(malloc(file_size));
            fread(data, file_size, 1, fp);
            fclose(fp);

            Archive ar(data, file_size);
            deserialize(ar, sim);

            sim->restart();

            printf("Starting at: %f\n", sim->time());

            free(data);

            return false;
        }

        Configuration config = sim->configuration();

        for(auto& particle: config.particles_){
            update_particle(particle, time, config.pbc_);
        }

        char buff[64];
        sprintf(buff, "%s/pid%u.pf%.3f.step%06u.xml", directory, getpid(), pf, nFiles);
        xml_save_config(buff, config);

        fprintf(pressure_fp, "%e\t%e\n", sim->time(), sim->average_pressure());
        fflush(pressure_fp);

        sim->reset_statistics();

        Archive ar;
        serialize(ar, *sim);
        sprintf(buff, "%s/archive.pf%.3f.pid%u.bin", directory, pf, getpid());
        FILE* fp = fopen(buff, "wb");
        fwrite(ar.data(), 1, ar.size(), fp);
        fclose(fp);

        ++nFiles;

        return true;
    });

    sim->run(100000.0, output);

    fclose(pressure_fp);

    return 0;
}