Example #1
0
int main(int argc, char* argv[])
{
    //init MPI
    MPI_Init( &argc, &argv);
    int rank, size;
    MPI_Comm_rank( MPI_COMM_WORLD, &rank);
    MPI_Comm_size( MPI_COMM_WORLD, &size);
    //create a grid and some data
    if( size != 4){ std::cerr << "Please run with 4 threads!\n"; return -1;}
    double Tmax=2.*M_PI;
    double NT = 10;
    double dt = Tmax/NT;
    dg::Grid1d<double> gx( 0, 2.*M_PI, 3, 10);
    dg::Grid1d<double> gy( 0, 2.*M_PI, 3, 10);
    dg::Grid1d<double> gz( 0, 2.*M_PI, 1, 20);
    dg::Grid3d<double> g( gx, gy, gz);
    std::string hello = "Hello world\n";
    thrust::host_vector<double> data = dg::evaluate( function, g);

    //create NetCDF File
    int ncid;
    file::NC_Error_Handle err;
    MPI_Info info = MPI_INFO_NULL;
    err = nc_create_par( "testmpi.nc", NC_NETCDF4|NC_MPIIO|NC_CLOBBER, MPI_COMM_WORLD, info, &ncid); 
    err = nc_put_att_text( ncid, NC_GLOBAL, "input", hello.size(), hello.data());

    int dimids[4], tvarID;
    err = file::define_dimensions( ncid, dimids, &tvarID, g);
    int dataID;
    err = nc_def_var( ncid, "data", NC_DOUBLE, 4, dimids, &dataID);

    /* Write metadata to file. */
    err = nc_enddef(ncid);

    //err = nc_enddef( ncid);
    size_t start[4] = {0, rank*g.Nz()/size, 0, 0};
    size_t count[4] = {1, g.Nz()/size, g.Ny()*g.n(), g.Nx()*g.n()};
    if( rank==0) std::cout<< "Write from "<< start[0]<< " "<<start[1]<<" "<<start[2]<<" "<<start[3]<<std::endl;
    if( rank==0) std::cout<< "Number of elements "<<count[0]<< " "<<count[1]<<" "<<count[2]<<" "<<count[3]<<std::endl;
    err = nc_var_par_access(ncid, dataID , NC_COLLECTIVE);
    err = nc_var_par_access(ncid, tvarID , NC_COLLECTIVE);
    size_t Tcount=1, Tstart=0;
    double time = 0;
    //err = nc_close(ncid);
    for(unsigned i=0; i<=NT; i++)
    {
        if(rank==0)std::cout<<"Write timestep "<<i<<"\n";
        //err = nc_open_par( "testmpi.nc", NC_WRITE|NC_MPIIO, MPI_COMM_WORLD, info, &ncid); //doesn't work I don't know why
        time = i*dt;
        Tstart = i;
        data = dg::evaluate( function, g);
        dg::blas1::scal( data, cos( time));
        start[0] = i;
        //write dataset (one timeslice)
        err = nc_put_vara_double( ncid, dataID, start, count, data.data() + start[1]*count[2]*count[3]);
        err = nc_put_vara_double( ncid, tvarID, &Tstart, &Tcount, &time);
        //err = nc_close(ncid);
    }
    err = nc_close(ncid);
    MPI_Finalize();
    return 0;
}
Example #2
0
// This does a tri-linear interpolation of the derivatives at each of the 8
// grid points around the given point.  This avoids additional calls to the
// function and speeds things up by a factor of 2 or so.
GLvoid MarchingCubes::surfaceNormal(GLvector &norm, GLfloat const& x, GLfloat const& y, 
   GLfloat const& z)
{
   // needs offloading to the Grid class
   GLvector V000, V001, V010, V011, V100, V101, V110, V111;

   GLfloat gx(x/m_stepSize);
   GLfloat gy(y/m_stepSize);
   GLfloat gz(z/m_stepSize);

   GLint x0( std::floor(gx) );
   GLint y0( std::floor(gy) );
   GLint z0( std::floor(gz) );

   GLint x1(x0+1);
   GLint y1(y0+1);
   GLint z1(z0+1);

   int i = x0; int j = y0; int k = z0;
   V000.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V000.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V000.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x0; j = y0; k = z1;
   V001.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V001.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V001.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x0; j = y1; k = z0;
   V010.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V010.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V010.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x0; j = y1; k = z1;
   V011.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V011.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V011.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x1; j = y0; k = z0;
   V100.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V100.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V100.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x1; j = y0; k = z1;
   V101.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V101.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V101.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x1; j = y1; k = z0;
   V110.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V110.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V110.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   i = x1; j = y1; k = z1;
   V111.fX = (*m_grid)(i+1, j,   k  ) - (*m_grid)(i-1, j,   k  );
   V111.fY = (*m_grid)(i,   j+1, k  ) - (*m_grid)(i,   j-1, k  );
   V111.fZ = (*m_grid)(i,   j,   k+1) - (*m_grid)(i,   j,   k-1);

   GLvector p0;
   p0.fX = gx - x0;
   p0.fY = gy - y0;
   p0.fZ = gz - z0;

   GLvector p1;
   p1.fX = x1 - gx;
   p1.fY = y1 - gy;
   p1.fZ = z1 - gz;

   GLfloat dX, dY, dZ;
   dX = V000.fX * p1.fX * p1.fY * p1.fZ
      + V001.fX * p1.fX * p1.fY * p0.fZ
      + V010.fX * p1.fX * p0.fY * p1.fZ
      + V011.fX * p1.fX * p0.fY * p0.fZ
      + V100.fX * p0.fX * p1.fY * p1.fZ
      + V101.fX * p0.fX * p1.fY * p0.fZ
      + V110.fX * p0.fX * p0.fY * p1.fZ
      + V111.fX * p0.fX * p0.fY * p0.fZ;

   dY = V000.fY * p1.fX * p1.fY * p1.fZ
      + V001.fY * p1.fX * p1.fY * p0.fZ
      + V010.fY * p1.fX * p0.fY * p1.fZ
      + V011.fY * p1.fX * p0.fY * p0.fZ
      + V100.fY * p0.fX * p1.fY * p1.fZ
      + V101.fY * p0.fX * p1.fY * p0.fZ
      + V110.fY * p0.fX * p0.fY * p1.fZ
      + V111.fY * p0.fX * p0.fY * p0.fZ;

   dZ = V000.fZ * p1.fX * p1.fY * p1.fZ
      + V001.fZ * p1.fX * p1.fY * p0.fZ
      + V010.fZ * p1.fX * p0.fY * p1.fZ
      + V011.fZ * p1.fX * p0.fY * p0.fZ
      + V100.fZ * p0.fX * p1.fY * p1.fZ
      + V101.fZ * p0.fX * p1.fY * p0.fZ
      + V110.fZ * p0.fX * p0.fY * p1.fZ
      + V111.fZ * p0.fX * p0.fY * p0.fZ;

   norm.fX = -dX;
   norm.fY = -dY;
   norm.fZ = -dZ;

   normalizeVector(norm, norm);
}
Example #3
0
  void grid_eval(cl::program_t& prog,
                 size_t block0,
                 size_t block1,
                 size_t refine) {
    const size_t Nx = px.size();
    const size_t Ny = py.size();
    const size_t Nz = pz.size();

    cl::ctx_t ctx(prog.context());
    cl::queue_t queue(ctx, ctx.device(0));

    init_mesh();

    {
      cl::kernel_t kern = prog.kernel("grid");

      boost::scoped_array<uint8_t> out(new uint8_t[block0 * block0 * block0]);
      boost::scoped_array<float> gx(new float[block0]);
      boost::scoped_array<float> gy(new float[block0]);
      boost::scoped_array<float> gz(new float[block0]);

      cl::mem_t gx_mem =
          ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL);
      cl::mem_t gy_mem =
          ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL);
      cl::mem_t gz_mem =
          ctx.create_buffer(CL_MEM_READ_ONLY, block0 * sizeof(float), NULL);

      cl::mem_t out_mem =
          ctx.create_buffer(CL_MEM_READ_WRITE, block0 * block0 * block0, NULL);

      const size_t Py = block0;
      const size_t Pz = block0 * block0;

      kern.arg(0, gx_mem);
      kern.arg(1, gy_mem);
      kern.arg(2, gz_mem);
      kern.arg(3, out_mem);
      kern.arg(4, Py);
      kern.arg(5, Pz);

      for (size_t _z = 0; _z < Nz; _z += block0 - 1) {
        const size_t Wz = std::min(block0, Nz - _z);
        for (size_t i = 0; i < Wz; ++i)
          gz[i] = (float)pz[_z + i];
        queue.sync(cl::cmd_write_buffer(gz_mem, CL_FALSE, 0, Wz * sizeof(float),
                                        gz.get()));

        for (size_t _y = 0; _y < Ny; _y += block0 - 1) {
          const size_t Wy = std::min(block0, Ny - _y);
          for (size_t i = 0; i < Wy; ++i)
            gy[i] = (float)py[_y + i];
          queue.sync(cl::cmd_write_buffer(gy_mem, CL_FALSE, 0,
                                          Wy * sizeof(float), gy.get()));

          for (size_t _x = 0; _x < Nx; _x += block0 - 1) {
            std::cerr << "block: " << _x << "," << _y << "," << _z << std::endl;
            const size_t Wx = std::min(block0, Nx - _x);
            for (size_t i = 0; i < Wx; ++i)
              gx[i] = (float)px[_x + i];
            queue.sync(cl::cmd_write_buffer(gx_mem, CL_FALSE, 0,
                                            Wx * sizeof(float), gx.get()));

            queue.sync(cl::cmd_task(kern).wrk_sz(Wx, Wy, Wz));

            queue.sync(cl::cmd_read_buffer(
                out_mem, CL_FALSE, 0, block0 * block0 * block0, out.get()));

            for (size_t z = 0; z < Wz - 1; ++z) {
              for (size_t y = 0; y < Wy - 1; ++y) {
                for (size_t x = 0; x < Wx - 1; ++x) {
                  size_t bits = 0;

                  if (out[(x + 0) + (y + 0) * Py + (z + 0) * Pz])
                    bits |= 0x01;
                  if (out[(x + 1) + (y + 0) * Py + (z + 0) * Pz])
                    bits |= 0x02;
                  if (out[(x + 0) + (y + 1) * Py + (z + 0) * Pz])
                    bits |= 0x04;
                  if (out[(x + 1) + (y + 1) * Py + (z + 0) * Pz])
                    bits |= 0x08;
                  if (out[(x + 0) + (y + 0) * Py + (z + 1) * Pz])
                    bits |= 0x10;
                  if (out[(x + 1) + (y + 0) * Py + (z + 1) * Pz])
                    bits |= 0x20;
                  if (out[(x + 0) + (y + 1) * Py + (z + 1) * Pz])
                    bits |= 0x40;
                  if (out[(x + 1) + (y + 1) * Py + (z + 1) * Pz])
                    bits |= 0x80;

                  const uint8_t* row = mc_table[bits];

                  size_t n_tri = *row++;
                  if (n_tri == 0)
                    continue;

                  {
                    for (size_t t = 0; t < n_tri; ++t) {
                      uint8_t ea, eb, ec;
                      size_t va, vb, vc;
                      ea = *row++;
                      eb = *row++;
                      ec = *row++;

                      va = get_intersection(
                          decode_edge(_x + x, _y + y, _z + z, ea));
                      vb = get_intersection(
                          decode_edge(_x + x, _y + y, _z + z, eb));
                      vc = get_intersection(
                          decode_edge(_x + x, _y + y, _z + z, ec));

                      mesh->add_tri(va, vb, vc);
                    }
                  }
                }
              }
            }
          }
        }
      }
    }

    std::vector<std::pair<edgeint_t, size_t> > vert_calc;
    vert_calc.reserve(int_idx.size());
    for (typename edgeint_map_t::iterator i = int_idx.begin();
         i != int_idx.end(); ++i) {
      vert_calc.push_back(std::make_pair((*i).first, (*i).second));
    }

    {
      boost::scoped_array<cl_float3> a(new cl_float3[block1]);
      boost::scoped_array<cl_float3> b(new cl_float3[block1]);
      boost::scoped_array<cl_float3> c(new cl_float3[block1]);

      cl::mem_t a_mem =
          ctx.create_buffer(CL_MEM_READ_ONLY, block1 * sizeof(cl_float3), NULL);
      cl::mem_t b_mem =
          ctx.create_buffer(CL_MEM_READ_ONLY, block1 * sizeof(cl_float3), NULL);
      cl::mem_t c_mem = ctx.create_buffer(CL_MEM_READ_WRITE,
                                          block1 * sizeof(cl_float3), NULL);

      cl::kernel_t kern = prog.kernel("chop");

      kern.arg(0, a_mem);
      kern.arg(1, b_mem);
      kern.arg(2, c_mem);
      kern.arg(3, refine);

      for (size_t i = 0; i < vert_calc.size(); i += block1) {
        const size_t N = std::min(vert_calc.size() - i, block1);

        for (size_t j = 0; j < N; ++j) {
          edgeint_t& e = vert_calc[i + j].first;

          v3d_t a_pos = position_of_vertex(e.a[0], e.a[1], e.a[2]);
          v3d_t b_pos = position_of_vertex(e.b[0], e.b[1], e.b[2]);

          a[j].x = (float)a_pos.x;
          a[j].y = (float)a_pos.y;
          a[j].z = (float)a_pos.z;
          b[j].x = (float)b_pos.x;
          b[j].y = (float)b_pos.y;
          b[j].z = (float)b_pos.z;
        }

        queue.sync(cl::cmd_write_buffer(a_mem, CL_FALSE, 0,
                                        N * sizeof(cl_float3), a.get()));
        queue.sync(cl::cmd_write_buffer(b_mem, CL_FALSE, 0,
                                        N * sizeof(cl_float3), b.get()));

        queue.sync(cl::cmd_task(kern).wrk_sz(N));

        queue.sync(cl::cmd_read_buffer(c_mem, CL_FALSE, 0,
                                       N * sizeof(cl_float3), c.get()));

        for (size_t j = 0; j < N; ++j) {
          mesh->vertices[vert_calc[i + j].second].pos =
              v3d_t::init(c[j].x, c[j].y, c[j].z);
        }
      }
    }
  }
