// @HEADER // ***************************************************************************** // Panzer: A partial differential equation assembly // engine for strongly coupled complex multiphysics systems // // Copyright 2011 NTESS and the Panzer contributors. // SPDX-License-Identifier: BSD-3-Clause // ***************************************************************************** // @HEADER #include #include #include #include using Teuchos::RCP; using Teuchos::rcp; #include "Teuchos_DefaultComm.hpp" #include "Teuchos_GlobalMPISession.hpp" #include "Panzer_STK_Version.hpp" #include "PanzerAdaptersSTK_config.hpp" #include "Panzer_STK_Interface.hpp" #include "Panzer_STK_SquareQuadMeshFactory.hpp" #include "Panzer_STK_SetupUtilities.hpp" #include "Panzer_STK_WorksetFactory.hpp" #include "Panzer_Workset_Builder.hpp" #include "Panzer_FieldManagerBuilder.hpp" #include "Panzer_STKConnManager.hpp" #include "Panzer_DOFManagerFactory.hpp" #include "Panzer_BlockedEpetraLinearObjFactory.hpp" #include "Panzer_GlobalData.hpp" #include "Panzer_ResponseEvaluatorFactory_Functional.hpp" #include "user_app_EquationSetFactory.hpp" #include "user_app_STKClosureModel_Factory_TemplateBuilder.hpp" #include "user_app_BCStrategy_Factory.hpp" #include "Panzer_ResponseLibrary.hpp" #include "Panzer_WorksetContainer.hpp" #include "TestEvaluators.hpp" #include #include #include #include "Panzer_TypeAssocMap.hpp" #include "Sacado_mpl_vector.hpp" using Teuchos::RCP; namespace panzer { void testInitialzation(const Teuchos::RCP& ipb, std::vector& bcs); std::pair >,RCP > > buildResponseLibrary( std::vector > & physics_blocks, panzer::ClosureModelFactory_TemplateManager & cm_factory, Teuchos::ParameterList & closure_models, Teuchos::ParameterList & user_data); struct Builder { template std::string build() const { return "other"; } }; template < > std::string Builder::build() const { return "Sint"; } template < > std::string Builder::build() const { return "Sshort"; } template < > std::string Builder::build() const { return "Schar"; } TEUCHOS_UNIT_TEST(type_assoc_map, test) { typedef Sacado::mpl::vector VecType; TypeAssocMap tMap; Builder builder; tMap.buildObjects(builder); TEST_EQUALITY(tMap.get(),"Schar"); TEST_EQUALITY(tMap.get(),"Sshort"); tMap.set("not char"); TEST_EQUALITY(tMap.get(),"not char"); } struct RespFactoryFunc_Builder { MPI_Comm comm; Teuchos::RCP > linearObjFactory; Teuchos::RCP globalIndexer; template Teuchos::RCP build() const { return Teuchos::rcp(new ResponseEvaluatorFactory_Functional(comm,1,true,"",linearObjFactory)); } }; TEUCHOS_UNIT_TEST(response_library2, test) { std::vector > physics_blocks; panzer::ClosureModelFactory_TemplateManager cm_factory; Teuchos::ParameterList closure_models("Closure Models"); Teuchos::ParameterList user_data("User Data"); // setup and evaluate ResponseLibrary /////////////////////////////////////////////////// out << "Adding responses" << std::endl; std::pair< RCP >, RCP > > data = buildResponseLibrary(physics_blocks,cm_factory,closure_models,user_data); RCP > rLibrary = data.first; RCP > lof = data.second; RespFactoryFunc_Builder builder; builder.comm = MPI_COMM_WORLD; std::vector blocks(1); blocks[0] = "eblock-0_0"; rLibrary->addResponse("FIELD_A",blocks,builder); blocks[0] = "eblock-1_0"; rLibrary->addResponse("FIELD_B",blocks,builder); Teuchos::RCP tResp = rLibrary->getResponse("FIELD_A"); Teuchos::RCP iResp = rLibrary->getResponse("FIELD_B"); TEST_ASSERT(tResp!=Teuchos::null); TEST_ASSERT(iResp!=Teuchos::null); TEST_EQUALITY(tResp->getName(),"FIELD_A"); TEST_EQUALITY(iResp->getName(),"FIELD_B"); TEST_EQUALITY(tResp->getLookupName(),"RESPONSE_FIELD_A"); TEST_EQUALITY(iResp->getLookupName(),"RESPONSE_FIELD_B"); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(tResp,true)); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(iResp,true)); RCP eVec; RCP > tVec; { RCP map = Teuchos::rcp_dynamic_cast >(iResp)->getMap(); RCP > vs = Teuchos::rcp_dynamic_cast >(tResp)->getVectorSpace(); eVec = Teuchos::rcp(new Epetra_Vector(*map)); // eVec->PutScalar(0.0); // TEST_EQUALITY(eVec->MyLength(),1); tVec = Thyra::createMember(vs); // Thyra::assign(tVec.ptr(),0.0); Teuchos::rcp_dynamic_cast >(iResp)->setVector(eVec); Teuchos::rcp_dynamic_cast >(tResp)->setVector(tVec); // test epetra or thyra only logic TEST_THROW(Teuchos::rcp_dynamic_cast >(iResp)->setVector(tVec),std::logic_error); TEST_THROW(Teuchos::rcp_dynamic_cast >(tResp)->setVector(eVec),std::logic_error); } std::vector > v; v.push_back(Teuchos::null); rLibrary->getResponses(v); TEST_EQUALITY(v.size(),2); TEST_ASSERT(v[0]->getName()=="FIELD_A" || v[0]->getName()=="FIELD_B"); TEST_ASSERT(v[1]->getName()=="FIELD_A" || v[1]->getName()=="FIELD_B"); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(v[0],true)); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(v[1],true)); TEST_ASSERT(!rLibrary->responseEvaluatorsBuilt()); rLibrary->buildResponseEvaluators(physics_blocks, cm_factory, closure_models, user_data,true); TEST_ASSERT(rLibrary->responseEvaluatorsBuilt()); Teuchos::RCP loc = lof->buildLinearObjContainer(); lof->initializeContainer(panzer::LinearObjContainer::X,*loc); Teuchos::RCP gloc = lof->buildGhostedLinearObjContainer(); lof->initializeGhostedContainer(panzer::LinearObjContainer::X,*gloc); panzer::AssemblyEngineInArgs ae_inargs(gloc,loc); rLibrary->addResponsesToInArgs(ae_inargs); rLibrary->evaluate(ae_inargs); Teuchos::ArrayRCP tData; Teuchos::rcp_dynamic_cast >(tVec)->getNonconstLocalData(Teuchos::outArg(tData)); double iValue = -2.3; double tValue = 82.9; TEST_FLOATING_EQUALITY((*eVec)[0],0.5*iValue,1e-14); TEST_FLOATING_EQUALITY(tData[0],0.5*tValue,1e-14); } TEUCHOS_UNIT_TEST(response_library2, test_surface) { std::vector > physics_blocks; panzer::ClosureModelFactory_TemplateManager cm_factory; Teuchos::ParameterList closure_models("Closure Models"); Teuchos::ParameterList user_data("User Data"); // setup and evaluate ResponseLibrary /////////////////////////////////////////////////// out << "Adding responses" << std::endl; std::pair< RCP >, RCP > > data = buildResponseLibrary(physics_blocks,cm_factory,closure_models,user_data); RCP > rLibrary = data.first; RCP > lof = data.second; RCP globalIndexer = user_data.sublist("Panzer Data").get >("DOF Manager"); RespFactoryFunc_Builder builder; builder.comm = MPI_COMM_WORLD; builder.linearObjFactory = lof; builder.globalIndexer = globalIndexer; std::vector blocks(1); blocks[0] = "eblock-0_0"; rLibrary->addResponse("FIELD_A",blocks,builder); builder.linearObjFactory = Teuchos::null; builder.globalIndexer = Teuchos::null; std::vector > sidesets; sidesets.push_back(std::make_pair("bottom","eblock-0_0")); // 0.5 sidesets.push_back(std::make_pair("top","eblock-0_0")); // 0.5 sidesets.push_back(std::make_pair("right","eblock-1_0")); // 1.0 rLibrary->addResponse("FIELD_B",sidesets,builder); Teuchos::RCP blkResp = rLibrary->getResponse("FIELD_A"); Teuchos::RCP ssResp = rLibrary->getResponse("FIELD_B"); Teuchos::RCP blkRespJac = rLibrary->getResponse("FIELD_A"); TEST_ASSERT(blkRespJac!=Teuchos::null); // no response should be build for this one TEST_ASSERT(rLibrary->getResponse("FIELD_B")==Teuchos::null); RCP eVec, eVec2; { RCP map = Teuchos::rcp_dynamic_cast >(ssResp)->getMap(); eVec = Teuchos::rcp(new Epetra_Vector(*map)); eVec2 = Teuchos::rcp(new Epetra_Vector(*map)); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(blkResp)->setVector(eVec)); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(ssResp)->setVector(eVec2)); RCP dVec = Teuchos::rcp_dynamic_cast >(blkRespJac,true)->buildEpetraDerivative(); TEST_NOTHROW(Teuchos::rcp_dynamic_cast >(blkRespJac,true)->setDerivative(dVec)); } rLibrary->buildResponseEvaluators(physics_blocks, cm_factory, closure_models, user_data,true); TEST_ASSERT(rLibrary->responseEvaluatorsBuilt()); Teuchos::RCP loc = lof->buildLinearObjContainer(); lof->initializeContainer(panzer::LinearObjContainer::X,*loc); Teuchos::RCP gloc = lof->buildGhostedLinearObjContainer(); lof->initializeGhostedContainer(panzer::LinearObjContainer::X,*gloc); { panzer::AssemblyEngineInArgs ae_inargs(gloc,loc); rLibrary->addResponsesToInArgs(ae_inargs); rLibrary->evaluate(ae_inargs); } // evaluate derivatives { panzer::AssemblyEngineInArgs ae_inargs(gloc,loc); rLibrary->addResponsesToInArgs(ae_inargs); rLibrary->evaluate(ae_inargs); } double iValue = -2.3; double tValue = 82.9; TEST_FLOATING_EQUALITY((*eVec)[0],0.5*tValue,1e-14); TEST_FLOATING_EQUALITY((*eVec2)[0],2.0*iValue,1e-14); } void testInitialzation(const Teuchos::RCP& ipb, std::vector& bcs) { // Physics block Teuchos::ParameterList& physics_block = ipb->sublist("test physics"); { Teuchos::ParameterList& p = physics_block.sublist("a"); p.set("Type","Energy"); p.set("Prefix",""); p.set("Model ID","solid"); p.set("Basis Type","HGrad"); p.set("Basis Order",2); p.set("Integration Order",1); } { Teuchos::ParameterList& p = physics_block.sublist("b"); p.set("Type","Energy"); p.set("Prefix","ION_"); p.set("Model ID","ion solid"); p.set("Basis Type","HGrad"); p.set("Basis Order",1); p.set("Integration Order",1); } { std::size_t bc_id = 0; panzer::BCType neumann = BCT_Dirichlet; std::string sideset_id = "left"; std::string element_block_id = "eblock-0_0"; std::string dof_name = "TEMPERATURE"; std::string strategy = "Constant"; double value = 5.0; Teuchos::ParameterList p; p.set("Value",value); panzer::BC bc(bc_id, neumann, sideset_id, element_block_id, dof_name, strategy, p); bcs.push_back(bc); } { std::size_t bc_id = 1; panzer::BCType neumann = BCT_Dirichlet; std::string sideset_id = "right"; std::string element_block_id = "eblock-1_0"; std::string dof_name = "TEMPERATURE"; std::string strategy = "Constant"; double value = 5.0; Teuchos::ParameterList p; p.set("Value",value); panzer::BC bc(bc_id, neumann, sideset_id, element_block_id, dof_name, strategy, p); bcs.push_back(bc); } { std::size_t bc_id = 2; panzer::BCType neumann = BCT_Dirichlet; std::string sideset_id = "top"; std::string element_block_id = "eblock-1_0"; std::string dof_name = "TEMPERATURE"; std::string strategy = "Constant"; double value = 5.0; Teuchos::ParameterList p; p.set("Value",value); panzer::BC bc(bc_id, neumann, sideset_id, element_block_id, dof_name, strategy, p); bcs.push_back(bc); } } std::pair >,RCP > > buildResponseLibrary( std::vector > & physics_blocks, panzer::ClosureModelFactory_TemplateManager & cm_factory, Teuchos::ParameterList & closure_models, Teuchos::ParameterList & user_data) { using Teuchos::RCP; #ifdef HAVE_MPI Teuchos::RCP > tcomm = Teuchos::rcp(new Teuchos::MpiComm(Teuchos::opaqueWrapper(MPI_COMM_WORLD))); #else Teuchos::RCP > tcomm = Teuchos::rcp(new Teuchos::SerialComm); #endif panzer_stk::SquareQuadMeshFactory mesh_factory; Teuchos::RCP eqset_factory = Teuchos::rcp(new user_app::MyFactory); user_app::BCFactory bc_factory; const std::size_t workset_size = 20; panzer::FieldManagerBuilder fmb; // setup mesh ///////////////////////////////////////////// RCP mesh; { RCP pl = rcp(new Teuchos::ParameterList); pl->set("X Blocks",2); pl->set("Y Blocks",1); pl->set("X Elements",4); pl->set("Y Elements",4); mesh_factory.setParameterList(pl); mesh = mesh_factory.buildMesh(MPI_COMM_WORLD); } // setup physic blocks ///////////////////////////////////////////// Teuchos::RCP ipb = Teuchos::parameterList("Physics Blocks"); std::vector bcs; { testInitialzation(ipb, bcs); std::map block_ids_to_physics_ids; block_ids_to_physics_ids["eblock-0_0"] = "test physics"; block_ids_to_physics_ids["eblock-1_0"] = "test physics"; std::map > block_ids_to_cell_topo; block_ids_to_cell_topo["eblock-0_0"] = mesh->getCellTopology("eblock-0_0"); block_ids_to_cell_topo["eblock-1_0"] = mesh->getCellTopology("eblock-1_0"); Teuchos::RCP gd = panzer::createGlobalData(); int default_integration_order = 1; panzer::buildPhysicsBlocks(block_ids_to_physics_ids, block_ids_to_cell_topo, ipb, default_integration_order, workset_size, eqset_factory, gd, false, physics_blocks); } // setup worksets ///////////////////////////////////////////// std::vector validEBlocks; mesh->getElementBlockNames(validEBlocks); // build WorksetContainer Teuchos::RCP wkstFactory = Teuchos::rcp(new panzer_stk::WorksetFactory(mesh)); // build STK workset factory Teuchos::RCP wkstContainer // attach it to a workset container (uses lazy evaluation) = Teuchos::rcp(new panzer::WorksetContainer); wkstContainer->setFactory(wkstFactory); for(size_t i=0;isetNeeds(physics_blocks[i]->elementBlockID(),physics_blocks[i]->getWorksetNeeds()); wkstContainer->setWorksetSize(workset_size); // setup DOF manager ///////////////////////////////////////////// const Teuchos::RCP conn_manager = Teuchos::rcp(new panzer_stk::STKConnManager(mesh)); Teuchos::RCP indexerFactory = Teuchos::rcp(new panzer::DOFManagerFactory); const Teuchos::RCP dofManager = indexerFactory->buildGlobalIndexer(Teuchos::opaqueWrapper(MPI_COMM_WORLD),physics_blocks,conn_manager); // and linear object factory Teuchos::RCP > tComm = Teuchos::rcp(new Teuchos::MpiComm(MPI_COMM_WORLD)); Teuchos::RCP > elof = Teuchos::rcp(new panzer::BlockedEpetraLinearObjFactory(tComm.getConst(),dofManager)); Teuchos::RCP > lof = elof; // setup field manager builder ///////////////////////////////////////////// // Add in the application specific closure model factory user_app::STKModelFactory_TemplateBuilder cm_builder; cm_factory.buildObjects(cm_builder); double iValue = -2.3; double tValue = 82.9; closure_models.sublist("solid").sublist("SOURCE_TEMPERATURE").set("Value",1.0); closure_models.sublist("solid").sublist("FIELD_A").set("Value",tValue); closure_models.sublist("ion solid").sublist("SOURCE_ION_TEMPERATURE").set("Value",1.0); closure_models.sublist("ion solid").sublist("FIELD_B").set("Value",iValue); user_data.sublist("Panzer Data").set("Mesh", mesh); user_data.sublist("Panzer Data").set("DOF Manager", dofManager); user_data.sublist("Panzer Data").set("Linear Object Factory", lof); user_data.set("Workset Size",workset_size); RCP > rLibrary = Teuchos::rcp(new ResponseLibrary(wkstContainer,dofManager,lof)); return std::make_pair(rLibrary,lof); } }