Ejemplo n.º 1
0
void MainWindow::on_pushButton_clicked()
{
    this->init();
    int nMax = pow (n, 2);
    int q=0;
    plot();
    plotArray();
    while (Nnew < nMax) {
//    vector->clear();

        QApplication::processEvents();
//        if (++q/3.0 == 1) {
//         plotArray();
//        } else {
//            q = 0;
//        }
        plotArray();
        plot();


    for (int k = 0; k < NNucleus; ++k) {

        r = speed * (t - timeNucleus[k]);

        for (int i = 0; i < n; ++i) {
            for (int j = 0; j < n; ++j) {
                if ((( pow(i - arrayNucleus[k][0], 2) + pow(j - arrayNucleus[k][1], 2)) < pow(r, 2))
                       && (array[i][j] == 0)) {
                    array[i][j] = -1;
                    Nnew++;
                }
            }
        }
    }


    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            if ((array[i][j] == 0) && (my_rand() < frequency*dx*dy*dt)) {
                 addNucleus(i, j);
            }
        }
     }

    t += dt;



    }

}
Ejemplo n.º 2
0
//Run Program
int main (int argc, char** argv) {
	
	//Read input argument
	int in = 0;
	double h = 0.5;
	double tol = 0.1;
	if (argc > 1) in = atoi(argv[1]);	
	if (argc > 2) h = atof(argv[2]);	
	if (argc > 3) tol = atof(argv[3]);	
	printf("Input arguments: %i %f %f \n", in, h, tol);
	
	//Check if SDL Initialization is successful
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		return -1;
	}

	//Create Window
	SDL_Window *win = SDL_CreateWindow("Adaptive Step Sizes",50,50,640,480,SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
	if (!win) {
		fprintf(stderr, "Could not create window: %s\n", SDL_GetError());
	}
	
	//Initialize OpenGL
	SDL_GLContext context = SDL_GL_CreateContext(win);
	if (!context) {
		fprintf(stderr, "Could not create OpenGL context: %s\n", SDL_GetError());
	}
	glViewport(0,0,(GLsizei)640,(GLsizei)480);
	glClearColor(1.0f,1.0f,1.0f,1.0f);
	glClear(GL_COLOR_BUFFER_BIT);
	SDL_GL_SwapWindow(win);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();
	glDisable(GL_DEPTH_TEST);

	//Plot Functions
	double xpos = -0.9;
	double ypos = -0.9;
	double width = 1.8;
	double height = 1.8;
	glColor4f(0.0f,0.0f,0.0f,1.0f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(xpos,ypos,0.0f);
		glVertex3f(xpos+width,ypos,0.0f);
		glVertex3f(xpos+width,ypos+height,0.0f);
		glVertex3f(xpos,ypos+height,0.0f);
	glEnd();
	glBegin(GL_LINES);
		for (int j=1; j<10; j++) {
		glVertex3f(xpos+j*width/10,ypos,0.0f);
		glVertex3f(xpos+j*width/10,ypos+0.02f,0.0f);
		glVertex3f(xpos+j*width/10,ypos+height,0.0f);
		glVertex3f(xpos+j*width/10,ypos+height-0.02f,0.0f);
		glVertex3f(xpos,ypos+j*height/10,0.0f);
		glVertex3f(xpos+0.02f,ypos+j*height/10,0.0f);
		glVertex3f(xpos+width,ypos+j*height/10,0.0f);
		glVertex3f(xpos+width-0.02f,ypos+j*height/10,0.0f);
		}
	glEnd();
	double xmin,xmax,ymin,ymax,t0;
	struct Vector* y0;
	int steps;
	void (*f)(double,struct Vector*,struct Vector*);
	switch(in) {
		case 0:
			xmin = 0.0;
			xmax = 20;
			ymin = 0.0;
			ymax = 1.2;
			t0 = 0;
			y0 = new_Vector(DIM);
			y0->values[0] = 1;
			steps = (int)floor((xmax-xmin)/h+1);
			f = f1;
		break;

		case 1:
			xmin = 0.0;
			xmax = 0.999;
			ymin = 0;
			ymax = 15;
			t0 = 0;
			y0 = new_Vector(DIM);
			y0->values[0] = 0;
			steps = (int)floor((xmax-xmin)/h+1);
			f = f2;
		break;
		
		default:
			printf("Invalid argument: %i\n",in);
		break;	
	}

	glColor4f(0.0f,0.0f,0.0f,1.0f);

	double* t = malloc(sizeof(double)*steps);
	double* values = malloc(sizeof(double)*steps);
	struct Vector** y = malloc(sizeof(struct Vector*)*steps);	
	for (int j=1; j < steps; j++) {
		y[j] = new_Vector(DIM);
	}

	/*	
	euler (f, t0, y0, h, t, y, steps);
	glColor4f(1.0f,0.0f,0.0f,1.0f);
	for (int j=0; j < steps; j++) {
		values[j] = y[j]->values[0];
	}
	plotArray(t, values, steps, xmin, xmax, ymin, ymax, xpos, ypos, width, height);
	*/
	
	/*
	modEuler (f, t0, y0, h, t, y, steps);
	glColor4f(0.0f,0.0f,1.0f,1.0f);
	for (int j=0; j < steps; j++) {
		values[j] = y[j]->values[0];
	}
	plotArray(t, values, steps, xmin, xmax, ymin, ymax, xpos, ypos, width, height);
	*/
			
	heun (f, t0, y0, h, t, y, steps);
	glColor4f(0.0f,1.0f,0.0f,1.0f);
	for (int j=0; j < steps; j++) {
		values[j] = y[j]->values[0];
	}
	plotArray(t, values, steps, xmin, xmax, ymin, ymax, xpos, ypos, width, height);
	
	adaptive_rk3 (f, t0, y0, tol, t, y, steps);
	glColor4f(1.0f,0.0f,0.0f,1.0f);
	for (int j=0; j < steps; j++) {
		values[j] = y[j]->values[0];
	}
	plotArray(t, values, steps, xmin, xmax, ymin, ymax, xpos, ypos, width, height);
	
	//Clean Up Memory					
	free(t);
	for (int j=1; j < steps; j++) {
		delete_Vector(y[j]);
	}
	free(y);
	free(values);
	delete_Vector(y0);

	SDL_GL_SwapWindow(win);

	//Save PLot as BMP
	/*
	unsigned char* pixels = malloc(640*480*4*sizeof(unsigned char));
	glReadPixels(0,0,640,480,GL_RGBA,GL_UNSIGNED_BYTE,pixels);
	SDL_Surface* sur = SDL_CreateRGBSurfaceFrom(pixels,640,480,8*4,640*4,0x000000FF,0x0000FF00,0x00FF0000,0);
	SDL_SaveBMP(sur,"test.bmp");
	SDL_FreeSurface(sur);
	free(pixels);
	*/
	

	
	//Wait for Window Close Event
	SDL_Event event;
	while (SDL_WaitEvent(&event) >= 0) {
		if (event.type == SDL_QUIT) {
			SDL_GL_DeleteContext(context);
			SDL_DestroyWindow(win);
			SDL_Quit();
			break;
		}
	}
	return 0;
}