@@ -2195,34 +2195,43 @@ const ModifiableLinearOp explicitAdd(const LinearOp &opl_in, const LinearOp &opr
21952195 // Build output operator
21962196 RCP<Thyra::LinearOpBase<ST>> explicitOp;
21972197 RCP<Tpetra::CrsMatrix<ST, LO, GO, NT>> explicitCrsOp;
2198- if (destOp != Teuchos::null ) {
2198+ if (!destOp. is_null () ) {
21992199 explicitOp = destOp;
2200- try { // try to reuse matrix sparsity with Add. If it fails, build new operator with add
2201- RCP <Thyra::TpetraLinearOp<ST, LO, GO, NT>> tOp =
2202- rcp_dynamic_cast<Thyra::TpetraLinearOp<ST, LO, GO, NT>>(destOp);
2200+ RCP<Thyra::TpetraLinearOp<ST, LO, GO, NT>> tOp =
2201+ rcp_dynamic_cast <Thyra::TpetraLinearOp<ST, LO, GO, NT>>(destOp);
2202+ if (!tOp. is_null ())
22032203 explicitCrsOp =
2204- rcp_dynamic_cast<Tpetra::CrsMatrix<ST, LO, GO, NT>>(tOp->getTpetraOperator (), true );
2205- Tpetra::MatrixMatrix::Add<ST, LO, GO, NT>(*tCrsOpl, transpl, scalarl, *tCrsOpr, transpr,
2206- scalarr, explicitCrsOp);
2207- } catch (std::logic_error &e) {
2208- RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream ();
2209- *out << " *** THROWN EXCEPTION ***\n " ;
2210- *out << e.what () << std::endl;
2211- *out << " ************************\n " ;
2212- *out << " Teko: explicitAdd unable to reuse existing operator. Creating new operator.\n "
2213- << std::endl;
2204+ rcp_dynamic_cast<Tpetra::CrsMatrix<ST, LO, GO, NT>>(tOp->getTpetraOperator ());
2205+ bool needNewTpetraMatrix =
2206+ (explicitCrsOp.is_null ()) || (tCrsOpl == explicitCrsOp) || (tCrsOpr == explicitCrsOp);
2207+ if (!needNewTpetraMatrix) {
2208+ try {
2209+ // try to reuse matrix sparsity with Add. If it fails, build new operator with add
2210+ Tpetra::MatrixMatrix::Add<ST, LO, GO, NT>(*tCrsOpl, transpl, scalarl, *tCrsOpr, transpr,
2211+ scalarr, explicitCrsOp);
2212+ } catch (std::logic_error &e) {
2213+ RCP<Teuchos::FancyOStream> out = Teuchos::VerboseObjectBase::getDefaultOStream ();
2214+ *out << " *** THROWN EXCEPTION ***\n " ;
2215+ *out << e.what () << std::endl;
2216+ *out << " ************************\n " ;
2217+ *out << " Teko: explicitAdd unable to reuse existing operator. Creating new operator.\n "
2218+ << std::endl;
2219+ needNewTpetraMatrix = true ;
2220+ }
2221+ }
2222+ if (needNewTpetraMatrix)
2223+ // Do explicit matrix-matrix add
22142224 explicitCrsOp = Tpetra::MatrixMatrix::add<ST, LO, GO, NT>(scalarl, transpl, *tCrsOpl,
22152225 scalarr, transpr, *tCrsOpr);
2216- }
22172226 } else {
2218- explicitOp = rcp (new Thyra::TpetraLinearOp<ST, LO, GO, NT>());
2227+ explicitOp = rcp (new Thyra::TpetraLinearOp<ST, LO, GO, NT>());
2228+ // Do explicit matrix-matrix add
22192229 explicitCrsOp = Tpetra::MatrixMatrix::add<ST, LO, GO, NT>(scalarl, transpl, *tCrsOpl, scalarr,
22202230 transpr, *tCrsOpr);
22212231 }
22222232 RCP<Thyra::TpetraLinearOp<ST, LO, GO, NT>> tExplicitOp =
22232233 rcp_dynamic_cast<Thyra::TpetraLinearOp<ST, LO, GO, NT>>(explicitOp);
22242234
2225- // Do explicit matrix-matrix add
22262235 tExplicitOp->initialize (Thyra::tpetraVectorSpace<ST, LO, GO, NT>(explicitCrsOp->getRangeMap ()),
22272236 Thyra::tpetraVectorSpace<ST, LO, GO, NT>(explicitCrsOp->getDomainMap ()),
22282237 explicitCrsOp);
0 commit comments