Esempio n. 1
0
void QgsGrassNewMapset::loadRegions()
{
    QgsDebugMsg( "entered." );

    QString path = QgsApplication::pkgDataPath() + "/grass/locations.gml";
    QgsDebugMsg( QString( "load:%1" ).arg( path.toLocal8Bit().constData() ) );

    QFile file( path );

    if ( !file.exists() )
    {
        QMessageBox::warning( 0, tr( "Warning" ),
                              tr( "Regions file (%1) not found." ).arg( path ) );
        return;
    }
    if ( ! file.open( QIODevice::ReadOnly ) )
    {
        QMessageBox::warning( 0, tr( "Warning" ),
                              tr( "Cannot open locations file (%1)" ).arg( path ) );
        return;
    }

    QDomDocument doc( "gml:FeatureCollection" );
    QString err;
    int line, column;

    if ( !doc.setContent( &file,  &err, &line, &column ) )
    {
        QString errmsg = tr( "Cannot read locations file (%1):" ).arg( path )
                         + tr( "\n%1\nat line %2 column %3" ).arg( err ).arg( line ).arg( column );
        QgsDebugMsg( errmsg );
        QMessageBox::warning( 0, tr( "Warning" ), errmsg );
        file.close();
        return;
    }

    QDomElement docElem = doc.documentElement();
    QDomNodeList nodes = docElem.elementsByTagName( "gml:featureMember" );

    for ( int i = 0; i < nodes.count(); i++ )
    {
        QDomNode node = nodes.item( i );

        if ( node.isNull() )
        {
            continue;
        }

        QDomElement elem = node.toElement();
        QDomNodeList nameNodes = elem.elementsByTagName( "gml:name" );
        if ( nameNodes.count() == 0 )
            continue;
        if ( nameNodes.item( 0 ).isNull() )
            continue;

        QDomElement nameElem = nameNodes.item( 0 ).toElement();
        if ( nameElem.text().isNull() )
            continue;

        QDomNodeList envNodes = elem.elementsByTagName( "gml:Envelope" );
        if ( envNodes.count() == 0 )
            continue;
        if ( envNodes.item( 0 ).isNull() )
            continue;
        QDomElement envElem = envNodes.item( 0 ).toElement();

        QDomNodeList coorNodes = envElem.elementsByTagName( "gml:coordinates" );
        if ( coorNodes.count() == 0 )
            continue;
        if ( coorNodes.item( 0 ).isNull() )
            continue;
        QDomElement coorElem = coorNodes.item( 0 ).toElement();
        if ( coorElem.text().isNull() )
            continue;

        QStringList coor = coorElem.text().split( " ", QString::SkipEmptyParts );
        if ( coor.size() != 2 )
        {
            QgsDebugMsg( QString( "Cannot parse coordinates: %1" ).arg( coorElem.text() ) );
            continue;
        }

        QStringList ll = coor[0].split( ",", QString::SkipEmptyParts );
        QStringList ur = coor[1].split( ",", QString::SkipEmptyParts );
        if ( ll.size() != 2 || ur.size() != 2 )
        {
            QgsDebugMsg( QString( "Cannot parse coordinates: %1" ).arg( coorElem.text() ) );
            continue;
        }

        // Add region
        mRegionsComboBox->addItem( nameElem.text() );

        QgsPoint llp( ll[0].toDouble(), ll[1].toDouble() );
        mRegionsPoints.push_back( llp );
        QgsPoint urp( ur[0].toDouble(), ur[1].toDouble() );
        mRegionsPoints.push_back( urp );
    }

    file.close();
}
int main(int argc, char** argv)
{
  
  try
  {
    Sundance::init(&argc, &argv);
    int np = MPIComm::world().getNProc();
    TEUCHOS_TEST_FOR_EXCEPT(np > 1); // works only in serial for now

    /* We will do our linear algebra using Epetra */
    VectorType<double> vecType = new EpetraVectorType();

    /* Create a mesh. This will be our coarsest mesh */
    MeshType meshType = new BasicSimplicialMeshType();
    int nx = 3;

    MeshSource mesher = new PartitionedRectangleMesher(0.0, 1.0, nx, 1,
      0.0, 1.0, nx, 1,
      meshType);
    Mesh coarse = mesher.getMesh();
    /* Label some random vertex. We'll check that the refinement preserves
     * vertex labels */
    coarse.setLabel(0, 3, 5);

    /* Refine the mesh. The URP object will store both meshes as well as
     * maps between cell indices at the two levels. */
    UniformRefinementPair urp(meshType, coarse);
    /* Run a consistency check on the refinement maps */
    int bad = urp.check();

    /* Get the fine mesh */
    const Mesh& fine = urp.fine();

    /* Create some expressions to be tested. The expression is linear
     * and so can be represented exactly with the P1 basis. Interpolation
     * to the fine mesh will also be exact. */
    Expr x = new CoordExpr(0);
    Expr y = new CoordExpr(1);
    Expr f = x + sqrt(2.0)*y;

    BasisFamily P1 = new Lagrange(1);
    
    /* Project a function onto the coarse space */
    DiscreteSpace coarseP1(coarse, P1, vecType);
    L2Projector projCoarseP1(coarseP1, f);
    Expr cP1 = projCoarseP1.project();
    Vector<double> cxP1 = DiscreteFunction::discFunc(cP1)->getVector();
    RCP<DOFMapBase> cDofMap = coarseP1.map();

    /* Project the same function onto the fine space */
    DiscreteSpace fineP1(fine, P1, vecType);
    L2Projector projFineP1(fineP1, f);
    Expr fP1 = projFineP1.project();
    Vector<double> fxP1 = DiscreteFunction::discFunc(fP1)->getVector();
    RCP<DOFMapBase> fDofMap = fineP1.map();

    /* Now we're going to interpolate the coarse function onto the fine
     * space. If the code is correct this will be equal to the function 
     * projected onto the fine mesh. */
    Vector<double> refinedX = fxP1.space().createMember();
    
    /* map the coarse space to the fine space */
    for (int f=0; f<fine.numCells(0); f++)
    {
      /* get the DOF for the node */
      int fDof = getNodeDof(fDofMap, f);
      /* If this fine vertex is on an edge of the coarse mesh, interpolate
       * from the facets of the edge. If the fine vertex is coincident
       * with a coarse vertex, use the value on that vertex */
      if (urp.newVertIsOnEdge()[f]) /* new vertex is on a coarse edge */
      {
        /* get the parent edge's LID in the coarse mesh */
        int cEdge = urp.newVertToOldLIDMap()[f];
        int ori; // dummy orientation, not needed here
        int cNode1 = coarse.facetLID(1, cEdge, 0, 0, ori);
        int cNode2 = coarse.facetLID(1, cEdge, 0, 1, ori);
        int cDof1 = getNodeDof(cDofMap, cNode1);
        int cDof2 = getNodeDof(cDofMap, cNode2);
        refinedX[fDof] = 0.5*(cxP1[cDof1]+cxP1[cDof2]);
      }
      else /* new vertex coincides with an old vertex */
      {
        int cNode = urp.newVertToOldLIDMap()[f];
        int cDof = getNodeDof(cDofMap, cNode);
        refinedX[fDof] = cxP1[cDof];
      }
    }
    
    /* Compare the R-mapped vector to the vector computed natively 
     * on the fine space */
    double err = (fxP1 - refinedX).norm2();
    cout << "error |fine - R*coarse| = " << err << endl;

    
    double finalTol = 0.5;
    Sundance::passFailTest(bad, finalTol);
  }
	catch(std::exception& e)
  {
    Sundance::handleException(e);
  }
  Sundance::finalize(); 
  return Sundance::testStatus(); 
}