Vector<DataType> computeNormal(unsigned int ind1, unsigned int ind2, DataType blend, const Array3D<DataType, IndexType> & A)
{
    DataType dx,dy,dz;
    int i,j,k;
    
    A.ind2sub(ind1, i,j,k);
    dx = (A(i+1,j,k) - A(i-1,j,k)) * 0.5;
    dy = (A(i,j+1,k) - A(i,j-1,k)) * 0.5;
    dz = (A(i,j,k+1) - A(i,j,k-1)) * 0.5;
    
    Vector<DataType> n1(dx,dy,dz);
    n1.normalize();
    
    A.ind2sub(ind2, i,j,k);
    dx = (A(i+1,j,k) - A(i-1,j,k)) * 0.5;
    dy = (A(i,j+1,k) - A(i,j-1,k)) * 0.5;
    dz = (A(i,j,k+1) - A(i,j,k-1)) * 0.5;
    
    Vector<DataType> n2(dx,dy,dz);
    n2.normalize();
    
    Vector<DataType> n = n1*(1-blend) + n2*blend;
    n.normalize();
        
    return n;
}
示例#2
0
int Application::main(int argc,char *argv[])
  {
    // Process command line
    CommandLine cmd(argc,argv,"");
    if(cmd.numArgs()!=2)
      throw String("train-3state <alignment-file> <outfile>");
    String infile=cmd.arg(0);
    String outfile=cmd.arg(1);

    // Initialization
    transCounts.resize(4,4);
    emitCounts.resize(4,nAlpha,nAlpha);
    transCounts.setAllTo(0);
    emitCounts.setAllTo(0);

    // Process all alignments (pairs of sequences in fasta file)
    FastaReader reader(infile,alphabet);
    String def1, seq1, def2, seq2;
    while(reader.nextSequence(def1,seq1) &&
	  reader.nextSequence(def2,seq2)) {
      Sequence S1(seq1,alphabet), S2(seq2,alphabet);
      updateCounts(S1,S2);
    }

    // Compute HMM parameters
    PairHMM hmm(alphabet,gapSymbol,4);
    hmm.setStateType(INSERTION_STATE,PHMM_INSERT);
    hmm.setStateType(DELETION_STATE,PHMM_DELETE);
    hmm.setStateType(MATCH_STATE,PHMM_MATCH);
    hmm.setStateType(0,PHMM_OTHER);
    for(STATE i=0 ; i<4 ; ++i) {
      double sum=0;
      for(STATE j=0 ; j<4 ; ++j)
	sum+=transCounts[i][j];
      for(STATE j=0 ; j<4 ; ++j)
	hmm.setTransP(i,j,transCounts[i][j]/sum);
    }
    for(STATE k=0 ; k<4 ; ++k) {
      double sum=0;
      for(Symbol i=0 ; i<nAlpha ; ++i)
	for(Symbol j=0 ; j<nAlpha ; ++j)
	  sum+=emitCounts(k,i,j);
      if(sum==0) sum=1;
      for(Symbol i=0 ; i<nAlpha ; ++i)
	for(Symbol j=0 ; j<nAlpha ; ++j)
	  hmm.setEmitP(k,i,j,emitCounts(k,i,j)/sum);
    }      

    // Save HMM
    hmm.save(outfile);

    return 0;
  }
