16 #include "Thyra_DefaultDiagonalLinearOp.hpp"
17 #include "Thyra_DefaultAddedLinearOp_def.hpp"
18 #include "Thyra_DefaultIdentityLinearOp_decl.hpp"
20 #include "Teuchos_Time.hpp"
24 #include "Teko_InvModALStrategy.hpp"
25 #include "Teko_ModALPreconditionerFactory.hpp"
28 using Teuchos::rcp_const_cast;
29 using Teuchos::rcp_dynamic_cast;
36 InvModALStrategy::InvModALStrategy()
37 : invFactoryA_(Teuchos::null),
38 invFactoryS_(Teuchos::null),
39 pressureMassMatrix_(Teuchos::null),
45 InvModALStrategy::InvModALStrategy(
const Teuchos::RCP<InverseFactory> &factory)
46 : invFactoryA_(factory),
47 invFactoryS_(factory),
48 pressureMassMatrix_(Teuchos::null),
54 InvModALStrategy::InvModALStrategy(
const Teuchos::RCP<InverseFactory> &invFactoryA,
55 const Teuchos::RCP<InverseFactory> &invFactoryS)
56 : invFactoryA_(invFactoryA),
57 invFactoryS_(invFactoryS),
58 pressureMassMatrix_(Teuchos::null),
64 InvModALStrategy::InvModALStrategy(
const Teuchos::RCP<InverseFactory> &invFactory,
65 LinearOp &pressureMassMatrix)
66 : invFactoryA_(invFactory),
67 invFactoryS_(invFactory),
68 pressureMassMatrix_(pressureMassMatrix),
74 InvModALStrategy::InvModALStrategy(
const Teuchos::RCP<InverseFactory> &invFactoryA,
75 const Teuchos::RCP<InverseFactory> &invFactoryS,
76 LinearOp &pressureMassMatrix)
77 : invFactoryA_(invFactoryA),
78 invFactoryS_(invFactoryS),
79 pressureMassMatrix_(pressureMassMatrix),
85 LinearOp InvModALStrategy::getInvA11p(BlockPreconditionerState &state)
const {
86 return state.getInverse(
"invA11p");
89 LinearOp InvModALStrategy::getInvA22p(BlockPreconditionerState &state)
const {
90 return state.getInverse(
"invA22p");
93 LinearOp InvModALStrategy::getInvA33p(BlockPreconditionerState &state)
const {
94 return state.getInverse(
"invA33p");
97 LinearOp InvModALStrategy::getInvS(BlockPreconditionerState &state)
const {
98 return state.getInverse(
"invS");
102 void InvModALStrategy::setPressureMassMatrix(
const LinearOp &pressureMassMatrix) {
103 pressureMassMatrix_ = pressureMassMatrix;
107 void InvModALStrategy::setGamma(
double gamma) {
108 TEUCHOS_ASSERT(gamma > 0.0);
112 void InvModALStrategy::buildState(
const BlockedLinearOp &alOp,
113 BlockPreconditionerState &state)
const {
114 Teko_DEBUG_SCOPE(
"InvModALStrategy::buildState", 10);
116 ModALPrecondState *modALState =
dynamic_cast<ModALPrecondState *
>(&state);
117 TEUCHOS_ASSERT(modALState != NULL);
120 if (not modALState->isInitialized()) {
121 Teko_DEBUG_EXPR(Teuchos::Time timer(
""));
125 Teko_DEBUG_SCOPE(
"ModAL::buildState: Initializing state object", 1);
126 Teko_DEBUG_EXPR(timer.start(
true));
128 initializeState(alOp, modALState);
130 Teko_DEBUG_EXPR(timer.stop());
131 Teko_DEBUG_MSG(
"ModAL::buildState: BuildOpsTime = " << timer.totalElapsedTime(), 1);
136 Teko_DEBUG_SCOPE(
"ModAL::buildState: Computing inverses", 1);
137 Teko_DEBUG_EXPR(timer.start(
true));
139 computeInverses(alOp, modALState);
141 Teko_DEBUG_EXPR(timer.stop());
142 Teko_DEBUG_MSG(
"ModAL::buildState: BuildInvTime = " << timer.totalElapsedTime(), 1);
148 void InvModALStrategy::initializeState(
const BlockedLinearOp &alOp,
149 ModALPrecondState *state)
const {
150 Teko_DEBUG_SCOPE(
"InvModALStrategy::initializeState", 10);
153 int dim = blockRowCount(alOp) - 1;
154 TEUCHOS_ASSERT(dim == 2 || dim == 3);
156 LinearOp lpA11 = getBlock(0, 0, alOp);
157 LinearOp lpA22 = getBlock(1, 1, alOp);
158 LinearOp lpA33, lpB1, lpB2, lpB3, lpB1t, lpB2t, lpB3t, lpC;
162 lpB1 = getBlock(2, 0, alOp);
163 lpB2 = getBlock(2, 1, alOp);
164 lpB1t = getBlock(0, 2, alOp);
165 lpB2t = getBlock(1, 2, alOp);
166 lpC = getBlock(2, 2, alOp);
170 lpA33 = getBlock(2, 2, alOp);
171 lpB1 = getBlock(3, 0, alOp);
172 lpB2 = getBlock(3, 1, alOp);
173 lpB3 = getBlock(3, 2, alOp);
174 lpB1t = getBlock(0, 3, alOp);
175 lpB2t = getBlock(1, 3, alOp);
176 lpB3t = getBlock(2, 3, alOp);
177 lpC = getBlock(3, 3, alOp);
182 LinearOp B1t = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpB1t))->getOp(0);
183 LinearOp B2t = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpB2t))->getOp(0);
186 B3t = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpB3t))->getOp(0);
191 state->isStabilized_ = (not isZeroOp(lpC));
195 state->pressureMassMatrix_ = pressureMassMatrix_;
197 if (state->pressureMassMatrix_ == Teuchos::null) {
198 Teko_DEBUG_MSG(
"InvModALStrategy::initializeState(): Build identity type \""
199 << getDiagonalName(scaleType_) <<
"\"",
201 state->invPressureMassMatrix_ = Thyra::identity<double>(lpB1->range());
205 else if (state->invPressureMassMatrix_ == Teuchos::null) {
206 Teko_DEBUG_MSG(
"ModAL::initializeState(): Build Scaling <mass> type \""
207 << getDiagonalName(scaleType_) <<
"\"",
209 state->invPressureMassMatrix_ = getInvDiagonalOp(pressureMassMatrix_, scaleType_);
212 state->gamma_ = gamma_;
214 std::cout << Teuchos::describe(*(state->invPressureMassMatrix_), Teuchos::VERB_EXTREME)
219 if (state->B1tMpB1_ == Teuchos::null)
220 state->B1tMpB1_ = explicitMultiply(B1t, state->invPressureMassMatrix_, lpB1, state->B1tMpB1_);
224 LinearOp A11 = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpA11))->getOp(0);
225 state->A11p_ = explicitAdd(A11, scale(state->gamma_, state->B1tMpB1_), state->A11p_);
227 Teko_DEBUG_MSG(
"Computed A11p", 10);
229 if (state->B2tMpB2_ == Teuchos::null)
230 state->B2tMpB2_ = explicitMultiply(B2t, state->invPressureMassMatrix_, lpB2, state->B2tMpB2_);
231 LinearOp A22 = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpA22))->getOp(0);
232 state->A22p_ = explicitAdd(A22, scale(state->gamma_, state->B2tMpB2_), state->A22p_);
233 Teko_DEBUG_MSG(
"Computed A22p", 10);
236 if (state->B3tMpB3_ == Teuchos::null)
237 state->B3tMpB3_ = explicitMultiply(B3t, state->invPressureMassMatrix_, lpB3, state->B3tMpB3_);
238 LinearOp A33 = (rcp_dynamic_cast<
const Thyra::DefaultAddedLinearOp<double> >(lpA33))->getOp(0);
239 state->A33p_ = explicitAdd(A33, scale(state->gamma_, state->B3tMpB3_), state->A33p_);
240 Teko_DEBUG_MSG(
"Computed A33p", 10);
244 if (state->isStabilized_) {
245 if (state->S_ == Teuchos::null) {
247 explicitAdd(scale(-1.0, lpC), scale(1.0 / state->gamma_, pressureMassMatrix_), state->S_);
249 Teko_DEBUG_MSG(
"Computed S", 10);
252 state->setInitialized(
true);
256 void InvModALStrategy::computeInverses(
const BlockedLinearOp &alOp,
257 ModALPrecondState *state)
const {
258 int dim = blockRowCount(alOp) - 1;
259 TEUCHOS_ASSERT(dim == 2 || dim == 3);
261 Teko_DEBUG_SCOPE(
"InvModALStrategy::computeInverses", 10);
262 Teko_DEBUG_EXPR(Teuchos::Time invTimer(
""));
265 Teko_DEBUG_MSG(
"ModAL::computeInverses(): Building inv(A11)", 1);
266 Teko_DEBUG_EXPR(invTimer.start(
true));
268 InverseLinearOp invA11p = state->getInverse(
"invA11p");
269 if (invA11p == Teuchos::null) {
271 state->addInverse(
"invA11p", invA11p);
276 Teko_DEBUG_EXPR(invTimer.stop());
277 Teko_DEBUG_MSG(
"ModAL::computeInverses GetInvA11 = " << invTimer.totalElapsedTime(), 1);
280 Teko_DEBUG_MSG(
"ModAL::computeInverses(): Building inv(A22)", 2);
281 Teko_DEBUG_EXPR(invTimer.start(
true));
283 InverseLinearOp invA22p = state->getInverse(
"invA22p");
284 if (invA22p == Teuchos::null) {
286 state->addInverse(
"invA22p", invA22p);
291 Teko_DEBUG_EXPR(invTimer.stop());
292 Teko_DEBUG_MSG(
"ModAL::computeInverses(): GetInvA22 = " << invTimer.totalElapsedTime(), 2);
296 Teko_DEBUG_MSG(
"ModAL::computeInverses Building inv(A33)", 3);
297 Teko_DEBUG_EXPR(invTimer.start(
true));
299 InverseLinearOp invA33p = state->getInverse(
"invA33p");
300 if (invA33p == Teuchos::null) {
302 state->addInverse(
"invA33p", invA33p);
307 Teko_DEBUG_EXPR(invTimer.stop());
308 Teko_DEBUG_MSG(
"ModAL::computeInverses GetInvA33 = " << invTimer.totalElapsedTime(), 3);
312 Teko_DEBUG_MSG(
"ModAL::computeInverses Building inv(S)", 4);
313 Teko_DEBUG_EXPR(invTimer.start(
true));
319 if (state->isStabilized_) {
320 InverseLinearOp invS = state->getInverse(
"invS");
321 if (invS == Teuchos::null) {
323 state->addInverse(
"invS", invS);
329 Teko_DEBUG_EXPR(invTimer.stop());
330 Teko_DEBUG_MSG(
"ModAL::computeInverses GetInvS = " << invTimer.totalElapsedTime(), 4);
void rebuildInverse(const InverseFactory &factory, const LinearOp &A, InverseLinearOp &invA)
InverseLinearOp buildInverse(const InverseFactory &factory, const LinearOp &A)
Build an inverse operator using a factory and a linear operator.