MueLu
Version of the Day
Toggle main menu visibility
Loading...
Searching...
No Matches
MueLu_MergedSmoother_def.hpp
Go to the documentation of this file.
1
// @HEADER
2
// *****************************************************************************
3
// MueLu: A package for multigrid based preconditioning
4
//
5
// Copyright 2012 NTESS and the MueLu contributors.
6
// SPDX-License-Identifier: BSD-3-Clause
7
// *****************************************************************************
8
// @HEADER
9
10
#ifndef MUELU_MERGEDSMOOTHER_DEF_HPP
11
#define MUELU_MERGEDSMOOTHER_DEF_HPP
12
13
#include "
MueLu_MergedSmoother_decl.hpp
"
14
#include "
MueLu_Exceptions.hpp
"
15
16
namespace
MueLu
{
17
18
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
19
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MergedSmoother
(ArrayRCP<RCP<SmootherPrototype> >& smootherList,
bool
verbose)
20
:
smootherList_
(smootherList)
21
,
reverseOrder_
(false)
22
,
verbose_
(verbose) {
23
// TODO: check that on each method TEUCHOS_TEST_FOR_EXCEPTION(smootherList == Teuchos::null, MueLu::Exceptions::RuntimeError, "");
24
25
SmootherPrototype::IsSetup
(
false
);
26
}
27
28
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
29
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::MergedSmoother
(
const
MergedSmoother
& src)
30
:
reverseOrder_
(src.
reverseOrder_
)
31
,
verbose_
(src.
verbose_
) {
32
// Deep copy of src.smootherList_
33
smootherList_
=
SmootherListDeepCopy
(src.
GetSmootherList
());
34
}
35
36
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
37
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::SetFactory
(
const
std::string& varName,
const
RCP<const FactoryBase>& factory) {
38
// We need to propagate SetFactory to proper place
39
for
(
typename
ArrayView<RCP<SmootherPrototype> >::iterator it =
smootherList_
.begin(); it !=
smootherList_
.end(); it++)
40
(*it)->SetFactory(varName, factory);
41
}
42
43
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
44
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::DeclareInput
(
Level
& currentLevel)
const
{
45
for
(
typename
ArrayView<RCP<SmootherPrototype> >::iterator it =
smootherList_
.begin(); it !=
smootherList_
.end(); ++it)
46
(*it)->DeclareInput(currentLevel);
47
}
48
49
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
50
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Setup
(
Level
& level) {
51
if
(
SmootherPrototype::IsSetup
() ==
true
)
52
this->
GetOStream
(
Warnings0
) <<
"MueLu::MergedSmoother::Setup(): Setup() has already been called"
;
53
54
for
(
typename
ArrayView<RCP<SmootherPrototype> >::iterator it =
smootherList_
.begin(); it !=
smootherList_
.end(); ++it) {
55
try
{
56
(*it)->Setup(level);
57
58
}
catch
(
MueLu::Exceptions::RuntimeError
& e) {
59
std::string msg =
"MueLu::MergedSmoother<>::Setup(): Runtime Error.\n One of the underlying smoother throwed the following exception: \n"
;
60
msg += e.what();
61
throw
MueLu::Exceptions::RuntimeError
(msg);
62
}
63
}
64
65
SmootherPrototype::IsSetup
(
true
);
66
}
67
68
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
69
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::Apply
(MultiVector& X,
const
MultiVector& B,
bool
InitialGuessIsZero)
const
{
70
TEUCHOS_TEST_FOR_EXCEPTION(
SmootherPrototype::IsSetup
() ==
false
,
MueLu::Exceptions::RuntimeError
,
"MueLu::MergedSmoother<>:Apply(): Setup() has not been called"
);
71
72
typedef
typename
ArrayRCP<RCP<SmootherPrototype> >::size_type sz_t;
73
sz_t n =
smootherList_
.size(), c = (
reverseOrder_
? n - 1 : 0);
74
char
d = (
reverseOrder_
? -1 : 1);
75
76
for
(sz_t i = 0; i < n; i++)
// loop unifying both forward and reverse order
77
try
{
78
// Be careful with nonnegative numbers
79
smootherList_
[c + d * Teuchos::as<char>(i)]->Apply(X, B, InitialGuessIsZero);
80
81
// For second and later iterations, initial guess = previous result
82
InitialGuessIsZero =
false
;
83
84
}
catch
(
MueLu::Exceptions::RuntimeError
& e) {
85
std::string msg =
"MueLu::MergedSmoother<>::Apply(): Runtime Error. One of the underlying smoothers throws the following exception: \n"
;
86
msg += e.what();
87
throw
MueLu::Exceptions::RuntimeError
(msg);
88
}
89
}
90
91
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
92
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::print
(Teuchos::FancyOStream&
/* out */
,
const
VerbLevel
/* verbLevel */
)
const
{
93
throw
Exceptions::NotImplemented
(
"MueLu::MergedSmoother<>::Print() is not implemented"
);
94
}
95
96
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
97
void
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::CopyParameters
(RCP<SmootherPrototype> src) {
// TODO: wrong prototype. We do not need an RCP here.
98
RCP<MergedSmoother> srcMergedSmoother = rcp_dynamic_cast<MergedSmoother>(src);
// TODO: check if dynamic cast fails
99
100
reverseOrder_
= srcMergedSmoother->GetReverseOrder();
101
102
{
103
const
ArrayRCP<const RCP<SmootherPrototype> >& srcSmootherList = srcMergedSmoother->GetSmootherList();
104
const
ArrayRCP<const RCP<SmootherPrototype> >& thisSmootherList =
smootherList_
;
105
TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList == Teuchos::null,
MueLu::Exceptions::RuntimeError
,
// might be allowed later if useful
106
"MueLu::MergedSmoother<>:CopyParameters(): thisSmootherList == Teuchos::null"
);
107
108
// If the smootherList of 'this' and 'src' contains the same type of smoothers,
109
// we can transfert parameters from src to 'this' in order to tentatively reuse
110
// the current setup information of each smoothers. Note that the reuse of the
111
// setup phase of the MergedSmoother 'src' can be implemented for a larger set
112
// of cases (and more complicated cases), but it does not seems useful for now.
113
114
bool
reuse =
true
;
// true == can we transfert parameters of smoothers one by one or do we have to copy the whole list of src?
115
116
// test 1: same list size
117
reuse = reuse && (thisSmootherList.size() == srcSmootherList.size());
118
119
if
(reuse) {
120
// test 2: one-by-one comparison of smoother types
121
for
(
typename
ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
122
// The following test should never throw in our use cases because 'src' is a prototype and
123
// 'this' is a real smoother so they don't share any data. We may allow such case later if useful.
124
TEUCHOS_TEST_FOR_EXCEPTION((thisSmootherList[i] == srcSmootherList[i]) && (thisSmootherList[i] != Teuchos::null),
MueLu::Exceptions::RuntimeError
,
125
"MueLu::MergedSmoother<>:CopyParameters(): internal logic error"
);
126
127
// TODO
128
// reuse = reuse && ((thisSmootherList[i] == Teuchos::null && srcSmootherList[i] == Teuchos::null) ||
129
// thisSmootherList[i]->GetType() == srcSmootherList[i]->GetType());
130
}
131
}
132
133
reuse =
false
;
// TODO: temporary disactivated.
134
135
if
(reuse) {
136
bool
isSetup =
true
;
137
138
// Call CopyParameters for each smoothers and update IsSetup status of the MergedSmoother
139
for
(
typename
ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++) {
140
if
(srcSmootherList[i] != Teuchos::null) {
141
TEUCHOS_TEST_FOR_EXCEPTION(srcSmootherList[i] == Teuchos::null,
MueLu::Exceptions::RuntimeError
,
"MueLu::MergedSmoother<>:CopyParameters(): internal logic error"
);
142
143
// TODO thisSmootherList[i]->CopyParameters(srcSmootherList[i]);
144
isSetup = isSetup && thisSmootherList[i]->IsSetup();
145
}
146
SmootherPrototype::IsSetup
(isSetup);
147
}
148
149
}
else
{
150
// No reuse: copy srcSmootherList.
151
smootherList_
= Teuchos::null;
152
smootherList_
=
SmootherListDeepCopy
(srcSmootherList);
153
SmootherPrototype::IsSetup
(
false
);
154
}
155
}
156
}
157
158
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
159
RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> >
160
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
161
Copy
()
const
{
162
return
rcp(
new
MergedSmoother
(*
this
));
163
}
164
165
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
166
ArrayRCP<RCP<MueLu::SmootherPrototype<Scalar, LocalOrdinal, GlobalOrdinal, Node> > >
167
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
168
SmootherListDeepCopy
(
const
ArrayRCP<
const
RCP<SmootherPrototype> >& srcSmootherList) {
169
ArrayRCP<RCP<SmootherPrototype> > newSmootherList(srcSmootherList.size());
170
171
for
(
typename
ArrayRCP<RCP<SmootherPrototype> >::size_type i = 0; i < srcSmootherList.size(); i++)
172
newSmootherList[i] = srcSmootherList[i]->
Copy
();
173
174
return
newSmootherList;
175
}
176
177
template
<
class
Scalar,
class
LocalOrdinal,
class
GlobalOrdinal,
class
Node>
178
size_t
MergedSmoother<Scalar, LocalOrdinal, GlobalOrdinal, Node>::
179
getNodeSmootherComplexity
()
const
{
180
// FIXME: This is a placeholder
181
return
Teuchos::OrdinalTraits<size_t>::invalid();
182
}
183
184
}
// namespace MueLu
185
186
#endif
// MUELU_MERGEDSMOOTHER_DEF_HPP
MueLu_Exceptions.hpp
MueLu_MergedSmoother_decl.hpp
MueLu::Exceptions::NotImplemented
Exception throws when you call an unimplemented method of MueLu.
Definition
MueLu_Exceptions.hpp:29
MueLu::Exceptions::RuntimeError
Exception throws to report errors in the internal logical of the program.
Definition
MueLu_Exceptions.hpp:36
MueLu::Level
Class that holds all level-specific information.
Definition
MueLu_Level.hpp:63
MueLu::MergedSmoother::getNodeSmootherComplexity
size_t getNodeSmootherComplexity() const
Get a rough estimate of cost per iteration.
Definition
MueLu_MergedSmoother_def.hpp:179
MueLu::MergedSmoother::Copy
RCP< SmootherPrototype > Copy() const
Copy method (performs a deep copy of input object).
Definition
MueLu_MergedSmoother_def.hpp:161
MueLu::MergedSmoother::DeclareInput
void DeclareInput(Level ¤tLevel) const
Input.
Definition
MueLu_MergedSmoother_def.hpp:44
MueLu::MergedSmoother::print
void print(Teuchos::FancyOStream &out, const VerbLevel verbLevel=Default) const
Definition
MueLu_MergedSmoother_def.hpp:92
MueLu::MergedSmoother::verbose_
bool verbose_
Definition
MueLu_MergedSmoother_decl.hpp:108
MueLu::MergedSmoother::smootherList_
ArrayRCP< RCP< SmootherPrototype > > smootherList_
Definition
MueLu_MergedSmoother_decl.hpp:102
MueLu::MergedSmoother::SmootherListDeepCopy
ArrayRCP< RCP< SmootherPrototype > > SmootherListDeepCopy(const ArrayRCP< const RCP< SmootherPrototype > > &srcSmootherList)
Definition
MueLu_MergedSmoother_def.hpp:168
MueLu::MergedSmoother::Setup
void Setup(Level &level)
Set up.
Definition
MueLu_MergedSmoother_def.hpp:50
MueLu::MergedSmoother::reverseOrder_
bool reverseOrder_
Definition
MueLu_MergedSmoother_decl.hpp:105
MueLu::MergedSmoother::SetFactory
void SetFactory(const std::string &varName, const RCP< const FactoryBase > &factory)
Custom SetFactory.
Definition
MueLu_MergedSmoother_def.hpp:37
MueLu::MergedSmoother::Apply
void Apply(MultiVector &X, const MultiVector &B, bool InitialGuessIsZero=false) const
Apply.
Definition
MueLu_MergedSmoother_def.hpp:69
MueLu::MergedSmoother::MergedSmoother
MergedSmoother(ArrayRCP< RCP< SmootherPrototype > > &smootherList, bool verbose=false)
Constructor.
Definition
MueLu_MergedSmoother_def.hpp:19
MueLu::MergedSmoother::CopyParameters
void CopyParameters(RCP< SmootherPrototype > src)
Definition
MueLu_MergedSmoother_def.hpp:97
MueLu::MergedSmoother::GetSmootherList
const ArrayRCP< const RCP< SmootherPrototype > > GetSmootherList() const
Definition
MueLu_MergedSmoother_decl.hpp:60
MueLu::SmootherPrototype::IsSetup
bool IsSetup() const
Get the state of a smoother prototype.
Definition
MueLu_SmootherPrototype_def.hpp:25
MueLu::VerboseObject::GetOStream
Teuchos::FancyOStream & GetOStream(MsgType type, int thisProcRankOnly=0) const
Get an output stream for outputting the input message type.
Definition
MueLu_VerboseObject.cpp:72
MueLu
Namespace for MueLu classes and methods.
Definition
MueLu_BrickAggregationFactory_decl.hpp:42
MueLu::VerbLevel
int VerbLevel
Definition
MueLu_VerbosityLevel.hpp:76
MueLu::Warnings0
@ Warnings0
Important warning messages (one line).
Definition
MueLu_VerbosityLevel.hpp:20
src
Smoothers
MueLu_MergedSmoother_def.hpp
Generated by
1.17.0