79 const Teuchos::ParameterList& p)
80 : globalIndexer_(indexer)
81 , globalDataKey_(
"Residual Scatter Container")
83 std::string scatterName = p.get<std::string>(
"Scatter Name");
85 Teuchos::rcp(
new PHX::Tag<ScalarT>(scatterName,Teuchos::rcp(
new PHX::MDALayout<Dummy>(0))));
88 const std::vector<std::string>& names =
89 *(p.get< Teuchos::RCP< std::vector<std::string> > >(
"Dependent Names"));
92 fieldMap_ = p.get< Teuchos::RCP< std::map<std::string,std::string> > >(
"Dependent Map");
95 scatterIC_ = p.isParameter(
"Scatter Initial Condition") ? p.get<
bool>(
"Scatter Initial Condition") :
false;
97 Teuchos::RCP<PHX::DataLayout> dl = (!scatterIC_) ?
98 p.get< Teuchos::RCP<panzer::PureBasis> >(
"Basis")->functional :
99 p.get< Teuchos::RCP<const panzer::PureBasis> >(
"Basis")->functional;
101 side_subcell_dim_ = p.get<
int>(
"Side Subcell Dimension");
102 local_side_id_ = p.get<
int>(
"Local Side ID");
106 scatterFields_.resize(names.size());
107 for (std::size_t eq = 0; eq < names.size(); ++eq) {
108 scatterFields_[eq] = PHX::MDField<const ScalarT,Cell,NODE>(names[eq],dl);
111 this->addDependentField(scatterFields_[eq]);
114 checkApplyBC_ = p.isParameter(
"Check Apply BC") ? p.get<
bool>(
"Check Apply BC") :
false;
115 applyBC_.resize(names.size());
117 for (std::size_t eq = 0; eq < names.size(); ++eq) {
118 applyBC_[eq] = PHX::MDField<const bool,Cell,NODE>(std::string(
"APPLY_BC_")+fieldMap_->find(names[eq])->second,dl);
119 this->addDependentField(applyBC_[eq]);
124 this->addEvaluatedField(*scatterHolder_);
126 if (p.isType<std::string>(
"Global Data Key"))
127 globalDataKey_ = p.get<std::string>(
"Global Data Key");
129 this->setName(scatterName+
" Scatter Dirichlet Residual");
138 const Workset & workset_0 = (*d.worksets_)[0];
139 const std::string blockId = this->wda(workset_0).
block_id;
141 fieldIds_.resize(scatterFields_.size());
142 fieldOffsets_.resize(scatterFields_.size());
143 basisIndexForMDFieldOffsets_.resize(scatterFields_.size());
144 fieldGlobalIndexers_.resize(scatterFields_.size());
145 productVectorBlockIndex_.resize(scatterFields_.size());
146 int maxElementBlockGIDCount = -1;
147 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
149 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
151 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
152 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
153 fieldGlobalIndexers_[fd] = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
154 fieldIds_[fd] = fieldGlobalIndexers_[fd]->getFieldNum(fieldName);
158 const auto& offsetPair = fieldGlobalIndexers_[fd]->getGIDFieldOffsets_closure(blockId,fieldIds_[fd],side_subcell_dim_,local_side_id_);
160 const auto&
offsets = offsetPair.first;
161 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):fieldOffsets",
offsets.size());
162 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
163 for (std::size_t i=0; i <
offsets.size(); ++i)
165 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
168 const auto& basisIndex = offsetPair.second;
169 basisIndexForMDFieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):basisIndexForMDFieldOffsets",basisIndex.size());
170 auto hostBasisIndex = Kokkos::create_mirror_view(basisIndexForMDFieldOffsets_[fd]);
171 for (std::size_t i=0; i < basisIndex.size(); ++i)
172 hostBasisIndex(i) = basisIndex[i];
173 Kokkos::deep_copy(basisIndexForMDFieldOffsets_[fd], hostBasisIndex);
178 const std::vector<int>&
offsets = fieldGlobalIndexers_[fd]->getGIDFieldOffsets(blockId,fieldIds_[fd]);
179 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Residual):fieldOffsets",
offsets.size());
180 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
181 for (std::size_t i=0; i <
offsets.size(); ++i)
183 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
186 maxElementBlockGIDCount = std::max(fieldGlobalIndexers_[fd]->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
192 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Residual):worksetLIDs",
193 scatterFields_[0].extent(0),
194 maxElementBlockGIDCount);
220 using Teuchos::rcp_dynamic_cast;
224 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
226 RCP<ProductVectorBase<double> > thyraScatterTarget = (!scatterIC_) ?
227 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_f(),
true) :
228 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x(),
true);
231 int currentWorksetLIDSubBlock = -1;
232 for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
234 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
235 fieldGlobalIndexers_[fieldIndex]->getElementLIDs(localCellIds,worksetLIDs_);
236 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
240 auto& tpetraScatterTarget = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(thyraScatterTarget->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
241 const auto& kokkosScatterTarget = tpetraScatterTarget.getLocalViewDevice(Tpetra::Access::ReadWrite);
244 auto& tpetraDirichletCounter = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(dirichletCounter_->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
245 const auto& kokkosDirichletCounter = tpetraDirichletCounter.getLocalViewDevice(Tpetra::Access::ReadWrite);
248 const auto fieldOffsets = fieldOffsets_[fieldIndex];
249 const auto basisIndices = basisIndexForMDFieldOffsets_[fieldIndex];
250 const auto worksetLIDs = worksetLIDs_;
251 const auto fieldValues = scatterFields_[fieldIndex].get_static_view();
252 const auto applyBC = applyBC_[fieldIndex].get_static_view();
257 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
258 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
259 const int lid = worksetLIDs(cell,fieldOffsets(basis));
262 const int basisIndex = basisIndices(basis);
269 kokkosScatterTarget(lid,0) = fieldValues(cell,basisIndex);
270 kokkosDirichletCounter(lid,0) = 1.0;
276 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
277 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
278 const int lid = worksetLIDs(cell,fieldOffsets(basis));
281 kokkosScatterTarget(lid,0) = fieldValues(cell,basis);
282 kokkosDirichletCounter(lid,0) = 1.0;
305 const std::vector<Teuchos::RCP<const panzer::GlobalIndexer> > & colIndexers,
306 const Teuchos::ParameterList& p)
307 : globalIndexer_(indexer)
308 , globalDataKey_(
"Residual Scatter Container")
309 , colGlobalIndexers_(colIndexers)
310 , hasColIndexers_(!colIndexers.empty())
314 if(!hasColIndexers_) {
315 const auto & rowManagers = globalIndexer_->getFieldDOFManagers();
316 colGlobalIndexers_.assign(rowManagers.begin(),rowManagers.end());
319 std::string scatterName = p.get<std::string>(
"Scatter Name");
321 Teuchos::rcp(
new PHX::Tag<ScalarT>(scatterName,Teuchos::rcp(
new PHX::MDALayout<Dummy>(0))));
324 const std::vector<std::string>& names =
325 *(p.get< Teuchos::RCP< std::vector<std::string> > >(
"Dependent Names"));
328 fieldMap_ = p.get< Teuchos::RCP< std::map<std::string,std::string> > >(
"Dependent Map");
330 Teuchos::RCP<PHX::DataLayout> dl =
331 p.get< Teuchos::RCP<panzer::PureBasis> >(
"Basis")->functional;
333 side_subcell_dim_ = p.get<
int>(
"Side Subcell Dimension");
334 local_side_id_ = p.get<
int>(
"Local Side ID");
337 scatterFields_.resize(names.size());
338 for (std::size_t eq = 0; eq < names.size(); ++eq) {
339 scatterFields_[eq] = PHX::MDField<const ScalarT,Cell,NODE>(names[eq],dl);
342 this->addDependentField(scatterFields_[eq]);
345 checkApplyBC_ = p.get<
bool>(
"Check Apply BC");
346 applyBC_.resize(names.size());
348 for (std::size_t eq = 0; eq < names.size(); ++eq) {
349 applyBC_[eq] = PHX::MDField<const bool,Cell,NODE>(std::string(
"APPLY_BC_")+fieldMap_->find(names[eq])->second,dl);
350 this->addDependentField(applyBC_[eq]);
355 this->addEvaluatedField(*scatterHolder_);
357 if (p.isType<std::string>(
"Global Data Key"))
358 globalDataKey_ = p.get<std::string>(
"Global Data Key");
360 this->setName(scatterName+
" Scatter Dirichlet Residual (Jacobian)");
369 const Workset & workset_0 = (*d.worksets_)[0];
370 const std::string blockId = this->wda(workset_0).
block_id;
372 fieldIds_.resize(scatterFields_.size());
373 fieldOffsets_.resize(scatterFields_.size());
374 basisIndexForMDFieldOffsets_.resize(scatterFields_.size());
375 productVectorBlockIndex_.resize(scatterFields_.size());
376 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
378 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
379 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
380 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
381 const auto& fieldGlobalIndexer = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
382 fieldIds_[fd] = fieldGlobalIndexer->getFieldNum(fieldName);
384 const auto& offsetPair = fieldGlobalIndexer->getGIDFieldOffsets_closure(blockId,fieldIds_[fd],side_subcell_dim_,local_side_id_);
386 const auto&
offsets = offsetPair.first;
387 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):fieldOffsets",
offsets.size());
388 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
389 for (std::size_t i=0; i <
offsets.size(); ++i)
391 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
394 const auto& basisIndex = offsetPair.second;
395 basisIndexForMDFieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):basisIndexForMDFieldOffsets",basisIndex.size());
396 auto hostBasisIndex = Kokkos::create_mirror_view(basisIndexForMDFieldOffsets_[fd]);
397 for (std::size_t i=0; i < basisIndex.size(); ++i)
398 hostBasisIndex(i) = basisIndex[i];
399 Kokkos::deep_copy(basisIndexForMDFieldOffsets_[fd], hostBasisIndex);
406 int elementBlockGIDCount = 0;
407 for (
const auto& blockDOFMgr : globalIndexer_->getFieldDOFManagers())
408 elementBlockGIDCount += blockDOFMgr->getElementBlockGIDCount(blockId);
410 worksetLIDs_ = PHX::View<LO**>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):worksetLIDs",
411 scatterFields_[0].extent(0),
412 elementBlockGIDCount);
415 const auto& blockGlobalIndexers = globalIndexer_->getFieldDOFManagers();
416 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
417 blockOffsets_ = PHX::View<LO*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):blockOffsets_",
419 const auto hostBlockOffsets = Kokkos::create_mirror_view(blockOffsets_);
420 for (
int blk=0;blk<numBlocks;blk++) {
421 int blockOffset = globalIndexer_->getBlockGIDOffset(blockId,blk);
422 hostBlockOffsets(blk) = blockOffset;
424 hostBlockOffsets(numBlocks) = hostBlockOffsets(numBlocks-1) + blockGlobalIndexers[blockGlobalIndexers.size()-1]->getElementBlockGIDCount(blockId);
425 Kokkos::deep_copy(blockOffsets_,hostBlockOffsets);
429 if (!hasColIndexers_) {
430 colBlockOffsets_ = blockOffsets_;
431 colWorksetLIDs_ = worksetLIDs_;
434 const int numColBlocks =
static_cast<int>(colGlobalIndexers_.size());
435 colBlockOffsets_ = PHX::View<LO*>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):colBlockOffsets_",
437 auto hostColOffsets = Kokkos::create_mirror_view(colBlockOffsets_);
438 hostColOffsets(0) = 0;
439 for (
int blk=0;blk<numColBlocks;blk++)
440 hostColOffsets(blk+1) = hostColOffsets(blk) + colGlobalIndexers_[blk]->getElementBlockGIDCount(blockId);
441 Kokkos::deep_copy(colBlockOffsets_,hostColOffsets);
443 colWorksetLIDs_ = PHX::View<LO**>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):colWorksetLIDs",
444 scatterFields_[0].extent(0), hostColOffsets(numColBlocks));
449 blockOffsets_h_ = Kokkos::create_mirror_view(blockOffsets_);
450 Kokkos::deep_copy(blockOffsets_h_, blockOffsets_);
451 colBlockOffsets_h_ = Kokkos::create_mirror_view(colBlockOffsets_);
452 Kokkos::deep_copy(colBlockOffsets_h_, colBlockOffsets_);
458 const int numRowBlocks = globalIndexer_->getNumFieldBlocks();
459 const int numColBlocks =
static_cast<int>(colGlobalIndexers_.size());
460 hostJacTpetraBlocks_ =
typename PHX::View<LocalMatrixType**>::host_mirror_type(
461 "ScatterDirichletResidual_BlockedTpetra(Jacobian):hostJacTpetraBlocks", numRowBlocks, numColBlocks);
462 jacTpetraBlocks_ = PHX::View<LocalMatrixType**>(
463 "ScatterDirichletResidual_BlockedTpetra(Jacobian):jacTpetraBlocks", numRowBlocks, numColBlocks);
464 blockExistsInJac_ = PHX::View<int**>(
"ScatterDirichletResidual_BlockedTpetra(Jacobian):blockExistsInJac", numRowBlocks, numColBlocks);
465 hostBlockExistsInJac_ = Kokkos::create_mirror_view(blockExistsInJac_);
471 for (
int blk=0;blk<numBlocks;blk++) {
472 const int blockDerivativeSize = hostBlockOffsets(blk+1) - hostBlockOffsets(blk);
473 TEUCHOS_TEST_FOR_EXCEPTION(blockDerivativeSize > maxDerivativeArraySize_, std::runtime_error,
474 "ERROR: the derivative dimension for sub block "
475 << blk <<
"with a value of " << blockDerivativeSize
476 <<
"is larger than the size allocated for cLIDs and vals "
477 <<
"in the evaluate call! You must manually increase the "
478 <<
"size and recompile!");
505 using Teuchos::rcp_dynamic_cast;
510 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
512 const int numFieldBlocks = globalIndexer_->getNumFieldBlocks();
513 const int numColFieldBlocks =
static_cast<int>(colGlobalIndexers_.size());
514 const RCP<const ContainerType> blockedContainer = blockedContainer_;
515 const RCP<ProductVectorBase<double>> thyraBlockResidual = rcp_dynamic_cast<ProductVectorBase<double>>(blockedContainer_->get_f());
516 const bool haveResidual = Teuchos::nonnull(thyraBlockResidual);
517 const RCP<BlockedLinearOpBase<double>> Jac = rcp_dynamic_cast<BlockedLinearOpBase<double>>(blockedContainer_->get_A(),
true);
524 TEUCHOS_ASSERT(
static_cast<int>(jacTpetraBlocks_.extent(0))==numFieldBlocks &&
525 static_cast<int>(jacTpetraBlocks_.extent(1))==numColFieldBlocks);
526 auto& hostJacTpetraBlocks = hostJacTpetraBlocks_;
527 auto& blockExistsInJac = blockExistsInJac_;
528 auto& hostBlockExistsInJac = hostBlockExistsInJac_;
530 for (
int row=0; row < numFieldBlocks; ++row) {
531 for (
int col=0; col < numColFieldBlocks; ++col) {
532 const auto thyraTpetraOperator = rcp_dynamic_cast<Thyra::TpetraLinearOp<double,LO,GO,NodeT>>(Jac->getNonconstBlock(row,col),
false);
533 if (nonnull(thyraTpetraOperator)) {
549 const auto tpetraCrsMatrix = rcp_dynamic_cast<Tpetra::CrsMatrix<double,LO,GO,NodeT>>(thyraTpetraOperator->getTpetraOperator(),
true);
550 const auto managedMatrix = tpetraCrsMatrix->getLocalMatrixDevice();
551 const auto managedGraph = managedMatrix.graph;
554 using StaticCrsGraphType =
typename LocalMatrixType::StaticCrsGraphType;
555 StaticCrsGraphType unmanagedGraph;
556 unmanagedGraph.entries =
typename StaticCrsGraphType::entries_type(managedGraph.entries.data(),managedGraph.entries.extent(0));
557 unmanagedGraph.row_map =
typename StaticCrsGraphType::row_map_type(managedGraph.row_map.data(),managedGraph.row_map.extent(0));
558 unmanagedGraph.row_block_offsets =
typename StaticCrsGraphType::row_block_type(managedGraph.row_block_offsets.data(),managedGraph.row_block_offsets.extent(0));
560 typename LocalMatrixType::values_type unmanagedValues(managedMatrix.values.data(),managedMatrix.values.extent(0));
561 LocalMatrixType unmanagedMatrix(managedMatrix.values.label(), managedMatrix.numCols(), unmanagedValues, unmanagedGraph);
565 hostBlockExistsInJac(row,col) = 1;
568 hostBlockExistsInJac(row,col) = 0;
572 auto& jacTpetraBlocks = jacTpetraBlocks_;
573 Kokkos::deep_copy(jacTpetraBlocks,hostJacTpetraBlocks);
574 Kokkos::deep_copy(blockExistsInJac,hostBlockExistsInJac);
581 const auto& globalIndexers = globalIndexer_->getFieldDOFManagers();
582 const auto& blockOffsets_h = blockOffsets_h_;
583 for (
size_t block=0; block < globalIndexers.size(); ++block) {
584 const auto subviewOfBlockLIDs = Kokkos::subview(worksetLIDs_,Kokkos::ALL(), std::make_pair(blockOffsets_h(block),blockOffsets_h(block+1)));
585 globalIndexers[block]->getElementLIDs(localCellIds,subviewOfBlockLIDs);
588 const auto& colBlockOffsets_h = colBlockOffsets_h_;
589 if (hasColIndexers_) {
590 for (
size_t block=0; block < colGlobalIndexers_.size(); ++block) {
591 const auto subviewOfBlockLIDs = Kokkos::subview(colWorksetLIDs_,Kokkos::ALL(), std::make_pair(colBlockOffsets_h(block),colBlockOffsets_h(block+1)));
592 colGlobalIndexers_[block]->getElementLIDs(localCellIds,subviewOfBlockLIDs);
597 for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
599 const int blockRowIndex = productVectorBlockIndex_[fieldIndex];
600 typename Tpetra::Vector<double,LO,GO,PHX::Device>::dual_view_type::t_dev kokkosResidual;
602 auto& tpetraResidual = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(thyraBlockResidual->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
603 kokkosResidual = tpetraResidual.getLocalViewDevice(Tpetra::Access::ReadWrite);
607 auto& tpetraDirichletCounter = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(dirichletCounter_->getNonconstVectorBlock(blockRowIndex),
true))->getTpetraVector());
608 const auto& kokkosDirichletCounter = tpetraDirichletCounter.getLocalViewDevice(Tpetra::Access::ReadWrite);
611 const PHX::View<const int*> fieldOffsets = fieldOffsets_[fieldIndex];
612 const auto& basisIndices = basisIndexForMDFieldOffsets_[fieldIndex];
613 const PHX::View<const LO**> worksetLIDs = worksetLIDs_;
614 const PHX::View<const ScalarT**> fieldValues = scatterFields_[fieldIndex].get_static_view();
615 const PHX::View<const LO*> blockOffsets = blockOffsets_;
616 const PHX::View<const LO*> colBlockOffsets = colBlockOffsets_;
617 const PHX::View<const LO**> colWorksetLIDs = colWorksetLIDs_;
618 const auto&
applyBC = applyBC_[fieldIndex].get_static_view();
621 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
622 LO cLIDs[maxDerivativeArraySize_];
623 typename Sacado::ScalarType<ScalarT>::type
vals[maxDerivativeArraySize_];
625 for(
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
626 const int block_row_offset = blockOffsets(blockRowIndex);
627 const int rowLID = worksetLIDs(cell,block_row_offset+fieldOffsets(basis));
632 const int basisIndex = basisIndices(basis);
639 typedef PHX::MDField<const ScalarT,Cell,NODE>
FieldType;
640 typename FieldType::array_type::reference_type tmpFieldVal = fieldValues(cell,basisIndex);
643 kokkosResidual(rowLID,0) = tmpFieldVal.val();
645 kokkosDirichletCounter(rowLID,0) = 1.0;
648 for (
int blockColIndex=0; blockColIndex < numColFieldBlocks; ++blockColIndex) {
649 if (blockExistsInJac(blockRowIndex,blockColIndex)) {
650 const auto& rowEntries = jacTpetraBlocks(blockRowIndex,blockColIndex).row(rowLID);
651 for (
int i=0; i < rowEntries.length; ++i)
657 for (
int blockColIndex=0; blockColIndex < numColFieldBlocks; ++blockColIndex) {
658 if (blockExistsInJac(blockRowIndex,blockColIndex)) {
659 const int start = colBlockOffsets(blockColIndex);
660 const int stop = colBlockOffsets(blockColIndex+1);
661 const int sensSize = stop-start;
663 for (
int i=0; i < sensSize; ++i) {
664 cLIDs[i] = colWorksetLIDs(cell,start+i);
665 vals[i] = tmpFieldVal.fastAccessDx(start+i);
667 jacTpetraBlocks(blockRowIndex,blockColIndex).replaceValues(rowLID,cLIDs,sensSize,
vals,
true,
true);
676 for (
int row=0; row < numFieldBlocks; ++row) {
677 for (
int col=0; col < numColFieldBlocks; ++col) {
678 if (hostBlockExistsInJac(row,col)) {
679 hostJacTpetraBlocks(row,col).~CrsMatrix();
696 const Teuchos::ParameterList& p)
697 : globalIndexer_(indexer)
698 , globalDataKey_(
"Residual Scatter Container")
700 std::string scatterName = p.get<std::string>(
"Scatter Name");
702 Teuchos::rcp(
new PHX::Tag<ScalarT>(scatterName,Teuchos::rcp(
new PHX::MDALayout<Dummy>(0))));
705 const std::vector<std::string>& names =
706 *(p.get< Teuchos::RCP< std::vector<std::string> > >(
"Dependent Names"));
709 fieldMap_ = p.get< Teuchos::RCP< std::map<std::string,std::string> > >(
"Dependent Map");
712 scatterIC_ = p.isParameter(
"Scatter Initial Condition") ? p.get<
bool>(
"Scatter Initial Condition") :
false;
714 Teuchos::RCP<PHX::DataLayout> dl = (!scatterIC_) ?
715 p.get< Teuchos::RCP<panzer::PureBasis> >(
"Basis")->functional :
716 p.get< Teuchos::RCP<const panzer::PureBasis> >(
"Basis")->functional;
718 side_subcell_dim_ = p.get<
int>(
"Side Subcell Dimension");
719 local_side_id_ = p.get<
int>(
"Local Side ID");
723 scatterFields_.resize(names.size());
724 for (std::size_t eq = 0; eq < names.size(); ++eq) {
725 scatterFields_[eq] = PHX::MDField<const ScalarT,Cell,NODE>(names[eq],dl);
728 this->addDependentField(scatterFields_[eq]);
731 checkApplyBC_ = p.isParameter(
"Check Apply BC") ? p.get<
bool>(
"Check Apply BC") :
false;
732 applyBC_.resize(names.size());
734 for (std::size_t eq = 0; eq < names.size(); ++eq) {
735 applyBC_[eq] = PHX::MDField<const bool,Cell,NODE>(std::string(
"APPLY_BC_")+fieldMap_->find(names[eq])->second,dl);
736 this->addDependentField(applyBC_[eq]);
741 this->addEvaluatedField(*scatterHolder_);
743 if (p.isType<std::string>(
"Global Data Key"))
744 globalDataKey_ = p.get<std::string>(
"Global Data Key");
746 this->setName(scatterName+
" Scatter Dirichlet Residual");
755 const Workset & workset_0 = (*d.worksets_)[0];
756 const std::string blockId = this->wda(workset_0).
block_id;
758 fieldIds_.resize(scatterFields_.size());
759 fieldOffsets_.resize(scatterFields_.size());
760 basisIndexForMDFieldOffsets_.resize(scatterFields_.size());
761 fieldGlobalIndexers_.resize(scatterFields_.size());
762 productVectorBlockIndex_.resize(scatterFields_.size());
763 int maxElementBlockGIDCount = -1;
764 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
766 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
768 const int globalFieldNum = globalIndexer_->getFieldNum(fieldName);
769 productVectorBlockIndex_[fd] = globalIndexer_->getFieldBlock(globalFieldNum);
770 fieldGlobalIndexers_[fd] = globalIndexer_->getFieldDOFManagers()[productVectorBlockIndex_[fd]];
771 fieldIds_[fd] = fieldGlobalIndexers_[fd]->getFieldNum(fieldName);
775 const auto& offsetPair = fieldGlobalIndexers_[fd]->getGIDFieldOffsets_closure(blockId,fieldIds_[fd],side_subcell_dim_,local_side_id_);
777 const auto&
offsets = offsetPair.first;
778 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Tangent):fieldOffsets",
offsets.size());
779 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
780 for (std::size_t i=0; i <
offsets.size(); ++i)
782 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
785 const auto& basisIndex = offsetPair.second;
786 basisIndexForMDFieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Tangent):basisIndexForMDFieldOffsets",basisIndex.size());
787 auto hostBasisIndex = Kokkos::create_mirror_view(basisIndexForMDFieldOffsets_[fd]);
788 for (std::size_t i=0; i < basisIndex.size(); ++i)
789 hostBasisIndex(i) = basisIndex[i];
790 Kokkos::deep_copy(basisIndexForMDFieldOffsets_[fd], hostBasisIndex);
795 const std::vector<int>&
offsets = fieldGlobalIndexers_[fd]->getGIDFieldOffsets(blockId,fieldIds_[fd]);
796 fieldOffsets_[fd] = PHX::View<int*>(
"ScatterDirichletResidual_BlockedTpetra(Tangent):fieldOffsets",
offsets.size());
797 auto hostOffsets = Kokkos::create_mirror_view(fieldOffsets_[fd]);
798 for (std::size_t i=0; i <
offsets.size(); ++i)
800 Kokkos::deep_copy(fieldOffsets_[fd], hostOffsets);
803 maxElementBlockGIDCount = std::max(fieldGlobalIndexers_[fd]->getElementBlockGIDCount(blockId),maxElementBlockGIDCount);
809 worksetLIDs_ = PHX::View<LO**>(
"ScatterResidual_BlockedTpetra(Tangent):worksetLIDs",
810 scatterFields_[0].extent(0),
811 maxElementBlockGIDCount);
821 std::vector<std::string> activeParameters =
822 Teuchos::rcp_dynamic_cast<ParameterList_GlobalEvaluationData>(d.gedc->getDataObject(
"PARAMETER_NAMES"))->getActiveParameters();
824 const int numBlocks =
static_cast<int>(globalIndexer_->getFieldDOFManagers().size());
828 dfdpFieldsVoV_.initialize(
"ScatterResidual_Tpetra<Tangent>::dfdpFieldsVoV_",activeParameters.size(),numBlocks);
830 dfdpVectors_.resize(activeParameters.size());
831 for(std::size_t i=0;i<activeParameters.size();i++) {
832 Teuchos::RCP<ContainerType> paramBlockedContainer = Teuchos::rcp_dynamic_cast<ContainerType>(d.gedc->getDataObject(activeParameters[i]),
true);
833 Teuchos::RCP<Thyra::ProductVectorBase<double>> productVector =
834 Teuchos::rcp_dynamic_cast<Thyra::ProductVectorBase<double>>(paramBlockedContainer->get_f(),
true);
835 dfdpVectors_[i].resize(numBlocks);
836 for(
int j=0;j<numBlocks;j++)
837 dfdpVectors_[i][j] = Teuchos::rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(productVector->getNonconstVectorBlock(j),
true)->getTpetraVector();
841 Teuchos::RCP<const ContainerType> blockContainer
842 = Teuchos::rcp_dynamic_cast<ContainerType>(d.gedc->getDataObject(
"Dirichlet Counter"),
true);
844 dirichletCounter_ = Teuchos::rcp_dynamic_cast<Thyra::ProductVectorBase<double> >(blockContainer->get_f(),
true);
845 TEUCHOS_ASSERT(!Teuchos::is_null(dirichletCounter_));
848 blockedContainer_ = Teuchos::rcp_dynamic_cast<const ContainerType>(d.gedc->getDataObject(globalDataKey_),
true);
849 TEUCHOS_ASSERT(!Teuchos::is_null(blockedContainer_));
858 using Teuchos::rcp_dynamic_cast;
862 const auto& localCellIds = this->wda(workset).cell_local_ids_k;
864 RCP<ProductVectorBase<double> > thyraScatterTarget = (!scatterIC_) ?
865 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_f(),
true) :
866 rcp_dynamic_cast<ProductVectorBase<double> >(blockedContainer_->get_x(),
true);
870 for(std::size_t i=0;i<dfdpVectors_.size();i++)
871 for(std::size_t j=0;j<dfdpVectors_[i].size();j++)
872 dfdpFieldsVoV_.addView(dfdpVectors_[i][j]->getLocalViewDevice(Tpetra::Access::ReadWrite),i,j);
873 dfdpFieldsVoV_.syncHostToDevice();
876 int currentWorksetLIDSubBlock = -1;
877 for (std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
879 if (productVectorBlockIndex_[fieldIndex] != currentWorksetLIDSubBlock) {
880 fieldGlobalIndexers_[fieldIndex]->getElementLIDs(localCellIds,worksetLIDs_);
881 currentWorksetLIDSubBlock = productVectorBlockIndex_[fieldIndex];
885 auto& tpetraScatterTarget = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(thyraScatterTarget->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
886 const auto& kokkosScatterTarget = tpetraScatterTarget.getLocalViewDevice(Tpetra::Access::ReadWrite);
889 auto& tpetraDirichletCounter = *((rcp_dynamic_cast<Thyra::TpetraVector<RealType,LO,GO,NodeT>>(dirichletCounter_->getNonconstVectorBlock(productVectorBlockIndex_[fieldIndex]),
true))->getTpetraVector());
890 const auto& kokkosDirichletCounter = tpetraDirichletCounter.getLocalViewDevice(Tpetra::Access::ReadWrite);
893 const auto fieldOffsets = fieldOffsets_[fieldIndex];
894 const auto basisIndices = basisIndexForMDFieldOffsets_[fieldIndex];
895 const auto worksetLIDs = worksetLIDs_;
896 const auto fieldValues = scatterFields_[fieldIndex].get_static_view();
897 const auto applyBC = applyBC_[fieldIndex].get_static_view();
899 const auto& tangentFieldsDevice = dfdpFieldsVoV_.getViewDevice();
900 const auto& kokkosTangents = Kokkos::subview(tangentFieldsDevice,Kokkos::ALL(),productVectorBlockIndex_[fieldIndex]);
901 const std::size_t
num_params = Sacado::dimension_scalar(fieldValues)-1;
905 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
906 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
907 const int lid = worksetLIDs(cell,fieldOffsets(basis));
910 const int basisIndex = basisIndices(basis);
917 kokkosScatterTarget(lid,0) = fieldValues(cell,basisIndex).val();
918 for(std::size_t i_param=0; i_param<
num_params; i_param++)
919 kokkosTangents(i_param)(lid,0) = fieldValues(cell,basisIndex).fastAccessDx(i_param);
921 kokkosDirichletCounter(lid,0) = 1.0;
927 Kokkos::parallel_for(Kokkos::RangePolicy<PHX::Device>(0,workset.num_cells), KOKKOS_LAMBDA (
const int& cell) {
928 for (
int basis=0; basis < static_cast<int>(fieldOffsets.size()); ++basis) {
929 const int lid = worksetLIDs(cell,fieldOffsets(basis));
932 kokkosScatterTarget(lid,0) = fieldValues(cell,basis).val();
933 for(std::size_t i_param=0; i_param<
num_params; i_param++)
934 kokkosTangents(i_param)(lid,0) = fieldValues(cell,basis).fastAccessDx(i_param);
935 kokkosDirichletCounter(lid,0) = 1.0;
945 for(std::size_t i=0;i<dfdpVectors_.size();i++)
946 for(std::size_t j=0;j<dfdpVectors_[i].size();j++)
947 dfdpFieldsVoV_.addView(Kokkos::View<RealType**,Kokkos::LayoutLeft,PHX::Device>(),i,j);