11 #ifndef __Panzer_GatherSolution_BlockedEpetra_impl_hpp__
12 #define __Panzer_GatherSolution_BlockedEpetra_impl_hpp__
30 #include "Phalanx_DataLayout.hpp"
33 #include "Teuchos_Assert.hpp"
34 #include "Teuchos_FancyOStream.hpp"
37 #include "Thyra_ProductVectorBase.hpp"
38 #include "Thyra_SpmdVectorBase.hpp"
45 template<
typename TRAITS,
typename LO,
typename GO>
54 hasTangentFields_(false)
63 using vvstring = std::vector<std::vector<std::string>>;
79 MDField<ScalarT, Cell, NODE>(names[fd], basis->functional);
80 this->addEvaluatedField(gatherFields_[fd]);
84 if (tangentFieldNames.size() > 0)
87 hasTangentFields_ =
true;
88 tangentFields_.resize(numFields);
91 int numTangentFields(tangentFieldNames[fd].size());
92 tangentFields_[fd].resize(numTangentFields);
93 for (
int i(0); i < numTangentFields; ++i)
95 tangentFields_[fd][i] =
96 MDField<const ScalarT, Cell, NODE>(tangentFieldNames[fd][i],
98 this->addDependentField(tangentFields_[fd][i]);
104 string firstName(
"<none>");
106 firstName = names[0];
107 string n(
"GatherSolution (BlockedEpetra): " + firstName +
" (" +
108 print<EvalT>() +
")");
117 template<
typename TRAITS,
typename LO,
typename GO>
122 typename TRAITS::SetupData ,
134 const string& fieldName(indexerNames_[fd]);
136 subFieldIds_[fd] = indexers_[indexerIds_[fd]]->getFieldNum(fieldName);
139 indexerNames_.clear();
147 template<
typename TRAITS,
typename LO,
typename GO>
152 typename TRAITS::PreEvalData d)
154 using std::logic_error;
157 using Teuchos::rcp_dynamic_cast;
166 string post(useTimeDerivativeSolutionVector_ ?
" - Xdot" :
" - X");
167 if (d.gedc->containsDataObject(globalDataKey_ + post))
169 ged = d.gedc->getDataObject(globalDataKey_ + post);
170 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
176 ged = d.gedc->getDataObject(globalDataKey_);
179 auto roGed = rcp_dynamic_cast<
const BVROGED>(ged);
180 auto beLoc = rcp_dynamic_cast<
const BELOC>(ged);
181 if (not roGed.is_null())
182 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
183 else if (not beLoc.is_null())
185 if (useTimeDerivativeSolutionVector_)
186 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_dxdt());
188 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_x());
190 "Residual: Can't find the x_ vector in GEDC \"" << globalDataKey_ <<
191 "\" (" << post <<
"). A cast failed for " << ged <<
". Type is " <<
202 template<
typename TRAITS,
typename LO,
typename GO>
207 typename TRAITS::EvalData workset)
214 using Teuchos::ptrFromRef;
216 using Teuchos::rcp_dynamic_cast;
218 using Thyra::SpmdVectorBase;
222 string blockId(this->wda(workset).block_id);
223 const vector<size_t>& localCellIds = this->wda(workset).cell_local_ids;
224 int numFields(gatherFields_.size()), numCells(localCellIds.size());
227 for (
int fieldInd(0); fieldInd <
numFields; ++fieldInd)
229 MDField<ScalarT, Cell, NODE>&
field = gatherFields_[fieldInd];
230 auto field_h = Kokkos::create_mirror_view(field.get_static_view());
232 int indexerId(indexerIds_[fieldInd]),
233 subFieldNum(subFieldIds_[fieldInd]);
239 if(not x_.is_null()) {
240 rcp_dynamic_cast<SpmdVectorBase<double>>(x_->
241 getNonconstVectorBlock(indexerId))->getLocalData(ptrFromRef(x));
244 xEvRoGed = xBvRoGed_->getGEDBlock(indexerId);
247 auto subRowIndexer = indexers_[indexerId];
248 const vector<int>& elmtOffset =
249 subRowIndexer->getGIDFieldOffsets(blockId, subFieldNum);
250 int numBases(elmtOffset.size());
252 auto LIDs = subRowIndexer->getLIDs();
253 auto LIDs_h = Kokkos::create_mirror_view(LIDs);
254 Kokkos::deep_copy(LIDs_h, LIDs);
257 for (
int cell(0); cell < numCells; ++cell)
259 LO cellLocalId = localCellIds[cell];
262 for (
int basis(0); basis < numBases; ++basis)
264 int offset(elmtOffset[basis]), lid(LIDs_h(cellLocalId, offset));
266 field_h(cell, basis) = (*xEvRoGed)[lid];
268 field_h(cell, basis) = x[lid];
271 Kokkos::deep_copy(field.get_static_view(), field_h);
280 template<
typename TRAITS,
typename LO,
typename GO>
288 hasTangentFields_(false)
297 using vvstring = std::vector<std::vector<std::string>>;
313 MDField<ScalarT, Cell, NODE>(names[fd], basis->functional);
314 this->addEvaluatedField(gatherFields_[fd]);
318 if (tangentFieldNames.size() > 0)
321 hasTangentFields_ =
true;
322 tangentFields_.resize(numFields);
325 int numTangentFields(tangentFieldNames[fd].size());
326 tangentFields_[fd].resize(numTangentFields);
327 for (
int i(0); i < numTangentFields; ++i)
329 tangentFields_[fd][i] =
330 MDField<const ScalarT, Cell, NODE>(tangentFieldNames[fd][i],
332 this->addDependentField(tangentFields_[fd][i]);
338 string firstName(
"<none>");
340 firstName = names[0];
341 string n(
"GatherSolution Tangent (BlockedEpetra): " + firstName +
" (" +
342 print<EvalT>() +
")");
351 template<
typename TRAITS,
typename LO,
typename GO>
355 typename TRAITS::SetupData ,
367 const string& fieldName(indexerNames_[fd]);
369 subFieldIds_[fd] = indexers_[indexerIds_[fd]]->getFieldNum(fieldName);
372 indexerNames_.clear();
380 template<
typename TRAITS,
typename LO,
typename GO>
384 typename TRAITS::PreEvalData d)
386 using std::logic_error;
389 using Teuchos::rcp_dynamic_cast;
398 string post(useTimeDerivativeSolutionVector_ ?
" - Xdot" :
" - X");
399 if (d.gedc->containsDataObject(globalDataKey_ + post))
401 ged = d.gedc->getDataObject(globalDataKey_ + post);
402 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
408 ged = d.gedc->getDataObject(globalDataKey_);
411 auto roGed = rcp_dynamic_cast<
const BVROGED>(ged);
412 auto beLoc = rcp_dynamic_cast<
const BELOC>(ged);
413 if (not roGed.is_null())
414 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
415 else if (not beLoc.is_null())
417 if (useTimeDerivativeSolutionVector_)
418 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_dxdt());
420 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_x());
422 "Tangent: Can't find the x_ vector in GEDC \"" << globalDataKey_ <<
423 "\" (" << post <<
"). A cast failed for " << ged <<
". Type is " <<
434 template<
typename TRAITS,
typename LO,
typename GO>
438 typename TRAITS::EvalData workset)
446 using Teuchos::ptrFromRef;
448 using Teuchos::rcp_dynamic_cast;
450 using Thyra::SpmdVectorBase;
454 vector<pair<int, GO>> GIDs;
455 string blockId(this->wda(workset).block_id);
456 const vector<size_t>& localCellIds = this->wda(workset).cell_local_ids;
457 int numFields(gatherFields_.size()), numCells(localCellIds.size());
462 for (
int fieldInd(0); fieldInd <
numFields; ++fieldInd)
464 MDField<ScalarT, Cell, NODE>&
field = gatherFields_[fieldInd];
465 int indexerId(indexerIds_[fieldInd]),
466 subFieldNum(subFieldIds_[fieldInd]);
469 auto xEvRoGed = xBvRoGed_->getGEDBlock(indexerId);
470 auto subRowIndexer = indexers_[indexerId];
471 const vector<int>& elmtOffset =
472 subRowIndexer->getGIDFieldOffsets(blockId, subFieldNum);
473 int numBases(elmtOffset.size());
476 for (
int cell(0); cell < numCells; ++cell)
478 LO cellLocalId = localCellIds[cell];
479 auto LIDs = subRowIndexer->getElementLIDs(cellLocalId);
482 for (
int basis(0); basis < numBases; ++basis)
484 int offset(elmtOffset[basis]), lid(LIDs[offset]);
485 field(cell, basis) = (*xEvRoGed)[lid];
493 for (
int fieldInd(0); fieldInd <
numFields; ++fieldInd)
495 MDField<ScalarT, Cell, NODE>&
field = gatherFields_[fieldInd];
496 int indexerId(indexerIds_[fieldInd]),
497 subFieldNum(subFieldIds_[fieldInd]);
501 rcp_dynamic_cast<SpmdVectorBase<double>>(x_->
502 getNonconstVectorBlock(indexerId))->getLocalData(ptrFromRef(x));
503 auto subRowIndexer = indexers_[indexerId];
504 const vector<int>& elmtOffset =
505 subRowIndexer->getGIDFieldOffsets(blockId, subFieldNum);
506 int numBases(elmtOffset.size());
509 for (
int cell(0); cell < numCells; ++cell)
511 LO cellLocalId = localCellIds[cell];
512 auto LIDs = subRowIndexer->getElementLIDs(cellLocalId);
515 for (
int basis(0); basis < numBases; ++basis)
517 int offset(elmtOffset[basis]), lid(LIDs[offset]);
518 field(cell, basis) = x[lid];
525 if (hasTangentFields_)
528 for (
int fieldInd(0); fieldInd <
numFields; ++fieldInd)
530 MDField<ScalarT, Cell, NODE>&
field = gatherFields_[fieldInd];
531 int indexerId(indexerIds_[fieldInd]),
532 subFieldNum(subFieldIds_[fieldInd]);
533 auto subRowIndexer = indexers_[indexerId];
534 const vector<int>& elmtOffset =
535 subRowIndexer->getGIDFieldOffsets(blockId, subFieldNum);
536 int numBases(elmtOffset.size());
539 for (
int cell(0); cell < numCells; ++cell)
542 for (
int basis(0); basis < numBases; ++basis)
544 int numTangentFields(tangentFields_[fieldInd].size());
545 for (
int i(0); i < numTangentFields; ++i)
546 field(cell, basis).fastAccessDx(i) =
547 tangentFields_[fieldInd][i](cell, basis).val();
559 template<
typename TRAITS,
typename LO,
typename GO>
592 MDField<ScalarT, Cell, NODE> f(names[fd], basis->functional);
593 gatherFields_[fd] = f;
594 this->addEvaluatedField(gatherFields_[fd]);
598 string firstName(
"<none>"), n(
"GatherSolution (BlockedEpetra");
600 firstName = names[0];
601 if (disableSensitivities_)
602 n +=
", No Sensitivities";
603 n +=
"): " + firstName +
" (" + print<EvalT>() +
")";
612 template<
typename TRAITS,
typename LO,
typename GO>
617 typename TRAITS::SetupData ,
629 const string& fieldName(indexerNames_[fd]);
631 subFieldIds_[fd] = indexers_[indexerIds_[fd]]->getFieldNum(fieldName);
634 indexerNames_.clear();
642 template<
typename TRAITS,
typename LO,
typename GO>
647 typename TRAITS::PreEvalData d)
650 using std::logic_error;
653 using Teuchos::rcp_dynamic_cast;
661 applySensitivities_ =
false;
662 if ((not disableSensitivities_ ) and
663 (d.first_sensitivities_name == sensitivitiesName_))
664 applySensitivities_ =
true;
668 string post(useTimeDerivativeSolutionVector_ ?
" - Xdot" :
" - X");
669 if (d.gedc->containsDataObject(globalDataKey_ + post))
671 ged = d.gedc->getDataObject(globalDataKey_ + post);
672 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
678 ged = d.gedc->getDataObject(globalDataKey_);
681 auto roGed = rcp_dynamic_cast<
const BVROGED>(ged);
682 auto beLoc = rcp_dynamic_cast<
const BELOC>(ged);
683 if (not roGed.is_null())
684 xBvRoGed_ = rcp_dynamic_cast<BVROGED>(ged,
true);
685 else if (not beLoc.is_null())
687 if (useTimeDerivativeSolutionVector_)
688 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_dxdt());
690 x_ = rcp_dynamic_cast<ProductVectorBase<double>>(beLoc->get_x());
692 "Jacobian: Can't find x vector in GEDC \"" << globalDataKey_ <<
693 "\" (" << post <<
"). A cast failed for " << ged <<
". Type is " <<
704 template<
typename TRAITS,
typename LO,
typename GO>
709 typename TRAITS::EvalData workset)
716 using Teuchos::ptrFromRef;
718 using Teuchos::rcp_dynamic_cast;
720 using Thyra::SpmdVectorBase;
724 string blockId(this->wda(workset).block_id);
725 const vector<size_t>& localCellIds = this->wda(workset).cell_local_ids;
726 int numFields(gatherFields_.size()), numCells(localCellIds.size());
728 if (applySensitivities_)
730 if ((useTimeDerivativeSolutionVector_) and (gatherSeedIndex_ < 0))
731 seedValue = workset.alpha;
732 else if (gatherSeedIndex_ < 0)
733 seedValue = workset.beta;
734 else if (not useTimeDerivativeSolutionVector_)
735 seedValue = workset.gather_seeds[gatherSeedIndex_];
743 if (not applySensitivities_)
746 vector<int> blockOffsets;
748 int numDerivs(blockOffsets[blockOffsets.size() - 1]);
755 for (
int fieldInd(0); fieldInd <
numFields; ++fieldInd)
757 MDField<ScalarT, Cell, NODE>&
field = gatherFields_[fieldInd];
758 auto field_h = Kokkos::create_mirror_view(field.get_view());
760 int indexerId(indexerIds_[fieldInd]), subFieldNum(subFieldIds_[fieldInd]);
765 if(not x_.is_null()) {
766 rcp_dynamic_cast<SpmdVectorBase<double>>(x_->getNonconstVectorBlock(indexerId))->getLocalData(ptrFromRef(x));
768 xEvRoGed = xBvRoGed_->getGEDBlock(indexerId);
771 auto subRowIndexer = indexers_[indexerId];
772 const vector<int>& elmtOffset =
773 subRowIndexer->getGIDFieldOffsets(blockId, subFieldNum);
774 int startBlkOffset(blockOffsets[indexerId]), numBases(elmtOffset.size());
776 auto LIDs = subRowIndexer->getLIDs();
777 auto LIDs_h = Kokkos::create_mirror_view(LIDs);
778 Kokkos::deep_copy(LIDs_h, LIDs);
781 for (
int cell(0); cell < numCells; ++cell)
783 LO cellLocalId = localCellIds[cell];
786 for (
int basis(0); basis < numBases; ++basis)
789 int offset(elmtOffset[basis]), lid(LIDs_h(cellLocalId, offset));
791 field_h(cell, basis) =
ScalarT(numDerivs, (*xEvRoGed)[lid]);
793 field_h(cell, basis) =
ScalarT(numDerivs, x[lid]);
795 field_h(cell, basis).fastAccessDx(startBlkOffset + offset) = seedValue;
798 Kokkos::deep_copy(field.get_static_view(), field_h);
802 #endif // __Panzer_GatherSolution_BlockedEpetra_impl_hpp__
#define TEUCHOS_TEST_FOR_EXCEPTION(throw_exception_test, Exception, msg)
void computeBlockOffsets(const std::string &blockId, const std::vector< Teuchos::RCP< GlobalIndexer >> &ugis, std::vector< int > &blockOffsets)
This class encapsulates the needs of a gather operation to do a halo exchange for blocked vectors...
int getFieldBlock(const std::string &fieldName, const std::vector< Teuchos::RCP< const GlobalIndexer >> &ugis)
void postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager< TRAITS > &fm)
Post-Registration Setup.
GatherSolution_BlockedEpetra(const Teuchos::ParameterList &p)
Constructor.
virtual void preEvaluate(typename Traits::PreEvalData d)=0
PHX::MDField< ScalarT, panzer::Cell, panzer::BASIS > field
A field to which we'll contribute, or in which we'll store, the result of computing this integral...
void evaluateFields(typename TRAITS::EvalData d)
Evaluate Fields.
Description and data layouts associated with a particular basis.
#define TEUCHOS_ASSERT(assertion_test)
panzer::Traits::Jacobian::ScalarT ScalarT
The scalar type.
std::string typeName(const T &t)