Exemplo n.º 1
0
/*
 * implementation of euler approximation of the shallow water PDE
 * @pre: a valid Mesh class instance @mesh
 * @post: all triangle in the mesh class have new value() = old value - dt/area() * total flux, 
		  where total flux is calculated by all three edges of the triangle
   @return: return total time t+dt
*/
double hyperbolic_step(MESH& mesh, FLUX& f, double t, double dt) {
  // Step the finite volume model in time by dt.
  // Implement Equation 7 from your pseudocode here.

  for (auto it = mesh.tri_begin(); it!=mesh.tri_end() ; ++it)
  {
	// value function will return the flux
	QVar total_flux=QVar(0,0,0);
	QVar qm = QVar(0,0,0);
	// iterate through 3 edges of a triangle
	auto edgetemp = (*it).edge1();
	for (int num = 0; num < 3; num++)
	{	
		if (num ==0)
			edgetemp= (*it).edge1();
		else if (num==1)
			edgetemp = (*it).edge2();
		else	
			edgetemp = (*it).edge3();
			
		if (  mesh.has_neighbor(edgetemp.index()) ) // it has a common triangle
		{
			auto nx =  ((*it).norm_vector(edgetemp)).x;
			auto ny =  ((*it).norm_vector(edgetemp)).y;
			
			// find the neighbour of a common edge
			for (auto i = mesh.tri_edge_begin(edgetemp.index()); i != mesh.tri_edge_end(edgetemp.index()); ++i){	
				if (!(*i==*it))
					qm = (*i).value();
			}
			// calculat the total flux
			total_flux += f(nx, ny, dt, (*it).value(), qm);
		}
		else{
			// when it doesnt have a neighbour shared with this edge
			auto nx =  ((*it).norm_vector(edgetemp)).x;
			auto ny =  ((*it).norm_vector(edgetemp)).y;
			qm = QVar((*it).value().h, 0, 0 ); // approximation

			total_flux += f(nx, ny, dt, (*it).value(), qm);
		}
	}
	
	(*it).value() +=  total_flux * (- dt / (*it).area());
  }
  
  
  return t + dt;
}