void myVecteur2D::normalise(){
	if (getNorme()==0)
	{
		std::cout << "vecteur nul" << std::endl;
	}
	else
	{
		float norm = sqrt(xdir*xdir + ydir*ydir);
		float a = xdir / norm;
		float b = ydir / norm;
		xdir = a;
		ydir = b;
		//std::cout << "vecteur non nul : "<< getNorme() << std::endl;
	}
}
Example #2
0
int main(void)
{
	float dx; /* space step */
	float dt; /* time step */
	float tOld,tNew;
	float rat; /* ratio dt/dx */
	float hOld[nbCell+2];
	float hNew[nbCell+2];
	float hExact[nbCell+2];
	float huOld[nbCell+2];
	float huNew[nbCell+2];
	float huExact[nbCell+2];
	float Z[nbCell+2];
	int i,compt,tmp;
	float norm;
	FILE *fichier2;
	FILE *fichier3;
	float temps;
    clock_t t1, t2;
	
	/* We compute the space step. */
	/*-----------------------------------------------------------------*/
		dx = (bSup-bInf)/(nbCell+1); /* we compute the space step */
	/*-----------------------------------------------------------------*/
	
	/* Select the topography you want */
	/*-----------------------------------------------------------------*/
		bumpBed(Z,dx);
		/*stepBed(Z,dx);*/
		/*noBed(Z);*/
		/*constantBed(Z);*/
		
		/* We save the topograhy in the file topo.txt*/
		fichier3 = fopen("topo.txt","w");
		for(i=0;i<=nbCell+1;i++)
		{
			fprintf(fichier3,"%f ",Z[i]);
		}
		fclose(fichier3);
	/*-----------------------------------------------------------------*/
	
	/* If you want to compute exact solution, first initialise hExact and huExact */
	/*-----------------------------------------------------------------*/
		for(i=0;i<=nbCell+1;i++) 
		{
			hExact[i] = 2.0 - Z[i];
			huExact[i] = 0.0;
		}
	/*-----------------------------------------------------------------*/
	
	/* Choose the initial condition you want */
	/*-----------------------------------------------------------------*/
		/*damBreakWetBed(hOld,huOld,Z,dx);*/
		/*damBreakDryBed(hOld,huOld,Z,dx);*/
		lakeAtRest(hOld,huOld,Z);
	/*-----------------------------------------------------------------*/
	
	dt = 0.0;
	tOld = 0.0;
	tNew = 0.0;
	tmp = 1;
	compt = 0;
	norm = 0;
	
	save(hOld,huOld,tmp); /* saving initial data */
	
	fichier2 = fopen("time.txt","w");
	fprintf(fichier2,"%f ",tNew);
	t1 = clock();
	while(tNew<T) /* loop on time*/
	{
		compt = compt + 1;
		
		/* We compute the time step thanks to te CFL condition. */
		/*-----------------------------------------------------------------*/
			dt = timeStepKinetic(dx,huOld,hOld); /* Choose this one if you want to use the kinetic solver. */
			/*dt = timeStepRusanov(dx,huOld,hOld);*/ /* Choose this one if you want to use the Rusanov solver. */
		/*-----------------------------------------------------------------*/
		
		tNew = tOld + dt;
		if(tNew > T)
		{
			dt = T - tOld;
			tNew = T;
		}
		printf("T = %f		dt = %f\n",tNew,dt); /* a print to have a look on the program. */
		
		rat = dt/dx;
		/* Choose the solver you want */
		/*-----------------------------------------------------------------*/
			updateKinetic(hNew,huNew,hOld,huOld,Z,rat);
			/*updateRusanov(hNew,huNew,hOld,huOld,Z,rat);*/
		/*-----------------------------------------------------------------*/

		/* Choose the boundary conditions you want */
		/*-----------------------------------------------------------------*/
			/*updateBC(hNew,huNew);*/ /* use this for free boundary conditions. */
			/*updateBCPeriodic(hNew,huNew,hOld,huOld);*/ /* use this to periodic boundary conditions. */
			updateBCLakeAtRest(hNew,huNew); /* we need to use this method to compute a lake at rest. */
		/*-----------------------------------------------------------------*/
		
		
		/* If you want to compare your results with the exact solution, choose the right one. */
		/*-----------------------------------------------------------------*/
			/*Ritter(hExact,huExact,dx,tNew,1.0);*/ /* Use this function for a dam break on dry bed */
		/*-----------------------------------------------------------------*/
		
		/* We save our results in file ".txt" */
		/*-----------------------------------------------------------------*/
			if((compt%freq==0)||(tNew==T))
			{
				tmp = tmp + 1;
				save(hNew,huNew,tmp);
				saveExact(hExact,huExact,tmp);
				norm = getNorme(hNew,hExact,dx);
				printf("|h-hex| = %f\n",norm);
				norm = getNorme(huNew,huExact,dx);
				printf("|hu-huex| = %f\n",norm);
				fprintf(fichier2,"%f ",tNew);
			}
		/*-----------------------------------------------------------------*/
		
		for(i=0;i<=nbCell+1;i++)
		{
			hOld[i] = hNew[i];
			huOld[i] = huNew[i];
		}
		tOld = tNew;
	}
	fclose(fichier2);
	t2 = clock();
    temps = (float)(t2-t1)/CLOCKS_PER_SEC;
    printf("Computation time = %f\n", temps);
    printf("number of iterations = %d\n",compt);
	return 0;
}