Example #4
0
		void Terrain::loadFromFile(std::string path, std::string sectionName) {
			res::openArchive(path);
			
			
			
			int width,height,channels;
			unsigned char* heightmap = res::getTexture2DBuffer(sectionName, "heightmap", &width, &height, &channels);
			
			for(int i = 0; i < width*height; i++)
				heightmap[i] /= 2;
			
			float* tmpArray = new float[width * height * 3];
			for(int i = 0; i < height; i++) {
				for(int j = 0; j < width; j++) {
					tmpArray[ coord(i, j)*3   ] = (float)i;
					tmpArray[ coord(i, j)*3 +1] = (float)heightmap[ coord(i, j) ];
					tmpArray[ coord(i, j)*3 +2] = (float)j;
				}
			}
			
			mVertices.bind();
			mVertices.alloc(width * height * 3);
			mVertices.sendData(0, tmpArray, width * height * 3);
			
			if(res::fileExists(sectionName, "normalmap")) {
				mNormalMap = res::getTexture2D(sectionName, "normalmap");
			}
			else {
				// Sobel operator
				mat3 gx(-1.0, -2.0, -1.0,
						 0.0,  0.0,  0.0,
						 1.0,  2.0,  1.0);
						
				mat3 gy(-1.0, 0.0, 1.0,
						-2.0, 0.0, 2.0,
						-1.0, 0.0, 1.0);
				
				for(int i = 1; i < height-1; i++) {
				   for(int j = 1; j < width-1; j++) {
						
						/*vec3 v1 = vec3( 0.0, ((float)heightmap[ coord(i-1, j) ] -(float)heightmap[ coord(i, j) ])/2,  1.0); v1.normalize();
						vec3 v2 = vec3( 1.0, ((float)heightmap[ coord(i, j-1) ] -(float)heightmap[ coord(i, j) ])/2,  0.0); v2.normalize();
						vec3 v3 = vec3( 0.0, ((float)heightmap[ coord(i+1, j) ] -(float)heightmap[ coord(i, j) ])/2, -1.0); v3.normalize();
						vec3 v4 = vec3(-1.0, ((float)heightmap[ coord(i, j+1) ] -(float)heightmap[ coord(i, j) ])/2,  0.0); v4.normalize();
						
						vec3 n = normalized( normalized(cross(v1, v2)) + normalized(cross(v2, v3)) + normalized(cross(v3, v4)) + normalized(cross(v4, v1)) );
						
						
						float sx = coord(i < height-1 ? i+1 : i, j) - coord(i != 0 ? i-1 : i, j);
						if (i == 0 || i == height -1)
							sx *= 2;

						float sy = coord(i, j < width-1 ? j+1 : j) - coord(i, j != 0 ?  j-1 : j);
						if (j == 0 || j == width -1)
							sy *= 2;
							
						n = normalized(vec3(-sx, 1, -sy));*/
						
						mat3 img( HeightMacro(i+1, j-1)/5, HeightMacro(i+1, j)/5, HeightMacro(i+1, j+1)/5,
								  HeightMacro(i  , j-1)/5, HeightMacro(i  , j)/5, HeightMacro(i  , j+1)/5,
								  HeightMacro(i-1, j-1)/5, HeightMacro(i-1, j)/5, HeightMacro(i-1, j+1)/5);
								  
						
						float cx = img.convolution( gx );
						float cy = img.convolution( gy );
						/*mat3 fx = gx * img;
						mat3 fy = gy * img;
						
						float cx = fx[0] + fx[1] + fx[2] + fx[6] + fx[7] + fx[8];
						float cy = fx[0] + fx[2] + fx[3] + fx[5] + fx[6] + fx[8];*/
						float cz = 0.5 + sqrt((cx*cx, cy*cy));
						
						vec3 n(cx, cz, cy);
						n.normalize();
						
						tmpArray[ coord(i, j)*3   ] = n.x;
						tmpArray[ coord(i, j)*3 +1] = n.y;
						tmpArray[ coord(i, j)*3 +2] = n.z;
					}
				}
				
				/*mNormals.bind();
				mNormals.alloc(width * height * 3);
				mNormals.sendData(0, tmpArray, width * height * 3);*/
				
			}
			
			std::vector<unsigned int> indicesVec;
			mRoot->build(0, 0, width, height, 5, ChunkInfo(width, height, 16, 16, &indicesVec));
			
			unsigned int indices[indicesVec.size()];
			for(int i = 0; i < indicesVec.size(); i++)
				indices[i] = indicesVec[i];
			
			mIndices.bind();
			mIndices.alloc(indicesVec.size());
			mIndices.sendData(0, indices, indicesVec.size());
			
			mAlphaMap = res::getTexture2D(sectionName, "alphamap");
			mDetailMaps = res::getTextureArray( res::getConf(sectionName, "detailtexturearray") );
			
			res::closeArchive();
		}
