12 #ifndef __Panzer_ScatterResidual_BlockedEpetra_Hessian_impl_hpp__
13 #define __Panzer_ScatterResidual_BlockedEpetra_Hessian_impl_hpp__
16 #ifdef Panzer_BUILD_HESSIAN_SUPPORT
26 template<
typename TRAITS,
typename LO,
typename GO>
31 bool useDiscreteAdjoint)
32 : rowIndexers_(rIndexers)
33 , colIndexers_(cIndexers)
34 , globalDataKey_(
"Residual Scatter Container")
35 , useDiscreteAdjoint_(useDiscreteAdjoint)
37 std::string scatterName = p.
get<std::string>(
"Scatter Name");
42 const std::vector<std::string>& names =
52 scatterFields_.resize(names.size());
53 for (std::size_t eq = 0; eq < names.size(); ++eq) {
57 this->addDependentField(scatterFields_[eq]);
61 this->addEvaluatedField(*scatterHolder_);
63 if (p.
isType<std::string>(
"Global Data Key"))
64 globalDataKey_ = p.
get<std::string>(
"Global Data Key");
65 if (p.
isType<
bool>(
"Use Discrete Adjoint"))
66 useDiscreteAdjoint = p.
get<
bool>(
"Use Discrete Adjoint");
69 if(useDiscreteAdjoint)
72 if(colIndexers_.size()==0)
73 colIndexers_ = rowIndexers_;
75 this->setName(scatterName+
" Scatter Residual BlockedEpetra (Hessian)");
78 template<
typename TRAITS,
typename LO,
typename GO>
84 indexerIds_.resize(scatterFields_.size());
85 subFieldIds_.resize(scatterFields_.size());
88 for(std::size_t fd=0;fd<scatterFields_.size();++fd) {
90 std::string fieldName = fieldMap_->find(scatterFields_[fd].fieldTag().name())->second;
93 subFieldIds_[fd] = rowIndexers_[indexerIds_[fd]]->getFieldNum(fieldName);
97 template<
typename TRAITS,
typename LO,
typename GO>
103 using Teuchos::rcp_dynamic_cast;
109 RCP<const BLOC> blockedContainer = rcp_dynamic_cast<
const BLOC>(d.gedc->getDataObject(globalDataKey_));
110 RCP<const ELOC> epetraContainer = rcp_dynamic_cast<
const ELOC>(d.gedc->getDataObject(globalDataKey_));
113 if(blockedContainer!=Teuchos::null) {
116 else if(epetraContainer!=Teuchos::null) {
125 template<
typename TRAITS,
typename LO,
typename GO>
132 using Teuchos::ptrFromRef;
133 using Teuchos::rcp_dynamic_cast;
136 using Thyra::SpmdVectorBase;
140 std::vector<double> jacRow;
143 std::string blockId = this->
wda(workset).block_id;
144 const std::vector<std::size_t> & localCellIds = this->
wda(workset).cell_local_ids;
146 int numFieldBlocks = Teuchos::as<int>(colIndexers_.size());
148 std::vector<int> blockOffsets;
154 for(std::size_t fieldIndex = 0; fieldIndex < scatterFields_.size(); fieldIndex++) {
155 int rowIndexer = indexerIds_[fieldIndex];
156 int subFieldNum = subFieldIds_[fieldIndex];
158 auto subRowIndexer = rowIndexers_[rowIndexer];
159 const std::vector<int> & elmtOffset = subRowIndexer->getGIDFieldOffsets(blockId,subFieldNum);
162 for(std::size_t worksetCellIndex=0;worksetCellIndex<localCellIds.size();++worksetCellIndex) {
163 std::size_t cellLocalId = localCellIds[worksetCellIndex];
165 auto rLIDs = subRowIndexer->getElementLIDs(cellLocalId);
168 for(std::size_t rowBasisNum = 0; rowBasisNum < elmtOffset.size(); rowBasisNum++) {
169 const ScalarT scatterField = (scatterFields_[fieldIndex])(worksetCellIndex,rowBasisNum);
170 int rowOffset = elmtOffset[rowBasisNum];
171 int r_lid = rLIDs[rowOffset];
174 jacRow.resize(scatterField.size());
177 if(scatterField.size() == 0)
180 for(
int sensIndex=0;sensIndex<scatterField.size();++sensIndex)
181 jacRow[sensIndex] = scatterField.fastAccessDx(sensIndex).fastAccessDx(0);
184 for(
int colIndexer=0;colIndexer<numFieldBlocks;colIndexer++) {
185 int start = blockOffsets[colIndexer];
186 int end = blockOffsets[colIndexer+1];
191 auto subColIndexer = colIndexers_[colIndexer];
192 auto cLIDs = subColIndexer->getElementLIDs(cellLocalId);
197 std::pair<int,int> blockIndex = std::make_pair(rowIndexer,colIndexer);
198 Teuchos::RCP<Epetra_CrsMatrix> subJac = jacEpetraBlocks[blockIndex];
201 if(subJac==Teuchos::null) {
210 jacEpetraBlocks[blockIndex] = subJac;
215 int err = subJac->
SumIntoMyValues(r_lid, end-start, &jacRow[start],&cLIDs[0]);
218 std::stringstream ss;
219 ss <<
"Failed inserting row: " <<
"LID = " << r_lid <<
": ";
220 for(
int i=0;i<end-start;i++)
221 ss << cLIDs[i] <<
" ";
223 ss <<
"Into block " << rowIndexer <<
", " << colIndexer << std::endl;
225 ss <<
"scatter field = ";
226 scatterFields_[fieldIndex].print(ss);
230 for(
int i=start;i<end;i++)
231 ss << jacRow[i] <<
" ";
234 std::cout << ss.str() << std::endl;
ScatterResidual_BlockedEpetra(const Teuchos::ParameterList &p)
panzer::Traits::Hessian::ScalarT ScalarT
T & get(const std::string &name, T def_value)
bool is_null(const std::shared_ptr< T > &p)
#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)
void evaluateFields(typename TRAITS::EvalData d)
TEUCHOS_DEPRECATED RCP< T > rcp(T *p, Dealloc_T dealloc, bool owns_mem)
int getFieldBlock(const std::string &fieldName, const std::vector< Teuchos::RCP< const GlobalIndexer >> &ugis)
int SumIntoMyValues(int MyRow, int NumEntries, const double *Values, const int *Indices)
virtual void preEvaluate(typename Traits::PreEvalData d)=0
void postRegistrationSetup(typename TRAITS::SetupData d, PHX::FieldManager< TRAITS > &vm)
bool isType(const std::string &name) const
WorksetDetailsAccessor wda
#define TEUCHOS_ASSERT(assertion_test)