TEUCHOS_UNIT_TEST_TEMPLATE_4_DECL(SubBlockAFactory, Constructor, Scalar, LocalOrdinal, GlobalOrdinal, Node)
  {
#   include "MueLu_UseShortNames.hpp"
    MUELU_TESTING_SET_OSTREAM;
    MUELU_TESTING_LIMIT_SCOPE(Scalar,GlobalOrdinal,NO);
    out << "version: " << MueLu::Version() << std::endl;

    RCP<const Teuchos::Comm<int> > comm = TestHelpers::Parameters::getDefaultComm();

    /**********************************************************************************/
    /* CREATE INITIAL MATRIX                                                          */
    /**********************************************************************************/
    RCP<const Map> bigMap;
    RCP<const Map> map1;
    RCP<const Map> map2;
    GO numElements = 500;
    GO numElements1 = 400;
    GO numElements2 = 100;

    Xpetra::UnderlyingLib lib = MueLuTests::TestHelpers::Parameters::getLib();

    std::vector<size_t> stridingInfo;
    stridingInfo.push_back(1);

    map1   = StridedMapFactory::Build(lib, numElements1, 0, stridingInfo, comm);
    map2   = StridedMapFactory::Build(lib, numElements2, numElements1, stridingInfo, comm);

    std::vector<GlobalOrdinal> localGids; // vector with all local GIDs on cur proc
    Teuchos::ArrayView< const GlobalOrdinal > map1eleList = map1->getNodeElementList(); // append all local gids from map1 and map2
    localGids.insert(localGids.end(), map1eleList.begin(), map1eleList.end());
    Teuchos::ArrayView< const GlobalOrdinal > map2eleList = map2->getNodeElementList();
    localGids.insert(localGids.end(), map2eleList.begin(), map2eleList.end());
    Teuchos::ArrayView<GlobalOrdinal> eleList(&localGids[0],localGids.size());
    bigMap = MapFactory::Build(lib, numElements, eleList, 0, comm); // create full big map (concatenation of map1 and map2)

    std::vector<Teuchos::RCP<const Map> > maps;
    maps.push_back(map1); maps.push_back(map2);

    Teuchos::RCP<const Xpetra::MapExtractor<Scalar, LO, GO, Node> > mapExtractor = Xpetra::MapExtractorFactory<Scalar,LO,GO,Node>::Build(bigMap, maps);

    RCP<CrsMatrixWrap> Op11 = GenerateProblemMatrix<Scalar, LO, GO, Node>(map1,2,-1,-1);
    RCP<CrsMatrixWrap> Op22 = GenerateProblemMatrix<Scalar, LO, GO, Node>(map2,3,-2,-1);

    // build blocked operator
    Teuchos::RCP<Xpetra::BlockedCrsMatrix<Scalar,LO,GO,Node> > bOp = Teuchos::rcp(new Xpetra::BlockedCrsMatrix<Scalar,LO,GO,Node>(mapExtractor,mapExtractor,10));

    Teuchos::RCP<Xpetra::CrsMatrix<Scalar,LO,GO,Node> > crsMat11 = Op11->getCrsMatrix();
    Teuchos::RCP<Xpetra::CrsMatrix<Scalar,LO,GO,Node> > crsMat22 = Op22->getCrsMatrix();
    bOp->setMatrix(0,0,crsMat11);
    bOp->setMatrix(1,1,crsMat22);
    bOp->fillComplete();
    TEST_EQUALITY(bOp!=Teuchos::null, true);

    // build hierarchy
    RCP<Level> levelOne = rcp(new Level());
#ifdef HAVE_MUELU_TIMER_SYNCHRONIZATION
    levelOne->SetComm(comm);
#endif
    levelOne->Set("A", Teuchos::rcp_dynamic_cast<Matrix>(bOp)); // set blocked operator

    // define sub block factories for blocked operator "A"
    RCP<SubBlockAFactory> A11Fact = Teuchos::rcp(new SubBlockAFactory());
    A11Fact->SetFactory("A",MueLu::NoFactory::getRCP());
    A11Fact->SetParameter("block row",Teuchos::ParameterEntry(0));
    A11Fact->SetParameter("block col",Teuchos::ParameterEntry(0));
    RCP<SubBlockAFactory> A22Fact = Teuchos::rcp(new SubBlockAFactory());
    A22Fact->SetFactory("A",MueLu::NoFactory::getRCP());
    A22Fact->SetParameter("block row",Teuchos::ParameterEntry(1));
    A22Fact->SetParameter("block col",Teuchos::ParameterEntry(1));

    // request subblocks of A
    levelOne->Request("A", A11Fact.get(), MueLu::NoFactory::get());
    levelOne->Request("A", A22Fact.get(), MueLu::NoFactory::get());
    TEST_EQUALITY(levelOne->IsRequested("A", A11Fact.get()),true);
    TEST_EQUALITY(levelOne->IsRequested("A", A22Fact.get()),true);

    RCP<Matrix> A11 = levelOne->Get<RCP<Matrix> >("A",A11Fact.get());
    RCP<Matrix> A22 = levelOne->Get<RCP<Matrix> >("A",A22Fact.get());
    TEST_EQUALITY(levelOne->IsAvailable("A", A11Fact.get()),true);
    TEST_EQUALITY(levelOne->IsAvailable("A", A22Fact.get()),true);

    levelOne->Release("A", A11Fact.get());
    levelOne->Release("A", A22Fact.get());

    TEST_EQUALITY(levelOne->IsAvailable("A", A11Fact.get()),false);
    TEST_EQUALITY(levelOne->IsAvailable("A", A22Fact.get()),false);
    TEST_EQUALITY(A11->getRowMap()->isSameAs(*(Op11->getRowMap())), true);
    TEST_EQUALITY(A11->getColMap()->isSameAs(*(Op11->getColMap())), true);
    TEST_EQUALITY(A11->getRangeMap()->isSameAs(*(Op11->getRangeMap())), true);
    TEST_EQUALITY(A11->getDomainMap()->isSameAs(*(Op11->getDomainMap())), true);
    TEST_EQUALITY(A11->getNodeNumEntries(),Op11->getNodeNumEntries());
    TEST_EQUALITY(A22->getRowMap()->isSameAs(*(Op22->getRowMap())), true);
    TEST_EQUALITY(A22->getColMap()->isSameAs(*(Op22->getColMap())), true);
    TEST_EQUALITY(A22->getRangeMap()->isSameAs(*(Op22->getRangeMap())), true);
    TEST_EQUALITY(A22->getDomainMap()->isSameAs(*(Op22->getDomainMap())), true);
    TEST_EQUALITY(A22->getNodeNumEntries(),Op22->getNodeNumEntries());

    //levelOne->print(out,Teuchos::VERB_EXTREME);
  } //Constructor