Example #5
0
void Tri2dFCBlockSolver::rhsViscous()
{
  if (order == 1 || order == 2){
    // Galerkin
    int n1,n2,n3,nn;
    double xa,ya,xb,yb,xc,yc,vr,fv[nq],gv[nq];
    Array2D<double> qc(3,nq),qac(3,nqa),cx(3,2);
    for (int n=0; n<nTri; n++){
      n1 = tri(n,0);
      n2 = tri(n,1);
      n3 = tri(n,2);
      xa = x(n1,0);
      ya = x(n1,1);
      xb = x(n2,0);
      yb = x(n2,1);
      xc = x(n3,0);
      yc = x(n3,1);
      vr = xa*(yb-yc)+xb*(yc-ya)+xc*(ya-yb);
      vr = 2./vr;
      
      for (int i=0; i<3; i++){
	for (int k=0; k<nq ; k++) qc (i,k) = q (n1,k);
	for (int k=0; k<nqa; k++) qac(i,k) = qa(n1,k);
	cx(i,0) = .5*(x(n3,1)-x(n2,1));
	cx(i,1) = .5*(x(n2,0)-x(n3,0));
	nn      = n1;
	n1      = n2;
	n2      = n3;
	n3      = nn;
      }
      
      sys->rhsVisFluxGalerkin(1,&vr,&cx(0,0),&qc(0,0),&qac(0,0),&fv[0],&gv[0]);
      for (int k=0; k<nq ; k++) d(n1,k) -=(cx(0,0)*fv[k]+cx(0,1)*gv[k]);
      for (int k=0; k<nq ; k++) d(n2,k) -=(cx(1,0)*fv[k]+cx(1,1)*gv[k]);
      for (int k=0; k<nq ; k++) d(n3,k) -=(cx(2,0)*fv[k]+cx(2,1)*gv[k]);
    }
    qc.deallocate();
    qac.deallocate();
    cx.deallocate();
  }


  else{// third-order scheme
    int m,l1,l2,n1,n2;
    double dx,dy,fk,Ax,Ay,cf,cg,a=.5;
    Array2D<double> qaxE(nne,nqaGradQa),qayE(nne,nqaGradQa);
    Array2D<double> f(nne,nq),g(nne,nq);
    Array2D<double> fx(nne,nq),gx(nne,nq),fy(nne,nq),gy(nne,nq);
    for (int n=0; n<nElem; n++){

      qaxE.set(0.);
      qayE.set(0.);
      fx.set(0.);
      fy.set(0.);
      gx.set(0.);
      gy.set(0.);

      // compute local gradients at the nodes in this element
      for (int i=0; i<nne; i++)
	for (int j=0; j<nne; j++){
	  dx = dxg(n,i,j,0);
	  dy = dxg(n,i,j,1);
	  m  = elem(n,j);
	  for (int k=0; k<nqaGradQa; k++){
	    qaxE(i,k) += qa(m,iqagrad(k))*dx;
	    qayE(i,k) += qa(m,iqagrad(k))*dy;
	  }}

      // compute viscous fluxes at the nodes in this element
      for (int i=0; i<nne; i++){
	m = elem(n,i);
	sys->rhsVisFlux(1,&q(m,0),&qa(m,0),&qaxE(i,0),&qayE(i,0),
			&f(i,0),&g(i,0));
      }

      // compute gradients of viscous fluxes
      for (int i=0; i<nne; i++)
	for (int j=0; j<nne; j++){
	  dx = dxg(n,i,j,0);
	  dy = dxg(n,i,j,1);
	  for (int k=0; k<nq ; k++){
	    fx(i,k) += f(j,k)*dx;
	    gx(i,k) += g(j,k)*dx;
	    fy(i,k) += f(j,k)*dy;
	    gy(i,k) += g(j,k)*dy;
	  }}

      // compute directed viscous fluxes and corrections
      // at edges and distribute to nodes
      for (int i=0; i<nee; i++){
	l1 = edgeE(i,0);
	l2 = edgeE(i,1);
	n1 = elem(n,l1);
	n2 = elem(n,l2);
	Ax = a*areaE(n,i,0);
	Ay = a*areaE(n,i,1);
	dx = .5*(x(n2,0)-x(n1,0));
	dy = .5*(x(n2,1)-x(n1,1));
	for (int k=0; k<nq; k++){
	  cf       = dx*(fx(l2,k)-fx(l1,k))+
		     dy*(fy(l2,k)-fy(l1,k));
	  cg       = dx*(gx(l2,k)-gx(l1,k))+
		     dy*(gy(l2,k)-gy(l1,k));
	  fk       = Ax*(f(l1,k)+f(l2,k)-cf)+Ay*(g(l1,k)+g(l2,k)-cg);
	  d(n1,k) -= fk;
	  d(n2,k) += fk;
	}}}

    qaxE.deallocate();
    qayE.deallocate();
    f.deallocate();
    g.deallocate();
    fx.deallocate();
    gx.deallocate();
    fy.deallocate();
    gy.deallocate();
  }
}
//---------------------------------------------------------
void NDG2D::OutputNodes_gauss()
//---------------------------------------------------------
{
  static int count = 0;
  string output_dir = ".";

  string buf = umOFORM("%s/gauss_N%02d_%04d.vtk", 
                      output_dir.c_str(), m_gauss.NGauss, ++count);

  FILE *fp = fopen(buf.c_str(), "w");
  if (!fp) {
    umLOG(1, "Could no open %s for output!\n", buf.c_str());
    return;
  }

  // Set flags and totals
 
  int Npoints = 3*m_gauss.NGauss;  // quadrature nodes per element

  // set totals for Vtk output
#if (1)
  // FIXME: no connectivity
  int vtkTotalPoints = this->K * Npoints;
  int vtkTotalCells  = vtkTotalPoints;
  int vtkTotalConns  = vtkTotalPoints;
  int Ncells = Npoints;
#else
  int vtkTotalPoints = this->K * Npoints;
  int vtkTotalCells  = this->K * Ncells;
  int vtkTotalConns  = (this->EToV.num_cols()+1) * this->K * Ncells;
  int Ncells = this->N * this->N;
#endif

  //-------------------------------------
  // 1. Write the VTK header details
  //-------------------------------------
  fprintf(fp, "# vtk DataFile Version 2");
  fprintf(fp, "\nNDGFem simulation nodes (surface quadrature)");
  fprintf(fp, "\nASCII");
  fprintf(fp, "\nDATASET UNSTRUCTURED_GRID\n");
  fprintf(fp, "\nPOINTS %d double", vtkTotalPoints);

  int newNpts=0;

  //-------------------------------------
  // 2. Write the vertex data
  //-------------------------------------
  const DMat& gx=m_gauss.x;
  const DMat& gy=m_gauss.y;
  for (int k=1; k<=this->K; ++k) {
    for (int n=1; n<=Npoints; ++n) {
      fprintf(fp, "\n%20.12e %20.12e %6.1lf", gx(n,k), gy(n,k), 0.0);
    }
  }

  //-------------------------------------
  // 3. Write the element connectivity
  //-------------------------------------

  // Number of indices required to define connectivity
  fprintf(fp, "\n\nCELLS %d %d", vtkTotalCells, 2*vtkTotalConns);


  // TODO: write element connectivity to file
  // FIXME: out-putting as VTK_VERTEX
  int nodesk=0;
  for (int k=0; k<this->K; ++k) {       // for each element
    for (int n=1; n<=Npoints; ++n) {
      fprintf(fp, "\n%d", 1);           // for each triangle
      for (int i=1; i<=1; ++i) {        // FIXME: no connectivity
        fprintf(fp, " %5d", nodesk);    // nodes in nth triangle
        nodesk++;                       // indexed from 0
      }
    }
  }


  //-------------------------------------
  // 4. Write the cell types
  //-------------------------------------

  // For each element (cell) write a single integer 
  // identifying the cell type.  The integer should 
  // correspond to the enumeration in the vtk file:
  // /VTK/Filtering/vtkCellType.h

  fprintf(fp, "\n\nCELL_TYPES %d", vtkTotalCells);

  for (int k=0; k<this->K; ++k) {
    fprintf(fp, "\n");
    for (int i=1; i<=Ncells; ++i) {
    //fprintf(fp, "5 ");            // 5:VTK_TRIANGLE
      fprintf(fp, "1 ");            // 1:VTK_VERTEX
      if (! (i%10))
        fprintf(fp, "\n");
    }
  }
  
  // add final newline to output
  fprintf(fp, "\n");
  fclose(fp);
}
Example #7
0
void tet_basis::proj2d(FLT *lin1, FLT *f1, FLT *dx1, FLT *dy1, int stride) {
	Array<FLT,2> wk0(gpy,3+em);
	Array<FLT,2> wk1(gpy,3+em);
	Array<FLT,2> wk2(gpy,3+em);
   const int be2 = em+3, be3 = 2*em+3, bint = 3+3*em;
   const int lgpx = gpx, lgpy = gpy, lnmodx = nmodx;  
   FLT lcl0, lcl1, lcl2;
   FLT xp1,oeta; 
   int sign;
#ifdef BZ_DEBUG
   Array<FLT,1> lin(lin1, shape(3+3*em+fm), neverDeleteData);
   Array<FLT,2> f(f1, shape(gpx,stride), neverDeleteData);
   Array<FLT,2> dx(dx1, shape(gpx,stride), neverDeleteData);
   Array<FLT,2> dy(dy1, shape(gpx,stride), neverDeleteData);
#endif
   
   /* DETERMINE U VALUES, GRAD U VALUES
      AT COLLOCATION POINTS
      SUM HAT(U) FOR DU/DX AND DU/DY
   */
   
   /* GENERAL FORMULA
   	dg/dr = 2.0/(1-n) g(s) dg/dx
   	dg/ds = g(x)dg/dn +(1+x)/2 dg/dr = g(x)dg/dn +(1+x)/(1-s) g(s) dg/dx 
   */

   /* PART I - sum u*g_mn for each n, s_j   */
	for(int j = 0; j < lgpy; ++j ) {
		oeta = y0(j);
      
		/* VERTEX 1 */
		wk0(j,0) = lin(0)*gy(j,1);
		wk1(j,0) = lin(0)*dgy(j,1);
		wk2(j,0) = wk0(j,0)*oeta;

		/* VERTEX 2, EDGE 3 */
		sign = 1;
		lcl0 = lin(1)*gy(j,2);
		lcl1 = lin(1)*dgy(j,2);
		for(int i = 0; i < em; ++i){
			lcl0 += sign*lin(be3+i)*gy(j,3+em+i);	
			lcl1 += sign*lin(be3+i)*dgy(j,3+em+i);
			sign*=-1;
		}
		wk0(j,1) = lcl0;
		wk1(j,1) = lcl1;
		wk2(j,1) = wk0(j,1)*oeta;

		/* VERTEX 3, EDGE 2 */
		lcl0 = lin(2)*gy(j,2);
		lcl1 = lin(2)*dgy(j,2);
		for(int i = 0; i < em; ++i){
			lcl0 += lin(be2+i)*gy(j,3+em+i);	
			lcl1 += lin(be2+i)*dgy(j,3+em+i);
		}
		wk0(j,2) = lcl0;
		wk1(j,2) = lcl1;
		wk2(j,2) = wk0(j,2)*oeta;
		
		/* EDGE 1, FACE 0 */		
		int ind2 = 0;		
		for(int p = 3; p < em+3; ++p){
			lcl0=lin(p)*gy(j,p);	
			lcl1=lin(p)*gy(j,p);		
			for(int i = 1; i <= em-p+2; ++i){	
				lcl0 += lin(bint+ind2)*gy(j,3+2*em+ind2);
				lcl1 += lin(bint+ind2)*gy(j,3+2*em+ind2);
				++ind2;
			}			
			wk0(j,p) = lcl0;
			wk1(j,p) = lcl1;
			wk2(j,p) = wk0(j,p)*oeta;
		}
	}
		
		
	/* SUM OVER N AT EACH I,J POINT   */  
	for (int i = 0; i < lgpx; ++i ) {
		xp1 = x0(i);
		for (int j = 0; j < lgpy; ++j) {
			lcl0 = wk0(j,0)*gx(i,0);
			lcl1 = wk1(j,0)*gx(i,0);
			lcl2 = wk2(j,0)*dgx(i,0);
			
			for(int n = 1; n < lnmodx; ++n ) {     
				lcl0 += wk0(j,n)*gx(i,n);
				lcl1 += wk1(j,n)*gx(i,n);
				lcl2 += wk2(j,n)*dgx(i,n);
			}
			f(i,j)  = lcl0;
			dy(i,j) = lcl1 +xp1*lcl2;
			dx(i,j) = lcl2;
		}
	}

   return;
}
Example #8
0
void tet_basis::proj2d(FLT *lin1, FLT *f1, int stride) {
	Array<FLT,2> wk0(gpy,3+em);
   const int be2 = em+3, be3 = 2*em+3, bint = 3+3*em;
   const int lgpx = gpx, lgpy = gpy, lnmodx = nmodx;  
   int sign;
   FLT lcl0;
#ifdef BZ_DEBUG
   Array<FLT,1> lin(lin1, shape(3+3*em+fm), neverDeleteData);
   Array<FLT,2> f(f1, shape(gpx,stride), neverDeleteData);
#endif

   /* DETERMINE U VALUES, GRAD U VALUES
      AT COLLOCATION POINTS
      SUM HAT(U) FOR DU/DX AND DU/DY
   */

   /* PART I - sum u*g_mn for each n, s_j   */
  	for(int j = 0; j < lgpy; ++j ) {
     
		/* VERTEX 1 */
		wk0(j,0) = lin(0)*gy(j,1);

		/* VERTEX 2, EDGE 3 */
		sign = 1;
		lcl0 = lin(1)*gy(j,2);
		for(int i = 0; i < em; ++i){
			lcl0 += sign*lin(be3+i)*gy(j,3+em+i);	
			sign*=-1;
		}
		wk0(j,1) = lcl0;

		/* VERTEX 3, EDGE 2 */
		lcl0 = lin(2)*gy(j,2);
		for(int i = 0; i < em; ++i){
			lcl0 += lin(be2+i)*gy(j,3+em+i);
		}
		wk0(j,2) = lcl0;
		
		/* EDGE 1, FACE 0 */		
		int ind2 = 0;	
		int bint1 = em-3+2;	
		for(int p = 3; p < em+3; ++p){
			lcl0=lin(p)*gy(j,p);	
			for(int i = 1; i <= em-p+2; ++i){	
				lcl0 += lin(bint+ind2)*gy(j,3+2*em+ind2);
				//cout << bint+i+ind2<< endl;
				++ind2;
			}						
			wk0(j,p) = lcl0;
	
		}
	}		
	
		
	/* SUM OVER N AT EACH I,J POINT   */  
	for (int i = 0; i < lgpx; ++i ) {
		for (int j = 0; j < lgpy; ++j) {
			lcl0 = wk0(j,0)*gx(i,0);
			for(int n = 1; n < lnmodx; ++n ) {     
				lcl0 += wk0(j,n)*gx(i,n);
			}
			f(i,j)  = lcl0;
		}
	}
	
   return;
}
Example #9
0
void ewol::resource::DistanceFieldFont::generateDistanceField(const egami::ImageMono& _input, egami::Image& _output) {
	std11::unique_lock<std11::recursive_mutex> lock(m_mutex);
	int32_t size = _input.getSize().x() * _input.getSize().y();
	std::vector<short> xdist(size);
	std::vector<short> ydist(size);
	std::vector<double> gx(size);
	std::vector<double> gy(size);
	std::vector<double> data(size);
	std::vector<double> outside(size);
	std::vector<double> inside(size);
	// Convert img into double (data)
	double img_min = 255, img_max = -255;
	for (int32_t yyy = 0; yyy < _input.getSize().y(); ++yyy) {
		for (int32_t xxx = 0; xxx < _input.getSize().x(); ++xxx) {
			int32_t iii = yyy * _input.getSize().x() + xxx;
			double v = _input.get(ivec2(xxx, yyy));
			data[iii] = v;
			if (v > img_max) {
				img_max = v;
			}
			if (v < img_min) {
				img_min = v;
			}
		}
	}
	// Rescale image levels between 0 and 1
	for (int32_t yyy = 0; yyy < _input.getSize().y(); ++yyy) {
		for (int32_t xxx = 0; xxx < _input.getSize().x(); ++xxx) {
			int32_t iii = yyy * _input.getSize().x() + xxx;
			data[iii] = (_input.get(ivec2(xxx, yyy))-img_min)/img_max;
		}
	}
	
	// Compute outside = edtaa3(bitmap); % Transform background (0's)
	computegradient(&data[0], _input.getSize().x(), _input.getSize().y(), &gx[0], &gy[0]);
	edtaa3(&data[0], &gx[0], &gy[0], _input.getSize().x(), _input.getSize().y(), &xdist[0], &ydist[0], &outside[0]);
	for(size_t iii = 0; iii < outside.size(); ++iii) {
		if( outside[iii] < 0 ) {
			outside[iii] = 0.0;
		}
	}
	
	// Compute inside = edtaa3(1-bitmap); % Transform foreground (1's)
	for(size_t iii = 0; iii < gx.size(); ++iii) {
		gx[iii] = 0;
	}
	for(size_t iii = 0; iii < gy.size(); ++iii) {
		gy[iii] = 0;
	}
	for(size_t iii = 0; iii < data.size(); ++iii) {
		data[iii] = 1 - data[iii];
	}
	computegradient( &data[0], _input.getSize().x(), _input.getSize().y(), &gx[0], &gy[0]);
	edtaa3(&data[0], &gx[0], &gy[0], _input.getSize().x(), _input.getSize().y(), &xdist[0], &ydist[0], &inside[0]);
	for(size_t iii = 0; iii < inside.size(); ++iii) {
		if( inside[iii] < 0 ) {
			inside[iii] = 0.0;
		}
	}
	
	_output.resize(_input.getSize(), etk::Color<>(0));
	_output.clear(etk::Color<>(0));
	for (int32_t xxx = 0; xxx < _output.getSize().x(); ++xxx) {
		for (int32_t yyy = 0; yyy < _output.getSize().y(); ++yyy) {
			int32_t iii = yyy * _output.getSize().x() + xxx;
			outside[iii] -= inside[iii];
			outside[iii] = 128+outside[iii]*16;
			if( outside[iii] < 0 ) {
				outside[iii] = 0;
			}
			if( outside[iii] > 255 ) {
				outside[iii] = 255;
			}
			uint8_t val = 255 - (unsigned char) outside[iii];
			// TODO : Remove multiple size of the map ...
			_output.set(ivec2(xxx, yyy), etk::Color<>((int32_t)val,(int32_t)val,(int32_t)val,255));
		}
	}
}