示例#3
0
//----------------------------------------------------------------------
int Array3D::copyTo(Array3D& a)
// copy to "a" from "this"
// return 0 if 0k, -1 otherwise
{
	//- printf("inside copyTo\n");
    int* dims = a.getDims();
    if (dims[0] != np1 || dims[1] != np2 || dims[2] != np3) {
        return(-1);
    }
    GE_FLOAT* ptr = a.getDataPtr();
	//- printf("copyTo:: npts= %d\n", npts);
    memcpy(ptr, data, sizeof(GE_FLOAT)*npts);
    return(0);
}
示例#4
0
//----------------------------------------------------------------------
int Array3D::copyFrom(Array3D& a)
// copy from "a" to "this"
{
    // //- printf("enter copyFrom\n");
    int* dims = a.getDims();
    // //- printf("dims= %d, %d, %d\n", dims[0], dims[1], dims[2]);
    if (dims[0] != np1 || dims[1] != np2 || dims[2] != np3) {
		// //- printf("dims from: %d, %d, %d\n", dims[0], dims[1], dims[2]);
		// //- printf("dims to: %d, %d, %d\n", np1, np2, np3);
        //- printf("error in copyFrom (array3D.cpp)\n");
        return(-1);
    }
    GE_FLOAT* ptr = a.getDataPtr();

    memcpy(data, ptr, sizeof(GE_FLOAT)*npts);
    return(0);
}
示例#5
0
//----------------------------------------------------------------------
int Array3D::copyFrom(Array3D& a, Vec3i& fromOrig, Vec3i& fromRange, Vec3i& toOrigin)
{
    Vec3i fromMaxDims = fromOrig + fromRange;
    fromMaxDims.clampMaxTo(a.getMaxDims());
    fromOrig.clampMinTo(a.getOrigin());
    fromRange = fromMaxDims - fromOrig;
    Vec3i toMaxDims = toOrigin + fromRange;

    if (toOrigin < this->origin) return -1;
    if ((toOrigin+fromRange) > maxDims) return -1;

    for (int k=0; k < fromRange[2]; k++) {
    for (int j=0; j < fromRange[1]; j++) {
    for (int i=0; i < fromRange[0]; i++) {
        set(i+toOrigin.x(), j+toOrigin.y(), k+toOrigin.z(),  a.get(i+fromOrig.x(), j+fromOrig.y(), k+fromOrig.z()));
    }}}
    return(0);
}
示例#6
0
//----------------------------------------------------------------------
int Array3D::copyTo(Array3D& a, Vec3i& toOrigin, Vec3i& fromRange, Vec3i& fromOrigin)
// copy from "this" to "a"
{
    Vec3i fromMaxDims = fromOrigin + fromRange;
    fromMaxDims.clampMaxTo(maxDims);
    fromOrigin.clampMinTo(origin);
    fromRange = fromMaxDims - fromOrigin;
    Vec3i toMaxDims = toOrigin + fromRange;

    if (toOrigin < a.getOrigin()) return -1;
    if ((toOrigin+fromRange) > a.getMaxDims()) return -1;

    for (int k=0; k < fromRange.z(); k++) {
    for (int j=0; j < fromRange.y(); j++) {
    for (int i=0; i < fromRange.x(); i++) {
        a.set(i+toOrigin.x(), j+toOrigin.y(), k+toOrigin.z(), 
                get(i+fromOrigin.x(), j+fromOrigin.y(), k+fromOrigin.z()));
    }}}
    return(0);
}
示例#7
0
文件: export.cpp 项目: pajadam/Genek
bool exporter::Export( string filename, settings &gen, Array3D &_map )
{
    ofstream mFile( filename );

    if( !mFile.good() )
        return 1;

    //Map Size
    mFile << "size " << gen.sizeX << " "
                     << gen.sizeY << " "
                     << gen.sizeZ << endl;

    // Map Data
    for( gSize y = 0; y < gen.sizeY; y++ ){
        for( gSize z = 0; z < gen.sizeZ; z++ ){
            mFile << "voxline " << y << " " << z;
            for( gSize x = 0; x < gen.sizeX; x++ ){
                mFile << " " << _map.get( x, y, z );
            }
            mFile << endl;
        }
    }

    // Materials
    for( gSize i = 0; i < gen.materials.size(); i++ )
    {
        mFile << "material " << gen.materials[ i ].name  << " "
                             << gen.materials[ i ].sizeX << " "
                             << gen.materials[ i ].sizeY << endl;
    }
    // Sky and Sun
    mFile << "sky mc" << endl;
    mFile << "sun " << gen.sunlight.dx << " "
                    << gen.sunlight.dy << " "
                    << gen.sunlight.dz << " "
                    << gen.sunlight.r  << " "
                    << gen.sunlight.g  << " "
                    << gen.sunlight.b  << " "
                    << gen.sunlight.ar << " "
                    << gen.sunlight.ag << " "
                    << gen.sunlight.ab << endl;
    // Player Spawn
    mFile << "startpos " << gen.player.x << " "
                         << gen.player.y << " "
                         << gen.player.z << " "
                         << gen.player.direction << " "
                         << gen.player.physics << " "
                         << gen.player.torchLevel << " "
                         << gen.player.testLightning << endl;

    mFile.close();

    return 0;
}
int WangTilesProcessor::SanitizeSmall(const TilePack & tiles,
                                      const Array3D<Pixel> & input,
                                      Array3D<Pixel> & output)
{
    if((tiles.size() <= 0) ||
       (input.Size(0)%tiles.size()) || (input.Size(1)%tiles[0].size()))
    {
        // small image, just make it a constant average of input
        output = input;

        for(int cha = 0; cha < input.Size(2); cha++)
        {
            Pixel sum = 0;

            {
                for(int row = 0; row < input.Size(0); row++)
                    for(int col = 0; col < input.Size(1); col++)
                    {
                        sum += input[row][col][cha];
                    }

                sum /= (input.Size(0)*input.Size(1));
            }

            {
                for(int row = 0; row < output.Size(0); row++)
                    for(int col = 0; col < output.Size(1); col++)
                    {
                        output[row][col][cha] = sum;
                    }
            }
        }

        return 1;
    }
    else
    {
        return 0;
    }
}
示例#9
0
T Mask3D<T>::get(size_t n, Array3D<V> array, size_t h, size_t w, size_t d) {
#ifdef __CUDA_ARCH__
    h += this->deviceMask[this->dimension*n];
    w += this->deviceMask[this->dimension*n+1];
    d += this->deviceMask[this->dimension*n+2];
    return (w<array.getWidth() && h<array.getHeight() && d<array.getDepth())?array(h,w,d):this->haloValue;
#else
    h += this->hostMask[this->dimension*n];
    w += this->hostMask[this->dimension*n+1];
    d += this->hostMask[this->dimension*n+2];
    return (w<array.getWidth() && h<array.getHeight() && d<array.getDepth())?array(h,w,d):this->haloValue;
#endif
}
int WangTilesProcessor::TileMismatch(const TilePack & tiles,
                                     const Array3D<Pixel> & input)
{
    int mismatch = 0;
    
    // count the number of edge colors
    int e_colors[4] = {0, 0, 0, 0};
    if(!CountEdgeColors(tiles,
                        e_colors[0],
                        e_colors[1],
                        e_colors[2],
                        e_colors[3]))
    {
        return 0;
    }
    
    // compute tile size
    const int tile_height = (tiles.size() > 0) ? input.Size(0)/tiles.size() : 0;
    const int tile_width = (tiles.size() > 0 && tiles[0].size() > 0) ? input.Size(1)/tiles[0].size() : 0;
    const int tile_depth = input.Size(2);

    // make sure all edges with
    // (1) the same S/E/N/W orientations and
    // (2) same edge id
    // have identical images
    for(int e_index = 0; e_index < 4; e_index++)
        for(int e_color = 0; e_color < e_colors[e_index]; e_color++)
        {
            // find a tile with e_color in e_index
            int t_row = -1; int t_col = -1;

            {
                for(unsigned int i = 0; i < tiles.size(); i++)
                    for(unsigned int j = 0; j < tiles[i].size(); j++)
                    {  
                        const WangTiles::Tile & tile = tiles[i][j];

                        const int my_e_colors[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
                        if(my_e_colors[e_index] == e_color)
                        {
                            // found the tile
                            t_row = i; t_col = j;
                            break;
                        }
                    }
            }
            
            if((t_row >= 0) && (t_col >= 0))
            {
                const WangTiles::Tile & sample_tile = tiles[t_row][t_col];

                const int row_offset1 = t_row*tile_height;
                const int col_offset1 = t_col*tile_width;
                            
                for(unsigned int i = 0; i < tiles.size(); i++)
                    for(unsigned int j = 0; j < tiles[i].size(); j++)
                    {  
                        const WangTiles::Tile & tile = tiles[i][j];

                        const int my_e_colors[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
                        if(my_e_colors[e_index] == e_color)
                        {
                            // try to see if they match
                            const int row_offset2 = i*tile_height;
                            const int col_offset2 = j*tile_width;
                
                            int row_start, row_end, col_start, col_end;

                            EdgeIndices(e_index, tile_height, tile_width,
                                        row_start, row_end,
                                        col_start, col_end);

                            for(int row = row_start; row <= row_end; row++)
                                for(int col = col_start; col <= col_end; col++)
                                    for(int cha = 0; cha < tile_depth; cha++)
                                    {
                                        if(input[row+row_offset1][col+col_offset1][cha] != input[row+row_offset2][col+col_offset2][cha])
                                        {
                                            mismatch = 1;
                                        }
                                    }
                        }
                    }
            }
        }
    
    // done
    return mismatch;
}
int WangTilesProcessor::SanitizeCorners(const TilePack & tiles,
                                        const Array3D<Pixel> & input,
                                        Array3D<Pixel> & output)
{
    if(SanitizeSmall(tiles, input, output))
    {
        return 1;
    }
    
    const int tile_height = input.Size(0)/tiles.size();
    const int tile_width = input.Size(1)/tiles[0].size();
    const int tile_depth = input.Size(2);
    
    // initialize
    // note that for each row we have all possible corner combinations
    MeanTile mean_corner_tile;
    {
        Array3D<Pixel> image(tile_height, tile_width, tile_depth);
    
        for(int row = 0; row < image.Size(0); row++)
            for(int col = 0; col < image.Size(1); col++)
                for(int cha = 0; cha < image.Size(2); cha++)
                {
                    image[row][col][cha] = 0;
                }

        mean_corner_tile.image = image;
        mean_corner_tile.count = 0;
    }

    // compute mean/average tiles for corner
    // note that we need to consider all tiles to get correct results!
    {
        for(unsigned int i = 0; i < tiles.size(); i++)
            for(unsigned int j = 0; j < tiles[i].size(); j++)
            {
                const WangTiles::Tile & tile = tiles[i][j];

                const int input_row_start = i*tile_height;
                const int input_col_start = j*tile_width;

                for(int row = 0; row < tile_height; row++)
                    for(int col = 0; col < tile_width; col++)
                        for(int cha = 0; cha < tile_depth; cha++)
                        {
                            mean_corner_tile.image[row][col][cha] += input[row+input_row_start][col+input_col_start][cha];
                        }

                    mean_corner_tile.count++;
            }
    }

    // normalization
    {
        if(mean_corner_tile.count > 0)
        {
            Array3D<Pixel> & image = mean_corner_tile.image;
            
            for(int row = 0; row < image.Size(0); row++)
                for(int col = 0; col < image.Size(1); col++)
                    for(int cha = 0; cha < image.Size(2); cha++)
                    {
                        image[row][col][cha] /= mean_corner_tile.count;
                    }

            mean_corner_tile.count = 1;
        }
    }

    // assign to the output
    {
        for(unsigned int i = 0; i < tiles.size(); i++)
            for(unsigned int j = 0; j < tiles[i].size(); j++)
            {
                const WangTiles::Tile & tile = tiles[i][j];

                const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
                
                const int output_row_start = i*tile_height;
                const int output_col_start = j*tile_width;
                
                for(int k = 0; k < 4; k++)
                {
                    int row, col;
                    CornerIndices(k, tile_height, tile_width, row, col);
                        
                    // only touch the corners
                    for(int cha = 0; cha < tile_depth; cha++)
                    {
                        output[row+output_row_start][col+output_col_start][cha] = mean_corner_tile.image[row][col][cha];
                    } 
                }
            }
    }
    
    // done
    return 1;
}
int WangTilesProcessor::SanitizeEdges(const TilePack & tiles,
                                      const Array3D<Pixel> & input,
                                      Array3D<Pixel> & output)
{
    // error checking
    if(SanitizeSmall(tiles, input, output))
    {
        return 1;
    }
    
    const int tile_height = input.Size(0)/tiles.size();
    const int tile_width = input.Size(1)/tiles[0].size();
    const int tile_depth = input.Size(2);
    
    // count the number of edge colors
    int e_colors[4] = {0, 0, 0, 0};
    { 
        if(!CountEdgeColors(tiles,
                            e_colors[0],
                            e_colors[1],
                            e_colors[2],
                            e_colors[3]))
        {
            return 0;
        }
        
        // check consistency
        if((e_colors[0] != e_colors[2]) || (e_colors[1] != e_colors[3]))
        {
            return 0;
        }
    }

    // initialize
    vector< vector<MeanTile> > mean_tiles(4);
    {
        Array3D<Pixel> image(tile_height, tile_width, tile_depth);
        for(int row = 0; row < image.Size(0); row++)
            for(int col = 0; col < image.Size(1); col++)
                for(int cha = 0; cha < image.Size(2); cha++)
                {
                    image[row][col][cha] = 0;
                }
        
        for(unsigned int i = 0; i < mean_tiles.size(); i++)
        {
            mean_tiles[i] = vector< MeanTile >(e_colors[i]);

            for(unsigned int j = 0; j < mean_tiles[i].size(); j++)
            {          
                mean_tiles[i][j].image = image;
                mean_tiles[i][j].count = 0;
            }
        }
    }

    // compute mean/average tiles per edge color
    {
        for(unsigned int i = 0; i < tiles.size(); i++)
            for(unsigned int j = 0; j < tiles[i].size(); j++)
            {
                const WangTiles::Tile & tile = tiles[i][j];

                const int input_row_start = i*tile_height;
                const int input_col_start = j*tile_width;

                const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
                
                for(int k = 0; k < 4; k++)
                {
                    MeanTile & output_tile = mean_tiles[k][tile_edges[k]];

                    for(int row = 0; row < tile_height; row++)
                        for(int col = 0; col < tile_width; col++)
                            for(int cha = 0; cha < tile_depth; cha++)
                            {
                                output_tile.image[row][col][cha] += input[row+input_row_start][col+input_col_start][cha];
                            }

                    output_tile.count++;
                }
            }
    }
    
    {
        for(unsigned int i = 0; i < mean_tiles.size(); i++)
            for(unsigned int j = 0; j < mean_tiles[i].size(); j++)
            {
                MeanTile & tile = mean_tiles[i][j];

                if(tile.count > 0)
                {
                    Array3D<Pixel> & image = tile.image;
                    
                    for(int row = 0; row < image.Size(0); row++)
                        for(int col = 0; col < image.Size(1); col++)
                            for(int cha = 0; cha < image.Size(2); cha++)
                            {
                                image[row][col][cha] /= tile.count;
                            }

                    tile.count = 1;
                }
            }
    }
    
    // fix the output tile boundaries
    output = input;
    Array3D<Pixel> boundary(output);
    Array3D<Pixel> boundary_count(output);

    {
        // initialization
        for(int i = 0; i < boundary.Size(0); i++)
            for(int j = 0; j < boundary.Size(1); j++)
                for(int k = 0; k < boundary.Size(2); k++)
                {
                    boundary[i][j][k] = 0;
                    boundary_count[i][j][k] = 0;
                }
    }
    
    {
        // compute the correct tile boundary
        for(unsigned int i = 0; i < tiles.size(); i++)
            for(unsigned int j = 0; j < tiles[i].size(); j++)
            {
                const WangTiles::Tile & tile = tiles[i][j];

                const int output_row_offset = i*tile_height;
                const int output_col_offset = j*tile_width;

                const int tile_edges[4] = {tile.e0(), tile.e1(), tile.e2(), tile.e3()};
                
                for(int k = 0; k < 4; k++)
                {
                    const Array3D<Pixel> & mean_tile = mean_tiles[k][tile_edges[k]].image;

                    int row_start, row_end, col_start, col_end;
                    EdgeIndices(k, tile_height, tile_width,
                                row_start, row_end, col_start, col_end);

                    for(int row = row_start; row <= row_end; row++)
                        for(int col = col_start; col <= col_end; col++)
                            for(int cha = 0; cha < tile_depth; cha++)
                            {
                                boundary[row+output_row_offset][col+output_col_offset][cha] += mean_tile[row][col][cha];
                                boundary_count[row+output_row_offset][col+output_col_offset][cha] += 1.0;
                            }
                }
            }
    }

    {
        // normalization and assign to the output
        for(int i = 0; i < boundary.Size(0); i++)
            for(int j = 0; j < boundary.Size(1); j++)
                for(int k = 0; k < boundary.Size(2); k++)
                {
                    if(boundary_count[i][j][k] > 0)
                    {
                        output[i][j][k] = boundary[i][j][k]/boundary_count[i][j][k];
                    }
                }
    }
    
    // done
    return 1;
}
示例#13
0
void FM_Freeman(
  const Array2D<E> &elevations,
  Array3D<float> &props,
  const double xparam
){
  RDLOG_ALG_NAME<<"Freeman (1991) Flow Accumulation (aka MFD, MD8)";
  RDLOG_CITATION<<"Freeman, T.G., 1991. Calculating catchment area with divergent flow based on a regular grid. Computers & Geosciences 17, 413–422.";
  RDLOG_CONFIG<<"p = "<<xparam;

  props.setAll(NO_FLOW_GEN);
  props.setNoData(NO_DATA_GEN);

  ProgressBar progress;
  progress.start(elevations.size());

  #pragma omp parallel for collapse(2)
  for(int y=0;y<elevations.height();y++)
  for(int x=0;x<elevations.width();x++){
    ++progress;

    if(elevations.isNoData(x,y)){
      props(x,y,0) = NO_DATA_GEN;
      continue;
    }

    if(elevations.isEdgeCell(x,y))
      continue;

    const E e    = elevations(x,y);

    double C = 0;
    for(int n=1;n<=8;n++){
      const int nx = x+dx[n];
      const int ny = y+dy[n];

      if(!elevations.inGrid(nx,ny))
        continue;
      if(elevations.isNoData(nx,ny)) //TODO: Don't I want water to drain this way?
        continue;

      const E ne = elevations(nx,ny);

      if(ne<e){
        const double rise = e-ne;
        const double run  = dr[n];
        const double grad = rise/run;
        const auto cval   = std::pow(grad,xparam);
        props(x,y,n)      = cval;
        C                += cval;
      }
    }

    if(C>0){
      props(x,y,0) = HAS_FLOW_GEN;

      C = 1/C; //TODO

      for(int n=1;n<=8;n++){
        auto &this_por = props(x,y,n);
        if(this_por>0)
          this_por *= C;
        else
          this_por = 0;
      }
    }
  }
  progress.stop();
}
示例#14
0
void Strand2dFCBlockSolver::gradSetupSub(Array3D<double>& gx)
{
  // initialize coefficient array
  gx.set(0.);


  // form quadratic sub elements
  int meshOrderL=2,nElemLocalL=meshOrder-1,nSurfElemL=nSurfElem*nElemLocalL;
  Array3D<int> surfElemL(nSurfElemL,meshOrderL+1,2);
  Array3D<int> elemLocalL(nElemLocalL,meshOrderL+1,2);
  if (meshOrder == 2){
    elemLocalL(0,0,0) = 0; elemLocalL(0,0,1) = 1;
    elemLocalL(0,1,0) = 1; elemLocalL(0,1,1) = 1;
    elemLocalL(0,2,0) = 2; elemLocalL(0,2,1) = 1;
  }
  else if (meshOrder == 3){
    elemLocalL(0,0,0) = 0; elemLocalL(0,0,1) = 1;
    elemLocalL(0,1,0) = 3; elemLocalL(0,1,1) = 0;
    elemLocalL(0,2,0) = 2; elemLocalL(0,2,1) = 1;
    elemLocalL(1,0,0) = 2; elemLocalL(1,0,1) = 0;
    elemLocalL(1,1,0) = 1; elemLocalL(1,1,1) = 1;
    elemLocalL(1,2,0) = 3; elemLocalL(1,2,1) = 1;
  }
  else if (meshOrder == 4){
    elemLocalL(0,0,0) = 0; elemLocalL(0,0,1) = 1;
    elemLocalL(0,1,0) = 3; elemLocalL(0,1,1) = 0;
    elemLocalL(0,2,0) = 2; elemLocalL(0,2,1) = 1;
    elemLocalL(1,0,0) = 2; elemLocalL(1,0,1) = 0;
    elemLocalL(1,1,0) = 4; elemLocalL(1,1,1) = 0;
    elemLocalL(1,2,0) = 3; elemLocalL(1,2,1) = 1;
    elemLocalL(2,0,0) = 3; elemLocalL(2,0,1) = 0;
    elemLocalL(2,1,0) = 1; elemLocalL(2,1,1) = 1;
    elemLocalL(2,2,0) = 4; elemLocalL(2,2,1) = 1;
  }
  
  int k=0;
  for (int n=0; n<nSurfElem; n++)
    for (int i=0; i<nElemLocalL; i++){
      for (int m=0; m<meshOrderL+1; m++){
	surfElemL(k,m,0) = surfElem(n,elemLocalL(i,m,0));
	surfElemL(k,m,1) = elemLocalL(i,m,1);
      }
      k++;
    }
  if (k != nSurfElemL){
    cout << "\n***Problem forming sub-elements in initialize.C***"
	 << endl;
    exit(0);
  }
 

  // Lagrange polynomial derivatives for lower order elements
  int spacing=0; // assume equally spaced points in surface elements for now
  Array1D<double> ss(meshOrderL+1);
  solutionPoints1D(meshOrderL,
		   spacing,
		   &ss(0));

  bool test=false;
  Array2D<double> lc(meshOrderL+1,meshOrderL+1);
  lagrangePoly1D(test, // coefficients to form Lagrange polynomials
		 meshOrderL,
		 &ss(0),
		 &lc(0,0));

  // ls(i,j) = (dl_j/ds)_i (a row is all Lagrange polynomials (derivatives)
  // evaluated at a single mesh point i)
  Array2D<double> lsL(meshOrderL+1,meshOrderL+1);
  lsL.set(0.);
  int km;
  for (int i=0; i<meshOrderL+1; i++) // ith mesh point
    for (int j=0; j<meshOrderL+1; j++) // jth Lagrange polynomial
      for (int k=0; k<meshOrderL+1; k++){
	km        = max(0,k-1);
	lsL(i,j) +=((double)k)*pow(ss(i),km)*lc(j,k);
      }


  // mapping terms for lower order elements
  Array3D<double> xsL(nSurfElemL,meshOrderL+1,nStrandNode);
  Array3D<double> ysL(nSurfElemL,meshOrderL+1,nStrandNode);
  Array3D<double> jacL(nSurfElemL,meshOrderL+1,nStrandNode);
  xsL.set(0.);
  ysL.set(0.);
  int ni,nm;
  double x0,y0,nx,ny;
  for (int n=0; n<nSurfElemL; n++)
    for (int i=0; i<meshOrderL+1; i++){ // ith point in the element
      ni = surfElemL(n,i,0);
      for (int m=0; m<meshOrderL+1; m++){ // mth Lagrange poly. in mapping
	nm = surfElemL(n,m,0);
	x0 = surfX(nm,0);
	y0 = surfX(nm,1);
	nx = pointingVec(nm,0);
	ny = pointingVec(nm,1);
	for (int j=0; j<nStrandNode; j++){
	  xsL(n,i,j) += lsL(i,m)*(x0+nx*strandX(j));
	  ysL(n,i,j) += lsL(i,m)*(y0+ny*strandX(j));
	}}
      for (int j=0; j<nStrandNode; j++)
	jacL(n,i,j) = xsL(n,i,j)*yn(ni,j)-ysL(n,i,j)*xn(ni,j);
    }


  // degree at each surface node
  Array1D<int> sum(nSurfNode);
  sum.set(0);
  for (int n=0; n<nSurfElemL; n++)
    for (int i=0; i<meshOrderL+1; i++)
      sum(surfElemL(n,i,0)) += surfElemL(n,i,1);


  // use elements to form gradient coefficients
  double xnj,ynj;
  Array3D<double> a(nSurfNode,nStrandNode,2);
  a.set(0.);
  for (int n=0; n<nSurfElemL; n++)
    for (int i=0; i<meshOrderL+1; i++){ //ith point in the element
      if (surfElemL(n,i,1) == 1){ //if a contributing node
	ni = surfElemL(n,i,0);
	for (int m=0; m<meshOrderL+1; m++){ //mth Lagrange poly. in mapping
	  nm = surfElemL(n,m,0);
	  for (int j=0; j<nStrandNode; j++){ //local gradient coefficients
	    xnj       = xn(ni,j)/(jacL(n,i,j)*(double)sum(ni));
	    ynj       = yn(ni,j)/(jacL(n,i,j)*(double)sum(ni));
	    a(nm,j,0) = lsL(i,m)*ynj;
	    a(nm,j,1) =-lsL(i,m)*xnj;
	  }}
	for (int m=psp2(ni); m<psp2(ni+1); m++){ //add to coefficient array
	  nm = psp1(m);
	  for (int j=0; j<nStrandNode; j++){
	    gx(m,j,0) += a(nm,j,0);
	    gx(m,j,1) += a(nm,j,1);
	  }}
	for (int m=0; m<meshOrderL+1; m++){ //reset helper array to zero
	  nm = surfElemL(n,m,0);
	  for (int j=0; j<nStrandNode; j++){
	    a(nm,j,0) = 0.;
	    a(nm,j,1) = 0.;
	  }}}}


  // clean up
  sum.deallocate();
  a.deallocate();
  surfElemL.deallocate();
  elemLocalL.deallocate();
  xsL.deallocate();
  ysL.deallocate();
  ss.deallocate();
  lc.deallocate();
  lsL.deallocate();
}
int shadow_preset(Array3D & shadow, VoxelBox vb, V3f sta, std::vector<CylData> & cyls, int nCyl)
{
  shadow.Reset(0.0);//Reset to 0.0 the shadow voxel box

  //********************************************
  //Below is the any pattern for the shadow box.
  //********************************************

  float sh_dist = 4.0;//dist at which the environment shades
  int xv,yv,zv;
  V3f cyl_point;
  // Average shadow field exerted by the surrounding
  // for(int i=0; i<nCyl; i++){
  //   if(cyls[i].is_deleted || cyls[i].order == 0)
  //     continue;
  //   if( ( cyls[i].end - sta ).Length() > (sh_dist - (float)SHLEN ) ){
  //     //cyl_point = cyls[i].start + 0.5*cyls[i].length*cyls[i].axis;
  //     cyl_point = cyls[i].end;
  //     xv = ceil( (cyl_point.x - vb.offset.x) / vb.vdx ) - 1;
  //     if(xv < 0 || xv >= shadow.m_width)
  // 	continue;
  //     yv = ceil( (cyl_point.y - vb.offset.y) / vb.vdy ) - 1;
  //     if(yv < 0 || yv >= shadow.m_height)
  // 	continue;
  //     zv = ceil( (cyl_point.z - vb.offset.z) / vb.vdz ) - 1;
  //     if(zv < 0 || zv >= (shadow.m_data.size()/(shadow.m_width*shadow.m_height)))
  // 	continue;
  //     shadow(xv,zv,yv) = (float)SH;
  //   }
  // }

  
  //Bright cube
  // for(int i = 0; i < GSY; i++){
  //   for(int j = 0; j < GSX; j++){
  //     for(int k = 0; k < GSZ; k++){
  // 	if( k >= 60 && k <= 130 && j >= 60 && j <= 130)
  // 	  continue;
  // 	shadow(j,k,i) = 1000*QRAD;
  //     }
  //   }
  // }
  //External layer of the voxel box is dark
  // int thick = 150;
  // for(int i = 0; i < GSY; i++){
  //   for(int j = 0; j < GSX; j++){
  //     for(int k = 0; k < GSZ; k++){
  // 	if( j < thick || j >= (GSX-thick) )
  // 	  shadow(j,k,i) = QRAD;
  // 	else{
  // 	  if(k < thick || k >= (GSZ-thick))
  // 	    shadow(j,k,i) = QRAD;
  // 	}
  //     }
  //   }
  // }
  //Centered pyramidal penumbra
  // float highest = 0.0;
  // float distal_x = 0.0;
  // float distal_z = 0.0;
  // for(int i=0;i<nCyl;i++){
  //   if(cyls[i].is_deleted)
  //     continue;
  //   if(highest < cyls[i].end.y){
  //     highest = cyls[i].end.y;
  //   }
  //   if(distal_x < fabs(cyls[i].end.x))
  //     distal_x = fabs(cyls[i].end.x);
  //   if(distal_z < fabs(cyls[i].end.z))
  //     distal_z = fabs(cyls[i].end.z);
  // }
  // int yv = ceil( (highest - vb.offset.y) / vb.vdy ) - 1;
  // int dxv = ceil( (fabs(distal_x-sta.x)) / vb.vdx ) - 1;
  // int dzv = ceil( (fabs(distal_z-sta.z)) / vb.vdz ) - 1;
  // //(0,0,0) point base or other sta
  // int yb = ceil( (sta.y - vb.offset.y) / vb.vdy ) - 1;
  // int xb = ceil( (sta.x - vb.offset.x) / vb.vdx ) - 1;
  // int zb = ceil( (sta.z - vb.offset.z) / vb.vdz ) - 1;
  
  // //std::cout << yv << ", " << dxv << ", " << dzv << ", " << yb << std::endl;
  // // float pivot_x[] = {0.5,-0.1,0.1,-0.1};
  // // float pivot_z[] = {0.5,0.5,-0.5,-0.5};
  // // std::cout << "Iterate over Y: " << yv-1 << ":" << yb << std::endl;
  // // std::cout << "Iterate over X: " << xb-dxv << ":" << dxv+xb << std::endl;
  // // std::cout << "Iterate over Z: " << zb-dzv << ":" << dzv+zb << std::endl;
  // // std::cout << yv << ", " << yb << ", " << std::endl;
  // for(int iy = yv; iy >= yb; iy--){//Iterate over the layers down, Y-span
  //   for(int ix = xb-dxv; ix <= dxv+xb; ix++){//Iterate over X-span
  //     for(int iz = zb-dzv; iz <= dzv+zb; iz++){//Iterate over Z-span
  // 	if( (ix < 0) || (ix >= GSX) || (iy < 0) || \
  // 	    (iy >= GSY) || (iz < 0) || (iz >= GSZ) )
  // 	  continue;
  // 	//shadow(ix,iz,iy) += QRAD * pow(1.1,-(iy-yb));
  // 	shadow(ix,iz,iy) += QRAD *					\
  // 	  -(float)(iy-yb) * (float)(iy-yb) / ((float)(yv-yb)*(yv-yb)) + 1.0;
  //     }
  //   }
  // }
  return 0;
}
示例#16
0
template <typename PointT> void
pcl::FastBilateralFilter<PointT>::applyFilter (PointCloud &output)
{
  if (!input_->isOrganized ())
  {
    PCL_ERROR ("[pcl::FastBilateralFilter] Input cloud needs to be organized.\n");
    return;
  }

  copyPointCloud (*input_, output);
  float base_max = std::numeric_limits<float>::min (),
        base_min = std::numeric_limits<float>::max ();
  for (size_t x = 0; x < output.width; ++x)
    for (size_t y = 0; y < output.height; ++y)
      if (pcl_isfinite (output (x, y).z))
      {
        if (base_max < output (x, y).z)
          base_max = output (x, y).z;
        if (base_min > output (x, y).z)
          base_min = output (x, y).z;
      }

  for (size_t x = 0; x < output.width; ++x)
      for (size_t y = 0; y < output.height; ++y)
        if (!pcl_isfinite (output (x, y).z))
          output (x, y).z = base_max;

  const float base_delta = base_max - base_min;

  const size_t padding_xy = 2;
  const size_t padding_z  = 2;

  const size_t small_width  = static_cast<size_t> (static_cast<float> (input_->width  - 1) / sigma_s_) + 1 + 2 * padding_xy;
  const size_t small_height = static_cast<size_t> (static_cast<float> (input_->height - 1) / sigma_s_) + 1 + 2 * padding_xy;
  const size_t small_depth  = static_cast<size_t> (base_delta / sigma_r_)   + 1 + 2 * padding_z;


  Array3D data (small_width, small_height, small_depth);
  for (size_t x = 0; x < input_->width; ++x)
  {
    const size_t small_x = static_cast<size_t> (static_cast<float> (x) / sigma_s_ + 0.5f) + padding_xy;
    for (size_t y = 0; y < input_->height; ++y)
    {
      const float z = output (x,y).z - base_min;

      const size_t small_y = static_cast<size_t> (static_cast<float> (y) / sigma_s_ + 0.5f) + padding_xy;
      const size_t small_z = static_cast<size_t> (static_cast<float> (z) / sigma_r_ + 0.5f) + padding_z;

      Eigen::Vector2f& d = data (small_x, small_y, small_z);
      d[0] += output (x,y).z;
      d[1] += 1.0f;
    }
  }


  std::vector<long int> offset (3);
  offset[0] = &(data (1,0,0)) - &(data (0,0,0));
  offset[1] = &(data (0,1,0)) - &(data (0,0,0));
  offset[2] = &(data (0,0,1)) - &(data (0,0,0));

  Array3D buffer (small_width, small_height, small_depth);

  for (size_t dim = 0; dim < 3; ++dim)
  {
    const long int off = offset[dim];
    for (size_t n_iter = 0; n_iter < 2; ++n_iter)
    {
      std::swap (buffer, data);
      for(size_t x = 1; x < small_width - 1; ++x)
        for(size_t y = 1; y < small_height - 1; ++y)
        {
          Eigen::Vector2f* d_ptr = &(data (x,y,1));
          Eigen::Vector2f* b_ptr = &(buffer (x,y,1));

          for(size_t z = 1; z < small_depth - 1; ++z, ++d_ptr, ++b_ptr)
            *d_ptr = (*(b_ptr - off) + *(b_ptr + off) + 2.0 * (*b_ptr)) / 4.0;
        }
    }
  }

  if (early_division_)
  {
    for (std::vector<Eigen::Vector2f >::iterator d = data.begin (); d != data.end (); ++d)
      *d /= ((*d)[0] != 0) ? (*d)[1] : 1;

    for (size_t x = 0; x < input_->width; x++)
      for (size_t y = 0; y < input_->height; y++)
      {
        const float z = output (x,y).z - base_min;
        const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
                                                                static_cast<float> (y) / sigma_s_ + padding_xy,
                                                                z / sigma_r_ + padding_z);
        output(x,y).z = D[0];
      }
  }
  else
  {
    for (size_t x = 0; x < input_->width; ++x)
      for (size_t y = 0; y < input_->height; ++y)
      {
        const float z = output (x,y).z - base_min;
        const Eigen::Vector2f D = data.trilinear_interpolation (static_cast<float> (x) / sigma_s_ + padding_xy,
                                                                static_cast<float> (y) / sigma_s_ + padding_xy,
                                                                z / sigma_r_ + padding_z);
        output (x,y).z = D[0] / D[1];
      }
  }
}
示例#17
0
int
main (int argc, char **argv)
{
  /* Allocate a 2d array */
  ofstream fout;
  ofstream gout;
  ofstream fin;
  ofstream logfile;
  ifstream init;


  int inject_jet=0;


  int ii = 0, jj = 0, kk = 0, hh = 0;
  int rc;
  int timestep = 0;
  int maxstep = 0;
  int printtime = 10;
  int second = 0;
  double max_speed = 0.0;
  double maxtime = 0.0;
  double *maximumspeed = &max_speed;
  double time = 0.0;
  double delta_t = 0.0;
  double dtodx = 0.0;
  double del = 0.0;
  double delh = 0.0;
  double cfl = 0.80;
  double gammam1 = gammag - 1;
  double fn[8];
  zone maxvar;
  zone minvar;
#ifdef DEBUG
  double px, py, et, ri, rl, ul, vl, ke, pl, al;
#endif /* DEBUG */

  clock_t start, end;
  double elapsed;
  double den_temp[4];
  string probtype;


  start = clock ();


  gammag = 5.0 / 3.0;
//  gammag =1.4;
//  
  /*
     init.open ("input/init.dat");
     init >> nx >> ny;
     init >> maxstep;
     init >> cfl;
     init.close ();
   */
  logfile.open ("output/roe.log");
  logfile.close ();
  logfile.open ("output/falle.log");
  logfile.close ();
  logfile.open ("logfile.txt");
  logfile.close ();
  ne = 8;


  if (argc > 1)
    {
      init.open (argv[1]);
    }
  else
    {
      init.open ("input/gaz1");
    }

  init >> nx;
  init.ignore (256, '\n');
  init >> ny;
  init.ignore (256, '\n');
  init >> maxstep;
  init.ignore (256, '\n');
  init >> cfl;
  init.ignore (256, '\n');
  init >> printtime;
  init.ignore (256, '\n');
  init >> delta_x;
  init.ignore (256, '\n');
  init >> gammag;
  init.ignore (256, '\n');
  init >> maxtime;
  init.ignore (256, '\n');
  init >> probtype;
  init.close ();


  gammam1 = gammag - 1;


  nz = 1;
  Array3D < zone > grid (nx, ny, nz);
  Array3D < zone > gridh (nx, ny, nz);
  Array3D < zone > gridn (nx, ny, nz);
  Array3D < zone > fx (nx, ny, nz);
  Array3D < zone > fy (nx, ny, nz);
  Array3D < zone > xResState (nx, ny, nz);
  Array3D < zone > yResState (nx, ny, nz);

  delta_x = 1.0 / nx;

  cout << "\t\t\t2D Roe Solver Code" << endl;
  cout << "\t\t\tVersion 2.0" << endl;
  cout << "\t\t\tNo of steps =  " << maxstep << endl;
  cout << "\t\t\tNX    =  " << nx << endl;
  cout << "\t\t\tNY    =  " << ny << endl;
  cout << "\t\t\tNE    =  " << ne << endl;
  cout << "\t\t\tCFL   =  " << cfl << endl;
  cout << "\t\t\tdel_x =  " << delta_x << endl;
  cout << "\t\t\tGamma =  " << gammag << endl;
#ifdef SECOND_ORDER_TIME
  cout << "\t\t\t2nd Order Time and Space " << endl;
#endif /* SECOND_ORDER_TIME */
#ifdef LAPIDUS_VISCOSITY
  cout << "\t\t\tLapidus Viscosity " << endl;
#endif /* LAPIDUS_VISCOSITY */
  cout << endl;
  cout << endl;

  // Set up initial values of conserved variables on the grid */


  if (probtype == "Shock")
    {
      if (argc > 1)
	{
	  rc = initialise (argv[1], grid, &maxstep, &cfl);
	}
      else
	{
	  rc = initialise ("input/gaz1", grid, &maxstep, &cfl);
	}

    }

  else if (probtype == "Jet")
    {
      if (argc > 1)
	{
	  rc = initialise_jet (argv[1], grid, &maxstep, &cfl);
	}
      else
	{
	  rc = initialise_jet ("input/input.jet", grid, &maxstep, &cfl);
	}
    }

  else if (probtype == "Blast")
    {
      if (argc > 1)
	{
	  rc = initialise_blast (argv[1], grid, &maxstep, &cfl);
	}
      else
	{
	  rc = initialise_blast ("input/input.jet", grid, &maxstep, &cfl);
	}
    }



/* Print out the initial array */
  fin.open ("infile.txt");
  for (ii = 0; ii < nx; ii++)
    {
      for (jj = 0; jj < ny; jj++)
	{
	  fin << ii
	    << " " << jj
	    << " " << grid[ii][jj][kk] _MASS
	    << " " << grid[ii][jj][kk] _MOMX
	    << " " << grid[ii][jj][kk] _MOMY
	    << " " << grid[ii][jj][kk] _MOMZ
	    << " " << grid[ii][jj][kk] _ENER
	    << " " << grid[ii][jj][kk] _B_X
	    << " " << grid[ii][jj][kk] _B_Y
	    << " " << grid[ii][jj][kk] _B_Z << endl;
	}
    }
  fin.close ();


#ifdef DEBUG1
  for (ii = 0; ii < nx; ii++)
    {
      for (jj = 0; jj < ny; jj++)
	{
	  cout << grid[ii][jj][kk] _MASS << "\t";
	}
      cout << endl;
    }

#endif
/* Using a second order in time Runge-Kutta method, advect the array */

	  rc = output (grid, fx, fy, 0, "out_2d_");
  for (timestep = 1; timestep < maxstep; timestep++)
    {
      for (int k = 0; k < ne; k++)
	{
	  maxvar.array[k] = 0.;
	  minvar.array[k] = 999.;
	}
      /* Set the maximum wave speed to zero */
      *maximumspeed = 0;
      /* Determine the maximum wave speed for use in
       * the time step */
      rc = maxspeed (grid, maximumspeed);

      /* Determine a value for time advance and courant number based on the
       * maximum wave speed */

      del = cfl / *maximumspeed;
      delh = 0.5 * del;
      delta_t = del * delta_x;
		dtodx = 1.0/ *maximumspeed;
      time = time + delta_t;
#ifdef VERBOSE_OUTPUT
      cout << "Tstep= " << setiosflags (ios::scientific) << timestep;
      cout << "\tTime= " << setiosflags (ios::scientific) << time;
      cout << "\tMaxspeed= " << setiosflags (ios::
					     scientific) << *maximumspeed;
      cout << "\t dt = " << setiosflags (ios::scientific) << delta_t;
      cout << "\tCFL= " << setiosflags (ios::scientific) << del;
      cout << endl;
#endif /* VERBOSE_OUTPUT */


//  rc = output ( grid, fx, fy, timestep, "oldg_2d_");

#ifdef SECOND_ORDER_TIME
      jj = 0;


#ifdef TWODIM
      for (jj = 2; jj < ny - 1; jj++)
#endif /* TWODIM */
	{
	  for (ii = 2; ii < nx - 1; ii++)
	    {
	      rc =
		flux (grid, fx[ii][jj][kk].array, xResState[ii][jj][kk].array, dtodx,
		      ii, jj, timestep, 1, 0);
#ifdef TWODIM
	      rc =
		flux (grid, fy[ii][jj][kk].array, yResState[ii][jj][kk].array,dtodx,
		      ii, jj, timestep, 2, 0);
#endif /* TWODIM */
	    }
	}

//#ifdef TWODIM
//          for (jj = 2; jj < ny - 2; jj++)
//#endif /* TWODIM */
//        {
      //      for (ii = 2; ii < nx - 2; ii++)
//             {

      rc =
	update (gridh, grid, fx, fy, xResState, yResState, delh, ii, jj,
		timestep, grid, delta_t, 0);

//             }
//        }

      /* Boundary Conditions */
      rc = boundary (gridh, inject_jet);
      /* End Boundary Conditions */
#ifdef DEBUG_HALFSTEP
      if (timestep % printtime == 0)
	{
	  rc = output (gridh, fx, fy, timestep, "hout_2d_");
	}
#endif /* DEBUG HALFSTEP */


#ifdef TWODIM
      for (jj = 2; jj < ny - 1; jj++)
#endif /* TWODIM */
	{
	  for (ii = 2; ii < nx - 1; ii++)
	    {
	      rc =
		flux (gridh, fx[ii][jj][kk].array,
		      xResState[ii][jj][kk].array,dtodx, ii, jj, timestep, 1, 1);
#ifdef TWODIM
	      rc =
		flux (gridh, fy[ii][jj][kk].array,
		      yResState[ii][jj][kk].array,dtodx, ii, jj, timestep, 2, 1);
#endif /* TWODIM */
	    }
	}

      rc =
	update (gridn, grid, fx, fy, xResState, yResState, del, ii, jj,
		timestep, gridh, delta_t, 1);
#ifdef TWODIM
      for (jj = 2; jj < ny - 2; jj++)
#endif /* TWODIM */
	{
	  for (ii = 2; ii < nx - 2; ii++)
	    {
	      for (int k = 0; k < ne; k++)
		{
		  maxvar.array[k] =
		    (maxvar.array[k] >
		     gridn[ii][jj][kk].array[k] ? maxvar.
		     array[k] : gridn[ii][jj][kk].array[k]);
		  minvar.array[k] =
		    (minvar.array[k] <
		     gridn[ii][jj][kk].array[k] ? minvar.
		     array[k] : gridn[ii][jj][kk].array[k]);
		}

	    }
	}
      grid = gridn.copy ();
//      rc = output ( gridh, fx, fy, timestep, "hout_2d_");
#else /* First order */


#ifdef TWODIM
      for (jj = 2; jj < ny - 1; jj++)
#endif /* TWODIM */
	{
	  for (ii = 2; ii < nx - 1; ii++)
	    {
	      rc =
		flux (grid, fx[ii][jj][kk].array, xResState[ii][jj][kk].array,dtodx,
		      ii, jj, timestep, 1, 0);
#ifdef TWODIM
	      rc =
		flux (grid, fy[ii][jj][kk].array, yResState[ii][jj][kk].array,dtodx,
		      ii, jj, timestep, 2, 0);
#endif /* TWODIM */
	    }
	}

      rc =
	update (gridn, grid, fx, fy, xResState, yResState, del, ii, jj,
		timestep, grid, delta_t, 0);
#ifdef TWODIM
      for (jj = 2; jj < ny - 2; jj++)
#endif /* TWODIM */
	{
	  for (ii = 2; ii < nx - 2; ii++)
	    {

	      for (int k = 0; k < ne; k++)
		{
		  maxvar.array[k] =
		    (maxvar.array[k] >
		     gridn[ii][jj][kk].array[k] ? maxvar.
		     array[k] : gridn[ii][jj][kk].array[k]);
		  minvar.array[k] =
		    (minvar.array[k] <
		     gridn[ii][jj][kk].array[k] ? minvar.
		     array[k] : gridn[ii][jj][kk].array[k]);
		}
	    }
	}
      grid = gridn.copy ();
#endif
      /* Boundary Conditions */
      rc = boundary (grid, inject_jet);
      /* End Boundary Conditions */

      cout << setiosflags (ios::fixed);
      for (int k = 0; k < ne; k++)
	{
	  cout << k +
	    1 << " " << maxvar.array[k] << " " << minvar.array[k] << endl;
	}


      if (timestep % printtime == 0)
	{
	  // cout << "outputting " << endl;
	  rc = output (grid, fx, fy, timestep, "out_2d_");
	}
      if (time > maxtime)
	{
	  rc = output (grid, fx, fy, timestep, "out_2d_");
	  break;
	}

      /* ------------- */
      /* End of loop through timestep */
    }
  /* ------------- */





  end = clock ();
  elapsed = ((double) (end - start)) / CLOCKS_PER_SEC;
  cout << "\n\n Elapsed time = " << elapsed << " sec\n\n" << endl;


  return 0;
}
示例#18
0
void Tri2dFCBlockSolver::output(const int& nBlocks,
				const int& step)
{
  // create output directory for this unsteady step
  stringstream a;
  a.str("");
  a.clear();
  a << "mkdir -p output." << step;
  system(a.str().c_str());
  a.str("");
  a.clear();


  // output solution file
  if (nOutputVars > 0){

    // determine error at the plotting points
    Array2D<double> qe(nNode,nq),er(nNode,nq);
    sys->initFlow(nNode,
		  &x(0,0),
		  &qe(0,0));

    for (int n=0; n<nNode; n++)
      for (int k=0; k<nq; k++) er(n,k) =(q(n,k)-qe(n,k))/rmsNorm(k);


    // output the step header information if on block 0
    if (ID == 0){
      ofstream ffile;
      a << "triSolution" << step << ".pvtu";
      ffile.open (a.str().c_str());
      a.str("");
      a.clear();
      ffile.setf(ios::scientific);
      ffile.precision(14);
      
      ffile << "<?xml version=\"1.0\"?>"
	    << endl
	    << "<VTKFile type=\"PUnstructuredGrid\" "
	    << "version=\"0.1\" byte_order=\"LittleEndian\">"
	    << endl
	    << "<PUnstructuredGrid GhostLevel=\"0\">"
	    << endl
	    << "<PPointData ";
      
      if (nOutputScalars > 0){
	ffile << "Scalars=\"";
	int k=0;
	for (int n=0; n<nOutputVars; n++)
	  if (outputVarLength(n) == 1){
	    if (k != 0) ffile << " ";
	    k++;
	    ffile << outputVars(n);
	  }
	ffile << "\"";
	if (nOutputVectors > 0) ffile << " ";
      }
      
      if (nOutputVectors > 0){
	ffile << "Vectors=\"";
	int k=0;
	for (int n=0; n<nOutputVars; n++)
	  if (outputVarLength(n) == 3){
	    if (k != 0) ffile << " ";
	    k++;
	    ffile << outputVars(n);
	  }
	ffile << "\"";
      }
      
      ffile << ">"
	    << endl;
      
      for (int n=0; n<nOutputVars; n++){
	ffile << "<PDataArray type=\"Float32\" Name=\""
	      << outputVars(n) << "\"";
	if (outputVarLength(n) == 3) ffile << " NumberOfComponents=\"3\"";
	ffile << "/>"
	      << endl;
      }
      
      ffile << "</PPointData>"
	    << endl
	    << "<PPoints>"
	    << endl
	    << "<PDataArray type=\"Float32\" NumberOfComponents=\"3\"/>"
	    << endl
	    << "</PPoints>"
	    << endl;
      
      for (int n=0; n<nBlocks; n++){
	a << "<Piece Source=\"output." << step
	  << "/triSolutionBlock" << n << ".vtu\"/>";
	ffile << a.str()
	      << endl;
	a.str("");
	a.clear();
      }
      
      ffile << "</PUnstructuredGrid>"
	    << endl
	    << "</VTKFile>"
	    << endl;
      
      ffile.close();
    }


    // write data to file for this block
    ofstream ffile;
    a << "triSolutionBlock" << ID << ".vtu";
    ffile.open (a.str().c_str());
    ffile.setf(ios::scientific);
    ffile.precision(14);
    a.str("");
    a.clear();
    
    ffile << "<?xml version=\"1.0\"?>"
	  << endl
	  <<  "<VTKFile type=\"UnstructuredGrid\" version=\"0.1"
	  <<"\" byte_order=\"LittleEndian\">"
	  << endl
	  << "<UnstructuredGrid>"
	  << endl
	  << "<Piece NumberOfPoints=\""
	  << nNode
	  <<"\" NumberOfCells=\""
	  << nTri << "\">"
	  << endl
	  << "<PointData ";
    if (nOutputScalars > 0){
      ffile << "Scalars=\"";
      int k=0;
      for (int n=0; n<nOutputVars; n++)
	if (outputVarLength(n) == 1){
	  if (k != 0) ffile << " ";
	  k++;
	  ffile << outputVars(n);
	}
      ffile << "\"";
      if (nOutputVectors > 0) ffile << " ";
    }
    
    if (nOutputVectors > 0){
      ffile << "Vectors=\"";
      int k=0;
      for (int n=0; n<nOutputVars; n++)
	if (outputVarLength(n) == 3){
	  if (k != 0) ffile << " ";
	  k++;
	  ffile << outputVars(n);
	}
      ffile << "\"";
    }
    
    ffile << ">"
	  << endl;
    
    for (int n=0; n<nOutputVars; n++){
      if (outputVarLength(n) == 1){
	ffile << "<DataArray type=\"Float32\" Name=\""
	      << outputVars(n)
	      << "\" format=\"ascii\">"
	      << endl;
      }
      else{
	ffile << "<DataArray type=\"Float32\" Name=\""
	      << outputVars(n)
	      << "\" NumberOfComponents=\"3\" format=\"ascii\">" 
	      << endl;
      }
      sys->outputSolution(nNode,
			  ffile,
			  outputVars(n),
			  &q(0,0),
			  &qa(0,0),
			  &er(0,0),
			  &r(0,0));
      ffile << "</DataArray>" << endl;
    }
    
    ffile << "</PointData>" << endl
	  << "<Points>" << endl
	  << "<DataArray type=\"Float32\" NumberOfComponents"
	  << "=\"3\" format=\"ascii\">" << endl;
    double xn,yn,eps=1.e-14;
    for (int n=0; n<nNode; n++){
      xn = x(n,0);
      yn = x(n,1);
      if (fabs(x(n,0)) < eps) xn = 0.;
      if (fabs(x(n,1)) < eps) yn = 0.;
      ffile << xn << "\t" << yn << "\t" << 0. << endl;
    }
    ffile << "</DataArray>" << endl
	  << "</Points>" << endl
	  << "<Cells>" << endl
	  << "<DataArray type=\"Int32\" Name=\"connectivity\" format=\"ascii\">"
	  << endl;
    for (int n=0; n<nTri; n++)
      ffile << tri(n,0) << "\t"
	    << tri(n,1) << "\t"
	    << tri(n,2) << endl;
    ffile << "</DataArray>" << endl
	  <<"<DataArray type=\"Int32\" Name=\"offsets\" format=\"ascii\">"
	  << endl;
    int k = 0;
    for (int n=0; n<nTri; n++){
      k += 3;
      ffile << k << endl;
    }
    ffile << "</DataArray>" << endl
	  << "<DataArray type=\"Int32\" Name=\"types\" format=\"ascii\">"
	  << endl;
    for (int n=0; n<nTri; n++) ffile << 5 << endl;
    ffile << "</DataArray>" << endl
	  << "</Cells>" << endl
	  << "</Piece>" << endl
	  << "</UnstructuredGrid>" << endl
	  << "</VTKFile>" << endl;
    ffile.close();

    a << "mv triSolutionBlock" << ID << ".vtu output." << step;
    system(a.str().c_str());
    a.str("");
    a.clear();


    // output L2 norm of solution error
    if (iErrFile != 0){
      ofstream efile;
      //a << "triErrorBlock" << ID << ".dat";
      a << "error.dat";
      efile.open(a.str().c_str(),ios::app);
      efile.setf(ios::scientific);
      efile.precision(14);
      a.str("");
      a.clear();

      double erms[nq];
      for (int k=0; k<nq; k++) erms[k] = 0.;
      for (int n=0; n<nNode; n++)
	for (int k=0; k<nq; k++) erms[k] += pow(er(n,k),2);
      for (int k=0; k<nq; k++) erms[k] = sqrt(erms[k]/(double)nNode);
      efile << sqrt((double)nNode) << "\t";
      for (int k=0; k<nq; k++) efile << erms[k] << "\t";
      efile << endl;
      efile.close();

      //a << "mv triErrorBlock" << ID << ".dat output." << step;
      //system(a.str().c_str());
      //a.str("");
      //a.clear();

      cout.setf(ios::scientific);
      cout << "\nError statistics: " << endl;
      cout << sqrt((double)nNode) << "\t";
      for (int k=0; k<nq; k++) cout << erms[k] << "\t";
      cout << endl;
    }


    // deallocate work arrays
    qe.deallocate();
    er.deallocate();
  }


  // output surface data to file
  if (iSurfFile != 0){
    ofstream sfile;
    a << "triSurfaceBlock" << ID << ".dat";
    if (step == 0) sfile.open (a.str().c_str());
    else sfile.open (a.str().c_str(),ios::app);
    sfile.setf(ios::scientific);
    sfile.precision(14);
    a.str("");
    a.clear();

    Array3D<double> qax;
    qax.allocate(nNode,2,nqa);
    gradient(nq,&q(0,0),&qx(0,0,0));
    gradient(nqa,&qa(0,0),&qax(0,0,0));

    int m=0;
    for (int n=nNode-nNodeBd; n<nNode; n++)
      sys->outputSurfaceSolution(1,
				 sfile,
				 &nodeBd(m++),
				 &x(n,0),
				 &x(n,1),
				 &q(n,0),
				 &qa(n,0),
				 &qx(n,0,0),
				 &qx(n,1,0),
				 &qax(n,0,0),
				 &qax(n,1,0));

    qax.deallocate();

    a << "mv triSurfaceBlock" << ID << ".dat output." << step;
    system(a.str().c_str());
    a.str("");
    a.clear();

    ofstream ffile; //force file
    a << "forces.dat";
    if (step == 0) ffile.open (a.str().c_str());
    else ffile.open (a.str().c_str(),ios::app);
    ffile.setf(ios::scientific);
    ffile.precision(14);
    a.str("");
    a.clear();

    forcex = 0.;
    forcey = 0.;
    int jj,nn,tag;
    double dd,dx,dy,
      qQ[nq],qxQ[nq],qyQ[nq],qaQ[nqa],qaxQ[nqa],qayQ[nqa],force[ndim];
    for (int n=0; n<nEdgeQ; n++){
      nn  = edgeQ(n,0); // element on which lies this edge
      jj  = edgeQ(n,1); // edge number on the element
      tag = edgeQ(n,2); // boundary tag for the edge
      for (int i=0; i<nsq; i++){ //for each surface quadrature point

	// interpolate solution and gradients to the quadrature points
	for (int k=0; k<nq ; k++) qQ[k]   = 0.;
	for (int k=0; k<nq ; k++) qxQ[k]  = 0.;
	for (int k=0; k<nq ; k++) qyQ[k]  = 0.;
	for (int k=0; k<nqa; k++) qaQ[k]  = 0.;
	for (int k=0; k<nqa; k++) qaxQ[k] = 0.;
	for (int k=0; k<nqa; k++) qayQ[k] = 0.;
	for (int j=0; j<nne; j++){
	  dd = gxS(n,i,j,0);
	  dx = gxS(n,i,j,1);
	  dy = gxS(n,i,j,2);
	  m  = elem(nn,j);
	  for (int k=0; k<nq ; k++){
	    qQ  [k] += q (m,k)*dd;
	    qxQ [k] += q (m,k)*dx;
	    qyQ [k] += q (m,k)*dy;
	  }
	  for (int k=0; k<nqa; k++){
	    qaQ [k] += qa(m,k)*dd;
	    qaxQ[k] += qa(m,k)*dx;
	    qayQ[k] += qa(m,k)*dy;
	  }}

	// compute surface force contribution
	sys->outputSurfaceForces(1,
				 &tag,
				 &nxQ(n,i,0),
				 &qQ[0],
				 &qxQ[0],
				 &qyQ[0],
				 &qaQ[0],
				 &qaxQ[0],
				 &qayQ[0],
				 &force[0]);
	forcex += force[0];
	forcey += force[1];
      }}

    cout << "\nx-component force: " << forcex << endl;
    cout <<   "y-component force: " << forcey << endl;
    cout << "\n";
    ffile << step << " " << double(step)*dtUnsteady
	  << " " << forcex << " " << forcey << endl;
    ffile.close();
  }
}