Пример #2
0
int main (int argc, char* argv[])
{
    ApplicationsLib::LogogSetup logog_setup;

    TCLAP::CmdLine cmd("Edit material IDs of mesh elements.", ' ', "0.1");
    TCLAP::SwitchArg replaceArg("r", "replace", "replace material IDs", false);
    TCLAP::SwitchArg condenseArg("c", "condense", "condense material IDs", false);
    TCLAP::SwitchArg specifyArg("s", "specify", "specify material IDs by element types (-e)", false);
    std::vector<TCLAP::Arg*> vec_xors;
    vec_xors.push_back(&replaceArg);
    vec_xors.push_back(&condenseArg);
    vec_xors.push_back(&specifyArg);
    cmd.xorAdd(vec_xors);
    TCLAP::ValueArg<std::string> mesh_in("i", "mesh-input-file",
                                         "the name of the file containing the input mesh", true,
                                         "", "file name");
    cmd.add(mesh_in);
    TCLAP::ValueArg<std::string> mesh_out("o", "mesh-output-file",
                                          "the name of the file the mesh will be written to", true,
                                          "", "file name");
    cmd.add(mesh_out);
    TCLAP::MultiArg<unsigned> matIDArg("m", "current-material-id",
                                          "current material id to be replaced", false, "number");
    cmd.add(matIDArg);
    TCLAP::ValueArg<unsigned> newIDArg("n", "new-material-id",
                                          "new material id", false, 0, "number");
    cmd.add(newIDArg);
    std::vector<std::string> eleList(MeshLib::getMeshElemTypeStringsShort());
    TCLAP::ValuesConstraint<std::string> allowedVals(eleList);
    TCLAP::ValueArg<std::string> eleTypeArg("e", "element-type",
                                          "element type", false, "", &allowedVals);
    cmd.add(eleTypeArg);

    cmd.parse(argc, argv);

    if (!replaceArg.isSet() && !condenseArg.isSet() && !specifyArg.isSet()) {
        INFO("Please select editing mode: -r or -c or -s");
        return 0;
    } else if (replaceArg.isSet() && condenseArg.isSet()) {
        INFO("Please select only one editing mode: -r or -c or -s");
        return 0;
    } else if (replaceArg.isSet()) {
        if (!matIDArg.isSet() || !newIDArg.isSet()) {
            INFO("current and new material IDs must be provided for replacement");
            return 0;
        }
    } else if (specifyArg.isSet()) {
        if (!eleTypeArg.isSet() || !newIDArg.isSet()) {
            INFO("element type and new material IDs must be provided to specify elements");
            return 0;
        }
    }

    std::unique_ptr<MeshLib::Mesh> mesh(
        MeshLib::IO::readMeshFromFile(mesh_in.getValue()));
    INFO("Mesh read: %d nodes, %d elements.", mesh->getNNodes(), mesh->getNElements());

    if (condenseArg.isSet()) {
        INFO("Condensing material ID...");
        MeshLib::ElementValueModification::condense(*mesh);
    } else if (replaceArg.isSet()) {
        INFO("Replacing material ID...");
        const auto vecOldID = matIDArg.getValue();
        const unsigned newID = newIDArg.getValue();
        for (auto oldID : vecOldID) {
            INFO("%d -> %d", oldID, newID);
            MeshLib::ElementValueModification::replace(*mesh, oldID, newID, true);
        }
    } else if (specifyArg.isSet()) {
        INFO("Specifying material ID...");
        const std::string eleTypeName(eleTypeArg.getValue());
        const MeshLib::MeshElemType eleType = MeshLib::String2MeshElemType(eleTypeName);
        const unsigned newID = newIDArg.getValue();
        unsigned cnt = MeshLib::ElementValueModification::setByElementType(*mesh, eleType, newID);
        INFO("updated %d elements", cnt);
    }

    MeshLib::IO::writeMeshToFile(*mesh, mesh_out.getValue());

    return EXIT_SUCCESS;
}